Linux 3.12.39
[linux/fpc-iii.git] / drivers / platform / x86 / sony-laptop.c
blob3484dd29245804daf2f85f6b511b9e7a19d7a3e6
1 /*
2 * ACPI Sony Notebook Control Driver (SNC and SPIC)
4 * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
5 * Copyright (C) 2007-2009 Mattia Dongili <malattia@linux.it>
7 * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
8 * which are copyrighted by their respective authors.
10 * The SNY6001 driver part is based on the sonypi driver which includes
11 * material from:
13 * Copyright (C) 2001-2005 Stelian Pop <stelian@popies.net>
15 * Copyright (C) 2005 Narayanan R S <nars@kadamba.org>
17 * Copyright (C) 2001-2002 AlcĂ´ve <www.alcove.com>
19 * Copyright (C) 2001 Michael Ashley <m.ashley@unsw.edu.au>
21 * Copyright (C) 2001 Junichi Morita <jun1m@mars.dti.ne.jp>
23 * Copyright (C) 2000 Takaya Kinjo <t-kinjo@tc4.so-net.ne.jp>
25 * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
27 * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
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.
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.
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.
45 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
52 #include <linux/backlight.h>
53 #include <linux/platform_device.h>
54 #include <linux/err.h>
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>
63 #include <linux/slab.h>
64 #include <acpi/acpi_drivers.h>
65 #include <acpi/acpi_bus.h>
66 #include <asm/uaccess.h>
67 #include <linux/sonypi.h>
68 #include <linux/sony-laptop.h>
69 #include <linux/rfkill.h>
70 #ifdef CONFIG_SONYPI_COMPAT
71 #include <linux/poll.h>
72 #include <linux/miscdevice.h>
73 #endif
75 #define dprintk(fmt, ...) \
76 do { \
77 if (debug) \
78 pr_warn(fmt, ##__VA_ARGS__); \
79 } while (0)
81 #define SONY_LAPTOP_DRIVER_VERSION "0.6"
83 #define SONY_NC_CLASS "sony-nc"
84 #define SONY_NC_HID "SNY5001"
85 #define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
87 #define SONY_PIC_CLASS "sony-pic"
88 #define SONY_PIC_HID "SNY6001"
89 #define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
91 MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
92 MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
93 MODULE_LICENSE("GPL");
94 MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
96 static int debug;
97 module_param(debug, int, 0);
98 MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
99 "the development of this driver");
101 static int no_spic; /* = 0 */
102 module_param(no_spic, int, 0444);
103 MODULE_PARM_DESC(no_spic,
104 "set this if you don't want to enable the SPIC device");
106 static int compat; /* = 0 */
107 module_param(compat, int, 0444);
108 MODULE_PARM_DESC(compat,
109 "set this if you want to enable backward compatibility mode");
111 static unsigned long mask = 0xffffffff;
112 module_param(mask, ulong, 0644);
113 MODULE_PARM_DESC(mask,
114 "set this to the mask of event you want to enable (see doc)");
116 static int camera; /* = 0 */
117 module_param(camera, int, 0444);
118 MODULE_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)");
122 #ifdef CONFIG_SONYPI_COMPAT
123 static int minor = -1;
124 module_param(minor, int, 0);
125 MODULE_PARM_DESC(minor,
126 "minor number of the misc device for the SPIC compatibility code, "
127 "default is -1 (automatic)");
128 #endif
130 static int kbd_backlight = -1;
131 module_param(kbd_backlight, int, 0444);
132 MODULE_PARM_DESC(kbd_backlight,
133 "set this to 0 to disable keyboard backlight, "
134 "1 to enable it (default: no change from current value)");
136 static int kbd_backlight_timeout = -1;
137 module_param(kbd_backlight_timeout, int, 0444);
138 MODULE_PARM_DESC(kbd_backlight_timeout,
139 "meaningful values vary from 0 to 3 and their meaning depends "
140 "on the model (default: no change from current value)");
142 #ifdef CONFIG_PM_SLEEP
143 static void sony_nc_thermal_resume(void);
144 #endif
145 static int sony_nc_kbd_backlight_setup(struct platform_device *pd,
146 unsigned int handle);
147 static void sony_nc_kbd_backlight_cleanup(struct platform_device *pd);
149 static int sony_nc_battery_care_setup(struct platform_device *pd,
150 unsigned int handle);
151 static void sony_nc_battery_care_cleanup(struct platform_device *pd);
153 static int sony_nc_thermal_setup(struct platform_device *pd);
154 static void sony_nc_thermal_cleanup(struct platform_device *pd);
156 static int sony_nc_lid_resume_setup(struct platform_device *pd);
157 static void sony_nc_lid_resume_cleanup(struct platform_device *pd);
159 static int sony_nc_gfx_switch_setup(struct platform_device *pd,
160 unsigned int handle);
161 static void sony_nc_gfx_switch_cleanup(struct platform_device *pd);
162 static int __sony_nc_gfx_switch_status_get(void);
164 static int sony_nc_highspeed_charging_setup(struct platform_device *pd);
165 static void sony_nc_highspeed_charging_cleanup(struct platform_device *pd);
167 static int sony_nc_touchpad_setup(struct platform_device *pd,
168 unsigned int handle);
169 static void sony_nc_touchpad_cleanup(struct platform_device *pd);
171 enum sony_nc_rfkill {
172 SONY_WIFI,
173 SONY_BLUETOOTH,
174 SONY_WWAN,
175 SONY_WIMAX,
176 N_SONY_RFKILL,
179 static int sony_rfkill_handle;
180 static struct rfkill *sony_rfkill_devices[N_SONY_RFKILL];
181 static int sony_rfkill_address[N_SONY_RFKILL] = {0x300, 0x500, 0x700, 0x900};
182 static int sony_nc_rfkill_setup(struct acpi_device *device,
183 unsigned int handle);
184 static void sony_nc_rfkill_cleanup(void);
185 static void sony_nc_rfkill_update(void);
187 /*********** Input Devices ***********/
189 #define SONY_LAPTOP_BUF_SIZE 128
190 struct sony_laptop_input_s {
191 atomic_t users;
192 struct input_dev *jog_dev;
193 struct input_dev *key_dev;
194 struct kfifo fifo;
195 spinlock_t fifo_lock;
196 struct timer_list release_key_timer;
199 static struct sony_laptop_input_s sony_laptop_input = {
200 .users = ATOMIC_INIT(0),
203 struct sony_laptop_keypress {
204 struct input_dev *dev;
205 int key;
208 /* Correspondance table between sonypi events
209 * and input layer indexes in the keymap
211 static int sony_laptop_input_index[] = {
212 -1, /* 0 no event */
213 -1, /* 1 SONYPI_EVENT_JOGDIAL_DOWN */
214 -1, /* 2 SONYPI_EVENT_JOGDIAL_UP */
215 -1, /* 3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
216 -1, /* 4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */
217 -1, /* 5 SONYPI_EVENT_JOGDIAL_PRESSED */
218 -1, /* 6 SONYPI_EVENT_JOGDIAL_RELEASED */
219 0, /* 7 SONYPI_EVENT_CAPTURE_PRESSED */
220 1, /* 8 SONYPI_EVENT_CAPTURE_RELEASED */
221 2, /* 9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
222 3, /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
223 4, /* 11 SONYPI_EVENT_FNKEY_ESC */
224 5, /* 12 SONYPI_EVENT_FNKEY_F1 */
225 6, /* 13 SONYPI_EVENT_FNKEY_F2 */
226 7, /* 14 SONYPI_EVENT_FNKEY_F3 */
227 8, /* 15 SONYPI_EVENT_FNKEY_F4 */
228 9, /* 16 SONYPI_EVENT_FNKEY_F5 */
229 10, /* 17 SONYPI_EVENT_FNKEY_F6 */
230 11, /* 18 SONYPI_EVENT_FNKEY_F7 */
231 12, /* 19 SONYPI_EVENT_FNKEY_F8 */
232 13, /* 20 SONYPI_EVENT_FNKEY_F9 */
233 14, /* 21 SONYPI_EVENT_FNKEY_F10 */
234 15, /* 22 SONYPI_EVENT_FNKEY_F11 */
235 16, /* 23 SONYPI_EVENT_FNKEY_F12 */
236 17, /* 24 SONYPI_EVENT_FNKEY_1 */
237 18, /* 25 SONYPI_EVENT_FNKEY_2 */
238 19, /* 26 SONYPI_EVENT_FNKEY_D */
239 20, /* 27 SONYPI_EVENT_FNKEY_E */
240 21, /* 28 SONYPI_EVENT_FNKEY_F */
241 22, /* 29 SONYPI_EVENT_FNKEY_S */
242 23, /* 30 SONYPI_EVENT_FNKEY_B */
243 24, /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */
244 25, /* 32 SONYPI_EVENT_PKEY_P1 */
245 26, /* 33 SONYPI_EVENT_PKEY_P2 */
246 27, /* 34 SONYPI_EVENT_PKEY_P3 */
247 28, /* 35 SONYPI_EVENT_BACK_PRESSED */
248 -1, /* 36 SONYPI_EVENT_LID_CLOSED */
249 -1, /* 37 SONYPI_EVENT_LID_OPENED */
250 29, /* 38 SONYPI_EVENT_BLUETOOTH_ON */
251 30, /* 39 SONYPI_EVENT_BLUETOOTH_OFF */
252 31, /* 40 SONYPI_EVENT_HELP_PRESSED */
253 32, /* 41 SONYPI_EVENT_FNKEY_ONLY */
254 33, /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
255 34, /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */
256 35, /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
257 36, /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
258 37, /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
259 38, /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */
260 39, /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
261 40, /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
262 41, /* 50 SONYPI_EVENT_ZOOM_PRESSED */
263 42, /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */
264 43, /* 52 SONYPI_EVENT_MEYE_FACE */
265 44, /* 53 SONYPI_EVENT_MEYE_OPPOSITE */
266 45, /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */
267 46, /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */
268 -1, /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */
269 -1, /* 57 SONYPI_EVENT_BATTERY_INSERT */
270 -1, /* 58 SONYPI_EVENT_BATTERY_REMOVE */
271 -1, /* 59 SONYPI_EVENT_FNKEY_RELEASED */
272 47, /* 60 SONYPI_EVENT_WIRELESS_ON */
273 48, /* 61 SONYPI_EVENT_WIRELESS_OFF */
274 49, /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */
275 50, /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */
276 51, /* 64 SONYPI_EVENT_CD_EJECT_PRESSED */
277 52, /* 65 SONYPI_EVENT_MODEKEY_PRESSED */
278 53, /* 66 SONYPI_EVENT_PKEY_P4 */
279 54, /* 67 SONYPI_EVENT_PKEY_P5 */
280 55, /* 68 SONYPI_EVENT_SETTINGKEY_PRESSED */
281 56, /* 69 SONYPI_EVENT_VOLUME_INC_PRESSED */
282 57, /* 70 SONYPI_EVENT_VOLUME_DEC_PRESSED */
283 -1, /* 71 SONYPI_EVENT_BRIGHTNESS_PRESSED */
284 58, /* 72 SONYPI_EVENT_MEDIA_PRESSED */
285 59, /* 72 SONYPI_EVENT_VENDOR_PRESSED */
288 static int sony_laptop_input_keycode_map[] = {
289 KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
290 KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
291 KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
292 KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
293 KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
294 KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
295 KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
296 KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
297 KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
298 KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
299 KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
300 KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
301 KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
302 KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
303 KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
304 KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
305 KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
306 KEY_FN_F1, /* 17 SONYPI_EVENT_FNKEY_1 */
307 KEY_FN_F2, /* 18 SONYPI_EVENT_FNKEY_2 */
308 KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
309 KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
310 KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
311 KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
312 KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
313 KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
314 KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
315 KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
316 KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
317 KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
318 KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
319 KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
320 KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
321 KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
322 KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
323 KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
324 KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
325 KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
326 KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
327 KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
328 KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
329 KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
330 KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
331 BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
332 KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
333 KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
334 KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
335 KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
336 KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
337 KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
338 KEY_ZOOMIN, /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
339 KEY_ZOOMOUT, /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
340 KEY_EJECTCD, /* 51 SONYPI_EVENT_CD_EJECT_PRESSED */
341 KEY_F13, /* 52 SONYPI_EVENT_MODEKEY_PRESSED */
342 KEY_PROG4, /* 53 SONYPI_EVENT_PKEY_P4 */
343 KEY_F14, /* 54 SONYPI_EVENT_PKEY_P5 */
344 KEY_F15, /* 55 SONYPI_EVENT_SETTINGKEY_PRESSED */
345 KEY_VOLUMEUP, /* 56 SONYPI_EVENT_VOLUME_INC_PRESSED */
346 KEY_VOLUMEDOWN, /* 57 SONYPI_EVENT_VOLUME_DEC_PRESSED */
347 KEY_MEDIA, /* 58 SONYPI_EVENT_MEDIA_PRESSED */
348 KEY_VENDOR, /* 59 SONYPI_EVENT_VENDOR_PRESSED */
351 /* release buttons after a short delay if pressed */
352 static void do_sony_laptop_release_key(unsigned long unused)
354 struct sony_laptop_keypress kp;
355 unsigned long flags;
357 spin_lock_irqsave(&sony_laptop_input.fifo_lock, flags);
359 if (kfifo_out(&sony_laptop_input.fifo,
360 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
361 input_report_key(kp.dev, kp.key, 0);
362 input_sync(kp.dev);
365 /* If there is something in the fifo schedule next release. */
366 if (kfifo_len(&sony_laptop_input.fifo) != 0)
367 mod_timer(&sony_laptop_input.release_key_timer,
368 jiffies + msecs_to_jiffies(10));
370 spin_unlock_irqrestore(&sony_laptop_input.fifo_lock, flags);
373 /* forward event to the input subsystem */
374 static void sony_laptop_report_input_event(u8 event)
376 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
377 struct input_dev *key_dev = sony_laptop_input.key_dev;
378 struct sony_laptop_keypress kp = { NULL };
379 int scancode = -1;
381 if (event == SONYPI_EVENT_FNKEY_RELEASED ||
382 event == SONYPI_EVENT_ANYBUTTON_RELEASED) {
383 /* Nothing, not all VAIOs generate this event */
384 return;
387 /* report events */
388 switch (event) {
389 /* jog_dev events */
390 case SONYPI_EVENT_JOGDIAL_UP:
391 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
392 input_report_rel(jog_dev, REL_WHEEL, 1);
393 input_sync(jog_dev);
394 return;
396 case SONYPI_EVENT_JOGDIAL_DOWN:
397 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
398 input_report_rel(jog_dev, REL_WHEEL, -1);
399 input_sync(jog_dev);
400 return;
402 /* key_dev events */
403 case SONYPI_EVENT_JOGDIAL_PRESSED:
404 kp.key = BTN_MIDDLE;
405 kp.dev = jog_dev;
406 break;
408 default:
409 if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
410 dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
411 break;
413 if ((scancode = sony_laptop_input_index[event]) != -1) {
414 kp.key = sony_laptop_input_keycode_map[scancode];
415 if (kp.key != KEY_UNKNOWN)
416 kp.dev = key_dev;
418 break;
421 if (kp.dev) {
422 /* if we have a scancode we emit it so we can always
423 remap the key */
424 if (scancode != -1)
425 input_event(kp.dev, EV_MSC, MSC_SCAN, scancode);
426 input_report_key(kp.dev, kp.key, 1);
427 input_sync(kp.dev);
429 /* schedule key release */
430 kfifo_in_locked(&sony_laptop_input.fifo,
431 (unsigned char *)&kp, sizeof(kp),
432 &sony_laptop_input.fifo_lock);
433 mod_timer(&sony_laptop_input.release_key_timer,
434 jiffies + msecs_to_jiffies(10));
435 } else
436 dprintk("unknown input event %.2x\n", event);
439 static int sony_laptop_setup_input(struct acpi_device *acpi_device)
441 struct input_dev *jog_dev;
442 struct input_dev *key_dev;
443 int i;
444 int error;
446 /* don't run again if already initialized */
447 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
448 return 0;
450 /* kfifo */
451 spin_lock_init(&sony_laptop_input.fifo_lock);
452 error = kfifo_alloc(&sony_laptop_input.fifo,
453 SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
454 if (error) {
455 pr_err("kfifo_alloc failed\n");
456 goto err_dec_users;
459 setup_timer(&sony_laptop_input.release_key_timer,
460 do_sony_laptop_release_key, 0);
462 /* input keys */
463 key_dev = input_allocate_device();
464 if (!key_dev) {
465 error = -ENOMEM;
466 goto err_free_kfifo;
469 key_dev->name = "Sony Vaio Keys";
470 key_dev->id.bustype = BUS_ISA;
471 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
472 key_dev->dev.parent = &acpi_device->dev;
474 /* Initialize the Input Drivers: special keys */
475 input_set_capability(key_dev, EV_MSC, MSC_SCAN);
477 __set_bit(EV_KEY, key_dev->evbit);
478 key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
479 key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
480 key_dev->keycode = &sony_laptop_input_keycode_map;
481 for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++)
482 __set_bit(sony_laptop_input_keycode_map[i], key_dev->keybit);
483 __clear_bit(KEY_RESERVED, key_dev->keybit);
485 error = input_register_device(key_dev);
486 if (error)
487 goto err_free_keydev;
489 sony_laptop_input.key_dev = key_dev;
491 /* jogdial */
492 jog_dev = input_allocate_device();
493 if (!jog_dev) {
494 error = -ENOMEM;
495 goto err_unregister_keydev;
498 jog_dev->name = "Sony Vaio Jogdial";
499 jog_dev->id.bustype = BUS_ISA;
500 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
501 jog_dev->dev.parent = &acpi_device->dev;
503 input_set_capability(jog_dev, EV_KEY, BTN_MIDDLE);
504 input_set_capability(jog_dev, EV_REL, REL_WHEEL);
506 error = input_register_device(jog_dev);
507 if (error)
508 goto err_free_jogdev;
510 sony_laptop_input.jog_dev = jog_dev;
512 return 0;
514 err_free_jogdev:
515 input_free_device(jog_dev);
517 err_unregister_keydev:
518 input_unregister_device(key_dev);
519 /* to avoid kref underflow below at input_free_device */
520 key_dev = NULL;
522 err_free_keydev:
523 input_free_device(key_dev);
525 err_free_kfifo:
526 kfifo_free(&sony_laptop_input.fifo);
528 err_dec_users:
529 atomic_dec(&sony_laptop_input.users);
530 return error;
533 static void sony_laptop_remove_input(void)
535 struct sony_laptop_keypress kp = { NULL };
537 /* Cleanup only after the last user has gone */
538 if (!atomic_dec_and_test(&sony_laptop_input.users))
539 return;
541 del_timer_sync(&sony_laptop_input.release_key_timer);
544 * Generate key-up events for remaining keys. Note that we don't
545 * need locking since nobody is adding new events to the kfifo.
547 while (kfifo_out(&sony_laptop_input.fifo,
548 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
549 input_report_key(kp.dev, kp.key, 0);
550 input_sync(kp.dev);
553 /* destroy input devs */
554 input_unregister_device(sony_laptop_input.key_dev);
555 sony_laptop_input.key_dev = NULL;
557 if (sony_laptop_input.jog_dev) {
558 input_unregister_device(sony_laptop_input.jog_dev);
559 sony_laptop_input.jog_dev = NULL;
562 kfifo_free(&sony_laptop_input.fifo);
565 /*********** Platform Device ***********/
567 static atomic_t sony_pf_users = ATOMIC_INIT(0);
568 static struct platform_driver sony_pf_driver = {
569 .driver = {
570 .name = "sony-laptop",
571 .owner = THIS_MODULE,
574 static struct platform_device *sony_pf_device;
576 static int sony_pf_add(void)
578 int ret = 0;
580 /* don't run again if already initialized */
581 if (atomic_add_return(1, &sony_pf_users) > 1)
582 return 0;
584 ret = platform_driver_register(&sony_pf_driver);
585 if (ret)
586 goto out;
588 sony_pf_device = platform_device_alloc("sony-laptop", -1);
589 if (!sony_pf_device) {
590 ret = -ENOMEM;
591 goto out_platform_registered;
594 ret = platform_device_add(sony_pf_device);
595 if (ret)
596 goto out_platform_alloced;
598 return 0;
600 out_platform_alloced:
601 platform_device_put(sony_pf_device);
602 sony_pf_device = NULL;
603 out_platform_registered:
604 platform_driver_unregister(&sony_pf_driver);
605 out:
606 atomic_dec(&sony_pf_users);
607 return ret;
610 static void sony_pf_remove(void)
612 /* deregister only after the last user has gone */
613 if (!atomic_dec_and_test(&sony_pf_users))
614 return;
616 platform_device_unregister(sony_pf_device);
617 platform_driver_unregister(&sony_pf_driver);
620 /*********** SNC (SNY5001) Device ***********/
622 /* the device uses 1-based values, while the backlight subsystem uses
623 0-based values */
624 #define SONY_MAX_BRIGHTNESS 8
626 #define SNC_VALIDATE_IN 0
627 #define SNC_VALIDATE_OUT 1
629 static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
630 char *);
631 static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
632 const char *, size_t);
633 static int boolean_validate(const int, const int);
634 static int brightness_default_validate(const int, const int);
636 struct sony_nc_value {
637 char *name; /* name of the entry */
638 char **acpiget; /* names of the ACPI get function */
639 char **acpiset; /* names of the ACPI set function */
640 int (*validate)(const int, const int); /* input/output validation */
641 int value; /* current setting */
642 int valid; /* Has ever been set */
643 int debug; /* active only in debug mode ? */
644 struct device_attribute devattr; /* sysfs attribute */
647 #define SNC_HANDLE_NAMES(_name, _values...) \
648 static char *snc_##_name[] = { _values, NULL }
650 #define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
652 .name = __stringify(_name), \
653 .acpiget = _getters, \
654 .acpiset = _setters, \
655 .validate = _validate, \
656 .debug = _debug, \
657 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
660 #define SNC_HANDLE_NULL { .name = NULL }
662 SNC_HANDLE_NAMES(fnkey_get, "GHKE");
664 SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
665 SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
667 SNC_HANDLE_NAMES(cdpower_get, "GCDP");
668 SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
670 SNC_HANDLE_NAMES(audiopower_get, "GAZP");
671 SNC_HANDLE_NAMES(audiopower_set, "AZPW");
673 SNC_HANDLE_NAMES(lanpower_get, "GLNP");
674 SNC_HANDLE_NAMES(lanpower_set, "LNPW");
676 SNC_HANDLE_NAMES(lidstate_get, "GLID");
678 SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
679 SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
681 SNC_HANDLE_NAMES(gainbass_get, "GMGB");
682 SNC_HANDLE_NAMES(gainbass_set, "CMGB");
684 SNC_HANDLE_NAMES(PID_get, "GPID");
686 SNC_HANDLE_NAMES(CTR_get, "GCTR");
687 SNC_HANDLE_NAMES(CTR_set, "SCTR");
689 SNC_HANDLE_NAMES(PCR_get, "GPCR");
690 SNC_HANDLE_NAMES(PCR_set, "SPCR");
692 SNC_HANDLE_NAMES(CMI_get, "GCMI");
693 SNC_HANDLE_NAMES(CMI_set, "SCMI");
695 static struct sony_nc_value sony_nc_values[] = {
696 SNC_HANDLE(brightness_default, snc_brightness_def_get,
697 snc_brightness_def_set, brightness_default_validate, 0),
698 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
699 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
700 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
701 boolean_validate, 0),
702 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
703 boolean_validate, 1),
704 SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
705 boolean_validate, 0),
706 SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
707 boolean_validate, 0),
708 SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
709 boolean_validate, 0),
710 /* unknown methods */
711 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
712 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
713 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
714 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
715 SNC_HANDLE_NULL
718 static acpi_handle sony_nc_acpi_handle;
719 static struct acpi_device *sony_nc_acpi_device = NULL;
722 * acpi_evaluate_object wrappers
723 * all useful calls into SNC methods take one or zero parameters and return
724 * integers or arrays.
726 static union acpi_object *__call_snc_method(acpi_handle handle, char *method,
727 u64 *value)
729 union acpi_object *result = NULL;
730 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
731 acpi_status status;
733 if (value) {
734 struct acpi_object_list params;
735 union acpi_object in;
736 in.type = ACPI_TYPE_INTEGER;
737 in.integer.value = *value;
738 params.count = 1;
739 params.pointer = &in;
740 status = acpi_evaluate_object(handle, method, &params, &output);
741 dprintk("__call_snc_method: [%s:0x%.8x%.8x]\n", method,
742 (unsigned int)(*value >> 32),
743 (unsigned int)*value & 0xffffffff);
744 } else {
745 status = acpi_evaluate_object(handle, method, NULL, &output);
746 dprintk("__call_snc_method: [%s]\n", method);
749 if (ACPI_FAILURE(status)) {
750 pr_err("Failed to evaluate [%s]\n", method);
751 return NULL;
754 result = (union acpi_object *) output.pointer;
755 if (!result)
756 dprintk("No return object [%s]\n", method);
758 return result;
761 static int sony_nc_int_call(acpi_handle handle, char *name, int *value,
762 int *result)
764 union acpi_object *object = NULL;
765 if (value) {
766 u64 v = *value;
767 object = __call_snc_method(handle, name, &v);
768 } else
769 object = __call_snc_method(handle, name, NULL);
771 if (!object)
772 return -EINVAL;
774 if (object->type != ACPI_TYPE_INTEGER) {
775 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
776 ACPI_TYPE_INTEGER, object->type);
777 kfree(object);
778 return -EINVAL;
781 if (result)
782 *result = object->integer.value;
784 kfree(object);
785 return 0;
788 #define MIN(a, b) (a > b ? b : a)
789 static int sony_nc_buffer_call(acpi_handle handle, char *name, u64 *value,
790 void *buffer, size_t buflen)
792 int ret = 0;
793 size_t len = len;
794 union acpi_object *object = __call_snc_method(handle, name, value);
796 if (!object)
797 return -EINVAL;
799 if (object->type == ACPI_TYPE_BUFFER) {
800 len = MIN(buflen, object->buffer.length);
801 memcpy(buffer, object->buffer.pointer, len);
803 } else if (object->type == ACPI_TYPE_INTEGER) {
804 len = MIN(buflen, sizeof(object->integer.value));
805 memcpy(buffer, &object->integer.value, len);
807 } else {
808 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
809 ACPI_TYPE_BUFFER, object->type);
810 ret = -EINVAL;
813 kfree(object);
814 return ret;
817 struct sony_nc_handles {
818 u16 cap[0x10];
819 struct device_attribute devattr;
822 static struct sony_nc_handles *handles;
824 static ssize_t sony_nc_handles_show(struct device *dev,
825 struct device_attribute *attr, char *buffer)
827 ssize_t len = 0;
828 int i;
830 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
831 len += snprintf(buffer + len, PAGE_SIZE - len, "0x%.4x ",
832 handles->cap[i]);
834 len += snprintf(buffer + len, PAGE_SIZE - len, "\n");
836 return len;
839 static int sony_nc_handles_setup(struct platform_device *pd)
841 int i, r, result, arg;
843 handles = kzalloc(sizeof(*handles), GFP_KERNEL);
844 if (!handles)
845 return -ENOMEM;
847 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
848 arg = i + 0x20;
849 r = sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg,
850 &result);
851 if (!r) {
852 dprintk("caching handle 0x%.4x (offset: 0x%.2x)\n",
853 result, i);
854 handles->cap[i] = result;
858 if (debug) {
859 sysfs_attr_init(&handles->devattr.attr);
860 handles->devattr.attr.name = "handles";
861 handles->devattr.attr.mode = S_IRUGO;
862 handles->devattr.show = sony_nc_handles_show;
864 /* allow reading capabilities via sysfs */
865 if (device_create_file(&pd->dev, &handles->devattr)) {
866 kfree(handles);
867 handles = NULL;
868 return -1;
872 return 0;
875 static int sony_nc_handles_cleanup(struct platform_device *pd)
877 if (handles) {
878 if (debug)
879 device_remove_file(&pd->dev, &handles->devattr);
880 kfree(handles);
881 handles = NULL;
883 return 0;
886 static int sony_find_snc_handle(int handle)
888 int i;
890 /* not initialized yet, return early */
891 if (!handles || !handle)
892 return -EINVAL;
894 for (i = 0; i < 0x10; i++) {
895 if (handles->cap[i] == handle) {
896 dprintk("found handle 0x%.4x (offset: 0x%.2x)\n",
897 handle, i);
898 return i;
901 dprintk("handle 0x%.4x not found\n", handle);
902 return -EINVAL;
905 static int sony_call_snc_handle(int handle, int argument, int *result)
907 int arg, ret = 0;
908 int offset = sony_find_snc_handle(handle);
910 if (offset < 0)
911 return offset;
913 arg = offset | argument;
914 ret = sony_nc_int_call(sony_nc_acpi_handle, "SN07", &arg, result);
915 dprintk("called SN07 with 0x%.4x (result: 0x%.4x)\n", arg, *result);
916 return ret;
920 * sony_nc_values input/output validate functions
923 /* brightness_default_validate:
925 * manipulate input output values to keep consistency with the
926 * backlight framework for which brightness values are 0-based.
928 static int brightness_default_validate(const int direction, const int value)
930 switch (direction) {
931 case SNC_VALIDATE_OUT:
932 return value - 1;
933 case SNC_VALIDATE_IN:
934 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
935 return value + 1;
937 return -EINVAL;
940 /* boolean_validate:
942 * on input validate boolean values 0/1, on output just pass the
943 * received value.
945 static int boolean_validate(const int direction, const int value)
947 if (direction == SNC_VALIDATE_IN) {
948 if (value != 0 && value != 1)
949 return -EINVAL;
951 return value;
955 * Sysfs show/store common to all sony_nc_values
957 static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
958 char *buffer)
960 int value, ret = 0;
961 struct sony_nc_value *item =
962 container_of(attr, struct sony_nc_value, devattr);
964 if (!*item->acpiget)
965 return -EIO;
967 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiget, NULL,
968 &value);
969 if (ret < 0)
970 return -EIO;
972 if (item->validate)
973 value = item->validate(SNC_VALIDATE_OUT, value);
975 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
978 static ssize_t sony_nc_sysfs_store(struct device *dev,
979 struct device_attribute *attr,
980 const char *buffer, size_t count)
982 int value;
983 int ret = 0;
984 struct sony_nc_value *item =
985 container_of(attr, struct sony_nc_value, devattr);
987 if (!item->acpiset)
988 return -EIO;
990 if (count > 31)
991 return -EINVAL;
993 if (kstrtoint(buffer, 10, &value))
994 return -EINVAL;
996 if (item->validate)
997 value = item->validate(SNC_VALIDATE_IN, value);
999 if (value < 0)
1000 return value;
1002 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
1003 &value, NULL);
1004 if (ret < 0)
1005 return -EIO;
1007 item->value = value;
1008 item->valid = 1;
1009 return count;
1014 * Backlight device
1016 struct sony_backlight_props {
1017 struct backlight_device *dev;
1018 int handle;
1019 int cmd_base;
1020 u8 offset;
1021 u8 maxlvl;
1023 struct sony_backlight_props sony_bl_props;
1025 static int sony_backlight_update_status(struct backlight_device *bd)
1027 int arg = bd->props.brightness + 1;
1028 return sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &arg, NULL);
1031 static int sony_backlight_get_brightness(struct backlight_device *bd)
1033 int value;
1035 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL, &value))
1036 return 0;
1037 /* brightness levels are 1-based, while backlight ones are 0-based */
1038 return value - 1;
1041 static int sony_nc_get_brightness_ng(struct backlight_device *bd)
1043 int result;
1044 struct sony_backlight_props *sdev =
1045 (struct sony_backlight_props *)bl_get_data(bd);
1047 sony_call_snc_handle(sdev->handle, sdev->cmd_base + 0x100, &result);
1049 return (result & 0xff) - sdev->offset;
1052 static int sony_nc_update_status_ng(struct backlight_device *bd)
1054 int value, result;
1055 struct sony_backlight_props *sdev =
1056 (struct sony_backlight_props *)bl_get_data(bd);
1058 value = bd->props.brightness + sdev->offset;
1059 if (sony_call_snc_handle(sdev->handle, sdev->cmd_base | (value << 0x10),
1060 &result))
1061 return -EIO;
1063 return value;
1066 static const struct backlight_ops sony_backlight_ops = {
1067 .options = BL_CORE_SUSPENDRESUME,
1068 .update_status = sony_backlight_update_status,
1069 .get_brightness = sony_backlight_get_brightness,
1071 static const struct backlight_ops sony_backlight_ng_ops = {
1072 .options = BL_CORE_SUSPENDRESUME,
1073 .update_status = sony_nc_update_status_ng,
1074 .get_brightness = sony_nc_get_brightness_ng,
1078 * New SNC-only Vaios event mapping to driver known keys
1080 struct sony_nc_event {
1081 u8 data;
1082 u8 event;
1085 static struct sony_nc_event sony_100_events[] = {
1086 { 0x90, SONYPI_EVENT_PKEY_P1 },
1087 { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
1088 { 0x91, SONYPI_EVENT_PKEY_P2 },
1089 { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
1090 { 0x81, SONYPI_EVENT_FNKEY_F1 },
1091 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
1092 { 0x82, SONYPI_EVENT_FNKEY_F2 },
1093 { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
1094 { 0x83, SONYPI_EVENT_FNKEY_F3 },
1095 { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
1096 { 0x84, SONYPI_EVENT_FNKEY_F4 },
1097 { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
1098 { 0x85, SONYPI_EVENT_FNKEY_F5 },
1099 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
1100 { 0x86, SONYPI_EVENT_FNKEY_F6 },
1101 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
1102 { 0x87, SONYPI_EVENT_FNKEY_F7 },
1103 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
1104 { 0x88, SONYPI_EVENT_FNKEY_F8 },
1105 { 0x08, SONYPI_EVENT_FNKEY_RELEASED },
1106 { 0x89, SONYPI_EVENT_FNKEY_F9 },
1107 { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
1108 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
1109 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
1110 { 0x8B, SONYPI_EVENT_FNKEY_F11 },
1111 { 0x0B, SONYPI_EVENT_FNKEY_RELEASED },
1112 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
1113 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
1114 { 0x9d, SONYPI_EVENT_ZOOM_PRESSED },
1115 { 0x1d, SONYPI_EVENT_ANYBUTTON_RELEASED },
1116 { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
1117 { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
1118 { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
1119 { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
1120 { 0xa4, SONYPI_EVENT_CD_EJECT_PRESSED },
1121 { 0x24, SONYPI_EVENT_ANYBUTTON_RELEASED },
1122 { 0xa5, SONYPI_EVENT_VENDOR_PRESSED },
1123 { 0x25, SONYPI_EVENT_ANYBUTTON_RELEASED },
1124 { 0xa6, SONYPI_EVENT_HELP_PRESSED },
1125 { 0x26, SONYPI_EVENT_ANYBUTTON_RELEASED },
1126 { 0, 0 },
1129 static struct sony_nc_event sony_127_events[] = {
1130 { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
1131 { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
1132 { 0x82, SONYPI_EVENT_PKEY_P1 },
1133 { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
1134 { 0x83, SONYPI_EVENT_PKEY_P2 },
1135 { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
1136 { 0x84, SONYPI_EVENT_PKEY_P3 },
1137 { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
1138 { 0x85, SONYPI_EVENT_PKEY_P4 },
1139 { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
1140 { 0x86, SONYPI_EVENT_PKEY_P5 },
1141 { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
1142 { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
1143 { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
1144 { 0, 0 },
1147 static int sony_nc_hotkeys_decode(u32 event, unsigned int handle)
1149 int ret = -EINVAL;
1150 unsigned int result = 0;
1151 struct sony_nc_event *key_event;
1153 if (sony_call_snc_handle(handle, 0x200, &result)) {
1154 dprintk("Unable to decode event 0x%.2x 0x%.2x\n", handle,
1155 event);
1156 return -EINVAL;
1159 result &= 0xFF;
1161 if (handle == 0x0100)
1162 key_event = sony_100_events;
1163 else
1164 key_event = sony_127_events;
1166 for (; key_event->data; key_event++) {
1167 if (key_event->data == result) {
1168 ret = key_event->event;
1169 break;
1173 if (!key_event->data)
1174 pr_info("Unknown hotkey 0x%.2x/0x%.2x (handle 0x%.2x)\n",
1175 event, result, handle);
1177 return ret;
1181 * ACPI callbacks
1183 enum event_types {
1184 HOTKEY = 1,
1185 KILLSWITCH,
1186 GFX_SWITCH
1188 static void sony_nc_notify(struct acpi_device *device, u32 event)
1190 u32 real_ev = event;
1191 u8 ev_type = 0;
1192 dprintk("sony_nc_notify, event: 0x%.2x\n", event);
1194 if (event >= 0x90) {
1195 unsigned int result = 0;
1196 unsigned int arg = 0;
1197 unsigned int handle = 0;
1198 unsigned int offset = event - 0x90;
1200 if (offset >= ARRAY_SIZE(handles->cap)) {
1201 pr_err("Event 0x%x outside of capabilities list\n",
1202 event);
1203 return;
1205 handle = handles->cap[offset];
1207 /* list of handles known for generating events */
1208 switch (handle) {
1209 /* hotkey event */
1210 case 0x0100:
1211 case 0x0127:
1212 ev_type = HOTKEY;
1213 real_ev = sony_nc_hotkeys_decode(event, handle);
1215 if (real_ev > 0)
1216 sony_laptop_report_input_event(real_ev);
1217 else
1218 /* restore the original event for reporting */
1219 real_ev = event;
1221 break;
1223 /* wlan switch */
1224 case 0x0124:
1225 case 0x0135:
1226 /* events on this handle are reported when the
1227 * switch changes position or for battery
1228 * events. We'll notify both of them but only
1229 * update the rfkill device status when the
1230 * switch is moved.
1232 ev_type = KILLSWITCH;
1233 sony_call_snc_handle(handle, 0x0100, &result);
1234 real_ev = result & 0x03;
1236 /* hw switch event */
1237 if (real_ev == 1)
1238 sony_nc_rfkill_update();
1240 break;
1242 case 0x0128:
1243 case 0x0146:
1244 /* Hybrid GFX switching */
1245 sony_call_snc_handle(handle, 0x0000, &result);
1246 dprintk("GFX switch event received (reason: %s)\n",
1247 (result == 0x1) ? "switch change" :
1248 (result == 0x2) ? "output switch" :
1249 (result == 0x3) ? "output switch" :
1250 "");
1252 ev_type = GFX_SWITCH;
1253 real_ev = __sony_nc_gfx_switch_status_get();
1254 break;
1256 case 0x015B:
1257 /* Hybrid GFX switching SVS151290S */
1258 ev_type = GFX_SWITCH;
1259 real_ev = __sony_nc_gfx_switch_status_get();
1260 break;
1261 default:
1262 dprintk("Unknown event 0x%x for handle 0x%x\n",
1263 event, handle);
1264 break;
1267 /* clear the event (and the event reason when present) */
1268 arg = 1 << offset;
1269 sony_nc_int_call(sony_nc_acpi_handle, "SN05", &arg, &result);
1271 } else {
1272 /* old style event */
1273 ev_type = HOTKEY;
1274 sony_laptop_report_input_event(real_ev);
1276 acpi_bus_generate_netlink_event(sony_nc_acpi_device->pnp.device_class,
1277 dev_name(&sony_nc_acpi_device->dev), ev_type, real_ev);
1280 static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
1281 void *context, void **return_value)
1283 struct acpi_device_info *info;
1285 if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
1286 pr_warn("method: name: %4.4s, args %X\n",
1287 (char *)&info->name, info->param_count);
1289 kfree(info);
1292 return AE_OK;
1296 * ACPI device
1298 static void sony_nc_function_setup(struct acpi_device *device,
1299 struct platform_device *pf_device)
1301 unsigned int i, result, bitmask, arg;
1303 if (!handles)
1304 return;
1306 /* setup found handles here */
1307 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1308 unsigned int handle = handles->cap[i];
1310 if (!handle)
1311 continue;
1313 dprintk("setting up handle 0x%.4x\n", handle);
1315 switch (handle) {
1316 case 0x0100:
1317 case 0x0101:
1318 case 0x0127:
1319 /* setup hotkeys */
1320 sony_call_snc_handle(handle, 0, &result);
1321 break;
1322 case 0x0102:
1323 /* setup hotkeys */
1324 sony_call_snc_handle(handle, 0x100, &result);
1325 break;
1326 case 0x0105:
1327 case 0x0148:
1328 /* touchpad enable/disable */
1329 result = sony_nc_touchpad_setup(pf_device, handle);
1330 if (result)
1331 pr_err("couldn't set up touchpad control function (%d)\n",
1332 result);
1333 break;
1334 case 0x0115:
1335 case 0x0136:
1336 case 0x013f:
1337 result = sony_nc_battery_care_setup(pf_device, handle);
1338 if (result)
1339 pr_err("couldn't set up battery care function (%d)\n",
1340 result);
1341 break;
1342 case 0x0119:
1343 result = sony_nc_lid_resume_setup(pf_device);
1344 if (result)
1345 pr_err("couldn't set up lid resume function (%d)\n",
1346 result);
1347 break;
1348 case 0x0122:
1349 result = sony_nc_thermal_setup(pf_device);
1350 if (result)
1351 pr_err("couldn't set up thermal profile function (%d)\n",
1352 result);
1353 break;
1354 case 0x0128:
1355 case 0x0146:
1356 case 0x015B:
1357 result = sony_nc_gfx_switch_setup(pf_device, handle);
1358 if (result)
1359 pr_err("couldn't set up GFX Switch status (%d)\n",
1360 result);
1361 break;
1362 case 0x0131:
1363 result = sony_nc_highspeed_charging_setup(pf_device);
1364 if (result)
1365 pr_err("couldn't set up high speed charging function (%d)\n",
1366 result);
1367 break;
1368 case 0x0124:
1369 case 0x0135:
1370 result = sony_nc_rfkill_setup(device, handle);
1371 if (result)
1372 pr_err("couldn't set up rfkill support (%d)\n",
1373 result);
1374 break;
1375 case 0x0137:
1376 case 0x0143:
1377 case 0x014b:
1378 case 0x014c:
1379 case 0x0163:
1380 result = sony_nc_kbd_backlight_setup(pf_device, handle);
1381 if (result)
1382 pr_err("couldn't set up keyboard backlight function (%d)\n",
1383 result);
1384 break;
1385 default:
1386 continue;
1390 /* Enable all events */
1391 arg = 0x10;
1392 if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1393 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1394 &result);
1397 static void sony_nc_function_cleanup(struct platform_device *pd)
1399 unsigned int i, result, bitmask, handle;
1401 /* get enabled events and disable them */
1402 sony_nc_int_call(sony_nc_acpi_handle, "SN01", NULL, &bitmask);
1403 sony_nc_int_call(sony_nc_acpi_handle, "SN03", &bitmask, &result);
1405 /* cleanup handles here */
1406 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1408 handle = handles->cap[i];
1410 if (!handle)
1411 continue;
1413 switch (handle) {
1414 case 0x0105:
1415 case 0x0148:
1416 sony_nc_touchpad_cleanup(pd);
1417 break;
1418 case 0x0115:
1419 case 0x0136:
1420 case 0x013f:
1421 sony_nc_battery_care_cleanup(pd);
1422 break;
1423 case 0x0119:
1424 sony_nc_lid_resume_cleanup(pd);
1425 break;
1426 case 0x0122:
1427 sony_nc_thermal_cleanup(pd);
1428 break;
1429 case 0x0128:
1430 case 0x0146:
1431 case 0x015B:
1432 sony_nc_gfx_switch_cleanup(pd);
1433 break;
1434 case 0x0131:
1435 sony_nc_highspeed_charging_cleanup(pd);
1436 break;
1437 case 0x0124:
1438 case 0x0135:
1439 sony_nc_rfkill_cleanup();
1440 break;
1441 case 0x0137:
1442 case 0x0143:
1443 case 0x014b:
1444 case 0x014c:
1445 case 0x0163:
1446 sony_nc_kbd_backlight_cleanup(pd);
1447 break;
1448 default:
1449 continue;
1453 /* finally cleanup the handles list */
1454 sony_nc_handles_cleanup(pd);
1457 #ifdef CONFIG_PM_SLEEP
1458 static void sony_nc_function_resume(void)
1460 unsigned int i, result, bitmask, arg;
1462 dprintk("Resuming SNC device\n");
1464 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1465 unsigned int handle = handles->cap[i];
1467 if (!handle)
1468 continue;
1470 switch (handle) {
1471 case 0x0100:
1472 case 0x0101:
1473 case 0x0127:
1474 /* re-enable hotkeys */
1475 sony_call_snc_handle(handle, 0, &result);
1476 break;
1477 case 0x0102:
1478 /* re-enable hotkeys */
1479 sony_call_snc_handle(handle, 0x100, &result);
1480 break;
1481 case 0x0122:
1482 sony_nc_thermal_resume();
1483 break;
1484 case 0x0124:
1485 case 0x0135:
1486 sony_nc_rfkill_update();
1487 break;
1488 default:
1489 continue;
1493 /* Enable all events */
1494 arg = 0x10;
1495 if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1496 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1497 &result);
1500 static int sony_nc_resume(struct device *dev)
1502 struct sony_nc_value *item;
1503 acpi_handle handle;
1505 for (item = sony_nc_values; item->name; item++) {
1506 int ret;
1508 if (!item->valid)
1509 continue;
1510 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
1511 &item->value, NULL);
1512 if (ret < 0) {
1513 pr_err("%s: %d\n", __func__, ret);
1514 break;
1518 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1519 &handle))) {
1520 int arg = 1;
1521 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
1522 dprintk("ECON Method failed\n");
1525 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1526 &handle)))
1527 sony_nc_function_resume();
1529 return 0;
1531 #endif
1533 static SIMPLE_DEV_PM_OPS(sony_nc_pm, NULL, sony_nc_resume);
1535 static void sony_nc_rfkill_cleanup(void)
1537 int i;
1539 for (i = 0; i < N_SONY_RFKILL; i++) {
1540 if (sony_rfkill_devices[i]) {
1541 rfkill_unregister(sony_rfkill_devices[i]);
1542 rfkill_destroy(sony_rfkill_devices[i]);
1547 static int sony_nc_rfkill_set(void *data, bool blocked)
1549 int result;
1550 int argument = sony_rfkill_address[(long) data] + 0x100;
1552 if (!blocked)
1553 argument |= 0x070000;
1555 return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
1558 static const struct rfkill_ops sony_rfkill_ops = {
1559 .set_block = sony_nc_rfkill_set,
1562 static int sony_nc_setup_rfkill(struct acpi_device *device,
1563 enum sony_nc_rfkill nc_type)
1565 int err = 0;
1566 struct rfkill *rfk;
1567 enum rfkill_type type;
1568 const char *name;
1569 int result;
1570 bool hwblock, swblock;
1572 switch (nc_type) {
1573 case SONY_WIFI:
1574 type = RFKILL_TYPE_WLAN;
1575 name = "sony-wifi";
1576 break;
1577 case SONY_BLUETOOTH:
1578 type = RFKILL_TYPE_BLUETOOTH;
1579 name = "sony-bluetooth";
1580 break;
1581 case SONY_WWAN:
1582 type = RFKILL_TYPE_WWAN;
1583 name = "sony-wwan";
1584 break;
1585 case SONY_WIMAX:
1586 type = RFKILL_TYPE_WIMAX;
1587 name = "sony-wimax";
1588 break;
1589 default:
1590 return -EINVAL;
1593 rfk = rfkill_alloc(name, &device->dev, type,
1594 &sony_rfkill_ops, (void *)nc_type);
1595 if (!rfk)
1596 return -ENOMEM;
1598 if (sony_call_snc_handle(sony_rfkill_handle, 0x200, &result) < 0) {
1599 rfkill_destroy(rfk);
1600 return -1;
1602 hwblock = !(result & 0x1);
1604 if (sony_call_snc_handle(sony_rfkill_handle,
1605 sony_rfkill_address[nc_type],
1606 &result) < 0) {
1607 rfkill_destroy(rfk);
1608 return -1;
1610 swblock = !(result & 0x2);
1612 rfkill_init_sw_state(rfk, swblock);
1613 rfkill_set_hw_state(rfk, hwblock);
1615 err = rfkill_register(rfk);
1616 if (err) {
1617 rfkill_destroy(rfk);
1618 return err;
1620 sony_rfkill_devices[nc_type] = rfk;
1621 return err;
1624 static void sony_nc_rfkill_update(void)
1626 enum sony_nc_rfkill i;
1627 int result;
1628 bool hwblock;
1630 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
1631 hwblock = !(result & 0x1);
1633 for (i = 0; i < N_SONY_RFKILL; i++) {
1634 int argument = sony_rfkill_address[i];
1636 if (!sony_rfkill_devices[i])
1637 continue;
1639 if (hwblock) {
1640 if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
1641 /* we already know we're blocked */
1643 continue;
1646 sony_call_snc_handle(sony_rfkill_handle, argument, &result);
1647 rfkill_set_states(sony_rfkill_devices[i],
1648 !(result & 0x2), false);
1652 static int sony_nc_rfkill_setup(struct acpi_device *device,
1653 unsigned int handle)
1655 u64 offset;
1656 int i;
1657 unsigned char buffer[32] = { 0 };
1659 offset = sony_find_snc_handle(handle);
1660 sony_rfkill_handle = handle;
1662 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
1663 32);
1664 if (i < 0)
1665 return i;
1667 /* The buffer is filled with magic numbers describing the devices
1668 * available, 0xff terminates the enumeration.
1669 * Known codes:
1670 * 0x00 WLAN
1671 * 0x10 BLUETOOTH
1672 * 0x20 WWAN GPRS-EDGE
1673 * 0x21 WWAN HSDPA
1674 * 0x22 WWAN EV-DO
1675 * 0x23 WWAN GPS
1676 * 0x25 Gobi WWAN no GPS
1677 * 0x26 Gobi WWAN + GPS
1678 * 0x28 Gobi WWAN no GPS
1679 * 0x29 Gobi WWAN + GPS
1680 * 0x30 WIMAX
1681 * 0x50 Gobi WWAN no GPS
1682 * 0x51 Gobi WWAN + GPS
1683 * 0x70 no SIM card slot
1684 * 0x71 SIM card slot
1686 for (i = 0; i < ARRAY_SIZE(buffer); i++) {
1688 if (buffer[i] == 0xff)
1689 break;
1691 dprintk("Radio devices, found 0x%.2x\n", buffer[i]);
1693 if (buffer[i] == 0 && !sony_rfkill_devices[SONY_WIFI])
1694 sony_nc_setup_rfkill(device, SONY_WIFI);
1696 if (buffer[i] == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
1697 sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
1699 if (((0xf0 & buffer[i]) == 0x20 ||
1700 (0xf0 & buffer[i]) == 0x50) &&
1701 !sony_rfkill_devices[SONY_WWAN])
1702 sony_nc_setup_rfkill(device, SONY_WWAN);
1704 if (buffer[i] == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
1705 sony_nc_setup_rfkill(device, SONY_WIMAX);
1707 return 0;
1710 /* Keyboard backlight feature */
1711 struct kbd_backlight {
1712 unsigned int handle;
1713 unsigned int base;
1714 unsigned int mode;
1715 unsigned int timeout;
1716 struct device_attribute mode_attr;
1717 struct device_attribute timeout_attr;
1720 static struct kbd_backlight *kbdbl_ctl;
1722 static ssize_t __sony_nc_kbd_backlight_mode_set(u8 value)
1724 int result;
1726 if (value > 1)
1727 return -EINVAL;
1729 if (sony_call_snc_handle(kbdbl_ctl->handle,
1730 (value << 0x10) | (kbdbl_ctl->base), &result))
1731 return -EIO;
1733 /* Try to turn the light on/off immediately */
1734 sony_call_snc_handle(kbdbl_ctl->handle,
1735 (value << 0x10) | (kbdbl_ctl->base + 0x100), &result);
1737 kbdbl_ctl->mode = value;
1739 return 0;
1742 static ssize_t sony_nc_kbd_backlight_mode_store(struct device *dev,
1743 struct device_attribute *attr,
1744 const char *buffer, size_t count)
1746 int ret = 0;
1747 unsigned long value;
1749 if (count > 31)
1750 return -EINVAL;
1752 if (kstrtoul(buffer, 10, &value))
1753 return -EINVAL;
1755 ret = __sony_nc_kbd_backlight_mode_set(value);
1756 if (ret < 0)
1757 return ret;
1759 return count;
1762 static ssize_t sony_nc_kbd_backlight_mode_show(struct device *dev,
1763 struct device_attribute *attr, char *buffer)
1765 ssize_t count = 0;
1766 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_ctl->mode);
1767 return count;
1770 static int __sony_nc_kbd_backlight_timeout_set(u8 value)
1772 int result;
1774 if (value > 3)
1775 return -EINVAL;
1777 if (sony_call_snc_handle(kbdbl_ctl->handle, (value << 0x10) |
1778 (kbdbl_ctl->base + 0x200), &result))
1779 return -EIO;
1781 kbdbl_ctl->timeout = value;
1783 return 0;
1786 static ssize_t sony_nc_kbd_backlight_timeout_store(struct device *dev,
1787 struct device_attribute *attr,
1788 const char *buffer, size_t count)
1790 int ret = 0;
1791 unsigned long value;
1793 if (count > 31)
1794 return -EINVAL;
1796 if (kstrtoul(buffer, 10, &value))
1797 return -EINVAL;
1799 ret = __sony_nc_kbd_backlight_timeout_set(value);
1800 if (ret < 0)
1801 return ret;
1803 return count;
1806 static ssize_t sony_nc_kbd_backlight_timeout_show(struct device *dev,
1807 struct device_attribute *attr, char *buffer)
1809 ssize_t count = 0;
1810 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_ctl->timeout);
1811 return count;
1814 static int sony_nc_kbd_backlight_setup(struct platform_device *pd,
1815 unsigned int handle)
1817 int result;
1818 int ret = 0;
1820 /* verify the kbd backlight presence, these handles are not used for
1821 * keyboard backlight only
1823 ret = sony_call_snc_handle(handle, handle == 0x0137 ? 0x0B00 : 0x0100,
1824 &result);
1825 if (ret)
1826 return ret;
1828 if ((handle == 0x0137 && !(result & 0x02)) ||
1829 !(result & 0x01)) {
1830 dprintk("no backlight keyboard found\n");
1831 return 0;
1834 kbdbl_ctl = kzalloc(sizeof(*kbdbl_ctl), GFP_KERNEL);
1835 if (!kbdbl_ctl)
1836 return -ENOMEM;
1838 kbdbl_ctl->mode = kbd_backlight;
1839 kbdbl_ctl->timeout = kbd_backlight_timeout;
1840 kbdbl_ctl->handle = handle;
1841 if (handle == 0x0137)
1842 kbdbl_ctl->base = 0x0C00;
1843 else
1844 kbdbl_ctl->base = 0x4000;
1846 sysfs_attr_init(&kbdbl_ctl->mode_attr.attr);
1847 kbdbl_ctl->mode_attr.attr.name = "kbd_backlight";
1848 kbdbl_ctl->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
1849 kbdbl_ctl->mode_attr.show = sony_nc_kbd_backlight_mode_show;
1850 kbdbl_ctl->mode_attr.store = sony_nc_kbd_backlight_mode_store;
1852 sysfs_attr_init(&kbdbl_ctl->timeout_attr.attr);
1853 kbdbl_ctl->timeout_attr.attr.name = "kbd_backlight_timeout";
1854 kbdbl_ctl->timeout_attr.attr.mode = S_IRUGO | S_IWUSR;
1855 kbdbl_ctl->timeout_attr.show = sony_nc_kbd_backlight_timeout_show;
1856 kbdbl_ctl->timeout_attr.store = sony_nc_kbd_backlight_timeout_store;
1858 ret = device_create_file(&pd->dev, &kbdbl_ctl->mode_attr);
1859 if (ret)
1860 goto outkzalloc;
1862 ret = device_create_file(&pd->dev, &kbdbl_ctl->timeout_attr);
1863 if (ret)
1864 goto outmode;
1866 __sony_nc_kbd_backlight_mode_set(kbdbl_ctl->mode);
1867 __sony_nc_kbd_backlight_timeout_set(kbdbl_ctl->timeout);
1869 return 0;
1871 outmode:
1872 device_remove_file(&pd->dev, &kbdbl_ctl->mode_attr);
1873 outkzalloc:
1874 kfree(kbdbl_ctl);
1875 kbdbl_ctl = NULL;
1876 return ret;
1879 static void sony_nc_kbd_backlight_cleanup(struct platform_device *pd)
1881 if (kbdbl_ctl) {
1882 device_remove_file(&pd->dev, &kbdbl_ctl->mode_attr);
1883 device_remove_file(&pd->dev, &kbdbl_ctl->timeout_attr);
1884 kfree(kbdbl_ctl);
1885 kbdbl_ctl = NULL;
1889 struct battery_care_control {
1890 struct device_attribute attrs[2];
1891 unsigned int handle;
1893 static struct battery_care_control *bcare_ctl;
1895 static ssize_t sony_nc_battery_care_limit_store(struct device *dev,
1896 struct device_attribute *attr,
1897 const char *buffer, size_t count)
1899 unsigned int result, cmd;
1900 unsigned long value;
1902 if (count > 31)
1903 return -EINVAL;
1905 if (kstrtoul(buffer, 10, &value))
1906 return -EINVAL;
1908 /* limit values (2 bits):
1909 * 00 - none
1910 * 01 - 80%
1911 * 10 - 50%
1912 * 11 - 100%
1914 * bit 0: 0 disable BCL, 1 enable BCL
1915 * bit 1: 1 tell to store the battery limit (see bits 6,7) too
1916 * bits 2,3: reserved
1917 * bits 4,5: store the limit into the EC
1918 * bits 6,7: store the limit into the battery
1920 cmd = 0;
1922 if (value > 0) {
1923 if (value <= 50)
1924 cmd = 0x20;
1926 else if (value <= 80)
1927 cmd = 0x10;
1929 else if (value <= 100)
1930 cmd = 0x30;
1932 else
1933 return -EINVAL;
1936 * handle 0x0115 should allow storing on battery too;
1937 * handle 0x0136 same as 0x0115 + health status;
1938 * handle 0x013f, same as 0x0136 but no storing on the battery
1940 if (bcare_ctl->handle != 0x013f)
1941 cmd = cmd | (cmd << 2);
1943 cmd = (cmd | 0x1) << 0x10;
1946 if (sony_call_snc_handle(bcare_ctl->handle, cmd | 0x0100, &result))
1947 return -EIO;
1949 return count;
1952 static ssize_t sony_nc_battery_care_limit_show(struct device *dev,
1953 struct device_attribute *attr, char *buffer)
1955 unsigned int result, status;
1957 if (sony_call_snc_handle(bcare_ctl->handle, 0x0000, &result))
1958 return -EIO;
1960 status = (result & 0x01) ? ((result & 0x30) >> 0x04) : 0;
1961 switch (status) {
1962 case 1:
1963 status = 80;
1964 break;
1965 case 2:
1966 status = 50;
1967 break;
1968 case 3:
1969 status = 100;
1970 break;
1971 default:
1972 status = 0;
1973 break;
1976 return snprintf(buffer, PAGE_SIZE, "%d\n", status);
1979 static ssize_t sony_nc_battery_care_health_show(struct device *dev,
1980 struct device_attribute *attr, char *buffer)
1982 ssize_t count = 0;
1983 unsigned int health;
1985 if (sony_call_snc_handle(bcare_ctl->handle, 0x0200, &health))
1986 return -EIO;
1988 count = snprintf(buffer, PAGE_SIZE, "%d\n", health & 0xff);
1990 return count;
1993 static int sony_nc_battery_care_setup(struct platform_device *pd,
1994 unsigned int handle)
1996 int ret = 0;
1998 bcare_ctl = kzalloc(sizeof(struct battery_care_control), GFP_KERNEL);
1999 if (!bcare_ctl)
2000 return -ENOMEM;
2002 bcare_ctl->handle = handle;
2004 sysfs_attr_init(&bcare_ctl->attrs[0].attr);
2005 bcare_ctl->attrs[0].attr.name = "battery_care_limiter";
2006 bcare_ctl->attrs[0].attr.mode = S_IRUGO | S_IWUSR;
2007 bcare_ctl->attrs[0].show = sony_nc_battery_care_limit_show;
2008 bcare_ctl->attrs[0].store = sony_nc_battery_care_limit_store;
2010 ret = device_create_file(&pd->dev, &bcare_ctl->attrs[0]);
2011 if (ret)
2012 goto outkzalloc;
2014 /* 0x0115 is for models with no health reporting capability */
2015 if (handle == 0x0115)
2016 return 0;
2018 sysfs_attr_init(&bcare_ctl->attrs[1].attr);
2019 bcare_ctl->attrs[1].attr.name = "battery_care_health";
2020 bcare_ctl->attrs[1].attr.mode = S_IRUGO;
2021 bcare_ctl->attrs[1].show = sony_nc_battery_care_health_show;
2023 ret = device_create_file(&pd->dev, &bcare_ctl->attrs[1]);
2024 if (ret)
2025 goto outlimiter;
2027 return 0;
2029 outlimiter:
2030 device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
2032 outkzalloc:
2033 kfree(bcare_ctl);
2034 bcare_ctl = NULL;
2036 return ret;
2039 static void sony_nc_battery_care_cleanup(struct platform_device *pd)
2041 if (bcare_ctl) {
2042 device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
2043 if (bcare_ctl->handle != 0x0115)
2044 device_remove_file(&pd->dev, &bcare_ctl->attrs[1]);
2046 kfree(bcare_ctl);
2047 bcare_ctl = NULL;
2051 struct snc_thermal_ctrl {
2052 unsigned int mode;
2053 unsigned int profiles;
2054 struct device_attribute mode_attr;
2055 struct device_attribute profiles_attr;
2057 static struct snc_thermal_ctrl *th_handle;
2059 #define THM_PROFILE_MAX 3
2060 static const char * const snc_thermal_profiles[] = {
2061 "balanced",
2062 "silent",
2063 "performance"
2066 static int sony_nc_thermal_mode_set(unsigned short mode)
2068 unsigned int result;
2070 /* the thermal profile seems to be a two bit bitmask:
2071 * lsb -> silent
2072 * msb -> performance
2073 * no bit set is the normal operation and is always valid
2074 * Some vaio models only have "balanced" and "performance"
2076 if ((mode && !(th_handle->profiles & mode)) || mode >= THM_PROFILE_MAX)
2077 return -EINVAL;
2079 if (sony_call_snc_handle(0x0122, mode << 0x10 | 0x0200, &result))
2080 return -EIO;
2082 th_handle->mode = mode;
2084 return 0;
2087 static int sony_nc_thermal_mode_get(void)
2089 unsigned int result;
2091 if (sony_call_snc_handle(0x0122, 0x0100, &result))
2092 return -EIO;
2094 return result & 0xff;
2097 static ssize_t sony_nc_thermal_profiles_show(struct device *dev,
2098 struct device_attribute *attr, char *buffer)
2100 short cnt;
2101 size_t idx = 0;
2103 for (cnt = 0; cnt < THM_PROFILE_MAX; cnt++) {
2104 if (!cnt || (th_handle->profiles & cnt))
2105 idx += snprintf(buffer + idx, PAGE_SIZE - idx, "%s ",
2106 snc_thermal_profiles[cnt]);
2108 idx += snprintf(buffer + idx, PAGE_SIZE - idx, "\n");
2110 return idx;
2113 static ssize_t sony_nc_thermal_mode_store(struct device *dev,
2114 struct device_attribute *attr,
2115 const char *buffer, size_t count)
2117 unsigned short cmd;
2118 size_t len = count;
2120 if (count == 0)
2121 return -EINVAL;
2123 /* skip the newline if present */
2124 if (buffer[len - 1] == '\n')
2125 len--;
2127 for (cmd = 0; cmd < THM_PROFILE_MAX; cmd++)
2128 if (strncmp(buffer, snc_thermal_profiles[cmd], len) == 0)
2129 break;
2131 if (sony_nc_thermal_mode_set(cmd))
2132 return -EIO;
2134 return count;
2137 static ssize_t sony_nc_thermal_mode_show(struct device *dev,
2138 struct device_attribute *attr, char *buffer)
2140 ssize_t count = 0;
2141 int mode = sony_nc_thermal_mode_get();
2143 if (mode < 0)
2144 return mode;
2146 count = snprintf(buffer, PAGE_SIZE, "%s\n", snc_thermal_profiles[mode]);
2148 return count;
2151 static int sony_nc_thermal_setup(struct platform_device *pd)
2153 int ret = 0;
2154 th_handle = kzalloc(sizeof(struct snc_thermal_ctrl), GFP_KERNEL);
2155 if (!th_handle)
2156 return -ENOMEM;
2158 ret = sony_call_snc_handle(0x0122, 0x0000, &th_handle->profiles);
2159 if (ret) {
2160 pr_warn("couldn't to read the thermal profiles\n");
2161 goto outkzalloc;
2164 ret = sony_nc_thermal_mode_get();
2165 if (ret < 0) {
2166 pr_warn("couldn't to read the current thermal profile");
2167 goto outkzalloc;
2169 th_handle->mode = ret;
2171 sysfs_attr_init(&th_handle->profiles_attr.attr);
2172 th_handle->profiles_attr.attr.name = "thermal_profiles";
2173 th_handle->profiles_attr.attr.mode = S_IRUGO;
2174 th_handle->profiles_attr.show = sony_nc_thermal_profiles_show;
2176 sysfs_attr_init(&th_handle->mode_attr.attr);
2177 th_handle->mode_attr.attr.name = "thermal_control";
2178 th_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
2179 th_handle->mode_attr.show = sony_nc_thermal_mode_show;
2180 th_handle->mode_attr.store = sony_nc_thermal_mode_store;
2182 ret = device_create_file(&pd->dev, &th_handle->profiles_attr);
2183 if (ret)
2184 goto outkzalloc;
2186 ret = device_create_file(&pd->dev, &th_handle->mode_attr);
2187 if (ret)
2188 goto outprofiles;
2190 return 0;
2192 outprofiles:
2193 device_remove_file(&pd->dev, &th_handle->profiles_attr);
2194 outkzalloc:
2195 kfree(th_handle);
2196 th_handle = NULL;
2197 return ret;
2200 static void sony_nc_thermal_cleanup(struct platform_device *pd)
2202 if (th_handle) {
2203 device_remove_file(&pd->dev, &th_handle->profiles_attr);
2204 device_remove_file(&pd->dev, &th_handle->mode_attr);
2205 kfree(th_handle);
2206 th_handle = NULL;
2210 #ifdef CONFIG_PM_SLEEP
2211 static void sony_nc_thermal_resume(void)
2213 unsigned int status = sony_nc_thermal_mode_get();
2215 if (status != th_handle->mode)
2216 sony_nc_thermal_mode_set(th_handle->mode);
2218 #endif
2220 /* resume on LID open */
2221 struct snc_lid_resume_control {
2222 struct device_attribute attrs[3];
2223 unsigned int status;
2225 static struct snc_lid_resume_control *lid_ctl;
2227 static ssize_t sony_nc_lid_resume_store(struct device *dev,
2228 struct device_attribute *attr,
2229 const char *buffer, size_t count)
2231 unsigned int result, pos;
2232 unsigned long value;
2233 if (count > 31)
2234 return -EINVAL;
2236 if (kstrtoul(buffer, 10, &value) || value > 1)
2237 return -EINVAL;
2239 /* the value we have to write to SNC is a bitmask:
2240 * +--------------+
2241 * | S3 | S4 | S5 |
2242 * +--------------+
2243 * 2 1 0
2245 if (strcmp(attr->attr.name, "lid_resume_S3") == 0)
2246 pos = 2;
2247 else if (strcmp(attr->attr.name, "lid_resume_S4") == 0)
2248 pos = 1;
2249 else if (strcmp(attr->attr.name, "lid_resume_S5") == 0)
2250 pos = 0;
2251 else
2252 return -EINVAL;
2254 if (value)
2255 value = lid_ctl->status | (1 << pos);
2256 else
2257 value = lid_ctl->status & ~(1 << pos);
2259 if (sony_call_snc_handle(0x0119, value << 0x10 | 0x0100, &result))
2260 return -EIO;
2262 lid_ctl->status = value;
2264 return count;
2267 static ssize_t sony_nc_lid_resume_show(struct device *dev,
2268 struct device_attribute *attr, char *buffer)
2270 unsigned int pos;
2272 if (strcmp(attr->attr.name, "lid_resume_S3") == 0)
2273 pos = 2;
2274 else if (strcmp(attr->attr.name, "lid_resume_S4") == 0)
2275 pos = 1;
2276 else if (strcmp(attr->attr.name, "lid_resume_S5") == 0)
2277 pos = 0;
2278 else
2279 return -EINVAL;
2281 return snprintf(buffer, PAGE_SIZE, "%d\n",
2282 (lid_ctl->status >> pos) & 0x01);
2285 static int sony_nc_lid_resume_setup(struct platform_device *pd)
2287 unsigned int result;
2288 int i;
2290 if (sony_call_snc_handle(0x0119, 0x0000, &result))
2291 return -EIO;
2293 lid_ctl = kzalloc(sizeof(struct snc_lid_resume_control), GFP_KERNEL);
2294 if (!lid_ctl)
2295 return -ENOMEM;
2297 lid_ctl->status = result & 0x7;
2299 sysfs_attr_init(&lid_ctl->attrs[0].attr);
2300 lid_ctl->attrs[0].attr.name = "lid_resume_S3";
2301 lid_ctl->attrs[0].attr.mode = S_IRUGO | S_IWUSR;
2302 lid_ctl->attrs[0].show = sony_nc_lid_resume_show;
2303 lid_ctl->attrs[0].store = sony_nc_lid_resume_store;
2305 sysfs_attr_init(&lid_ctl->attrs[1].attr);
2306 lid_ctl->attrs[1].attr.name = "lid_resume_S4";
2307 lid_ctl->attrs[1].attr.mode = S_IRUGO | S_IWUSR;
2308 lid_ctl->attrs[1].show = sony_nc_lid_resume_show;
2309 lid_ctl->attrs[1].store = sony_nc_lid_resume_store;
2311 sysfs_attr_init(&lid_ctl->attrs[2].attr);
2312 lid_ctl->attrs[2].attr.name = "lid_resume_S5";
2313 lid_ctl->attrs[2].attr.mode = S_IRUGO | S_IWUSR;
2314 lid_ctl->attrs[2].show = sony_nc_lid_resume_show;
2315 lid_ctl->attrs[2].store = sony_nc_lid_resume_store;
2317 for (i = 0; i < 3; i++) {
2318 result = device_create_file(&pd->dev, &lid_ctl->attrs[i]);
2319 if (result)
2320 goto liderror;
2323 return 0;
2325 liderror:
2326 for (i--; i >= 0; i--)
2327 device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
2329 kfree(lid_ctl);
2330 lid_ctl = NULL;
2332 return result;
2335 static void sony_nc_lid_resume_cleanup(struct platform_device *pd)
2337 int i;
2339 if (lid_ctl) {
2340 for (i = 0; i < 3; i++)
2341 device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
2343 kfree(lid_ctl);
2344 lid_ctl = NULL;
2348 /* GFX Switch position */
2349 enum gfx_switch {
2350 SPEED,
2351 STAMINA,
2352 AUTO
2354 struct snc_gfx_switch_control {
2355 struct device_attribute attr;
2356 unsigned int handle;
2358 static struct snc_gfx_switch_control *gfxs_ctl;
2360 /* returns 0 for speed, 1 for stamina */
2361 static int __sony_nc_gfx_switch_status_get(void)
2363 unsigned int result;
2365 if (sony_call_snc_handle(gfxs_ctl->handle,
2366 gfxs_ctl->handle == 0x015B ? 0x0000 : 0x0100,
2367 &result))
2368 return -EIO;
2370 switch (gfxs_ctl->handle) {
2371 case 0x0146:
2372 /* 1: discrete GFX (speed)
2373 * 0: integrated GFX (stamina)
2375 return result & 0x1 ? SPEED : STAMINA;
2376 break;
2377 case 0x015B:
2378 /* 0: discrete GFX (speed)
2379 * 1: integrated GFX (stamina)
2381 return result & 0x1 ? STAMINA : SPEED;
2382 break;
2383 case 0x0128:
2384 /* it's a more elaborated bitmask, for now:
2385 * 2: integrated GFX (stamina)
2386 * 0: discrete GFX (speed)
2388 dprintk("GFX Status: 0x%x\n", result);
2389 return result & 0x80 ? AUTO :
2390 result & 0x02 ? STAMINA : SPEED;
2391 break;
2393 return -EINVAL;
2396 static ssize_t sony_nc_gfx_switch_status_show(struct device *dev,
2397 struct device_attribute *attr,
2398 char *buffer)
2400 int pos = __sony_nc_gfx_switch_status_get();
2402 if (pos < 0)
2403 return pos;
2405 return snprintf(buffer, PAGE_SIZE, "%s\n",
2406 pos == SPEED ? "speed" :
2407 pos == STAMINA ? "stamina" :
2408 pos == AUTO ? "auto" : "unknown");
2411 static int sony_nc_gfx_switch_setup(struct platform_device *pd,
2412 unsigned int handle)
2414 unsigned int result;
2416 gfxs_ctl = kzalloc(sizeof(struct snc_gfx_switch_control), GFP_KERNEL);
2417 if (!gfxs_ctl)
2418 return -ENOMEM;
2420 gfxs_ctl->handle = handle;
2422 sysfs_attr_init(&gfxs_ctl->attr.attr);
2423 gfxs_ctl->attr.attr.name = "gfx_switch_status";
2424 gfxs_ctl->attr.attr.mode = S_IRUGO;
2425 gfxs_ctl->attr.show = sony_nc_gfx_switch_status_show;
2427 result = device_create_file(&pd->dev, &gfxs_ctl->attr);
2428 if (result)
2429 goto gfxerror;
2431 return 0;
2433 gfxerror:
2434 kfree(gfxs_ctl);
2435 gfxs_ctl = NULL;
2437 return result;
2440 static void sony_nc_gfx_switch_cleanup(struct platform_device *pd)
2442 if (gfxs_ctl) {
2443 device_remove_file(&pd->dev, &gfxs_ctl->attr);
2445 kfree(gfxs_ctl);
2446 gfxs_ctl = NULL;
2450 /* High speed charging function */
2451 static struct device_attribute *hsc_handle;
2453 static ssize_t sony_nc_highspeed_charging_store(struct device *dev,
2454 struct device_attribute *attr,
2455 const char *buffer, size_t count)
2457 unsigned int result;
2458 unsigned long value;
2460 if (count > 31)
2461 return -EINVAL;
2463 if (kstrtoul(buffer, 10, &value) || value > 1)
2464 return -EINVAL;
2466 if (sony_call_snc_handle(0x0131, value << 0x10 | 0x0200, &result))
2467 return -EIO;
2469 return count;
2472 static ssize_t sony_nc_highspeed_charging_show(struct device *dev,
2473 struct device_attribute *attr, char *buffer)
2475 unsigned int result;
2477 if (sony_call_snc_handle(0x0131, 0x0100, &result))
2478 return -EIO;
2480 return snprintf(buffer, PAGE_SIZE, "%d\n", result & 0x01);
2483 static int sony_nc_highspeed_charging_setup(struct platform_device *pd)
2485 unsigned int result;
2487 if (sony_call_snc_handle(0x0131, 0x0000, &result) || !(result & 0x01)) {
2488 /* some models advertise the handle but have no implementation
2489 * for it
2491 pr_info("No High Speed Charging capability found\n");
2492 return 0;
2495 hsc_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
2496 if (!hsc_handle)
2497 return -ENOMEM;
2499 sysfs_attr_init(&hsc_handle->attr);
2500 hsc_handle->attr.name = "battery_highspeed_charging";
2501 hsc_handle->attr.mode = S_IRUGO | S_IWUSR;
2502 hsc_handle->show = sony_nc_highspeed_charging_show;
2503 hsc_handle->store = sony_nc_highspeed_charging_store;
2505 result = device_create_file(&pd->dev, hsc_handle);
2506 if (result) {
2507 kfree(hsc_handle);
2508 hsc_handle = NULL;
2509 return result;
2512 return 0;
2515 static void sony_nc_highspeed_charging_cleanup(struct platform_device *pd)
2517 if (hsc_handle) {
2518 device_remove_file(&pd->dev, hsc_handle);
2519 kfree(hsc_handle);
2520 hsc_handle = NULL;
2524 /* Touchpad enable/disable */
2525 struct touchpad_control {
2526 struct device_attribute attr;
2527 int handle;
2529 static struct touchpad_control *tp_ctl;
2531 static ssize_t sony_nc_touchpad_store(struct device *dev,
2532 struct device_attribute *attr, const char *buffer, size_t count)
2534 unsigned int result;
2535 unsigned long value;
2537 if (count > 31)
2538 return -EINVAL;
2540 if (kstrtoul(buffer, 10, &value) || value > 1)
2541 return -EINVAL;
2543 /* sysfs: 0 disabled, 1 enabled
2544 * EC: 0 enabled, 1 disabled
2546 if (sony_call_snc_handle(tp_ctl->handle,
2547 (!value << 0x10) | 0x100, &result))
2548 return -EIO;
2550 return count;
2553 static ssize_t sony_nc_touchpad_show(struct device *dev,
2554 struct device_attribute *attr, char *buffer)
2556 unsigned int result;
2558 if (sony_call_snc_handle(tp_ctl->handle, 0x000, &result))
2559 return -EINVAL;
2561 return snprintf(buffer, PAGE_SIZE, "%d\n", !(result & 0x01));
2564 static int sony_nc_touchpad_setup(struct platform_device *pd,
2565 unsigned int handle)
2567 int ret = 0;
2569 tp_ctl = kzalloc(sizeof(struct touchpad_control), GFP_KERNEL);
2570 if (!tp_ctl)
2571 return -ENOMEM;
2573 tp_ctl->handle = handle;
2575 sysfs_attr_init(&tp_ctl->attr.attr);
2576 tp_ctl->attr.attr.name = "touchpad";
2577 tp_ctl->attr.attr.mode = S_IRUGO | S_IWUSR;
2578 tp_ctl->attr.show = sony_nc_touchpad_show;
2579 tp_ctl->attr.store = sony_nc_touchpad_store;
2581 ret = device_create_file(&pd->dev, &tp_ctl->attr);
2582 if (ret) {
2583 kfree(tp_ctl);
2584 tp_ctl = NULL;
2587 return ret;
2590 static void sony_nc_touchpad_cleanup(struct platform_device *pd)
2592 if (tp_ctl) {
2593 device_remove_file(&pd->dev, &tp_ctl->attr);
2594 kfree(tp_ctl);
2595 tp_ctl = NULL;
2599 static void sony_nc_backlight_ng_read_limits(int handle,
2600 struct sony_backlight_props *props)
2602 u64 offset;
2603 int i;
2604 int lvl_table_len = 0;
2605 u8 min = 0xff, max = 0x00;
2606 unsigned char buffer[32] = { 0 };
2608 props->handle = handle;
2609 props->offset = 0;
2610 props->maxlvl = 0xff;
2612 offset = sony_find_snc_handle(handle);
2614 /* try to read the boundaries from ACPI tables, if we fail the above
2615 * defaults should be reasonable
2617 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
2618 32);
2619 if (i < 0)
2620 return;
2622 switch (handle) {
2623 case 0x012f:
2624 case 0x0137:
2625 lvl_table_len = 9;
2626 break;
2627 case 0x143:
2628 case 0x14b:
2629 case 0x14c:
2630 lvl_table_len = 16;
2631 break;
2634 /* the buffer lists brightness levels available, brightness levels are
2635 * from position 0 to 8 in the array, other values are used by ALS
2636 * control.
2638 for (i = 0; i < lvl_table_len && i < ARRAY_SIZE(buffer); i++) {
2640 dprintk("Brightness level: %d\n", buffer[i]);
2642 if (!buffer[i])
2643 break;
2645 if (buffer[i] > max)
2646 max = buffer[i];
2647 if (buffer[i] < min)
2648 min = buffer[i];
2650 props->offset = min;
2651 props->maxlvl = max;
2652 dprintk("Brightness levels: min=%d max=%d\n", props->offset,
2653 props->maxlvl);
2656 static void sony_nc_backlight_setup(void)
2658 acpi_handle unused;
2659 int max_brightness = 0;
2660 const struct backlight_ops *ops = NULL;
2661 struct backlight_properties props;
2663 if (sony_find_snc_handle(0x12f) >= 0) {
2664 ops = &sony_backlight_ng_ops;
2665 sony_bl_props.cmd_base = 0x0100;
2666 sony_nc_backlight_ng_read_limits(0x12f, &sony_bl_props);
2667 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
2669 } else if (sony_find_snc_handle(0x137) >= 0) {
2670 ops = &sony_backlight_ng_ops;
2671 sony_bl_props.cmd_base = 0x0100;
2672 sony_nc_backlight_ng_read_limits(0x137, &sony_bl_props);
2673 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
2675 } else if (sony_find_snc_handle(0x143) >= 0) {
2676 ops = &sony_backlight_ng_ops;
2677 sony_bl_props.cmd_base = 0x3000;
2678 sony_nc_backlight_ng_read_limits(0x143, &sony_bl_props);
2679 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
2681 } else if (sony_find_snc_handle(0x14b) >= 0) {
2682 ops = &sony_backlight_ng_ops;
2683 sony_bl_props.cmd_base = 0x3000;
2684 sony_nc_backlight_ng_read_limits(0x14b, &sony_bl_props);
2685 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
2687 } else if (sony_find_snc_handle(0x14c) >= 0) {
2688 ops = &sony_backlight_ng_ops;
2689 sony_bl_props.cmd_base = 0x3000;
2690 sony_nc_backlight_ng_read_limits(0x14c, &sony_bl_props);
2691 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
2693 } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
2694 &unused))) {
2695 ops = &sony_backlight_ops;
2696 max_brightness = SONY_MAX_BRIGHTNESS - 1;
2698 } else
2699 return;
2701 memset(&props, 0, sizeof(struct backlight_properties));
2702 props.type = BACKLIGHT_PLATFORM;
2703 props.max_brightness = max_brightness;
2704 sony_bl_props.dev = backlight_device_register("sony", NULL,
2705 &sony_bl_props,
2706 ops, &props);
2708 if (IS_ERR(sony_bl_props.dev)) {
2709 pr_warn("unable to register backlight device\n");
2710 sony_bl_props.dev = NULL;
2711 } else
2712 sony_bl_props.dev->props.brightness =
2713 ops->get_brightness(sony_bl_props.dev);
2716 static void sony_nc_backlight_cleanup(void)
2718 if (sony_bl_props.dev)
2719 backlight_device_unregister(sony_bl_props.dev);
2722 static int sony_nc_add(struct acpi_device *device)
2724 acpi_status status;
2725 int result = 0;
2726 acpi_handle handle;
2727 struct sony_nc_value *item;
2729 pr_info("%s v%s\n", SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
2731 sony_nc_acpi_device = device;
2732 strcpy(acpi_device_class(device), "sony/hotkey");
2734 sony_nc_acpi_handle = device->handle;
2736 /* read device status */
2737 result = acpi_bus_get_status(device);
2738 /* bail IFF the above call was successful and the device is not present */
2739 if (!result && !device->status.present) {
2740 dprintk("Device not present\n");
2741 result = -ENODEV;
2742 goto outwalk;
2745 result = sony_pf_add();
2746 if (result)
2747 goto outpresent;
2749 if (debug) {
2750 status = acpi_walk_namespace(ACPI_TYPE_METHOD,
2751 sony_nc_acpi_handle, 1, sony_walk_callback,
2752 NULL, NULL, NULL);
2753 if (ACPI_FAILURE(status)) {
2754 pr_warn("unable to walk acpi resources\n");
2755 result = -ENODEV;
2756 goto outpresent;
2760 result = sony_laptop_setup_input(device);
2761 if (result) {
2762 pr_err("Unable to create input devices\n");
2763 goto outplatform;
2766 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
2767 &handle))) {
2768 int arg = 1;
2769 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
2770 dprintk("ECON Method failed\n");
2773 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
2774 &handle))) {
2775 dprintk("Doing SNC setup\n");
2776 /* retrieve the available handles */
2777 result = sony_nc_handles_setup(sony_pf_device);
2778 if (!result)
2779 sony_nc_function_setup(device, sony_pf_device);
2782 /* setup input devices and helper fifo */
2783 if (acpi_video_backlight_support()) {
2784 pr_info("brightness ignored, must be controlled by ACPI video driver\n");
2785 } else {
2786 sony_nc_backlight_setup();
2789 /* create sony_pf sysfs attributes related to the SNC device */
2790 for (item = sony_nc_values; item->name; ++item) {
2792 if (!debug && item->debug)
2793 continue;
2795 /* find the available acpiget as described in the DSDT */
2796 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
2797 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
2798 *item->acpiget,
2799 &handle))) {
2800 dprintk("Found %s getter: %s\n",
2801 item->name, *item->acpiget);
2802 item->devattr.attr.mode |= S_IRUGO;
2803 break;
2807 /* find the available acpiset as described in the DSDT */
2808 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
2809 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
2810 *item->acpiset,
2811 &handle))) {
2812 dprintk("Found %s setter: %s\n",
2813 item->name, *item->acpiset);
2814 item->devattr.attr.mode |= S_IWUSR;
2815 break;
2819 if (item->devattr.attr.mode != 0) {
2820 result =
2821 device_create_file(&sony_pf_device->dev,
2822 &item->devattr);
2823 if (result)
2824 goto out_sysfs;
2828 return 0;
2830 out_sysfs:
2831 for (item = sony_nc_values; item->name; ++item) {
2832 device_remove_file(&sony_pf_device->dev, &item->devattr);
2834 sony_nc_backlight_cleanup();
2835 sony_nc_function_cleanup(sony_pf_device);
2836 sony_nc_handles_cleanup(sony_pf_device);
2838 outplatform:
2839 sony_laptop_remove_input();
2841 outpresent:
2842 sony_pf_remove();
2844 outwalk:
2845 sony_nc_rfkill_cleanup();
2846 return result;
2849 static int sony_nc_remove(struct acpi_device *device)
2851 struct sony_nc_value *item;
2853 sony_nc_backlight_cleanup();
2855 sony_nc_acpi_device = NULL;
2857 for (item = sony_nc_values; item->name; ++item) {
2858 device_remove_file(&sony_pf_device->dev, &item->devattr);
2861 sony_nc_function_cleanup(sony_pf_device);
2862 sony_nc_handles_cleanup(sony_pf_device);
2863 sony_pf_remove();
2864 sony_laptop_remove_input();
2865 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
2867 return 0;
2870 static const struct acpi_device_id sony_device_ids[] = {
2871 {SONY_NC_HID, 0},
2872 {SONY_PIC_HID, 0},
2873 {"", 0},
2875 MODULE_DEVICE_TABLE(acpi, sony_device_ids);
2877 static const struct acpi_device_id sony_nc_device_ids[] = {
2878 {SONY_NC_HID, 0},
2879 {"", 0},
2882 static struct acpi_driver sony_nc_driver = {
2883 .name = SONY_NC_DRIVER_NAME,
2884 .class = SONY_NC_CLASS,
2885 .ids = sony_nc_device_ids,
2886 .owner = THIS_MODULE,
2887 .ops = {
2888 .add = sony_nc_add,
2889 .remove = sony_nc_remove,
2890 .notify = sony_nc_notify,
2892 .drv.pm = &sony_nc_pm,
2895 /*********** SPIC (SNY6001) Device ***********/
2897 #define SONYPI_DEVICE_TYPE1 0x00000001
2898 #define SONYPI_DEVICE_TYPE2 0x00000002
2899 #define SONYPI_DEVICE_TYPE3 0x00000004
2901 #define SONYPI_TYPE1_OFFSET 0x04
2902 #define SONYPI_TYPE2_OFFSET 0x12
2903 #define SONYPI_TYPE3_OFFSET 0x12
2905 struct sony_pic_ioport {
2906 struct acpi_resource_io io1;
2907 struct acpi_resource_io io2;
2908 struct list_head list;
2911 struct sony_pic_irq {
2912 struct acpi_resource_irq irq;
2913 struct list_head list;
2916 struct sonypi_eventtypes {
2917 u8 data;
2918 unsigned long mask;
2919 struct sonypi_event *events;
2922 struct sony_pic_dev {
2923 struct acpi_device *acpi_dev;
2924 struct sony_pic_irq *cur_irq;
2925 struct sony_pic_ioport *cur_ioport;
2926 struct list_head interrupts;
2927 struct list_head ioports;
2928 struct mutex lock;
2929 struct sonypi_eventtypes *event_types;
2930 int (*handle_irq)(const u8, const u8);
2931 int model;
2932 u16 evport_offset;
2933 u8 camera_power;
2934 u8 bluetooth_power;
2935 u8 wwan_power;
2938 static struct sony_pic_dev spic_dev = {
2939 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
2940 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
2943 static int spic_drv_registered;
2945 /* Event masks */
2946 #define SONYPI_JOGGER_MASK 0x00000001
2947 #define SONYPI_CAPTURE_MASK 0x00000002
2948 #define SONYPI_FNKEY_MASK 0x00000004
2949 #define SONYPI_BLUETOOTH_MASK 0x00000008
2950 #define SONYPI_PKEY_MASK 0x00000010
2951 #define SONYPI_BACK_MASK 0x00000020
2952 #define SONYPI_HELP_MASK 0x00000040
2953 #define SONYPI_LID_MASK 0x00000080
2954 #define SONYPI_ZOOM_MASK 0x00000100
2955 #define SONYPI_THUMBPHRASE_MASK 0x00000200
2956 #define SONYPI_MEYE_MASK 0x00000400
2957 #define SONYPI_MEMORYSTICK_MASK 0x00000800
2958 #define SONYPI_BATTERY_MASK 0x00001000
2959 #define SONYPI_WIRELESS_MASK 0x00002000
2961 struct sonypi_event {
2962 u8 data;
2963 u8 event;
2966 /* The set of possible button release events */
2967 static struct sonypi_event sonypi_releaseev[] = {
2968 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
2969 { 0, 0 }
2972 /* The set of possible jogger events */
2973 static struct sonypi_event sonypi_joggerev[] = {
2974 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
2975 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
2976 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
2977 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
2978 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
2979 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
2980 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
2981 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
2982 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
2983 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
2984 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
2985 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
2986 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
2987 { 0, 0 }
2990 /* The set of possible capture button events */
2991 static struct sonypi_event sonypi_captureev[] = {
2992 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
2993 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
2994 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
2995 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
2996 { 0, 0 }
2999 /* The set of possible fnkeys events */
3000 static struct sonypi_event sonypi_fnkeyev[] = {
3001 { 0x10, SONYPI_EVENT_FNKEY_ESC },
3002 { 0x11, SONYPI_EVENT_FNKEY_F1 },
3003 { 0x12, SONYPI_EVENT_FNKEY_F2 },
3004 { 0x13, SONYPI_EVENT_FNKEY_F3 },
3005 { 0x14, SONYPI_EVENT_FNKEY_F4 },
3006 { 0x15, SONYPI_EVENT_FNKEY_F5 },
3007 { 0x16, SONYPI_EVENT_FNKEY_F6 },
3008 { 0x17, SONYPI_EVENT_FNKEY_F7 },
3009 { 0x18, SONYPI_EVENT_FNKEY_F8 },
3010 { 0x19, SONYPI_EVENT_FNKEY_F9 },
3011 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
3012 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
3013 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
3014 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
3015 { 0x21, SONYPI_EVENT_FNKEY_1 },
3016 { 0x22, SONYPI_EVENT_FNKEY_2 },
3017 { 0x31, SONYPI_EVENT_FNKEY_D },
3018 { 0x32, SONYPI_EVENT_FNKEY_E },
3019 { 0x33, SONYPI_EVENT_FNKEY_F },
3020 { 0x34, SONYPI_EVENT_FNKEY_S },
3021 { 0x35, SONYPI_EVENT_FNKEY_B },
3022 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
3023 { 0, 0 }
3026 /* The set of possible program key events */
3027 static struct sonypi_event sonypi_pkeyev[] = {
3028 { 0x01, SONYPI_EVENT_PKEY_P1 },
3029 { 0x02, SONYPI_EVENT_PKEY_P2 },
3030 { 0x04, SONYPI_EVENT_PKEY_P3 },
3031 { 0x20, SONYPI_EVENT_PKEY_P1 },
3032 { 0, 0 }
3035 /* The set of possible bluetooth events */
3036 static struct sonypi_event sonypi_blueev[] = {
3037 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
3038 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
3039 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
3040 { 0, 0 }
3043 /* The set of possible wireless events */
3044 static struct sonypi_event sonypi_wlessev[] = {
3045 { 0x59, SONYPI_EVENT_IGNORE },
3046 { 0x5a, SONYPI_EVENT_IGNORE },
3047 { 0, 0 }
3050 /* The set of possible back button events */
3051 static struct sonypi_event sonypi_backev[] = {
3052 { 0x20, SONYPI_EVENT_BACK_PRESSED },
3053 { 0, 0 }
3056 /* The set of possible help button events */
3057 static struct sonypi_event sonypi_helpev[] = {
3058 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
3059 { 0, 0 }
3063 /* The set of possible lid events */
3064 static struct sonypi_event sonypi_lidev[] = {
3065 { 0x51, SONYPI_EVENT_LID_CLOSED },
3066 { 0x50, SONYPI_EVENT_LID_OPENED },
3067 { 0, 0 }
3070 /* The set of possible zoom events */
3071 static struct sonypi_event sonypi_zoomev[] = {
3072 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
3073 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
3074 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
3075 { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
3076 { 0, 0 }
3079 /* The set of possible thumbphrase events */
3080 static struct sonypi_event sonypi_thumbphraseev[] = {
3081 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
3082 { 0, 0 }
3085 /* The set of possible motioneye camera events */
3086 static struct sonypi_event sonypi_meyeev[] = {
3087 { 0x00, SONYPI_EVENT_MEYE_FACE },
3088 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
3089 { 0, 0 }
3092 /* The set of possible memorystick events */
3093 static struct sonypi_event sonypi_memorystickev[] = {
3094 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
3095 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
3096 { 0, 0 }
3099 /* The set of possible battery events */
3100 static struct sonypi_event sonypi_batteryev[] = {
3101 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
3102 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
3103 { 0, 0 }
3106 /* The set of possible volume events */
3107 static struct sonypi_event sonypi_volumeev[] = {
3108 { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
3109 { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
3110 { 0, 0 }
3113 /* The set of possible brightness events */
3114 static struct sonypi_event sonypi_brightnessev[] = {
3115 { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
3116 { 0, 0 }
3119 static struct sonypi_eventtypes type1_events[] = {
3120 { 0, 0xffffffff, sonypi_releaseev },
3121 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
3122 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
3123 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
3124 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
3125 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3126 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
3127 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
3128 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3129 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
3130 { 0 },
3132 static struct sonypi_eventtypes type2_events[] = {
3133 { 0, 0xffffffff, sonypi_releaseev },
3134 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
3135 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
3136 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
3137 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3138 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
3139 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
3140 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
3141 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
3142 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
3143 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
3144 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3145 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
3146 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
3147 { 0 },
3149 static struct sonypi_eventtypes type3_events[] = {
3150 { 0, 0xffffffff, sonypi_releaseev },
3151 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3152 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
3153 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3154 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
3155 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
3156 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
3157 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
3158 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
3159 { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
3160 { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
3161 { 0 },
3164 /* low level spic calls */
3165 #define ITERATIONS_LONG 10000
3166 #define ITERATIONS_SHORT 10
3167 #define wait_on_command(command, iterations) { \
3168 unsigned int n = iterations; \
3169 while (--n && (command)) \
3170 udelay(1); \
3171 if (!n) \
3172 dprintk("command failed at %s : %s (line %d)\n", \
3173 __FILE__, __func__, __LINE__); \
3176 static u8 sony_pic_call1(u8 dev)
3178 u8 v1, v2;
3180 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
3181 ITERATIONS_LONG);
3182 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3183 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
3184 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
3185 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
3186 return v2;
3189 static u8 sony_pic_call2(u8 dev, u8 fn)
3191 u8 v1;
3193 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
3194 ITERATIONS_LONG);
3195 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3196 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
3197 ITERATIONS_LONG);
3198 outb(fn, spic_dev.cur_ioport->io1.minimum);
3199 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
3200 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
3201 return v1;
3204 static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
3206 u8 v1;
3208 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3209 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3210 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3211 outb(fn, spic_dev.cur_ioport->io1.minimum);
3212 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3213 outb(v, spic_dev.cur_ioport->io1.minimum);
3214 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
3215 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
3216 dev, fn, v, v1);
3217 return v1;
3221 * minidrivers for SPIC models
3223 static int type3_handle_irq(const u8 data_mask, const u8 ev)
3226 * 0x31 could mean we have to take some extra action and wait for
3227 * the next irq for some Type3 models, it will generate a new
3228 * irq and we can read new data from the device:
3229 * - 0x5c and 0x5f requires 0xA0
3230 * - 0x61 requires 0xB3
3232 if (data_mask == 0x31) {
3233 if (ev == 0x5c || ev == 0x5f)
3234 sony_pic_call1(0xA0);
3235 else if (ev == 0x61)
3236 sony_pic_call1(0xB3);
3237 return 0;
3239 return 1;
3242 static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
3244 struct pci_dev *pcidev;
3246 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3247 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
3248 if (pcidev) {
3249 dev->model = SONYPI_DEVICE_TYPE1;
3250 dev->evport_offset = SONYPI_TYPE1_OFFSET;
3251 dev->event_types = type1_events;
3252 goto out;
3255 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3256 PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
3257 if (pcidev) {
3258 dev->model = SONYPI_DEVICE_TYPE2;
3259 dev->evport_offset = SONYPI_TYPE2_OFFSET;
3260 dev->event_types = type2_events;
3261 goto out;
3264 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3265 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
3266 if (pcidev) {
3267 dev->model = SONYPI_DEVICE_TYPE3;
3268 dev->handle_irq = type3_handle_irq;
3269 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3270 dev->event_types = type3_events;
3271 goto out;
3274 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3275 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
3276 if (pcidev) {
3277 dev->model = SONYPI_DEVICE_TYPE3;
3278 dev->handle_irq = type3_handle_irq;
3279 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3280 dev->event_types = type3_events;
3281 goto out;
3284 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3285 PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
3286 if (pcidev) {
3287 dev->model = SONYPI_DEVICE_TYPE3;
3288 dev->handle_irq = type3_handle_irq;
3289 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3290 dev->event_types = type3_events;
3291 goto out;
3294 /* default */
3295 dev->model = SONYPI_DEVICE_TYPE2;
3296 dev->evport_offset = SONYPI_TYPE2_OFFSET;
3297 dev->event_types = type2_events;
3299 out:
3300 if (pcidev)
3301 pci_dev_put(pcidev);
3303 pr_info("detected Type%d model\n",
3304 dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
3305 dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
3308 /* camera tests and poweron/poweroff */
3309 #define SONYPI_CAMERA_PICTURE 5
3310 #define SONYPI_CAMERA_CONTROL 0x10
3312 #define SONYPI_CAMERA_BRIGHTNESS 0
3313 #define SONYPI_CAMERA_CONTRAST 1
3314 #define SONYPI_CAMERA_HUE 2
3315 #define SONYPI_CAMERA_COLOR 3
3316 #define SONYPI_CAMERA_SHARPNESS 4
3318 #define SONYPI_CAMERA_EXPOSURE_MASK 0xC
3319 #define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
3320 #define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
3321 #define SONYPI_CAMERA_MUTE_MASK 0x40
3323 /* the rest don't need a loop until not 0xff */
3324 #define SONYPI_CAMERA_AGC 6
3325 #define SONYPI_CAMERA_AGC_MASK 0x30
3326 #define SONYPI_CAMERA_SHUTTER_MASK 0x7
3328 #define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
3329 #define SONYPI_CAMERA_CONTROL 0x10
3331 #define SONYPI_CAMERA_STATUS 7
3332 #define SONYPI_CAMERA_STATUS_READY 0x2
3333 #define SONYPI_CAMERA_STATUS_POSITION 0x4
3335 #define SONYPI_DIRECTION_BACKWARDS 0x4
3337 #define SONYPI_CAMERA_REVISION 8
3338 #define SONYPI_CAMERA_ROMVERSION 9
3340 static int __sony_pic_camera_ready(void)
3342 u8 v;
3344 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
3345 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
3348 static int __sony_pic_camera_off(void)
3350 if (!camera) {
3351 pr_warn("camera control not enabled\n");
3352 return -ENODEV;
3355 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
3356 SONYPI_CAMERA_MUTE_MASK),
3357 ITERATIONS_SHORT);
3359 if (spic_dev.camera_power) {
3360 sony_pic_call2(0x91, 0);
3361 spic_dev.camera_power = 0;
3363 return 0;
3366 static int __sony_pic_camera_on(void)
3368 int i, j, x;
3370 if (!camera) {
3371 pr_warn("camera control not enabled\n");
3372 return -ENODEV;
3375 if (spic_dev.camera_power)
3376 return 0;
3378 for (j = 5; j > 0; j--) {
3380 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
3381 msleep(10);
3382 sony_pic_call1(0x93);
3384 for (i = 400; i > 0; i--) {
3385 if (__sony_pic_camera_ready())
3386 break;
3387 msleep(10);
3389 if (i)
3390 break;
3393 if (j == 0) {
3394 pr_warn("failed to power on camera\n");
3395 return -ENODEV;
3398 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
3399 0x5a),
3400 ITERATIONS_SHORT);
3402 spic_dev.camera_power = 1;
3403 return 0;
3406 /* External camera command (exported to the motion eye v4l driver) */
3407 int sony_pic_camera_command(int command, u8 value)
3409 if (!camera)
3410 return -EIO;
3412 mutex_lock(&spic_dev.lock);
3414 switch (command) {
3415 case SONY_PIC_COMMAND_SETCAMERA:
3416 if (value)
3417 __sony_pic_camera_on();
3418 else
3419 __sony_pic_camera_off();
3420 break;
3421 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
3422 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
3423 ITERATIONS_SHORT);
3424 break;
3425 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
3426 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
3427 ITERATIONS_SHORT);
3428 break;
3429 case SONY_PIC_COMMAND_SETCAMERAHUE:
3430 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
3431 ITERATIONS_SHORT);
3432 break;
3433 case SONY_PIC_COMMAND_SETCAMERACOLOR:
3434 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
3435 ITERATIONS_SHORT);
3436 break;
3437 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
3438 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
3439 ITERATIONS_SHORT);
3440 break;
3441 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
3442 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
3443 ITERATIONS_SHORT);
3444 break;
3445 case SONY_PIC_COMMAND_SETCAMERAAGC:
3446 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
3447 ITERATIONS_SHORT);
3448 break;
3449 default:
3450 pr_err("sony_pic_camera_command invalid: %d\n", command);
3451 break;
3453 mutex_unlock(&spic_dev.lock);
3454 return 0;
3456 EXPORT_SYMBOL(sony_pic_camera_command);
3458 /* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
3459 static void __sony_pic_set_wwanpower(u8 state)
3461 state = !!state;
3462 if (spic_dev.wwan_power == state)
3463 return;
3464 sony_pic_call2(0xB0, state);
3465 sony_pic_call1(0x82);
3466 spic_dev.wwan_power = state;
3469 static ssize_t sony_pic_wwanpower_store(struct device *dev,
3470 struct device_attribute *attr,
3471 const char *buffer, size_t count)
3473 unsigned long value;
3474 if (count > 31)
3475 return -EINVAL;
3477 if (kstrtoul(buffer, 10, &value))
3478 return -EINVAL;
3480 mutex_lock(&spic_dev.lock);
3481 __sony_pic_set_wwanpower(value);
3482 mutex_unlock(&spic_dev.lock);
3484 return count;
3487 static ssize_t sony_pic_wwanpower_show(struct device *dev,
3488 struct device_attribute *attr, char *buffer)
3490 ssize_t count;
3491 mutex_lock(&spic_dev.lock);
3492 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
3493 mutex_unlock(&spic_dev.lock);
3494 return count;
3497 /* bluetooth subsystem power state */
3498 static void __sony_pic_set_bluetoothpower(u8 state)
3500 state = !!state;
3501 if (spic_dev.bluetooth_power == state)
3502 return;
3503 sony_pic_call2(0x96, state);
3504 sony_pic_call1(0x82);
3505 spic_dev.bluetooth_power = state;
3508 static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
3509 struct device_attribute *attr,
3510 const char *buffer, size_t count)
3512 unsigned long value;
3513 if (count > 31)
3514 return -EINVAL;
3516 if (kstrtoul(buffer, 10, &value))
3517 return -EINVAL;
3519 mutex_lock(&spic_dev.lock);
3520 __sony_pic_set_bluetoothpower(value);
3521 mutex_unlock(&spic_dev.lock);
3523 return count;
3526 static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
3527 struct device_attribute *attr, char *buffer)
3529 ssize_t count = 0;
3530 mutex_lock(&spic_dev.lock);
3531 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
3532 mutex_unlock(&spic_dev.lock);
3533 return count;
3536 /* fan speed */
3537 /* FAN0 information (reverse engineered from ACPI tables) */
3538 #define SONY_PIC_FAN0_STATUS 0x93
3539 static int sony_pic_set_fanspeed(unsigned long value)
3541 return ec_write(SONY_PIC_FAN0_STATUS, value);
3544 static int sony_pic_get_fanspeed(u8 *value)
3546 return ec_read(SONY_PIC_FAN0_STATUS, value);
3549 static ssize_t sony_pic_fanspeed_store(struct device *dev,
3550 struct device_attribute *attr,
3551 const char *buffer, size_t count)
3553 unsigned long value;
3554 if (count > 31)
3555 return -EINVAL;
3557 if (kstrtoul(buffer, 10, &value))
3558 return -EINVAL;
3560 if (sony_pic_set_fanspeed(value))
3561 return -EIO;
3563 return count;
3566 static ssize_t sony_pic_fanspeed_show(struct device *dev,
3567 struct device_attribute *attr, char *buffer)
3569 u8 value = 0;
3570 if (sony_pic_get_fanspeed(&value))
3571 return -EIO;
3573 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
3576 #define SPIC_ATTR(_name, _mode) \
3577 struct device_attribute spic_attr_##_name = __ATTR(_name, \
3578 _mode, sony_pic_## _name ##_show, \
3579 sony_pic_## _name ##_store)
3581 static SPIC_ATTR(bluetoothpower, 0644);
3582 static SPIC_ATTR(wwanpower, 0644);
3583 static SPIC_ATTR(fanspeed, 0644);
3585 static struct attribute *spic_attributes[] = {
3586 &spic_attr_bluetoothpower.attr,
3587 &spic_attr_wwanpower.attr,
3588 &spic_attr_fanspeed.attr,
3589 NULL
3592 static struct attribute_group spic_attribute_group = {
3593 .attrs = spic_attributes
3596 /******** SONYPI compatibility **********/
3597 #ifdef CONFIG_SONYPI_COMPAT
3599 /* battery / brightness / temperature addresses */
3600 #define SONYPI_BAT_FLAGS 0x81
3601 #define SONYPI_LCD_LIGHT 0x96
3602 #define SONYPI_BAT1_PCTRM 0xa0
3603 #define SONYPI_BAT1_LEFT 0xa2
3604 #define SONYPI_BAT1_MAXRT 0xa4
3605 #define SONYPI_BAT2_PCTRM 0xa8
3606 #define SONYPI_BAT2_LEFT 0xaa
3607 #define SONYPI_BAT2_MAXRT 0xac
3608 #define SONYPI_BAT1_MAXTK 0xb0
3609 #define SONYPI_BAT1_FULL 0xb2
3610 #define SONYPI_BAT2_MAXTK 0xb8
3611 #define SONYPI_BAT2_FULL 0xba
3612 #define SONYPI_TEMP_STATUS 0xC1
3614 struct sonypi_compat_s {
3615 struct fasync_struct *fifo_async;
3616 struct kfifo fifo;
3617 spinlock_t fifo_lock;
3618 wait_queue_head_t fifo_proc_list;
3619 atomic_t open_count;
3621 static struct sonypi_compat_s sonypi_compat = {
3622 .open_count = ATOMIC_INIT(0),
3625 static int sonypi_misc_fasync(int fd, struct file *filp, int on)
3627 return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
3630 static int sonypi_misc_release(struct inode *inode, struct file *file)
3632 atomic_dec(&sonypi_compat.open_count);
3633 return 0;
3636 static int sonypi_misc_open(struct inode *inode, struct file *file)
3638 /* Flush input queue on first open */
3639 unsigned long flags;
3641 spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
3643 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
3644 kfifo_reset(&sonypi_compat.fifo);
3646 spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
3648 return 0;
3651 static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
3652 size_t count, loff_t *pos)
3654 ssize_t ret;
3655 unsigned char c;
3657 if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
3658 (file->f_flags & O_NONBLOCK))
3659 return -EAGAIN;
3661 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
3662 kfifo_len(&sonypi_compat.fifo) != 0);
3663 if (ret)
3664 return ret;
3666 while (ret < count &&
3667 (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
3668 &sonypi_compat.fifo_lock) == sizeof(c))) {
3669 if (put_user(c, buf++))
3670 return -EFAULT;
3671 ret++;
3674 if (ret > 0) {
3675 struct inode *inode = file_inode(file);
3676 inode->i_atime = current_fs_time(inode->i_sb);
3679 return ret;
3682 static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
3684 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
3685 if (kfifo_len(&sonypi_compat.fifo))
3686 return POLLIN | POLLRDNORM;
3687 return 0;
3690 static int ec_read16(u8 addr, u16 *value)
3692 u8 val_lb, val_hb;
3693 if (ec_read(addr, &val_lb))
3694 return -1;
3695 if (ec_read(addr + 1, &val_hb))
3696 return -1;
3697 *value = val_lb | (val_hb << 8);
3698 return 0;
3701 static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
3702 unsigned long arg)
3704 int ret = 0;
3705 void __user *argp = (void __user *)arg;
3706 u8 val8;
3707 u16 val16;
3708 int value;
3710 mutex_lock(&spic_dev.lock);
3711 switch (cmd) {
3712 case SONYPI_IOCGBRT:
3713 if (sony_bl_props.dev == NULL) {
3714 ret = -EIO;
3715 break;
3717 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL,
3718 &value)) {
3719 ret = -EIO;
3720 break;
3722 val8 = ((value & 0xff) - 1) << 5;
3723 if (copy_to_user(argp, &val8, sizeof(val8)))
3724 ret = -EFAULT;
3725 break;
3726 case SONYPI_IOCSBRT:
3727 if (sony_bl_props.dev == NULL) {
3728 ret = -EIO;
3729 break;
3731 if (copy_from_user(&val8, argp, sizeof(val8))) {
3732 ret = -EFAULT;
3733 break;
3735 value = (val8 >> 5) + 1;
3736 if (sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &value,
3737 NULL)) {
3738 ret = -EIO;
3739 break;
3741 /* sync the backlight device status */
3742 sony_bl_props.dev->props.brightness =
3743 sony_backlight_get_brightness(sony_bl_props.dev);
3744 break;
3745 case SONYPI_IOCGBAT1CAP:
3746 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
3747 ret = -EIO;
3748 break;
3750 if (copy_to_user(argp, &val16, sizeof(val16)))
3751 ret = -EFAULT;
3752 break;
3753 case SONYPI_IOCGBAT1REM:
3754 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
3755 ret = -EIO;
3756 break;
3758 if (copy_to_user(argp, &val16, sizeof(val16)))
3759 ret = -EFAULT;
3760 break;
3761 case SONYPI_IOCGBAT2CAP:
3762 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
3763 ret = -EIO;
3764 break;
3766 if (copy_to_user(argp, &val16, sizeof(val16)))
3767 ret = -EFAULT;
3768 break;
3769 case SONYPI_IOCGBAT2REM:
3770 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
3771 ret = -EIO;
3772 break;
3774 if (copy_to_user(argp, &val16, sizeof(val16)))
3775 ret = -EFAULT;
3776 break;
3777 case SONYPI_IOCGBATFLAGS:
3778 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
3779 ret = -EIO;
3780 break;
3782 val8 &= 0x07;
3783 if (copy_to_user(argp, &val8, sizeof(val8)))
3784 ret = -EFAULT;
3785 break;
3786 case SONYPI_IOCGBLUE:
3787 val8 = spic_dev.bluetooth_power;
3788 if (copy_to_user(argp, &val8, sizeof(val8)))
3789 ret = -EFAULT;
3790 break;
3791 case SONYPI_IOCSBLUE:
3792 if (copy_from_user(&val8, argp, sizeof(val8))) {
3793 ret = -EFAULT;
3794 break;
3796 __sony_pic_set_bluetoothpower(val8);
3797 break;
3798 /* FAN Controls */
3799 case SONYPI_IOCGFAN:
3800 if (sony_pic_get_fanspeed(&val8)) {
3801 ret = -EIO;
3802 break;
3804 if (copy_to_user(argp, &val8, sizeof(val8)))
3805 ret = -EFAULT;
3806 break;
3807 case SONYPI_IOCSFAN:
3808 if (copy_from_user(&val8, argp, sizeof(val8))) {
3809 ret = -EFAULT;
3810 break;
3812 if (sony_pic_set_fanspeed(val8))
3813 ret = -EIO;
3814 break;
3815 /* GET Temperature (useful under APM) */
3816 case SONYPI_IOCGTEMP:
3817 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
3818 ret = -EIO;
3819 break;
3821 if (copy_to_user(argp, &val8, sizeof(val8)))
3822 ret = -EFAULT;
3823 break;
3824 default:
3825 ret = -EINVAL;
3827 mutex_unlock(&spic_dev.lock);
3828 return ret;
3831 static const struct file_operations sonypi_misc_fops = {
3832 .owner = THIS_MODULE,
3833 .read = sonypi_misc_read,
3834 .poll = sonypi_misc_poll,
3835 .open = sonypi_misc_open,
3836 .release = sonypi_misc_release,
3837 .fasync = sonypi_misc_fasync,
3838 .unlocked_ioctl = sonypi_misc_ioctl,
3839 .llseek = noop_llseek,
3842 static struct miscdevice sonypi_misc_device = {
3843 .minor = MISC_DYNAMIC_MINOR,
3844 .name = "sonypi",
3845 .fops = &sonypi_misc_fops,
3848 static void sonypi_compat_report_event(u8 event)
3850 kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
3851 sizeof(event), &sonypi_compat.fifo_lock);
3852 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
3853 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
3856 static int sonypi_compat_init(void)
3858 int error;
3860 spin_lock_init(&sonypi_compat.fifo_lock);
3861 error =
3862 kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
3863 if (error) {
3864 pr_err("kfifo_alloc failed\n");
3865 return error;
3868 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
3870 if (minor != -1)
3871 sonypi_misc_device.minor = minor;
3872 error = misc_register(&sonypi_misc_device);
3873 if (error) {
3874 pr_err("misc_register failed\n");
3875 goto err_free_kfifo;
3877 if (minor == -1)
3878 pr_info("device allocated minor is %d\n",
3879 sonypi_misc_device.minor);
3881 return 0;
3883 err_free_kfifo:
3884 kfifo_free(&sonypi_compat.fifo);
3885 return error;
3888 static void sonypi_compat_exit(void)
3890 misc_deregister(&sonypi_misc_device);
3891 kfifo_free(&sonypi_compat.fifo);
3893 #else
3894 static int sonypi_compat_init(void) { return 0; }
3895 static void sonypi_compat_exit(void) { }
3896 static void sonypi_compat_report_event(u8 event) { }
3897 #endif /* CONFIG_SONYPI_COMPAT */
3900 * ACPI callbacks
3902 static acpi_status
3903 sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
3905 u32 i;
3906 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
3908 switch (resource->type) {
3909 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
3911 /* start IO enumeration */
3912 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
3913 if (!ioport)
3914 return AE_ERROR;
3916 list_add(&ioport->list, &dev->ioports);
3917 return AE_OK;
3920 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
3921 /* end IO enumeration */
3922 return AE_OK;
3924 case ACPI_RESOURCE_TYPE_IRQ:
3926 struct acpi_resource_irq *p = &resource->data.irq;
3927 struct sony_pic_irq *interrupt = NULL;
3928 if (!p || !p->interrupt_count) {
3930 * IRQ descriptors may have no IRQ# bits set,
3931 * particularly those those w/ _STA disabled
3933 dprintk("Blank IRQ resource\n");
3934 return AE_OK;
3936 for (i = 0; i < p->interrupt_count; i++) {
3937 if (!p->interrupts[i]) {
3938 pr_warn("Invalid IRQ %d\n",
3939 p->interrupts[i]);
3940 continue;
3942 interrupt = kzalloc(sizeof(*interrupt),
3943 GFP_KERNEL);
3944 if (!interrupt)
3945 return AE_ERROR;
3947 list_add(&interrupt->list, &dev->interrupts);
3948 interrupt->irq.triggering = p->triggering;
3949 interrupt->irq.polarity = p->polarity;
3950 interrupt->irq.sharable = p->sharable;
3951 interrupt->irq.interrupt_count = 1;
3952 interrupt->irq.interrupts[0] = p->interrupts[i];
3954 return AE_OK;
3956 case ACPI_RESOURCE_TYPE_IO:
3958 struct acpi_resource_io *io = &resource->data.io;
3959 struct sony_pic_ioport *ioport =
3960 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
3961 if (!io) {
3962 dprintk("Blank IO resource\n");
3963 return AE_OK;
3966 if (!ioport->io1.minimum) {
3967 memcpy(&ioport->io1, io, sizeof(*io));
3968 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
3969 ioport->io1.address_length);
3971 else if (!ioport->io2.minimum) {
3972 memcpy(&ioport->io2, io, sizeof(*io));
3973 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
3974 ioport->io2.address_length);
3976 else {
3977 pr_err("Unknown SPIC Type, more than 2 IO Ports\n");
3978 return AE_ERROR;
3980 return AE_OK;
3982 default:
3983 dprintk("Resource %d isn't an IRQ nor an IO port\n",
3984 resource->type);
3986 case ACPI_RESOURCE_TYPE_END_TAG:
3987 return AE_OK;
3989 return AE_CTRL_TERMINATE;
3992 static int sony_pic_possible_resources(struct acpi_device *device)
3994 int result = 0;
3995 acpi_status status = AE_OK;
3997 if (!device)
3998 return -EINVAL;
4000 /* get device status */
4001 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
4002 dprintk("Evaluating _STA\n");
4003 result = acpi_bus_get_status(device);
4004 if (result) {
4005 pr_warn("Unable to read status\n");
4006 goto end;
4009 if (!device->status.enabled)
4010 dprintk("Device disabled\n");
4011 else
4012 dprintk("Device enabled\n");
4015 * Query and parse 'method'
4017 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
4018 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
4019 sony_pic_read_possible_resource, &spic_dev);
4020 if (ACPI_FAILURE(status)) {
4021 pr_warn("Failure evaluating %s\n", METHOD_NAME__PRS);
4022 result = -ENODEV;
4024 end:
4025 return result;
4029 * Disable the spic device by calling its _DIS method
4031 static int sony_pic_disable(struct acpi_device *device)
4033 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
4034 NULL);
4036 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
4037 return -ENXIO;
4039 dprintk("Device disabled\n");
4040 return 0;
4045 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
4047 * Call _SRS to set current resources
4049 static int sony_pic_enable(struct acpi_device *device,
4050 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
4052 acpi_status status;
4053 int result = 0;
4054 /* Type 1 resource layout is:
4055 * IO
4056 * IO
4057 * IRQNoFlags
4058 * End
4060 * Type 2 and 3 resource layout is:
4061 * IO
4062 * IRQNoFlags
4063 * End
4065 struct {
4066 struct acpi_resource res1;
4067 struct acpi_resource res2;
4068 struct acpi_resource res3;
4069 struct acpi_resource res4;
4070 } *resource;
4071 struct acpi_buffer buffer = { 0, NULL };
4073 if (!ioport || !irq)
4074 return -EINVAL;
4076 /* init acpi_buffer */
4077 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
4078 if (!resource)
4079 return -ENOMEM;
4081 buffer.length = sizeof(*resource) + 1;
4082 buffer.pointer = resource;
4084 /* setup Type 1 resources */
4085 if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
4087 /* setup io resources */
4088 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
4089 resource->res1.length = sizeof(struct acpi_resource);
4090 memcpy(&resource->res1.data.io, &ioport->io1,
4091 sizeof(struct acpi_resource_io));
4093 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
4094 resource->res2.length = sizeof(struct acpi_resource);
4095 memcpy(&resource->res2.data.io, &ioport->io2,
4096 sizeof(struct acpi_resource_io));
4098 /* setup irq resource */
4099 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
4100 resource->res3.length = sizeof(struct acpi_resource);
4101 memcpy(&resource->res3.data.irq, &irq->irq,
4102 sizeof(struct acpi_resource_irq));
4103 /* we requested a shared irq */
4104 resource->res3.data.irq.sharable = ACPI_SHARED;
4106 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
4107 resource->res4.length = sizeof(struct acpi_resource);
4109 /* setup Type 2/3 resources */
4110 else {
4111 /* setup io resource */
4112 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
4113 resource->res1.length = sizeof(struct acpi_resource);
4114 memcpy(&resource->res1.data.io, &ioport->io1,
4115 sizeof(struct acpi_resource_io));
4117 /* setup irq resource */
4118 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
4119 resource->res2.length = sizeof(struct acpi_resource);
4120 memcpy(&resource->res2.data.irq, &irq->irq,
4121 sizeof(struct acpi_resource_irq));
4122 /* we requested a shared irq */
4123 resource->res2.data.irq.sharable = ACPI_SHARED;
4125 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
4126 resource->res3.length = sizeof(struct acpi_resource);
4129 /* Attempt to set the resource */
4130 dprintk("Evaluating _SRS\n");
4131 status = acpi_set_current_resources(device->handle, &buffer);
4133 /* check for total failure */
4134 if (ACPI_FAILURE(status)) {
4135 pr_err("Error evaluating _SRS\n");
4136 result = -ENODEV;
4137 goto end;
4140 /* Necessary device initializations calls (from sonypi) */
4141 sony_pic_call1(0x82);
4142 sony_pic_call2(0x81, 0xff);
4143 sony_pic_call1(compat ? 0x92 : 0x82);
4145 end:
4146 kfree(resource);
4147 return result;
4150 /*****************
4152 * ISR: some event is available
4154 *****************/
4155 static irqreturn_t sony_pic_irq(int irq, void *dev_id)
4157 int i, j;
4158 u8 ev = 0;
4159 u8 data_mask = 0;
4160 u8 device_event = 0;
4162 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
4164 ev = inb_p(dev->cur_ioport->io1.minimum);
4165 if (dev->cur_ioport->io2.minimum)
4166 data_mask = inb_p(dev->cur_ioport->io2.minimum);
4167 else
4168 data_mask = inb_p(dev->cur_ioport->io1.minimum +
4169 dev->evport_offset);
4171 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
4172 ev, data_mask, dev->cur_ioport->io1.minimum,
4173 dev->evport_offset);
4175 if (ev == 0x00 || ev == 0xff)
4176 return IRQ_HANDLED;
4178 for (i = 0; dev->event_types[i].mask; i++) {
4180 if ((data_mask & dev->event_types[i].data) !=
4181 dev->event_types[i].data)
4182 continue;
4184 if (!(mask & dev->event_types[i].mask))
4185 continue;
4187 for (j = 0; dev->event_types[i].events[j].event; j++) {
4188 if (ev == dev->event_types[i].events[j].data) {
4189 device_event =
4190 dev->event_types[i].events[j].event;
4191 /* some events may require ignoring */
4192 if (!device_event)
4193 return IRQ_HANDLED;
4194 goto found;
4198 /* Still not able to decode the event try to pass
4199 * it over to the minidriver
4201 if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
4202 return IRQ_HANDLED;
4204 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
4205 ev, data_mask, dev->cur_ioport->io1.minimum,
4206 dev->evport_offset);
4207 return IRQ_HANDLED;
4209 found:
4210 sony_laptop_report_input_event(device_event);
4211 sonypi_compat_report_event(device_event);
4212 return IRQ_HANDLED;
4215 /*****************
4217 * ACPI driver
4219 *****************/
4220 static int sony_pic_remove(struct acpi_device *device)
4222 struct sony_pic_ioport *io, *tmp_io;
4223 struct sony_pic_irq *irq, *tmp_irq;
4225 if (sony_pic_disable(device)) {
4226 pr_err("Couldn't disable device\n");
4227 return -ENXIO;
4230 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
4231 release_region(spic_dev.cur_ioport->io1.minimum,
4232 spic_dev.cur_ioport->io1.address_length);
4233 if (spic_dev.cur_ioport->io2.minimum)
4234 release_region(spic_dev.cur_ioport->io2.minimum,
4235 spic_dev.cur_ioport->io2.address_length);
4237 sonypi_compat_exit();
4239 sony_laptop_remove_input();
4241 /* pf attrs */
4242 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
4243 sony_pf_remove();
4245 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
4246 list_del(&io->list);
4247 kfree(io);
4249 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
4250 list_del(&irq->list);
4251 kfree(irq);
4253 spic_dev.cur_ioport = NULL;
4254 spic_dev.cur_irq = NULL;
4256 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
4257 return 0;
4260 static int sony_pic_add(struct acpi_device *device)
4262 int result;
4263 struct sony_pic_ioport *io, *tmp_io;
4264 struct sony_pic_irq *irq, *tmp_irq;
4266 pr_info("%s v%s\n", SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
4268 spic_dev.acpi_dev = device;
4269 strcpy(acpi_device_class(device), "sony/hotkey");
4270 sony_pic_detect_device_type(&spic_dev);
4271 mutex_init(&spic_dev.lock);
4273 /* read _PRS resources */
4274 result = sony_pic_possible_resources(device);
4275 if (result) {
4276 pr_err("Unable to read possible resources\n");
4277 goto err_free_resources;
4280 /* setup input devices and helper fifo */
4281 result = sony_laptop_setup_input(device);
4282 if (result) {
4283 pr_err("Unable to create input devices\n");
4284 goto err_free_resources;
4287 result = sonypi_compat_init();
4288 if (result)
4289 goto err_remove_input;
4291 /* request io port */
4292 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
4293 if (request_region(io->io1.minimum, io->io1.address_length,
4294 "Sony Programmable I/O Device")) {
4295 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
4296 io->io1.minimum, io->io1.maximum,
4297 io->io1.address_length);
4298 /* Type 1 have 2 ioports */
4299 if (io->io2.minimum) {
4300 if (request_region(io->io2.minimum,
4301 io->io2.address_length,
4302 "Sony Programmable I/O Device")) {
4303 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
4304 io->io2.minimum, io->io2.maximum,
4305 io->io2.address_length);
4306 spic_dev.cur_ioport = io;
4307 break;
4309 else {
4310 dprintk("Unable to get I/O port2: "
4311 "0x%.4x (0x%.4x) + 0x%.2x\n",
4312 io->io2.minimum, io->io2.maximum,
4313 io->io2.address_length);
4314 release_region(io->io1.minimum,
4315 io->io1.address_length);
4318 else {
4319 spic_dev.cur_ioport = io;
4320 break;
4324 if (!spic_dev.cur_ioport) {
4325 pr_err("Failed to request_region\n");
4326 result = -ENODEV;
4327 goto err_remove_compat;
4330 /* request IRQ */
4331 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
4332 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
4333 0, "sony-laptop", &spic_dev)) {
4334 dprintk("IRQ: %d - triggering: %d - "
4335 "polarity: %d - shr: %d\n",
4336 irq->irq.interrupts[0],
4337 irq->irq.triggering,
4338 irq->irq.polarity,
4339 irq->irq.sharable);
4340 spic_dev.cur_irq = irq;
4341 break;
4344 if (!spic_dev.cur_irq) {
4345 pr_err("Failed to request_irq\n");
4346 result = -ENODEV;
4347 goto err_release_region;
4350 /* set resource status _SRS */
4351 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
4352 if (result) {
4353 pr_err("Couldn't enable device\n");
4354 goto err_free_irq;
4357 spic_dev.bluetooth_power = -1;
4358 /* create device attributes */
4359 result = sony_pf_add();
4360 if (result)
4361 goto err_disable_device;
4363 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
4364 if (result)
4365 goto err_remove_pf;
4367 return 0;
4369 err_remove_pf:
4370 sony_pf_remove();
4372 err_disable_device:
4373 sony_pic_disable(device);
4375 err_free_irq:
4376 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
4378 err_release_region:
4379 release_region(spic_dev.cur_ioport->io1.minimum,
4380 spic_dev.cur_ioport->io1.address_length);
4381 if (spic_dev.cur_ioport->io2.minimum)
4382 release_region(spic_dev.cur_ioport->io2.minimum,
4383 spic_dev.cur_ioport->io2.address_length);
4385 err_remove_compat:
4386 sonypi_compat_exit();
4388 err_remove_input:
4389 sony_laptop_remove_input();
4391 err_free_resources:
4392 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
4393 list_del(&io->list);
4394 kfree(io);
4396 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
4397 list_del(&irq->list);
4398 kfree(irq);
4400 spic_dev.cur_ioport = NULL;
4401 spic_dev.cur_irq = NULL;
4403 return result;
4406 #ifdef CONFIG_PM_SLEEP
4407 static int sony_pic_suspend(struct device *dev)
4409 if (sony_pic_disable(to_acpi_device(dev)))
4410 return -ENXIO;
4411 return 0;
4414 static int sony_pic_resume(struct device *dev)
4416 sony_pic_enable(to_acpi_device(dev),
4417 spic_dev.cur_ioport, spic_dev.cur_irq);
4418 return 0;
4420 #endif
4422 static SIMPLE_DEV_PM_OPS(sony_pic_pm, sony_pic_suspend, sony_pic_resume);
4424 static const struct acpi_device_id sony_pic_device_ids[] = {
4425 {SONY_PIC_HID, 0},
4426 {"", 0},
4429 static struct acpi_driver sony_pic_driver = {
4430 .name = SONY_PIC_DRIVER_NAME,
4431 .class = SONY_PIC_CLASS,
4432 .ids = sony_pic_device_ids,
4433 .owner = THIS_MODULE,
4434 .ops = {
4435 .add = sony_pic_add,
4436 .remove = sony_pic_remove,
4438 .drv.pm = &sony_pic_pm,
4441 static struct dmi_system_id __initdata sonypi_dmi_table[] = {
4443 .ident = "Sony Vaio",
4444 .matches = {
4445 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
4446 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
4450 .ident = "Sony Vaio",
4451 .matches = {
4452 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
4453 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
4459 static int __init sony_laptop_init(void)
4461 int result;
4463 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
4464 result = acpi_bus_register_driver(&sony_pic_driver);
4465 if (result) {
4466 pr_err("Unable to register SPIC driver\n");
4467 goto out;
4469 spic_drv_registered = 1;
4472 result = acpi_bus_register_driver(&sony_nc_driver);
4473 if (result) {
4474 pr_err("Unable to register SNC driver\n");
4475 goto out_unregister_pic;
4478 return 0;
4480 out_unregister_pic:
4481 if (spic_drv_registered)
4482 acpi_bus_unregister_driver(&sony_pic_driver);
4483 out:
4484 return result;
4487 static void __exit sony_laptop_exit(void)
4489 acpi_bus_unregister_driver(&sony_nc_driver);
4490 if (spic_drv_registered)
4491 acpi_bus_unregister_driver(&sony_pic_driver);
4494 module_init(sony_laptop_init);
4495 module_exit(sony_laptop_exit);