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
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 <linux/sonypi.h>
65 #include <linux/sony-laptop.h>
66 #include <linux/rfkill.h>
67 #ifdef CONFIG_SONYPI_COMPAT
68 #include <linux/poll.h>
69 #include <linux/miscdevice.h>
71 #include <asm/uaccess.h>
73 #define dprintk(fmt, ...) \
76 pr_warn(fmt, ##__VA_ARGS__); \
79 #define SONY_LAPTOP_DRIVER_VERSION "0.6"
81 #define SONY_NC_CLASS "sony-nc"
82 #define SONY_NC_HID "SNY5001"
83 #define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
85 #define SONY_PIC_CLASS "sony-pic"
86 #define SONY_PIC_HID "SNY6001"
87 #define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
89 MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
90 MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
91 MODULE_LICENSE("GPL");
92 MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
95 module_param(debug, int, 0);
96 MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
97 "the development of this driver");
99 static int no_spic; /* = 0 */
100 module_param(no_spic, int, 0444);
101 MODULE_PARM_DESC(no_spic,
102 "set this if you don't want to enable the SPIC device");
104 static int compat; /* = 0 */
105 module_param(compat, int, 0444);
106 MODULE_PARM_DESC(compat,
107 "set this if you want to enable backward compatibility mode");
109 static unsigned long mask = 0xffffffff;
110 module_param(mask, ulong, 0644);
111 MODULE_PARM_DESC(mask,
112 "set this to the mask of event you want to enable (see doc)");
114 static int camera; /* = 0 */
115 module_param(camera, int, 0444);
116 MODULE_PARM_DESC(camera,
117 "set this to 1 to enable Motion Eye camera controls "
118 "(only use it if you have a C1VE or C1VN model)");
120 #ifdef CONFIG_SONYPI_COMPAT
121 static int minor = -1;
122 module_param(minor, int, 0);
123 MODULE_PARM_DESC(minor,
124 "minor number of the misc device for the SPIC compatibility code, "
125 "default is -1 (automatic)");
128 static int kbd_backlight = -1;
129 module_param(kbd_backlight, int, 0444);
130 MODULE_PARM_DESC(kbd_backlight,
131 "set this to 0 to disable keyboard backlight, "
132 "1 to enable it (default: no change from current value)");
134 static int kbd_backlight_timeout = -1;
135 module_param(kbd_backlight_timeout, int, 0444);
136 MODULE_PARM_DESC(kbd_backlight_timeout,
137 "meaningful values vary from 0 to 3 and their meaning depends "
138 "on the model (default: no change from current value)");
140 #ifdef CONFIG_PM_SLEEP
141 static void sony_nc_thermal_resume(void);
143 static int sony_nc_kbd_backlight_setup(struct platform_device *pd,
144 unsigned int handle);
145 static void sony_nc_kbd_backlight_cleanup(struct platform_device *pd,
146 unsigned int handle);
148 static int sony_nc_battery_care_setup(struct platform_device *pd,
149 unsigned int handle);
150 static void sony_nc_battery_care_cleanup(struct platform_device *pd);
152 static int sony_nc_thermal_setup(struct platform_device *pd);
153 static void sony_nc_thermal_cleanup(struct platform_device *pd);
155 static int sony_nc_lid_resume_setup(struct platform_device *pd,
156 unsigned int handle);
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_panelid_setup(struct platform_device *pd);
168 static void sony_nc_panelid_cleanup(struct platform_device *pd);
170 static int sony_nc_touchpad_setup(struct platform_device *pd,
171 unsigned int handle);
172 static void sony_nc_touchpad_cleanup(struct platform_device *pd);
174 enum sony_nc_rfkill {
182 static int sony_rfkill_handle;
183 static struct rfkill *sony_rfkill_devices[N_SONY_RFKILL];
184 static int sony_rfkill_address[N_SONY_RFKILL] = {0x300, 0x500, 0x700, 0x900};
185 static int sony_nc_rfkill_setup(struct acpi_device *device,
186 unsigned int handle);
187 static void sony_nc_rfkill_cleanup(void);
188 static void sony_nc_rfkill_update(void);
190 /*********** Input Devices ***********/
192 #define SONY_LAPTOP_BUF_SIZE 128
193 struct sony_laptop_input_s {
195 struct input_dev *jog_dev;
196 struct input_dev *key_dev;
198 spinlock_t fifo_lock;
199 struct timer_list release_key_timer;
202 static struct sony_laptop_input_s sony_laptop_input = {
203 .users = ATOMIC_INIT(0),
206 struct sony_laptop_keypress {
207 struct input_dev *dev;
211 /* Correspondance table between sonypi events
212 * and input layer indexes in the keymap
214 static int sony_laptop_input_index[] = {
216 -1, /* 1 SONYPI_EVENT_JOGDIAL_DOWN */
217 -1, /* 2 SONYPI_EVENT_JOGDIAL_UP */
218 -1, /* 3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
219 -1, /* 4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */
220 -1, /* 5 SONYPI_EVENT_JOGDIAL_PRESSED */
221 -1, /* 6 SONYPI_EVENT_JOGDIAL_RELEASED */
222 0, /* 7 SONYPI_EVENT_CAPTURE_PRESSED */
223 1, /* 8 SONYPI_EVENT_CAPTURE_RELEASED */
224 2, /* 9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
225 3, /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
226 4, /* 11 SONYPI_EVENT_FNKEY_ESC */
227 5, /* 12 SONYPI_EVENT_FNKEY_F1 */
228 6, /* 13 SONYPI_EVENT_FNKEY_F2 */
229 7, /* 14 SONYPI_EVENT_FNKEY_F3 */
230 8, /* 15 SONYPI_EVENT_FNKEY_F4 */
231 9, /* 16 SONYPI_EVENT_FNKEY_F5 */
232 10, /* 17 SONYPI_EVENT_FNKEY_F6 */
233 11, /* 18 SONYPI_EVENT_FNKEY_F7 */
234 12, /* 19 SONYPI_EVENT_FNKEY_F8 */
235 13, /* 20 SONYPI_EVENT_FNKEY_F9 */
236 14, /* 21 SONYPI_EVENT_FNKEY_F10 */
237 15, /* 22 SONYPI_EVENT_FNKEY_F11 */
238 16, /* 23 SONYPI_EVENT_FNKEY_F12 */
239 17, /* 24 SONYPI_EVENT_FNKEY_1 */
240 18, /* 25 SONYPI_EVENT_FNKEY_2 */
241 19, /* 26 SONYPI_EVENT_FNKEY_D */
242 20, /* 27 SONYPI_EVENT_FNKEY_E */
243 21, /* 28 SONYPI_EVENT_FNKEY_F */
244 22, /* 29 SONYPI_EVENT_FNKEY_S */
245 23, /* 30 SONYPI_EVENT_FNKEY_B */
246 24, /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */
247 25, /* 32 SONYPI_EVENT_PKEY_P1 */
248 26, /* 33 SONYPI_EVENT_PKEY_P2 */
249 27, /* 34 SONYPI_EVENT_PKEY_P3 */
250 28, /* 35 SONYPI_EVENT_BACK_PRESSED */
251 -1, /* 36 SONYPI_EVENT_LID_CLOSED */
252 -1, /* 37 SONYPI_EVENT_LID_OPENED */
253 29, /* 38 SONYPI_EVENT_BLUETOOTH_ON */
254 30, /* 39 SONYPI_EVENT_BLUETOOTH_OFF */
255 31, /* 40 SONYPI_EVENT_HELP_PRESSED */
256 32, /* 41 SONYPI_EVENT_FNKEY_ONLY */
257 33, /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
258 34, /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */
259 35, /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
260 36, /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
261 37, /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
262 38, /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */
263 39, /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
264 40, /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
265 41, /* 50 SONYPI_EVENT_ZOOM_PRESSED */
266 42, /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */
267 43, /* 52 SONYPI_EVENT_MEYE_FACE */
268 44, /* 53 SONYPI_EVENT_MEYE_OPPOSITE */
269 45, /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */
270 46, /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */
271 -1, /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */
272 -1, /* 57 SONYPI_EVENT_BATTERY_INSERT */
273 -1, /* 58 SONYPI_EVENT_BATTERY_REMOVE */
274 -1, /* 59 SONYPI_EVENT_FNKEY_RELEASED */
275 47, /* 60 SONYPI_EVENT_WIRELESS_ON */
276 48, /* 61 SONYPI_EVENT_WIRELESS_OFF */
277 49, /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */
278 50, /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */
279 51, /* 64 SONYPI_EVENT_CD_EJECT_PRESSED */
280 52, /* 65 SONYPI_EVENT_MODEKEY_PRESSED */
281 53, /* 66 SONYPI_EVENT_PKEY_P4 */
282 54, /* 67 SONYPI_EVENT_PKEY_P5 */
283 55, /* 68 SONYPI_EVENT_SETTINGKEY_PRESSED */
284 56, /* 69 SONYPI_EVENT_VOLUME_INC_PRESSED */
285 57, /* 70 SONYPI_EVENT_VOLUME_DEC_PRESSED */
286 -1, /* 71 SONYPI_EVENT_BRIGHTNESS_PRESSED */
287 58, /* 72 SONYPI_EVENT_MEDIA_PRESSED */
288 59, /* 72 SONYPI_EVENT_VENDOR_PRESSED */
291 static int sony_laptop_input_keycode_map[] = {
292 KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
293 KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
294 KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
295 KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
296 KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
297 KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
298 KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
299 KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
300 KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
301 KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
302 KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
303 KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
304 KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
305 KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
306 KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
307 KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
308 KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
309 KEY_FN_1, /* 17 SONYPI_EVENT_FNKEY_1 */
310 KEY_FN_2, /* 18 SONYPI_EVENT_FNKEY_2 */
311 KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
312 KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
313 KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
314 KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
315 KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
316 KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
317 KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
318 KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
319 KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
320 KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
321 KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
322 KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
323 KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
324 KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
325 KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
326 KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
327 KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
328 KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
329 KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
330 KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
331 KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
332 KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
333 KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
334 BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
335 KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
336 KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
337 KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
338 KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
339 KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
340 KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
341 KEY_ZOOMIN, /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
342 KEY_ZOOMOUT, /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
343 KEY_EJECTCD, /* 51 SONYPI_EVENT_CD_EJECT_PRESSED */
344 KEY_F13, /* 52 SONYPI_EVENT_MODEKEY_PRESSED */
345 KEY_PROG4, /* 53 SONYPI_EVENT_PKEY_P4 */
346 KEY_F14, /* 54 SONYPI_EVENT_PKEY_P5 */
347 KEY_F15, /* 55 SONYPI_EVENT_SETTINGKEY_PRESSED */
348 KEY_VOLUMEUP, /* 56 SONYPI_EVENT_VOLUME_INC_PRESSED */
349 KEY_VOLUMEDOWN, /* 57 SONYPI_EVENT_VOLUME_DEC_PRESSED */
350 KEY_MEDIA, /* 58 SONYPI_EVENT_MEDIA_PRESSED */
351 KEY_VENDOR, /* 59 SONYPI_EVENT_VENDOR_PRESSED */
354 /* release buttons after a short delay if pressed */
355 static void do_sony_laptop_release_key(unsigned long unused)
357 struct sony_laptop_keypress kp;
360 spin_lock_irqsave(&sony_laptop_input.fifo_lock, flags);
362 if (kfifo_out(&sony_laptop_input.fifo,
363 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
364 input_report_key(kp.dev, kp.key, 0);
368 /* If there is something in the fifo schedule next release. */
369 if (kfifo_len(&sony_laptop_input.fifo) != 0)
370 mod_timer(&sony_laptop_input.release_key_timer,
371 jiffies + msecs_to_jiffies(10));
373 spin_unlock_irqrestore(&sony_laptop_input.fifo_lock, flags);
376 /* forward event to the input subsystem */
377 static void sony_laptop_report_input_event(u8 event)
379 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
380 struct input_dev *key_dev = sony_laptop_input.key_dev;
381 struct sony_laptop_keypress kp = { NULL };
384 if (event == SONYPI_EVENT_FNKEY_RELEASED ||
385 event == SONYPI_EVENT_ANYBUTTON_RELEASED) {
386 /* Nothing, not all VAIOs generate this event */
393 case SONYPI_EVENT_JOGDIAL_UP:
394 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
395 input_report_rel(jog_dev, REL_WHEEL, 1);
399 case SONYPI_EVENT_JOGDIAL_DOWN:
400 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
401 input_report_rel(jog_dev, REL_WHEEL, -1);
406 case SONYPI_EVENT_JOGDIAL_PRESSED:
412 if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
413 dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
416 if ((scancode = sony_laptop_input_index[event]) != -1) {
417 kp.key = sony_laptop_input_keycode_map[scancode];
418 if (kp.key != KEY_UNKNOWN)
425 /* if we have a scancode we emit it so we can always
428 input_event(kp.dev, EV_MSC, MSC_SCAN, scancode);
429 input_report_key(kp.dev, kp.key, 1);
432 /* schedule key release */
433 kfifo_in_locked(&sony_laptop_input.fifo,
434 (unsigned char *)&kp, sizeof(kp),
435 &sony_laptop_input.fifo_lock);
436 mod_timer(&sony_laptop_input.release_key_timer,
437 jiffies + msecs_to_jiffies(10));
439 dprintk("unknown input event %.2x\n", event);
442 static int sony_laptop_setup_input(struct acpi_device *acpi_device)
444 struct input_dev *jog_dev;
445 struct input_dev *key_dev;
449 /* don't run again if already initialized */
450 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
454 spin_lock_init(&sony_laptop_input.fifo_lock);
455 error = kfifo_alloc(&sony_laptop_input.fifo,
456 SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
458 pr_err("kfifo_alloc failed\n");
462 setup_timer(&sony_laptop_input.release_key_timer,
463 do_sony_laptop_release_key, 0);
466 key_dev = input_allocate_device();
472 key_dev->name = "Sony Vaio Keys";
473 key_dev->id.bustype = BUS_ISA;
474 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
475 key_dev->dev.parent = &acpi_device->dev;
477 /* Initialize the Input Drivers: special keys */
478 input_set_capability(key_dev, EV_MSC, MSC_SCAN);
480 __set_bit(EV_KEY, key_dev->evbit);
481 key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
482 key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
483 key_dev->keycode = &sony_laptop_input_keycode_map;
484 for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++)
485 __set_bit(sony_laptop_input_keycode_map[i], key_dev->keybit);
486 __clear_bit(KEY_RESERVED, key_dev->keybit);
488 error = input_register_device(key_dev);
490 goto err_free_keydev;
492 sony_laptop_input.key_dev = key_dev;
495 jog_dev = input_allocate_device();
498 goto err_unregister_keydev;
501 jog_dev->name = "Sony Vaio Jogdial";
502 jog_dev->id.bustype = BUS_ISA;
503 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
504 jog_dev->dev.parent = &acpi_device->dev;
506 input_set_capability(jog_dev, EV_KEY, BTN_MIDDLE);
507 input_set_capability(jog_dev, EV_REL, REL_WHEEL);
509 error = input_register_device(jog_dev);
511 goto err_free_jogdev;
513 sony_laptop_input.jog_dev = jog_dev;
518 input_free_device(jog_dev);
520 err_unregister_keydev:
521 input_unregister_device(key_dev);
522 /* to avoid kref underflow below at input_free_device */
526 input_free_device(key_dev);
529 kfifo_free(&sony_laptop_input.fifo);
532 atomic_dec(&sony_laptop_input.users);
536 static void sony_laptop_remove_input(void)
538 struct sony_laptop_keypress kp = { NULL };
540 /* Cleanup only after the last user has gone */
541 if (!atomic_dec_and_test(&sony_laptop_input.users))
544 del_timer_sync(&sony_laptop_input.release_key_timer);
547 * Generate key-up events for remaining keys. Note that we don't
548 * need locking since nobody is adding new events to the kfifo.
550 while (kfifo_out(&sony_laptop_input.fifo,
551 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
552 input_report_key(kp.dev, kp.key, 0);
556 /* destroy input devs */
557 input_unregister_device(sony_laptop_input.key_dev);
558 sony_laptop_input.key_dev = NULL;
560 if (sony_laptop_input.jog_dev) {
561 input_unregister_device(sony_laptop_input.jog_dev);
562 sony_laptop_input.jog_dev = NULL;
565 kfifo_free(&sony_laptop_input.fifo);
568 /*********** Platform Device ***********/
570 static atomic_t sony_pf_users = ATOMIC_INIT(0);
571 static struct platform_driver sony_pf_driver = {
573 .name = "sony-laptop",
574 .owner = THIS_MODULE,
577 static struct platform_device *sony_pf_device;
579 static int sony_pf_add(void)
583 /* don't run again if already initialized */
584 if (atomic_add_return(1, &sony_pf_users) > 1)
587 ret = platform_driver_register(&sony_pf_driver);
591 sony_pf_device = platform_device_alloc("sony-laptop", -1);
592 if (!sony_pf_device) {
594 goto out_platform_registered;
597 ret = platform_device_add(sony_pf_device);
599 goto out_platform_alloced;
603 out_platform_alloced:
604 platform_device_put(sony_pf_device);
605 sony_pf_device = NULL;
606 out_platform_registered:
607 platform_driver_unregister(&sony_pf_driver);
609 atomic_dec(&sony_pf_users);
613 static void sony_pf_remove(void)
615 /* deregister only after the last user has gone */
616 if (!atomic_dec_and_test(&sony_pf_users))
619 platform_device_unregister(sony_pf_device);
620 platform_driver_unregister(&sony_pf_driver);
623 /*********** SNC (SNY5001) Device ***********/
625 /* the device uses 1-based values, while the backlight subsystem uses
627 #define SONY_MAX_BRIGHTNESS 8
629 #define SNC_VALIDATE_IN 0
630 #define SNC_VALIDATE_OUT 1
632 static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
634 static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
635 const char *, size_t);
636 static int boolean_validate(const int, const int);
637 static int brightness_default_validate(const int, const int);
639 struct sony_nc_value {
640 char *name; /* name of the entry */
641 char **acpiget; /* names of the ACPI get function */
642 char **acpiset; /* names of the ACPI set function */
643 int (*validate)(const int, const int); /* input/output validation */
644 int value; /* current setting */
645 int valid; /* Has ever been set */
646 int debug; /* active only in debug mode ? */
647 struct device_attribute devattr; /* sysfs attribute */
650 #define SNC_HANDLE_NAMES(_name, _values...) \
651 static char *snc_##_name[] = { _values, NULL }
653 #define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
655 .name = __stringify(_name), \
656 .acpiget = _getters, \
657 .acpiset = _setters, \
658 .validate = _validate, \
660 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
663 #define SNC_HANDLE_NULL { .name = NULL }
665 SNC_HANDLE_NAMES(fnkey_get, "GHKE");
667 SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
668 SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
670 SNC_HANDLE_NAMES(cdpower_get, "GCDP");
671 SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
673 SNC_HANDLE_NAMES(audiopower_get, "GAZP");
674 SNC_HANDLE_NAMES(audiopower_set, "AZPW");
676 SNC_HANDLE_NAMES(lanpower_get, "GLNP");
677 SNC_HANDLE_NAMES(lanpower_set, "LNPW");
679 SNC_HANDLE_NAMES(lidstate_get, "GLID");
681 SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
682 SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
684 SNC_HANDLE_NAMES(gainbass_get, "GMGB");
685 SNC_HANDLE_NAMES(gainbass_set, "CMGB");
687 SNC_HANDLE_NAMES(PID_get, "GPID");
689 SNC_HANDLE_NAMES(CTR_get, "GCTR");
690 SNC_HANDLE_NAMES(CTR_set, "SCTR");
692 SNC_HANDLE_NAMES(PCR_get, "GPCR");
693 SNC_HANDLE_NAMES(PCR_set, "SPCR");
695 SNC_HANDLE_NAMES(CMI_get, "GCMI");
696 SNC_HANDLE_NAMES(CMI_set, "SCMI");
698 static struct sony_nc_value sony_nc_values[] = {
699 SNC_HANDLE(brightness_default, snc_brightness_def_get,
700 snc_brightness_def_set, brightness_default_validate, 0),
701 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
702 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
703 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
704 boolean_validate, 0),
705 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
706 boolean_validate, 1),
707 SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
708 boolean_validate, 0),
709 SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
710 boolean_validate, 0),
711 SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
712 boolean_validate, 0),
713 /* unknown methods */
714 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
715 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
716 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
717 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
721 static acpi_handle sony_nc_acpi_handle;
722 static struct acpi_device *sony_nc_acpi_device = NULL;
725 * acpi_evaluate_object wrappers
726 * all useful calls into SNC methods take one or zero parameters and return
727 * integers or arrays.
729 static union acpi_object *__call_snc_method(acpi_handle handle, char *method,
732 union acpi_object *result = NULL;
733 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
737 struct acpi_object_list params;
738 union acpi_object in;
739 in.type = ACPI_TYPE_INTEGER;
740 in.integer.value = *value;
742 params.pointer = ∈
743 status = acpi_evaluate_object(handle, method, ¶ms, &output);
744 dprintk("__call_snc_method: [%s:0x%.8x%.8x]\n", method,
745 (unsigned int)(*value >> 32),
746 (unsigned int)*value & 0xffffffff);
748 status = acpi_evaluate_object(handle, method, NULL, &output);
749 dprintk("__call_snc_method: [%s]\n", method);
752 if (ACPI_FAILURE(status)) {
753 pr_err("Failed to evaluate [%s]\n", method);
757 result = (union acpi_object *) output.pointer;
759 dprintk("No return object [%s]\n", method);
764 static int sony_nc_int_call(acpi_handle handle, char *name, int *value,
767 union acpi_object *object = NULL;
770 object = __call_snc_method(handle, name, &v);
772 object = __call_snc_method(handle, name, NULL);
777 if (object->type != ACPI_TYPE_INTEGER) {
778 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
779 ACPI_TYPE_INTEGER, object->type);
785 *result = object->integer.value;
791 #define MIN(a, b) (a > b ? b : a)
792 static int sony_nc_buffer_call(acpi_handle handle, char *name, u64 *value,
793 void *buffer, size_t buflen)
797 union acpi_object *object = __call_snc_method(handle, name, value);
802 if (object->type == ACPI_TYPE_BUFFER) {
803 len = MIN(buflen, object->buffer.length);
804 memcpy(buffer, object->buffer.pointer, len);
806 } else if (object->type == ACPI_TYPE_INTEGER) {
807 len = MIN(buflen, sizeof(object->integer.value));
808 memcpy(buffer, &object->integer.value, len);
811 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
812 ACPI_TYPE_BUFFER, object->type);
820 struct sony_nc_handles {
822 struct device_attribute devattr;
825 static struct sony_nc_handles *handles;
827 static ssize_t sony_nc_handles_show(struct device *dev,
828 struct device_attribute *attr, char *buffer)
833 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
834 len += snprintf(buffer + len, PAGE_SIZE - len, "0x%.4x ",
837 len += snprintf(buffer + len, PAGE_SIZE - len, "\n");
842 static int sony_nc_handles_setup(struct platform_device *pd)
844 int i, r, result, arg;
846 handles = kzalloc(sizeof(*handles), GFP_KERNEL);
850 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
852 r = sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg,
855 dprintk("caching handle 0x%.4x (offset: 0x%.2x)\n",
857 handles->cap[i] = result;
862 sysfs_attr_init(&handles->devattr.attr);
863 handles->devattr.attr.name = "handles";
864 handles->devattr.attr.mode = S_IRUGO;
865 handles->devattr.show = sony_nc_handles_show;
867 /* allow reading capabilities via sysfs */
868 if (device_create_file(&pd->dev, &handles->devattr)) {
878 static int sony_nc_handles_cleanup(struct platform_device *pd)
882 device_remove_file(&pd->dev, &handles->devattr);
889 static int sony_find_snc_handle(int handle)
893 /* not initialized yet, return early */
894 if (!handles || !handle)
897 for (i = 0; i < 0x10; i++) {
898 if (handles->cap[i] == handle) {
899 dprintk("found handle 0x%.4x (offset: 0x%.2x)\n",
904 dprintk("handle 0x%.4x not found\n", handle);
908 static int sony_call_snc_handle(int handle, int argument, int *result)
911 int offset = sony_find_snc_handle(handle);
916 arg = offset | argument;
917 ret = sony_nc_int_call(sony_nc_acpi_handle, "SN07", &arg, result);
918 dprintk("called SN07 with 0x%.4x (result: 0x%.4x)\n", arg, *result);
923 * sony_nc_values input/output validate functions
926 /* brightness_default_validate:
928 * manipulate input output values to keep consistency with the
929 * backlight framework for which brightness values are 0-based.
931 static int brightness_default_validate(const int direction, const int value)
934 case SNC_VALIDATE_OUT:
936 case SNC_VALIDATE_IN:
937 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
945 * on input validate boolean values 0/1, on output just pass the
948 static int boolean_validate(const int direction, const int value)
950 if (direction == SNC_VALIDATE_IN) {
951 if (value != 0 && value != 1)
958 * Sysfs show/store common to all sony_nc_values
960 static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
964 struct sony_nc_value *item =
965 container_of(attr, struct sony_nc_value, devattr);
970 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiget, NULL,
976 value = item->validate(SNC_VALIDATE_OUT, value);
978 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
981 static ssize_t sony_nc_sysfs_store(struct device *dev,
982 struct device_attribute *attr,
983 const char *buffer, size_t count)
987 struct sony_nc_value *item =
988 container_of(attr, struct sony_nc_value, devattr);
996 if (kstrtoint(buffer, 10, &value))
1000 value = item->validate(SNC_VALIDATE_IN, value);
1005 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
1010 item->value = value;
1019 struct sony_backlight_props {
1020 struct backlight_device *dev;
1026 struct sony_backlight_props sony_bl_props;
1028 static int sony_backlight_update_status(struct backlight_device *bd)
1030 int arg = bd->props.brightness + 1;
1031 return sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &arg, NULL);
1034 static int sony_backlight_get_brightness(struct backlight_device *bd)
1038 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL, &value))
1040 /* brightness levels are 1-based, while backlight ones are 0-based */
1044 static int sony_nc_get_brightness_ng(struct backlight_device *bd)
1047 struct sony_backlight_props *sdev =
1048 (struct sony_backlight_props *)bl_get_data(bd);
1050 sony_call_snc_handle(sdev->handle, sdev->cmd_base + 0x100, &result);
1052 return (result & 0xff) - sdev->offset;
1055 static int sony_nc_update_status_ng(struct backlight_device *bd)
1058 struct sony_backlight_props *sdev =
1059 (struct sony_backlight_props *)bl_get_data(bd);
1061 value = bd->props.brightness + sdev->offset;
1062 if (sony_call_snc_handle(sdev->handle, sdev->cmd_base | (value << 0x10),
1069 static const struct backlight_ops sony_backlight_ops = {
1070 .options = BL_CORE_SUSPENDRESUME,
1071 .update_status = sony_backlight_update_status,
1072 .get_brightness = sony_backlight_get_brightness,
1074 static const struct backlight_ops sony_backlight_ng_ops = {
1075 .options = BL_CORE_SUSPENDRESUME,
1076 .update_status = sony_nc_update_status_ng,
1077 .get_brightness = sony_nc_get_brightness_ng,
1081 * New SNC-only Vaios event mapping to driver known keys
1083 struct sony_nc_event {
1088 static struct sony_nc_event sony_100_events[] = {
1089 { 0x90, SONYPI_EVENT_PKEY_P1 },
1090 { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
1091 { 0x91, SONYPI_EVENT_PKEY_P2 },
1092 { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
1093 { 0x81, SONYPI_EVENT_FNKEY_F1 },
1094 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
1095 { 0x82, SONYPI_EVENT_FNKEY_F2 },
1096 { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
1097 { 0x83, SONYPI_EVENT_FNKEY_F3 },
1098 { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
1099 { 0x84, SONYPI_EVENT_FNKEY_F4 },
1100 { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
1101 { 0x85, SONYPI_EVENT_FNKEY_F5 },
1102 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
1103 { 0x86, SONYPI_EVENT_FNKEY_F6 },
1104 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
1105 { 0x87, SONYPI_EVENT_FNKEY_F7 },
1106 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
1107 { 0x88, SONYPI_EVENT_FNKEY_F8 },
1108 { 0x08, SONYPI_EVENT_FNKEY_RELEASED },
1109 { 0x89, SONYPI_EVENT_FNKEY_F9 },
1110 { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
1111 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
1112 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
1113 { 0x8B, SONYPI_EVENT_FNKEY_F11 },
1114 { 0x0B, SONYPI_EVENT_FNKEY_RELEASED },
1115 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
1116 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
1117 { 0x9d, SONYPI_EVENT_ZOOM_PRESSED },
1118 { 0x1d, SONYPI_EVENT_ANYBUTTON_RELEASED },
1119 { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
1120 { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
1121 { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
1122 { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
1123 { 0xa4, SONYPI_EVENT_CD_EJECT_PRESSED },
1124 { 0x24, SONYPI_EVENT_ANYBUTTON_RELEASED },
1125 { 0xa5, SONYPI_EVENT_VENDOR_PRESSED },
1126 { 0x25, SONYPI_EVENT_ANYBUTTON_RELEASED },
1127 { 0xa6, SONYPI_EVENT_HELP_PRESSED },
1128 { 0x26, SONYPI_EVENT_ANYBUTTON_RELEASED },
1129 { 0xa8, SONYPI_EVENT_FNKEY_1 },
1130 { 0x28, SONYPI_EVENT_ANYBUTTON_RELEASED },
1134 static struct sony_nc_event sony_127_events[] = {
1135 { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
1136 { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
1137 { 0x82, SONYPI_EVENT_PKEY_P1 },
1138 { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
1139 { 0x83, SONYPI_EVENT_PKEY_P2 },
1140 { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
1141 { 0x84, SONYPI_EVENT_PKEY_P3 },
1142 { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
1143 { 0x85, SONYPI_EVENT_PKEY_P4 },
1144 { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
1145 { 0x86, SONYPI_EVENT_PKEY_P5 },
1146 { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
1147 { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
1148 { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
1152 static int sony_nc_hotkeys_decode(u32 event, unsigned int handle)
1155 unsigned int result = 0;
1156 struct sony_nc_event *key_event;
1158 if (sony_call_snc_handle(handle, 0x200, &result)) {
1159 dprintk("Unable to decode event 0x%.2x 0x%.2x\n", handle,
1166 if (handle == 0x0100)
1167 key_event = sony_100_events;
1169 key_event = sony_127_events;
1171 for (; key_event->data; key_event++) {
1172 if (key_event->data == result) {
1173 ret = key_event->event;
1178 if (!key_event->data)
1179 pr_info("Unknown hotkey 0x%.2x/0x%.2x (handle 0x%.2x)\n",
1180 event, result, handle);
1193 static void sony_nc_notify(struct acpi_device *device, u32 event)
1195 u32 real_ev = event;
1197 dprintk("sony_nc_notify, event: 0x%.2x\n", event);
1199 if (event >= 0x90) {
1200 unsigned int result = 0;
1201 unsigned int arg = 0;
1202 unsigned int handle = 0;
1203 unsigned int offset = event - 0x90;
1205 if (offset >= ARRAY_SIZE(handles->cap)) {
1206 pr_err("Event 0x%x outside of capabilities list\n",
1210 handle = handles->cap[offset];
1212 /* list of handles known for generating events */
1218 real_ev = sony_nc_hotkeys_decode(event, handle);
1221 sony_laptop_report_input_event(real_ev);
1223 /* restore the original event for reporting */
1231 /* events on this handle are reported when the
1232 * switch changes position or for battery
1233 * events. We'll notify both of them but only
1234 * update the rfkill device status when the
1237 ev_type = KILLSWITCH;
1238 sony_call_snc_handle(handle, 0x0100, &result);
1239 real_ev = result & 0x03;
1241 /* hw switch event */
1243 sony_nc_rfkill_update();
1249 /* Hybrid GFX switching */
1250 sony_call_snc_handle(handle, 0x0000, &result);
1251 dprintk("GFX switch event received (reason: %s)\n",
1252 (result == 0x1) ? "switch change" :
1253 (result == 0x2) ? "output switch" :
1254 (result == 0x3) ? "output switch" :
1257 ev_type = GFX_SWITCH;
1258 real_ev = __sony_nc_gfx_switch_status_get();
1262 /* Hybrid GFX switching SVS151290S */
1263 ev_type = GFX_SWITCH;
1264 real_ev = __sony_nc_gfx_switch_status_get();
1267 dprintk("Unknown event 0x%x for handle 0x%x\n",
1272 /* clear the event (and the event reason when present) */
1274 sony_nc_int_call(sony_nc_acpi_handle, "SN05", &arg, &result);
1277 /* old style event */
1279 sony_laptop_report_input_event(real_ev);
1281 acpi_bus_generate_netlink_event(sony_nc_acpi_device->pnp.device_class,
1282 dev_name(&sony_nc_acpi_device->dev), ev_type, real_ev);
1285 static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
1286 void *context, void **return_value)
1288 struct acpi_device_info *info;
1290 if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
1291 pr_warn("method: name: %4.4s, args %X\n",
1292 (char *)&info->name, info->param_count);
1303 static void sony_nc_function_setup(struct acpi_device *device,
1304 struct platform_device *pf_device)
1306 unsigned int i, result, bitmask, arg;
1311 /* setup found handles here */
1312 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1313 unsigned int handle = handles->cap[i];
1318 dprintk("setting up handle 0x%.4x\n", handle);
1325 sony_call_snc_handle(handle, 0, &result);
1329 sony_call_snc_handle(handle, 0x100, &result);
1333 /* touchpad enable/disable */
1334 result = sony_nc_touchpad_setup(pf_device, handle);
1336 pr_err("couldn't set up touchpad control function (%d)\n",
1342 result = sony_nc_battery_care_setup(pf_device, handle);
1344 pr_err("couldn't set up battery care function (%d)\n",
1349 result = sony_nc_lid_resume_setup(pf_device, handle);
1351 pr_err("couldn't set up lid resume function (%d)\n",
1355 result = sony_nc_thermal_setup(pf_device);
1357 pr_err("couldn't set up thermal profile function (%d)\n",
1363 result = sony_nc_gfx_switch_setup(pf_device, handle);
1365 pr_err("couldn't set up GFX Switch status (%d)\n",
1369 result = sony_nc_highspeed_charging_setup(pf_device);
1371 pr_err("couldn't set up high speed charging function (%d)\n",
1376 result = sony_nc_rfkill_setup(device, handle);
1378 pr_err("couldn't set up rfkill support (%d)\n",
1386 result = sony_nc_kbd_backlight_setup(pf_device, handle);
1388 pr_err("couldn't set up keyboard backlight function (%d)\n",
1392 result = sony_nc_panelid_setup(pf_device);
1394 pr_err("couldn't set up panel ID function (%d)\n",
1402 /* Enable all events */
1404 if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1405 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1409 static void sony_nc_function_cleanup(struct platform_device *pd)
1411 unsigned int i, result, bitmask, handle;
1413 /* get enabled events and disable them */
1414 sony_nc_int_call(sony_nc_acpi_handle, "SN01", NULL, &bitmask);
1415 sony_nc_int_call(sony_nc_acpi_handle, "SN03", &bitmask, &result);
1417 /* cleanup handles here */
1418 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1420 handle = handles->cap[i];
1428 sony_nc_touchpad_cleanup(pd);
1433 sony_nc_battery_care_cleanup(pd);
1437 sony_nc_lid_resume_cleanup(pd);
1440 sony_nc_thermal_cleanup(pd);
1445 sony_nc_gfx_switch_cleanup(pd);
1448 sony_nc_highspeed_charging_cleanup(pd);
1452 sony_nc_rfkill_cleanup();
1459 sony_nc_kbd_backlight_cleanup(pd, handle);
1462 sony_nc_panelid_cleanup(pd);
1469 /* finally cleanup the handles list */
1470 sony_nc_handles_cleanup(pd);
1473 #ifdef CONFIG_PM_SLEEP
1474 static void sony_nc_function_resume(void)
1476 unsigned int i, result, bitmask, arg;
1478 dprintk("Resuming SNC device\n");
1480 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1481 unsigned int handle = handles->cap[i];
1490 /* re-enable hotkeys */
1491 sony_call_snc_handle(handle, 0, &result);
1494 /* re-enable hotkeys */
1495 sony_call_snc_handle(handle, 0x100, &result);
1498 sony_nc_thermal_resume();
1502 sony_nc_rfkill_update();
1509 /* Enable all events */
1511 if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1512 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1516 static int sony_nc_resume(struct device *dev)
1518 struct sony_nc_value *item;
1520 for (item = sony_nc_values; item->name; item++) {
1525 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
1526 &item->value, NULL);
1528 pr_err("%s: %d\n", __func__, ret);
1533 if (acpi_has_method(sony_nc_acpi_handle, "ECON")) {
1535 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
1536 dprintk("ECON Method failed\n");
1539 if (acpi_has_method(sony_nc_acpi_handle, "SN00"))
1540 sony_nc_function_resume();
1546 static SIMPLE_DEV_PM_OPS(sony_nc_pm, NULL, sony_nc_resume);
1548 static void sony_nc_rfkill_cleanup(void)
1552 for (i = 0; i < N_SONY_RFKILL; i++) {
1553 if (sony_rfkill_devices[i]) {
1554 rfkill_unregister(sony_rfkill_devices[i]);
1555 rfkill_destroy(sony_rfkill_devices[i]);
1560 static int sony_nc_rfkill_set(void *data, bool blocked)
1563 int argument = sony_rfkill_address[(long) data] + 0x100;
1566 argument |= 0x070000;
1568 return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
1571 static const struct rfkill_ops sony_rfkill_ops = {
1572 .set_block = sony_nc_rfkill_set,
1575 static int sony_nc_setup_rfkill(struct acpi_device *device,
1576 enum sony_nc_rfkill nc_type)
1580 enum rfkill_type type;
1583 bool hwblock, swblock;
1587 type = RFKILL_TYPE_WLAN;
1590 case SONY_BLUETOOTH:
1591 type = RFKILL_TYPE_BLUETOOTH;
1592 name = "sony-bluetooth";
1595 type = RFKILL_TYPE_WWAN;
1599 type = RFKILL_TYPE_WIMAX;
1600 name = "sony-wimax";
1606 rfk = rfkill_alloc(name, &device->dev, type,
1607 &sony_rfkill_ops, (void *)nc_type);
1611 if (sony_call_snc_handle(sony_rfkill_handle, 0x200, &result) < 0) {
1612 rfkill_destroy(rfk);
1615 hwblock = !(result & 0x1);
1617 if (sony_call_snc_handle(sony_rfkill_handle,
1618 sony_rfkill_address[nc_type],
1620 rfkill_destroy(rfk);
1623 swblock = !(result & 0x2);
1625 rfkill_init_sw_state(rfk, swblock);
1626 rfkill_set_hw_state(rfk, hwblock);
1628 err = rfkill_register(rfk);
1630 rfkill_destroy(rfk);
1633 sony_rfkill_devices[nc_type] = rfk;
1637 static void sony_nc_rfkill_update(void)
1639 enum sony_nc_rfkill i;
1643 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
1644 hwblock = !(result & 0x1);
1646 for (i = 0; i < N_SONY_RFKILL; i++) {
1647 int argument = sony_rfkill_address[i];
1649 if (!sony_rfkill_devices[i])
1653 if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
1654 /* we already know we're blocked */
1659 sony_call_snc_handle(sony_rfkill_handle, argument, &result);
1660 rfkill_set_states(sony_rfkill_devices[i],
1661 !(result & 0x2), false);
1665 static int sony_nc_rfkill_setup(struct acpi_device *device,
1666 unsigned int handle)
1670 unsigned char buffer[32] = { 0 };
1672 offset = sony_find_snc_handle(handle);
1673 sony_rfkill_handle = handle;
1675 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
1680 /* The buffer is filled with magic numbers describing the devices
1681 * available, 0xff terminates the enumeration.
1685 * 0x20 WWAN GPRS-EDGE
1689 * 0x25 Gobi WWAN no GPS
1690 * 0x26 Gobi WWAN + GPS
1691 * 0x28 Gobi WWAN no GPS
1692 * 0x29 Gobi WWAN + GPS
1694 * 0x50 Gobi WWAN no GPS
1695 * 0x51 Gobi WWAN + GPS
1696 * 0x70 no SIM card slot
1697 * 0x71 SIM card slot
1699 for (i = 0; i < ARRAY_SIZE(buffer); i++) {
1701 if (buffer[i] == 0xff)
1704 dprintk("Radio devices, found 0x%.2x\n", buffer[i]);
1706 if (buffer[i] == 0 && !sony_rfkill_devices[SONY_WIFI])
1707 sony_nc_setup_rfkill(device, SONY_WIFI);
1709 if (buffer[i] == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
1710 sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
1712 if (((0xf0 & buffer[i]) == 0x20 ||
1713 (0xf0 & buffer[i]) == 0x50) &&
1714 !sony_rfkill_devices[SONY_WWAN])
1715 sony_nc_setup_rfkill(device, SONY_WWAN);
1717 if (buffer[i] == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
1718 sony_nc_setup_rfkill(device, SONY_WIMAX);
1723 /* Keyboard backlight feature */
1724 struct kbd_backlight {
1725 unsigned int handle;
1728 unsigned int timeout;
1729 struct device_attribute mode_attr;
1730 struct device_attribute timeout_attr;
1733 static struct kbd_backlight *kbdbl_ctl;
1735 static ssize_t __sony_nc_kbd_backlight_mode_set(u8 value)
1742 if (sony_call_snc_handle(kbdbl_ctl->handle,
1743 (value << 0x10) | (kbdbl_ctl->base), &result))
1746 /* Try to turn the light on/off immediately */
1747 sony_call_snc_handle(kbdbl_ctl->handle,
1748 (value << 0x10) | (kbdbl_ctl->base + 0x100), &result);
1750 kbdbl_ctl->mode = value;
1755 static ssize_t sony_nc_kbd_backlight_mode_store(struct device *dev,
1756 struct device_attribute *attr,
1757 const char *buffer, size_t count)
1760 unsigned long value;
1765 if (kstrtoul(buffer, 10, &value))
1768 ret = __sony_nc_kbd_backlight_mode_set(value);
1775 static ssize_t sony_nc_kbd_backlight_mode_show(struct device *dev,
1776 struct device_attribute *attr, char *buffer)
1779 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_ctl->mode);
1783 static int __sony_nc_kbd_backlight_timeout_set(u8 value)
1790 if (sony_call_snc_handle(kbdbl_ctl->handle, (value << 0x10) |
1791 (kbdbl_ctl->base + 0x200), &result))
1794 kbdbl_ctl->timeout = value;
1799 static ssize_t sony_nc_kbd_backlight_timeout_store(struct device *dev,
1800 struct device_attribute *attr,
1801 const char *buffer, size_t count)
1804 unsigned long value;
1809 if (kstrtoul(buffer, 10, &value))
1812 ret = __sony_nc_kbd_backlight_timeout_set(value);
1819 static ssize_t sony_nc_kbd_backlight_timeout_show(struct device *dev,
1820 struct device_attribute *attr, char *buffer)
1823 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_ctl->timeout);
1827 static int sony_nc_kbd_backlight_setup(struct platform_device *pd,
1828 unsigned int handle)
1834 pr_warn("handle 0x%.4x: keyboard backlight setup already done for 0x%.4x\n",
1835 handle, kbdbl_ctl->handle);
1839 /* verify the kbd backlight presence, these handles are not used for
1840 * keyboard backlight only
1842 ret = sony_call_snc_handle(handle, handle == 0x0137 ? 0x0B00 : 0x0100,
1847 if ((handle == 0x0137 && !(result & 0x02)) ||
1849 dprintk("no backlight keyboard found\n");
1853 kbdbl_ctl = kzalloc(sizeof(*kbdbl_ctl), GFP_KERNEL);
1857 kbdbl_ctl->mode = kbd_backlight;
1858 kbdbl_ctl->timeout = kbd_backlight_timeout;
1859 kbdbl_ctl->handle = handle;
1860 if (handle == 0x0137)
1861 kbdbl_ctl->base = 0x0C00;
1863 kbdbl_ctl->base = 0x4000;
1865 sysfs_attr_init(&kbdbl_ctl->mode_attr.attr);
1866 kbdbl_ctl->mode_attr.attr.name = "kbd_backlight";
1867 kbdbl_ctl->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
1868 kbdbl_ctl->mode_attr.show = sony_nc_kbd_backlight_mode_show;
1869 kbdbl_ctl->mode_attr.store = sony_nc_kbd_backlight_mode_store;
1871 sysfs_attr_init(&kbdbl_ctl->timeout_attr.attr);
1872 kbdbl_ctl->timeout_attr.attr.name = "kbd_backlight_timeout";
1873 kbdbl_ctl->timeout_attr.attr.mode = S_IRUGO | S_IWUSR;
1874 kbdbl_ctl->timeout_attr.show = sony_nc_kbd_backlight_timeout_show;
1875 kbdbl_ctl->timeout_attr.store = sony_nc_kbd_backlight_timeout_store;
1877 ret = device_create_file(&pd->dev, &kbdbl_ctl->mode_attr);
1881 ret = device_create_file(&pd->dev, &kbdbl_ctl->timeout_attr);
1885 __sony_nc_kbd_backlight_mode_set(kbdbl_ctl->mode);
1886 __sony_nc_kbd_backlight_timeout_set(kbdbl_ctl->timeout);
1891 device_remove_file(&pd->dev, &kbdbl_ctl->mode_attr);
1898 static void sony_nc_kbd_backlight_cleanup(struct platform_device *pd,
1899 unsigned int handle)
1901 if (kbdbl_ctl && handle == kbdbl_ctl->handle) {
1902 device_remove_file(&pd->dev, &kbdbl_ctl->mode_attr);
1903 device_remove_file(&pd->dev, &kbdbl_ctl->timeout_attr);
1909 struct battery_care_control {
1910 struct device_attribute attrs[2];
1911 unsigned int handle;
1913 static struct battery_care_control *bcare_ctl;
1915 static ssize_t sony_nc_battery_care_limit_store(struct device *dev,
1916 struct device_attribute *attr,
1917 const char *buffer, size_t count)
1919 unsigned int result, cmd;
1920 unsigned long value;
1925 if (kstrtoul(buffer, 10, &value))
1928 /* limit values (2 bits):
1934 * bit 0: 0 disable BCL, 1 enable BCL
1935 * bit 1: 1 tell to store the battery limit (see bits 6,7) too
1936 * bits 2,3: reserved
1937 * bits 4,5: store the limit into the EC
1938 * bits 6,7: store the limit into the battery
1946 else if (value <= 80)
1949 else if (value <= 100)
1956 * handle 0x0115 should allow storing on battery too;
1957 * handle 0x0136 same as 0x0115 + health status;
1958 * handle 0x013f, same as 0x0136 but no storing on the battery
1960 if (bcare_ctl->handle != 0x013f)
1961 cmd = cmd | (cmd << 2);
1963 cmd = (cmd | 0x1) << 0x10;
1966 if (sony_call_snc_handle(bcare_ctl->handle, cmd | 0x0100, &result))
1972 static ssize_t sony_nc_battery_care_limit_show(struct device *dev,
1973 struct device_attribute *attr, char *buffer)
1975 unsigned int result, status;
1977 if (sony_call_snc_handle(bcare_ctl->handle, 0x0000, &result))
1980 status = (result & 0x01) ? ((result & 0x30) >> 0x04) : 0;
1996 return snprintf(buffer, PAGE_SIZE, "%d\n", status);
1999 static ssize_t sony_nc_battery_care_health_show(struct device *dev,
2000 struct device_attribute *attr, char *buffer)
2003 unsigned int health;
2005 if (sony_call_snc_handle(bcare_ctl->handle, 0x0200, &health))
2008 count = snprintf(buffer, PAGE_SIZE, "%d\n", health & 0xff);
2013 static int sony_nc_battery_care_setup(struct platform_device *pd,
2014 unsigned int handle)
2018 bcare_ctl = kzalloc(sizeof(struct battery_care_control), GFP_KERNEL);
2022 bcare_ctl->handle = handle;
2024 sysfs_attr_init(&bcare_ctl->attrs[0].attr);
2025 bcare_ctl->attrs[0].attr.name = "battery_care_limiter";
2026 bcare_ctl->attrs[0].attr.mode = S_IRUGO | S_IWUSR;
2027 bcare_ctl->attrs[0].show = sony_nc_battery_care_limit_show;
2028 bcare_ctl->attrs[0].store = sony_nc_battery_care_limit_store;
2030 ret = device_create_file(&pd->dev, &bcare_ctl->attrs[0]);
2034 /* 0x0115 is for models with no health reporting capability */
2035 if (handle == 0x0115)
2038 sysfs_attr_init(&bcare_ctl->attrs[1].attr);
2039 bcare_ctl->attrs[1].attr.name = "battery_care_health";
2040 bcare_ctl->attrs[1].attr.mode = S_IRUGO;
2041 bcare_ctl->attrs[1].show = sony_nc_battery_care_health_show;
2043 ret = device_create_file(&pd->dev, &bcare_ctl->attrs[1]);
2050 device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
2059 static void sony_nc_battery_care_cleanup(struct platform_device *pd)
2062 device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
2063 if (bcare_ctl->handle != 0x0115)
2064 device_remove_file(&pd->dev, &bcare_ctl->attrs[1]);
2071 struct snc_thermal_ctrl {
2073 unsigned int profiles;
2074 struct device_attribute mode_attr;
2075 struct device_attribute profiles_attr;
2077 static struct snc_thermal_ctrl *th_handle;
2079 #define THM_PROFILE_MAX 3
2080 static const char * const snc_thermal_profiles[] = {
2086 static int sony_nc_thermal_mode_set(unsigned short mode)
2088 unsigned int result;
2090 /* the thermal profile seems to be a two bit bitmask:
2092 * msb -> performance
2093 * no bit set is the normal operation and is always valid
2094 * Some vaio models only have "balanced" and "performance"
2096 if ((mode && !(th_handle->profiles & mode)) || mode >= THM_PROFILE_MAX)
2099 if (sony_call_snc_handle(0x0122, mode << 0x10 | 0x0200, &result))
2102 th_handle->mode = mode;
2107 static int sony_nc_thermal_mode_get(void)
2109 unsigned int result;
2111 if (sony_call_snc_handle(0x0122, 0x0100, &result))
2114 return result & 0xff;
2117 static ssize_t sony_nc_thermal_profiles_show(struct device *dev,
2118 struct device_attribute *attr, char *buffer)
2123 for (cnt = 0; cnt < THM_PROFILE_MAX; cnt++) {
2124 if (!cnt || (th_handle->profiles & cnt))
2125 idx += snprintf(buffer + idx, PAGE_SIZE - idx, "%s ",
2126 snc_thermal_profiles[cnt]);
2128 idx += snprintf(buffer + idx, PAGE_SIZE - idx, "\n");
2133 static ssize_t sony_nc_thermal_mode_store(struct device *dev,
2134 struct device_attribute *attr,
2135 const char *buffer, size_t count)
2143 /* skip the newline if present */
2144 if (buffer[len - 1] == '\n')
2147 for (cmd = 0; cmd < THM_PROFILE_MAX; cmd++)
2148 if (strncmp(buffer, snc_thermal_profiles[cmd], len) == 0)
2151 if (sony_nc_thermal_mode_set(cmd))
2157 static ssize_t sony_nc_thermal_mode_show(struct device *dev,
2158 struct device_attribute *attr, char *buffer)
2161 int mode = sony_nc_thermal_mode_get();
2166 count = snprintf(buffer, PAGE_SIZE, "%s\n", snc_thermal_profiles[mode]);
2171 static int sony_nc_thermal_setup(struct platform_device *pd)
2174 th_handle = kzalloc(sizeof(struct snc_thermal_ctrl), GFP_KERNEL);
2178 ret = sony_call_snc_handle(0x0122, 0x0000, &th_handle->profiles);
2180 pr_warn("couldn't to read the thermal profiles\n");
2184 ret = sony_nc_thermal_mode_get();
2186 pr_warn("couldn't to read the current thermal profile");
2189 th_handle->mode = ret;
2191 sysfs_attr_init(&th_handle->profiles_attr.attr);
2192 th_handle->profiles_attr.attr.name = "thermal_profiles";
2193 th_handle->profiles_attr.attr.mode = S_IRUGO;
2194 th_handle->profiles_attr.show = sony_nc_thermal_profiles_show;
2196 sysfs_attr_init(&th_handle->mode_attr.attr);
2197 th_handle->mode_attr.attr.name = "thermal_control";
2198 th_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
2199 th_handle->mode_attr.show = sony_nc_thermal_mode_show;
2200 th_handle->mode_attr.store = sony_nc_thermal_mode_store;
2202 ret = device_create_file(&pd->dev, &th_handle->profiles_attr);
2206 ret = device_create_file(&pd->dev, &th_handle->mode_attr);
2213 device_remove_file(&pd->dev, &th_handle->profiles_attr);
2220 static void sony_nc_thermal_cleanup(struct platform_device *pd)
2223 device_remove_file(&pd->dev, &th_handle->profiles_attr);
2224 device_remove_file(&pd->dev, &th_handle->mode_attr);
2230 #ifdef CONFIG_PM_SLEEP
2231 static void sony_nc_thermal_resume(void)
2233 unsigned int status = sony_nc_thermal_mode_get();
2235 if (status != th_handle->mode)
2236 sony_nc_thermal_mode_set(th_handle->mode);
2240 /* resume on LID open */
2241 #define LID_RESUME_S5 0
2242 #define LID_RESUME_S4 1
2243 #define LID_RESUME_S3 2
2244 #define LID_RESUME_MAX 3
2245 struct snc_lid_resume_control {
2246 struct device_attribute attrs[LID_RESUME_MAX];
2247 unsigned int status;
2250 static struct snc_lid_resume_control *lid_ctl;
2252 static ssize_t sony_nc_lid_resume_store(struct device *dev,
2253 struct device_attribute *attr,
2254 const char *buffer, size_t count)
2256 unsigned int result;
2257 unsigned long value;
2258 unsigned int pos = LID_RESUME_S5;
2262 if (kstrtoul(buffer, 10, &value) || value > 1)
2265 /* the value we have to write to SNC is a bitmask:
2271 while (pos < LID_RESUME_MAX) {
2272 if (&lid_ctl->attrs[pos].attr == &attr->attr)
2276 if (pos == LID_RESUME_MAX)
2280 value = lid_ctl->status | (1 << pos);
2282 value = lid_ctl->status & ~(1 << pos);
2284 if (sony_call_snc_handle(lid_ctl->handle, value << 0x10 | 0x0100,
2288 lid_ctl->status = value;
2293 static ssize_t sony_nc_lid_resume_show(struct device *dev,
2294 struct device_attribute *attr,
2297 unsigned int pos = LID_RESUME_S5;
2299 while (pos < LID_RESUME_MAX) {
2300 if (&lid_ctl->attrs[pos].attr == &attr->attr)
2301 return snprintf(buffer, PAGE_SIZE, "%d\n",
2302 (lid_ctl->status >> pos) & 0x01);
2308 static int sony_nc_lid_resume_setup(struct platform_device *pd,
2309 unsigned int handle)
2311 unsigned int result;
2314 if (sony_call_snc_handle(handle, 0x0000, &result))
2317 lid_ctl = kzalloc(sizeof(struct snc_lid_resume_control), GFP_KERNEL);
2321 lid_ctl->status = result & 0x7;
2322 lid_ctl->handle = handle;
2324 sysfs_attr_init(&lid_ctl->attrs[0].attr);
2325 lid_ctl->attrs[LID_RESUME_S5].attr.name = "lid_resume_S5";
2326 lid_ctl->attrs[LID_RESUME_S5].attr.mode = S_IRUGO | S_IWUSR;
2327 lid_ctl->attrs[LID_RESUME_S5].show = sony_nc_lid_resume_show;
2328 lid_ctl->attrs[LID_RESUME_S5].store = sony_nc_lid_resume_store;
2330 if (handle == 0x0119) {
2331 sysfs_attr_init(&lid_ctl->attrs[1].attr);
2332 lid_ctl->attrs[LID_RESUME_S4].attr.name = "lid_resume_S4";
2333 lid_ctl->attrs[LID_RESUME_S4].attr.mode = S_IRUGO | S_IWUSR;
2334 lid_ctl->attrs[LID_RESUME_S4].show = sony_nc_lid_resume_show;
2335 lid_ctl->attrs[LID_RESUME_S4].store = sony_nc_lid_resume_store;
2337 sysfs_attr_init(&lid_ctl->attrs[2].attr);
2338 lid_ctl->attrs[LID_RESUME_S3].attr.name = "lid_resume_S3";
2339 lid_ctl->attrs[LID_RESUME_S3].attr.mode = S_IRUGO | S_IWUSR;
2340 lid_ctl->attrs[LID_RESUME_S3].show = sony_nc_lid_resume_show;
2341 lid_ctl->attrs[LID_RESUME_S3].store = sony_nc_lid_resume_store;
2343 for (i = 0; i < LID_RESUME_MAX &&
2344 lid_ctl->attrs[LID_RESUME_S3].attr.name; i++) {
2345 result = device_create_file(&pd->dev, &lid_ctl->attrs[i]);
2353 for (i--; i >= 0; i--)
2354 device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
2362 static void sony_nc_lid_resume_cleanup(struct platform_device *pd)
2367 for (i = 0; i < LID_RESUME_MAX; i++) {
2368 if (!lid_ctl->attrs[i].attr.name)
2371 device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
2379 /* GFX Switch position */
2385 struct snc_gfx_switch_control {
2386 struct device_attribute attr;
2387 unsigned int handle;
2389 static struct snc_gfx_switch_control *gfxs_ctl;
2391 /* returns 0 for speed, 1 for stamina */
2392 static int __sony_nc_gfx_switch_status_get(void)
2394 unsigned int result;
2396 if (sony_call_snc_handle(gfxs_ctl->handle,
2397 gfxs_ctl->handle == 0x015B ? 0x0000 : 0x0100,
2401 switch (gfxs_ctl->handle) {
2403 /* 1: discrete GFX (speed)
2404 * 0: integrated GFX (stamina)
2406 return result & 0x1 ? SPEED : STAMINA;
2409 /* 0: discrete GFX (speed)
2410 * 1: integrated GFX (stamina)
2412 return result & 0x1 ? STAMINA : SPEED;
2415 /* it's a more elaborated bitmask, for now:
2416 * 2: integrated GFX (stamina)
2417 * 0: discrete GFX (speed)
2419 dprintk("GFX Status: 0x%x\n", result);
2420 return result & 0x80 ? AUTO :
2421 result & 0x02 ? STAMINA : SPEED;
2427 static ssize_t sony_nc_gfx_switch_status_show(struct device *dev,
2428 struct device_attribute *attr,
2431 int pos = __sony_nc_gfx_switch_status_get();
2436 return snprintf(buffer, PAGE_SIZE, "%s\n",
2437 pos == SPEED ? "speed" :
2438 pos == STAMINA ? "stamina" :
2439 pos == AUTO ? "auto" : "unknown");
2442 static int sony_nc_gfx_switch_setup(struct platform_device *pd,
2443 unsigned int handle)
2445 unsigned int result;
2447 gfxs_ctl = kzalloc(sizeof(struct snc_gfx_switch_control), GFP_KERNEL);
2451 gfxs_ctl->handle = handle;
2453 sysfs_attr_init(&gfxs_ctl->attr.attr);
2454 gfxs_ctl->attr.attr.name = "gfx_switch_status";
2455 gfxs_ctl->attr.attr.mode = S_IRUGO;
2456 gfxs_ctl->attr.show = sony_nc_gfx_switch_status_show;
2458 result = device_create_file(&pd->dev, &gfxs_ctl->attr);
2471 static void sony_nc_gfx_switch_cleanup(struct platform_device *pd)
2474 device_remove_file(&pd->dev, &gfxs_ctl->attr);
2481 /* High speed charging function */
2482 static struct device_attribute *hsc_handle;
2484 static ssize_t sony_nc_highspeed_charging_store(struct device *dev,
2485 struct device_attribute *attr,
2486 const char *buffer, size_t count)
2488 unsigned int result;
2489 unsigned long value;
2494 if (kstrtoul(buffer, 10, &value) || value > 1)
2497 if (sony_call_snc_handle(0x0131, value << 0x10 | 0x0200, &result))
2503 static ssize_t sony_nc_highspeed_charging_show(struct device *dev,
2504 struct device_attribute *attr, char *buffer)
2506 unsigned int result;
2508 if (sony_call_snc_handle(0x0131, 0x0100, &result))
2511 return snprintf(buffer, PAGE_SIZE, "%d\n", result & 0x01);
2514 static int sony_nc_highspeed_charging_setup(struct platform_device *pd)
2516 unsigned int result;
2518 if (sony_call_snc_handle(0x0131, 0x0000, &result) || !(result & 0x01)) {
2519 /* some models advertise the handle but have no implementation
2522 pr_info("No High Speed Charging capability found\n");
2526 hsc_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
2530 sysfs_attr_init(&hsc_handle->attr);
2531 hsc_handle->attr.name = "battery_highspeed_charging";
2532 hsc_handle->attr.mode = S_IRUGO | S_IWUSR;
2533 hsc_handle->show = sony_nc_highspeed_charging_show;
2534 hsc_handle->store = sony_nc_highspeed_charging_store;
2536 result = device_create_file(&pd->dev, hsc_handle);
2546 static void sony_nc_highspeed_charging_cleanup(struct platform_device *pd)
2549 device_remove_file(&pd->dev, hsc_handle);
2555 /* Panel ID function */
2556 static struct device_attribute *panel_handle;
2558 static ssize_t sony_nc_panelid_show(struct device *dev,
2559 struct device_attribute *attr, char *buffer)
2561 unsigned int result;
2563 if (sony_call_snc_handle(0x011D, 0x0000, &result))
2566 return snprintf(buffer, PAGE_SIZE, "%d\n", result);
2569 static int sony_nc_panelid_setup(struct platform_device *pd)
2571 unsigned int result;
2573 panel_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
2577 sysfs_attr_init(&panel_handle->attr);
2578 panel_handle->attr.name = "panel_id";
2579 panel_handle->attr.mode = S_IRUGO;
2580 panel_handle->show = sony_nc_panelid_show;
2581 panel_handle->store = NULL;
2583 result = device_create_file(&pd->dev, panel_handle);
2585 kfree(panel_handle);
2586 panel_handle = NULL;
2593 static void sony_nc_panelid_cleanup(struct platform_device *pd)
2596 device_remove_file(&pd->dev, panel_handle);
2597 kfree(panel_handle);
2598 panel_handle = NULL;
2602 /* Touchpad enable/disable */
2603 struct touchpad_control {
2604 struct device_attribute attr;
2607 static struct touchpad_control *tp_ctl;
2609 static ssize_t sony_nc_touchpad_store(struct device *dev,
2610 struct device_attribute *attr, const char *buffer, size_t count)
2612 unsigned int result;
2613 unsigned long value;
2618 if (kstrtoul(buffer, 10, &value) || value > 1)
2621 /* sysfs: 0 disabled, 1 enabled
2622 * EC: 0 enabled, 1 disabled
2624 if (sony_call_snc_handle(tp_ctl->handle,
2625 (!value << 0x10) | 0x100, &result))
2631 static ssize_t sony_nc_touchpad_show(struct device *dev,
2632 struct device_attribute *attr, char *buffer)
2634 unsigned int result;
2636 if (sony_call_snc_handle(tp_ctl->handle, 0x000, &result))
2639 return snprintf(buffer, PAGE_SIZE, "%d\n", !(result & 0x01));
2642 static int sony_nc_touchpad_setup(struct platform_device *pd,
2643 unsigned int handle)
2647 tp_ctl = kzalloc(sizeof(struct touchpad_control), GFP_KERNEL);
2651 tp_ctl->handle = handle;
2653 sysfs_attr_init(&tp_ctl->attr.attr);
2654 tp_ctl->attr.attr.name = "touchpad";
2655 tp_ctl->attr.attr.mode = S_IRUGO | S_IWUSR;
2656 tp_ctl->attr.show = sony_nc_touchpad_show;
2657 tp_ctl->attr.store = sony_nc_touchpad_store;
2659 ret = device_create_file(&pd->dev, &tp_ctl->attr);
2668 static void sony_nc_touchpad_cleanup(struct platform_device *pd)
2671 device_remove_file(&pd->dev, &tp_ctl->attr);
2677 static void sony_nc_backlight_ng_read_limits(int handle,
2678 struct sony_backlight_props *props)
2682 int lvl_table_len = 0;
2683 u8 min = 0xff, max = 0x00;
2684 unsigned char buffer[32] = { 0 };
2686 props->handle = handle;
2688 props->maxlvl = 0xff;
2690 offset = sony_find_snc_handle(handle);
2692 /* try to read the boundaries from ACPI tables, if we fail the above
2693 * defaults should be reasonable
2695 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
2712 /* the buffer lists brightness levels available, brightness levels are
2713 * from position 0 to 8 in the array, other values are used by ALS
2716 for (i = 0; i < lvl_table_len && i < ARRAY_SIZE(buffer); i++) {
2718 dprintk("Brightness level: %d\n", buffer[i]);
2723 if (buffer[i] > max)
2725 if (buffer[i] < min)
2728 props->offset = min;
2729 props->maxlvl = max;
2730 dprintk("Brightness levels: min=%d max=%d\n", props->offset,
2734 static void sony_nc_backlight_setup(void)
2736 int max_brightness = 0;
2737 const struct backlight_ops *ops = NULL;
2738 struct backlight_properties props;
2740 if (sony_find_snc_handle(0x12f) >= 0) {
2741 ops = &sony_backlight_ng_ops;
2742 sony_bl_props.cmd_base = 0x0100;
2743 sony_nc_backlight_ng_read_limits(0x12f, &sony_bl_props);
2744 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
2746 } else if (sony_find_snc_handle(0x137) >= 0) {
2747 ops = &sony_backlight_ng_ops;
2748 sony_bl_props.cmd_base = 0x0100;
2749 sony_nc_backlight_ng_read_limits(0x137, &sony_bl_props);
2750 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
2752 } else if (sony_find_snc_handle(0x143) >= 0) {
2753 ops = &sony_backlight_ng_ops;
2754 sony_bl_props.cmd_base = 0x3000;
2755 sony_nc_backlight_ng_read_limits(0x143, &sony_bl_props);
2756 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
2758 } else if (sony_find_snc_handle(0x14b) >= 0) {
2759 ops = &sony_backlight_ng_ops;
2760 sony_bl_props.cmd_base = 0x3000;
2761 sony_nc_backlight_ng_read_limits(0x14b, &sony_bl_props);
2762 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
2764 } else if (sony_find_snc_handle(0x14c) >= 0) {
2765 ops = &sony_backlight_ng_ops;
2766 sony_bl_props.cmd_base = 0x3000;
2767 sony_nc_backlight_ng_read_limits(0x14c, &sony_bl_props);
2768 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
2770 } else if (acpi_has_method(sony_nc_acpi_handle, "GBRT")) {
2771 ops = &sony_backlight_ops;
2772 max_brightness = SONY_MAX_BRIGHTNESS - 1;
2777 memset(&props, 0, sizeof(struct backlight_properties));
2778 props.type = BACKLIGHT_PLATFORM;
2779 props.max_brightness = max_brightness;
2780 sony_bl_props.dev = backlight_device_register("sony", NULL,
2784 if (IS_ERR(sony_bl_props.dev)) {
2785 pr_warn("unable to register backlight device\n");
2786 sony_bl_props.dev = NULL;
2788 sony_bl_props.dev->props.brightness =
2789 ops->get_brightness(sony_bl_props.dev);
2792 static void sony_nc_backlight_cleanup(void)
2794 if (sony_bl_props.dev)
2795 backlight_device_unregister(sony_bl_props.dev);
2798 static int sony_nc_add(struct acpi_device *device)
2802 struct sony_nc_value *item;
2804 pr_info("%s v%s\n", SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
2806 sony_nc_acpi_device = device;
2807 strcpy(acpi_device_class(device), "sony/hotkey");
2809 sony_nc_acpi_handle = device->handle;
2811 /* read device status */
2812 result = acpi_bus_get_status(device);
2813 /* bail IFF the above call was successful and the device is not present */
2814 if (!result && !device->status.present) {
2815 dprintk("Device not present\n");
2820 result = sony_pf_add();
2825 status = acpi_walk_namespace(ACPI_TYPE_METHOD,
2826 sony_nc_acpi_handle, 1, sony_walk_callback,
2828 if (ACPI_FAILURE(status)) {
2829 pr_warn("unable to walk acpi resources\n");
2835 result = sony_laptop_setup_input(device);
2837 pr_err("Unable to create input devices\n");
2841 if (acpi_has_method(sony_nc_acpi_handle, "ECON")) {
2843 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
2844 dprintk("ECON Method failed\n");
2847 if (acpi_has_method(sony_nc_acpi_handle, "SN00")) {
2848 dprintk("Doing SNC setup\n");
2849 /* retrieve the available handles */
2850 result = sony_nc_handles_setup(sony_pf_device);
2852 sony_nc_function_setup(device, sony_pf_device);
2855 /* setup input devices and helper fifo */
2856 if (acpi_video_backlight_support()) {
2857 pr_info("brightness ignored, must be controlled by ACPI video driver\n");
2859 sony_nc_backlight_setup();
2862 /* create sony_pf sysfs attributes related to the SNC device */
2863 for (item = sony_nc_values; item->name; ++item) {
2865 if (!debug && item->debug)
2868 /* find the available acpiget as described in the DSDT */
2869 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
2870 if (acpi_has_method(sony_nc_acpi_handle,
2872 dprintk("Found %s getter: %s\n",
2873 item->name, *item->acpiget);
2874 item->devattr.attr.mode |= S_IRUGO;
2879 /* find the available acpiset as described in the DSDT */
2880 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
2881 if (acpi_has_method(sony_nc_acpi_handle,
2883 dprintk("Found %s setter: %s\n",
2884 item->name, *item->acpiset);
2885 item->devattr.attr.mode |= S_IWUSR;
2890 if (item->devattr.attr.mode != 0) {
2892 device_create_file(&sony_pf_device->dev,
2902 for (item = sony_nc_values; item->name; ++item) {
2903 device_remove_file(&sony_pf_device->dev, &item->devattr);
2905 sony_nc_backlight_cleanup();
2906 sony_nc_function_cleanup(sony_pf_device);
2907 sony_nc_handles_cleanup(sony_pf_device);
2910 sony_laptop_remove_input();
2916 sony_nc_rfkill_cleanup();
2920 static int sony_nc_remove(struct acpi_device *device)
2922 struct sony_nc_value *item;
2924 sony_nc_backlight_cleanup();
2926 sony_nc_acpi_device = NULL;
2928 for (item = sony_nc_values; item->name; ++item) {
2929 device_remove_file(&sony_pf_device->dev, &item->devattr);
2932 sony_nc_function_cleanup(sony_pf_device);
2933 sony_nc_handles_cleanup(sony_pf_device);
2935 sony_laptop_remove_input();
2936 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
2941 static const struct acpi_device_id sony_device_ids[] = {
2946 MODULE_DEVICE_TABLE(acpi, sony_device_ids);
2948 static const struct acpi_device_id sony_nc_device_ids[] = {
2953 static struct acpi_driver sony_nc_driver = {
2954 .name = SONY_NC_DRIVER_NAME,
2955 .class = SONY_NC_CLASS,
2956 .ids = sony_nc_device_ids,
2957 .owner = THIS_MODULE,
2960 .remove = sony_nc_remove,
2961 .notify = sony_nc_notify,
2963 .drv.pm = &sony_nc_pm,
2966 /*********** SPIC (SNY6001) Device ***********/
2968 #define SONYPI_DEVICE_TYPE1 0x00000001
2969 #define SONYPI_DEVICE_TYPE2 0x00000002
2970 #define SONYPI_DEVICE_TYPE3 0x00000004
2972 #define SONYPI_TYPE1_OFFSET 0x04
2973 #define SONYPI_TYPE2_OFFSET 0x12
2974 #define SONYPI_TYPE3_OFFSET 0x12
2976 struct sony_pic_ioport {
2977 struct acpi_resource_io io1;
2978 struct acpi_resource_io io2;
2979 struct list_head list;
2982 struct sony_pic_irq {
2983 struct acpi_resource_irq irq;
2984 struct list_head list;
2987 struct sonypi_eventtypes {
2990 struct sonypi_event *events;
2993 struct sony_pic_dev {
2994 struct acpi_device *acpi_dev;
2995 struct sony_pic_irq *cur_irq;
2996 struct sony_pic_ioport *cur_ioport;
2997 struct list_head interrupts;
2998 struct list_head ioports;
3000 struct sonypi_eventtypes *event_types;
3001 int (*handle_irq)(const u8, const u8);
3009 static struct sony_pic_dev spic_dev = {
3010 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
3011 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
3014 static int spic_drv_registered;
3017 #define SONYPI_JOGGER_MASK 0x00000001
3018 #define SONYPI_CAPTURE_MASK 0x00000002
3019 #define SONYPI_FNKEY_MASK 0x00000004
3020 #define SONYPI_BLUETOOTH_MASK 0x00000008
3021 #define SONYPI_PKEY_MASK 0x00000010
3022 #define SONYPI_BACK_MASK 0x00000020
3023 #define SONYPI_HELP_MASK 0x00000040
3024 #define SONYPI_LID_MASK 0x00000080
3025 #define SONYPI_ZOOM_MASK 0x00000100
3026 #define SONYPI_THUMBPHRASE_MASK 0x00000200
3027 #define SONYPI_MEYE_MASK 0x00000400
3028 #define SONYPI_MEMORYSTICK_MASK 0x00000800
3029 #define SONYPI_BATTERY_MASK 0x00001000
3030 #define SONYPI_WIRELESS_MASK 0x00002000
3032 struct sonypi_event {
3037 /* The set of possible button release events */
3038 static struct sonypi_event sonypi_releaseev[] = {
3039 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
3043 /* The set of possible jogger events */
3044 static struct sonypi_event sonypi_joggerev[] = {
3045 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
3046 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
3047 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
3048 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
3049 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
3050 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
3051 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
3052 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
3053 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
3054 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
3055 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
3056 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
3057 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
3061 /* The set of possible capture button events */
3062 static struct sonypi_event sonypi_captureev[] = {
3063 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
3064 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
3065 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
3066 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
3070 /* The set of possible fnkeys events */
3071 static struct sonypi_event sonypi_fnkeyev[] = {
3072 { 0x10, SONYPI_EVENT_FNKEY_ESC },
3073 { 0x11, SONYPI_EVENT_FNKEY_F1 },
3074 { 0x12, SONYPI_EVENT_FNKEY_F2 },
3075 { 0x13, SONYPI_EVENT_FNKEY_F3 },
3076 { 0x14, SONYPI_EVENT_FNKEY_F4 },
3077 { 0x15, SONYPI_EVENT_FNKEY_F5 },
3078 { 0x16, SONYPI_EVENT_FNKEY_F6 },
3079 { 0x17, SONYPI_EVENT_FNKEY_F7 },
3080 { 0x18, SONYPI_EVENT_FNKEY_F8 },
3081 { 0x19, SONYPI_EVENT_FNKEY_F9 },
3082 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
3083 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
3084 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
3085 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
3086 { 0x21, SONYPI_EVENT_FNKEY_1 },
3087 { 0x22, SONYPI_EVENT_FNKEY_2 },
3088 { 0x31, SONYPI_EVENT_FNKEY_D },
3089 { 0x32, SONYPI_EVENT_FNKEY_E },
3090 { 0x33, SONYPI_EVENT_FNKEY_F },
3091 { 0x34, SONYPI_EVENT_FNKEY_S },
3092 { 0x35, SONYPI_EVENT_FNKEY_B },
3093 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
3097 /* The set of possible program key events */
3098 static struct sonypi_event sonypi_pkeyev[] = {
3099 { 0x01, SONYPI_EVENT_PKEY_P1 },
3100 { 0x02, SONYPI_EVENT_PKEY_P2 },
3101 { 0x04, SONYPI_EVENT_PKEY_P3 },
3102 { 0x20, SONYPI_EVENT_PKEY_P1 },
3106 /* The set of possible bluetooth events */
3107 static struct sonypi_event sonypi_blueev[] = {
3108 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
3109 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
3110 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
3114 /* The set of possible wireless events */
3115 static struct sonypi_event sonypi_wlessev[] = {
3116 { 0x59, SONYPI_EVENT_IGNORE },
3117 { 0x5a, SONYPI_EVENT_IGNORE },
3121 /* The set of possible back button events */
3122 static struct sonypi_event sonypi_backev[] = {
3123 { 0x20, SONYPI_EVENT_BACK_PRESSED },
3127 /* The set of possible help button events */
3128 static struct sonypi_event sonypi_helpev[] = {
3129 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
3134 /* The set of possible lid events */
3135 static struct sonypi_event sonypi_lidev[] = {
3136 { 0x51, SONYPI_EVENT_LID_CLOSED },
3137 { 0x50, SONYPI_EVENT_LID_OPENED },
3141 /* The set of possible zoom events */
3142 static struct sonypi_event sonypi_zoomev[] = {
3143 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
3144 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
3145 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
3146 { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
3150 /* The set of possible thumbphrase events */
3151 static struct sonypi_event sonypi_thumbphraseev[] = {
3152 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
3156 /* The set of possible motioneye camera events */
3157 static struct sonypi_event sonypi_meyeev[] = {
3158 { 0x00, SONYPI_EVENT_MEYE_FACE },
3159 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
3163 /* The set of possible memorystick events */
3164 static struct sonypi_event sonypi_memorystickev[] = {
3165 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
3166 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
3170 /* The set of possible battery events */
3171 static struct sonypi_event sonypi_batteryev[] = {
3172 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
3173 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
3177 /* The set of possible volume events */
3178 static struct sonypi_event sonypi_volumeev[] = {
3179 { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
3180 { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
3184 /* The set of possible brightness events */
3185 static struct sonypi_event sonypi_brightnessev[] = {
3186 { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
3190 static struct sonypi_eventtypes type1_events[] = {
3191 { 0, 0xffffffff, sonypi_releaseev },
3192 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
3193 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
3194 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
3195 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
3196 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3197 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
3198 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
3199 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3200 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
3203 static struct sonypi_eventtypes type2_events[] = {
3204 { 0, 0xffffffff, sonypi_releaseev },
3205 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
3206 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
3207 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
3208 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3209 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
3210 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
3211 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
3212 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
3213 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
3214 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
3215 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3216 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
3217 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
3220 static struct sonypi_eventtypes type3_events[] = {
3221 { 0, 0xffffffff, sonypi_releaseev },
3222 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3223 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
3224 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3225 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
3226 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
3227 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
3228 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
3229 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
3230 { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
3231 { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
3235 /* low level spic calls */
3236 #define ITERATIONS_LONG 10000
3237 #define ITERATIONS_SHORT 10
3238 #define wait_on_command(command, iterations) { \
3239 unsigned int n = iterations; \
3240 while (--n && (command)) \
3243 dprintk("command failed at %s : %s (line %d)\n", \
3244 __FILE__, __func__, __LINE__); \
3247 static u8 sony_pic_call1(u8 dev)
3251 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
3253 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3254 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
3255 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
3256 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
3260 static u8 sony_pic_call2(u8 dev, u8 fn)
3264 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
3266 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3267 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
3269 outb(fn, spic_dev.cur_ioport->io1.minimum);
3270 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
3271 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
3275 static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
3279 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3280 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3281 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3282 outb(fn, spic_dev.cur_ioport->io1.minimum);
3283 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3284 outb(v, spic_dev.cur_ioport->io1.minimum);
3285 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
3286 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
3292 * minidrivers for SPIC models
3294 static int type3_handle_irq(const u8 data_mask, const u8 ev)
3297 * 0x31 could mean we have to take some extra action and wait for
3298 * the next irq for some Type3 models, it will generate a new
3299 * irq and we can read new data from the device:
3300 * - 0x5c and 0x5f requires 0xA0
3301 * - 0x61 requires 0xB3
3303 if (data_mask == 0x31) {
3304 if (ev == 0x5c || ev == 0x5f)
3305 sony_pic_call1(0xA0);
3306 else if (ev == 0x61)
3307 sony_pic_call1(0xB3);
3313 static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
3315 struct pci_dev *pcidev;
3317 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3318 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
3320 dev->model = SONYPI_DEVICE_TYPE1;
3321 dev->evport_offset = SONYPI_TYPE1_OFFSET;
3322 dev->event_types = type1_events;
3326 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3327 PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
3329 dev->model = SONYPI_DEVICE_TYPE2;
3330 dev->evport_offset = SONYPI_TYPE2_OFFSET;
3331 dev->event_types = type2_events;
3335 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3336 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
3338 dev->model = SONYPI_DEVICE_TYPE3;
3339 dev->handle_irq = type3_handle_irq;
3340 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3341 dev->event_types = type3_events;
3345 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3346 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
3348 dev->model = SONYPI_DEVICE_TYPE3;
3349 dev->handle_irq = type3_handle_irq;
3350 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3351 dev->event_types = type3_events;
3355 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3356 PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
3358 dev->model = SONYPI_DEVICE_TYPE3;
3359 dev->handle_irq = type3_handle_irq;
3360 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3361 dev->event_types = type3_events;
3366 dev->model = SONYPI_DEVICE_TYPE2;
3367 dev->evport_offset = SONYPI_TYPE2_OFFSET;
3368 dev->event_types = type2_events;
3372 pci_dev_put(pcidev);
3374 pr_info("detected Type%d model\n",
3375 dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
3376 dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
3379 /* camera tests and poweron/poweroff */
3380 #define SONYPI_CAMERA_PICTURE 5
3381 #define SONYPI_CAMERA_CONTROL 0x10
3383 #define SONYPI_CAMERA_BRIGHTNESS 0
3384 #define SONYPI_CAMERA_CONTRAST 1
3385 #define SONYPI_CAMERA_HUE 2
3386 #define SONYPI_CAMERA_COLOR 3
3387 #define SONYPI_CAMERA_SHARPNESS 4
3389 #define SONYPI_CAMERA_EXPOSURE_MASK 0xC
3390 #define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
3391 #define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
3392 #define SONYPI_CAMERA_MUTE_MASK 0x40
3394 /* the rest don't need a loop until not 0xff */
3395 #define SONYPI_CAMERA_AGC 6
3396 #define SONYPI_CAMERA_AGC_MASK 0x30
3397 #define SONYPI_CAMERA_SHUTTER_MASK 0x7
3399 #define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
3400 #define SONYPI_CAMERA_CONTROL 0x10
3402 #define SONYPI_CAMERA_STATUS 7
3403 #define SONYPI_CAMERA_STATUS_READY 0x2
3404 #define SONYPI_CAMERA_STATUS_POSITION 0x4
3406 #define SONYPI_DIRECTION_BACKWARDS 0x4
3408 #define SONYPI_CAMERA_REVISION 8
3409 #define SONYPI_CAMERA_ROMVERSION 9
3411 static int __sony_pic_camera_ready(void)
3415 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
3416 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
3419 static int __sony_pic_camera_off(void)
3422 pr_warn("camera control not enabled\n");
3426 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
3427 SONYPI_CAMERA_MUTE_MASK),
3430 if (spic_dev.camera_power) {
3431 sony_pic_call2(0x91, 0);
3432 spic_dev.camera_power = 0;
3437 static int __sony_pic_camera_on(void)
3442 pr_warn("camera control not enabled\n");
3446 if (spic_dev.camera_power)
3449 for (j = 5; j > 0; j--) {
3451 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
3453 sony_pic_call1(0x93);
3455 for (i = 400; i > 0; i--) {
3456 if (__sony_pic_camera_ready())
3465 pr_warn("failed to power on camera\n");
3469 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
3473 spic_dev.camera_power = 1;
3477 /* External camera command (exported to the motion eye v4l driver) */
3478 int sony_pic_camera_command(int command, u8 value)
3483 mutex_lock(&spic_dev.lock);
3486 case SONY_PIC_COMMAND_SETCAMERA:
3488 __sony_pic_camera_on();
3490 __sony_pic_camera_off();
3492 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
3493 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
3496 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
3497 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
3500 case SONY_PIC_COMMAND_SETCAMERAHUE:
3501 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
3504 case SONY_PIC_COMMAND_SETCAMERACOLOR:
3505 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
3508 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
3509 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
3512 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
3513 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
3516 case SONY_PIC_COMMAND_SETCAMERAAGC:
3517 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
3521 pr_err("sony_pic_camera_command invalid: %d\n", command);
3524 mutex_unlock(&spic_dev.lock);
3527 EXPORT_SYMBOL(sony_pic_camera_command);
3529 /* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
3530 static void __sony_pic_set_wwanpower(u8 state)
3533 if (spic_dev.wwan_power == state)
3535 sony_pic_call2(0xB0, state);
3536 sony_pic_call1(0x82);
3537 spic_dev.wwan_power = state;
3540 static ssize_t sony_pic_wwanpower_store(struct device *dev,
3541 struct device_attribute *attr,
3542 const char *buffer, size_t count)
3544 unsigned long value;
3548 if (kstrtoul(buffer, 10, &value))
3551 mutex_lock(&spic_dev.lock);
3552 __sony_pic_set_wwanpower(value);
3553 mutex_unlock(&spic_dev.lock);
3558 static ssize_t sony_pic_wwanpower_show(struct device *dev,
3559 struct device_attribute *attr, char *buffer)
3562 mutex_lock(&spic_dev.lock);
3563 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
3564 mutex_unlock(&spic_dev.lock);
3568 /* bluetooth subsystem power state */
3569 static void __sony_pic_set_bluetoothpower(u8 state)
3572 if (spic_dev.bluetooth_power == state)
3574 sony_pic_call2(0x96, state);
3575 sony_pic_call1(0x82);
3576 spic_dev.bluetooth_power = state;
3579 static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
3580 struct device_attribute *attr,
3581 const char *buffer, size_t count)
3583 unsigned long value;
3587 if (kstrtoul(buffer, 10, &value))
3590 mutex_lock(&spic_dev.lock);
3591 __sony_pic_set_bluetoothpower(value);
3592 mutex_unlock(&spic_dev.lock);
3597 static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
3598 struct device_attribute *attr, char *buffer)
3601 mutex_lock(&spic_dev.lock);
3602 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
3603 mutex_unlock(&spic_dev.lock);
3608 /* FAN0 information (reverse engineered from ACPI tables) */
3609 #define SONY_PIC_FAN0_STATUS 0x93
3610 static int sony_pic_set_fanspeed(unsigned long value)
3612 return ec_write(SONY_PIC_FAN0_STATUS, value);
3615 static int sony_pic_get_fanspeed(u8 *value)
3617 return ec_read(SONY_PIC_FAN0_STATUS, value);
3620 static ssize_t sony_pic_fanspeed_store(struct device *dev,
3621 struct device_attribute *attr,
3622 const char *buffer, size_t count)
3624 unsigned long value;
3628 if (kstrtoul(buffer, 10, &value))
3631 if (sony_pic_set_fanspeed(value))
3637 static ssize_t sony_pic_fanspeed_show(struct device *dev,
3638 struct device_attribute *attr, char *buffer)
3641 if (sony_pic_get_fanspeed(&value))
3644 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
3647 #define SPIC_ATTR(_name, _mode) \
3648 struct device_attribute spic_attr_##_name = __ATTR(_name, \
3649 _mode, sony_pic_## _name ##_show, \
3650 sony_pic_## _name ##_store)
3652 static SPIC_ATTR(bluetoothpower, 0644);
3653 static SPIC_ATTR(wwanpower, 0644);
3654 static SPIC_ATTR(fanspeed, 0644);
3656 static struct attribute *spic_attributes[] = {
3657 &spic_attr_bluetoothpower.attr,
3658 &spic_attr_wwanpower.attr,
3659 &spic_attr_fanspeed.attr,
3663 static struct attribute_group spic_attribute_group = {
3664 .attrs = spic_attributes
3667 /******** SONYPI compatibility **********/
3668 #ifdef CONFIG_SONYPI_COMPAT
3670 /* battery / brightness / temperature addresses */
3671 #define SONYPI_BAT_FLAGS 0x81
3672 #define SONYPI_LCD_LIGHT 0x96
3673 #define SONYPI_BAT1_PCTRM 0xa0
3674 #define SONYPI_BAT1_LEFT 0xa2
3675 #define SONYPI_BAT1_MAXRT 0xa4
3676 #define SONYPI_BAT2_PCTRM 0xa8
3677 #define SONYPI_BAT2_LEFT 0xaa
3678 #define SONYPI_BAT2_MAXRT 0xac
3679 #define SONYPI_BAT1_MAXTK 0xb0
3680 #define SONYPI_BAT1_FULL 0xb2
3681 #define SONYPI_BAT2_MAXTK 0xb8
3682 #define SONYPI_BAT2_FULL 0xba
3683 #define SONYPI_TEMP_STATUS 0xC1
3685 struct sonypi_compat_s {
3686 struct fasync_struct *fifo_async;
3688 spinlock_t fifo_lock;
3689 wait_queue_head_t fifo_proc_list;
3690 atomic_t open_count;
3692 static struct sonypi_compat_s sonypi_compat = {
3693 .open_count = ATOMIC_INIT(0),
3696 static int sonypi_misc_fasync(int fd, struct file *filp, int on)
3698 return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
3701 static int sonypi_misc_release(struct inode *inode, struct file *file)
3703 atomic_dec(&sonypi_compat.open_count);
3707 static int sonypi_misc_open(struct inode *inode, struct file *file)
3709 /* Flush input queue on first open */
3710 unsigned long flags;
3712 spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
3714 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
3715 kfifo_reset(&sonypi_compat.fifo);
3717 spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
3722 static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
3723 size_t count, loff_t *pos)
3728 if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
3729 (file->f_flags & O_NONBLOCK))
3732 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
3733 kfifo_len(&sonypi_compat.fifo) != 0);
3737 while (ret < count &&
3738 (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
3739 &sonypi_compat.fifo_lock) == sizeof(c))) {
3740 if (put_user(c, buf++))
3746 struct inode *inode = file_inode(file);
3747 inode->i_atime = current_fs_time(inode->i_sb);
3753 static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
3755 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
3756 if (kfifo_len(&sonypi_compat.fifo))
3757 return POLLIN | POLLRDNORM;
3761 static int ec_read16(u8 addr, u16 *value)
3764 if (ec_read(addr, &val_lb))
3766 if (ec_read(addr + 1, &val_hb))
3768 *value = val_lb | (val_hb << 8);
3772 static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
3776 void __user *argp = (void __user *)arg;
3781 mutex_lock(&spic_dev.lock);
3783 case SONYPI_IOCGBRT:
3784 if (sony_bl_props.dev == NULL) {
3788 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL,
3793 val8 = ((value & 0xff) - 1) << 5;
3794 if (copy_to_user(argp, &val8, sizeof(val8)))
3797 case SONYPI_IOCSBRT:
3798 if (sony_bl_props.dev == NULL) {
3802 if (copy_from_user(&val8, argp, sizeof(val8))) {
3806 value = (val8 >> 5) + 1;
3807 if (sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &value,
3812 /* sync the backlight device status */
3813 sony_bl_props.dev->props.brightness =
3814 sony_backlight_get_brightness(sony_bl_props.dev);
3816 case SONYPI_IOCGBAT1CAP:
3817 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
3821 if (copy_to_user(argp, &val16, sizeof(val16)))
3824 case SONYPI_IOCGBAT1REM:
3825 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
3829 if (copy_to_user(argp, &val16, sizeof(val16)))
3832 case SONYPI_IOCGBAT2CAP:
3833 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
3837 if (copy_to_user(argp, &val16, sizeof(val16)))
3840 case SONYPI_IOCGBAT2REM:
3841 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
3845 if (copy_to_user(argp, &val16, sizeof(val16)))
3848 case SONYPI_IOCGBATFLAGS:
3849 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
3854 if (copy_to_user(argp, &val8, sizeof(val8)))
3857 case SONYPI_IOCGBLUE:
3858 val8 = spic_dev.bluetooth_power;
3859 if (copy_to_user(argp, &val8, sizeof(val8)))
3862 case SONYPI_IOCSBLUE:
3863 if (copy_from_user(&val8, argp, sizeof(val8))) {
3867 __sony_pic_set_bluetoothpower(val8);
3870 case SONYPI_IOCGFAN:
3871 if (sony_pic_get_fanspeed(&val8)) {
3875 if (copy_to_user(argp, &val8, sizeof(val8)))
3878 case SONYPI_IOCSFAN:
3879 if (copy_from_user(&val8, argp, sizeof(val8))) {
3883 if (sony_pic_set_fanspeed(val8))
3886 /* GET Temperature (useful under APM) */
3887 case SONYPI_IOCGTEMP:
3888 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
3892 if (copy_to_user(argp, &val8, sizeof(val8)))
3898 mutex_unlock(&spic_dev.lock);
3902 static const struct file_operations sonypi_misc_fops = {
3903 .owner = THIS_MODULE,
3904 .read = sonypi_misc_read,
3905 .poll = sonypi_misc_poll,
3906 .open = sonypi_misc_open,
3907 .release = sonypi_misc_release,
3908 .fasync = sonypi_misc_fasync,
3909 .unlocked_ioctl = sonypi_misc_ioctl,
3910 .llseek = noop_llseek,
3913 static struct miscdevice sonypi_misc_device = {
3914 .minor = MISC_DYNAMIC_MINOR,
3916 .fops = &sonypi_misc_fops,
3919 static void sonypi_compat_report_event(u8 event)
3921 kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
3922 sizeof(event), &sonypi_compat.fifo_lock);
3923 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
3924 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
3927 static int sonypi_compat_init(void)
3931 spin_lock_init(&sonypi_compat.fifo_lock);
3933 kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
3935 pr_err("kfifo_alloc failed\n");
3939 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
3942 sonypi_misc_device.minor = minor;
3943 error = misc_register(&sonypi_misc_device);
3945 pr_err("misc_register failed\n");
3946 goto err_free_kfifo;
3949 pr_info("device allocated minor is %d\n",
3950 sonypi_misc_device.minor);
3955 kfifo_free(&sonypi_compat.fifo);
3959 static void sonypi_compat_exit(void)
3961 misc_deregister(&sonypi_misc_device);
3962 kfifo_free(&sonypi_compat.fifo);
3965 static int sonypi_compat_init(void) { return 0; }
3966 static void sonypi_compat_exit(void) { }
3967 static void sonypi_compat_report_event(u8 event) { }
3968 #endif /* CONFIG_SONYPI_COMPAT */
3974 sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
3977 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
3979 switch (resource->type) {
3980 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
3982 /* start IO enumeration */
3983 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
3987 list_add(&ioport->list, &dev->ioports);
3991 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
3992 /* end IO enumeration */
3995 case ACPI_RESOURCE_TYPE_IRQ:
3997 struct acpi_resource_irq *p = &resource->data.irq;
3998 struct sony_pic_irq *interrupt = NULL;
3999 if (!p || !p->interrupt_count) {
4001 * IRQ descriptors may have no IRQ# bits set,
4002 * particularly those those w/ _STA disabled
4004 dprintk("Blank IRQ resource\n");
4007 for (i = 0; i < p->interrupt_count; i++) {
4008 if (!p->interrupts[i]) {
4009 pr_warn("Invalid IRQ %d\n",
4013 interrupt = kzalloc(sizeof(*interrupt),
4018 list_add(&interrupt->list, &dev->interrupts);
4019 interrupt->irq.triggering = p->triggering;
4020 interrupt->irq.polarity = p->polarity;
4021 interrupt->irq.sharable = p->sharable;
4022 interrupt->irq.interrupt_count = 1;
4023 interrupt->irq.interrupts[0] = p->interrupts[i];
4027 case ACPI_RESOURCE_TYPE_IO:
4029 struct acpi_resource_io *io = &resource->data.io;
4030 struct sony_pic_ioport *ioport =
4031 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
4033 dprintk("Blank IO resource\n");
4037 if (!ioport->io1.minimum) {
4038 memcpy(&ioport->io1, io, sizeof(*io));
4039 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
4040 ioport->io1.address_length);
4042 else if (!ioport->io2.minimum) {
4043 memcpy(&ioport->io2, io, sizeof(*io));
4044 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
4045 ioport->io2.address_length);
4048 pr_err("Unknown SPIC Type, more than 2 IO Ports\n");
4054 dprintk("Resource %d isn't an IRQ nor an IO port\n",
4057 case ACPI_RESOURCE_TYPE_END_TAG:
4060 return AE_CTRL_TERMINATE;
4063 static int sony_pic_possible_resources(struct acpi_device *device)
4066 acpi_status status = AE_OK;
4071 /* get device status */
4072 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
4073 dprintk("Evaluating _STA\n");
4074 result = acpi_bus_get_status(device);
4076 pr_warn("Unable to read status\n");
4080 if (!device->status.enabled)
4081 dprintk("Device disabled\n");
4083 dprintk("Device enabled\n");
4086 * Query and parse 'method'
4088 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
4089 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
4090 sony_pic_read_possible_resource, &spic_dev);
4091 if (ACPI_FAILURE(status)) {
4092 pr_warn("Failure evaluating %s\n", METHOD_NAME__PRS);
4100 * Disable the spic device by calling its _DIS method
4102 static int sony_pic_disable(struct acpi_device *device)
4104 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
4107 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
4110 dprintk("Device disabled\n");
4116 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
4118 * Call _SRS to set current resources
4120 static int sony_pic_enable(struct acpi_device *device,
4121 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
4125 /* Type 1 resource layout is:
4131 * Type 2 and 3 resource layout is:
4137 struct acpi_resource res1;
4138 struct acpi_resource res2;
4139 struct acpi_resource res3;
4140 struct acpi_resource res4;
4142 struct acpi_buffer buffer = { 0, NULL };
4144 if (!ioport || !irq)
4147 /* init acpi_buffer */
4148 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
4152 buffer.length = sizeof(*resource) + 1;
4153 buffer.pointer = resource;
4155 /* setup Type 1 resources */
4156 if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
4158 /* setup io resources */
4159 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
4160 resource->res1.length = sizeof(struct acpi_resource);
4161 memcpy(&resource->res1.data.io, &ioport->io1,
4162 sizeof(struct acpi_resource_io));
4164 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
4165 resource->res2.length = sizeof(struct acpi_resource);
4166 memcpy(&resource->res2.data.io, &ioport->io2,
4167 sizeof(struct acpi_resource_io));
4169 /* setup irq resource */
4170 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
4171 resource->res3.length = sizeof(struct acpi_resource);
4172 memcpy(&resource->res3.data.irq, &irq->irq,
4173 sizeof(struct acpi_resource_irq));
4174 /* we requested a shared irq */
4175 resource->res3.data.irq.sharable = ACPI_SHARED;
4177 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
4178 resource->res4.length = sizeof(struct acpi_resource);
4180 /* setup Type 2/3 resources */
4182 /* setup io resource */
4183 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
4184 resource->res1.length = sizeof(struct acpi_resource);
4185 memcpy(&resource->res1.data.io, &ioport->io1,
4186 sizeof(struct acpi_resource_io));
4188 /* setup irq resource */
4189 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
4190 resource->res2.length = sizeof(struct acpi_resource);
4191 memcpy(&resource->res2.data.irq, &irq->irq,
4192 sizeof(struct acpi_resource_irq));
4193 /* we requested a shared irq */
4194 resource->res2.data.irq.sharable = ACPI_SHARED;
4196 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
4197 resource->res3.length = sizeof(struct acpi_resource);
4200 /* Attempt to set the resource */
4201 dprintk("Evaluating _SRS\n");
4202 status = acpi_set_current_resources(device->handle, &buffer);
4204 /* check for total failure */
4205 if (ACPI_FAILURE(status)) {
4206 pr_err("Error evaluating _SRS\n");
4211 /* Necessary device initializations calls (from sonypi) */
4212 sony_pic_call1(0x82);
4213 sony_pic_call2(0x81, 0xff);
4214 sony_pic_call1(compat ? 0x92 : 0x82);
4223 * ISR: some event is available
4226 static irqreturn_t sony_pic_irq(int irq, void *dev_id)
4231 u8 device_event = 0;
4233 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
4235 ev = inb_p(dev->cur_ioport->io1.minimum);
4236 if (dev->cur_ioport->io2.minimum)
4237 data_mask = inb_p(dev->cur_ioport->io2.minimum);
4239 data_mask = inb_p(dev->cur_ioport->io1.minimum +
4240 dev->evport_offset);
4242 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
4243 ev, data_mask, dev->cur_ioport->io1.minimum,
4244 dev->evport_offset);
4246 if (ev == 0x00 || ev == 0xff)
4249 for (i = 0; dev->event_types[i].mask; i++) {
4251 if ((data_mask & dev->event_types[i].data) !=
4252 dev->event_types[i].data)
4255 if (!(mask & dev->event_types[i].mask))
4258 for (j = 0; dev->event_types[i].events[j].event; j++) {
4259 if (ev == dev->event_types[i].events[j].data) {
4261 dev->event_types[i].events[j].event;
4262 /* some events may require ignoring */
4269 /* Still not able to decode the event try to pass
4270 * it over to the minidriver
4272 if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
4275 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
4276 ev, data_mask, dev->cur_ioport->io1.minimum,
4277 dev->evport_offset);
4281 sony_laptop_report_input_event(device_event);
4282 sonypi_compat_report_event(device_event);
4291 static int sony_pic_remove(struct acpi_device *device)
4293 struct sony_pic_ioport *io, *tmp_io;
4294 struct sony_pic_irq *irq, *tmp_irq;
4296 if (sony_pic_disable(device)) {
4297 pr_err("Couldn't disable device\n");
4301 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
4302 release_region(spic_dev.cur_ioport->io1.minimum,
4303 spic_dev.cur_ioport->io1.address_length);
4304 if (spic_dev.cur_ioport->io2.minimum)
4305 release_region(spic_dev.cur_ioport->io2.minimum,
4306 spic_dev.cur_ioport->io2.address_length);
4308 sonypi_compat_exit();
4310 sony_laptop_remove_input();
4313 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
4316 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
4317 list_del(&io->list);
4320 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
4321 list_del(&irq->list);
4324 spic_dev.cur_ioport = NULL;
4325 spic_dev.cur_irq = NULL;
4327 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
4331 static int sony_pic_add(struct acpi_device *device)
4334 struct sony_pic_ioport *io, *tmp_io;
4335 struct sony_pic_irq *irq, *tmp_irq;
4337 pr_info("%s v%s\n", SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
4339 spic_dev.acpi_dev = device;
4340 strcpy(acpi_device_class(device), "sony/hotkey");
4341 sony_pic_detect_device_type(&spic_dev);
4342 mutex_init(&spic_dev.lock);
4344 /* read _PRS resources */
4345 result = sony_pic_possible_resources(device);
4347 pr_err("Unable to read possible resources\n");
4348 goto err_free_resources;
4351 /* setup input devices and helper fifo */
4352 result = sony_laptop_setup_input(device);
4354 pr_err("Unable to create input devices\n");
4355 goto err_free_resources;
4358 result = sonypi_compat_init();
4360 goto err_remove_input;
4362 /* request io port */
4363 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
4364 if (request_region(io->io1.minimum, io->io1.address_length,
4365 "Sony Programmable I/O Device")) {
4366 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
4367 io->io1.minimum, io->io1.maximum,
4368 io->io1.address_length);
4369 /* Type 1 have 2 ioports */
4370 if (io->io2.minimum) {
4371 if (request_region(io->io2.minimum,
4372 io->io2.address_length,
4373 "Sony Programmable I/O Device")) {
4374 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
4375 io->io2.minimum, io->io2.maximum,
4376 io->io2.address_length);
4377 spic_dev.cur_ioport = io;
4381 dprintk("Unable to get I/O port2: "
4382 "0x%.4x (0x%.4x) + 0x%.2x\n",
4383 io->io2.minimum, io->io2.maximum,
4384 io->io2.address_length);
4385 release_region(io->io1.minimum,
4386 io->io1.address_length);
4390 spic_dev.cur_ioport = io;
4395 if (!spic_dev.cur_ioport) {
4396 pr_err("Failed to request_region\n");
4398 goto err_remove_compat;
4402 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
4403 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
4404 0, "sony-laptop", &spic_dev)) {
4405 dprintk("IRQ: %d - triggering: %d - "
4406 "polarity: %d - shr: %d\n",
4407 irq->irq.interrupts[0],
4408 irq->irq.triggering,
4411 spic_dev.cur_irq = irq;
4415 if (!spic_dev.cur_irq) {
4416 pr_err("Failed to request_irq\n");
4418 goto err_release_region;
4421 /* set resource status _SRS */
4422 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
4424 pr_err("Couldn't enable device\n");
4428 spic_dev.bluetooth_power = -1;
4429 /* create device attributes */
4430 result = sony_pf_add();
4432 goto err_disable_device;
4434 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
4444 sony_pic_disable(device);
4447 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
4450 release_region(spic_dev.cur_ioport->io1.minimum,
4451 spic_dev.cur_ioport->io1.address_length);
4452 if (spic_dev.cur_ioport->io2.minimum)
4453 release_region(spic_dev.cur_ioport->io2.minimum,
4454 spic_dev.cur_ioport->io2.address_length);
4457 sonypi_compat_exit();
4460 sony_laptop_remove_input();
4463 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
4464 list_del(&io->list);
4467 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
4468 list_del(&irq->list);
4471 spic_dev.cur_ioport = NULL;
4472 spic_dev.cur_irq = NULL;
4477 #ifdef CONFIG_PM_SLEEP
4478 static int sony_pic_suspend(struct device *dev)
4480 if (sony_pic_disable(to_acpi_device(dev)))
4485 static int sony_pic_resume(struct device *dev)
4487 sony_pic_enable(to_acpi_device(dev),
4488 spic_dev.cur_ioport, spic_dev.cur_irq);
4493 static SIMPLE_DEV_PM_OPS(sony_pic_pm, sony_pic_suspend, sony_pic_resume);
4495 static const struct acpi_device_id sony_pic_device_ids[] = {
4500 static struct acpi_driver sony_pic_driver = {
4501 .name = SONY_PIC_DRIVER_NAME,
4502 .class = SONY_PIC_CLASS,
4503 .ids = sony_pic_device_ids,
4504 .owner = THIS_MODULE,
4506 .add = sony_pic_add,
4507 .remove = sony_pic_remove,
4509 .drv.pm = &sony_pic_pm,
4512 static struct dmi_system_id __initdata sonypi_dmi_table[] = {
4514 .ident = "Sony Vaio",
4516 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
4517 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
4521 .ident = "Sony Vaio",
4523 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
4524 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
4530 static int __init sony_laptop_init(void)
4534 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
4535 result = acpi_bus_register_driver(&sony_pic_driver);
4537 pr_err("Unable to register SPIC driver\n");
4540 spic_drv_registered = 1;
4543 result = acpi_bus_register_driver(&sony_nc_driver);
4545 pr_err("Unable to register SNC driver\n");
4546 goto out_unregister_pic;
4552 if (spic_drv_registered)
4553 acpi_bus_unregister_driver(&sony_pic_driver);
4558 static void __exit sony_laptop_exit(void)
4560 acpi_bus_unregister_driver(&sony_nc_driver);
4561 if (spic_drv_registered)
4562 acpi_bus_unregister_driver(&sony_pic_driver);
4565 module_init(sony_laptop_init);
4566 module_exit(sony_laptop_exit);