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