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