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