| 1 | /* |
| 2 | * ACPI Sony Notebook Control Driver (SNC and SPIC) |
| 3 | * |
| 4 | * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net> |
| 5 | * Copyright (C) 2007-2009 Mattia Dongili <malattia@linux.it> |
| 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 | * The SNY6001 driver part is based on the sonypi driver which includes |
| 11 | * material from: |
| 12 | * |
| 13 | * Copyright (C) 2001-2005 Stelian Pop <stelian@popies.net> |
| 14 | * |
| 15 | * Copyright (C) 2005 Narayanan R S <nars@kadamba.org> |
| 16 | * |
| 17 | * Copyright (C) 2001-2002 AlcĂ´ve <www.alcove.com> |
| 18 | * |
| 19 | * Copyright (C) 2001 Michael Ashley <m.ashley@unsw.edu.au> |
| 20 | * |
| 21 | * Copyright (C) 2001 Junichi Morita <jun1m@mars.dti.ne.jp> |
| 22 | * |
| 23 | * Copyright (C) 2000 Takaya Kinjo <t-kinjo@tc4.so-net.ne.jp> |
| 24 | * |
| 25 | * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com> |
| 26 | * |
| 27 | * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras. |
| 28 | * |
| 29 | * This program is free software; you can redistribute it and/or modify |
| 30 | * it under the terms of the GNU General Public License as published by |
| 31 | * the Free Software Foundation; either version 2 of the License, or |
| 32 | * (at your option) any later version. |
| 33 | * |
| 34 | * This program is distributed in the hope that it will be useful, |
| 35 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 36 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 37 | * GNU General Public License for more details. |
| 38 | * |
| 39 | * You should have received a copy of the GNU General Public License |
| 40 | * along with this program; if not, write to the Free Software |
| 41 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 42 | * |
| 43 | */ |
| 44 | |
| 45 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 46 | |
| 47 | #include <linux/kernel.h> |
| 48 | #include <linux/module.h> |
| 49 | #include <linux/moduleparam.h> |
| 50 | #include <linux/init.h> |
| 51 | #include <linux/types.h> |
| 52 | #include <linux/backlight.h> |
| 53 | #include <linux/platform_device.h> |
| 54 | #include <linux/err.h> |
| 55 | #include <linux/dmi.h> |
| 56 | #include <linux/pci.h> |
| 57 | #include <linux/interrupt.h> |
| 58 | #include <linux/delay.h> |
| 59 | #include <linux/input.h> |
| 60 | #include <linux/kfifo.h> |
| 61 | #include <linux/workqueue.h> |
| 62 | #include <linux/acpi.h> |
| 63 | #include <linux/slab.h> |
| 64 | #include <acpi/acpi_drivers.h> |
| 65 | #include <acpi/acpi_bus.h> |
| 66 | #include <asm/uaccess.h> |
| 67 | #include <linux/sonypi.h> |
| 68 | #include <linux/sony-laptop.h> |
| 69 | #include <linux/rfkill.h> |
| 70 | #ifdef CONFIG_SONYPI_COMPAT |
| 71 | #include <linux/poll.h> |
| 72 | #include <linux/miscdevice.h> |
| 73 | #endif |
| 74 | |
| 75 | #define dprintk(fmt, ...) \ |
| 76 | do { \ |
| 77 | if (debug) \ |
| 78 | pr_warn(fmt, ##__VA_ARGS__); \ |
| 79 | } while (0) |
| 80 | |
| 81 | #define SONY_LAPTOP_DRIVER_VERSION "0.6" |
| 82 | |
| 83 | #define SONY_NC_CLASS "sony-nc" |
| 84 | #define SONY_NC_HID "SNY5001" |
| 85 | #define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver" |
| 86 | |
| 87 | #define SONY_PIC_CLASS "sony-pic" |
| 88 | #define SONY_PIC_HID "SNY6001" |
| 89 | #define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver" |
| 90 | |
| 91 | MODULE_AUTHOR("Stelian Pop, Mattia Dongili"); |
| 92 | MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)"); |
| 93 | MODULE_LICENSE("GPL"); |
| 94 | MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION); |
| 95 | |
| 96 | static int debug; |
| 97 | module_param(debug, int, 0); |
| 98 | MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help " |
| 99 | "the development of this driver"); |
| 100 | |
| 101 | static int no_spic; /* = 0 */ |
| 102 | module_param(no_spic, int, 0444); |
| 103 | MODULE_PARM_DESC(no_spic, |
| 104 | "set this if you don't want to enable the SPIC device"); |
| 105 | |
| 106 | static int compat; /* = 0 */ |
| 107 | module_param(compat, int, 0444); |
| 108 | MODULE_PARM_DESC(compat, |
| 109 | "set this if you want to enable backward compatibility mode"); |
| 110 | |
| 111 | static unsigned long mask = 0xffffffff; |
| 112 | module_param(mask, ulong, 0644); |
| 113 | MODULE_PARM_DESC(mask, |
| 114 | "set this to the mask of event you want to enable (see doc)"); |
| 115 | |
| 116 | static int camera; /* = 0 */ |
| 117 | module_param(camera, int, 0444); |
| 118 | MODULE_PARM_DESC(camera, |
| 119 | "set this to 1 to enable Motion Eye camera controls " |
| 120 | "(only use it if you have a C1VE or C1VN model)"); |
| 121 | |
| 122 | #ifdef CONFIG_SONYPI_COMPAT |
| 123 | static int minor = -1; |
| 124 | module_param(minor, int, 0); |
| 125 | MODULE_PARM_DESC(minor, |
| 126 | "minor number of the misc device for the SPIC compatibility code, " |
| 127 | "default is -1 (automatic)"); |
| 128 | #endif |
| 129 | |
| 130 | static int kbd_backlight = 1; |
| 131 | module_param(kbd_backlight, int, 0444); |
| 132 | MODULE_PARM_DESC(kbd_backlight, |
| 133 | "set this to 0 to disable keyboard backlight, " |
| 134 | "1 to enable it (default: 0)"); |
| 135 | |
| 136 | static int kbd_backlight_timeout; /* = 0 */ |
| 137 | module_param(kbd_backlight_timeout, int, 0444); |
| 138 | MODULE_PARM_DESC(kbd_backlight_timeout, |
| 139 | "set this to 0 to set the default 10 seconds timeout, " |
| 140 | "1 for 30 seconds, 2 for 60 seconds and 3 to disable timeout " |
| 141 | "(default: 0)"); |
| 142 | |
| 143 | static void sony_nc_kbd_backlight_resume(void); |
| 144 | static void sony_nc_kbd_backlight_setup(struct platform_device *pd); |
| 145 | static int sony_nc_kbd_backlight_cleanup(struct platform_device *pd); |
| 146 | |
| 147 | static int sony_nc_battery_care_setup(struct platform_device *pd, |
| 148 | unsigned int handle); |
| 149 | static void sony_nc_battery_care_cleanup(struct platform_device *pd); |
| 150 | |
| 151 | static int sony_nc_thermal_setup(struct platform_device *pd); |
| 152 | static void sony_nc_thermal_cleanup(struct platform_device *pd); |
| 153 | static void sony_nc_thermal_resume(void); |
| 154 | |
| 155 | enum sony_nc_rfkill { |
| 156 | SONY_WIFI, |
| 157 | SONY_BLUETOOTH, |
| 158 | SONY_WWAN, |
| 159 | SONY_WIMAX, |
| 160 | N_SONY_RFKILL, |
| 161 | }; |
| 162 | |
| 163 | static int sony_rfkill_handle; |
| 164 | static struct rfkill *sony_rfkill_devices[N_SONY_RFKILL]; |
| 165 | static int sony_rfkill_address[N_SONY_RFKILL] = {0x300, 0x500, 0x700, 0x900}; |
| 166 | static void sony_nc_rfkill_setup(struct acpi_device *device); |
| 167 | static void sony_nc_rfkill_cleanup(void); |
| 168 | static void sony_nc_rfkill_update(void); |
| 169 | |
| 170 | /*********** Input Devices ***********/ |
| 171 | |
| 172 | #define SONY_LAPTOP_BUF_SIZE 128 |
| 173 | struct sony_laptop_input_s { |
| 174 | atomic_t users; |
| 175 | struct input_dev *jog_dev; |
| 176 | struct input_dev *key_dev; |
| 177 | struct kfifo fifo; |
| 178 | spinlock_t fifo_lock; |
| 179 | struct timer_list release_key_timer; |
| 180 | }; |
| 181 | |
| 182 | static struct sony_laptop_input_s sony_laptop_input = { |
| 183 | .users = ATOMIC_INIT(0), |
| 184 | }; |
| 185 | |
| 186 | struct sony_laptop_keypress { |
| 187 | struct input_dev *dev; |
| 188 | int key; |
| 189 | }; |
| 190 | |
| 191 | /* Correspondance table between sonypi events |
| 192 | * and input layer indexes in the keymap |
| 193 | */ |
| 194 | static int sony_laptop_input_index[] = { |
| 195 | -1, /* 0 no event */ |
| 196 | -1, /* 1 SONYPI_EVENT_JOGDIAL_DOWN */ |
| 197 | -1, /* 2 SONYPI_EVENT_JOGDIAL_UP */ |
| 198 | -1, /* 3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */ |
| 199 | -1, /* 4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */ |
| 200 | -1, /* 5 SONYPI_EVENT_JOGDIAL_PRESSED */ |
| 201 | -1, /* 6 SONYPI_EVENT_JOGDIAL_RELEASED */ |
| 202 | 0, /* 7 SONYPI_EVENT_CAPTURE_PRESSED */ |
| 203 | 1, /* 8 SONYPI_EVENT_CAPTURE_RELEASED */ |
| 204 | 2, /* 9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */ |
| 205 | 3, /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */ |
| 206 | 4, /* 11 SONYPI_EVENT_FNKEY_ESC */ |
| 207 | 5, /* 12 SONYPI_EVENT_FNKEY_F1 */ |
| 208 | 6, /* 13 SONYPI_EVENT_FNKEY_F2 */ |
| 209 | 7, /* 14 SONYPI_EVENT_FNKEY_F3 */ |
| 210 | 8, /* 15 SONYPI_EVENT_FNKEY_F4 */ |
| 211 | 9, /* 16 SONYPI_EVENT_FNKEY_F5 */ |
| 212 | 10, /* 17 SONYPI_EVENT_FNKEY_F6 */ |
| 213 | 11, /* 18 SONYPI_EVENT_FNKEY_F7 */ |
| 214 | 12, /* 19 SONYPI_EVENT_FNKEY_F8 */ |
| 215 | 13, /* 20 SONYPI_EVENT_FNKEY_F9 */ |
| 216 | 14, /* 21 SONYPI_EVENT_FNKEY_F10 */ |
| 217 | 15, /* 22 SONYPI_EVENT_FNKEY_F11 */ |
| 218 | 16, /* 23 SONYPI_EVENT_FNKEY_F12 */ |
| 219 | 17, /* 24 SONYPI_EVENT_FNKEY_1 */ |
| 220 | 18, /* 25 SONYPI_EVENT_FNKEY_2 */ |
| 221 | 19, /* 26 SONYPI_EVENT_FNKEY_D */ |
| 222 | 20, /* 27 SONYPI_EVENT_FNKEY_E */ |
| 223 | 21, /* 28 SONYPI_EVENT_FNKEY_F */ |
| 224 | 22, /* 29 SONYPI_EVENT_FNKEY_S */ |
| 225 | 23, /* 30 SONYPI_EVENT_FNKEY_B */ |
| 226 | 24, /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */ |
| 227 | 25, /* 32 SONYPI_EVENT_PKEY_P1 */ |
| 228 | 26, /* 33 SONYPI_EVENT_PKEY_P2 */ |
| 229 | 27, /* 34 SONYPI_EVENT_PKEY_P3 */ |
| 230 | 28, /* 35 SONYPI_EVENT_BACK_PRESSED */ |
| 231 | -1, /* 36 SONYPI_EVENT_LID_CLOSED */ |
| 232 | -1, /* 37 SONYPI_EVENT_LID_OPENED */ |
| 233 | 29, /* 38 SONYPI_EVENT_BLUETOOTH_ON */ |
| 234 | 30, /* 39 SONYPI_EVENT_BLUETOOTH_OFF */ |
| 235 | 31, /* 40 SONYPI_EVENT_HELP_PRESSED */ |
| 236 | 32, /* 41 SONYPI_EVENT_FNKEY_ONLY */ |
| 237 | 33, /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */ |
| 238 | 34, /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */ |
| 239 | 35, /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */ |
| 240 | 36, /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */ |
| 241 | 37, /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */ |
| 242 | 38, /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */ |
| 243 | 39, /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */ |
| 244 | 40, /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */ |
| 245 | 41, /* 50 SONYPI_EVENT_ZOOM_PRESSED */ |
| 246 | 42, /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */ |
| 247 | 43, /* 52 SONYPI_EVENT_MEYE_FACE */ |
| 248 | 44, /* 53 SONYPI_EVENT_MEYE_OPPOSITE */ |
| 249 | 45, /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */ |
| 250 | 46, /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */ |
| 251 | -1, /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */ |
| 252 | -1, /* 57 SONYPI_EVENT_BATTERY_INSERT */ |
| 253 | -1, /* 58 SONYPI_EVENT_BATTERY_REMOVE */ |
| 254 | -1, /* 59 SONYPI_EVENT_FNKEY_RELEASED */ |
| 255 | 47, /* 60 SONYPI_EVENT_WIRELESS_ON */ |
| 256 | 48, /* 61 SONYPI_EVENT_WIRELESS_OFF */ |
| 257 | 49, /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */ |
| 258 | 50, /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */ |
| 259 | 51, /* 64 SONYPI_EVENT_CD_EJECT_PRESSED */ |
| 260 | 52, /* 65 SONYPI_EVENT_MODEKEY_PRESSED */ |
| 261 | 53, /* 66 SONYPI_EVENT_PKEY_P4 */ |
| 262 | 54, /* 67 SONYPI_EVENT_PKEY_P5 */ |
| 263 | 55, /* 68 SONYPI_EVENT_SETTINGKEY_PRESSED */ |
| 264 | 56, /* 69 SONYPI_EVENT_VOLUME_INC_PRESSED */ |
| 265 | 57, /* 70 SONYPI_EVENT_VOLUME_DEC_PRESSED */ |
| 266 | -1, /* 71 SONYPI_EVENT_BRIGHTNESS_PRESSED */ |
| 267 | 58, /* 72 SONYPI_EVENT_MEDIA_PRESSED */ |
| 268 | 59, /* 72 SONYPI_EVENT_VENDOR_PRESSED */ |
| 269 | }; |
| 270 | |
| 271 | static int sony_laptop_input_keycode_map[] = { |
| 272 | KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */ |
| 273 | KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */ |
| 274 | KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */ |
| 275 | KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */ |
| 276 | KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */ |
| 277 | KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */ |
| 278 | KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */ |
| 279 | KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */ |
| 280 | KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */ |
| 281 | KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */ |
| 282 | KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */ |
| 283 | KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */ |
| 284 | KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */ |
| 285 | KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */ |
| 286 | KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */ |
| 287 | KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */ |
| 288 | KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */ |
| 289 | KEY_FN_F1, /* 17 SONYPI_EVENT_FNKEY_1 */ |
| 290 | KEY_FN_F2, /* 18 SONYPI_EVENT_FNKEY_2 */ |
| 291 | KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */ |
| 292 | KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */ |
| 293 | KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */ |
| 294 | KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */ |
| 295 | KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */ |
| 296 | KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */ |
| 297 | KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */ |
| 298 | KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */ |
| 299 | KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */ |
| 300 | KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */ |
| 301 | KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */ |
| 302 | KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */ |
| 303 | KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */ |
| 304 | KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */ |
| 305 | KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */ |
| 306 | KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */ |
| 307 | KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */ |
| 308 | KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */ |
| 309 | KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */ |
| 310 | KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */ |
| 311 | KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */ |
| 312 | KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */ |
| 313 | KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */ |
| 314 | BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */ |
| 315 | KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */ |
| 316 | KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */ |
| 317 | KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */ |
| 318 | KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */ |
| 319 | KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */ |
| 320 | KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */ |
| 321 | KEY_ZOOMIN, /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */ |
| 322 | KEY_ZOOMOUT, /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */ |
| 323 | KEY_EJECTCD, /* 51 SONYPI_EVENT_CD_EJECT_PRESSED */ |
| 324 | KEY_F13, /* 52 SONYPI_EVENT_MODEKEY_PRESSED */ |
| 325 | KEY_PROG4, /* 53 SONYPI_EVENT_PKEY_P4 */ |
| 326 | KEY_F14, /* 54 SONYPI_EVENT_PKEY_P5 */ |
| 327 | KEY_F15, /* 55 SONYPI_EVENT_SETTINGKEY_PRESSED */ |
| 328 | KEY_VOLUMEUP, /* 56 SONYPI_EVENT_VOLUME_INC_PRESSED */ |
| 329 | KEY_VOLUMEDOWN, /* 57 SONYPI_EVENT_VOLUME_DEC_PRESSED */ |
| 330 | KEY_MEDIA, /* 58 SONYPI_EVENT_MEDIA_PRESSED */ |
| 331 | KEY_VENDOR, /* 59 SONYPI_EVENT_VENDOR_PRESSED */ |
| 332 | }; |
| 333 | |
| 334 | /* release buttons after a short delay if pressed */ |
| 335 | static void do_sony_laptop_release_key(unsigned long unused) |
| 336 | { |
| 337 | struct sony_laptop_keypress kp; |
| 338 | unsigned long flags; |
| 339 | |
| 340 | spin_lock_irqsave(&sony_laptop_input.fifo_lock, flags); |
| 341 | |
| 342 | if (kfifo_out(&sony_laptop_input.fifo, |
| 343 | (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) { |
| 344 | input_report_key(kp.dev, kp.key, 0); |
| 345 | input_sync(kp.dev); |
| 346 | } |
| 347 | |
| 348 | /* If there is something in the fifo schedule next release. */ |
| 349 | if (kfifo_len(&sony_laptop_input.fifo) != 0) |
| 350 | mod_timer(&sony_laptop_input.release_key_timer, |
| 351 | jiffies + msecs_to_jiffies(10)); |
| 352 | |
| 353 | spin_unlock_irqrestore(&sony_laptop_input.fifo_lock, flags); |
| 354 | } |
| 355 | |
| 356 | /* forward event to the input subsystem */ |
| 357 | static void sony_laptop_report_input_event(u8 event) |
| 358 | { |
| 359 | struct input_dev *jog_dev = sony_laptop_input.jog_dev; |
| 360 | struct input_dev *key_dev = sony_laptop_input.key_dev; |
| 361 | struct sony_laptop_keypress kp = { NULL }; |
| 362 | int scancode = -1; |
| 363 | |
| 364 | if (event == SONYPI_EVENT_FNKEY_RELEASED || |
| 365 | event == SONYPI_EVENT_ANYBUTTON_RELEASED) { |
| 366 | /* Nothing, not all VAIOs generate this event */ |
| 367 | return; |
| 368 | } |
| 369 | |
| 370 | /* report events */ |
| 371 | switch (event) { |
| 372 | /* jog_dev events */ |
| 373 | case SONYPI_EVENT_JOGDIAL_UP: |
| 374 | case SONYPI_EVENT_JOGDIAL_UP_PRESSED: |
| 375 | input_report_rel(jog_dev, REL_WHEEL, 1); |
| 376 | input_sync(jog_dev); |
| 377 | return; |
| 378 | |
| 379 | case SONYPI_EVENT_JOGDIAL_DOWN: |
| 380 | case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED: |
| 381 | input_report_rel(jog_dev, REL_WHEEL, -1); |
| 382 | input_sync(jog_dev); |
| 383 | return; |
| 384 | |
| 385 | /* key_dev events */ |
| 386 | case SONYPI_EVENT_JOGDIAL_PRESSED: |
| 387 | kp.key = BTN_MIDDLE; |
| 388 | kp.dev = jog_dev; |
| 389 | break; |
| 390 | |
| 391 | default: |
| 392 | if (event >= ARRAY_SIZE(sony_laptop_input_index)) { |
| 393 | dprintk("sony_laptop_report_input_event, event not known: %d\n", event); |
| 394 | break; |
| 395 | } |
| 396 | if ((scancode = sony_laptop_input_index[event]) != -1) { |
| 397 | kp.key = sony_laptop_input_keycode_map[scancode]; |
| 398 | if (kp.key != KEY_UNKNOWN) |
| 399 | kp.dev = key_dev; |
| 400 | } |
| 401 | break; |
| 402 | } |
| 403 | |
| 404 | if (kp.dev) { |
| 405 | /* if we have a scancode we emit it so we can always |
| 406 | remap the key */ |
| 407 | if (scancode != -1) |
| 408 | input_event(kp.dev, EV_MSC, MSC_SCAN, scancode); |
| 409 | input_report_key(kp.dev, kp.key, 1); |
| 410 | input_sync(kp.dev); |
| 411 | |
| 412 | /* schedule key release */ |
| 413 | kfifo_in_locked(&sony_laptop_input.fifo, |
| 414 | (unsigned char *)&kp, sizeof(kp), |
| 415 | &sony_laptop_input.fifo_lock); |
| 416 | mod_timer(&sony_laptop_input.release_key_timer, |
| 417 | jiffies + msecs_to_jiffies(10)); |
| 418 | } else |
| 419 | dprintk("unknown input event %.2x\n", event); |
| 420 | } |
| 421 | |
| 422 | static int sony_laptop_setup_input(struct acpi_device *acpi_device) |
| 423 | { |
| 424 | struct input_dev *jog_dev; |
| 425 | struct input_dev *key_dev; |
| 426 | int i; |
| 427 | int error; |
| 428 | |
| 429 | /* don't run again if already initialized */ |
| 430 | if (atomic_add_return(1, &sony_laptop_input.users) > 1) |
| 431 | return 0; |
| 432 | |
| 433 | /* kfifo */ |
| 434 | spin_lock_init(&sony_laptop_input.fifo_lock); |
| 435 | error = kfifo_alloc(&sony_laptop_input.fifo, |
| 436 | SONY_LAPTOP_BUF_SIZE, GFP_KERNEL); |
| 437 | if (error) { |
| 438 | pr_err("kfifo_alloc failed\n"); |
| 439 | goto err_dec_users; |
| 440 | } |
| 441 | |
| 442 | setup_timer(&sony_laptop_input.release_key_timer, |
| 443 | do_sony_laptop_release_key, 0); |
| 444 | |
| 445 | /* input keys */ |
| 446 | key_dev = input_allocate_device(); |
| 447 | if (!key_dev) { |
| 448 | error = -ENOMEM; |
| 449 | goto err_free_kfifo; |
| 450 | } |
| 451 | |
| 452 | key_dev->name = "Sony Vaio Keys"; |
| 453 | key_dev->id.bustype = BUS_ISA; |
| 454 | key_dev->id.vendor = PCI_VENDOR_ID_SONY; |
| 455 | key_dev->dev.parent = &acpi_device->dev; |
| 456 | |
| 457 | /* Initialize the Input Drivers: special keys */ |
| 458 | input_set_capability(key_dev, EV_MSC, MSC_SCAN); |
| 459 | |
| 460 | __set_bit(EV_KEY, key_dev->evbit); |
| 461 | key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]); |
| 462 | key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map); |
| 463 | key_dev->keycode = &sony_laptop_input_keycode_map; |
| 464 | for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++) |
| 465 | __set_bit(sony_laptop_input_keycode_map[i], key_dev->keybit); |
| 466 | __clear_bit(KEY_RESERVED, key_dev->keybit); |
| 467 | |
| 468 | error = input_register_device(key_dev); |
| 469 | if (error) |
| 470 | goto err_free_keydev; |
| 471 | |
| 472 | sony_laptop_input.key_dev = key_dev; |
| 473 | |
| 474 | /* jogdial */ |
| 475 | jog_dev = input_allocate_device(); |
| 476 | if (!jog_dev) { |
| 477 | error = -ENOMEM; |
| 478 | goto err_unregister_keydev; |
| 479 | } |
| 480 | |
| 481 | jog_dev->name = "Sony Vaio Jogdial"; |
| 482 | jog_dev->id.bustype = BUS_ISA; |
| 483 | jog_dev->id.vendor = PCI_VENDOR_ID_SONY; |
| 484 | jog_dev->dev.parent = &acpi_device->dev; |
| 485 | |
| 486 | input_set_capability(jog_dev, EV_KEY, BTN_MIDDLE); |
| 487 | input_set_capability(jog_dev, EV_REL, REL_WHEEL); |
| 488 | |
| 489 | error = input_register_device(jog_dev); |
| 490 | if (error) |
| 491 | goto err_free_jogdev; |
| 492 | |
| 493 | sony_laptop_input.jog_dev = jog_dev; |
| 494 | |
| 495 | return 0; |
| 496 | |
| 497 | err_free_jogdev: |
| 498 | input_free_device(jog_dev); |
| 499 | |
| 500 | err_unregister_keydev: |
| 501 | input_unregister_device(key_dev); |
| 502 | /* to avoid kref underflow below at input_free_device */ |
| 503 | key_dev = NULL; |
| 504 | |
| 505 | err_free_keydev: |
| 506 | input_free_device(key_dev); |
| 507 | |
| 508 | err_free_kfifo: |
| 509 | kfifo_free(&sony_laptop_input.fifo); |
| 510 | |
| 511 | err_dec_users: |
| 512 | atomic_dec(&sony_laptop_input.users); |
| 513 | return error; |
| 514 | } |
| 515 | |
| 516 | static void sony_laptop_remove_input(void) |
| 517 | { |
| 518 | struct sony_laptop_keypress kp = { NULL }; |
| 519 | |
| 520 | /* Cleanup only after the last user has gone */ |
| 521 | if (!atomic_dec_and_test(&sony_laptop_input.users)) |
| 522 | return; |
| 523 | |
| 524 | del_timer_sync(&sony_laptop_input.release_key_timer); |
| 525 | |
| 526 | /* |
| 527 | * Generate key-up events for remaining keys. Note that we don't |
| 528 | * need locking since nobody is adding new events to the kfifo. |
| 529 | */ |
| 530 | while (kfifo_out(&sony_laptop_input.fifo, |
| 531 | (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) { |
| 532 | input_report_key(kp.dev, kp.key, 0); |
| 533 | input_sync(kp.dev); |
| 534 | } |
| 535 | |
| 536 | /* destroy input devs */ |
| 537 | input_unregister_device(sony_laptop_input.key_dev); |
| 538 | sony_laptop_input.key_dev = NULL; |
| 539 | |
| 540 | if (sony_laptop_input.jog_dev) { |
| 541 | input_unregister_device(sony_laptop_input.jog_dev); |
| 542 | sony_laptop_input.jog_dev = NULL; |
| 543 | } |
| 544 | |
| 545 | kfifo_free(&sony_laptop_input.fifo); |
| 546 | } |
| 547 | |
| 548 | /*********** Platform Device ***********/ |
| 549 | |
| 550 | static atomic_t sony_pf_users = ATOMIC_INIT(0); |
| 551 | static struct platform_driver sony_pf_driver = { |
| 552 | .driver = { |
| 553 | .name = "sony-laptop", |
| 554 | .owner = THIS_MODULE, |
| 555 | } |
| 556 | }; |
| 557 | static struct platform_device *sony_pf_device; |
| 558 | |
| 559 | static int sony_pf_add(void) |
| 560 | { |
| 561 | int ret = 0; |
| 562 | |
| 563 | /* don't run again if already initialized */ |
| 564 | if (atomic_add_return(1, &sony_pf_users) > 1) |
| 565 | return 0; |
| 566 | |
| 567 | ret = platform_driver_register(&sony_pf_driver); |
| 568 | if (ret) |
| 569 | goto out; |
| 570 | |
| 571 | sony_pf_device = platform_device_alloc("sony-laptop", -1); |
| 572 | if (!sony_pf_device) { |
| 573 | ret = -ENOMEM; |
| 574 | goto out_platform_registered; |
| 575 | } |
| 576 | |
| 577 | ret = platform_device_add(sony_pf_device); |
| 578 | if (ret) |
| 579 | goto out_platform_alloced; |
| 580 | |
| 581 | return 0; |
| 582 | |
| 583 | out_platform_alloced: |
| 584 | platform_device_put(sony_pf_device); |
| 585 | sony_pf_device = NULL; |
| 586 | out_platform_registered: |
| 587 | platform_driver_unregister(&sony_pf_driver); |
| 588 | out: |
| 589 | atomic_dec(&sony_pf_users); |
| 590 | return ret; |
| 591 | } |
| 592 | |
| 593 | static void sony_pf_remove(void) |
| 594 | { |
| 595 | /* deregister only after the last user has gone */ |
| 596 | if (!atomic_dec_and_test(&sony_pf_users)) |
| 597 | return; |
| 598 | |
| 599 | platform_device_unregister(sony_pf_device); |
| 600 | platform_driver_unregister(&sony_pf_driver); |
| 601 | } |
| 602 | |
| 603 | /*********** SNC (SNY5001) Device ***********/ |
| 604 | |
| 605 | /* the device uses 1-based values, while the backlight subsystem uses |
| 606 | 0-based values */ |
| 607 | #define SONY_MAX_BRIGHTNESS 8 |
| 608 | |
| 609 | #define SNC_VALIDATE_IN 0 |
| 610 | #define SNC_VALIDATE_OUT 1 |
| 611 | |
| 612 | static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *, |
| 613 | char *); |
| 614 | static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *, |
| 615 | const char *, size_t); |
| 616 | static int boolean_validate(const int, const int); |
| 617 | static int brightness_default_validate(const int, const int); |
| 618 | |
| 619 | struct sony_nc_value { |
| 620 | char *name; /* name of the entry */ |
| 621 | char **acpiget; /* names of the ACPI get function */ |
| 622 | char **acpiset; /* names of the ACPI set function */ |
| 623 | int (*validate)(const int, const int); /* input/output validation */ |
| 624 | int value; /* current setting */ |
| 625 | int valid; /* Has ever been set */ |
| 626 | int debug; /* active only in debug mode ? */ |
| 627 | struct device_attribute devattr; /* sysfs attribute */ |
| 628 | }; |
| 629 | |
| 630 | #define SNC_HANDLE_NAMES(_name, _values...) \ |
| 631 | static char *snc_##_name[] = { _values, NULL } |
| 632 | |
| 633 | #define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \ |
| 634 | { \ |
| 635 | .name = __stringify(_name), \ |
| 636 | .acpiget = _getters, \ |
| 637 | .acpiset = _setters, \ |
| 638 | .validate = _validate, \ |
| 639 | .debug = _debug, \ |
| 640 | .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \ |
| 641 | } |
| 642 | |
| 643 | #define SNC_HANDLE_NULL { .name = NULL } |
| 644 | |
| 645 | SNC_HANDLE_NAMES(fnkey_get, "GHKE"); |
| 646 | |
| 647 | SNC_HANDLE_NAMES(brightness_def_get, "GPBR"); |
| 648 | SNC_HANDLE_NAMES(brightness_def_set, "SPBR"); |
| 649 | |
| 650 | SNC_HANDLE_NAMES(cdpower_get, "GCDP"); |
| 651 | SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW"); |
| 652 | |
| 653 | SNC_HANDLE_NAMES(audiopower_get, "GAZP"); |
| 654 | SNC_HANDLE_NAMES(audiopower_set, "AZPW"); |
| 655 | |
| 656 | SNC_HANDLE_NAMES(lanpower_get, "GLNP"); |
| 657 | SNC_HANDLE_NAMES(lanpower_set, "LNPW"); |
| 658 | |
| 659 | SNC_HANDLE_NAMES(lidstate_get, "GLID"); |
| 660 | |
| 661 | SNC_HANDLE_NAMES(indicatorlamp_get, "GILS"); |
| 662 | SNC_HANDLE_NAMES(indicatorlamp_set, "SILS"); |
| 663 | |
| 664 | SNC_HANDLE_NAMES(gainbass_get, "GMGB"); |
| 665 | SNC_HANDLE_NAMES(gainbass_set, "CMGB"); |
| 666 | |
| 667 | SNC_HANDLE_NAMES(PID_get, "GPID"); |
| 668 | |
| 669 | SNC_HANDLE_NAMES(CTR_get, "GCTR"); |
| 670 | SNC_HANDLE_NAMES(CTR_set, "SCTR"); |
| 671 | |
| 672 | SNC_HANDLE_NAMES(PCR_get, "GPCR"); |
| 673 | SNC_HANDLE_NAMES(PCR_set, "SPCR"); |
| 674 | |
| 675 | SNC_HANDLE_NAMES(CMI_get, "GCMI"); |
| 676 | SNC_HANDLE_NAMES(CMI_set, "SCMI"); |
| 677 | |
| 678 | static struct sony_nc_value sony_nc_values[] = { |
| 679 | SNC_HANDLE(brightness_default, snc_brightness_def_get, |
| 680 | snc_brightness_def_set, brightness_default_validate, 0), |
| 681 | SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0), |
| 682 | SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0), |
| 683 | SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set, |
| 684 | boolean_validate, 0), |
| 685 | SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set, |
| 686 | boolean_validate, 1), |
| 687 | SNC_HANDLE(lidstate, snc_lidstate_get, NULL, |
| 688 | boolean_validate, 0), |
| 689 | SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set, |
| 690 | boolean_validate, 0), |
| 691 | SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set, |
| 692 | boolean_validate, 0), |
| 693 | /* unknown methods */ |
| 694 | SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1), |
| 695 | SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1), |
| 696 | SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1), |
| 697 | SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1), |
| 698 | SNC_HANDLE_NULL |
| 699 | }; |
| 700 | |
| 701 | static acpi_handle sony_nc_acpi_handle; |
| 702 | static struct acpi_device *sony_nc_acpi_device = NULL; |
| 703 | |
| 704 | /* |
| 705 | * acpi_evaluate_object wrappers |
| 706 | * all useful calls into SNC methods take one or zero parameters and return |
| 707 | * integers or arrays. |
| 708 | */ |
| 709 | static union acpi_object *__call_snc_method(acpi_handle handle, char *method, |
| 710 | u64 *value) |
| 711 | { |
| 712 | union acpi_object *result = NULL; |
| 713 | struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 714 | acpi_status status; |
| 715 | |
| 716 | if (value) { |
| 717 | struct acpi_object_list params; |
| 718 | union acpi_object in; |
| 719 | in.type = ACPI_TYPE_INTEGER; |
| 720 | in.integer.value = *value; |
| 721 | params.count = 1; |
| 722 | params.pointer = ∈ |
| 723 | status = acpi_evaluate_object(handle, method, ¶ms, &output); |
| 724 | dprintk("__call_snc_method: [%s:0x%.8x%.8x]\n", method, |
| 725 | (unsigned int)(*value >> 32), |
| 726 | (unsigned int)*value & 0xffffffff); |
| 727 | } else { |
| 728 | status = acpi_evaluate_object(handle, method, NULL, &output); |
| 729 | dprintk("__call_snc_method: [%s]\n", method); |
| 730 | } |
| 731 | |
| 732 | if (ACPI_FAILURE(status)) { |
| 733 | pr_err("Failed to evaluate [%s]\n", method); |
| 734 | return NULL; |
| 735 | } |
| 736 | |
| 737 | result = (union acpi_object *) output.pointer; |
| 738 | if (!result) |
| 739 | dprintk("No return object [%s]\n", method); |
| 740 | |
| 741 | return result; |
| 742 | } |
| 743 | |
| 744 | static int sony_nc_int_call(acpi_handle handle, char *name, int *value, |
| 745 | int *result) |
| 746 | { |
| 747 | union acpi_object *object = NULL; |
| 748 | if (value) { |
| 749 | u64 v = *value; |
| 750 | object = __call_snc_method(handle, name, &v); |
| 751 | } else |
| 752 | object = __call_snc_method(handle, name, NULL); |
| 753 | |
| 754 | if (!object) |
| 755 | return -EINVAL; |
| 756 | |
| 757 | if (object->type != ACPI_TYPE_INTEGER) { |
| 758 | pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n", |
| 759 | ACPI_TYPE_INTEGER, object->type); |
| 760 | kfree(object); |
| 761 | return -EINVAL; |
| 762 | } |
| 763 | |
| 764 | if (result) |
| 765 | *result = object->integer.value; |
| 766 | |
| 767 | kfree(object); |
| 768 | return 0; |
| 769 | } |
| 770 | |
| 771 | #define MIN(a, b) (a > b ? b : a) |
| 772 | static int sony_nc_buffer_call(acpi_handle handle, char *name, u64 *value, |
| 773 | void *buffer, size_t buflen) |
| 774 | { |
| 775 | size_t len = len; |
| 776 | union acpi_object *object = __call_snc_method(handle, name, value); |
| 777 | |
| 778 | if (!object) |
| 779 | return -EINVAL; |
| 780 | |
| 781 | if (object->type == ACPI_TYPE_BUFFER) |
| 782 | len = MIN(buflen, object->buffer.length); |
| 783 | |
| 784 | else if (object->type == ACPI_TYPE_INTEGER) |
| 785 | len = MIN(buflen, sizeof(object->integer.value)); |
| 786 | |
| 787 | else { |
| 788 | pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n", |
| 789 | ACPI_TYPE_BUFFER, object->type); |
| 790 | kfree(object); |
| 791 | return -EINVAL; |
| 792 | } |
| 793 | |
| 794 | memcpy(buffer, object->buffer.pointer, len); |
| 795 | kfree(object); |
| 796 | return 0; |
| 797 | } |
| 798 | |
| 799 | struct sony_nc_handles { |
| 800 | u16 cap[0x10]; |
| 801 | struct device_attribute devattr; |
| 802 | }; |
| 803 | |
| 804 | static struct sony_nc_handles *handles; |
| 805 | |
| 806 | static ssize_t sony_nc_handles_show(struct device *dev, |
| 807 | struct device_attribute *attr, char *buffer) |
| 808 | { |
| 809 | ssize_t len = 0; |
| 810 | int i; |
| 811 | |
| 812 | for (i = 0; i < ARRAY_SIZE(handles->cap); i++) { |
| 813 | len += snprintf(buffer + len, PAGE_SIZE - len, "0x%.4x ", |
| 814 | handles->cap[i]); |
| 815 | } |
| 816 | len += snprintf(buffer + len, PAGE_SIZE - len, "\n"); |
| 817 | |
| 818 | return len; |
| 819 | } |
| 820 | |
| 821 | static int sony_nc_handles_setup(struct platform_device *pd) |
| 822 | { |
| 823 | int i, r, result, arg; |
| 824 | |
| 825 | handles = kzalloc(sizeof(*handles), GFP_KERNEL); |
| 826 | if (!handles) |
| 827 | return -ENOMEM; |
| 828 | |
| 829 | for (i = 0; i < ARRAY_SIZE(handles->cap); i++) { |
| 830 | arg = i + 0x20; |
| 831 | r = sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, |
| 832 | &result); |
| 833 | if (!r) { |
| 834 | dprintk("caching handle 0x%.4x (offset: 0x%.2x)\n", |
| 835 | result, i); |
| 836 | handles->cap[i] = result; |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | if (debug) { |
| 841 | sysfs_attr_init(&handles->devattr.attr); |
| 842 | handles->devattr.attr.name = "handles"; |
| 843 | handles->devattr.attr.mode = S_IRUGO; |
| 844 | handles->devattr.show = sony_nc_handles_show; |
| 845 | |
| 846 | /* allow reading capabilities via sysfs */ |
| 847 | if (device_create_file(&pd->dev, &handles->devattr)) { |
| 848 | kfree(handles); |
| 849 | handles = NULL; |
| 850 | return -1; |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | return 0; |
| 855 | } |
| 856 | |
| 857 | static int sony_nc_handles_cleanup(struct platform_device *pd) |
| 858 | { |
| 859 | if (handles) { |
| 860 | if (debug) |
| 861 | device_remove_file(&pd->dev, &handles->devattr); |
| 862 | kfree(handles); |
| 863 | handles = NULL; |
| 864 | } |
| 865 | return 0; |
| 866 | } |
| 867 | |
| 868 | static int sony_find_snc_handle(int handle) |
| 869 | { |
| 870 | int i; |
| 871 | |
| 872 | /* not initialized yet, return early */ |
| 873 | if (!handles) |
| 874 | return -1; |
| 875 | |
| 876 | for (i = 0; i < 0x10; i++) { |
| 877 | if (handles->cap[i] == handle) { |
| 878 | dprintk("found handle 0x%.4x (offset: 0x%.2x)\n", |
| 879 | handle, i); |
| 880 | return i; |
| 881 | } |
| 882 | } |
| 883 | dprintk("handle 0x%.4x not found\n", handle); |
| 884 | return -1; |
| 885 | } |
| 886 | |
| 887 | static int sony_call_snc_handle(int handle, int argument, int *result) |
| 888 | { |
| 889 | int arg, ret = 0; |
| 890 | int offset = sony_find_snc_handle(handle); |
| 891 | |
| 892 | if (offset < 0) |
| 893 | return -1; |
| 894 | |
| 895 | arg = offset | argument; |
| 896 | ret = sony_nc_int_call(sony_nc_acpi_handle, "SN07", &arg, result); |
| 897 | dprintk("called SN07 with 0x%.4x (result: 0x%.4x)\n", arg, *result); |
| 898 | return ret; |
| 899 | } |
| 900 | |
| 901 | /* |
| 902 | * sony_nc_values input/output validate functions |
| 903 | */ |
| 904 | |
| 905 | /* brightness_default_validate: |
| 906 | * |
| 907 | * manipulate input output values to keep consistency with the |
| 908 | * backlight framework for which brightness values are 0-based. |
| 909 | */ |
| 910 | static int brightness_default_validate(const int direction, const int value) |
| 911 | { |
| 912 | switch (direction) { |
| 913 | case SNC_VALIDATE_OUT: |
| 914 | return value - 1; |
| 915 | case SNC_VALIDATE_IN: |
| 916 | if (value >= 0 && value < SONY_MAX_BRIGHTNESS) |
| 917 | return value + 1; |
| 918 | } |
| 919 | return -EINVAL; |
| 920 | } |
| 921 | |
| 922 | /* boolean_validate: |
| 923 | * |
| 924 | * on input validate boolean values 0/1, on output just pass the |
| 925 | * received value. |
| 926 | */ |
| 927 | static int boolean_validate(const int direction, const int value) |
| 928 | { |
| 929 | if (direction == SNC_VALIDATE_IN) { |
| 930 | if (value != 0 && value != 1) |
| 931 | return -EINVAL; |
| 932 | } |
| 933 | return value; |
| 934 | } |
| 935 | |
| 936 | /* |
| 937 | * Sysfs show/store common to all sony_nc_values |
| 938 | */ |
| 939 | static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr, |
| 940 | char *buffer) |
| 941 | { |
| 942 | int value, ret = 0; |
| 943 | struct sony_nc_value *item = |
| 944 | container_of(attr, struct sony_nc_value, devattr); |
| 945 | |
| 946 | if (!*item->acpiget) |
| 947 | return -EIO; |
| 948 | |
| 949 | ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiget, NULL, |
| 950 | &value); |
| 951 | if (ret < 0) |
| 952 | return -EIO; |
| 953 | |
| 954 | if (item->validate) |
| 955 | value = item->validate(SNC_VALIDATE_OUT, value); |
| 956 | |
| 957 | return snprintf(buffer, PAGE_SIZE, "%d\n", value); |
| 958 | } |
| 959 | |
| 960 | static ssize_t sony_nc_sysfs_store(struct device *dev, |
| 961 | struct device_attribute *attr, |
| 962 | const char *buffer, size_t count) |
| 963 | { |
| 964 | unsigned long value = 0; |
| 965 | int ret = 0; |
| 966 | struct sony_nc_value *item = |
| 967 | container_of(attr, struct sony_nc_value, devattr); |
| 968 | |
| 969 | if (!item->acpiset) |
| 970 | return -EIO; |
| 971 | |
| 972 | if (count > 31) |
| 973 | return -EINVAL; |
| 974 | |
| 975 | if (kstrtoul(buffer, 10, &value)) |
| 976 | return -EINVAL; |
| 977 | |
| 978 | if (item->validate) |
| 979 | value = item->validate(SNC_VALIDATE_IN, value); |
| 980 | |
| 981 | if (value < 0) |
| 982 | return value; |
| 983 | |
| 984 | ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset, |
| 985 | (int *)&value, NULL); |
| 986 | if (ret < 0) |
| 987 | return -EIO; |
| 988 | |
| 989 | item->value = value; |
| 990 | item->valid = 1; |
| 991 | return count; |
| 992 | } |
| 993 | |
| 994 | |
| 995 | /* |
| 996 | * Backlight device |
| 997 | */ |
| 998 | struct sony_backlight_props { |
| 999 | struct backlight_device *dev; |
| 1000 | int handle; |
| 1001 | u8 offset; |
| 1002 | u8 maxlvl; |
| 1003 | }; |
| 1004 | struct sony_backlight_props sony_bl_props; |
| 1005 | |
| 1006 | static int sony_backlight_update_status(struct backlight_device *bd) |
| 1007 | { |
| 1008 | int arg = bd->props.brightness + 1; |
| 1009 | return sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &arg, NULL); |
| 1010 | } |
| 1011 | |
| 1012 | static int sony_backlight_get_brightness(struct backlight_device *bd) |
| 1013 | { |
| 1014 | int value; |
| 1015 | |
| 1016 | if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL, &value)) |
| 1017 | return 0; |
| 1018 | /* brightness levels are 1-based, while backlight ones are 0-based */ |
| 1019 | return value - 1; |
| 1020 | } |
| 1021 | |
| 1022 | static int sony_nc_get_brightness_ng(struct backlight_device *bd) |
| 1023 | { |
| 1024 | int result; |
| 1025 | struct sony_backlight_props *sdev = |
| 1026 | (struct sony_backlight_props *)bl_get_data(bd); |
| 1027 | |
| 1028 | sony_call_snc_handle(sdev->handle, 0x0200, &result); |
| 1029 | |
| 1030 | return (result & 0xff) - sdev->offset; |
| 1031 | } |
| 1032 | |
| 1033 | static int sony_nc_update_status_ng(struct backlight_device *bd) |
| 1034 | { |
| 1035 | int value, result; |
| 1036 | struct sony_backlight_props *sdev = |
| 1037 | (struct sony_backlight_props *)bl_get_data(bd); |
| 1038 | |
| 1039 | value = bd->props.brightness + sdev->offset; |
| 1040 | if (sony_call_snc_handle(sdev->handle, 0x0100 | (value << 16), &result)) |
| 1041 | return -EIO; |
| 1042 | |
| 1043 | return value; |
| 1044 | } |
| 1045 | |
| 1046 | static const struct backlight_ops sony_backlight_ops = { |
| 1047 | .options = BL_CORE_SUSPENDRESUME, |
| 1048 | .update_status = sony_backlight_update_status, |
| 1049 | .get_brightness = sony_backlight_get_brightness, |
| 1050 | }; |
| 1051 | static const struct backlight_ops sony_backlight_ng_ops = { |
| 1052 | .options = BL_CORE_SUSPENDRESUME, |
| 1053 | .update_status = sony_nc_update_status_ng, |
| 1054 | .get_brightness = sony_nc_get_brightness_ng, |
| 1055 | }; |
| 1056 | |
| 1057 | /* |
| 1058 | * New SNC-only Vaios event mapping to driver known keys |
| 1059 | */ |
| 1060 | struct sony_nc_event { |
| 1061 | u8 data; |
| 1062 | u8 event; |
| 1063 | }; |
| 1064 | |
| 1065 | static struct sony_nc_event sony_100_events[] = { |
| 1066 | { 0x90, SONYPI_EVENT_PKEY_P1 }, |
| 1067 | { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED }, |
| 1068 | { 0x91, SONYPI_EVENT_PKEY_P2 }, |
| 1069 | { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED }, |
| 1070 | { 0x81, SONYPI_EVENT_FNKEY_F1 }, |
| 1071 | { 0x01, SONYPI_EVENT_FNKEY_RELEASED }, |
| 1072 | { 0x82, SONYPI_EVENT_FNKEY_F2 }, |
| 1073 | { 0x02, SONYPI_EVENT_FNKEY_RELEASED }, |
| 1074 | { 0x83, SONYPI_EVENT_FNKEY_F3 }, |
| 1075 | { 0x03, SONYPI_EVENT_FNKEY_RELEASED }, |
| 1076 | { 0x84, SONYPI_EVENT_FNKEY_F4 }, |
| 1077 | { 0x04, SONYPI_EVENT_FNKEY_RELEASED }, |
| 1078 | { 0x85, SONYPI_EVENT_FNKEY_F5 }, |
| 1079 | { 0x05, SONYPI_EVENT_FNKEY_RELEASED }, |
| 1080 | { 0x86, SONYPI_EVENT_FNKEY_F6 }, |
| 1081 | { 0x06, SONYPI_EVENT_FNKEY_RELEASED }, |
| 1082 | { 0x87, SONYPI_EVENT_FNKEY_F7 }, |
| 1083 | { 0x07, SONYPI_EVENT_FNKEY_RELEASED }, |
| 1084 | { 0x89, SONYPI_EVENT_FNKEY_F9 }, |
| 1085 | { 0x09, SONYPI_EVENT_FNKEY_RELEASED }, |
| 1086 | { 0x8A, SONYPI_EVENT_FNKEY_F10 }, |
| 1087 | { 0x0A, SONYPI_EVENT_FNKEY_RELEASED }, |
| 1088 | { 0x8C, SONYPI_EVENT_FNKEY_F12 }, |
| 1089 | { 0x0C, SONYPI_EVENT_FNKEY_RELEASED }, |
| 1090 | { 0x9d, SONYPI_EVENT_ZOOM_PRESSED }, |
| 1091 | { 0x1d, SONYPI_EVENT_ANYBUTTON_RELEASED }, |
| 1092 | { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED }, |
| 1093 | { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED }, |
| 1094 | { 0xa1, SONYPI_EVENT_MEDIA_PRESSED }, |
| 1095 | { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED }, |
| 1096 | { 0xa4, SONYPI_EVENT_CD_EJECT_PRESSED }, |
| 1097 | { 0x24, SONYPI_EVENT_ANYBUTTON_RELEASED }, |
| 1098 | { 0xa5, SONYPI_EVENT_VENDOR_PRESSED }, |
| 1099 | { 0x25, SONYPI_EVENT_ANYBUTTON_RELEASED }, |
| 1100 | { 0xa6, SONYPI_EVENT_HELP_PRESSED }, |
| 1101 | { 0x26, SONYPI_EVENT_ANYBUTTON_RELEASED }, |
| 1102 | { 0, 0 }, |
| 1103 | }; |
| 1104 | |
| 1105 | static struct sony_nc_event sony_127_events[] = { |
| 1106 | { 0x81, SONYPI_EVENT_MODEKEY_PRESSED }, |
| 1107 | { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED }, |
| 1108 | { 0x82, SONYPI_EVENT_PKEY_P1 }, |
| 1109 | { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED }, |
| 1110 | { 0x83, SONYPI_EVENT_PKEY_P2 }, |
| 1111 | { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED }, |
| 1112 | { 0x84, SONYPI_EVENT_PKEY_P3 }, |
| 1113 | { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED }, |
| 1114 | { 0x85, SONYPI_EVENT_PKEY_P4 }, |
| 1115 | { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED }, |
| 1116 | { 0x86, SONYPI_EVENT_PKEY_P5 }, |
| 1117 | { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED }, |
| 1118 | { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED }, |
| 1119 | { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED }, |
| 1120 | { 0, 0 }, |
| 1121 | }; |
| 1122 | |
| 1123 | static int sony_nc_hotkeys_decode(u32 event, unsigned int handle) |
| 1124 | { |
| 1125 | int ret = -EINVAL; |
| 1126 | unsigned int result = 0; |
| 1127 | struct sony_nc_event *key_event; |
| 1128 | |
| 1129 | if (sony_call_snc_handle(handle, 0x200, &result)) { |
| 1130 | dprintk("Unable to decode event 0x%.2x 0x%.2x\n", handle, |
| 1131 | event); |
| 1132 | return -EINVAL; |
| 1133 | } |
| 1134 | |
| 1135 | result &= 0xFF; |
| 1136 | |
| 1137 | if (handle == 0x0100) |
| 1138 | key_event = sony_100_events; |
| 1139 | else |
| 1140 | key_event = sony_127_events; |
| 1141 | |
| 1142 | for (; key_event->data; key_event++) { |
| 1143 | if (key_event->data == result) { |
| 1144 | ret = key_event->event; |
| 1145 | break; |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | if (!key_event->data) |
| 1150 | pr_info("Unknown hotkey 0x%.2x/0x%.2x (handle 0x%.2x)\n", |
| 1151 | event, result, handle); |
| 1152 | |
| 1153 | return ret; |
| 1154 | } |
| 1155 | |
| 1156 | /* |
| 1157 | * ACPI callbacks |
| 1158 | */ |
| 1159 | static void sony_nc_notify(struct acpi_device *device, u32 event) |
| 1160 | { |
| 1161 | u32 real_ev = event; |
| 1162 | u8 ev_type = 0; |
| 1163 | dprintk("sony_nc_notify, event: 0x%.2x\n", event); |
| 1164 | |
| 1165 | if (event >= 0x90) { |
| 1166 | unsigned int result = 0; |
| 1167 | unsigned int arg = 0; |
| 1168 | unsigned int handle = 0; |
| 1169 | unsigned int offset = event - 0x90; |
| 1170 | |
| 1171 | if (offset >= ARRAY_SIZE(handles->cap)) { |
| 1172 | pr_err("Event 0x%x outside of capabilities list\n", |
| 1173 | event); |
| 1174 | return; |
| 1175 | } |
| 1176 | handle = handles->cap[offset]; |
| 1177 | |
| 1178 | /* list of handles known for generating events */ |
| 1179 | switch (handle) { |
| 1180 | /* hotkey event */ |
| 1181 | case 0x0100: |
| 1182 | case 0x0127: |
| 1183 | ev_type = 1; |
| 1184 | real_ev = sony_nc_hotkeys_decode(event, handle); |
| 1185 | |
| 1186 | if (real_ev > 0) |
| 1187 | sony_laptop_report_input_event(real_ev); |
| 1188 | else |
| 1189 | /* restore the original event for reporting */ |
| 1190 | real_ev = event; |
| 1191 | |
| 1192 | break; |
| 1193 | |
| 1194 | /* wlan switch */ |
| 1195 | case 0x0124: |
| 1196 | case 0x0135: |
| 1197 | /* events on this handle are reported when the |
| 1198 | * switch changes position or for battery |
| 1199 | * events. We'll notify both of them but only |
| 1200 | * update the rfkill device status when the |
| 1201 | * switch is moved. |
| 1202 | */ |
| 1203 | ev_type = 2; |
| 1204 | sony_call_snc_handle(handle, 0x0100, &result); |
| 1205 | real_ev = result & 0x03; |
| 1206 | |
| 1207 | /* hw switch event */ |
| 1208 | if (real_ev == 1) |
| 1209 | sony_nc_rfkill_update(); |
| 1210 | |
| 1211 | break; |
| 1212 | |
| 1213 | default: |
| 1214 | dprintk("Unknown event 0x%x for handle 0x%x\n", |
| 1215 | event, handle); |
| 1216 | break; |
| 1217 | } |
| 1218 | |
| 1219 | /* clear the event (and the event reason when present) */ |
| 1220 | arg = 1 << offset; |
| 1221 | sony_nc_int_call(sony_nc_acpi_handle, "SN05", &arg, &result); |
| 1222 | |
| 1223 | } else { |
| 1224 | /* old style event */ |
| 1225 | ev_type = 1; |
| 1226 | sony_laptop_report_input_event(real_ev); |
| 1227 | } |
| 1228 | |
| 1229 | acpi_bus_generate_proc_event(sony_nc_acpi_device, ev_type, real_ev); |
| 1230 | |
| 1231 | acpi_bus_generate_netlink_event(sony_nc_acpi_device->pnp.device_class, |
| 1232 | dev_name(&sony_nc_acpi_device->dev), ev_type, real_ev); |
| 1233 | } |
| 1234 | |
| 1235 | static acpi_status sony_walk_callback(acpi_handle handle, u32 level, |
| 1236 | void *context, void **return_value) |
| 1237 | { |
| 1238 | struct acpi_device_info *info; |
| 1239 | |
| 1240 | if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) { |
| 1241 | pr_warn("method: name: %4.4s, args %X\n", |
| 1242 | (char *)&info->name, info->param_count); |
| 1243 | |
| 1244 | kfree(info); |
| 1245 | } |
| 1246 | |
| 1247 | return AE_OK; |
| 1248 | } |
| 1249 | |
| 1250 | /* |
| 1251 | * ACPI device |
| 1252 | */ |
| 1253 | static void sony_nc_function_setup(struct acpi_device *device, |
| 1254 | struct platform_device *pf_device) |
| 1255 | { |
| 1256 | unsigned int i, result, bitmask, arg; |
| 1257 | |
| 1258 | if (!handles) |
| 1259 | return; |
| 1260 | |
| 1261 | /* setup found handles here */ |
| 1262 | for (i = 0; i < ARRAY_SIZE(handles->cap); i++) { |
| 1263 | unsigned int handle = handles->cap[i]; |
| 1264 | |
| 1265 | if (!handle) |
| 1266 | continue; |
| 1267 | |
| 1268 | dprintk("setting up handle 0x%.4x\n", handle); |
| 1269 | |
| 1270 | switch (handle) { |
| 1271 | case 0x0100: |
| 1272 | case 0x0101: |
| 1273 | case 0x0127: |
| 1274 | /* setup hotkeys */ |
| 1275 | sony_call_snc_handle(handle, 0, &result); |
| 1276 | break; |
| 1277 | case 0x0102: |
| 1278 | /* setup hotkeys */ |
| 1279 | sony_call_snc_handle(handle, 0x100, &result); |
| 1280 | break; |
| 1281 | case 0x0115: |
| 1282 | case 0x0136: |
| 1283 | case 0x013f: |
| 1284 | result = sony_nc_battery_care_setup(pf_device, handle); |
| 1285 | if (result) |
| 1286 | pr_err("couldn't set up battery care function (%d)\n", |
| 1287 | result); |
| 1288 | break; |
| 1289 | case 0x0122: |
| 1290 | result = sony_nc_thermal_setup(pf_device); |
| 1291 | if (result) |
| 1292 | pr_err("couldn't set up thermal profile function (%d)\n", |
| 1293 | result); |
| 1294 | break; |
| 1295 | case 0x0124: |
| 1296 | case 0x0135: |
| 1297 | sony_nc_rfkill_setup(device); |
| 1298 | break; |
| 1299 | case 0x0137: |
| 1300 | sony_nc_kbd_backlight_setup(pf_device); |
| 1301 | break; |
| 1302 | default: |
| 1303 | continue; |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | /* Enable all events */ |
| 1308 | arg = 0x10; |
| 1309 | if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask)) |
| 1310 | sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask, |
| 1311 | &result); |
| 1312 | } |
| 1313 | |
| 1314 | static void sony_nc_function_cleanup(struct platform_device *pd) |
| 1315 | { |
| 1316 | unsigned int i, result, bitmask, handle; |
| 1317 | |
| 1318 | /* get enabled events and disable them */ |
| 1319 | sony_nc_int_call(sony_nc_acpi_handle, "SN01", NULL, &bitmask); |
| 1320 | sony_nc_int_call(sony_nc_acpi_handle, "SN03", &bitmask, &result); |
| 1321 | |
| 1322 | /* cleanup handles here */ |
| 1323 | for (i = 0; i < ARRAY_SIZE(handles->cap); i++) { |
| 1324 | |
| 1325 | handle = handles->cap[i]; |
| 1326 | |
| 1327 | if (!handle) |
| 1328 | continue; |
| 1329 | |
| 1330 | switch (handle) { |
| 1331 | case 0x0115: |
| 1332 | case 0x0136: |
| 1333 | case 0x013f: |
| 1334 | sony_nc_battery_care_cleanup(pd); |
| 1335 | break; |
| 1336 | case 0x0122: |
| 1337 | sony_nc_thermal_cleanup(pd); |
| 1338 | break; |
| 1339 | case 0x0124: |
| 1340 | case 0x0135: |
| 1341 | sony_nc_rfkill_cleanup(); |
| 1342 | break; |
| 1343 | case 0x0137: |
| 1344 | sony_nc_kbd_backlight_cleanup(pd); |
| 1345 | break; |
| 1346 | default: |
| 1347 | continue; |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | /* finally cleanup the handles list */ |
| 1352 | sony_nc_handles_cleanup(pd); |
| 1353 | } |
| 1354 | |
| 1355 | static void sony_nc_function_resume(void) |
| 1356 | { |
| 1357 | unsigned int i, result, bitmask, arg; |
| 1358 | |
| 1359 | dprintk("Resuming SNC device\n"); |
| 1360 | |
| 1361 | for (i = 0; i < ARRAY_SIZE(handles->cap); i++) { |
| 1362 | unsigned int handle = handles->cap[i]; |
| 1363 | |
| 1364 | if (!handle) |
| 1365 | continue; |
| 1366 | |
| 1367 | switch (handle) { |
| 1368 | case 0x0100: |
| 1369 | case 0x0101: |
| 1370 | case 0x0127: |
| 1371 | /* re-enable hotkeys */ |
| 1372 | sony_call_snc_handle(handle, 0, &result); |
| 1373 | break; |
| 1374 | case 0x0102: |
| 1375 | /* re-enable hotkeys */ |
| 1376 | sony_call_snc_handle(handle, 0x100, &result); |
| 1377 | break; |
| 1378 | case 0x0122: |
| 1379 | sony_nc_thermal_resume(); |
| 1380 | break; |
| 1381 | case 0x0124: |
| 1382 | case 0x0135: |
| 1383 | sony_nc_rfkill_update(); |
| 1384 | break; |
| 1385 | case 0x0137: |
| 1386 | sony_nc_kbd_backlight_resume(); |
| 1387 | break; |
| 1388 | default: |
| 1389 | continue; |
| 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | /* Enable all events */ |
| 1394 | arg = 0x10; |
| 1395 | if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask)) |
| 1396 | sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask, |
| 1397 | &result); |
| 1398 | } |
| 1399 | |
| 1400 | static int sony_nc_resume(struct acpi_device *device) |
| 1401 | { |
| 1402 | struct sony_nc_value *item; |
| 1403 | acpi_handle handle; |
| 1404 | |
| 1405 | for (item = sony_nc_values; item->name; item++) { |
| 1406 | int ret; |
| 1407 | |
| 1408 | if (!item->valid) |
| 1409 | continue; |
| 1410 | ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset, |
| 1411 | &item->value, NULL); |
| 1412 | if (ret < 0) { |
| 1413 | pr_err("%s: %d\n", __func__, ret); |
| 1414 | break; |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON", |
| 1419 | &handle))) { |
| 1420 | int arg = 1; |
| 1421 | if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL)) |
| 1422 | dprintk("ECON Method failed\n"); |
| 1423 | } |
| 1424 | |
| 1425 | if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00", |
| 1426 | &handle))) |
| 1427 | sony_nc_function_resume(); |
| 1428 | |
| 1429 | return 0; |
| 1430 | } |
| 1431 | |
| 1432 | static void sony_nc_rfkill_cleanup(void) |
| 1433 | { |
| 1434 | int i; |
| 1435 | |
| 1436 | for (i = 0; i < N_SONY_RFKILL; i++) { |
| 1437 | if (sony_rfkill_devices[i]) { |
| 1438 | rfkill_unregister(sony_rfkill_devices[i]); |
| 1439 | rfkill_destroy(sony_rfkill_devices[i]); |
| 1440 | } |
| 1441 | } |
| 1442 | } |
| 1443 | |
| 1444 | static int sony_nc_rfkill_set(void *data, bool blocked) |
| 1445 | { |
| 1446 | int result; |
| 1447 | int argument = sony_rfkill_address[(long) data] + 0x100; |
| 1448 | |
| 1449 | if (!blocked) |
| 1450 | argument |= 0x030000; |
| 1451 | |
| 1452 | return sony_call_snc_handle(sony_rfkill_handle, argument, &result); |
| 1453 | } |
| 1454 | |
| 1455 | static const struct rfkill_ops sony_rfkill_ops = { |
| 1456 | .set_block = sony_nc_rfkill_set, |
| 1457 | }; |
| 1458 | |
| 1459 | static int sony_nc_setup_rfkill(struct acpi_device *device, |
| 1460 | enum sony_nc_rfkill nc_type) |
| 1461 | { |
| 1462 | int err = 0; |
| 1463 | struct rfkill *rfk; |
| 1464 | enum rfkill_type type; |
| 1465 | const char *name; |
| 1466 | int result; |
| 1467 | bool hwblock, swblock; |
| 1468 | |
| 1469 | switch (nc_type) { |
| 1470 | case SONY_WIFI: |
| 1471 | type = RFKILL_TYPE_WLAN; |
| 1472 | name = "sony-wifi"; |
| 1473 | break; |
| 1474 | case SONY_BLUETOOTH: |
| 1475 | type = RFKILL_TYPE_BLUETOOTH; |
| 1476 | name = "sony-bluetooth"; |
| 1477 | break; |
| 1478 | case SONY_WWAN: |
| 1479 | type = RFKILL_TYPE_WWAN; |
| 1480 | name = "sony-wwan"; |
| 1481 | break; |
| 1482 | case SONY_WIMAX: |
| 1483 | type = RFKILL_TYPE_WIMAX; |
| 1484 | name = "sony-wimax"; |
| 1485 | break; |
| 1486 | default: |
| 1487 | return -EINVAL; |
| 1488 | } |
| 1489 | |
| 1490 | rfk = rfkill_alloc(name, &device->dev, type, |
| 1491 | &sony_rfkill_ops, (void *)nc_type); |
| 1492 | if (!rfk) |
| 1493 | return -ENOMEM; |
| 1494 | |
| 1495 | if (sony_call_snc_handle(sony_rfkill_handle, 0x200, &result) < 0) { |
| 1496 | rfkill_destroy(rfk); |
| 1497 | return -1; |
| 1498 | } |
| 1499 | hwblock = !(result & 0x1); |
| 1500 | |
| 1501 | if (sony_call_snc_handle(sony_rfkill_handle, |
| 1502 | sony_rfkill_address[nc_type], |
| 1503 | &result) < 0) { |
| 1504 | rfkill_destroy(rfk); |
| 1505 | return -1; |
| 1506 | } |
| 1507 | swblock = !(result & 0x2); |
| 1508 | |
| 1509 | rfkill_init_sw_state(rfk, swblock); |
| 1510 | rfkill_set_hw_state(rfk, hwblock); |
| 1511 | |
| 1512 | err = rfkill_register(rfk); |
| 1513 | if (err) { |
| 1514 | rfkill_destroy(rfk); |
| 1515 | return err; |
| 1516 | } |
| 1517 | sony_rfkill_devices[nc_type] = rfk; |
| 1518 | return err; |
| 1519 | } |
| 1520 | |
| 1521 | static void sony_nc_rfkill_update(void) |
| 1522 | { |
| 1523 | enum sony_nc_rfkill i; |
| 1524 | int result; |
| 1525 | bool hwblock; |
| 1526 | |
| 1527 | sony_call_snc_handle(sony_rfkill_handle, 0x200, &result); |
| 1528 | hwblock = !(result & 0x1); |
| 1529 | |
| 1530 | for (i = 0; i < N_SONY_RFKILL; i++) { |
| 1531 | int argument = sony_rfkill_address[i]; |
| 1532 | |
| 1533 | if (!sony_rfkill_devices[i]) |
| 1534 | continue; |
| 1535 | |
| 1536 | if (hwblock) { |
| 1537 | if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) { |
| 1538 | /* we already know we're blocked */ |
| 1539 | } |
| 1540 | continue; |
| 1541 | } |
| 1542 | |
| 1543 | sony_call_snc_handle(sony_rfkill_handle, argument, &result); |
| 1544 | rfkill_set_states(sony_rfkill_devices[i], |
| 1545 | !(result & 0x2), false); |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | static void sony_nc_rfkill_setup(struct acpi_device *device) |
| 1550 | { |
| 1551 | u64 offset; |
| 1552 | int i; |
| 1553 | unsigned char buffer[32] = { 0 }; |
| 1554 | |
| 1555 | offset = sony_find_snc_handle(0x124); |
| 1556 | if (offset == -1) { |
| 1557 | offset = sony_find_snc_handle(0x135); |
| 1558 | if (offset == -1) |
| 1559 | return; |
| 1560 | else |
| 1561 | sony_rfkill_handle = 0x135; |
| 1562 | } else |
| 1563 | sony_rfkill_handle = 0x124; |
| 1564 | dprintk("Found rkfill handle: 0x%.4x\n", sony_rfkill_handle); |
| 1565 | |
| 1566 | i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer, |
| 1567 | 32); |
| 1568 | if (i < 0) |
| 1569 | return; |
| 1570 | |
| 1571 | /* the buffer is filled with magic numbers describing the devices |
| 1572 | * available, 0xff terminates the enumeration |
| 1573 | */ |
| 1574 | for (i = 0; i < ARRAY_SIZE(buffer); i++) { |
| 1575 | |
| 1576 | if (buffer[i] == 0xff) |
| 1577 | break; |
| 1578 | |
| 1579 | dprintk("Radio devices, looking at 0x%.2x\n", buffer[i]); |
| 1580 | |
| 1581 | if (buffer[i] == 0 && !sony_rfkill_devices[SONY_WIFI]) |
| 1582 | sony_nc_setup_rfkill(device, SONY_WIFI); |
| 1583 | |
| 1584 | if (buffer[i] == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH]) |
| 1585 | sony_nc_setup_rfkill(device, SONY_BLUETOOTH); |
| 1586 | |
| 1587 | if ((0xf0 & buffer[i]) == 0x20 && |
| 1588 | !sony_rfkill_devices[SONY_WWAN]) |
| 1589 | sony_nc_setup_rfkill(device, SONY_WWAN); |
| 1590 | |
| 1591 | if (buffer[i] == 0x30 && !sony_rfkill_devices[SONY_WIMAX]) |
| 1592 | sony_nc_setup_rfkill(device, SONY_WIMAX); |
| 1593 | } |
| 1594 | } |
| 1595 | |
| 1596 | /* Keyboard backlight feature */ |
| 1597 | #define KBDBL_HANDLER 0x137 |
| 1598 | #define KBDBL_PRESENT 0xB00 |
| 1599 | #define SET_MODE 0xC00 |
| 1600 | #define SET_STATE 0xD00 |
| 1601 | #define SET_TIMEOUT 0xE00 |
| 1602 | |
| 1603 | struct kbd_backlight { |
| 1604 | int mode; |
| 1605 | int timeout; |
| 1606 | struct device_attribute mode_attr; |
| 1607 | struct device_attribute timeout_attr; |
| 1608 | }; |
| 1609 | |
| 1610 | static struct kbd_backlight *kbdbl_handle; |
| 1611 | |
| 1612 | static ssize_t __sony_nc_kbd_backlight_mode_set(u8 value) |
| 1613 | { |
| 1614 | int result; |
| 1615 | |
| 1616 | if (value > 1) |
| 1617 | return -EINVAL; |
| 1618 | |
| 1619 | if (sony_call_snc_handle(KBDBL_HANDLER, |
| 1620 | (value << 0x10) | SET_MODE, &result)) |
| 1621 | return -EIO; |
| 1622 | |
| 1623 | /* Try to turn the light on/off immediately */ |
| 1624 | sony_call_snc_handle(KBDBL_HANDLER, (value << 0x10) | SET_STATE, |
| 1625 | &result); |
| 1626 | |
| 1627 | kbdbl_handle->mode = value; |
| 1628 | |
| 1629 | return 0; |
| 1630 | } |
| 1631 | |
| 1632 | static ssize_t sony_nc_kbd_backlight_mode_store(struct device *dev, |
| 1633 | struct device_attribute *attr, |
| 1634 | const char *buffer, size_t count) |
| 1635 | { |
| 1636 | int ret = 0; |
| 1637 | unsigned long value; |
| 1638 | |
| 1639 | if (count > 31) |
| 1640 | return -EINVAL; |
| 1641 | |
| 1642 | if (kstrtoul(buffer, 10, &value)) |
| 1643 | return -EINVAL; |
| 1644 | |
| 1645 | ret = __sony_nc_kbd_backlight_mode_set(value); |
| 1646 | if (ret < 0) |
| 1647 | return ret; |
| 1648 | |
| 1649 | return count; |
| 1650 | } |
| 1651 | |
| 1652 | static ssize_t sony_nc_kbd_backlight_mode_show(struct device *dev, |
| 1653 | struct device_attribute *attr, char *buffer) |
| 1654 | { |
| 1655 | ssize_t count = 0; |
| 1656 | count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_handle->mode); |
| 1657 | return count; |
| 1658 | } |
| 1659 | |
| 1660 | static int __sony_nc_kbd_backlight_timeout_set(u8 value) |
| 1661 | { |
| 1662 | int result; |
| 1663 | |
| 1664 | if (value > 3) |
| 1665 | return -EINVAL; |
| 1666 | |
| 1667 | if (sony_call_snc_handle(KBDBL_HANDLER, |
| 1668 | (value << 0x10) | SET_TIMEOUT, &result)) |
| 1669 | return -EIO; |
| 1670 | |
| 1671 | kbdbl_handle->timeout = value; |
| 1672 | |
| 1673 | return 0; |
| 1674 | } |
| 1675 | |
| 1676 | static ssize_t sony_nc_kbd_backlight_timeout_store(struct device *dev, |
| 1677 | struct device_attribute *attr, |
| 1678 | const char *buffer, size_t count) |
| 1679 | { |
| 1680 | int ret = 0; |
| 1681 | unsigned long value; |
| 1682 | |
| 1683 | if (count > 31) |
| 1684 | return -EINVAL; |
| 1685 | |
| 1686 | if (kstrtoul(buffer, 10, &value)) |
| 1687 | return -EINVAL; |
| 1688 | |
| 1689 | ret = __sony_nc_kbd_backlight_timeout_set(value); |
| 1690 | if (ret < 0) |
| 1691 | return ret; |
| 1692 | |
| 1693 | return count; |
| 1694 | } |
| 1695 | |
| 1696 | static ssize_t sony_nc_kbd_backlight_timeout_show(struct device *dev, |
| 1697 | struct device_attribute *attr, char *buffer) |
| 1698 | { |
| 1699 | ssize_t count = 0; |
| 1700 | count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_handle->timeout); |
| 1701 | return count; |
| 1702 | } |
| 1703 | |
| 1704 | static void sony_nc_kbd_backlight_setup(struct platform_device *pd) |
| 1705 | { |
| 1706 | int result; |
| 1707 | |
| 1708 | if (sony_call_snc_handle(KBDBL_HANDLER, KBDBL_PRESENT, &result)) |
| 1709 | return; |
| 1710 | if (!(result & 0x02)) |
| 1711 | return; |
| 1712 | |
| 1713 | kbdbl_handle = kzalloc(sizeof(*kbdbl_handle), GFP_KERNEL); |
| 1714 | if (!kbdbl_handle) |
| 1715 | return; |
| 1716 | |
| 1717 | sysfs_attr_init(&kbdbl_handle->mode_attr.attr); |
| 1718 | kbdbl_handle->mode_attr.attr.name = "kbd_backlight"; |
| 1719 | kbdbl_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 1720 | kbdbl_handle->mode_attr.show = sony_nc_kbd_backlight_mode_show; |
| 1721 | kbdbl_handle->mode_attr.store = sony_nc_kbd_backlight_mode_store; |
| 1722 | |
| 1723 | sysfs_attr_init(&kbdbl_handle->timeout_attr.attr); |
| 1724 | kbdbl_handle->timeout_attr.attr.name = "kbd_backlight_timeout"; |
| 1725 | kbdbl_handle->timeout_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 1726 | kbdbl_handle->timeout_attr.show = sony_nc_kbd_backlight_timeout_show; |
| 1727 | kbdbl_handle->timeout_attr.store = sony_nc_kbd_backlight_timeout_store; |
| 1728 | |
| 1729 | if (device_create_file(&pd->dev, &kbdbl_handle->mode_attr)) |
| 1730 | goto outkzalloc; |
| 1731 | |
| 1732 | if (device_create_file(&pd->dev, &kbdbl_handle->timeout_attr)) |
| 1733 | goto outmode; |
| 1734 | |
| 1735 | __sony_nc_kbd_backlight_mode_set(kbd_backlight); |
| 1736 | __sony_nc_kbd_backlight_timeout_set(kbd_backlight_timeout); |
| 1737 | |
| 1738 | return; |
| 1739 | |
| 1740 | outmode: |
| 1741 | device_remove_file(&pd->dev, &kbdbl_handle->mode_attr); |
| 1742 | outkzalloc: |
| 1743 | kfree(kbdbl_handle); |
| 1744 | kbdbl_handle = NULL; |
| 1745 | return; |
| 1746 | } |
| 1747 | |
| 1748 | static int sony_nc_kbd_backlight_cleanup(struct platform_device *pd) |
| 1749 | { |
| 1750 | if (kbdbl_handle) { |
| 1751 | int result; |
| 1752 | |
| 1753 | device_remove_file(&pd->dev, &kbdbl_handle->mode_attr); |
| 1754 | device_remove_file(&pd->dev, &kbdbl_handle->timeout_attr); |
| 1755 | |
| 1756 | /* restore the default hw behaviour */ |
| 1757 | sony_call_snc_handle(KBDBL_HANDLER, 0x1000 | SET_MODE, &result); |
| 1758 | sony_call_snc_handle(KBDBL_HANDLER, SET_TIMEOUT, &result); |
| 1759 | |
| 1760 | kfree(kbdbl_handle); |
| 1761 | } |
| 1762 | return 0; |
| 1763 | } |
| 1764 | |
| 1765 | static void sony_nc_kbd_backlight_resume(void) |
| 1766 | { |
| 1767 | int ignore = 0; |
| 1768 | |
| 1769 | if (!kbdbl_handle) |
| 1770 | return; |
| 1771 | |
| 1772 | if (kbdbl_handle->mode == 0) |
| 1773 | sony_call_snc_handle(KBDBL_HANDLER, SET_MODE, &ignore); |
| 1774 | |
| 1775 | if (kbdbl_handle->timeout != 0) |
| 1776 | sony_call_snc_handle(KBDBL_HANDLER, |
| 1777 | (kbdbl_handle->timeout << 0x10) | SET_TIMEOUT, |
| 1778 | &ignore); |
| 1779 | } |
| 1780 | |
| 1781 | struct battery_care_control { |
| 1782 | struct device_attribute attrs[2]; |
| 1783 | unsigned int handle; |
| 1784 | }; |
| 1785 | static struct battery_care_control *bcare_ctl; |
| 1786 | |
| 1787 | static ssize_t sony_nc_battery_care_limit_store(struct device *dev, |
| 1788 | struct device_attribute *attr, |
| 1789 | const char *buffer, size_t count) |
| 1790 | { |
| 1791 | unsigned int result, cmd; |
| 1792 | unsigned long value; |
| 1793 | |
| 1794 | if (count > 31) |
| 1795 | return -EINVAL; |
| 1796 | |
| 1797 | if (kstrtoul(buffer, 10, &value)) |
| 1798 | return -EINVAL; |
| 1799 | |
| 1800 | /* limit values (2 bits): |
| 1801 | * 00 - none |
| 1802 | * 01 - 80% |
| 1803 | * 10 - 50% |
| 1804 | * 11 - 100% |
| 1805 | * |
| 1806 | * bit 0: 0 disable BCL, 1 enable BCL |
| 1807 | * bit 1: 1 tell to store the battery limit (see bits 6,7) too |
| 1808 | * bits 2,3: reserved |
| 1809 | * bits 4,5: store the limit into the EC |
| 1810 | * bits 6,7: store the limit into the battery |
| 1811 | */ |
| 1812 | |
| 1813 | /* |
| 1814 | * handle 0x0115 should allow storing on battery too; |
| 1815 | * handle 0x0136 same as 0x0115 + health status; |
| 1816 | * handle 0x013f, same as 0x0136 but no storing on the battery |
| 1817 | * |
| 1818 | * Store only inside the EC for now, regardless the handle number |
| 1819 | */ |
| 1820 | if (value == 0) |
| 1821 | /* disable limits */ |
| 1822 | cmd = 0x0; |
| 1823 | |
| 1824 | else if (value <= 50) |
| 1825 | cmd = 0x21; |
| 1826 | |
| 1827 | else if (value <= 80) |
| 1828 | cmd = 0x11; |
| 1829 | |
| 1830 | else if (value <= 100) |
| 1831 | cmd = 0x31; |
| 1832 | |
| 1833 | else |
| 1834 | return -EINVAL; |
| 1835 | |
| 1836 | if (sony_call_snc_handle(bcare_ctl->handle, (cmd << 0x10) | 0x0100, |
| 1837 | &result)) |
| 1838 | return -EIO; |
| 1839 | |
| 1840 | return count; |
| 1841 | } |
| 1842 | |
| 1843 | static ssize_t sony_nc_battery_care_limit_show(struct device *dev, |
| 1844 | struct device_attribute *attr, char *buffer) |
| 1845 | { |
| 1846 | unsigned int result, status; |
| 1847 | |
| 1848 | if (sony_call_snc_handle(bcare_ctl->handle, 0x0000, &result)) |
| 1849 | return -EIO; |
| 1850 | |
| 1851 | status = (result & 0x01) ? ((result & 0x30) >> 0x04) : 0; |
| 1852 | switch (status) { |
| 1853 | case 1: |
| 1854 | status = 80; |
| 1855 | break; |
| 1856 | case 2: |
| 1857 | status = 50; |
| 1858 | break; |
| 1859 | case 3: |
| 1860 | status = 100; |
| 1861 | break; |
| 1862 | default: |
| 1863 | status = 0; |
| 1864 | break; |
| 1865 | } |
| 1866 | |
| 1867 | return snprintf(buffer, PAGE_SIZE, "%d\n", status); |
| 1868 | } |
| 1869 | |
| 1870 | static ssize_t sony_nc_battery_care_health_show(struct device *dev, |
| 1871 | struct device_attribute *attr, char *buffer) |
| 1872 | { |
| 1873 | ssize_t count = 0; |
| 1874 | unsigned int health; |
| 1875 | |
| 1876 | if (sony_call_snc_handle(bcare_ctl->handle, 0x0200, &health)) |
| 1877 | return -EIO; |
| 1878 | |
| 1879 | count = snprintf(buffer, PAGE_SIZE, "%d\n", health & 0xff); |
| 1880 | |
| 1881 | return count; |
| 1882 | } |
| 1883 | |
| 1884 | static int sony_nc_battery_care_setup(struct platform_device *pd, |
| 1885 | unsigned int handle) |
| 1886 | { |
| 1887 | int ret = 0; |
| 1888 | |
| 1889 | bcare_ctl = kzalloc(sizeof(struct battery_care_control), GFP_KERNEL); |
| 1890 | if (!bcare_ctl) |
| 1891 | return -ENOMEM; |
| 1892 | |
| 1893 | bcare_ctl->handle = handle; |
| 1894 | |
| 1895 | sysfs_attr_init(&bcare_ctl->attrs[0].attr); |
| 1896 | bcare_ctl->attrs[0].attr.name = "battery_care_limiter"; |
| 1897 | bcare_ctl->attrs[0].attr.mode = S_IRUGO | S_IWUSR; |
| 1898 | bcare_ctl->attrs[0].show = sony_nc_battery_care_limit_show; |
| 1899 | bcare_ctl->attrs[0].store = sony_nc_battery_care_limit_store; |
| 1900 | |
| 1901 | ret = device_create_file(&pd->dev, &bcare_ctl->attrs[0]); |
| 1902 | if (ret) |
| 1903 | goto outkzalloc; |
| 1904 | |
| 1905 | /* 0x0115 is for models with no health reporting capability */ |
| 1906 | if (handle == 0x0115) |
| 1907 | return 0; |
| 1908 | |
| 1909 | sysfs_attr_init(&bcare_ctl->attrs[1].attr); |
| 1910 | bcare_ctl->attrs[1].attr.name = "battery_care_health"; |
| 1911 | bcare_ctl->attrs[1].attr.mode = S_IRUGO; |
| 1912 | bcare_ctl->attrs[1].show = sony_nc_battery_care_health_show; |
| 1913 | |
| 1914 | ret = device_create_file(&pd->dev, &bcare_ctl->attrs[1]); |
| 1915 | if (ret) |
| 1916 | goto outlimiter; |
| 1917 | |
| 1918 | return 0; |
| 1919 | |
| 1920 | outlimiter: |
| 1921 | device_remove_file(&pd->dev, &bcare_ctl->attrs[0]); |
| 1922 | |
| 1923 | outkzalloc: |
| 1924 | kfree(bcare_ctl); |
| 1925 | bcare_ctl = NULL; |
| 1926 | |
| 1927 | return ret; |
| 1928 | } |
| 1929 | |
| 1930 | static void sony_nc_battery_care_cleanup(struct platform_device *pd) |
| 1931 | { |
| 1932 | if (bcare_ctl) { |
| 1933 | device_remove_file(&pd->dev, &bcare_ctl->attrs[0]); |
| 1934 | if (bcare_ctl->handle != 0x0115) |
| 1935 | device_remove_file(&pd->dev, &bcare_ctl->attrs[1]); |
| 1936 | |
| 1937 | kfree(bcare_ctl); |
| 1938 | bcare_ctl = NULL; |
| 1939 | } |
| 1940 | } |
| 1941 | |
| 1942 | struct snc_thermal_ctrl { |
| 1943 | unsigned int mode; |
| 1944 | unsigned int profiles; |
| 1945 | struct device_attribute mode_attr; |
| 1946 | struct device_attribute profiles_attr; |
| 1947 | }; |
| 1948 | static struct snc_thermal_ctrl *th_handle; |
| 1949 | |
| 1950 | #define THM_PROFILE_MAX 3 |
| 1951 | static const char * const snc_thermal_profiles[] = { |
| 1952 | "balanced", |
| 1953 | "silent", |
| 1954 | "performance" |
| 1955 | }; |
| 1956 | |
| 1957 | static int sony_nc_thermal_mode_set(unsigned short mode) |
| 1958 | { |
| 1959 | unsigned int result; |
| 1960 | |
| 1961 | /* the thermal profile seems to be a two bit bitmask: |
| 1962 | * lsb -> silent |
| 1963 | * msb -> performance |
| 1964 | * no bit set is the normal operation and is always valid |
| 1965 | * Some vaio models only have "balanced" and "performance" |
| 1966 | */ |
| 1967 | if ((mode && !(th_handle->profiles & mode)) || mode >= THM_PROFILE_MAX) |
| 1968 | return -EINVAL; |
| 1969 | |
| 1970 | if (sony_call_snc_handle(0x0122, mode << 0x10 | 0x0200, &result)) |
| 1971 | return -EIO; |
| 1972 | |
| 1973 | th_handle->mode = mode; |
| 1974 | |
| 1975 | return 0; |
| 1976 | } |
| 1977 | |
| 1978 | static int sony_nc_thermal_mode_get(void) |
| 1979 | { |
| 1980 | unsigned int result; |
| 1981 | |
| 1982 | if (sony_call_snc_handle(0x0122, 0x0100, &result)) |
| 1983 | return -EIO; |
| 1984 | |
| 1985 | return result & 0xff; |
| 1986 | } |
| 1987 | |
| 1988 | static ssize_t sony_nc_thermal_profiles_show(struct device *dev, |
| 1989 | struct device_attribute *attr, char *buffer) |
| 1990 | { |
| 1991 | short cnt; |
| 1992 | size_t idx = 0; |
| 1993 | |
| 1994 | for (cnt = 0; cnt < THM_PROFILE_MAX; cnt++) { |
| 1995 | if (!cnt || (th_handle->profiles & cnt)) |
| 1996 | idx += snprintf(buffer + idx, PAGE_SIZE - idx, "%s ", |
| 1997 | snc_thermal_profiles[cnt]); |
| 1998 | } |
| 1999 | idx += snprintf(buffer + idx, PAGE_SIZE - idx, "\n"); |
| 2000 | |
| 2001 | return idx; |
| 2002 | } |
| 2003 | |
| 2004 | static ssize_t sony_nc_thermal_mode_store(struct device *dev, |
| 2005 | struct device_attribute *attr, |
| 2006 | const char *buffer, size_t count) |
| 2007 | { |
| 2008 | unsigned short cmd; |
| 2009 | size_t len = count; |
| 2010 | |
| 2011 | if (count == 0) |
| 2012 | return -EINVAL; |
| 2013 | |
| 2014 | /* skip the newline if present */ |
| 2015 | if (buffer[len - 1] == '\n') |
| 2016 | len--; |
| 2017 | |
| 2018 | for (cmd = 0; cmd < THM_PROFILE_MAX; cmd++) |
| 2019 | if (strncmp(buffer, snc_thermal_profiles[cmd], len) == 0) |
| 2020 | break; |
| 2021 | |
| 2022 | if (sony_nc_thermal_mode_set(cmd)) |
| 2023 | return -EIO; |
| 2024 | |
| 2025 | return count; |
| 2026 | } |
| 2027 | |
| 2028 | static ssize_t sony_nc_thermal_mode_show(struct device *dev, |
| 2029 | struct device_attribute *attr, char *buffer) |
| 2030 | { |
| 2031 | ssize_t count = 0; |
| 2032 | unsigned int mode = sony_nc_thermal_mode_get(); |
| 2033 | |
| 2034 | if (mode < 0) |
| 2035 | return mode; |
| 2036 | |
| 2037 | count = snprintf(buffer, PAGE_SIZE, "%s\n", snc_thermal_profiles[mode]); |
| 2038 | |
| 2039 | return count; |
| 2040 | } |
| 2041 | |
| 2042 | static int sony_nc_thermal_setup(struct platform_device *pd) |
| 2043 | { |
| 2044 | int ret = 0; |
| 2045 | th_handle = kzalloc(sizeof(struct snc_thermal_ctrl), GFP_KERNEL); |
| 2046 | if (!th_handle) |
| 2047 | return -ENOMEM; |
| 2048 | |
| 2049 | ret = sony_call_snc_handle(0x0122, 0x0000, &th_handle->profiles); |
| 2050 | if (ret) { |
| 2051 | pr_warn("couldn't to read the thermal profiles\n"); |
| 2052 | goto outkzalloc; |
| 2053 | } |
| 2054 | |
| 2055 | ret = sony_nc_thermal_mode_get(); |
| 2056 | if (ret < 0) { |
| 2057 | pr_warn("couldn't to read the current thermal profile"); |
| 2058 | goto outkzalloc; |
| 2059 | } |
| 2060 | th_handle->mode = ret; |
| 2061 | |
| 2062 | sysfs_attr_init(&th_handle->profiles_attr.attr); |
| 2063 | th_handle->profiles_attr.attr.name = "thermal_profiles"; |
| 2064 | th_handle->profiles_attr.attr.mode = S_IRUGO; |
| 2065 | th_handle->profiles_attr.show = sony_nc_thermal_profiles_show; |
| 2066 | |
| 2067 | sysfs_attr_init(&th_handle->mode_attr.attr); |
| 2068 | th_handle->mode_attr.attr.name = "thermal_control"; |
| 2069 | th_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 2070 | th_handle->mode_attr.show = sony_nc_thermal_mode_show; |
| 2071 | th_handle->mode_attr.store = sony_nc_thermal_mode_store; |
| 2072 | |
| 2073 | ret = device_create_file(&pd->dev, &th_handle->profiles_attr); |
| 2074 | if (ret) |
| 2075 | goto outkzalloc; |
| 2076 | |
| 2077 | ret = device_create_file(&pd->dev, &th_handle->mode_attr); |
| 2078 | if (ret) |
| 2079 | goto outprofiles; |
| 2080 | |
| 2081 | return 0; |
| 2082 | |
| 2083 | outprofiles: |
| 2084 | device_remove_file(&pd->dev, &th_handle->profiles_attr); |
| 2085 | outkzalloc: |
| 2086 | kfree(th_handle); |
| 2087 | th_handle = NULL; |
| 2088 | return ret; |
| 2089 | } |
| 2090 | |
| 2091 | static void sony_nc_thermal_cleanup(struct platform_device *pd) |
| 2092 | { |
| 2093 | if (th_handle) { |
| 2094 | device_remove_file(&pd->dev, &th_handle->profiles_attr); |
| 2095 | device_remove_file(&pd->dev, &th_handle->mode_attr); |
| 2096 | kfree(th_handle); |
| 2097 | th_handle = NULL; |
| 2098 | } |
| 2099 | } |
| 2100 | |
| 2101 | static void sony_nc_thermal_resume(void) |
| 2102 | { |
| 2103 | unsigned int status = sony_nc_thermal_mode_get(); |
| 2104 | |
| 2105 | if (status != th_handle->mode) |
| 2106 | sony_nc_thermal_mode_set(th_handle->mode); |
| 2107 | } |
| 2108 | |
| 2109 | static void sony_nc_backlight_ng_read_limits(int handle, |
| 2110 | struct sony_backlight_props *props) |
| 2111 | { |
| 2112 | u64 offset; |
| 2113 | int i; |
| 2114 | u8 min = 0xff, max = 0x00; |
| 2115 | unsigned char buffer[32] = { 0 }; |
| 2116 | |
| 2117 | props->handle = handle; |
| 2118 | props->offset = 0; |
| 2119 | props->maxlvl = 0xff; |
| 2120 | |
| 2121 | offset = sony_find_snc_handle(handle); |
| 2122 | if (offset < 0) |
| 2123 | return; |
| 2124 | |
| 2125 | /* try to read the boundaries from ACPI tables, if we fail the above |
| 2126 | * defaults should be reasonable |
| 2127 | */ |
| 2128 | i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer, |
| 2129 | 32); |
| 2130 | if (i < 0) |
| 2131 | return; |
| 2132 | |
| 2133 | /* the buffer lists brightness levels available, brightness levels are |
| 2134 | * from position 0 to 8 in the array, other values are used by ALS |
| 2135 | * control. |
| 2136 | */ |
| 2137 | for (i = 0; i < 9 && i < ARRAY_SIZE(buffer); i++) { |
| 2138 | |
| 2139 | dprintk("Brightness level: %d\n", buffer[i]); |
| 2140 | |
| 2141 | if (!buffer[i]) |
| 2142 | break; |
| 2143 | |
| 2144 | if (buffer[i] > max) |
| 2145 | max = buffer[i]; |
| 2146 | if (buffer[i] < min) |
| 2147 | min = buffer[i]; |
| 2148 | } |
| 2149 | props->offset = min; |
| 2150 | props->maxlvl = max; |
| 2151 | dprintk("Brightness levels: min=%d max=%d\n", props->offset, |
| 2152 | props->maxlvl); |
| 2153 | } |
| 2154 | |
| 2155 | static void sony_nc_backlight_setup(void) |
| 2156 | { |
| 2157 | acpi_handle unused; |
| 2158 | int max_brightness = 0; |
| 2159 | const struct backlight_ops *ops = NULL; |
| 2160 | struct backlight_properties props; |
| 2161 | |
| 2162 | if (sony_find_snc_handle(0x12f) != -1) { |
| 2163 | ops = &sony_backlight_ng_ops; |
| 2164 | sony_nc_backlight_ng_read_limits(0x12f, &sony_bl_props); |
| 2165 | max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset; |
| 2166 | |
| 2167 | } else if (sony_find_snc_handle(0x137) != -1) { |
| 2168 | ops = &sony_backlight_ng_ops; |
| 2169 | sony_nc_backlight_ng_read_limits(0x137, &sony_bl_props); |
| 2170 | max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset; |
| 2171 | |
| 2172 | } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT", |
| 2173 | &unused))) { |
| 2174 | ops = &sony_backlight_ops; |
| 2175 | max_brightness = SONY_MAX_BRIGHTNESS - 1; |
| 2176 | |
| 2177 | } else |
| 2178 | return; |
| 2179 | |
| 2180 | memset(&props, 0, sizeof(struct backlight_properties)); |
| 2181 | props.type = BACKLIGHT_PLATFORM; |
| 2182 | props.max_brightness = max_brightness; |
| 2183 | sony_bl_props.dev = backlight_device_register("sony", NULL, |
| 2184 | &sony_bl_props, |
| 2185 | ops, &props); |
| 2186 | |
| 2187 | if (IS_ERR(sony_bl_props.dev)) { |
| 2188 | pr_warn("unable to register backlight device\n"); |
| 2189 | sony_bl_props.dev = NULL; |
| 2190 | } else |
| 2191 | sony_bl_props.dev->props.brightness = |
| 2192 | ops->get_brightness(sony_bl_props.dev); |
| 2193 | } |
| 2194 | |
| 2195 | static void sony_nc_backlight_cleanup(void) |
| 2196 | { |
| 2197 | if (sony_bl_props.dev) |
| 2198 | backlight_device_unregister(sony_bl_props.dev); |
| 2199 | } |
| 2200 | |
| 2201 | static int sony_nc_add(struct acpi_device *device) |
| 2202 | { |
| 2203 | acpi_status status; |
| 2204 | int result = 0; |
| 2205 | acpi_handle handle; |
| 2206 | struct sony_nc_value *item; |
| 2207 | |
| 2208 | pr_info("%s v%s\n", SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION); |
| 2209 | |
| 2210 | sony_nc_acpi_device = device; |
| 2211 | strcpy(acpi_device_class(device), "sony/hotkey"); |
| 2212 | |
| 2213 | sony_nc_acpi_handle = device->handle; |
| 2214 | |
| 2215 | /* read device status */ |
| 2216 | result = acpi_bus_get_status(device); |
| 2217 | /* bail IFF the above call was successful and the device is not present */ |
| 2218 | if (!result && !device->status.present) { |
| 2219 | dprintk("Device not present\n"); |
| 2220 | result = -ENODEV; |
| 2221 | goto outwalk; |
| 2222 | } |
| 2223 | |
| 2224 | result = sony_pf_add(); |
| 2225 | if (result) |
| 2226 | goto outpresent; |
| 2227 | |
| 2228 | if (debug) { |
| 2229 | status = acpi_walk_namespace(ACPI_TYPE_METHOD, |
| 2230 | sony_nc_acpi_handle, 1, sony_walk_callback, |
| 2231 | NULL, NULL, NULL); |
| 2232 | if (ACPI_FAILURE(status)) { |
| 2233 | pr_warn("unable to walk acpi resources\n"); |
| 2234 | result = -ENODEV; |
| 2235 | goto outpresent; |
| 2236 | } |
| 2237 | } |
| 2238 | |
| 2239 | if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON", |
| 2240 | &handle))) { |
| 2241 | int arg = 1; |
| 2242 | if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL)) |
| 2243 | dprintk("ECON Method failed\n"); |
| 2244 | } |
| 2245 | |
| 2246 | if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00", |
| 2247 | &handle))) { |
| 2248 | dprintk("Doing SNC setup\n"); |
| 2249 | /* retrieve the available handles */ |
| 2250 | result = sony_nc_handles_setup(sony_pf_device); |
| 2251 | if (!result) |
| 2252 | sony_nc_function_setup(device, sony_pf_device); |
| 2253 | } |
| 2254 | |
| 2255 | /* setup input devices and helper fifo */ |
| 2256 | result = sony_laptop_setup_input(device); |
| 2257 | if (result) { |
| 2258 | pr_err("Unable to create input devices\n"); |
| 2259 | goto outsnc; |
| 2260 | } |
| 2261 | |
| 2262 | if (acpi_video_backlight_support()) { |
| 2263 | pr_info("brightness ignored, must be controlled by ACPI video driver\n"); |
| 2264 | } else { |
| 2265 | sony_nc_backlight_setup(); |
| 2266 | } |
| 2267 | |
| 2268 | /* create sony_pf sysfs attributes related to the SNC device */ |
| 2269 | for (item = sony_nc_values; item->name; ++item) { |
| 2270 | |
| 2271 | if (!debug && item->debug) |
| 2272 | continue; |
| 2273 | |
| 2274 | /* find the available acpiget as described in the DSDT */ |
| 2275 | for (; item->acpiget && *item->acpiget; ++item->acpiget) { |
| 2276 | if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, |
| 2277 | *item->acpiget, |
| 2278 | &handle))) { |
| 2279 | dprintk("Found %s getter: %s\n", |
| 2280 | item->name, *item->acpiget); |
| 2281 | item->devattr.attr.mode |= S_IRUGO; |
| 2282 | break; |
| 2283 | } |
| 2284 | } |
| 2285 | |
| 2286 | /* find the available acpiset as described in the DSDT */ |
| 2287 | for (; item->acpiset && *item->acpiset; ++item->acpiset) { |
| 2288 | if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, |
| 2289 | *item->acpiset, |
| 2290 | &handle))) { |
| 2291 | dprintk("Found %s setter: %s\n", |
| 2292 | item->name, *item->acpiset); |
| 2293 | item->devattr.attr.mode |= S_IWUSR; |
| 2294 | break; |
| 2295 | } |
| 2296 | } |
| 2297 | |
| 2298 | if (item->devattr.attr.mode != 0) { |
| 2299 | result = |
| 2300 | device_create_file(&sony_pf_device->dev, |
| 2301 | &item->devattr); |
| 2302 | if (result) |
| 2303 | goto out_sysfs; |
| 2304 | } |
| 2305 | } |
| 2306 | |
| 2307 | return 0; |
| 2308 | |
| 2309 | out_sysfs: |
| 2310 | for (item = sony_nc_values; item->name; ++item) { |
| 2311 | device_remove_file(&sony_pf_device->dev, &item->devattr); |
| 2312 | } |
| 2313 | sony_nc_backlight_cleanup(); |
| 2314 | |
| 2315 | sony_laptop_remove_input(); |
| 2316 | |
| 2317 | outsnc: |
| 2318 | sony_nc_function_cleanup(sony_pf_device); |
| 2319 | sony_nc_handles_cleanup(sony_pf_device); |
| 2320 | |
| 2321 | outpresent: |
| 2322 | sony_pf_remove(); |
| 2323 | |
| 2324 | outwalk: |
| 2325 | sony_nc_rfkill_cleanup(); |
| 2326 | return result; |
| 2327 | } |
| 2328 | |
| 2329 | static int sony_nc_remove(struct acpi_device *device, int type) |
| 2330 | { |
| 2331 | struct sony_nc_value *item; |
| 2332 | |
| 2333 | sony_nc_backlight_cleanup(); |
| 2334 | |
| 2335 | sony_nc_acpi_device = NULL; |
| 2336 | |
| 2337 | for (item = sony_nc_values; item->name; ++item) { |
| 2338 | device_remove_file(&sony_pf_device->dev, &item->devattr); |
| 2339 | } |
| 2340 | |
| 2341 | sony_nc_function_cleanup(sony_pf_device); |
| 2342 | sony_nc_handles_cleanup(sony_pf_device); |
| 2343 | sony_pf_remove(); |
| 2344 | sony_laptop_remove_input(); |
| 2345 | dprintk(SONY_NC_DRIVER_NAME " removed.\n"); |
| 2346 | |
| 2347 | return 0; |
| 2348 | } |
| 2349 | |
| 2350 | static const struct acpi_device_id sony_device_ids[] = { |
| 2351 | {SONY_NC_HID, 0}, |
| 2352 | {SONY_PIC_HID, 0}, |
| 2353 | {"", 0}, |
| 2354 | }; |
| 2355 | MODULE_DEVICE_TABLE(acpi, sony_device_ids); |
| 2356 | |
| 2357 | static const struct acpi_device_id sony_nc_device_ids[] = { |
| 2358 | {SONY_NC_HID, 0}, |
| 2359 | {"", 0}, |
| 2360 | }; |
| 2361 | |
| 2362 | static struct acpi_driver sony_nc_driver = { |
| 2363 | .name = SONY_NC_DRIVER_NAME, |
| 2364 | .class = SONY_NC_CLASS, |
| 2365 | .ids = sony_nc_device_ids, |
| 2366 | .owner = THIS_MODULE, |
| 2367 | .ops = { |
| 2368 | .add = sony_nc_add, |
| 2369 | .remove = sony_nc_remove, |
| 2370 | .resume = sony_nc_resume, |
| 2371 | .notify = sony_nc_notify, |
| 2372 | }, |
| 2373 | }; |
| 2374 | |
| 2375 | /*********** SPIC (SNY6001) Device ***********/ |
| 2376 | |
| 2377 | #define SONYPI_DEVICE_TYPE1 0x00000001 |
| 2378 | #define SONYPI_DEVICE_TYPE2 0x00000002 |
| 2379 | #define SONYPI_DEVICE_TYPE3 0x00000004 |
| 2380 | |
| 2381 | #define SONYPI_TYPE1_OFFSET 0x04 |
| 2382 | #define SONYPI_TYPE2_OFFSET 0x12 |
| 2383 | #define SONYPI_TYPE3_OFFSET 0x12 |
| 2384 | |
| 2385 | struct sony_pic_ioport { |
| 2386 | struct acpi_resource_io io1; |
| 2387 | struct acpi_resource_io io2; |
| 2388 | struct list_head list; |
| 2389 | }; |
| 2390 | |
| 2391 | struct sony_pic_irq { |
| 2392 | struct acpi_resource_irq irq; |
| 2393 | struct list_head list; |
| 2394 | }; |
| 2395 | |
| 2396 | struct sonypi_eventtypes { |
| 2397 | u8 data; |
| 2398 | unsigned long mask; |
| 2399 | struct sonypi_event *events; |
| 2400 | }; |
| 2401 | |
| 2402 | struct sony_pic_dev { |
| 2403 | struct acpi_device *acpi_dev; |
| 2404 | struct sony_pic_irq *cur_irq; |
| 2405 | struct sony_pic_ioport *cur_ioport; |
| 2406 | struct list_head interrupts; |
| 2407 | struct list_head ioports; |
| 2408 | struct mutex lock; |
| 2409 | struct sonypi_eventtypes *event_types; |
| 2410 | int (*handle_irq)(const u8, const u8); |
| 2411 | int model; |
| 2412 | u16 evport_offset; |
| 2413 | u8 camera_power; |
| 2414 | u8 bluetooth_power; |
| 2415 | u8 wwan_power; |
| 2416 | }; |
| 2417 | |
| 2418 | static struct sony_pic_dev spic_dev = { |
| 2419 | .interrupts = LIST_HEAD_INIT(spic_dev.interrupts), |
| 2420 | .ioports = LIST_HEAD_INIT(spic_dev.ioports), |
| 2421 | }; |
| 2422 | |
| 2423 | static int spic_drv_registered; |
| 2424 | |
| 2425 | /* Event masks */ |
| 2426 | #define SONYPI_JOGGER_MASK 0x00000001 |
| 2427 | #define SONYPI_CAPTURE_MASK 0x00000002 |
| 2428 | #define SONYPI_FNKEY_MASK 0x00000004 |
| 2429 | #define SONYPI_BLUETOOTH_MASK 0x00000008 |
| 2430 | #define SONYPI_PKEY_MASK 0x00000010 |
| 2431 | #define SONYPI_BACK_MASK 0x00000020 |
| 2432 | #define SONYPI_HELP_MASK 0x00000040 |
| 2433 | #define SONYPI_LID_MASK 0x00000080 |
| 2434 | #define SONYPI_ZOOM_MASK 0x00000100 |
| 2435 | #define SONYPI_THUMBPHRASE_MASK 0x00000200 |
| 2436 | #define SONYPI_MEYE_MASK 0x00000400 |
| 2437 | #define SONYPI_MEMORYSTICK_MASK 0x00000800 |
| 2438 | #define SONYPI_BATTERY_MASK 0x00001000 |
| 2439 | #define SONYPI_WIRELESS_MASK 0x00002000 |
| 2440 | |
| 2441 | struct sonypi_event { |
| 2442 | u8 data; |
| 2443 | u8 event; |
| 2444 | }; |
| 2445 | |
| 2446 | /* The set of possible button release events */ |
| 2447 | static struct sonypi_event sonypi_releaseev[] = { |
| 2448 | { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED }, |
| 2449 | { 0, 0 } |
| 2450 | }; |
| 2451 | |
| 2452 | /* The set of possible jogger events */ |
| 2453 | static struct sonypi_event sonypi_joggerev[] = { |
| 2454 | { 0x1f, SONYPI_EVENT_JOGDIAL_UP }, |
| 2455 | { 0x01, SONYPI_EVENT_JOGDIAL_DOWN }, |
| 2456 | { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED }, |
| 2457 | { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED }, |
| 2458 | { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP }, |
| 2459 | { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN }, |
| 2460 | { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED }, |
| 2461 | { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED }, |
| 2462 | { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP }, |
| 2463 | { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN }, |
| 2464 | { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED }, |
| 2465 | { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED }, |
| 2466 | { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED }, |
| 2467 | { 0, 0 } |
| 2468 | }; |
| 2469 | |
| 2470 | /* The set of possible capture button events */ |
| 2471 | static struct sonypi_event sonypi_captureev[] = { |
| 2472 | { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED }, |
| 2473 | { 0x07, SONYPI_EVENT_CAPTURE_PRESSED }, |
| 2474 | { 0x40, SONYPI_EVENT_CAPTURE_PRESSED }, |
| 2475 | { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED }, |
| 2476 | { 0, 0 } |
| 2477 | }; |
| 2478 | |
| 2479 | /* The set of possible fnkeys events */ |
| 2480 | static struct sonypi_event sonypi_fnkeyev[] = { |
| 2481 | { 0x10, SONYPI_EVENT_FNKEY_ESC }, |
| 2482 | { 0x11, SONYPI_EVENT_FNKEY_F1 }, |
| 2483 | { 0x12, SONYPI_EVENT_FNKEY_F2 }, |
| 2484 | { 0x13, SONYPI_EVENT_FNKEY_F3 }, |
| 2485 | { 0x14, SONYPI_EVENT_FNKEY_F4 }, |
| 2486 | { 0x15, SONYPI_EVENT_FNKEY_F5 }, |
| 2487 | { 0x16, SONYPI_EVENT_FNKEY_F6 }, |
| 2488 | { 0x17, SONYPI_EVENT_FNKEY_F7 }, |
| 2489 | { 0x18, SONYPI_EVENT_FNKEY_F8 }, |
| 2490 | { 0x19, SONYPI_EVENT_FNKEY_F9 }, |
| 2491 | { 0x1a, SONYPI_EVENT_FNKEY_F10 }, |
| 2492 | { 0x1b, SONYPI_EVENT_FNKEY_F11 }, |
| 2493 | { 0x1c, SONYPI_EVENT_FNKEY_F12 }, |
| 2494 | { 0x1f, SONYPI_EVENT_FNKEY_RELEASED }, |
| 2495 | { 0x21, SONYPI_EVENT_FNKEY_1 }, |
| 2496 | { 0x22, SONYPI_EVENT_FNKEY_2 }, |
| 2497 | { 0x31, SONYPI_EVENT_FNKEY_D }, |
| 2498 | { 0x32, SONYPI_EVENT_FNKEY_E }, |
| 2499 | { 0x33, SONYPI_EVENT_FNKEY_F }, |
| 2500 | { 0x34, SONYPI_EVENT_FNKEY_S }, |
| 2501 | { 0x35, SONYPI_EVENT_FNKEY_B }, |
| 2502 | { 0x36, SONYPI_EVENT_FNKEY_ONLY }, |
| 2503 | { 0, 0 } |
| 2504 | }; |
| 2505 | |
| 2506 | /* The set of possible program key events */ |
| 2507 | static struct sonypi_event sonypi_pkeyev[] = { |
| 2508 | { 0x01, SONYPI_EVENT_PKEY_P1 }, |
| 2509 | { 0x02, SONYPI_EVENT_PKEY_P2 }, |
| 2510 | { 0x04, SONYPI_EVENT_PKEY_P3 }, |
| 2511 | { 0x20, SONYPI_EVENT_PKEY_P1 }, |
| 2512 | { 0, 0 } |
| 2513 | }; |
| 2514 | |
| 2515 | /* The set of possible bluetooth events */ |
| 2516 | static struct sonypi_event sonypi_blueev[] = { |
| 2517 | { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED }, |
| 2518 | { 0x59, SONYPI_EVENT_BLUETOOTH_ON }, |
| 2519 | { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF }, |
| 2520 | { 0, 0 } |
| 2521 | }; |
| 2522 | |
| 2523 | /* The set of possible wireless events */ |
| 2524 | static struct sonypi_event sonypi_wlessev[] = { |
| 2525 | { 0x59, SONYPI_EVENT_IGNORE }, |
| 2526 | { 0x5a, SONYPI_EVENT_IGNORE }, |
| 2527 | { 0, 0 } |
| 2528 | }; |
| 2529 | |
| 2530 | /* The set of possible back button events */ |
| 2531 | static struct sonypi_event sonypi_backev[] = { |
| 2532 | { 0x20, SONYPI_EVENT_BACK_PRESSED }, |
| 2533 | { 0, 0 } |
| 2534 | }; |
| 2535 | |
| 2536 | /* The set of possible help button events */ |
| 2537 | static struct sonypi_event sonypi_helpev[] = { |
| 2538 | { 0x3b, SONYPI_EVENT_HELP_PRESSED }, |
| 2539 | { 0, 0 } |
| 2540 | }; |
| 2541 | |
| 2542 | |
| 2543 | /* The set of possible lid events */ |
| 2544 | static struct sonypi_event sonypi_lidev[] = { |
| 2545 | { 0x51, SONYPI_EVENT_LID_CLOSED }, |
| 2546 | { 0x50, SONYPI_EVENT_LID_OPENED }, |
| 2547 | { 0, 0 } |
| 2548 | }; |
| 2549 | |
| 2550 | /* The set of possible zoom events */ |
| 2551 | static struct sonypi_event sonypi_zoomev[] = { |
| 2552 | { 0x39, SONYPI_EVENT_ZOOM_PRESSED }, |
| 2553 | { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED }, |
| 2554 | { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED }, |
| 2555 | { 0x04, SONYPI_EVENT_ZOOM_PRESSED }, |
| 2556 | { 0, 0 } |
| 2557 | }; |
| 2558 | |
| 2559 | /* The set of possible thumbphrase events */ |
| 2560 | static struct sonypi_event sonypi_thumbphraseev[] = { |
| 2561 | { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED }, |
| 2562 | { 0, 0 } |
| 2563 | }; |
| 2564 | |
| 2565 | /* The set of possible motioneye camera events */ |
| 2566 | static struct sonypi_event sonypi_meyeev[] = { |
| 2567 | { 0x00, SONYPI_EVENT_MEYE_FACE }, |
| 2568 | { 0x01, SONYPI_EVENT_MEYE_OPPOSITE }, |
| 2569 | { 0, 0 } |
| 2570 | }; |
| 2571 | |
| 2572 | /* The set of possible memorystick events */ |
| 2573 | static struct sonypi_event sonypi_memorystickev[] = { |
| 2574 | { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT }, |
| 2575 | { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT }, |
| 2576 | { 0, 0 } |
| 2577 | }; |
| 2578 | |
| 2579 | /* The set of possible battery events */ |
| 2580 | static struct sonypi_event sonypi_batteryev[] = { |
| 2581 | { 0x20, SONYPI_EVENT_BATTERY_INSERT }, |
| 2582 | { 0x30, SONYPI_EVENT_BATTERY_REMOVE }, |
| 2583 | { 0, 0 } |
| 2584 | }; |
| 2585 | |
| 2586 | /* The set of possible volume events */ |
| 2587 | static struct sonypi_event sonypi_volumeev[] = { |
| 2588 | { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED }, |
| 2589 | { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED }, |
| 2590 | { 0, 0 } |
| 2591 | }; |
| 2592 | |
| 2593 | /* The set of possible brightness events */ |
| 2594 | static struct sonypi_event sonypi_brightnessev[] = { |
| 2595 | { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED }, |
| 2596 | { 0, 0 } |
| 2597 | }; |
| 2598 | |
| 2599 | static struct sonypi_eventtypes type1_events[] = { |
| 2600 | { 0, 0xffffffff, sonypi_releaseev }, |
| 2601 | { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev }, |
| 2602 | { 0x30, SONYPI_LID_MASK, sonypi_lidev }, |
| 2603 | { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev }, |
| 2604 | { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev }, |
| 2605 | { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev }, |
| 2606 | { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev }, |
| 2607 | { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev }, |
| 2608 | { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev }, |
| 2609 | { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev }, |
| 2610 | { 0 }, |
| 2611 | }; |
| 2612 | static struct sonypi_eventtypes type2_events[] = { |
| 2613 | { 0, 0xffffffff, sonypi_releaseev }, |
| 2614 | { 0x38, SONYPI_LID_MASK, sonypi_lidev }, |
| 2615 | { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev }, |
| 2616 | { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev }, |
| 2617 | { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev }, |
| 2618 | { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev }, |
| 2619 | { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev }, |
| 2620 | { 0x11, SONYPI_BACK_MASK, sonypi_backev }, |
| 2621 | { 0x21, SONYPI_HELP_MASK, sonypi_helpev }, |
| 2622 | { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev }, |
| 2623 | { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev }, |
| 2624 | { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev }, |
| 2625 | { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev }, |
| 2626 | { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev }, |
| 2627 | { 0 }, |
| 2628 | }; |
| 2629 | static struct sonypi_eventtypes type3_events[] = { |
| 2630 | { 0, 0xffffffff, sonypi_releaseev }, |
| 2631 | { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev }, |
| 2632 | { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev }, |
| 2633 | { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev }, |
| 2634 | { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev }, |
| 2635 | { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev }, |
| 2636 | { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev }, |
| 2637 | { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev }, |
| 2638 | { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev }, |
| 2639 | { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev }, |
| 2640 | { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev }, |
| 2641 | { 0 }, |
| 2642 | }; |
| 2643 | |
| 2644 | /* low level spic calls */ |
| 2645 | #define ITERATIONS_LONG 10000 |
| 2646 | #define ITERATIONS_SHORT 10 |
| 2647 | #define wait_on_command(command, iterations) { \ |
| 2648 | unsigned int n = iterations; \ |
| 2649 | while (--n && (command)) \ |
| 2650 | udelay(1); \ |
| 2651 | if (!n) \ |
| 2652 | dprintk("command failed at %s : %s (line %d)\n", \ |
| 2653 | __FILE__, __func__, __LINE__); \ |
| 2654 | } |
| 2655 | |
| 2656 | static u8 sony_pic_call1(u8 dev) |
| 2657 | { |
| 2658 | u8 v1, v2; |
| 2659 | |
| 2660 | wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, |
| 2661 | ITERATIONS_LONG); |
| 2662 | outb(dev, spic_dev.cur_ioport->io1.minimum + 4); |
| 2663 | v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4); |
| 2664 | v2 = inb_p(spic_dev.cur_ioport->io1.minimum); |
| 2665 | dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1); |
| 2666 | return v2; |
| 2667 | } |
| 2668 | |
| 2669 | static u8 sony_pic_call2(u8 dev, u8 fn) |
| 2670 | { |
| 2671 | u8 v1; |
| 2672 | |
| 2673 | wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, |
| 2674 | ITERATIONS_LONG); |
| 2675 | outb(dev, spic_dev.cur_ioport->io1.minimum + 4); |
| 2676 | wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, |
| 2677 | ITERATIONS_LONG); |
| 2678 | outb(fn, spic_dev.cur_ioport->io1.minimum); |
| 2679 | v1 = inb_p(spic_dev.cur_ioport->io1.minimum); |
| 2680 | dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1); |
| 2681 | return v1; |
| 2682 | } |
| 2683 | |
| 2684 | static u8 sony_pic_call3(u8 dev, u8 fn, u8 v) |
| 2685 | { |
| 2686 | u8 v1; |
| 2687 | |
| 2688 | wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG); |
| 2689 | outb(dev, spic_dev.cur_ioport->io1.minimum + 4); |
| 2690 | wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG); |
| 2691 | outb(fn, spic_dev.cur_ioport->io1.minimum); |
| 2692 | wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG); |
| 2693 | outb(v, spic_dev.cur_ioport->io1.minimum); |
| 2694 | v1 = inb_p(spic_dev.cur_ioport->io1.minimum); |
| 2695 | dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n", |
| 2696 | dev, fn, v, v1); |
| 2697 | return v1; |
| 2698 | } |
| 2699 | |
| 2700 | /* |
| 2701 | * minidrivers for SPIC models |
| 2702 | */ |
| 2703 | static int type3_handle_irq(const u8 data_mask, const u8 ev) |
| 2704 | { |
| 2705 | /* |
| 2706 | * 0x31 could mean we have to take some extra action and wait for |
| 2707 | * the next irq for some Type3 models, it will generate a new |
| 2708 | * irq and we can read new data from the device: |
| 2709 | * - 0x5c and 0x5f requires 0xA0 |
| 2710 | * - 0x61 requires 0xB3 |
| 2711 | */ |
| 2712 | if (data_mask == 0x31) { |
| 2713 | if (ev == 0x5c || ev == 0x5f) |
| 2714 | sony_pic_call1(0xA0); |
| 2715 | else if (ev == 0x61) |
| 2716 | sony_pic_call1(0xB3); |
| 2717 | return 0; |
| 2718 | } |
| 2719 | return 1; |
| 2720 | } |
| 2721 | |
| 2722 | static void sony_pic_detect_device_type(struct sony_pic_dev *dev) |
| 2723 | { |
| 2724 | struct pci_dev *pcidev; |
| 2725 | |
| 2726 | pcidev = pci_get_device(PCI_VENDOR_ID_INTEL, |
| 2727 | PCI_DEVICE_ID_INTEL_82371AB_3, NULL); |
| 2728 | if (pcidev) { |
| 2729 | dev->model = SONYPI_DEVICE_TYPE1; |
| 2730 | dev->evport_offset = SONYPI_TYPE1_OFFSET; |
| 2731 | dev->event_types = type1_events; |
| 2732 | goto out; |
| 2733 | } |
| 2734 | |
| 2735 | pcidev = pci_get_device(PCI_VENDOR_ID_INTEL, |
| 2736 | PCI_DEVICE_ID_INTEL_ICH6_1, NULL); |
| 2737 | if (pcidev) { |
| 2738 | dev->model = SONYPI_DEVICE_TYPE2; |
| 2739 | dev->evport_offset = SONYPI_TYPE2_OFFSET; |
| 2740 | dev->event_types = type2_events; |
| 2741 | goto out; |
| 2742 | } |
| 2743 | |
| 2744 | pcidev = pci_get_device(PCI_VENDOR_ID_INTEL, |
| 2745 | PCI_DEVICE_ID_INTEL_ICH7_1, NULL); |
| 2746 | if (pcidev) { |
| 2747 | dev->model = SONYPI_DEVICE_TYPE3; |
| 2748 | dev->handle_irq = type3_handle_irq; |
| 2749 | dev->evport_offset = SONYPI_TYPE3_OFFSET; |
| 2750 | dev->event_types = type3_events; |
| 2751 | goto out; |
| 2752 | } |
| 2753 | |
| 2754 | pcidev = pci_get_device(PCI_VENDOR_ID_INTEL, |
| 2755 | PCI_DEVICE_ID_INTEL_ICH8_4, NULL); |
| 2756 | if (pcidev) { |
| 2757 | dev->model = SONYPI_DEVICE_TYPE3; |
| 2758 | dev->handle_irq = type3_handle_irq; |
| 2759 | dev->evport_offset = SONYPI_TYPE3_OFFSET; |
| 2760 | dev->event_types = type3_events; |
| 2761 | goto out; |
| 2762 | } |
| 2763 | |
| 2764 | pcidev = pci_get_device(PCI_VENDOR_ID_INTEL, |
| 2765 | PCI_DEVICE_ID_INTEL_ICH9_1, NULL); |
| 2766 | if (pcidev) { |
| 2767 | dev->model = SONYPI_DEVICE_TYPE3; |
| 2768 | dev->handle_irq = type3_handle_irq; |
| 2769 | dev->evport_offset = SONYPI_TYPE3_OFFSET; |
| 2770 | dev->event_types = type3_events; |
| 2771 | goto out; |
| 2772 | } |
| 2773 | |
| 2774 | /* default */ |
| 2775 | dev->model = SONYPI_DEVICE_TYPE2; |
| 2776 | dev->evport_offset = SONYPI_TYPE2_OFFSET; |
| 2777 | dev->event_types = type2_events; |
| 2778 | |
| 2779 | out: |
| 2780 | if (pcidev) |
| 2781 | pci_dev_put(pcidev); |
| 2782 | |
| 2783 | pr_info("detected Type%d model\n", |
| 2784 | dev->model == SONYPI_DEVICE_TYPE1 ? 1 : |
| 2785 | dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3); |
| 2786 | } |
| 2787 | |
| 2788 | /* camera tests and poweron/poweroff */ |
| 2789 | #define SONYPI_CAMERA_PICTURE 5 |
| 2790 | #define SONYPI_CAMERA_CONTROL 0x10 |
| 2791 | |
| 2792 | #define SONYPI_CAMERA_BRIGHTNESS 0 |
| 2793 | #define SONYPI_CAMERA_CONTRAST 1 |
| 2794 | #define SONYPI_CAMERA_HUE 2 |
| 2795 | #define SONYPI_CAMERA_COLOR 3 |
| 2796 | #define SONYPI_CAMERA_SHARPNESS 4 |
| 2797 | |
| 2798 | #define SONYPI_CAMERA_EXPOSURE_MASK 0xC |
| 2799 | #define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3 |
| 2800 | #define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30 |
| 2801 | #define SONYPI_CAMERA_MUTE_MASK 0x40 |
| 2802 | |
| 2803 | /* the rest don't need a loop until not 0xff */ |
| 2804 | #define SONYPI_CAMERA_AGC 6 |
| 2805 | #define SONYPI_CAMERA_AGC_MASK 0x30 |
| 2806 | #define SONYPI_CAMERA_SHUTTER_MASK 0x7 |
| 2807 | |
| 2808 | #define SONYPI_CAMERA_SHUTDOWN_REQUEST 7 |
| 2809 | #define SONYPI_CAMERA_CONTROL 0x10 |
| 2810 | |
| 2811 | #define SONYPI_CAMERA_STATUS 7 |
| 2812 | #define SONYPI_CAMERA_STATUS_READY 0x2 |
| 2813 | #define SONYPI_CAMERA_STATUS_POSITION 0x4 |
| 2814 | |
| 2815 | #define SONYPI_DIRECTION_BACKWARDS 0x4 |
| 2816 | |
| 2817 | #define SONYPI_CAMERA_REVISION 8 |
| 2818 | #define SONYPI_CAMERA_ROMVERSION 9 |
| 2819 | |
| 2820 | static int __sony_pic_camera_ready(void) |
| 2821 | { |
| 2822 | u8 v; |
| 2823 | |
| 2824 | v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS); |
| 2825 | return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY)); |
| 2826 | } |
| 2827 | |
| 2828 | static int __sony_pic_camera_off(void) |
| 2829 | { |
| 2830 | if (!camera) { |
| 2831 | pr_warn("camera control not enabled\n"); |
| 2832 | return -ENODEV; |
| 2833 | } |
| 2834 | |
| 2835 | wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, |
| 2836 | SONYPI_CAMERA_MUTE_MASK), |
| 2837 | ITERATIONS_SHORT); |
| 2838 | |
| 2839 | if (spic_dev.camera_power) { |
| 2840 | sony_pic_call2(0x91, 0); |
| 2841 | spic_dev.camera_power = 0; |
| 2842 | } |
| 2843 | return 0; |
| 2844 | } |
| 2845 | |
| 2846 | static int __sony_pic_camera_on(void) |
| 2847 | { |
| 2848 | int i, j, x; |
| 2849 | |
| 2850 | if (!camera) { |
| 2851 | pr_warn("camera control not enabled\n"); |
| 2852 | return -ENODEV; |
| 2853 | } |
| 2854 | |
| 2855 | if (spic_dev.camera_power) |
| 2856 | return 0; |
| 2857 | |
| 2858 | for (j = 5; j > 0; j--) { |
| 2859 | |
| 2860 | for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++) |
| 2861 | msleep(10); |
| 2862 | sony_pic_call1(0x93); |
| 2863 | |
| 2864 | for (i = 400; i > 0; i--) { |
| 2865 | if (__sony_pic_camera_ready()) |
| 2866 | break; |
| 2867 | msleep(10); |
| 2868 | } |
| 2869 | if (i) |
| 2870 | break; |
| 2871 | } |
| 2872 | |
| 2873 | if (j == 0) { |
| 2874 | pr_warn("failed to power on camera\n"); |
| 2875 | return -ENODEV; |
| 2876 | } |
| 2877 | |
| 2878 | wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL, |
| 2879 | 0x5a), |
| 2880 | ITERATIONS_SHORT); |
| 2881 | |
| 2882 | spic_dev.camera_power = 1; |
| 2883 | return 0; |
| 2884 | } |
| 2885 | |
| 2886 | /* External camera command (exported to the motion eye v4l driver) */ |
| 2887 | int sony_pic_camera_command(int command, u8 value) |
| 2888 | { |
| 2889 | if (!camera) |
| 2890 | return -EIO; |
| 2891 | |
| 2892 | mutex_lock(&spic_dev.lock); |
| 2893 | |
| 2894 | switch (command) { |
| 2895 | case SONY_PIC_COMMAND_SETCAMERA: |
| 2896 | if (value) |
| 2897 | __sony_pic_camera_on(); |
| 2898 | else |
| 2899 | __sony_pic_camera_off(); |
| 2900 | break; |
| 2901 | case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS: |
| 2902 | wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value), |
| 2903 | ITERATIONS_SHORT); |
| 2904 | break; |
| 2905 | case SONY_PIC_COMMAND_SETCAMERACONTRAST: |
| 2906 | wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value), |
| 2907 | ITERATIONS_SHORT); |
| 2908 | break; |
| 2909 | case SONY_PIC_COMMAND_SETCAMERAHUE: |
| 2910 | wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value), |
| 2911 | ITERATIONS_SHORT); |
| 2912 | break; |
| 2913 | case SONY_PIC_COMMAND_SETCAMERACOLOR: |
| 2914 | wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value), |
| 2915 | ITERATIONS_SHORT); |
| 2916 | break; |
| 2917 | case SONY_PIC_COMMAND_SETCAMERASHARPNESS: |
| 2918 | wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value), |
| 2919 | ITERATIONS_SHORT); |
| 2920 | break; |
| 2921 | case SONY_PIC_COMMAND_SETCAMERAPICTURE: |
| 2922 | wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value), |
| 2923 | ITERATIONS_SHORT); |
| 2924 | break; |
| 2925 | case SONY_PIC_COMMAND_SETCAMERAAGC: |
| 2926 | wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value), |
| 2927 | ITERATIONS_SHORT); |
| 2928 | break; |
| 2929 | default: |
| 2930 | pr_err("sony_pic_camera_command invalid: %d\n", command); |
| 2931 | break; |
| 2932 | } |
| 2933 | mutex_unlock(&spic_dev.lock); |
| 2934 | return 0; |
| 2935 | } |
| 2936 | EXPORT_SYMBOL(sony_pic_camera_command); |
| 2937 | |
| 2938 | /* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */ |
| 2939 | static void __sony_pic_set_wwanpower(u8 state) |
| 2940 | { |
| 2941 | state = !!state; |
| 2942 | if (spic_dev.wwan_power == state) |
| 2943 | return; |
| 2944 | sony_pic_call2(0xB0, state); |
| 2945 | sony_pic_call1(0x82); |
| 2946 | spic_dev.wwan_power = state; |
| 2947 | } |
| 2948 | |
| 2949 | static ssize_t sony_pic_wwanpower_store(struct device *dev, |
| 2950 | struct device_attribute *attr, |
| 2951 | const char *buffer, size_t count) |
| 2952 | { |
| 2953 | unsigned long value; |
| 2954 | if (count > 31) |
| 2955 | return -EINVAL; |
| 2956 | |
| 2957 | if (kstrtoul(buffer, 10, &value)) |
| 2958 | return -EINVAL; |
| 2959 | |
| 2960 | mutex_lock(&spic_dev.lock); |
| 2961 | __sony_pic_set_wwanpower(value); |
| 2962 | mutex_unlock(&spic_dev.lock); |
| 2963 | |
| 2964 | return count; |
| 2965 | } |
| 2966 | |
| 2967 | static ssize_t sony_pic_wwanpower_show(struct device *dev, |
| 2968 | struct device_attribute *attr, char *buffer) |
| 2969 | { |
| 2970 | ssize_t count; |
| 2971 | mutex_lock(&spic_dev.lock); |
| 2972 | count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power); |
| 2973 | mutex_unlock(&spic_dev.lock); |
| 2974 | return count; |
| 2975 | } |
| 2976 | |
| 2977 | /* bluetooth subsystem power state */ |
| 2978 | static void __sony_pic_set_bluetoothpower(u8 state) |
| 2979 | { |
| 2980 | state = !!state; |
| 2981 | if (spic_dev.bluetooth_power == state) |
| 2982 | return; |
| 2983 | sony_pic_call2(0x96, state); |
| 2984 | sony_pic_call1(0x82); |
| 2985 | spic_dev.bluetooth_power = state; |
| 2986 | } |
| 2987 | |
| 2988 | static ssize_t sony_pic_bluetoothpower_store(struct device *dev, |
| 2989 | struct device_attribute *attr, |
| 2990 | const char *buffer, size_t count) |
| 2991 | { |
| 2992 | unsigned long value; |
| 2993 | if (count > 31) |
| 2994 | return -EINVAL; |
| 2995 | |
| 2996 | if (kstrtoul(buffer, 10, &value)) |
| 2997 | return -EINVAL; |
| 2998 | |
| 2999 | mutex_lock(&spic_dev.lock); |
| 3000 | __sony_pic_set_bluetoothpower(value); |
| 3001 | mutex_unlock(&spic_dev.lock); |
| 3002 | |
| 3003 | return count; |
| 3004 | } |
| 3005 | |
| 3006 | static ssize_t sony_pic_bluetoothpower_show(struct device *dev, |
| 3007 | struct device_attribute *attr, char *buffer) |
| 3008 | { |
| 3009 | ssize_t count = 0; |
| 3010 | mutex_lock(&spic_dev.lock); |
| 3011 | count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power); |
| 3012 | mutex_unlock(&spic_dev.lock); |
| 3013 | return count; |
| 3014 | } |
| 3015 | |
| 3016 | /* fan speed */ |
| 3017 | /* FAN0 information (reverse engineered from ACPI tables) */ |
| 3018 | #define SONY_PIC_FAN0_STATUS 0x93 |
| 3019 | static int sony_pic_set_fanspeed(unsigned long value) |
| 3020 | { |
| 3021 | return ec_write(SONY_PIC_FAN0_STATUS, value); |
| 3022 | } |
| 3023 | |
| 3024 | static int sony_pic_get_fanspeed(u8 *value) |
| 3025 | { |
| 3026 | return ec_read(SONY_PIC_FAN0_STATUS, value); |
| 3027 | } |
| 3028 | |
| 3029 | static ssize_t sony_pic_fanspeed_store(struct device *dev, |
| 3030 | struct device_attribute *attr, |
| 3031 | const char *buffer, size_t count) |
| 3032 | { |
| 3033 | unsigned long value; |
| 3034 | if (count > 31) |
| 3035 | return -EINVAL; |
| 3036 | |
| 3037 | if (kstrtoul(buffer, 10, &value)) |
| 3038 | return -EINVAL; |
| 3039 | |
| 3040 | if (sony_pic_set_fanspeed(value)) |
| 3041 | return -EIO; |
| 3042 | |
| 3043 | return count; |
| 3044 | } |
| 3045 | |
| 3046 | static ssize_t sony_pic_fanspeed_show(struct device *dev, |
| 3047 | struct device_attribute *attr, char *buffer) |
| 3048 | { |
| 3049 | u8 value = 0; |
| 3050 | if (sony_pic_get_fanspeed(&value)) |
| 3051 | return -EIO; |
| 3052 | |
| 3053 | return snprintf(buffer, PAGE_SIZE, "%d\n", value); |
| 3054 | } |
| 3055 | |
| 3056 | #define SPIC_ATTR(_name, _mode) \ |
| 3057 | struct device_attribute spic_attr_##_name = __ATTR(_name, \ |
| 3058 | _mode, sony_pic_## _name ##_show, \ |
| 3059 | sony_pic_## _name ##_store) |
| 3060 | |
| 3061 | static SPIC_ATTR(bluetoothpower, 0644); |
| 3062 | static SPIC_ATTR(wwanpower, 0644); |
| 3063 | static SPIC_ATTR(fanspeed, 0644); |
| 3064 | |
| 3065 | static struct attribute *spic_attributes[] = { |
| 3066 | &spic_attr_bluetoothpower.attr, |
| 3067 | &spic_attr_wwanpower.attr, |
| 3068 | &spic_attr_fanspeed.attr, |
| 3069 | NULL |
| 3070 | }; |
| 3071 | |
| 3072 | static struct attribute_group spic_attribute_group = { |
| 3073 | .attrs = spic_attributes |
| 3074 | }; |
| 3075 | |
| 3076 | /******** SONYPI compatibility **********/ |
| 3077 | #ifdef CONFIG_SONYPI_COMPAT |
| 3078 | |
| 3079 | /* battery / brightness / temperature addresses */ |
| 3080 | #define SONYPI_BAT_FLAGS 0x81 |
| 3081 | #define SONYPI_LCD_LIGHT 0x96 |
| 3082 | #define SONYPI_BAT1_PCTRM 0xa0 |
| 3083 | #define SONYPI_BAT1_LEFT 0xa2 |
| 3084 | #define SONYPI_BAT1_MAXRT 0xa4 |
| 3085 | #define SONYPI_BAT2_PCTRM 0xa8 |
| 3086 | #define SONYPI_BAT2_LEFT 0xaa |
| 3087 | #define SONYPI_BAT2_MAXRT 0xac |
| 3088 | #define SONYPI_BAT1_MAXTK 0xb0 |
| 3089 | #define SONYPI_BAT1_FULL 0xb2 |
| 3090 | #define SONYPI_BAT2_MAXTK 0xb8 |
| 3091 | #define SONYPI_BAT2_FULL 0xba |
| 3092 | #define SONYPI_TEMP_STATUS 0xC1 |
| 3093 | |
| 3094 | struct sonypi_compat_s { |
| 3095 | struct fasync_struct *fifo_async; |
| 3096 | struct kfifo fifo; |
| 3097 | spinlock_t fifo_lock; |
| 3098 | wait_queue_head_t fifo_proc_list; |
| 3099 | atomic_t open_count; |
| 3100 | }; |
| 3101 | static struct sonypi_compat_s sonypi_compat = { |
| 3102 | .open_count = ATOMIC_INIT(0), |
| 3103 | }; |
| 3104 | |
| 3105 | static int sonypi_misc_fasync(int fd, struct file *filp, int on) |
| 3106 | { |
| 3107 | return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async); |
| 3108 | } |
| 3109 | |
| 3110 | static int sonypi_misc_release(struct inode *inode, struct file *file) |
| 3111 | { |
| 3112 | atomic_dec(&sonypi_compat.open_count); |
| 3113 | return 0; |
| 3114 | } |
| 3115 | |
| 3116 | static int sonypi_misc_open(struct inode *inode, struct file *file) |
| 3117 | { |
| 3118 | /* Flush input queue on first open */ |
| 3119 | unsigned long flags; |
| 3120 | |
| 3121 | spin_lock_irqsave(&sonypi_compat.fifo_lock, flags); |
| 3122 | |
| 3123 | if (atomic_inc_return(&sonypi_compat.open_count) == 1) |
| 3124 | kfifo_reset(&sonypi_compat.fifo); |
| 3125 | |
| 3126 | spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags); |
| 3127 | |
| 3128 | return 0; |
| 3129 | } |
| 3130 | |
| 3131 | static ssize_t sonypi_misc_read(struct file *file, char __user *buf, |
| 3132 | size_t count, loff_t *pos) |
| 3133 | { |
| 3134 | ssize_t ret; |
| 3135 | unsigned char c; |
| 3136 | |
| 3137 | if ((kfifo_len(&sonypi_compat.fifo) == 0) && |
| 3138 | (file->f_flags & O_NONBLOCK)) |
| 3139 | return -EAGAIN; |
| 3140 | |
| 3141 | ret = wait_event_interruptible(sonypi_compat.fifo_proc_list, |
| 3142 | kfifo_len(&sonypi_compat.fifo) != 0); |
| 3143 | if (ret) |
| 3144 | return ret; |
| 3145 | |
| 3146 | while (ret < count && |
| 3147 | (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c), |
| 3148 | &sonypi_compat.fifo_lock) == sizeof(c))) { |
| 3149 | if (put_user(c, buf++)) |
| 3150 | return -EFAULT; |
| 3151 | ret++; |
| 3152 | } |
| 3153 | |
| 3154 | if (ret > 0) { |
| 3155 | struct inode *inode = file->f_path.dentry->d_inode; |
| 3156 | inode->i_atime = current_fs_time(inode->i_sb); |
| 3157 | } |
| 3158 | |
| 3159 | return ret; |
| 3160 | } |
| 3161 | |
| 3162 | static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait) |
| 3163 | { |
| 3164 | poll_wait(file, &sonypi_compat.fifo_proc_list, wait); |
| 3165 | if (kfifo_len(&sonypi_compat.fifo)) |
| 3166 | return POLLIN | POLLRDNORM; |
| 3167 | return 0; |
| 3168 | } |
| 3169 | |
| 3170 | static int ec_read16(u8 addr, u16 *value) |
| 3171 | { |
| 3172 | u8 val_lb, val_hb; |
| 3173 | if (ec_read(addr, &val_lb)) |
| 3174 | return -1; |
| 3175 | if (ec_read(addr + 1, &val_hb)) |
| 3176 | return -1; |
| 3177 | *value = val_lb | (val_hb << 8); |
| 3178 | return 0; |
| 3179 | } |
| 3180 | |
| 3181 | static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd, |
| 3182 | unsigned long arg) |
| 3183 | { |
| 3184 | int ret = 0; |
| 3185 | void __user *argp = (void __user *)arg; |
| 3186 | u8 val8; |
| 3187 | u16 val16; |
| 3188 | int value; |
| 3189 | |
| 3190 | mutex_lock(&spic_dev.lock); |
| 3191 | switch (cmd) { |
| 3192 | case SONYPI_IOCGBRT: |
| 3193 | if (sony_bl_props.dev == NULL) { |
| 3194 | ret = -EIO; |
| 3195 | break; |
| 3196 | } |
| 3197 | if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL, |
| 3198 | &value)) { |
| 3199 | ret = -EIO; |
| 3200 | break; |
| 3201 | } |
| 3202 | val8 = ((value & 0xff) - 1) << 5; |
| 3203 | if (copy_to_user(argp, &val8, sizeof(val8))) |
| 3204 | ret = -EFAULT; |
| 3205 | break; |
| 3206 | case SONYPI_IOCSBRT: |
| 3207 | if (sony_bl_props.dev == NULL) { |
| 3208 | ret = -EIO; |
| 3209 | break; |
| 3210 | } |
| 3211 | if (copy_from_user(&val8, argp, sizeof(val8))) { |
| 3212 | ret = -EFAULT; |
| 3213 | break; |
| 3214 | } |
| 3215 | value = (val8 >> 5) + 1; |
| 3216 | if (sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &value, |
| 3217 | NULL)) { |
| 3218 | ret = -EIO; |
| 3219 | break; |
| 3220 | } |
| 3221 | /* sync the backlight device status */ |
| 3222 | sony_bl_props.dev->props.brightness = |
| 3223 | sony_backlight_get_brightness(sony_bl_props.dev); |
| 3224 | break; |
| 3225 | case SONYPI_IOCGBAT1CAP: |
| 3226 | if (ec_read16(SONYPI_BAT1_FULL, &val16)) { |
| 3227 | ret = -EIO; |
| 3228 | break; |
| 3229 | } |
| 3230 | if (copy_to_user(argp, &val16, sizeof(val16))) |
| 3231 | ret = -EFAULT; |
| 3232 | break; |
| 3233 | case SONYPI_IOCGBAT1REM: |
| 3234 | if (ec_read16(SONYPI_BAT1_LEFT, &val16)) { |
| 3235 | ret = -EIO; |
| 3236 | break; |
| 3237 | } |
| 3238 | if (copy_to_user(argp, &val16, sizeof(val16))) |
| 3239 | ret = -EFAULT; |
| 3240 | break; |
| 3241 | case SONYPI_IOCGBAT2CAP: |
| 3242 | if (ec_read16(SONYPI_BAT2_FULL, &val16)) { |
| 3243 | ret = -EIO; |
| 3244 | break; |
| 3245 | } |
| 3246 | if (copy_to_user(argp, &val16, sizeof(val16))) |
| 3247 | ret = -EFAULT; |
| 3248 | break; |
| 3249 | case SONYPI_IOCGBAT2REM: |
| 3250 | if (ec_read16(SONYPI_BAT2_LEFT, &val16)) { |
| 3251 | ret = -EIO; |
| 3252 | break; |
| 3253 | } |
| 3254 | if (copy_to_user(argp, &val16, sizeof(val16))) |
| 3255 | ret = -EFAULT; |
| 3256 | break; |
| 3257 | case SONYPI_IOCGBATFLAGS: |
| 3258 | if (ec_read(SONYPI_BAT_FLAGS, &val8)) { |
| 3259 | ret = -EIO; |
| 3260 | break; |
| 3261 | } |
| 3262 | val8 &= 0x07; |
| 3263 | if (copy_to_user(argp, &val8, sizeof(val8))) |
| 3264 | ret = -EFAULT; |
| 3265 | break; |
| 3266 | case SONYPI_IOCGBLUE: |
| 3267 | val8 = spic_dev.bluetooth_power; |
| 3268 | if (copy_to_user(argp, &val8, sizeof(val8))) |
| 3269 | ret = -EFAULT; |
| 3270 | break; |
| 3271 | case SONYPI_IOCSBLUE: |
| 3272 | if (copy_from_user(&val8, argp, sizeof(val8))) { |
| 3273 | ret = -EFAULT; |
| 3274 | break; |
| 3275 | } |
| 3276 | __sony_pic_set_bluetoothpower(val8); |
| 3277 | break; |
| 3278 | /* FAN Controls */ |
| 3279 | case SONYPI_IOCGFAN: |
| 3280 | if (sony_pic_get_fanspeed(&val8)) { |
| 3281 | ret = -EIO; |
| 3282 | break; |
| 3283 | } |
| 3284 | if (copy_to_user(argp, &val8, sizeof(val8))) |
| 3285 | ret = -EFAULT; |
| 3286 | break; |
| 3287 | case SONYPI_IOCSFAN: |
| 3288 | if (copy_from_user(&val8, argp, sizeof(val8))) { |
| 3289 | ret = -EFAULT; |
| 3290 | break; |
| 3291 | } |
| 3292 | if (sony_pic_set_fanspeed(val8)) |
| 3293 | ret = -EIO; |
| 3294 | break; |
| 3295 | /* GET Temperature (useful under APM) */ |
| 3296 | case SONYPI_IOCGTEMP: |
| 3297 | if (ec_read(SONYPI_TEMP_STATUS, &val8)) { |
| 3298 | ret = -EIO; |
| 3299 | break; |
| 3300 | } |
| 3301 | if (copy_to_user(argp, &val8, sizeof(val8))) |
| 3302 | ret = -EFAULT; |
| 3303 | break; |
| 3304 | default: |
| 3305 | ret = -EINVAL; |
| 3306 | } |
| 3307 | mutex_unlock(&spic_dev.lock); |
| 3308 | return ret; |
| 3309 | } |
| 3310 | |
| 3311 | static const struct file_operations sonypi_misc_fops = { |
| 3312 | .owner = THIS_MODULE, |
| 3313 | .read = sonypi_misc_read, |
| 3314 | .poll = sonypi_misc_poll, |
| 3315 | .open = sonypi_misc_open, |
| 3316 | .release = sonypi_misc_release, |
| 3317 | .fasync = sonypi_misc_fasync, |
| 3318 | .unlocked_ioctl = sonypi_misc_ioctl, |
| 3319 | .llseek = noop_llseek, |
| 3320 | }; |
| 3321 | |
| 3322 | static struct miscdevice sonypi_misc_device = { |
| 3323 | .minor = MISC_DYNAMIC_MINOR, |
| 3324 | .name = "sonypi", |
| 3325 | .fops = &sonypi_misc_fops, |
| 3326 | }; |
| 3327 | |
| 3328 | static void sonypi_compat_report_event(u8 event) |
| 3329 | { |
| 3330 | kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event, |
| 3331 | sizeof(event), &sonypi_compat.fifo_lock); |
| 3332 | kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN); |
| 3333 | wake_up_interruptible(&sonypi_compat.fifo_proc_list); |
| 3334 | } |
| 3335 | |
| 3336 | static int sonypi_compat_init(void) |
| 3337 | { |
| 3338 | int error; |
| 3339 | |
| 3340 | spin_lock_init(&sonypi_compat.fifo_lock); |
| 3341 | error = |
| 3342 | kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL); |
| 3343 | if (error) { |
| 3344 | pr_err("kfifo_alloc failed\n"); |
| 3345 | return error; |
| 3346 | } |
| 3347 | |
| 3348 | init_waitqueue_head(&sonypi_compat.fifo_proc_list); |
| 3349 | |
| 3350 | if (minor != -1) |
| 3351 | sonypi_misc_device.minor = minor; |
| 3352 | error = misc_register(&sonypi_misc_device); |
| 3353 | if (error) { |
| 3354 | pr_err("misc_register failed\n"); |
| 3355 | goto err_free_kfifo; |
| 3356 | } |
| 3357 | if (minor == -1) |
| 3358 | pr_info("device allocated minor is %d\n", |
| 3359 | sonypi_misc_device.minor); |
| 3360 | |
| 3361 | return 0; |
| 3362 | |
| 3363 | err_free_kfifo: |
| 3364 | kfifo_free(&sonypi_compat.fifo); |
| 3365 | return error; |
| 3366 | } |
| 3367 | |
| 3368 | static void sonypi_compat_exit(void) |
| 3369 | { |
| 3370 | misc_deregister(&sonypi_misc_device); |
| 3371 | kfifo_free(&sonypi_compat.fifo); |
| 3372 | } |
| 3373 | #else |
| 3374 | static int sonypi_compat_init(void) { return 0; } |
| 3375 | static void sonypi_compat_exit(void) { } |
| 3376 | static void sonypi_compat_report_event(u8 event) { } |
| 3377 | #endif /* CONFIG_SONYPI_COMPAT */ |
| 3378 | |
| 3379 | /* |
| 3380 | * ACPI callbacks |
| 3381 | */ |
| 3382 | static acpi_status |
| 3383 | sony_pic_read_possible_resource(struct acpi_resource *resource, void *context) |
| 3384 | { |
| 3385 | u32 i; |
| 3386 | struct sony_pic_dev *dev = (struct sony_pic_dev *)context; |
| 3387 | |
| 3388 | switch (resource->type) { |
| 3389 | case ACPI_RESOURCE_TYPE_START_DEPENDENT: |
| 3390 | { |
| 3391 | /* start IO enumeration */ |
| 3392 | struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL); |
| 3393 | if (!ioport) |
| 3394 | return AE_ERROR; |
| 3395 | |
| 3396 | list_add(&ioport->list, &dev->ioports); |
| 3397 | return AE_OK; |
| 3398 | } |
| 3399 | |
| 3400 | case ACPI_RESOURCE_TYPE_END_DEPENDENT: |
| 3401 | /* end IO enumeration */ |
| 3402 | return AE_OK; |
| 3403 | |
| 3404 | case ACPI_RESOURCE_TYPE_IRQ: |
| 3405 | { |
| 3406 | struct acpi_resource_irq *p = &resource->data.irq; |
| 3407 | struct sony_pic_irq *interrupt = NULL; |
| 3408 | if (!p || !p->interrupt_count) { |
| 3409 | /* |
| 3410 | * IRQ descriptors may have no IRQ# bits set, |
| 3411 | * particularly those those w/ _STA disabled |
| 3412 | */ |
| 3413 | dprintk("Blank IRQ resource\n"); |
| 3414 | return AE_OK; |
| 3415 | } |
| 3416 | for (i = 0; i < p->interrupt_count; i++) { |
| 3417 | if (!p->interrupts[i]) { |
| 3418 | pr_warn("Invalid IRQ %d\n", |
| 3419 | p->interrupts[i]); |
| 3420 | continue; |
| 3421 | } |
| 3422 | interrupt = kzalloc(sizeof(*interrupt), |
| 3423 | GFP_KERNEL); |
| 3424 | if (!interrupt) |
| 3425 | return AE_ERROR; |
| 3426 | |
| 3427 | list_add(&interrupt->list, &dev->interrupts); |
| 3428 | interrupt->irq.triggering = p->triggering; |
| 3429 | interrupt->irq.polarity = p->polarity; |
| 3430 | interrupt->irq.sharable = p->sharable; |
| 3431 | interrupt->irq.interrupt_count = 1; |
| 3432 | interrupt->irq.interrupts[0] = p->interrupts[i]; |
| 3433 | } |
| 3434 | return AE_OK; |
| 3435 | } |
| 3436 | case ACPI_RESOURCE_TYPE_IO: |
| 3437 | { |
| 3438 | struct acpi_resource_io *io = &resource->data.io; |
| 3439 | struct sony_pic_ioport *ioport = |
| 3440 | list_first_entry(&dev->ioports, struct sony_pic_ioport, list); |
| 3441 | if (!io) { |
| 3442 | dprintk("Blank IO resource\n"); |
| 3443 | return AE_OK; |
| 3444 | } |
| 3445 | |
| 3446 | if (!ioport->io1.minimum) { |
| 3447 | memcpy(&ioport->io1, io, sizeof(*io)); |
| 3448 | dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum, |
| 3449 | ioport->io1.address_length); |
| 3450 | } |
| 3451 | else if (!ioport->io2.minimum) { |
| 3452 | memcpy(&ioport->io2, io, sizeof(*io)); |
| 3453 | dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum, |
| 3454 | ioport->io2.address_length); |
| 3455 | } |
| 3456 | else { |
| 3457 | pr_err("Unknown SPIC Type, more than 2 IO Ports\n"); |
| 3458 | return AE_ERROR; |
| 3459 | } |
| 3460 | return AE_OK; |
| 3461 | } |
| 3462 | default: |
| 3463 | dprintk("Resource %d isn't an IRQ nor an IO port\n", |
| 3464 | resource->type); |
| 3465 | |
| 3466 | case ACPI_RESOURCE_TYPE_END_TAG: |
| 3467 | return AE_OK; |
| 3468 | } |
| 3469 | return AE_CTRL_TERMINATE; |
| 3470 | } |
| 3471 | |
| 3472 | static int sony_pic_possible_resources(struct acpi_device *device) |
| 3473 | { |
| 3474 | int result = 0; |
| 3475 | acpi_status status = AE_OK; |
| 3476 | |
| 3477 | if (!device) |
| 3478 | return -EINVAL; |
| 3479 | |
| 3480 | /* get device status */ |
| 3481 | /* see acpi_pci_link_get_current acpi_pci_link_get_possible */ |
| 3482 | dprintk("Evaluating _STA\n"); |
| 3483 | result = acpi_bus_get_status(device); |
| 3484 | if (result) { |
| 3485 | pr_warn("Unable to read status\n"); |
| 3486 | goto end; |
| 3487 | } |
| 3488 | |
| 3489 | if (!device->status.enabled) |
| 3490 | dprintk("Device disabled\n"); |
| 3491 | else |
| 3492 | dprintk("Device enabled\n"); |
| 3493 | |
| 3494 | /* |
| 3495 | * Query and parse 'method' |
| 3496 | */ |
| 3497 | dprintk("Evaluating %s\n", METHOD_NAME__PRS); |
| 3498 | status = acpi_walk_resources(device->handle, METHOD_NAME__PRS, |
| 3499 | sony_pic_read_possible_resource, &spic_dev); |
| 3500 | if (ACPI_FAILURE(status)) { |
| 3501 | pr_warn("Failure evaluating %s\n", METHOD_NAME__PRS); |
| 3502 | result = -ENODEV; |
| 3503 | } |
| 3504 | end: |
| 3505 | return result; |
| 3506 | } |
| 3507 | |
| 3508 | /* |
| 3509 | * Disable the spic device by calling its _DIS method |
| 3510 | */ |
| 3511 | static int sony_pic_disable(struct acpi_device *device) |
| 3512 | { |
| 3513 | acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL, |
| 3514 | NULL); |
| 3515 | |
| 3516 | if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND) |
| 3517 | return -ENXIO; |
| 3518 | |
| 3519 | dprintk("Device disabled\n"); |
| 3520 | return 0; |
| 3521 | } |
| 3522 | |
| 3523 | |
| 3524 | /* |
| 3525 | * Based on drivers/acpi/pci_link.c:acpi_pci_link_set |
| 3526 | * |
| 3527 | * Call _SRS to set current resources |
| 3528 | */ |
| 3529 | static int sony_pic_enable(struct acpi_device *device, |
| 3530 | struct sony_pic_ioport *ioport, struct sony_pic_irq *irq) |
| 3531 | { |
| 3532 | acpi_status status; |
| 3533 | int result = 0; |
| 3534 | /* Type 1 resource layout is: |
| 3535 | * IO |
| 3536 | * IO |
| 3537 | * IRQNoFlags |
| 3538 | * End |
| 3539 | * |
| 3540 | * Type 2 and 3 resource layout is: |
| 3541 | * IO |
| 3542 | * IRQNoFlags |
| 3543 | * End |
| 3544 | */ |
| 3545 | struct { |
| 3546 | struct acpi_resource res1; |
| 3547 | struct acpi_resource res2; |
| 3548 | struct acpi_resource res3; |
| 3549 | struct acpi_resource res4; |
| 3550 | } *resource; |
| 3551 | struct acpi_buffer buffer = { 0, NULL }; |
| 3552 | |
| 3553 | if (!ioport || !irq) |
| 3554 | return -EINVAL; |
| 3555 | |
| 3556 | /* init acpi_buffer */ |
| 3557 | resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL); |
| 3558 | if (!resource) |
| 3559 | return -ENOMEM; |
| 3560 | |
| 3561 | buffer.length = sizeof(*resource) + 1; |
| 3562 | buffer.pointer = resource; |
| 3563 | |
| 3564 | /* setup Type 1 resources */ |
| 3565 | if (spic_dev.model == SONYPI_DEVICE_TYPE1) { |
| 3566 | |
| 3567 | /* setup io resources */ |
| 3568 | resource->res1.type = ACPI_RESOURCE_TYPE_IO; |
| 3569 | resource->res1.length = sizeof(struct acpi_resource); |
| 3570 | memcpy(&resource->res1.data.io, &ioport->io1, |
| 3571 | sizeof(struct acpi_resource_io)); |
| 3572 | |
| 3573 | resource->res2.type = ACPI_RESOURCE_TYPE_IO; |
| 3574 | resource->res2.length = sizeof(struct acpi_resource); |
| 3575 | memcpy(&resource->res2.data.io, &ioport->io2, |
| 3576 | sizeof(struct acpi_resource_io)); |
| 3577 | |
| 3578 | /* setup irq resource */ |
| 3579 | resource->res3.type = ACPI_RESOURCE_TYPE_IRQ; |
| 3580 | resource->res3.length = sizeof(struct acpi_resource); |
| 3581 | memcpy(&resource->res3.data.irq, &irq->irq, |
| 3582 | sizeof(struct acpi_resource_irq)); |
| 3583 | /* we requested a shared irq */ |
| 3584 | resource->res3.data.irq.sharable = ACPI_SHARED; |
| 3585 | |
| 3586 | resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG; |
| 3587 | |
| 3588 | } |
| 3589 | /* setup Type 2/3 resources */ |
| 3590 | else { |
| 3591 | /* setup io resource */ |
| 3592 | resource->res1.type = ACPI_RESOURCE_TYPE_IO; |
| 3593 | resource->res1.length = sizeof(struct acpi_resource); |
| 3594 | memcpy(&resource->res1.data.io, &ioport->io1, |
| 3595 | sizeof(struct acpi_resource_io)); |
| 3596 | |
| 3597 | /* setup irq resource */ |
| 3598 | resource->res2.type = ACPI_RESOURCE_TYPE_IRQ; |
| 3599 | resource->res2.length = sizeof(struct acpi_resource); |
| 3600 | memcpy(&resource->res2.data.irq, &irq->irq, |
| 3601 | sizeof(struct acpi_resource_irq)); |
| 3602 | /* we requested a shared irq */ |
| 3603 | resource->res2.data.irq.sharable = ACPI_SHARED; |
| 3604 | |
| 3605 | resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG; |
| 3606 | } |
| 3607 | |
| 3608 | /* Attempt to set the resource */ |
| 3609 | dprintk("Evaluating _SRS\n"); |
| 3610 | status = acpi_set_current_resources(device->handle, &buffer); |
| 3611 | |
| 3612 | /* check for total failure */ |
| 3613 | if (ACPI_FAILURE(status)) { |
| 3614 | pr_err("Error evaluating _SRS\n"); |
| 3615 | result = -ENODEV; |
| 3616 | goto end; |
| 3617 | } |
| 3618 | |
| 3619 | /* Necessary device initializations calls (from sonypi) */ |
| 3620 | sony_pic_call1(0x82); |
| 3621 | sony_pic_call2(0x81, 0xff); |
| 3622 | sony_pic_call1(compat ? 0x92 : 0x82); |
| 3623 | |
| 3624 | end: |
| 3625 | kfree(resource); |
| 3626 | return result; |
| 3627 | } |
| 3628 | |
| 3629 | /***************** |
| 3630 | * |
| 3631 | * ISR: some event is available |
| 3632 | * |
| 3633 | *****************/ |
| 3634 | static irqreturn_t sony_pic_irq(int irq, void *dev_id) |
| 3635 | { |
| 3636 | int i, j; |
| 3637 | u8 ev = 0; |
| 3638 | u8 data_mask = 0; |
| 3639 | u8 device_event = 0; |
| 3640 | |
| 3641 | struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id; |
| 3642 | |
| 3643 | ev = inb_p(dev->cur_ioport->io1.minimum); |
| 3644 | if (dev->cur_ioport->io2.minimum) |
| 3645 | data_mask = inb_p(dev->cur_ioport->io2.minimum); |
| 3646 | else |
| 3647 | data_mask = inb_p(dev->cur_ioport->io1.minimum + |
| 3648 | dev->evport_offset); |
| 3649 | |
| 3650 | dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n", |
| 3651 | ev, data_mask, dev->cur_ioport->io1.minimum, |
| 3652 | dev->evport_offset); |
| 3653 | |
| 3654 | if (ev == 0x00 || ev == 0xff) |
| 3655 | return IRQ_HANDLED; |
| 3656 | |
| 3657 | for (i = 0; dev->event_types[i].mask; i++) { |
| 3658 | |
| 3659 | if ((data_mask & dev->event_types[i].data) != |
| 3660 | dev->event_types[i].data) |
| 3661 | continue; |
| 3662 | |
| 3663 | if (!(mask & dev->event_types[i].mask)) |
| 3664 | continue; |
| 3665 | |
| 3666 | for (j = 0; dev->event_types[i].events[j].event; j++) { |
| 3667 | if (ev == dev->event_types[i].events[j].data) { |
| 3668 | device_event = |
| 3669 | dev->event_types[i].events[j].event; |
| 3670 | /* some events may require ignoring */ |
| 3671 | if (!device_event) |
| 3672 | return IRQ_HANDLED; |
| 3673 | goto found; |
| 3674 | } |
| 3675 | } |
| 3676 | } |
| 3677 | /* Still not able to decode the event try to pass |
| 3678 | * it over to the minidriver |
| 3679 | */ |
| 3680 | if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0) |
| 3681 | return IRQ_HANDLED; |
| 3682 | |
| 3683 | dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n", |
| 3684 | ev, data_mask, dev->cur_ioport->io1.minimum, |
| 3685 | dev->evport_offset); |
| 3686 | return IRQ_HANDLED; |
| 3687 | |
| 3688 | found: |
| 3689 | sony_laptop_report_input_event(device_event); |
| 3690 | acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event); |
| 3691 | sonypi_compat_report_event(device_event); |
| 3692 | return IRQ_HANDLED; |
| 3693 | } |
| 3694 | |
| 3695 | /***************** |
| 3696 | * |
| 3697 | * ACPI driver |
| 3698 | * |
| 3699 | *****************/ |
| 3700 | static int sony_pic_remove(struct acpi_device *device, int type) |
| 3701 | { |
| 3702 | struct sony_pic_ioport *io, *tmp_io; |
| 3703 | struct sony_pic_irq *irq, *tmp_irq; |
| 3704 | |
| 3705 | if (sony_pic_disable(device)) { |
| 3706 | pr_err("Couldn't disable device\n"); |
| 3707 | return -ENXIO; |
| 3708 | } |
| 3709 | |
| 3710 | free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev); |
| 3711 | release_region(spic_dev.cur_ioport->io1.minimum, |
| 3712 | spic_dev.cur_ioport->io1.address_length); |
| 3713 | if (spic_dev.cur_ioport->io2.minimum) |
| 3714 | release_region(spic_dev.cur_ioport->io2.minimum, |
| 3715 | spic_dev.cur_ioport->io2.address_length); |
| 3716 | |
| 3717 | sonypi_compat_exit(); |
| 3718 | |
| 3719 | sony_laptop_remove_input(); |
| 3720 | |
| 3721 | /* pf attrs */ |
| 3722 | sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group); |
| 3723 | sony_pf_remove(); |
| 3724 | |
| 3725 | list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) { |
| 3726 | list_del(&io->list); |
| 3727 | kfree(io); |
| 3728 | } |
| 3729 | list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) { |
| 3730 | list_del(&irq->list); |
| 3731 | kfree(irq); |
| 3732 | } |
| 3733 | spic_dev.cur_ioport = NULL; |
| 3734 | spic_dev.cur_irq = NULL; |
| 3735 | |
| 3736 | dprintk(SONY_PIC_DRIVER_NAME " removed.\n"); |
| 3737 | return 0; |
| 3738 | } |
| 3739 | |
| 3740 | static int sony_pic_add(struct acpi_device *device) |
| 3741 | { |
| 3742 | int result; |
| 3743 | struct sony_pic_ioport *io, *tmp_io; |
| 3744 | struct sony_pic_irq *irq, *tmp_irq; |
| 3745 | |
| 3746 | pr_info("%s v%s\n", SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION); |
| 3747 | |
| 3748 | spic_dev.acpi_dev = device; |
| 3749 | strcpy(acpi_device_class(device), "sony/hotkey"); |
| 3750 | sony_pic_detect_device_type(&spic_dev); |
| 3751 | mutex_init(&spic_dev.lock); |
| 3752 | |
| 3753 | /* read _PRS resources */ |
| 3754 | result = sony_pic_possible_resources(device); |
| 3755 | if (result) { |
| 3756 | pr_err("Unable to read possible resources\n"); |
| 3757 | goto err_free_resources; |
| 3758 | } |
| 3759 | |
| 3760 | /* setup input devices and helper fifo */ |
| 3761 | result = sony_laptop_setup_input(device); |
| 3762 | if (result) { |
| 3763 | pr_err("Unable to create input devices\n"); |
| 3764 | goto err_free_resources; |
| 3765 | } |
| 3766 | |
| 3767 | if (sonypi_compat_init()) |
| 3768 | goto err_remove_input; |
| 3769 | |
| 3770 | /* request io port */ |
| 3771 | list_for_each_entry_reverse(io, &spic_dev.ioports, list) { |
| 3772 | if (request_region(io->io1.minimum, io->io1.address_length, |
| 3773 | "Sony Programmable I/O Device")) { |
| 3774 | dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n", |
| 3775 | io->io1.minimum, io->io1.maximum, |
| 3776 | io->io1.address_length); |
| 3777 | /* Type 1 have 2 ioports */ |
| 3778 | if (io->io2.minimum) { |
| 3779 | if (request_region(io->io2.minimum, |
| 3780 | io->io2.address_length, |
| 3781 | "Sony Programmable I/O Device")) { |
| 3782 | dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n", |
| 3783 | io->io2.minimum, io->io2.maximum, |
| 3784 | io->io2.address_length); |
| 3785 | spic_dev.cur_ioport = io; |
| 3786 | break; |
| 3787 | } |
| 3788 | else { |
| 3789 | dprintk("Unable to get I/O port2: " |
| 3790 | "0x%.4x (0x%.4x) + 0x%.2x\n", |
| 3791 | io->io2.minimum, io->io2.maximum, |
| 3792 | io->io2.address_length); |
| 3793 | release_region(io->io1.minimum, |
| 3794 | io->io1.address_length); |
| 3795 | } |
| 3796 | } |
| 3797 | else { |
| 3798 | spic_dev.cur_ioport = io; |
| 3799 | break; |
| 3800 | } |
| 3801 | } |
| 3802 | } |
| 3803 | if (!spic_dev.cur_ioport) { |
| 3804 | pr_err("Failed to request_region\n"); |
| 3805 | result = -ENODEV; |
| 3806 | goto err_remove_compat; |
| 3807 | } |
| 3808 | |
| 3809 | /* request IRQ */ |
| 3810 | list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) { |
| 3811 | if (!request_irq(irq->irq.interrupts[0], sony_pic_irq, |
| 3812 | 0, "sony-laptop", &spic_dev)) { |
| 3813 | dprintk("IRQ: %d - triggering: %d - " |
| 3814 | "polarity: %d - shr: %d\n", |
| 3815 | irq->irq.interrupts[0], |
| 3816 | irq->irq.triggering, |
| 3817 | irq->irq.polarity, |
| 3818 | irq->irq.sharable); |
| 3819 | spic_dev.cur_irq = irq; |
| 3820 | break; |
| 3821 | } |
| 3822 | } |
| 3823 | if (!spic_dev.cur_irq) { |
| 3824 | pr_err("Failed to request_irq\n"); |
| 3825 | result = -ENODEV; |
| 3826 | goto err_release_region; |
| 3827 | } |
| 3828 | |
| 3829 | /* set resource status _SRS */ |
| 3830 | result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq); |
| 3831 | if (result) { |
| 3832 | pr_err("Couldn't enable device\n"); |
| 3833 | goto err_free_irq; |
| 3834 | } |
| 3835 | |
| 3836 | spic_dev.bluetooth_power = -1; |
| 3837 | /* create device attributes */ |
| 3838 | result = sony_pf_add(); |
| 3839 | if (result) |
| 3840 | goto err_disable_device; |
| 3841 | |
| 3842 | result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group); |
| 3843 | if (result) |
| 3844 | goto err_remove_pf; |
| 3845 | |
| 3846 | return 0; |
| 3847 | |
| 3848 | err_remove_pf: |
| 3849 | sony_pf_remove(); |
| 3850 | |
| 3851 | err_disable_device: |
| 3852 | sony_pic_disable(device); |
| 3853 | |
| 3854 | err_free_irq: |
| 3855 | free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev); |
| 3856 | |
| 3857 | err_release_region: |
| 3858 | release_region(spic_dev.cur_ioport->io1.minimum, |
| 3859 | spic_dev.cur_ioport->io1.address_length); |
| 3860 | if (spic_dev.cur_ioport->io2.minimum) |
| 3861 | release_region(spic_dev.cur_ioport->io2.minimum, |
| 3862 | spic_dev.cur_ioport->io2.address_length); |
| 3863 | |
| 3864 | err_remove_compat: |
| 3865 | sonypi_compat_exit(); |
| 3866 | |
| 3867 | err_remove_input: |
| 3868 | sony_laptop_remove_input(); |
| 3869 | |
| 3870 | err_free_resources: |
| 3871 | list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) { |
| 3872 | list_del(&io->list); |
| 3873 | kfree(io); |
| 3874 | } |
| 3875 | list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) { |
| 3876 | list_del(&irq->list); |
| 3877 | kfree(irq); |
| 3878 | } |
| 3879 | spic_dev.cur_ioport = NULL; |
| 3880 | spic_dev.cur_irq = NULL; |
| 3881 | |
| 3882 | return result; |
| 3883 | } |
| 3884 | |
| 3885 | static int sony_pic_suspend(struct acpi_device *device, pm_message_t state) |
| 3886 | { |
| 3887 | if (sony_pic_disable(device)) |
| 3888 | return -ENXIO; |
| 3889 | return 0; |
| 3890 | } |
| 3891 | |
| 3892 | static int sony_pic_resume(struct acpi_device *device) |
| 3893 | { |
| 3894 | sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq); |
| 3895 | return 0; |
| 3896 | } |
| 3897 | |
| 3898 | static const struct acpi_device_id sony_pic_device_ids[] = { |
| 3899 | {SONY_PIC_HID, 0}, |
| 3900 | {"", 0}, |
| 3901 | }; |
| 3902 | |
| 3903 | static struct acpi_driver sony_pic_driver = { |
| 3904 | .name = SONY_PIC_DRIVER_NAME, |
| 3905 | .class = SONY_PIC_CLASS, |
| 3906 | .ids = sony_pic_device_ids, |
| 3907 | .owner = THIS_MODULE, |
| 3908 | .ops = { |
| 3909 | .add = sony_pic_add, |
| 3910 | .remove = sony_pic_remove, |
| 3911 | .suspend = sony_pic_suspend, |
| 3912 | .resume = sony_pic_resume, |
| 3913 | }, |
| 3914 | }; |
| 3915 | |
| 3916 | static struct dmi_system_id __initdata sonypi_dmi_table[] = { |
| 3917 | { |
| 3918 | .ident = "Sony Vaio", |
| 3919 | .matches = { |
| 3920 | DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"), |
| 3921 | DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"), |
| 3922 | }, |
| 3923 | }, |
| 3924 | { |
| 3925 | .ident = "Sony Vaio", |
| 3926 | .matches = { |
| 3927 | DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"), |
| 3928 | DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"), |
| 3929 | }, |
| 3930 | }, |
| 3931 | { } |
| 3932 | }; |
| 3933 | |
| 3934 | static int __init sony_laptop_init(void) |
| 3935 | { |
| 3936 | int result; |
| 3937 | |
| 3938 | if (!no_spic && dmi_check_system(sonypi_dmi_table)) { |
| 3939 | result = acpi_bus_register_driver(&sony_pic_driver); |
| 3940 | if (result) { |
| 3941 | pr_err("Unable to register SPIC driver\n"); |
| 3942 | goto out; |
| 3943 | } |
| 3944 | spic_drv_registered = 1; |
| 3945 | } |
| 3946 | |
| 3947 | result = acpi_bus_register_driver(&sony_nc_driver); |
| 3948 | if (result) { |
| 3949 | pr_err("Unable to register SNC driver\n"); |
| 3950 | goto out_unregister_pic; |
| 3951 | } |
| 3952 | |
| 3953 | return 0; |
| 3954 | |
| 3955 | out_unregister_pic: |
| 3956 | if (spic_drv_registered) |
| 3957 | acpi_bus_unregister_driver(&sony_pic_driver); |
| 3958 | out: |
| 3959 | return result; |
| 3960 | } |
| 3961 | |
| 3962 | static void __exit sony_laptop_exit(void) |
| 3963 | { |
| 3964 | acpi_bus_unregister_driver(&sony_nc_driver); |
| 3965 | if (spic_drv_registered) |
| 3966 | acpi_bus_unregister_driver(&sony_pic_driver); |
| 3967 | } |
| 3968 | |
| 3969 | module_init(sony_laptop_init); |
| 3970 | module_exit(sony_laptop_exit); |