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