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