blob: bc7f40bcd9cce660d90daf1be349665fea3c2527 [file] [log] [blame]
Stelian Pop7f09c432007-01-13 23:04:31 +01001/*
malattia@linux.it33a04452007-04-09 19:26:03 +02002 * ACPI Sony Notebook Control Driver (SNC and SPIC)
Stelian Pop7f09c432007-01-13 23:04:31 +01003 *
4 * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
Mattia Dongili37418342009-03-26 21:58:24 +09005 * Copyright (C) 2007-2009 Mattia Dongili <malattia@linux.it>
Stelian Pop7f09c432007-01-13 23:04:31 +01006 *
7 * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
8 * which are copyrighted by their respective authors.
9 *
malattia@linux.it33a04452007-04-09 19:26:03 +020010 * The SNY6001 driver part is based on the sonypi driver which includes
11 * material from:
12 *
13 * Copyright (C) 2001-2005 Stelian Pop <stelian@popies.net>
14 *
15 * Copyright (C) 2005 Narayanan R S <nars@kadamba.org>
16 *
Marcin Garskidb9551702007-10-19 23:22:11 +020017 * Copyright (C) 2001-2002 AlcĂ´ve <www.alcove.com>
malattia@linux.it33a04452007-04-09 19:26:03 +020018 *
19 * Copyright (C) 2001 Michael Ashley <m.ashley@unsw.edu.au>
20 *
21 * Copyright (C) 2001 Junichi Morita <jun1m@mars.dti.ne.jp>
22 *
23 * Copyright (C) 2000 Takaya Kinjo <t-kinjo@tc4.so-net.ne.jp>
24 *
25 * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
26 *
27 * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
28 *
Stelian Pop7f09c432007-01-13 23:04:31 +010029 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
33 *
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
38 *
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
42 *
43 */
44
Joe Perches50f581a2011-03-29 15:21:48 -070045#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
46
Stelian Pop7f09c432007-01-13 23:04:31 +010047#include <linux/kernel.h>
48#include <linux/module.h>
49#include <linux/moduleparam.h>
50#include <linux/init.h>
51#include <linux/types.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010052#include <linux/backlight.h>
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010053#include <linux/platform_device.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010054#include <linux/err.h>
malattia@linux.it33a04452007-04-09 19:26:03 +020055#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090063#include <linux/slab.h>
Stelian Pop7f09c432007-01-13 23:04:31 +010064#include <acpi/acpi_drivers.h>
65#include <acpi/acpi_bus.h>
66#include <asm/uaccess.h>
malattia@linux.it33a04452007-04-09 19:26:03 +020067#include <linux/sonypi.h>
malattia@linux.it1ce82c12007-04-28 23:34:36 +090068#include <linux/sony-laptop.h>
Matthew Garrett6cc056b2009-03-26 21:58:15 +090069#include <linux/rfkill.h>
Mattia Dongilia64e62a2007-05-01 11:19:53 +090070#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +020071#include <linux/poll.h>
72#include <linux/miscdevice.h>
73#endif
Stelian Pop7f09c432007-01-13 23:04:31 +010074
Joe Perches50f581a2011-03-29 15:21:48 -070075#define dprintk(fmt, ...) \
76do { \
77 if (debug) \
78 pr_warn(fmt, ##__VA_ARGS__); \
malattia@linux.itb9a218b2007-04-09 10:19:06 +020079} while (0)
80
Mattia Dongili425ef5d2008-01-14 18:05:44 +090081#define SONY_LAPTOP_DRIVER_VERSION "0.6"
Alessandro Guido50f62af2007-01-13 23:04:34 +010082
malattia@linux.it33a04452007-04-09 19:26:03 +020083#define SONY_NC_CLASS "sony-nc"
84#define SONY_NC_HID "SNY5001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020085#define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
malattia@linux.it33a04452007-04-09 19:26:03 +020086
87#define SONY_PIC_CLASS "sony-pic"
88#define SONY_PIC_HID "SNY6001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020089#define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
Stelian Pop7f09c432007-01-13 23:04:31 +010090
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010091MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
malattia@linux.it33a04452007-04-09 19:26:03 +020092MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
Stelian Pop7f09c432007-01-13 23:04:31 +010093MODULE_LICENSE("GPL");
malattia@linux.it33a04452007-04-09 19:26:03 +020094MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
Stelian Pop7f09c432007-01-13 23:04:31 +010095
96static int debug;
97module_param(debug, int, 0);
98MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
Len Browna02d1c12007-02-07 15:34:02 -050099 "the development of this driver");
Stelian Pop7f09c432007-01-13 23:04:31 +0100100
malattia@linux.it33a04452007-04-09 19:26:03 +0200101static int no_spic; /* = 0 */
102module_param(no_spic, int, 0444);
103MODULE_PARM_DESC(no_spic,
104 "set this if you don't want to enable the SPIC device");
105
106static int compat; /* = 0 */
107module_param(compat, int, 0444);
108MODULE_PARM_DESC(compat,
malattia@linux.it7b153f32007-04-09 19:31:25 +0200109 "set this if you want to enable backward compatibility mode");
malattia@linux.it33a04452007-04-09 19:26:03 +0200110
111static unsigned long mask = 0xffffffff;
112module_param(mask, ulong, 0644);
113MODULE_PARM_DESC(mask,
114 "set this to the mask of event you want to enable (see doc)");
115
malattia@linux.it5f3d2892007-04-28 23:18:45 +0900116static int camera; /* = 0 */
117module_param(camera, int, 0444);
118MODULE_PARM_DESC(camera,
119 "set this to 1 to enable Motion Eye camera controls "
120 "(only use it if you have a C1VE or C1VN model)");
121
Mattia Dongilia64e62a2007-05-01 11:19:53 +0900122#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +0200123static int minor = -1;
124module_param(minor, int, 0);
125MODULE_PARM_DESC(minor,
126 "minor number of the misc device for the SPIC compatibility code, "
127 "default is -1 (automatic)");
128#endif
129
Josh Boyer6fe6ae52011-11-02 14:32:00 -0400130static int kbd_backlight = 1;
Mattia Dongilibf155712011-02-19 11:52:31 +0900131module_param(kbd_backlight, int, 0444);
132MODULE_PARM_DESC(kbd_backlight,
133 "set this to 0 to disable keyboard backlight, "
134 "1 to enable it (default: 0)");
135
136static int kbd_backlight_timeout; /* = 0 */
137module_param(kbd_backlight_timeout, int, 0444);
138MODULE_PARM_DESC(kbd_backlight_timeout,
139 "set this to 0 to set the default 10 seconds timeout, "
140 "1 for 30 seconds, 2 for 60 seconds and 3 to disable timeout "
141 "(default: 0)");
142
Marco Chiapperodf410d52011-04-05 23:38:34 +0900143static void sony_nc_kbd_backlight_resume(void);
Marco Chiappero5fe801a2012-05-19 22:35:48 +0900144static void sony_nc_kbd_backlight_setup(struct platform_device *pd);
145static int sony_nc_kbd_backlight_cleanup(struct platform_device *pd);
Marco Chiapperodf410d52011-04-05 23:38:34 +0900146
Marco Chiappero967145a2012-05-19 22:35:50 +0900147static int sony_nc_battery_care_setup(struct platform_device *pd,
148 unsigned int handle);
149static void sony_nc_battery_care_cleanup(struct platform_device *pd);
150
Marco Chiappero49f000a2012-05-19 22:35:51 +0900151static int sony_nc_thermal_setup(struct platform_device *pd);
152static void sony_nc_thermal_cleanup(struct platform_device *pd);
153static void sony_nc_thermal_resume(void);
154
Marco Chiappero54535d02012-05-19 22:35:54 +0900155static int sony_nc_lid_resume_setup(struct platform_device *pd);
156static void sony_nc_lid_resume_cleanup(struct platform_device *pd);
157
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900158enum sony_nc_rfkill {
159 SONY_WIFI,
160 SONY_BLUETOOTH,
161 SONY_WWAN,
162 SONY_WIMAX,
Johannes Berg19d337d2009-06-02 13:01:37 +0200163 N_SONY_RFKILL,
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900164};
165
Mattia Dongilid5a664a2009-12-17 00:08:35 +0900166static int sony_rfkill_handle;
Johannes Berg19d337d2009-06-02 13:01:37 +0200167static struct rfkill *sony_rfkill_devices[N_SONY_RFKILL];
168static int sony_rfkill_address[N_SONY_RFKILL] = {0x300, 0x500, 0x700, 0x900};
Marco Chiappero5fe801a2012-05-19 22:35:48 +0900169static void sony_nc_rfkill_setup(struct acpi_device *device);
170static void sony_nc_rfkill_cleanup(void);
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900171static void sony_nc_rfkill_update(void);
172
malattia@linux.it1549ee62007-04-09 10:19:08 +0200173/*********** Input Devices ***********/
174
175#define SONY_LAPTOP_BUF_SIZE 128
176struct sony_laptop_input_s {
177 atomic_t users;
178 struct input_dev *jog_dev;
179 struct input_dev *key_dev;
Stefani Seibold45465482009-12-21 14:37:26 -0800180 struct kfifo fifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200181 spinlock_t fifo_lock;
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800182 struct timer_list release_key_timer;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200183};
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900184
malattia@linux.it1549ee62007-04-09 10:19:08 +0200185static struct sony_laptop_input_s sony_laptop_input = {
186 .users = ATOMIC_INIT(0),
187};
188
189struct sony_laptop_keypress {
190 struct input_dev *dev;
191 int key;
192};
193
Mattia Dongilibc57f862007-07-20 02:01:57 +0900194/* Correspondance table between sonypi events
195 * and input layer indexes in the keymap
196 */
197static int sony_laptop_input_index[] = {
Mattia Dongili3eb87492008-01-14 18:05:45 +0900198 -1, /* 0 no event */
199 -1, /* 1 SONYPI_EVENT_JOGDIAL_DOWN */
200 -1, /* 2 SONYPI_EVENT_JOGDIAL_UP */
201 -1, /* 3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
202 -1, /* 4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */
203 -1, /* 5 SONYPI_EVENT_JOGDIAL_PRESSED */
204 -1, /* 6 SONYPI_EVENT_JOGDIAL_RELEASED */
205 0, /* 7 SONYPI_EVENT_CAPTURE_PRESSED */
206 1, /* 8 SONYPI_EVENT_CAPTURE_RELEASED */
207 2, /* 9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
208 3, /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
209 4, /* 11 SONYPI_EVENT_FNKEY_ESC */
210 5, /* 12 SONYPI_EVENT_FNKEY_F1 */
211 6, /* 13 SONYPI_EVENT_FNKEY_F2 */
212 7, /* 14 SONYPI_EVENT_FNKEY_F3 */
213 8, /* 15 SONYPI_EVENT_FNKEY_F4 */
214 9, /* 16 SONYPI_EVENT_FNKEY_F5 */
215 10, /* 17 SONYPI_EVENT_FNKEY_F6 */
216 11, /* 18 SONYPI_EVENT_FNKEY_F7 */
217 12, /* 19 SONYPI_EVENT_FNKEY_F8 */
218 13, /* 20 SONYPI_EVENT_FNKEY_F9 */
219 14, /* 21 SONYPI_EVENT_FNKEY_F10 */
220 15, /* 22 SONYPI_EVENT_FNKEY_F11 */
221 16, /* 23 SONYPI_EVENT_FNKEY_F12 */
222 17, /* 24 SONYPI_EVENT_FNKEY_1 */
223 18, /* 25 SONYPI_EVENT_FNKEY_2 */
224 19, /* 26 SONYPI_EVENT_FNKEY_D */
225 20, /* 27 SONYPI_EVENT_FNKEY_E */
226 21, /* 28 SONYPI_EVENT_FNKEY_F */
227 22, /* 29 SONYPI_EVENT_FNKEY_S */
228 23, /* 30 SONYPI_EVENT_FNKEY_B */
229 24, /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */
230 25, /* 32 SONYPI_EVENT_PKEY_P1 */
231 26, /* 33 SONYPI_EVENT_PKEY_P2 */
232 27, /* 34 SONYPI_EVENT_PKEY_P3 */
233 28, /* 35 SONYPI_EVENT_BACK_PRESSED */
234 -1, /* 36 SONYPI_EVENT_LID_CLOSED */
235 -1, /* 37 SONYPI_EVENT_LID_OPENED */
236 29, /* 38 SONYPI_EVENT_BLUETOOTH_ON */
237 30, /* 39 SONYPI_EVENT_BLUETOOTH_OFF */
238 31, /* 40 SONYPI_EVENT_HELP_PRESSED */
239 32, /* 41 SONYPI_EVENT_FNKEY_ONLY */
240 33, /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
241 34, /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */
242 35, /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
243 36, /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
244 37, /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
245 38, /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */
246 39, /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
247 40, /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
248 41, /* 50 SONYPI_EVENT_ZOOM_PRESSED */
249 42, /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */
250 43, /* 52 SONYPI_EVENT_MEYE_FACE */
251 44, /* 53 SONYPI_EVENT_MEYE_OPPOSITE */
252 45, /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */
253 46, /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */
254 -1, /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */
255 -1, /* 57 SONYPI_EVENT_BATTERY_INSERT */
256 -1, /* 58 SONYPI_EVENT_BATTERY_REMOVE */
257 -1, /* 59 SONYPI_EVENT_FNKEY_RELEASED */
258 47, /* 60 SONYPI_EVENT_WIRELESS_ON */
259 48, /* 61 SONYPI_EVENT_WIRELESS_OFF */
260 49, /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */
261 50, /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900262 51, /* 64 SONYPI_EVENT_CD_EJECT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900263 52, /* 65 SONYPI_EVENT_MODEKEY_PRESSED */
264 53, /* 66 SONYPI_EVENT_PKEY_P4 */
265 54, /* 67 SONYPI_EVENT_PKEY_P5 */
266 55, /* 68 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900267 56, /* 69 SONYPI_EVENT_VOLUME_INC_PRESSED */
268 57, /* 70 SONYPI_EVENT_VOLUME_DEC_PRESSED */
269 -1, /* 71 SONYPI_EVENT_BRIGHTNESS_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900270 58, /* 72 SONYPI_EVENT_MEDIA_PRESSED */
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900271 59, /* 72 SONYPI_EVENT_VENDOR_PRESSED */
Mattia Dongilibc57f862007-07-20 02:01:57 +0900272};
273
274static int sony_laptop_input_keycode_map[] = {
275 KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
276 KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
277 KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
278 KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
279 KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
280 KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
281 KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
282 KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
283 KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
284 KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
285 KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
286 KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
287 KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
288 KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
289 KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
290 KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
291 KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
292 KEY_FN_F1, /* 17 SONYPI_EVENT_FNKEY_1 */
293 KEY_FN_F2, /* 18 SONYPI_EVENT_FNKEY_2 */
294 KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
295 KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
296 KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
297 KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
298 KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
299 KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
300 KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
301 KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
302 KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
303 KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
304 KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
305 KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
306 KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
307 KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
308 KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
309 KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
310 KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
311 KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
312 KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
313 KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
314 KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
315 KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
316 KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
317 BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
318 KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
319 KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
320 KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
321 KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
322 KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
323 KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
Mattia Dongili3eb87492008-01-14 18:05:45 +0900324 KEY_ZOOMIN, /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900325 KEY_ZOOMOUT, /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900326 KEY_EJECTCD, /* 51 SONYPI_EVENT_CD_EJECT_PRESSED */
327 KEY_F13, /* 52 SONYPI_EVENT_MODEKEY_PRESSED */
328 KEY_PROG4, /* 53 SONYPI_EVENT_PKEY_P4 */
329 KEY_F14, /* 54 SONYPI_EVENT_PKEY_P5 */
330 KEY_F15, /* 55 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900331 KEY_VOLUMEUP, /* 56 SONYPI_EVENT_VOLUME_INC_PRESSED */
332 KEY_VOLUMEDOWN, /* 57 SONYPI_EVENT_VOLUME_DEC_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900333 KEY_MEDIA, /* 58 SONYPI_EVENT_MEDIA_PRESSED */
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900334 KEY_VENDOR, /* 59 SONYPI_EVENT_VENDOR_PRESSED */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200335};
336
337/* release buttons after a short delay if pressed */
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800338static void do_sony_laptop_release_key(unsigned long unused)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200339{
340 struct sony_laptop_keypress kp;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800341 unsigned long flags;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200342
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800343 spin_lock_irqsave(&sony_laptop_input.fifo_lock, flags);
344
345 if (kfifo_out(&sony_laptop_input.fifo,
346 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200347 input_report_key(kp.dev, kp.key, 0);
348 input_sync(kp.dev);
349 }
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800350
351 /* If there is something in the fifo schedule next release. */
352 if (kfifo_len(&sony_laptop_input.fifo) != 0)
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800353 mod_timer(&sony_laptop_input.release_key_timer,
354 jiffies + msecs_to_jiffies(10));
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800355
356 spin_unlock_irqrestore(&sony_laptop_input.fifo_lock, flags);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200357}
malattia@linux.it1549ee62007-04-09 10:19:08 +0200358
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200359/* forward event to the input subsystem */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200360static void sony_laptop_report_input_event(u8 event)
361{
362 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
363 struct input_dev *key_dev = sony_laptop_input.key_dev;
364 struct sony_laptop_keypress kp = { NULL };
John Hughes747a5622011-11-16 19:51:57 +0100365 int scancode = -1;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200366
Almer S. Tigelaara83021a2009-04-12 11:26:28 +0000367 if (event == SONYPI_EVENT_FNKEY_RELEASED ||
368 event == SONYPI_EVENT_ANYBUTTON_RELEASED) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200369 /* Nothing, not all VAIOs generate this event */
370 return;
371 }
372
373 /* report events */
374 switch (event) {
375 /* jog_dev events */
376 case SONYPI_EVENT_JOGDIAL_UP:
377 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
378 input_report_rel(jog_dev, REL_WHEEL, 1);
379 input_sync(jog_dev);
380 return;
381
382 case SONYPI_EVENT_JOGDIAL_DOWN:
383 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
384 input_report_rel(jog_dev, REL_WHEEL, -1);
385 input_sync(jog_dev);
386 return;
387
388 /* key_dev events */
389 case SONYPI_EVENT_JOGDIAL_PRESSED:
390 kp.key = BTN_MIDDLE;
391 kp.dev = jog_dev;
392 break;
393
394 default:
Adrian Bunkd399d132008-02-20 00:59:03 +0200395 if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
Mattia Dongilibc57f862007-07-20 02:01:57 +0900396 dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
397 break;
398 }
John Hughes747a5622011-11-16 19:51:57 +0100399 if ((scancode = sony_laptop_input_index[event]) != -1) {
400 kp.key = sony_laptop_input_keycode_map[scancode];
Mattia Dongilibc57f862007-07-20 02:01:57 +0900401 if (kp.key != KEY_UNKNOWN)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200402 kp.dev = key_dev;
Mattia Dongilibc57f862007-07-20 02:01:57 +0900403 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200404 break;
405 }
406
407 if (kp.dev) {
John Hughes747a5622011-11-16 19:51:57 +0100408 /* if we have a scancode we emit it so we can always
409 remap the key */
410 if (scancode != -1)
411 input_event(kp.dev, EV_MSC, MSC_SCAN, scancode);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200412 input_report_key(kp.dev, kp.key, 1);
413 input_sync(kp.dev);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200414
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800415 /* schedule key release */
416 kfifo_in_locked(&sony_laptop_input.fifo,
417 (unsigned char *)&kp, sizeof(kp),
418 &sony_laptop_input.fifo_lock);
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800419 mod_timer(&sony_laptop_input.release_key_timer,
420 jiffies + msecs_to_jiffies(10));
malattia@linux.it1549ee62007-04-09 10:19:08 +0200421 } else
422 dprintk("unknown input event %.2x\n", event);
423}
424
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500425static int sony_laptop_setup_input(struct acpi_device *acpi_device)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200426{
427 struct input_dev *jog_dev;
428 struct input_dev *key_dev;
429 int i;
430 int error;
431
432 /* don't run again if already initialized */
433 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
434 return 0;
435
436 /* kfifo */
437 spin_lock_init(&sony_laptop_input.fifo_lock);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800438 error = kfifo_alloc(&sony_laptop_input.fifo,
439 SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -0800440 if (error) {
Joe Perches50f581a2011-03-29 15:21:48 -0700441 pr_err("kfifo_alloc failed\n");
malattia@linux.it1549ee62007-04-09 10:19:08 +0200442 goto err_dec_users;
443 }
444
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800445 setup_timer(&sony_laptop_input.release_key_timer,
446 do_sony_laptop_release_key, 0);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200447
448 /* input keys */
449 key_dev = input_allocate_device();
450 if (!key_dev) {
451 error = -ENOMEM;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800452 goto err_free_kfifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200453 }
454
455 key_dev->name = "Sony Vaio Keys";
456 key_dev->id.bustype = BUS_ISA;
457 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500458 key_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200459
460 /* Initialize the Input Drivers: special keys */
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800461 input_set_capability(key_dev, EV_MSC, MSC_SCAN);
462
463 __set_bit(EV_KEY, key_dev->evbit);
Mattia Dongilibc57f862007-07-20 02:01:57 +0900464 key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
465 key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
466 key_dev->keycode = &sony_laptop_input_keycode_map;
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800467 for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++)
468 __set_bit(sony_laptop_input_keycode_map[i], key_dev->keybit);
469 __clear_bit(KEY_RESERVED, key_dev->keybit);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200470
471 error = input_register_device(key_dev);
472 if (error)
473 goto err_free_keydev;
474
475 sony_laptop_input.key_dev = key_dev;
476
477 /* jogdial */
478 jog_dev = input_allocate_device();
479 if (!jog_dev) {
480 error = -ENOMEM;
481 goto err_unregister_keydev;
482 }
483
484 jog_dev->name = "Sony Vaio Jogdial";
485 jog_dev->id.bustype = BUS_ISA;
486 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
John Hughes747a5622011-11-16 19:51:57 +0100487 jog_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200488
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800489 input_set_capability(jog_dev, EV_KEY, BTN_MIDDLE);
490 input_set_capability(jog_dev, EV_REL, REL_WHEEL);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200491
492 error = input_register_device(jog_dev);
493 if (error)
494 goto err_free_jogdev;
495
496 sony_laptop_input.jog_dev = jog_dev;
497
498 return 0;
499
500err_free_jogdev:
501 input_free_device(jog_dev);
502
503err_unregister_keydev:
504 input_unregister_device(key_dev);
505 /* to avoid kref underflow below at input_free_device */
506 key_dev = NULL;
507
508err_free_keydev:
509 input_free_device(key_dev);
510
malattia@linux.it1549ee62007-04-09 10:19:08 +0200511err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -0800512 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200513
514err_dec_users:
515 atomic_dec(&sony_laptop_input.users);
516 return error;
517}
518
519static void sony_laptop_remove_input(void)
520{
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800521 struct sony_laptop_keypress kp = { NULL };
522
523 /* Cleanup only after the last user has gone */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200524 if (!atomic_dec_and_test(&sony_laptop_input.users))
525 return;
526
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800527 del_timer_sync(&sony_laptop_input.release_key_timer);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800528
529 /*
530 * Generate key-up events for remaining keys. Note that we don't
531 * need locking since nobody is adding new events to the kfifo.
532 */
533 while (kfifo_out(&sony_laptop_input.fifo,
534 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
535 input_report_key(kp.dev, kp.key, 0);
536 input_sync(kp.dev);
537 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200538
539 /* destroy input devs */
540 input_unregister_device(sony_laptop_input.key_dev);
541 sony_laptop_input.key_dev = NULL;
542
543 if (sony_laptop_input.jog_dev) {
544 input_unregister_device(sony_laptop_input.jog_dev);
545 sony_laptop_input.jog_dev = NULL;
546 }
547
Stefani Seibold45465482009-12-21 14:37:26 -0800548 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200549}
550
malattia@linux.it56b87562007-04-09 10:19:05 +0200551/*********** Platform Device ***********/
552
553static atomic_t sony_pf_users = ATOMIC_INIT(0);
554static struct platform_driver sony_pf_driver = {
555 .driver = {
556 .name = "sony-laptop",
557 .owner = THIS_MODULE,
558 }
559};
560static struct platform_device *sony_pf_device;
561
562static int sony_pf_add(void)
563{
564 int ret = 0;
565
566 /* don't run again if already initialized */
567 if (atomic_add_return(1, &sony_pf_users) > 1)
568 return 0;
569
570 ret = platform_driver_register(&sony_pf_driver);
571 if (ret)
572 goto out;
573
574 sony_pf_device = platform_device_alloc("sony-laptop", -1);
575 if (!sony_pf_device) {
576 ret = -ENOMEM;
577 goto out_platform_registered;
578 }
579
580 ret = platform_device_add(sony_pf_device);
581 if (ret)
582 goto out_platform_alloced;
583
584 return 0;
585
586 out_platform_alloced:
587 platform_device_put(sony_pf_device);
588 sony_pf_device = NULL;
589 out_platform_registered:
590 platform_driver_unregister(&sony_pf_driver);
591 out:
592 atomic_dec(&sony_pf_users);
593 return ret;
594}
595
596static void sony_pf_remove(void)
597{
598 /* deregister only after the last user has gone */
599 if (!atomic_dec_and_test(&sony_pf_users))
600 return;
601
Axel Lin08db2b32010-07-01 10:18:01 +0800602 platform_device_unregister(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +0200603 platform_driver_unregister(&sony_pf_driver);
604}
605
606/*********** SNC (SNY5001) Device ***********/
607
malattia@linux.it33a04452007-04-09 19:26:03 +0200608/* the device uses 1-based values, while the backlight subsystem uses
609 0-based values */
610#define SONY_MAX_BRIGHTNESS 8
611
612#define SNC_VALIDATE_IN 0
613#define SNC_VALIDATE_OUT 1
614
malattia@linux.it59b19102007-04-09 10:19:04 +0200615static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500616 char *);
malattia@linux.it59b19102007-04-09 10:19:04 +0200617static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500618 const char *, size_t);
Mattia Dongili156c2212007-02-12 22:01:07 +0100619static int boolean_validate(const int, const int);
620static int brightness_default_validate(const int, const int);
621
malattia@linux.it59b19102007-04-09 10:19:04 +0200622struct sony_nc_value {
Len Browna02d1c12007-02-07 15:34:02 -0500623 char *name; /* name of the entry */
624 char **acpiget; /* names of the ACPI get function */
625 char **acpiset; /* names of the ACPI set function */
Mattia Dongili156c2212007-02-12 22:01:07 +0100626 int (*validate)(const int, const int); /* input/output validation */
Len Browna02d1c12007-02-07 15:34:02 -0500627 int value; /* current setting */
628 int valid; /* Has ever been set */
629 int debug; /* active only in debug mode ? */
Lucas De Marchic8440332011-03-17 17:18:22 -0300630 struct device_attribute devattr; /* sysfs attribute */
Stelian Pop7f09c432007-01-13 23:04:31 +0100631};
632
malattia@linux.it59b19102007-04-09 10:19:04 +0200633#define SNC_HANDLE_NAMES(_name, _values...) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100634 static char *snc_##_name[] = { _values, NULL }
635
malattia@linux.it59b19102007-04-09 10:19:04 +0200636#define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100637 { \
638 .name = __stringify(_name), \
639 .acpiget = _getters, \
640 .acpiset = _setters, \
Mattia Dongili156c2212007-02-12 22:01:07 +0100641 .validate = _validate, \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100642 .debug = _debug, \
malattia@linux.it59b19102007-04-09 10:19:04 +0200643 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100644 }
645
malattia@linux.it59b19102007-04-09 10:19:04 +0200646#define SNC_HANDLE_NULL { .name = NULL }
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100647
malattia@linux.it59b19102007-04-09 10:19:04 +0200648SNC_HANDLE_NAMES(fnkey_get, "GHKE");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100649
malattia@linux.it59b19102007-04-09 10:19:04 +0200650SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
651SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100652
malattia@linux.it59b19102007-04-09 10:19:04 +0200653SNC_HANDLE_NAMES(cdpower_get, "GCDP");
654SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100655
malattia@linux.it59b19102007-04-09 10:19:04 +0200656SNC_HANDLE_NAMES(audiopower_get, "GAZP");
657SNC_HANDLE_NAMES(audiopower_set, "AZPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100658
malattia@linux.it59b19102007-04-09 10:19:04 +0200659SNC_HANDLE_NAMES(lanpower_get, "GLNP");
660SNC_HANDLE_NAMES(lanpower_set, "LNPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100661
Mattia Dongili044847e2007-07-16 02:34:33 +0900662SNC_HANDLE_NAMES(lidstate_get, "GLID");
663
664SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
665SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
666
667SNC_HANDLE_NAMES(gainbass_get, "GMGB");
668SNC_HANDLE_NAMES(gainbass_set, "CMGB");
669
malattia@linux.it59b19102007-04-09 10:19:04 +0200670SNC_HANDLE_NAMES(PID_get, "GPID");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100671
malattia@linux.it59b19102007-04-09 10:19:04 +0200672SNC_HANDLE_NAMES(CTR_get, "GCTR");
673SNC_HANDLE_NAMES(CTR_set, "SCTR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100674
malattia@linux.it59b19102007-04-09 10:19:04 +0200675SNC_HANDLE_NAMES(PCR_get, "GPCR");
676SNC_HANDLE_NAMES(PCR_set, "SPCR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100677
malattia@linux.it59b19102007-04-09 10:19:04 +0200678SNC_HANDLE_NAMES(CMI_get, "GCMI");
679SNC_HANDLE_NAMES(CMI_set, "SCMI");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100680
malattia@linux.it59b19102007-04-09 10:19:04 +0200681static struct sony_nc_value sony_nc_values[] = {
682 SNC_HANDLE(brightness_default, snc_brightness_def_get,
Mattia Dongili156c2212007-02-12 22:01:07 +0100683 snc_brightness_def_set, brightness_default_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200684 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
685 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
686 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100687 boolean_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200688 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100689 boolean_validate, 1),
Mattia Dongili044847e2007-07-16 02:34:33 +0900690 SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
691 boolean_validate, 0),
692 SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
693 boolean_validate, 0),
694 SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
695 boolean_validate, 0),
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100696 /* unknown methods */
malattia@linux.it59b19102007-04-09 10:19:04 +0200697 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
698 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
699 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
700 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
701 SNC_HANDLE_NULL
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100702};
703
malattia@linux.it59b19102007-04-09 10:19:04 +0200704static acpi_handle sony_nc_acpi_handle;
705static struct acpi_device *sony_nc_acpi_device = NULL;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100706
Mattia Dongilid78865c2007-02-07 20:01:56 +0100707/*
708 * acpi_evaluate_object wrappers
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900709 * all useful calls into SNC methods take one or zero parameters and return
710 * integers or arrays.
Mattia Dongilid78865c2007-02-07 20:01:56 +0100711 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900712static union acpi_object *__call_snc_method(acpi_handle handle, char *method,
713 u64 *value)
Stelian Pop7f09c432007-01-13 23:04:31 +0100714{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900715 union acpi_object *result = NULL;
716 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
Stelian Pop7f09c432007-01-13 23:04:31 +0100717 acpi_status status;
718
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900719 if (value) {
720 struct acpi_object_list params;
721 union acpi_object in;
722 in.type = ACPI_TYPE_INTEGER;
723 in.integer.value = *value;
724 params.count = 1;
725 params.pointer = &in;
726 status = acpi_evaluate_object(handle, method, &params, &output);
Mattia Dongiliae188712012-05-19 22:35:49 +0900727 dprintk("__call_snc_method: [%s:0x%.8x%.8x]\n", method,
728 (unsigned int)(*value >> 32),
729 (unsigned int)*value & 0xffffffff);
730 } else {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900731 status = acpi_evaluate_object(handle, method, NULL, &output);
Mattia Dongiliae188712012-05-19 22:35:49 +0900732 dprintk("__call_snc_method: [%s]\n", method);
733 }
Stelian Pop7f09c432007-01-13 23:04:31 +0100734
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900735 if (ACPI_FAILURE(status)) {
736 pr_err("Failed to evaluate [%s]\n", method);
737 return NULL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100738 }
739
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900740 result = (union acpi_object *) output.pointer;
741 if (!result)
742 dprintk("No return object [%s]\n", method);
Stelian Pop7f09c432007-01-13 23:04:31 +0100743
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900744 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +0100745}
746
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900747static int sony_nc_int_call(acpi_handle handle, char *name, int *value,
748 int *result)
Stelian Pop7f09c432007-01-13 23:04:31 +0100749{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900750 union acpi_object *object = NULL;
751 if (value) {
752 u64 v = *value;
753 object = __call_snc_method(handle, name, &v);
754 } else
755 object = __call_snc_method(handle, name, NULL);
Stelian Pop7f09c432007-01-13 23:04:31 +0100756
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900757 if (!object)
758 return -EINVAL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100759
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900760 if (object->type != ACPI_TYPE_INTEGER) {
761 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
762 ACPI_TYPE_INTEGER, object->type);
763 kfree(object);
764 return -EINVAL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100765 }
766
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900767 if (result)
768 *result = object->integer.value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100769
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900770 kfree(object);
771 return 0;
772}
773
774#define MIN(a, b) (a > b ? b : a)
775static int sony_nc_buffer_call(acpi_handle handle, char *name, u64 *value,
776 void *buffer, size_t buflen)
777{
778 size_t len = len;
779 union acpi_object *object = __call_snc_method(handle, name, value);
780
781 if (!object)
782 return -EINVAL;
783
784 if (object->type == ACPI_TYPE_BUFFER)
785 len = MIN(buflen, object->buffer.length);
786
787 else if (object->type == ACPI_TYPE_INTEGER)
788 len = MIN(buflen, sizeof(object->integer.value));
789
790 else {
791 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
792 ACPI_TYPE_BUFFER, object->type);
793 kfree(object);
794 return -EINVAL;
795 }
796
797 memcpy(buffer, object->buffer.pointer, len);
798 kfree(object);
799 return 0;
Stelian Pop7f09c432007-01-13 23:04:31 +0100800}
801
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900802struct sony_nc_handles {
803 u16 cap[0x10];
804 struct device_attribute devattr;
805};
806
Dan Carpenterf11113b2011-02-26 15:54:27 +0300807static struct sony_nc_handles *handles;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900808
809static ssize_t sony_nc_handles_show(struct device *dev,
810 struct device_attribute *attr, char *buffer)
811{
812 ssize_t len = 0;
813 int i;
814
815 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
816 len += snprintf(buffer + len, PAGE_SIZE - len, "0x%.4x ",
817 handles->cap[i]);
818 }
819 len += snprintf(buffer + len, PAGE_SIZE - len, "\n");
820
821 return len;
822}
823
824static int sony_nc_handles_setup(struct platform_device *pd)
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900825{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900826 int i, r, result, arg;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900827
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900828 handles = kzalloc(sizeof(*handles), GFP_KERNEL);
Dan Carpenter31f00752011-02-26 15:55:24 +0300829 if (!handles)
830 return -ENOMEM;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900831
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900832 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900833 arg = i + 0x20;
834 r = sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg,
835 &result);
836 if (!r) {
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900837 dprintk("caching handle 0x%.4x (offset: 0x%.2x)\n",
838 result, i);
839 handles->cap[i] = result;
Mattia Dongili56e6e712011-02-19 11:52:25 +0900840 }
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900841 }
842
Mattia Dongili855b8bc2011-04-05 23:38:35 +0900843 if (debug) {
844 sysfs_attr_init(&handles->devattr.attr);
845 handles->devattr.attr.name = "handles";
846 handles->devattr.attr.mode = S_IRUGO;
847 handles->devattr.show = sony_nc_handles_show;
848
849 /* allow reading capabilities via sysfs */
850 if (device_create_file(&pd->dev, &handles->devattr)) {
851 kfree(handles);
852 handles = NULL;
853 return -1;
854 }
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900855 }
856
857 return 0;
858}
859
860static int sony_nc_handles_cleanup(struct platform_device *pd)
861{
862 if (handles) {
Mattia Dongili855b8bc2011-04-05 23:38:35 +0900863 if (debug)
864 device_remove_file(&pd->dev, &handles->devattr);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900865 kfree(handles);
866 handles = NULL;
867 }
868 return 0;
869}
870
871static int sony_find_snc_handle(int handle)
872{
873 int i;
Mattia Dongilifef34862011-04-02 19:00:44 +0900874
875 /* not initialized yet, return early */
Mattia Dongilibab70842012-05-19 22:35:52 +0900876 if (!handles || !handle)
877 return -EINVAL;
Mattia Dongilifef34862011-04-02 19:00:44 +0900878
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900879 for (i = 0; i < 0x10; i++) {
880 if (handles->cap[i] == handle) {
881 dprintk("found handle 0x%.4x (offset: 0x%.2x)\n",
882 handle, i);
883 return i;
884 }
885 }
Mattia Dongili56e6e712011-02-19 11:52:25 +0900886 dprintk("handle 0x%.4x not found\n", handle);
Mattia Dongilibab70842012-05-19 22:35:52 +0900887 return -EINVAL;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900888}
889
890static int sony_call_snc_handle(int handle, int argument, int *result)
891{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900892 int arg, ret = 0;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900893 int offset = sony_find_snc_handle(handle);
894
895 if (offset < 0)
Mattia Dongilibab70842012-05-19 22:35:52 +0900896 return offset;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900897
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900898 arg = offset | argument;
899 ret = sony_nc_int_call(sony_nc_acpi_handle, "SN07", &arg, result);
900 dprintk("called SN07 with 0x%.4x (result: 0x%.4x)\n", arg, *result);
Mattia Dongili56e6e712011-02-19 11:52:25 +0900901 return ret;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900902}
903
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100904/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200905 * sony_nc_values input/output validate functions
Mattia Dongili156c2212007-02-12 22:01:07 +0100906 */
907
908/* brightness_default_validate:
909 *
910 * manipulate input output values to keep consistency with the
911 * backlight framework for which brightness values are 0-based.
912 */
913static int brightness_default_validate(const int direction, const int value)
914{
915 switch (direction) {
916 case SNC_VALIDATE_OUT:
917 return value - 1;
918 case SNC_VALIDATE_IN:
919 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
920 return value + 1;
921 }
922 return -EINVAL;
923}
924
925/* boolean_validate:
926 *
927 * on input validate boolean values 0/1, on output just pass the
928 * received value.
929 */
930static int boolean_validate(const int direction, const int value)
931{
932 if (direction == SNC_VALIDATE_IN) {
933 if (value != 0 && value != 1)
934 return -EINVAL;
935 }
936 return value;
937}
938
939/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200940 * Sysfs show/store common to all sony_nc_values
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100941 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200942static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
Len Browna02d1c12007-02-07 15:34:02 -0500943 char *buffer)
Stelian Pop7f09c432007-01-13 23:04:31 +0100944{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900945 int value, ret = 0;
malattia@linux.it59b19102007-04-09 10:19:04 +0200946 struct sony_nc_value *item =
947 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100948
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100949 if (!*item->acpiget)
Stelian Pop7f09c432007-01-13 23:04:31 +0100950 return -EIO;
951
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900952 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiget, NULL,
953 &value);
954 if (ret < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100955 return -EIO;
956
Mattia Dongili156c2212007-02-12 22:01:07 +0100957 if (item->validate)
958 value = item->validate(SNC_VALIDATE_OUT, value);
959
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100960 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
Stelian Pop7f09c432007-01-13 23:04:31 +0100961}
962
malattia@linux.it59b19102007-04-09 10:19:04 +0200963static ssize_t sony_nc_sysfs_store(struct device *dev,
Len Browna02d1c12007-02-07 15:34:02 -0500964 struct device_attribute *attr,
965 const char *buffer, size_t count)
Stelian Pop7f09c432007-01-13 23:04:31 +0100966{
Mattia Dongili9e123372012-05-19 22:35:47 +0900967 unsigned long value = 0;
968 int ret = 0;
malattia@linux.it59b19102007-04-09 10:19:04 +0200969 struct sony_nc_value *item =
970 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100971
972 if (!item->acpiset)
973 return -EIO;
974
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100975 if (count > 31)
976 return -EINVAL;
977
Mattia Dongili9e123372012-05-19 22:35:47 +0900978 if (kstrtoul(buffer, 10, &value))
979 return -EINVAL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100980
Mattia Dongili156c2212007-02-12 22:01:07 +0100981 if (item->validate)
982 value = item->validate(SNC_VALIDATE_IN, value);
983
984 if (value < 0)
985 return value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100986
Mattia Dongili9e123372012-05-19 22:35:47 +0900987 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
988 (int *)&value, NULL);
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900989 if (ret < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100990 return -EIO;
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900991
Andrew Morton3f4f4612007-01-13 23:04:32 +0100992 item->value = value;
993 item->valid = 1;
Stelian Pop7f09c432007-01-13 23:04:31 +0100994 return count;
995}
996
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100997
Mattia Dongilid78865c2007-02-07 20:01:56 +0100998/*
999 * Backlight device
1000 */
Mattia Dongili62d2f232011-05-09 10:20:29 -04001001struct sony_backlight_props {
1002 struct backlight_device *dev;
1003 int handle;
1004 u8 offset;
1005 u8 maxlvl;
1006};
1007struct sony_backlight_props sony_bl_props;
1008
Mattia Dongilid78865c2007-02-07 20:01:56 +01001009static int sony_backlight_update_status(struct backlight_device *bd)
Andrew Morton3f4f4612007-01-13 23:04:32 +01001010{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001011 int arg = bd->props.brightness + 1;
1012 return sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &arg, NULL);
Andrew Morton3f4f4612007-01-13 23:04:32 +01001013}
1014
Mattia Dongilid78865c2007-02-07 20:01:56 +01001015static int sony_backlight_get_brightness(struct backlight_device *bd)
1016{
1017 int value;
1018
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001019 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL, &value))
Mattia Dongilid78865c2007-02-07 20:01:56 +01001020 return 0;
1021 /* brightness levels are 1-based, while backlight ones are 0-based */
1022 return value - 1;
1023}
1024
Mattia Dongili7751ab82011-02-19 11:52:32 +09001025static int sony_nc_get_brightness_ng(struct backlight_device *bd)
1026{
1027 int result;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001028 struct sony_backlight_props *sdev =
1029 (struct sony_backlight_props *)bl_get_data(bd);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001030
Mattia Dongili62d2f232011-05-09 10:20:29 -04001031 sony_call_snc_handle(sdev->handle, 0x0200, &result);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001032
Mattia Dongili62d2f232011-05-09 10:20:29 -04001033 return (result & 0xff) - sdev->offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001034}
1035
1036static int sony_nc_update_status_ng(struct backlight_device *bd)
1037{
1038 int value, result;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001039 struct sony_backlight_props *sdev =
1040 (struct sony_backlight_props *)bl_get_data(bd);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001041
Mattia Dongili62d2f232011-05-09 10:20:29 -04001042 value = bd->props.brightness + sdev->offset;
1043 if (sony_call_snc_handle(sdev->handle, 0x0100 | (value << 16), &result))
Mattia Dongili6192fa72011-04-05 23:38:36 +09001044 return -EIO;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001045
Mattia Dongili6192fa72011-04-05 23:38:36 +09001046 return value;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001047}
1048
Lionel Debrouxacc24722010-11-16 14:14:02 +01001049static const struct backlight_ops sony_backlight_ops = {
Mattia Dongili7751ab82011-02-19 11:52:32 +09001050 .options = BL_CORE_SUSPENDRESUME,
Len Browna02d1c12007-02-07 15:34:02 -05001051 .update_status = sony_backlight_update_status,
1052 .get_brightness = sony_backlight_get_brightness,
Mattia Dongilid78865c2007-02-07 20:01:56 +01001053};
Mattia Dongili7751ab82011-02-19 11:52:32 +09001054static const struct backlight_ops sony_backlight_ng_ops = {
1055 .options = BL_CORE_SUSPENDRESUME,
1056 .update_status = sony_nc_update_status_ng,
1057 .get_brightness = sony_nc_get_brightness_ng,
1058};
Mattia Dongilid78865c2007-02-07 20:01:56 +01001059
1060/*
Mattia Dongili6315fd12007-07-16 02:34:35 +09001061 * New SNC-only Vaios event mapping to driver known keys
1062 */
1063struct sony_nc_event {
1064 u8 data;
1065 u8 event;
1066};
1067
Matthew Garrett45c79422009-03-26 21:58:16 +09001068static struct sony_nc_event sony_100_events[] = {
Matthew Garrett9b578962009-03-26 21:58:14 +09001069 { 0x90, SONYPI_EVENT_PKEY_P1 },
1070 { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthias Welwarsky6479efb2009-04-01 22:10:45 +09001071 { 0x91, SONYPI_EVENT_PKEY_P2 },
Matthew Garrett9b578962009-03-26 21:58:14 +09001072 { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001073 { 0x81, SONYPI_EVENT_FNKEY_F1 },
1074 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
Anton Veretenenkof5acf5e2009-03-26 22:44:26 +09001075 { 0x82, SONYPI_EVENT_FNKEY_F2 },
1076 { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
1077 { 0x83, SONYPI_EVENT_FNKEY_F3 },
1078 { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
1079 { 0x84, SONYPI_EVENT_FNKEY_F4 },
1080 { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001081 { 0x85, SONYPI_EVENT_FNKEY_F5 },
1082 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
1083 { 0x86, SONYPI_EVENT_FNKEY_F6 },
1084 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
1085 { 0x87, SONYPI_EVENT_FNKEY_F7 },
1086 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +09001087 { 0x89, SONYPI_EVENT_FNKEY_F9 },
1088 { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001089 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
1090 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
1091 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
1092 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +09001093 { 0x9d, SONYPI_EVENT_ZOOM_PRESSED },
1094 { 0x1d, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +09001095 { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
1096 { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili4f924ba2009-12-17 00:08:33 +09001097 { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
1098 { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +09001099 { 0xa4, SONYPI_EVENT_CD_EJECT_PRESSED },
1100 { 0x24, SONYPI_EVENT_ANYBUTTON_RELEASED },
1101 { 0xa5, SONYPI_EVENT_VENDOR_PRESSED },
1102 { 0x25, SONYPI_EVENT_ANYBUTTON_RELEASED },
1103 { 0xa6, SONYPI_EVENT_HELP_PRESSED },
1104 { 0x26, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001105 { 0, 0 },
1106};
1107
Matthew Garrett45c79422009-03-26 21:58:16 +09001108static struct sony_nc_event sony_127_events[] = {
1109 { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
1110 { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
1111 { 0x82, SONYPI_EVENT_PKEY_P1 },
1112 { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
1113 { 0x83, SONYPI_EVENT_PKEY_P2 },
1114 { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
1115 { 0x84, SONYPI_EVENT_PKEY_P3 },
1116 { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
1117 { 0x85, SONYPI_EVENT_PKEY_P4 },
1118 { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
1119 { 0x86, SONYPI_EVENT_PKEY_P5 },
1120 { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett45c79422009-03-26 21:58:16 +09001121 { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
1122 { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
1123 { 0, 0 },
1124};
1125
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001126static int sony_nc_hotkeys_decode(u32 event, unsigned int handle)
1127{
1128 int ret = -EINVAL;
1129 unsigned int result = 0;
1130 struct sony_nc_event *key_event;
1131
1132 if (sony_call_snc_handle(handle, 0x200, &result)) {
1133 dprintk("Unable to decode event 0x%.2x 0x%.2x\n", handle,
1134 event);
1135 return -EINVAL;
1136 }
1137
1138 result &= 0xFF;
1139
1140 if (handle == 0x0100)
1141 key_event = sony_100_events;
1142 else
1143 key_event = sony_127_events;
1144
1145 for (; key_event->data; key_event++) {
1146 if (key_event->data == result) {
1147 ret = key_event->event;
1148 break;
1149 }
1150 }
1151
1152 if (!key_event->data)
1153 pr_info("Unknown hotkey 0x%.2x/0x%.2x (handle 0x%.2x)\n",
1154 event, result, handle);
1155
1156 return ret;
1157}
1158
Mattia Dongili6315fd12007-07-16 02:34:35 +09001159/*
Mattia Dongilid78865c2007-02-07 20:01:56 +01001160 * ACPI callbacks
1161 */
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001162static void sony_nc_notify(struct acpi_device *device, u32 event)
Stelian Pop7f09c432007-01-13 23:04:31 +01001163{
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001164 u32 real_ev = event;
1165 u8 ev_type = 0;
1166 dprintk("sony_nc_notify, event: 0x%.2x\n", event);
Mattia Dongili6315fd12007-07-16 02:34:35 +09001167
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001168 if (event >= 0x90) {
1169 unsigned int result = 0;
1170 unsigned int arg = 0;
1171 unsigned int handle = 0;
1172 unsigned int offset = event - 0x90;
Mattia Dongili6315fd12007-07-16 02:34:35 +09001173
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001174 if (offset >= ARRAY_SIZE(handles->cap)) {
1175 pr_err("Event 0x%x outside of capabilities list\n",
1176 event);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001177 return;
Matthew Garrett9b578962009-03-26 21:58:14 +09001178 }
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001179 handle = handles->cap[offset];
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001180
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001181 /* list of handles known for generating events */
1182 switch (handle) {
1183 /* hotkey event */
1184 case 0x0100:
1185 case 0x0127:
1186 ev_type = 1;
1187 real_ev = sony_nc_hotkeys_decode(event, handle);
1188
1189 if (real_ev > 0)
1190 sony_laptop_report_input_event(real_ev);
1191 else
1192 /* restore the original event for reporting */
1193 real_ev = event;
1194
1195 break;
1196
1197 /* wlan switch */
1198 case 0x0124:
1199 case 0x0135:
1200 /* events on this handle are reported when the
1201 * switch changes position or for battery
1202 * events. We'll notify both of them but only
1203 * update the rfkill device status when the
1204 * switch is moved.
1205 */
1206 ev_type = 2;
1207 sony_call_snc_handle(handle, 0x0100, &result);
1208 real_ev = result & 0x03;
1209
1210 /* hw switch event */
1211 if (real_ev == 1)
1212 sony_nc_rfkill_update();
1213
1214 break;
1215
1216 default:
1217 dprintk("Unknown event 0x%x for handle 0x%x\n",
1218 event, handle);
1219 break;
1220 }
1221
1222 /* clear the event (and the event reason when present) */
1223 arg = 1 << offset;
1224 sony_nc_int_call(sony_nc_acpi_handle, "SN05", &arg, &result);
1225
1226 } else {
1227 /* old style event */
1228 ev_type = 1;
1229 sony_laptop_report_input_event(real_ev);
1230 }
1231
1232 acpi_bus_generate_proc_event(sony_nc_acpi_device, ev_type, real_ev);
1233
1234 acpi_bus_generate_netlink_event(sony_nc_acpi_device->pnp.device_class,
1235 dev_name(&sony_nc_acpi_device->dev), ev_type, real_ev);
Stelian Pop7f09c432007-01-13 23:04:31 +01001236}
1237
1238static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
1239 void *context, void **return_value)
1240{
Lin Ming30823732008-12-16 16:59:35 +08001241 struct acpi_device_info *info;
Stelian Pop7f09c432007-01-13 23:04:31 +01001242
Bob Moore15b8dd52009-06-29 13:39:29 +08001243 if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
Joe Perches50f581a2011-03-29 15:21:48 -07001244 pr_warn("method: name: %4.4s, args %X\n",
Lin Ming30823732008-12-16 16:59:35 +08001245 (char *)&info->name, info->param_count);
1246
Bob Moore15b8dd52009-06-29 13:39:29 +08001247 kfree(info);
Lin Ming30823732008-12-16 16:59:35 +08001248 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001249
1250 return AE_OK;
1251}
1252
Mattia Dongilid78865c2007-02-07 20:01:56 +01001253/*
1254 * ACPI device
1255 */
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001256static void sony_nc_function_setup(struct acpi_device *device,
1257 struct platform_device *pf_device)
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001258{
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001259 unsigned int i, result, bitmask, arg;
1260
1261 if (!handles)
1262 return;
1263
1264 /* setup found handles here */
1265 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1266 unsigned int handle = handles->cap[i];
1267
1268 if (!handle)
1269 continue;
1270
1271 dprintk("setting up handle 0x%.4x\n", handle);
1272
1273 switch (handle) {
1274 case 0x0100:
1275 case 0x0101:
1276 case 0x0127:
1277 /* setup hotkeys */
1278 sony_call_snc_handle(handle, 0, &result);
1279 break;
1280 case 0x0102:
1281 /* setup hotkeys */
1282 sony_call_snc_handle(handle, 0x100, &result);
1283 break;
Marco Chiappero967145a2012-05-19 22:35:50 +09001284 case 0x0115:
1285 case 0x0136:
1286 case 0x013f:
1287 result = sony_nc_battery_care_setup(pf_device, handle);
1288 if (result)
1289 pr_err("couldn't set up battery care function (%d)\n",
1290 result);
1291 break;
Marco Chiappero54535d02012-05-19 22:35:54 +09001292 case 0x0119:
1293 result = sony_nc_lid_resume_setup(pf_device);
1294 if (result)
1295 pr_err("couldn't set up lid resume function (%d)\n",
1296 result);
1297 break;
Marco Chiappero49f000a2012-05-19 22:35:51 +09001298 case 0x0122:
1299 result = sony_nc_thermal_setup(pf_device);
1300 if (result)
1301 pr_err("couldn't set up thermal profile function (%d)\n",
1302 result);
1303 break;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001304 case 0x0124:
1305 case 0x0135:
1306 sony_nc_rfkill_setup(device);
1307 break;
1308 case 0x0137:
1309 sony_nc_kbd_backlight_setup(pf_device);
1310 break;
1311 default:
1312 continue;
1313 }
1314 }
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001315
1316 /* Enable all events */
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001317 arg = 0x10;
1318 if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1319 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1320 &result);
1321}
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001322
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001323static void sony_nc_function_cleanup(struct platform_device *pd)
1324{
1325 unsigned int i, result, bitmask, handle;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001326
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001327 /* get enabled events and disable them */
1328 sony_nc_int_call(sony_nc_acpi_handle, "SN01", NULL, &bitmask);
1329 sony_nc_int_call(sony_nc_acpi_handle, "SN03", &bitmask, &result);
1330
1331 /* cleanup handles here */
1332 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1333
1334 handle = handles->cap[i];
1335
1336 if (!handle)
1337 continue;
1338
1339 switch (handle) {
Marco Chiappero967145a2012-05-19 22:35:50 +09001340 case 0x0115:
1341 case 0x0136:
1342 case 0x013f:
1343 sony_nc_battery_care_cleanup(pd);
1344 break;
Marco Chiappero54535d02012-05-19 22:35:54 +09001345 case 0x0119:
1346 sony_nc_lid_resume_cleanup(pd);
1347 break;
Marco Chiappero49f000a2012-05-19 22:35:51 +09001348 case 0x0122:
1349 sony_nc_thermal_cleanup(pd);
1350 break;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001351 case 0x0124:
1352 case 0x0135:
1353 sony_nc_rfkill_cleanup();
1354 break;
1355 case 0x0137:
1356 sony_nc_kbd_backlight_cleanup(pd);
1357 break;
1358 default:
1359 continue;
1360 }
1361 }
1362
1363 /* finally cleanup the handles list */
1364 sony_nc_handles_cleanup(pd);
1365}
1366
1367static void sony_nc_function_resume(void)
1368{
1369 unsigned int i, result, bitmask, arg;
1370
1371 dprintk("Resuming SNC device\n");
1372
1373 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1374 unsigned int handle = handles->cap[i];
1375
1376 if (!handle)
1377 continue;
1378
1379 switch (handle) {
1380 case 0x0100:
1381 case 0x0101:
1382 case 0x0127:
1383 /* re-enable hotkeys */
1384 sony_call_snc_handle(handle, 0, &result);
1385 break;
1386 case 0x0102:
1387 /* re-enable hotkeys */
1388 sony_call_snc_handle(handle, 0x100, &result);
1389 break;
Marco Chiappero49f000a2012-05-19 22:35:51 +09001390 case 0x0122:
1391 sony_nc_thermal_resume();
1392 break;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001393 case 0x0124:
1394 case 0x0135:
1395 sony_nc_rfkill_update();
1396 break;
1397 case 0x0137:
1398 sony_nc_kbd_backlight_resume();
1399 break;
1400 default:
1401 continue;
1402 }
1403 }
1404
1405 /* Enable all events */
1406 arg = 0x10;
1407 if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1408 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1409 &result);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001410}
1411
malattia@linux.it59b19102007-04-09 10:19:04 +02001412static int sony_nc_resume(struct acpi_device *device)
Mattia Dongilid78865c2007-02-07 20:01:56 +01001413{
malattia@linux.it59b19102007-04-09 10:19:04 +02001414 struct sony_nc_value *item;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001415 acpi_handle handle;
Mattia Dongilid78865c2007-02-07 20:01:56 +01001416
malattia@linux.it59b19102007-04-09 10:19:04 +02001417 for (item = sony_nc_values; item->name; item++) {
Mattia Dongilid78865c2007-02-07 20:01:56 +01001418 int ret;
1419
1420 if (!item->valid)
1421 continue;
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001422 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
1423 &item->value, NULL);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001424 if (ret < 0) {
Joe Perches50f581a2011-03-29 15:21:48 -07001425 pr_err("%s: %d\n", __func__, ret);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001426 break;
1427 }
1428 }
Mattia Dongili6315fd12007-07-16 02:34:35 +09001429
Matthew Garrett82734bf2009-03-26 21:58:13 +09001430 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1431 &handle))) {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001432 int arg = 1;
1433 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
Matthew Garrett82734bf2009-03-26 21:58:13 +09001434 dprintk("ECON Method failed\n");
1435 }
1436
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001437 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001438 &handle)))
1439 sony_nc_function_resume();
Marco Chiapperodf410d52011-04-05 23:38:34 +09001440
Mattia Dongilid78865c2007-02-07 20:01:56 +01001441 return 0;
1442}
1443
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001444static void sony_nc_rfkill_cleanup(void)
1445{
1446 int i;
1447
Johannes Berg19d337d2009-06-02 13:01:37 +02001448 for (i = 0; i < N_SONY_RFKILL; i++) {
1449 if (sony_rfkill_devices[i]) {
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001450 rfkill_unregister(sony_rfkill_devices[i]);
Johannes Berg19d337d2009-06-02 13:01:37 +02001451 rfkill_destroy(sony_rfkill_devices[i]);
1452 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001453 }
1454}
1455
Johannes Berg19d337d2009-06-02 13:01:37 +02001456static int sony_nc_rfkill_set(void *data, bool blocked)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001457{
1458 int result;
1459 int argument = sony_rfkill_address[(long) data] + 0x100;
1460
Johannes Berg19d337d2009-06-02 13:01:37 +02001461 if (!blocked)
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001462 argument |= 0x030000;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001463
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001464 return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001465}
1466
Johannes Berg19d337d2009-06-02 13:01:37 +02001467static const struct rfkill_ops sony_rfkill_ops = {
1468 .set_block = sony_nc_rfkill_set,
1469};
1470
1471static int sony_nc_setup_rfkill(struct acpi_device *device,
1472 enum sony_nc_rfkill nc_type)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001473{
1474 int err = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001475 struct rfkill *rfk;
1476 enum rfkill_type type;
1477 const char *name;
Alan Jenkins50fab072009-09-24 20:15:24 +01001478 int result;
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001479 bool hwblock, swblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001480
Johannes Berg19d337d2009-06-02 13:01:37 +02001481 switch (nc_type) {
1482 case SONY_WIFI:
1483 type = RFKILL_TYPE_WLAN;
1484 name = "sony-wifi";
1485 break;
1486 case SONY_BLUETOOTH:
1487 type = RFKILL_TYPE_BLUETOOTH;
1488 name = "sony-bluetooth";
1489 break;
1490 case SONY_WWAN:
1491 type = RFKILL_TYPE_WWAN;
1492 name = "sony-wwan";
1493 break;
1494 case SONY_WIMAX:
1495 type = RFKILL_TYPE_WIMAX;
1496 name = "sony-wimax";
1497 break;
1498 default:
1499 return -EINVAL;
Mattia Dongili53005a02009-04-12 11:26:31 +00001500 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001501
Johannes Berg19d337d2009-06-02 13:01:37 +02001502 rfk = rfkill_alloc(name, &device->dev, type,
1503 &sony_rfkill_ops, (void *)nc_type);
1504 if (!rfk)
1505 return -ENOMEM;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001506
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001507 if (sony_call_snc_handle(sony_rfkill_handle, 0x200, &result) < 0) {
1508 rfkill_destroy(rfk);
1509 return -1;
1510 }
Alan Jenkins50fab072009-09-24 20:15:24 +01001511 hwblock = !(result & 0x1);
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001512
1513 if (sony_call_snc_handle(sony_rfkill_handle,
1514 sony_rfkill_address[nc_type],
1515 &result) < 0) {
1516 rfkill_destroy(rfk);
1517 return -1;
1518 }
1519 swblock = !(result & 0x2);
1520
1521 rfkill_init_sw_state(rfk, swblock);
Alan Jenkins50fab072009-09-24 20:15:24 +01001522 rfkill_set_hw_state(rfk, hwblock);
1523
Johannes Berg19d337d2009-06-02 13:01:37 +02001524 err = rfkill_register(rfk);
1525 if (err) {
1526 rfkill_destroy(rfk);
1527 return err;
Mattia Dongili53005a02009-04-12 11:26:31 +00001528 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001529 sony_rfkill_devices[nc_type] = rfk;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001530 return err;
1531}
1532
Randy Dunlapa46a7802011-01-08 19:56:44 -08001533static void sony_nc_rfkill_update(void)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001534{
Johannes Berg19d337d2009-06-02 13:01:37 +02001535 enum sony_nc_rfkill i;
1536 int result;
1537 bool hwblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001538
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001539 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001540 hwblock = !(result & 0x1);
1541
1542 for (i = 0; i < N_SONY_RFKILL; i++) {
1543 int argument = sony_rfkill_address[i];
1544
1545 if (!sony_rfkill_devices[i])
1546 continue;
1547
1548 if (hwblock) {
Johannes Berge1f8a192009-06-11 12:08:15 +02001549 if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
1550 /* we already know we're blocked */
1551 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001552 continue;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001553 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001554
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001555 sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001556 rfkill_set_states(sony_rfkill_devices[i],
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001557 !(result & 0x2), false);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001558 }
1559}
1560
Mattia Dongili528809c2009-12-17 00:08:36 +09001561static void sony_nc_rfkill_setup(struct acpi_device *device)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001562{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001563 u64 offset;
1564 int i;
1565 unsigned char buffer[32] = { 0 };
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001566
Mattia Dongili528809c2009-12-17 00:08:36 +09001567 offset = sony_find_snc_handle(0x124);
1568 if (offset == -1) {
1569 offset = sony_find_snc_handle(0x135);
1570 if (offset == -1)
1571 return;
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001572 else
1573 sony_rfkill_handle = 0x135;
1574 } else
1575 sony_rfkill_handle = 0x124;
Mattia Dongili528809c2009-12-17 00:08:36 +09001576 dprintk("Found rkfill handle: 0x%.4x\n", sony_rfkill_handle);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001577
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001578 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
1579 32);
1580 if (i < 0)
Mattia Dongili528809c2009-12-17 00:08:36 +09001581 return;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001582
Mattia Dongili528809c2009-12-17 00:08:36 +09001583 /* the buffer is filled with magic numbers describing the devices
1584 * available, 0xff terminates the enumeration
1585 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001586 for (i = 0; i < ARRAY_SIZE(buffer); i++) {
Dmitry Torokhovc14973f2010-01-10 00:15:44 -08001587
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001588 if (buffer[i] == 0xff)
Dmitry Torokhovc14973f2010-01-10 00:15:44 -08001589 break;
1590
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001591 dprintk("Radio devices, looking at 0x%.2x\n", buffer[i]);
Mattia Dongili528809c2009-12-17 00:08:36 +09001592
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001593 if (buffer[i] == 0 && !sony_rfkill_devices[SONY_WIFI])
Mattia Dongili528809c2009-12-17 00:08:36 +09001594 sony_nc_setup_rfkill(device, SONY_WIFI);
1595
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001596 if (buffer[i] == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
Mattia Dongili528809c2009-12-17 00:08:36 +09001597 sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
1598
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001599 if ((0xf0 & buffer[i]) == 0x20 &&
Mattia Dongili528809c2009-12-17 00:08:36 +09001600 !sony_rfkill_devices[SONY_WWAN])
1601 sony_nc_setup_rfkill(device, SONY_WWAN);
1602
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001603 if (buffer[i] == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
Mattia Dongili528809c2009-12-17 00:08:36 +09001604 sony_nc_setup_rfkill(device, SONY_WIMAX);
1605 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001606}
1607
Mattia Dongilibf155712011-02-19 11:52:31 +09001608/* Keyboard backlight feature */
1609#define KBDBL_HANDLER 0x137
1610#define KBDBL_PRESENT 0xB00
1611#define SET_MODE 0xC00
Marco Chiapperodf410d52011-04-05 23:38:34 +09001612#define SET_STATE 0xD00
Mattia Dongilibf155712011-02-19 11:52:31 +09001613#define SET_TIMEOUT 0xE00
1614
1615struct kbd_backlight {
1616 int mode;
1617 int timeout;
1618 struct device_attribute mode_attr;
1619 struct device_attribute timeout_attr;
1620};
1621
Dan Carpenterf11113b2011-02-26 15:54:27 +03001622static struct kbd_backlight *kbdbl_handle;
Mattia Dongilibf155712011-02-19 11:52:31 +09001623
1624static ssize_t __sony_nc_kbd_backlight_mode_set(u8 value)
1625{
1626 int result;
1627
1628 if (value > 1)
1629 return -EINVAL;
1630
1631 if (sony_call_snc_handle(KBDBL_HANDLER,
1632 (value << 0x10) | SET_MODE, &result))
1633 return -EIO;
1634
Marco Chiapperodf410d52011-04-05 23:38:34 +09001635 /* Try to turn the light on/off immediately */
1636 sony_call_snc_handle(KBDBL_HANDLER, (value << 0x10) | SET_STATE,
1637 &result);
1638
Mattia Dongilibf155712011-02-19 11:52:31 +09001639 kbdbl_handle->mode = value;
1640
1641 return 0;
1642}
1643
1644static ssize_t sony_nc_kbd_backlight_mode_store(struct device *dev,
1645 struct device_attribute *attr,
1646 const char *buffer, size_t count)
1647{
1648 int ret = 0;
1649 unsigned long value;
1650
1651 if (count > 31)
1652 return -EINVAL;
1653
Mattia Dongili9e123372012-05-19 22:35:47 +09001654 if (kstrtoul(buffer, 10, &value))
Mattia Dongilibf155712011-02-19 11:52:31 +09001655 return -EINVAL;
1656
1657 ret = __sony_nc_kbd_backlight_mode_set(value);
1658 if (ret < 0)
1659 return ret;
1660
1661 return count;
1662}
1663
1664static ssize_t sony_nc_kbd_backlight_mode_show(struct device *dev,
1665 struct device_attribute *attr, char *buffer)
1666{
1667 ssize_t count = 0;
1668 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_handle->mode);
1669 return count;
1670}
1671
1672static int __sony_nc_kbd_backlight_timeout_set(u8 value)
1673{
1674 int result;
1675
1676 if (value > 3)
1677 return -EINVAL;
1678
1679 if (sony_call_snc_handle(KBDBL_HANDLER,
1680 (value << 0x10) | SET_TIMEOUT, &result))
1681 return -EIO;
1682
1683 kbdbl_handle->timeout = value;
1684
1685 return 0;
1686}
1687
1688static ssize_t sony_nc_kbd_backlight_timeout_store(struct device *dev,
1689 struct device_attribute *attr,
1690 const char *buffer, size_t count)
1691{
1692 int ret = 0;
1693 unsigned long value;
1694
1695 if (count > 31)
1696 return -EINVAL;
1697
Mattia Dongili9e123372012-05-19 22:35:47 +09001698 if (kstrtoul(buffer, 10, &value))
Mattia Dongilibf155712011-02-19 11:52:31 +09001699 return -EINVAL;
1700
1701 ret = __sony_nc_kbd_backlight_timeout_set(value);
1702 if (ret < 0)
1703 return ret;
1704
1705 return count;
1706}
1707
1708static ssize_t sony_nc_kbd_backlight_timeout_show(struct device *dev,
1709 struct device_attribute *attr, char *buffer)
1710{
1711 ssize_t count = 0;
1712 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_handle->timeout);
1713 return count;
1714}
1715
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001716static void sony_nc_kbd_backlight_setup(struct platform_device *pd)
Mattia Dongilibf155712011-02-19 11:52:31 +09001717{
1718 int result;
1719
Marco Chiapperodf410d52011-04-05 23:38:34 +09001720 if (sony_call_snc_handle(KBDBL_HANDLER, KBDBL_PRESENT, &result))
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001721 return;
Mattia Dongilibf155712011-02-19 11:52:31 +09001722 if (!(result & 0x02))
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001723 return;
Mattia Dongilibf155712011-02-19 11:52:31 +09001724
1725 kbdbl_handle = kzalloc(sizeof(*kbdbl_handle), GFP_KERNEL);
Dan Carpenter31f00752011-02-26 15:55:24 +03001726 if (!kbdbl_handle)
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001727 return;
Mattia Dongilibf155712011-02-19 11:52:31 +09001728
1729 sysfs_attr_init(&kbdbl_handle->mode_attr.attr);
1730 kbdbl_handle->mode_attr.attr.name = "kbd_backlight";
1731 kbdbl_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
1732 kbdbl_handle->mode_attr.show = sony_nc_kbd_backlight_mode_show;
1733 kbdbl_handle->mode_attr.store = sony_nc_kbd_backlight_mode_store;
1734
1735 sysfs_attr_init(&kbdbl_handle->timeout_attr.attr);
1736 kbdbl_handle->timeout_attr.attr.name = "kbd_backlight_timeout";
1737 kbdbl_handle->timeout_attr.attr.mode = S_IRUGO | S_IWUSR;
1738 kbdbl_handle->timeout_attr.show = sony_nc_kbd_backlight_timeout_show;
1739 kbdbl_handle->timeout_attr.store = sony_nc_kbd_backlight_timeout_store;
1740
1741 if (device_create_file(&pd->dev, &kbdbl_handle->mode_attr))
1742 goto outkzalloc;
1743
1744 if (device_create_file(&pd->dev, &kbdbl_handle->timeout_attr))
1745 goto outmode;
1746
1747 __sony_nc_kbd_backlight_mode_set(kbd_backlight);
1748 __sony_nc_kbd_backlight_timeout_set(kbd_backlight_timeout);
1749
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001750 return;
Mattia Dongilibf155712011-02-19 11:52:31 +09001751
1752outmode:
1753 device_remove_file(&pd->dev, &kbdbl_handle->mode_attr);
1754outkzalloc:
1755 kfree(kbdbl_handle);
1756 kbdbl_handle = NULL;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001757 return;
Mattia Dongilibf155712011-02-19 11:52:31 +09001758}
1759
1760static int sony_nc_kbd_backlight_cleanup(struct platform_device *pd)
1761{
1762 if (kbdbl_handle) {
Marco Chiapperodf410d52011-04-05 23:38:34 +09001763 int result;
1764
Mattia Dongilibf155712011-02-19 11:52:31 +09001765 device_remove_file(&pd->dev, &kbdbl_handle->mode_attr);
1766 device_remove_file(&pd->dev, &kbdbl_handle->timeout_attr);
Marco Chiapperodf410d52011-04-05 23:38:34 +09001767
1768 /* restore the default hw behaviour */
1769 sony_call_snc_handle(KBDBL_HANDLER, 0x1000 | SET_MODE, &result);
1770 sony_call_snc_handle(KBDBL_HANDLER, SET_TIMEOUT, &result);
1771
Mattia Dongilibf155712011-02-19 11:52:31 +09001772 kfree(kbdbl_handle);
1773 }
1774 return 0;
1775}
1776
Marco Chiapperodf410d52011-04-05 23:38:34 +09001777static void sony_nc_kbd_backlight_resume(void)
1778{
1779 int ignore = 0;
1780
1781 if (!kbdbl_handle)
1782 return;
1783
1784 if (kbdbl_handle->mode == 0)
1785 sony_call_snc_handle(KBDBL_HANDLER, SET_MODE, &ignore);
1786
1787 if (kbdbl_handle->timeout != 0)
1788 sony_call_snc_handle(KBDBL_HANDLER,
1789 (kbdbl_handle->timeout << 0x10) | SET_TIMEOUT,
1790 &ignore);
1791}
1792
Marco Chiappero967145a2012-05-19 22:35:50 +09001793struct battery_care_control {
1794 struct device_attribute attrs[2];
1795 unsigned int handle;
1796};
1797static struct battery_care_control *bcare_ctl;
1798
1799static ssize_t sony_nc_battery_care_limit_store(struct device *dev,
1800 struct device_attribute *attr,
1801 const char *buffer, size_t count)
1802{
1803 unsigned int result, cmd;
1804 unsigned long value;
1805
1806 if (count > 31)
1807 return -EINVAL;
1808
1809 if (kstrtoul(buffer, 10, &value))
1810 return -EINVAL;
1811
1812 /* limit values (2 bits):
1813 * 00 - none
1814 * 01 - 80%
1815 * 10 - 50%
1816 * 11 - 100%
1817 *
1818 * bit 0: 0 disable BCL, 1 enable BCL
1819 * bit 1: 1 tell to store the battery limit (see bits 6,7) too
1820 * bits 2,3: reserved
1821 * bits 4,5: store the limit into the EC
1822 * bits 6,7: store the limit into the battery
1823 */
1824
1825 /*
1826 * handle 0x0115 should allow storing on battery too;
1827 * handle 0x0136 same as 0x0115 + health status;
1828 * handle 0x013f, same as 0x0136 but no storing on the battery
1829 *
1830 * Store only inside the EC for now, regardless the handle number
1831 */
1832 if (value == 0)
1833 /* disable limits */
1834 cmd = 0x0;
1835
1836 else if (value <= 50)
1837 cmd = 0x21;
1838
1839 else if (value <= 80)
1840 cmd = 0x11;
1841
1842 else if (value <= 100)
1843 cmd = 0x31;
1844
1845 else
1846 return -EINVAL;
1847
1848 if (sony_call_snc_handle(bcare_ctl->handle, (cmd << 0x10) | 0x0100,
1849 &result))
1850 return -EIO;
1851
1852 return count;
1853}
1854
1855static ssize_t sony_nc_battery_care_limit_show(struct device *dev,
1856 struct device_attribute *attr, char *buffer)
1857{
1858 unsigned int result, status;
1859
1860 if (sony_call_snc_handle(bcare_ctl->handle, 0x0000, &result))
1861 return -EIO;
1862
1863 status = (result & 0x01) ? ((result & 0x30) >> 0x04) : 0;
1864 switch (status) {
1865 case 1:
1866 status = 80;
1867 break;
1868 case 2:
1869 status = 50;
1870 break;
1871 case 3:
1872 status = 100;
1873 break;
1874 default:
1875 status = 0;
1876 break;
1877 }
1878
1879 return snprintf(buffer, PAGE_SIZE, "%d\n", status);
1880}
1881
1882static ssize_t sony_nc_battery_care_health_show(struct device *dev,
1883 struct device_attribute *attr, char *buffer)
1884{
1885 ssize_t count = 0;
1886 unsigned int health;
1887
1888 if (sony_call_snc_handle(bcare_ctl->handle, 0x0200, &health))
1889 return -EIO;
1890
1891 count = snprintf(buffer, PAGE_SIZE, "%d\n", health & 0xff);
1892
1893 return count;
1894}
1895
1896static int sony_nc_battery_care_setup(struct platform_device *pd,
1897 unsigned int handle)
1898{
1899 int ret = 0;
1900
1901 bcare_ctl = kzalloc(sizeof(struct battery_care_control), GFP_KERNEL);
1902 if (!bcare_ctl)
1903 return -ENOMEM;
1904
1905 bcare_ctl->handle = handle;
1906
1907 sysfs_attr_init(&bcare_ctl->attrs[0].attr);
1908 bcare_ctl->attrs[0].attr.name = "battery_care_limiter";
1909 bcare_ctl->attrs[0].attr.mode = S_IRUGO | S_IWUSR;
1910 bcare_ctl->attrs[0].show = sony_nc_battery_care_limit_show;
1911 bcare_ctl->attrs[0].store = sony_nc_battery_care_limit_store;
1912
1913 ret = device_create_file(&pd->dev, &bcare_ctl->attrs[0]);
1914 if (ret)
1915 goto outkzalloc;
1916
1917 /* 0x0115 is for models with no health reporting capability */
1918 if (handle == 0x0115)
1919 return 0;
1920
1921 sysfs_attr_init(&bcare_ctl->attrs[1].attr);
1922 bcare_ctl->attrs[1].attr.name = "battery_care_health";
1923 bcare_ctl->attrs[1].attr.mode = S_IRUGO;
1924 bcare_ctl->attrs[1].show = sony_nc_battery_care_health_show;
1925
1926 ret = device_create_file(&pd->dev, &bcare_ctl->attrs[1]);
1927 if (ret)
1928 goto outlimiter;
1929
1930 return 0;
1931
1932outlimiter:
1933 device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
1934
1935outkzalloc:
1936 kfree(bcare_ctl);
1937 bcare_ctl = NULL;
1938
1939 return ret;
1940}
1941
1942static void sony_nc_battery_care_cleanup(struct platform_device *pd)
1943{
1944 if (bcare_ctl) {
1945 device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
1946 if (bcare_ctl->handle != 0x0115)
1947 device_remove_file(&pd->dev, &bcare_ctl->attrs[1]);
1948
1949 kfree(bcare_ctl);
1950 bcare_ctl = NULL;
1951 }
1952}
1953
Marco Chiappero49f000a2012-05-19 22:35:51 +09001954struct snc_thermal_ctrl {
1955 unsigned int mode;
1956 unsigned int profiles;
1957 struct device_attribute mode_attr;
1958 struct device_attribute profiles_attr;
1959};
1960static struct snc_thermal_ctrl *th_handle;
1961
1962#define THM_PROFILE_MAX 3
1963static const char * const snc_thermal_profiles[] = {
1964 "balanced",
1965 "silent",
1966 "performance"
1967};
1968
1969static int sony_nc_thermal_mode_set(unsigned short mode)
1970{
1971 unsigned int result;
1972
1973 /* the thermal profile seems to be a two bit bitmask:
1974 * lsb -> silent
1975 * msb -> performance
1976 * no bit set is the normal operation and is always valid
1977 * Some vaio models only have "balanced" and "performance"
1978 */
1979 if ((mode && !(th_handle->profiles & mode)) || mode >= THM_PROFILE_MAX)
1980 return -EINVAL;
1981
1982 if (sony_call_snc_handle(0x0122, mode << 0x10 | 0x0200, &result))
1983 return -EIO;
1984
1985 th_handle->mode = mode;
1986
1987 return 0;
1988}
1989
1990static int sony_nc_thermal_mode_get(void)
1991{
1992 unsigned int result;
1993
1994 if (sony_call_snc_handle(0x0122, 0x0100, &result))
1995 return -EIO;
1996
1997 return result & 0xff;
1998}
1999
2000static ssize_t sony_nc_thermal_profiles_show(struct device *dev,
2001 struct device_attribute *attr, char *buffer)
2002{
2003 short cnt;
2004 size_t idx = 0;
2005
2006 for (cnt = 0; cnt < THM_PROFILE_MAX; cnt++) {
2007 if (!cnt || (th_handle->profiles & cnt))
2008 idx += snprintf(buffer + idx, PAGE_SIZE - idx, "%s ",
2009 snc_thermal_profiles[cnt]);
2010 }
2011 idx += snprintf(buffer + idx, PAGE_SIZE - idx, "\n");
2012
2013 return idx;
2014}
2015
2016static ssize_t sony_nc_thermal_mode_store(struct device *dev,
2017 struct device_attribute *attr,
2018 const char *buffer, size_t count)
2019{
2020 unsigned short cmd;
2021 size_t len = count;
2022
2023 if (count == 0)
2024 return -EINVAL;
2025
2026 /* skip the newline if present */
2027 if (buffer[len - 1] == '\n')
2028 len--;
2029
2030 for (cmd = 0; cmd < THM_PROFILE_MAX; cmd++)
2031 if (strncmp(buffer, snc_thermal_profiles[cmd], len) == 0)
2032 break;
2033
2034 if (sony_nc_thermal_mode_set(cmd))
2035 return -EIO;
2036
2037 return count;
2038}
2039
2040static ssize_t sony_nc_thermal_mode_show(struct device *dev,
2041 struct device_attribute *attr, char *buffer)
2042{
2043 ssize_t count = 0;
2044 unsigned int mode = sony_nc_thermal_mode_get();
2045
2046 if (mode < 0)
2047 return mode;
2048
2049 count = snprintf(buffer, PAGE_SIZE, "%s\n", snc_thermal_profiles[mode]);
2050
2051 return count;
2052}
2053
2054static int sony_nc_thermal_setup(struct platform_device *pd)
2055{
2056 int ret = 0;
2057 th_handle = kzalloc(sizeof(struct snc_thermal_ctrl), GFP_KERNEL);
2058 if (!th_handle)
2059 return -ENOMEM;
2060
2061 ret = sony_call_snc_handle(0x0122, 0x0000, &th_handle->profiles);
2062 if (ret) {
2063 pr_warn("couldn't to read the thermal profiles\n");
2064 goto outkzalloc;
2065 }
2066
2067 ret = sony_nc_thermal_mode_get();
2068 if (ret < 0) {
2069 pr_warn("couldn't to read the current thermal profile");
2070 goto outkzalloc;
2071 }
2072 th_handle->mode = ret;
2073
2074 sysfs_attr_init(&th_handle->profiles_attr.attr);
2075 th_handle->profiles_attr.attr.name = "thermal_profiles";
2076 th_handle->profiles_attr.attr.mode = S_IRUGO;
2077 th_handle->profiles_attr.show = sony_nc_thermal_profiles_show;
2078
2079 sysfs_attr_init(&th_handle->mode_attr.attr);
2080 th_handle->mode_attr.attr.name = "thermal_control";
2081 th_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
2082 th_handle->mode_attr.show = sony_nc_thermal_mode_show;
2083 th_handle->mode_attr.store = sony_nc_thermal_mode_store;
2084
2085 ret = device_create_file(&pd->dev, &th_handle->profiles_attr);
2086 if (ret)
2087 goto outkzalloc;
2088
2089 ret = device_create_file(&pd->dev, &th_handle->mode_attr);
2090 if (ret)
2091 goto outprofiles;
2092
2093 return 0;
2094
2095outprofiles:
2096 device_remove_file(&pd->dev, &th_handle->profiles_attr);
2097outkzalloc:
2098 kfree(th_handle);
2099 th_handle = NULL;
2100 return ret;
2101}
2102
2103static void sony_nc_thermal_cleanup(struct platform_device *pd)
2104{
2105 if (th_handle) {
2106 device_remove_file(&pd->dev, &th_handle->profiles_attr);
2107 device_remove_file(&pd->dev, &th_handle->mode_attr);
2108 kfree(th_handle);
2109 th_handle = NULL;
2110 }
2111}
2112
2113static void sony_nc_thermal_resume(void)
2114{
2115 unsigned int status = sony_nc_thermal_mode_get();
2116
2117 if (status != th_handle->mode)
2118 sony_nc_thermal_mode_set(th_handle->mode);
2119}
2120
Marco Chiappero54535d02012-05-19 22:35:54 +09002121/* resume on LID open */
2122struct snc_lid_resume_control {
2123 struct device_attribute attrs[3];
2124 unsigned int status;
2125};
2126static struct snc_lid_resume_control *lid_ctl;
2127
2128static ssize_t sony_nc_lid_resume_store(struct device *dev,
2129 struct device_attribute *attr,
2130 const char *buffer, size_t count)
2131{
2132 unsigned int result, pos;
2133 unsigned long value;
2134 if (count > 31)
2135 return -EINVAL;
2136
2137 if (kstrtoul(buffer, 10, &value) || value > 1)
2138 return -EINVAL;
2139
2140 /* the value we have to write to SNC is a bitmask:
2141 * +--------------+
2142 * | S3 | S4 | S5 |
2143 * +--------------+
2144 * 2 1 0
2145 */
2146 if (strcmp(attr->attr.name, "lid_resume_S3") == 0)
2147 pos = 2;
2148 else if (strcmp(attr->attr.name, "lid_resume_S4") == 0)
2149 pos = 1;
2150 else if (strcmp(attr->attr.name, "lid_resume_S5") == 0)
2151 pos = 0;
2152 else
2153 return -EINVAL;
2154
2155 if (value)
2156 value = lid_ctl->status | (1 << pos);
2157 else
2158 value = lid_ctl->status & ~(1 << pos);
2159
2160 if (sony_call_snc_handle(0x0119, value << 0x10 | 0x0100, &result))
2161 return -EIO;
2162
2163 lid_ctl->status = value;
2164
2165 return count;
2166}
2167
2168static ssize_t sony_nc_lid_resume_show(struct device *dev,
2169 struct device_attribute *attr, char *buffer)
2170{
2171 unsigned int pos;
2172
2173 if (strcmp(attr->attr.name, "lid_resume_S3") == 0)
2174 pos = 2;
2175 else if (strcmp(attr->attr.name, "lid_resume_S4") == 0)
2176 pos = 1;
2177 else if (strcmp(attr->attr.name, "lid_resume_S5") == 0)
2178 pos = 0;
2179 else
2180 return -EINVAL;
2181
2182 return snprintf(buffer, PAGE_SIZE, "%d\n",
2183 (lid_ctl->status >> pos) & 0x01);
2184}
2185
2186static int sony_nc_lid_resume_setup(struct platform_device *pd)
2187{
2188 unsigned int result;
2189 int i;
2190
2191 if (sony_call_snc_handle(0x0119, 0x0000, &result))
2192 return -EIO;
2193
2194 lid_ctl = kzalloc(sizeof(struct snc_lid_resume_control), GFP_KERNEL);
2195 if (!lid_ctl)
2196 return -ENOMEM;
2197
2198 lid_ctl->status = result & 0x7;
2199
2200 sysfs_attr_init(&lid_ctl->attrs[0].attr);
2201 lid_ctl->attrs[0].attr.name = "lid_resume_S3";
2202 lid_ctl->attrs[0].attr.mode = S_IRUGO | S_IWUSR;
2203 lid_ctl->attrs[0].show = sony_nc_lid_resume_show;
2204 lid_ctl->attrs[0].store = sony_nc_lid_resume_store;
2205
2206 sysfs_attr_init(&lid_ctl->attrs[1].attr);
2207 lid_ctl->attrs[1].attr.name = "lid_resume_S4";
2208 lid_ctl->attrs[1].attr.mode = S_IRUGO | S_IWUSR;
2209 lid_ctl->attrs[1].show = sony_nc_lid_resume_show;
2210 lid_ctl->attrs[1].store = sony_nc_lid_resume_store;
2211
2212 sysfs_attr_init(&lid_ctl->attrs[2].attr);
2213 lid_ctl->attrs[2].attr.name = "lid_resume_S5";
2214 lid_ctl->attrs[2].attr.mode = S_IRUGO | S_IWUSR;
2215 lid_ctl->attrs[2].show = sony_nc_lid_resume_show;
2216 lid_ctl->attrs[2].store = sony_nc_lid_resume_store;
2217
2218 for (i = 0; i < 3; i++) {
2219 result = device_create_file(&pd->dev, &lid_ctl->attrs[i]);
2220 if (result)
2221 goto liderror;
2222 }
2223
2224 return 0;
2225
2226liderror:
2227 for (; i > 0; i--)
2228 device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
2229
2230 kfree(lid_ctl);
2231 lid_ctl = NULL;
2232
2233 return result;
2234}
2235
2236static void sony_nc_lid_resume_cleanup(struct platform_device *pd)
2237{
2238 int i;
2239
2240 if (lid_ctl) {
2241 for (i = 0; i < 3; i++)
2242 device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
2243
2244 kfree(lid_ctl);
2245 lid_ctl = NULL;
2246 }
2247}
2248
Mattia Dongili62d2f232011-05-09 10:20:29 -04002249static void sony_nc_backlight_ng_read_limits(int handle,
2250 struct sony_backlight_props *props)
2251{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002252 u64 offset;
2253 int i;
Mattia Dongili62d2f232011-05-09 10:20:29 -04002254 u8 min = 0xff, max = 0x00;
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002255 unsigned char buffer[32] = { 0 };
Mattia Dongili62d2f232011-05-09 10:20:29 -04002256
2257 props->handle = handle;
2258 props->offset = 0;
2259 props->maxlvl = 0xff;
2260
2261 offset = sony_find_snc_handle(handle);
2262 if (offset < 0)
2263 return;
2264
2265 /* try to read the boundaries from ACPI tables, if we fail the above
2266 * defaults should be reasonable
2267 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002268 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
2269 32);
2270 if (i < 0)
Mattia Dongili62d2f232011-05-09 10:20:29 -04002271 return;
2272
Mattia Dongili62d2f232011-05-09 10:20:29 -04002273 /* the buffer lists brightness levels available, brightness levels are
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002274 * from position 0 to 8 in the array, other values are used by ALS
2275 * control.
Mattia Dongili62d2f232011-05-09 10:20:29 -04002276 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002277 for (i = 0; i < 9 && i < ARRAY_SIZE(buffer); i++) {
Mattia Dongili62d2f232011-05-09 10:20:29 -04002278
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002279 dprintk("Brightness level: %d\n", buffer[i]);
Mattia Dongili62d2f232011-05-09 10:20:29 -04002280
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002281 if (!buffer[i])
Mattia Dongili62d2f232011-05-09 10:20:29 -04002282 break;
2283
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002284 if (buffer[i] > max)
2285 max = buffer[i];
2286 if (buffer[i] < min)
2287 min = buffer[i];
Mattia Dongili62d2f232011-05-09 10:20:29 -04002288 }
2289 props->offset = min;
2290 props->maxlvl = max;
2291 dprintk("Brightness levels: min=%d max=%d\n", props->offset,
2292 props->maxlvl);
Mattia Dongili62d2f232011-05-09 10:20:29 -04002293}
2294
Mattia Dongili7751ab82011-02-19 11:52:32 +09002295static void sony_nc_backlight_setup(void)
2296{
2297 acpi_handle unused;
2298 int max_brightness = 0;
2299 const struct backlight_ops *ops = NULL;
2300 struct backlight_properties props;
2301
2302 if (sony_find_snc_handle(0x12f) != -1) {
Mattia Dongili7751ab82011-02-19 11:52:32 +09002303 ops = &sony_backlight_ng_ops;
Mattia Dongili62d2f232011-05-09 10:20:29 -04002304 sony_nc_backlight_ng_read_limits(0x12f, &sony_bl_props);
2305 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +09002306
2307 } else if (sony_find_snc_handle(0x137) != -1) {
Mattia Dongili7751ab82011-02-19 11:52:32 +09002308 ops = &sony_backlight_ng_ops;
Mattia Dongili62d2f232011-05-09 10:20:29 -04002309 sony_nc_backlight_ng_read_limits(0x137, &sony_bl_props);
2310 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +09002311
2312 } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
2313 &unused))) {
2314 ops = &sony_backlight_ops;
2315 max_brightness = SONY_MAX_BRIGHTNESS - 1;
2316
2317 } else
2318 return;
2319
2320 memset(&props, 0, sizeof(struct backlight_properties));
2321 props.type = BACKLIGHT_PLATFORM;
2322 props.max_brightness = max_brightness;
Mattia Dongili62d2f232011-05-09 10:20:29 -04002323 sony_bl_props.dev = backlight_device_register("sony", NULL,
2324 &sony_bl_props,
2325 ops, &props);
Mattia Dongili7751ab82011-02-19 11:52:32 +09002326
Mattia Dongili62d2f232011-05-09 10:20:29 -04002327 if (IS_ERR(sony_bl_props.dev)) {
Joe Perches50f581a2011-03-29 15:21:48 -07002328 pr_warn("unable to register backlight device\n");
Mattia Dongili62d2f232011-05-09 10:20:29 -04002329 sony_bl_props.dev = NULL;
Mattia Dongili7751ab82011-02-19 11:52:32 +09002330 } else
Mattia Dongili62d2f232011-05-09 10:20:29 -04002331 sony_bl_props.dev->props.brightness =
2332 ops->get_brightness(sony_bl_props.dev);
Mattia Dongili7751ab82011-02-19 11:52:32 +09002333}
2334
2335static void sony_nc_backlight_cleanup(void)
2336{
Mattia Dongili62d2f232011-05-09 10:20:29 -04002337 if (sony_bl_props.dev)
2338 backlight_device_unregister(sony_bl_props.dev);
Mattia Dongili7751ab82011-02-19 11:52:32 +09002339}
2340
malattia@linux.it59b19102007-04-09 10:19:04 +02002341static int sony_nc_add(struct acpi_device *device)
Stelian Pop7f09c432007-01-13 23:04:31 +01002342{
2343 acpi_status status;
Andrew Morton8607c672007-03-06 02:29:42 -08002344 int result = 0;
Alessandro Guido50f62af2007-01-13 23:04:34 +01002345 acpi_handle handle;
malattia@linux.it56b87562007-04-09 10:19:05 +02002346 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01002347
Joe Perches50f581a2011-03-29 15:21:48 -07002348 pr_info("%s v%s\n", SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.itf6119b02007-04-09 19:31:06 +02002349
malattia@linux.it59b19102007-04-09 10:19:04 +02002350 sony_nc_acpi_device = device;
malattia@linux.it33a04452007-04-09 19:26:03 +02002351 strcpy(acpi_device_class(device), "sony/hotkey");
Stelian Popc5611622007-01-13 23:04:37 +01002352
malattia@linux.it59b19102007-04-09 10:19:04 +02002353 sony_nc_acpi_handle = device->handle;
Stelian Pop7f09c432007-01-13 23:04:31 +01002354
Mattia Dongilib25b7322007-07-16 02:34:36 +09002355 /* read device status */
2356 result = acpi_bus_get_status(device);
2357 /* bail IFF the above call was successful and the device is not present */
2358 if (!result && !device->status.present) {
2359 dprintk("Device not present\n");
2360 result = -ENODEV;
2361 goto outwalk;
2362 }
2363
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002364 result = sony_pf_add();
2365 if (result)
2366 goto outpresent;
2367
Stelian Pop7f09c432007-01-13 23:04:31 +01002368 if (debug) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002369 status = acpi_walk_namespace(ACPI_TYPE_METHOD,
2370 sony_nc_acpi_handle, 1, sony_walk_callback,
2371 NULL, NULL, NULL);
Stelian Pop7f09c432007-01-13 23:04:31 +01002372 if (ACPI_FAILURE(status)) {
Joe Perches50f581a2011-03-29 15:21:48 -07002373 pr_warn("unable to walk acpi resources\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01002374 result = -ENODEV;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002375 goto outpresent;
Stelian Pop7f09c432007-01-13 23:04:31 +01002376 }
Stelian Popc5611622007-01-13 23:04:37 +01002377 }
Stelian Pop7f09c432007-01-13 23:04:31 +01002378
Matthew Garrett82734bf2009-03-26 21:58:13 +09002379 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
2380 &handle))) {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002381 int arg = 1;
2382 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
Matthew Garrett82734bf2009-03-26 21:58:13 +09002383 dprintk("ECON Method failed\n");
2384 }
2385
Matthew Garrettbadf26f2009-03-26 21:58:12 +09002386 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
2387 &handle))) {
2388 dprintk("Doing SNC setup\n");
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002389 /* retrieve the available handles */
Dan Carpenter7227ded2011-02-26 15:54:57 +03002390 result = sony_nc_handles_setup(sony_pf_device);
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002391 if (!result)
2392 sony_nc_function_setup(device, sony_pf_device);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09002393 }
2394
malattia@linux.it1549ee62007-04-09 10:19:08 +02002395 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05002396 result = sony_laptop_setup_input(device);
malattia@linux.it1549ee62007-04-09 10:19:08 +02002397 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07002398 pr_err("Unable to create input devices\n");
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002399 goto outsnc;
malattia@linux.it1549ee62007-04-09 10:19:08 +02002400 }
2401
Alessandro Guido38cfc142008-11-12 23:03:28 +01002402 if (acpi_video_backlight_support()) {
Joe Perches50f581a2011-03-29 15:21:48 -07002403 pr_info("brightness ignored, must be controlled by ACPI video driver\n");
Mattia Dongili7751ab82011-02-19 11:52:32 +09002404 } else {
2405 sony_nc_backlight_setup();
Alessandro Guido50f62af2007-01-13 23:04:34 +01002406 }
Stelian Pop7f09c432007-01-13 23:04:31 +01002407
malattia@linux.it56b87562007-04-09 10:19:05 +02002408 /* create sony_pf sysfs attributes related to the SNC device */
2409 for (item = sony_nc_values; item->name; ++item) {
2410
2411 if (!debug && item->debug)
2412 continue;
2413
2414 /* find the available acpiget as described in the DSDT */
2415 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
2416 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
2417 *item->acpiget,
2418 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02002419 dprintk("Found %s getter: %s\n",
2420 item->name, *item->acpiget);
malattia@linux.it56b87562007-04-09 10:19:05 +02002421 item->devattr.attr.mode |= S_IRUGO;
2422 break;
2423 }
2424 }
2425
2426 /* find the available acpiset as described in the DSDT */
2427 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
2428 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
2429 *item->acpiset,
2430 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02002431 dprintk("Found %s setter: %s\n",
2432 item->name, *item->acpiset);
malattia@linux.it56b87562007-04-09 10:19:05 +02002433 item->devattr.attr.mode |= S_IWUSR;
2434 break;
2435 }
2436 }
2437
2438 if (item->devattr.attr.mode != 0) {
2439 result =
2440 device_create_file(&sony_pf_device->dev,
2441 &item->devattr);
2442 if (result)
2443 goto out_sysfs;
2444 }
2445 }
2446
Stelian Pop7f09c432007-01-13 23:04:31 +01002447 return 0;
2448
malattia@linux.it56b87562007-04-09 10:19:05 +02002449 out_sysfs:
2450 for (item = sony_nc_values; item->name; ++item) {
2451 device_remove_file(&sony_pf_device->dev, &item->devattr);
2452 }
Mattia Dongili7751ab82011-02-19 11:52:32 +09002453 sony_nc_backlight_cleanup();
Mattia Dongili7df03b82007-01-13 23:04:41 +01002454
malattia@linux.it1549ee62007-04-09 10:19:08 +02002455 sony_laptop_remove_input();
2456
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002457 outsnc:
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002458 sony_nc_function_cleanup(sony_pf_device);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002459 sony_nc_handles_cleanup(sony_pf_device);
2460
2461 outpresent:
2462 sony_pf_remove();
2463
Len Browna02d1c12007-02-07 15:34:02 -05002464 outwalk:
Matthew Garrett6cc056b2009-03-26 21:58:15 +09002465 sony_nc_rfkill_cleanup();
Stelian Pop7f09c432007-01-13 23:04:31 +01002466 return result;
2467}
2468
malattia@linux.it59b19102007-04-09 10:19:04 +02002469static int sony_nc_remove(struct acpi_device *device, int type)
Stelian Pop7f09c432007-01-13 23:04:31 +01002470{
malattia@linux.it56b87562007-04-09 10:19:05 +02002471 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01002472
Mattia Dongili7751ab82011-02-19 11:52:32 +09002473 sony_nc_backlight_cleanup();
Alessandro Guido50f62af2007-01-13 23:04:34 +01002474
malattia@linux.it59b19102007-04-09 10:19:04 +02002475 sony_nc_acpi_device = NULL;
Stelian Popc5611622007-01-13 23:04:37 +01002476
malattia@linux.it56b87562007-04-09 10:19:05 +02002477 for (item = sony_nc_values; item->name; ++item) {
2478 device_remove_file(&sony_pf_device->dev, &item->devattr);
2479 }
2480
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002481 sony_nc_function_cleanup(sony_pf_device);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002482 sony_nc_handles_cleanup(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +02002483 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +02002484 sony_laptop_remove_input();
malattia@linux.itf6119b02007-04-09 19:31:06 +02002485 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01002486
2487 return 0;
2488}
2489
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002490static const struct acpi_device_id sony_device_ids[] = {
2491 {SONY_NC_HID, 0},
2492 {SONY_PIC_HID, 0},
2493 {"", 0},
2494};
2495MODULE_DEVICE_TABLE(acpi, sony_device_ids);
2496
2497static const struct acpi_device_id sony_nc_device_ids[] = {
2498 {SONY_NC_HID, 0},
2499 {"", 0},
2500};
2501
malattia@linux.it59b19102007-04-09 10:19:04 +02002502static struct acpi_driver sony_nc_driver = {
2503 .name = SONY_NC_DRIVER_NAME,
2504 .class = SONY_NC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002505 .ids = sony_nc_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02002506 .owner = THIS_MODULE,
Len Browna02d1c12007-02-07 15:34:02 -05002507 .ops = {
malattia@linux.it59b19102007-04-09 10:19:04 +02002508 .add = sony_nc_add,
2509 .remove = sony_nc_remove,
2510 .resume = sony_nc_resume,
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00002511 .notify = sony_nc_notify,
Len Browna02d1c12007-02-07 15:34:02 -05002512 },
Andrew Morton3f4f4612007-01-13 23:04:32 +01002513};
2514
malattia@linux.it33a04452007-04-09 19:26:03 +02002515/*********** SPIC (SNY6001) Device ***********/
2516
2517#define SONYPI_DEVICE_TYPE1 0x00000001
2518#define SONYPI_DEVICE_TYPE2 0x00000002
2519#define SONYPI_DEVICE_TYPE3 0x00000004
2520
Mattia Dongili22a17782007-07-16 02:34:39 +09002521#define SONYPI_TYPE1_OFFSET 0x04
2522#define SONYPI_TYPE2_OFFSET 0x12
2523#define SONYPI_TYPE3_OFFSET 0x12
malattia@linux.it33a04452007-04-09 19:26:03 +02002524
malattia@linux.it33a04452007-04-09 19:26:03 +02002525struct sony_pic_ioport {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002526 struct acpi_resource_io io1;
2527 struct acpi_resource_io io2;
malattia@linux.it33a04452007-04-09 19:26:03 +02002528 struct list_head list;
2529};
2530
2531struct sony_pic_irq {
2532 struct acpi_resource_irq irq;
2533 struct list_head list;
2534};
2535
Mattia Dongilide920432008-01-14 18:05:43 +09002536struct sonypi_eventtypes {
2537 u8 data;
2538 unsigned long mask;
2539 struct sonypi_event *events;
2540};
2541
malattia@linux.it33a04452007-04-09 19:26:03 +02002542struct sony_pic_dev {
Mattia Dongili31df7142009-09-16 00:05:29 +09002543 struct acpi_device *acpi_dev;
2544 struct sony_pic_irq *cur_irq;
2545 struct sony_pic_ioport *cur_ioport;
2546 struct list_head interrupts;
2547 struct list_head ioports;
2548 struct mutex lock;
2549 struct sonypi_eventtypes *event_types;
2550 int (*handle_irq)(const u8, const u8);
2551 int model;
2552 u16 evport_offset;
2553 u8 camera_power;
2554 u8 bluetooth_power;
2555 u8 wwan_power;
malattia@linux.it33a04452007-04-09 19:26:03 +02002556};
2557
2558static struct sony_pic_dev spic_dev = {
2559 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
2560 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
2561};
2562
Alan Jenkins5e6f9722009-09-16 00:05:32 +09002563static int spic_drv_registered;
2564
malattia@linux.it33a04452007-04-09 19:26:03 +02002565/* Event masks */
2566#define SONYPI_JOGGER_MASK 0x00000001
2567#define SONYPI_CAPTURE_MASK 0x00000002
2568#define SONYPI_FNKEY_MASK 0x00000004
2569#define SONYPI_BLUETOOTH_MASK 0x00000008
2570#define SONYPI_PKEY_MASK 0x00000010
2571#define SONYPI_BACK_MASK 0x00000020
2572#define SONYPI_HELP_MASK 0x00000040
2573#define SONYPI_LID_MASK 0x00000080
2574#define SONYPI_ZOOM_MASK 0x00000100
2575#define SONYPI_THUMBPHRASE_MASK 0x00000200
2576#define SONYPI_MEYE_MASK 0x00000400
2577#define SONYPI_MEMORYSTICK_MASK 0x00000800
2578#define SONYPI_BATTERY_MASK 0x00001000
2579#define SONYPI_WIRELESS_MASK 0x00002000
2580
2581struct sonypi_event {
2582 u8 data;
2583 u8 event;
2584};
2585
2586/* The set of possible button release events */
2587static struct sonypi_event sonypi_releaseev[] = {
2588 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
2589 { 0, 0 }
2590};
2591
2592/* The set of possible jogger events */
2593static struct sonypi_event sonypi_joggerev[] = {
2594 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
2595 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
2596 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
2597 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
2598 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
2599 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
2600 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
2601 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
2602 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
2603 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
2604 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
2605 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
2606 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
2607 { 0, 0 }
2608};
2609
2610/* The set of possible capture button events */
2611static struct sonypi_event sonypi_captureev[] = {
2612 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
2613 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002614 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02002615 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
2616 { 0, 0 }
2617};
2618
2619/* The set of possible fnkeys events */
2620static struct sonypi_event sonypi_fnkeyev[] = {
2621 { 0x10, SONYPI_EVENT_FNKEY_ESC },
2622 { 0x11, SONYPI_EVENT_FNKEY_F1 },
2623 { 0x12, SONYPI_EVENT_FNKEY_F2 },
2624 { 0x13, SONYPI_EVENT_FNKEY_F3 },
2625 { 0x14, SONYPI_EVENT_FNKEY_F4 },
2626 { 0x15, SONYPI_EVENT_FNKEY_F5 },
2627 { 0x16, SONYPI_EVENT_FNKEY_F6 },
2628 { 0x17, SONYPI_EVENT_FNKEY_F7 },
2629 { 0x18, SONYPI_EVENT_FNKEY_F8 },
2630 { 0x19, SONYPI_EVENT_FNKEY_F9 },
2631 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
2632 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
2633 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
2634 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
2635 { 0x21, SONYPI_EVENT_FNKEY_1 },
2636 { 0x22, SONYPI_EVENT_FNKEY_2 },
2637 { 0x31, SONYPI_EVENT_FNKEY_D },
2638 { 0x32, SONYPI_EVENT_FNKEY_E },
2639 { 0x33, SONYPI_EVENT_FNKEY_F },
2640 { 0x34, SONYPI_EVENT_FNKEY_S },
2641 { 0x35, SONYPI_EVENT_FNKEY_B },
2642 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
2643 { 0, 0 }
2644};
2645
2646/* The set of possible program key events */
2647static struct sonypi_event sonypi_pkeyev[] = {
2648 { 0x01, SONYPI_EVENT_PKEY_P1 },
2649 { 0x02, SONYPI_EVENT_PKEY_P2 },
2650 { 0x04, SONYPI_EVENT_PKEY_P3 },
Harald Jenny1cae7102009-03-26 21:58:18 +09002651 { 0x20, SONYPI_EVENT_PKEY_P1 },
malattia@linux.it33a04452007-04-09 19:26:03 +02002652 { 0, 0 }
2653};
2654
2655/* The set of possible bluetooth events */
2656static struct sonypi_event sonypi_blueev[] = {
2657 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
2658 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
2659 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
2660 { 0, 0 }
2661};
2662
2663/* The set of possible wireless events */
2664static struct sonypi_event sonypi_wlessev[] = {
Mattia Dongili4eeb5022011-02-19 11:52:27 +09002665 { 0x59, SONYPI_EVENT_IGNORE },
2666 { 0x5a, SONYPI_EVENT_IGNORE },
malattia@linux.it33a04452007-04-09 19:26:03 +02002667 { 0, 0 }
2668};
2669
2670/* The set of possible back button events */
2671static struct sonypi_event sonypi_backev[] = {
2672 { 0x20, SONYPI_EVENT_BACK_PRESSED },
2673 { 0, 0 }
2674};
2675
2676/* The set of possible help button events */
2677static struct sonypi_event sonypi_helpev[] = {
2678 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
2679 { 0, 0 }
2680};
2681
2682
2683/* The set of possible lid events */
2684static struct sonypi_event sonypi_lidev[] = {
2685 { 0x51, SONYPI_EVENT_LID_CLOSED },
2686 { 0x50, SONYPI_EVENT_LID_OPENED },
2687 { 0, 0 }
2688};
2689
2690/* The set of possible zoom events */
2691static struct sonypi_event sonypi_zoomev[] = {
2692 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002693 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
2694 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
Harald Jenny1cae7102009-03-26 21:58:18 +09002695 { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02002696 { 0, 0 }
2697};
2698
2699/* The set of possible thumbphrase events */
2700static struct sonypi_event sonypi_thumbphraseev[] = {
2701 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
2702 { 0, 0 }
2703};
2704
2705/* The set of possible motioneye camera events */
2706static struct sonypi_event sonypi_meyeev[] = {
2707 { 0x00, SONYPI_EVENT_MEYE_FACE },
2708 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
2709 { 0, 0 }
2710};
2711
2712/* The set of possible memorystick events */
2713static struct sonypi_event sonypi_memorystickev[] = {
2714 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
2715 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
2716 { 0, 0 }
2717};
2718
2719/* The set of possible battery events */
2720static struct sonypi_event sonypi_batteryev[] = {
2721 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
2722 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
2723 { 0, 0 }
2724};
2725
Harald Jenny1cae7102009-03-26 21:58:18 +09002726/* The set of possible volume events */
2727static struct sonypi_event sonypi_volumeev[] = {
2728 { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
2729 { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
2730 { 0, 0 }
2731};
2732
2733/* The set of possible brightness events */
2734static struct sonypi_event sonypi_brightnessev[] = {
2735 { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
2736 { 0, 0 }
2737};
2738
Mattia Dongilide920432008-01-14 18:05:43 +09002739static struct sonypi_eventtypes type1_events[] = {
2740 { 0, 0xffffffff, sonypi_releaseev },
2741 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
2742 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
2743 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
2744 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
2745 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2746 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
2747 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
2748 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2749 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
2750 { 0 },
2751};
2752static struct sonypi_eventtypes type2_events[] = {
2753 { 0, 0xffffffff, sonypi_releaseev },
2754 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
2755 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
2756 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
2757 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2758 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
2759 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
2760 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
2761 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
2762 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
2763 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
2764 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2765 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
2766 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
2767 { 0 },
2768};
2769static struct sonypi_eventtypes type3_events[] = {
2770 { 0, 0xffffffff, sonypi_releaseev },
2771 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2772 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
2773 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2774 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
2775 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002776 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
2777 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
2778 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
Harald Jenny1cae7102009-03-26 21:58:18 +09002779 { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
2780 { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002781 { 0 },
Mattia Dongilide920432008-01-14 18:05:43 +09002782};
2783
Mattia Dongili3eb87492008-01-14 18:05:45 +09002784/* low level spic calls */
malattia@linux.it33a04452007-04-09 19:26:03 +02002785#define ITERATIONS_LONG 10000
2786#define ITERATIONS_SHORT 10
2787#define wait_on_command(command, iterations) { \
2788 unsigned int n = iterations; \
2789 while (--n && (command)) \
2790 udelay(1); \
2791 if (!n) \
2792 dprintk("command failed at %s : %s (line %d)\n", \
Harvey Harrison6e574192008-04-29 00:59:20 -07002793 __FILE__, __func__, __LINE__); \
malattia@linux.it33a04452007-04-09 19:26:03 +02002794}
2795
2796static u8 sony_pic_call1(u8 dev)
2797{
2798 u8 v1, v2;
2799
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002800 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002801 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002802 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2803 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
2804 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002805 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02002806 return v2;
2807}
2808
2809static u8 sony_pic_call2(u8 dev, u8 fn)
2810{
2811 u8 v1;
2812
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002813 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002814 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002815 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2816 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002817 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002818 outb(fn, spic_dev.cur_ioport->io1.minimum);
2819 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002820 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02002821 return v1;
2822}
2823
malattia@linux.it49a11de2007-04-09 19:28:56 +02002824static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
2825{
2826 u8 v1;
2827
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002828 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2829 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2830 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2831 outb(fn, spic_dev.cur_ioport->io1.minimum);
2832 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2833 outb(v, spic_dev.cur_ioport->io1.minimum);
2834 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002835 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
2836 dev, fn, v, v1);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002837 return v1;
2838}
2839
Mattia Dongili3eb87492008-01-14 18:05:45 +09002840/*
2841 * minidrivers for SPIC models
2842 */
Mattia Dongilie93c8a62009-03-26 21:58:17 +09002843static int type3_handle_irq(const u8 data_mask, const u8 ev)
Mattia Dongili3eb87492008-01-14 18:05:45 +09002844{
2845 /*
2846 * 0x31 could mean we have to take some extra action and wait for
Mattia Dongilie93c8a62009-03-26 21:58:17 +09002847 * the next irq for some Type3 models, it will generate a new
Mattia Dongili3eb87492008-01-14 18:05:45 +09002848 * irq and we can read new data from the device:
2849 * - 0x5c and 0x5f requires 0xA0
2850 * - 0x61 requires 0xB3
2851 */
2852 if (data_mask == 0x31) {
2853 if (ev == 0x5c || ev == 0x5f)
2854 sony_pic_call1(0xA0);
2855 else if (ev == 0x61)
2856 sony_pic_call1(0xB3);
2857 return 0;
2858 }
2859 return 1;
2860}
2861
Mattia Dongili3eb87492008-01-14 18:05:45 +09002862static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
2863{
2864 struct pci_dev *pcidev;
2865
2866 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2867 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
2868 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002869 dev->model = SONYPI_DEVICE_TYPE1;
2870 dev->evport_offset = SONYPI_TYPE1_OFFSET;
2871 dev->event_types = type1_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002872 goto out;
2873 }
2874
2875 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2876 PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
2877 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002878 dev->model = SONYPI_DEVICE_TYPE2;
2879 dev->evport_offset = SONYPI_TYPE2_OFFSET;
2880 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002881 goto out;
2882 }
2883
2884 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2885 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
2886 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002887 dev->model = SONYPI_DEVICE_TYPE3;
2888 dev->handle_irq = type3_handle_irq;
2889 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2890 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002891 goto out;
2892 }
2893
2894 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2895 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
2896 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002897 dev->model = SONYPI_DEVICE_TYPE3;
2898 dev->handle_irq = type3_handle_irq;
2899 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2900 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002901 goto out;
2902 }
2903
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09002904 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2905 PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
2906 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002907 dev->model = SONYPI_DEVICE_TYPE3;
2908 dev->handle_irq = type3_handle_irq;
2909 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2910 dev->event_types = type3_events;
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09002911 goto out;
2912 }
2913
Mattia Dongili3eb87492008-01-14 18:05:45 +09002914 /* default */
Mattia Dongili31df7142009-09-16 00:05:29 +09002915 dev->model = SONYPI_DEVICE_TYPE2;
2916 dev->evport_offset = SONYPI_TYPE2_OFFSET;
2917 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002918
2919out:
2920 if (pcidev)
2921 pci_dev_put(pcidev);
2922
Joe Perches50f581a2011-03-29 15:21:48 -07002923 pr_info("detected Type%d model\n",
2924 dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
2925 dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
Mattia Dongili3eb87492008-01-14 18:05:45 +09002926}
2927
malattia@linux.it49a11de2007-04-09 19:28:56 +02002928/* camera tests and poweron/poweroff */
2929#define SONYPI_CAMERA_PICTURE 5
malattia@linux.it49a11de2007-04-09 19:28:56 +02002930#define SONYPI_CAMERA_CONTROL 0x10
malattia@linux.ite3646322007-04-28 23:34:22 +09002931
2932#define SONYPI_CAMERA_BRIGHTNESS 0
2933#define SONYPI_CAMERA_CONTRAST 1
2934#define SONYPI_CAMERA_HUE 2
2935#define SONYPI_CAMERA_COLOR 3
2936#define SONYPI_CAMERA_SHARPNESS 4
2937
2938#define SONYPI_CAMERA_EXPOSURE_MASK 0xC
2939#define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
2940#define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
2941#define SONYPI_CAMERA_MUTE_MASK 0x40
2942
2943/* the rest don't need a loop until not 0xff */
2944#define SONYPI_CAMERA_AGC 6
2945#define SONYPI_CAMERA_AGC_MASK 0x30
2946#define SONYPI_CAMERA_SHUTTER_MASK 0x7
2947
2948#define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
2949#define SONYPI_CAMERA_CONTROL 0x10
2950
2951#define SONYPI_CAMERA_STATUS 7
2952#define SONYPI_CAMERA_STATUS_READY 0x2
2953#define SONYPI_CAMERA_STATUS_POSITION 0x4
2954
2955#define SONYPI_DIRECTION_BACKWARDS 0x4
2956
2957#define SONYPI_CAMERA_REVISION 8
2958#define SONYPI_CAMERA_ROMVERSION 9
malattia@linux.it49a11de2007-04-09 19:28:56 +02002959
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002960static int __sony_pic_camera_ready(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002961{
2962 u8 v;
2963
2964 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
2965 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
2966}
2967
malattia@linux.ite3646322007-04-28 23:34:22 +09002968static int __sony_pic_camera_off(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002969{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002970 if (!camera) {
Joe Perches50f581a2011-03-29 15:21:48 -07002971 pr_warn("camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002972 return -ENODEV;
2973 }
2974
malattia@linux.it49a11de2007-04-09 19:28:56 +02002975 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
2976 SONYPI_CAMERA_MUTE_MASK),
2977 ITERATIONS_SHORT);
2978
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002979 if (spic_dev.camera_power) {
2980 sony_pic_call2(0x91, 0);
2981 spic_dev.camera_power = 0;
2982 }
2983 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002984}
2985
malattia@linux.ite3646322007-04-28 23:34:22 +09002986static int __sony_pic_camera_on(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002987{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002988 int i, j, x;
2989
2990 if (!camera) {
Joe Perches50f581a2011-03-29 15:21:48 -07002991 pr_warn("camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002992 return -ENODEV;
2993 }
malattia@linux.it49a11de2007-04-09 19:28:56 +02002994
2995 if (spic_dev.camera_power)
malattia@linux.ite3646322007-04-28 23:34:22 +09002996 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002997
2998 for (j = 5; j > 0; j--) {
2999
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003000 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003001 msleep(10);
3002 sony_pic_call1(0x93);
3003
3004 for (i = 400; i > 0; i--) {
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003005 if (__sony_pic_camera_ready())
malattia@linux.it49a11de2007-04-09 19:28:56 +02003006 break;
3007 msleep(10);
3008 }
3009 if (i)
3010 break;
3011 }
3012
3013 if (j == 0) {
Joe Perches50f581a2011-03-29 15:21:48 -07003014 pr_warn("failed to power on camera\n");
malattia@linux.ite3646322007-04-28 23:34:22 +09003015 return -ENODEV;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003016 }
3017
3018 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
3019 0x5a),
3020 ITERATIONS_SHORT);
3021
3022 spic_dev.camera_power = 1;
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003023 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003024}
3025
malattia@linux.ite3646322007-04-28 23:34:22 +09003026/* External camera command (exported to the motion eye v4l driver) */
3027int sony_pic_camera_command(int command, u8 value)
3028{
3029 if (!camera)
3030 return -EIO;
3031
3032 mutex_lock(&spic_dev.lock);
3033
3034 switch (command) {
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003035 case SONY_PIC_COMMAND_SETCAMERA:
malattia@linux.ite3646322007-04-28 23:34:22 +09003036 if (value)
3037 __sony_pic_camera_on();
3038 else
3039 __sony_pic_camera_off();
3040 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003041 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09003042 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
3043 ITERATIONS_SHORT);
3044 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003045 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
malattia@linux.ite3646322007-04-28 23:34:22 +09003046 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
3047 ITERATIONS_SHORT);
3048 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003049 case SONY_PIC_COMMAND_SETCAMERAHUE:
malattia@linux.ite3646322007-04-28 23:34:22 +09003050 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
3051 ITERATIONS_SHORT);
3052 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003053 case SONY_PIC_COMMAND_SETCAMERACOLOR:
malattia@linux.ite3646322007-04-28 23:34:22 +09003054 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
3055 ITERATIONS_SHORT);
3056 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003057 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09003058 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
3059 ITERATIONS_SHORT);
3060 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003061 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
malattia@linux.ite3646322007-04-28 23:34:22 +09003062 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
3063 ITERATIONS_SHORT);
3064 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003065 case SONY_PIC_COMMAND_SETCAMERAAGC:
malattia@linux.ite3646322007-04-28 23:34:22 +09003066 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
3067 ITERATIONS_SHORT);
3068 break;
3069 default:
Joe Perches50f581a2011-03-29 15:21:48 -07003070 pr_err("sony_pic_camera_command invalid: %d\n", command);
malattia@linux.ite3646322007-04-28 23:34:22 +09003071 break;
3072 }
3073 mutex_unlock(&spic_dev.lock);
3074 return 0;
3075}
3076EXPORT_SYMBOL(sony_pic_camera_command);
3077
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003078/* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09003079static void __sony_pic_set_wwanpower(u8 state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003080{
3081 state = !!state;
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09003082 if (spic_dev.wwan_power == state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003083 return;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003084 sony_pic_call2(0xB0, state);
Sergey Yanovich3ad1b762009-03-26 21:58:21 +09003085 sony_pic_call1(0x82);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003086 spic_dev.wwan_power = state;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003087}
3088
3089static ssize_t sony_pic_wwanpower_store(struct device *dev,
3090 struct device_attribute *attr,
3091 const char *buffer, size_t count)
3092{
3093 unsigned long value;
3094 if (count > 31)
3095 return -EINVAL;
3096
Mattia Dongili9e123372012-05-19 22:35:47 +09003097 if (kstrtoul(buffer, 10, &value))
3098 return -EINVAL;
3099
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09003100 mutex_lock(&spic_dev.lock);
3101 __sony_pic_set_wwanpower(value);
3102 mutex_unlock(&spic_dev.lock);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003103
3104 return count;
3105}
3106
3107static ssize_t sony_pic_wwanpower_show(struct device *dev,
3108 struct device_attribute *attr, char *buffer)
3109{
3110 ssize_t count;
3111 mutex_lock(&spic_dev.lock);
3112 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
3113 mutex_unlock(&spic_dev.lock);
3114 return count;
3115}
3116
malattia@linux.it49a11de2007-04-09 19:28:56 +02003117/* bluetooth subsystem power state */
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003118static void __sony_pic_set_bluetoothpower(u8 state)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003119{
3120 state = !!state;
3121 if (spic_dev.bluetooth_power == state)
3122 return;
3123 sony_pic_call2(0x96, state);
3124 sony_pic_call1(0x82);
3125 spic_dev.bluetooth_power = state;
3126}
3127
3128static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
3129 struct device_attribute *attr,
3130 const char *buffer, size_t count)
3131{
3132 unsigned long value;
3133 if (count > 31)
3134 return -EINVAL;
3135
Mattia Dongili9e123372012-05-19 22:35:47 +09003136 if (kstrtoul(buffer, 10, &value))
3137 return -EINVAL;
3138
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003139 mutex_lock(&spic_dev.lock);
3140 __sony_pic_set_bluetoothpower(value);
3141 mutex_unlock(&spic_dev.lock);
malattia@linux.it49a11de2007-04-09 19:28:56 +02003142
3143 return count;
3144}
3145
3146static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
3147 struct device_attribute *attr, char *buffer)
3148{
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003149 ssize_t count = 0;
3150 mutex_lock(&spic_dev.lock);
3151 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
3152 mutex_unlock(&spic_dev.lock);
3153 return count;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003154}
3155
3156/* fan speed */
3157/* FAN0 information (reverse engineered from ACPI tables) */
3158#define SONY_PIC_FAN0_STATUS 0x93
malattia@linux.it7b153f32007-04-09 19:31:25 +02003159static int sony_pic_set_fanspeed(unsigned long value)
3160{
3161 return ec_write(SONY_PIC_FAN0_STATUS, value);
3162}
3163
3164static int sony_pic_get_fanspeed(u8 *value)
3165{
3166 return ec_read(SONY_PIC_FAN0_STATUS, value);
3167}
3168
malattia@linux.it49a11de2007-04-09 19:28:56 +02003169static ssize_t sony_pic_fanspeed_store(struct device *dev,
3170 struct device_attribute *attr,
3171 const char *buffer, size_t count)
3172{
3173 unsigned long value;
3174 if (count > 31)
3175 return -EINVAL;
3176
Mattia Dongili9e123372012-05-19 22:35:47 +09003177 if (kstrtoul(buffer, 10, &value))
3178 return -EINVAL;
3179
malattia@linux.it7b153f32007-04-09 19:31:25 +02003180 if (sony_pic_set_fanspeed(value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02003181 return -EIO;
3182
3183 return count;
3184}
3185
3186static ssize_t sony_pic_fanspeed_show(struct device *dev,
3187 struct device_attribute *attr, char *buffer)
3188{
3189 u8 value = 0;
malattia@linux.it7b153f32007-04-09 19:31:25 +02003190 if (sony_pic_get_fanspeed(&value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02003191 return -EIO;
3192
3193 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
3194}
3195
3196#define SPIC_ATTR(_name, _mode) \
3197struct device_attribute spic_attr_##_name = __ATTR(_name, \
3198 _mode, sony_pic_## _name ##_show, \
3199 sony_pic_## _name ##_store)
3200
malattia@linux.it49a11de2007-04-09 19:28:56 +02003201static SPIC_ATTR(bluetoothpower, 0644);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003202static SPIC_ATTR(wwanpower, 0644);
malattia@linux.it49a11de2007-04-09 19:28:56 +02003203static SPIC_ATTR(fanspeed, 0644);
3204
3205static struct attribute *spic_attributes[] = {
malattia@linux.it49a11de2007-04-09 19:28:56 +02003206 &spic_attr_bluetoothpower.attr,
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003207 &spic_attr_wwanpower.attr,
malattia@linux.it49a11de2007-04-09 19:28:56 +02003208 &spic_attr_fanspeed.attr,
3209 NULL
3210};
3211
3212static struct attribute_group spic_attribute_group = {
3213 .attrs = spic_attributes
3214};
3215
malattia@linux.it7b153f32007-04-09 19:31:25 +02003216/******** SONYPI compatibility **********/
Mattia Dongilia64e62a2007-05-01 11:19:53 +09003217#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +02003218
3219/* battery / brightness / temperature addresses */
3220#define SONYPI_BAT_FLAGS 0x81
3221#define SONYPI_LCD_LIGHT 0x96
3222#define SONYPI_BAT1_PCTRM 0xa0
3223#define SONYPI_BAT1_LEFT 0xa2
3224#define SONYPI_BAT1_MAXRT 0xa4
3225#define SONYPI_BAT2_PCTRM 0xa8
3226#define SONYPI_BAT2_LEFT 0xaa
3227#define SONYPI_BAT2_MAXRT 0xac
3228#define SONYPI_BAT1_MAXTK 0xb0
3229#define SONYPI_BAT1_FULL 0xb2
3230#define SONYPI_BAT2_MAXTK 0xb8
3231#define SONYPI_BAT2_FULL 0xba
3232#define SONYPI_TEMP_STATUS 0xC1
3233
3234struct sonypi_compat_s {
3235 struct fasync_struct *fifo_async;
Stefani Seibold45465482009-12-21 14:37:26 -08003236 struct kfifo fifo;
malattia@linux.it7b153f32007-04-09 19:31:25 +02003237 spinlock_t fifo_lock;
3238 wait_queue_head_t fifo_proc_list;
3239 atomic_t open_count;
3240};
3241static struct sonypi_compat_s sonypi_compat = {
3242 .open_count = ATOMIC_INIT(0),
3243};
3244
3245static int sonypi_misc_fasync(int fd, struct file *filp, int on)
3246{
Jonathan Corbet60aa4922009-02-01 14:52:56 -07003247 return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003248}
3249
3250static int sonypi_misc_release(struct inode *inode, struct file *file)
3251{
malattia@linux.it7b153f32007-04-09 19:31:25 +02003252 atomic_dec(&sonypi_compat.open_count);
3253 return 0;
3254}
3255
3256static int sonypi_misc_open(struct inode *inode, struct file *file)
3257{
3258 /* Flush input queue on first open */
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09003259 unsigned long flags;
3260
Stefani Seibold45465482009-12-21 14:37:26 -08003261 spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09003262
malattia@linux.it7b153f32007-04-09 19:31:25 +02003263 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
Stefani Seibolde64c0262009-12-21 14:37:28 -08003264 kfifo_reset(&sonypi_compat.fifo);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09003265
Stefani Seibold45465482009-12-21 14:37:26 -08003266 spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09003267
malattia@linux.it7b153f32007-04-09 19:31:25 +02003268 return 0;
3269}
3270
3271static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
3272 size_t count, loff_t *pos)
3273{
3274 ssize_t ret;
3275 unsigned char c;
3276
Stefani Seibold45465482009-12-21 14:37:26 -08003277 if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
malattia@linux.it7b153f32007-04-09 19:31:25 +02003278 (file->f_flags & O_NONBLOCK))
3279 return -EAGAIN;
3280
3281 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
Stefani Seibold45465482009-12-21 14:37:26 -08003282 kfifo_len(&sonypi_compat.fifo) != 0);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003283 if (ret)
3284 return ret;
3285
3286 while (ret < count &&
Stefani Seibold7acd72e2009-12-21 14:37:28 -08003287 (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08003288 &sonypi_compat.fifo_lock) == sizeof(c))) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003289 if (put_user(c, buf++))
3290 return -EFAULT;
3291 ret++;
3292 }
3293
3294 if (ret > 0) {
3295 struct inode *inode = file->f_path.dentry->d_inode;
3296 inode->i_atime = current_fs_time(inode->i_sb);
3297 }
3298
3299 return ret;
3300}
3301
3302static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
3303{
3304 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
Stefani Seibold45465482009-12-21 14:37:26 -08003305 if (kfifo_len(&sonypi_compat.fifo))
malattia@linux.it7b153f32007-04-09 19:31:25 +02003306 return POLLIN | POLLRDNORM;
3307 return 0;
3308}
3309
3310static int ec_read16(u8 addr, u16 *value)
3311{
3312 u8 val_lb, val_hb;
3313 if (ec_read(addr, &val_lb))
3314 return -1;
3315 if (ec_read(addr + 1, &val_hb))
3316 return -1;
3317 *value = val_lb | (val_hb << 8);
3318 return 0;
3319}
3320
Alan Cox2b24ef02009-03-26 21:58:19 +09003321static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
3322 unsigned long arg)
malattia@linux.it7b153f32007-04-09 19:31:25 +02003323{
3324 int ret = 0;
3325 void __user *argp = (void __user *)arg;
3326 u8 val8;
3327 u16 val16;
3328 int value;
3329
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003330 mutex_lock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003331 switch (cmd) {
3332 case SONYPI_IOCGBRT:
Mattia Dongili62d2f232011-05-09 10:20:29 -04003333 if (sony_bl_props.dev == NULL) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003334 ret = -EIO;
3335 break;
3336 }
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09003337 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL,
3338 &value)) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003339 ret = -EIO;
3340 break;
3341 }
3342 val8 = ((value & 0xff) - 1) << 5;
3343 if (copy_to_user(argp, &val8, sizeof(val8)))
3344 ret = -EFAULT;
3345 break;
3346 case SONYPI_IOCSBRT:
Mattia Dongili62d2f232011-05-09 10:20:29 -04003347 if (sony_bl_props.dev == NULL) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003348 ret = -EIO;
3349 break;
3350 }
3351 if (copy_from_user(&val8, argp, sizeof(val8))) {
3352 ret = -EFAULT;
3353 break;
3354 }
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09003355 value = (val8 >> 5) + 1;
3356 if (sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &value,
3357 NULL)) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003358 ret = -EIO;
3359 break;
3360 }
3361 /* sync the backlight device status */
Mattia Dongili62d2f232011-05-09 10:20:29 -04003362 sony_bl_props.dev->props.brightness =
3363 sony_backlight_get_brightness(sony_bl_props.dev);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003364 break;
3365 case SONYPI_IOCGBAT1CAP:
3366 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
3367 ret = -EIO;
3368 break;
3369 }
3370 if (copy_to_user(argp, &val16, sizeof(val16)))
3371 ret = -EFAULT;
3372 break;
3373 case SONYPI_IOCGBAT1REM:
3374 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
3375 ret = -EIO;
3376 break;
3377 }
3378 if (copy_to_user(argp, &val16, sizeof(val16)))
3379 ret = -EFAULT;
3380 break;
3381 case SONYPI_IOCGBAT2CAP:
3382 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
3383 ret = -EIO;
3384 break;
3385 }
3386 if (copy_to_user(argp, &val16, sizeof(val16)))
3387 ret = -EFAULT;
3388 break;
3389 case SONYPI_IOCGBAT2REM:
3390 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
3391 ret = -EIO;
3392 break;
3393 }
3394 if (copy_to_user(argp, &val16, sizeof(val16)))
3395 ret = -EFAULT;
3396 break;
3397 case SONYPI_IOCGBATFLAGS:
3398 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
3399 ret = -EIO;
3400 break;
3401 }
3402 val8 &= 0x07;
3403 if (copy_to_user(argp, &val8, sizeof(val8)))
3404 ret = -EFAULT;
3405 break;
3406 case SONYPI_IOCGBLUE:
3407 val8 = spic_dev.bluetooth_power;
3408 if (copy_to_user(argp, &val8, sizeof(val8)))
3409 ret = -EFAULT;
3410 break;
3411 case SONYPI_IOCSBLUE:
3412 if (copy_from_user(&val8, argp, sizeof(val8))) {
3413 ret = -EFAULT;
3414 break;
3415 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003416 __sony_pic_set_bluetoothpower(val8);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003417 break;
3418 /* FAN Controls */
3419 case SONYPI_IOCGFAN:
3420 if (sony_pic_get_fanspeed(&val8)) {
3421 ret = -EIO;
3422 break;
3423 }
3424 if (copy_to_user(argp, &val8, sizeof(val8)))
3425 ret = -EFAULT;
3426 break;
3427 case SONYPI_IOCSFAN:
3428 if (copy_from_user(&val8, argp, sizeof(val8))) {
3429 ret = -EFAULT;
3430 break;
3431 }
3432 if (sony_pic_set_fanspeed(val8))
3433 ret = -EIO;
3434 break;
3435 /* GET Temperature (useful under APM) */
3436 case SONYPI_IOCGTEMP:
3437 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
3438 ret = -EIO;
3439 break;
3440 }
3441 if (copy_to_user(argp, &val8, sizeof(val8)))
3442 ret = -EFAULT;
3443 break;
3444 default:
3445 ret = -EINVAL;
3446 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003447 mutex_unlock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003448 return ret;
3449}
3450
3451static const struct file_operations sonypi_misc_fops = {
3452 .owner = THIS_MODULE,
3453 .read = sonypi_misc_read,
3454 .poll = sonypi_misc_poll,
3455 .open = sonypi_misc_open,
3456 .release = sonypi_misc_release,
3457 .fasync = sonypi_misc_fasync,
Alan Cox2b24ef02009-03-26 21:58:19 +09003458 .unlocked_ioctl = sonypi_misc_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003459 .llseek = noop_llseek,
malattia@linux.it7b153f32007-04-09 19:31:25 +02003460};
3461
3462static struct miscdevice sonypi_misc_device = {
3463 .minor = MISC_DYNAMIC_MINOR,
3464 .name = "sonypi",
3465 .fops = &sonypi_misc_fops,
3466};
3467
3468static void sonypi_compat_report_event(u8 event)
3469{
Stefani Seibold7acd72e2009-12-21 14:37:28 -08003470 kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08003471 sizeof(event), &sonypi_compat.fifo_lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003472 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
3473 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
3474}
3475
3476static int sonypi_compat_init(void)
3477{
3478 int error;
3479
3480 spin_lock_init(&sonypi_compat.fifo_lock);
Stefani Seibold45465482009-12-21 14:37:26 -08003481 error =
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08003482 kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -08003483 if (error) {
Joe Perches50f581a2011-03-29 15:21:48 -07003484 pr_err("kfifo_alloc failed\n");
Stefani Seibold45465482009-12-21 14:37:26 -08003485 return error;
malattia@linux.it7b153f32007-04-09 19:31:25 +02003486 }
3487
3488 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003489
3490 if (minor != -1)
3491 sonypi_misc_device.minor = minor;
3492 error = misc_register(&sonypi_misc_device);
3493 if (error) {
Joe Perches50f581a2011-03-29 15:21:48 -07003494 pr_err("misc_register failed\n");
malattia@linux.it7b153f32007-04-09 19:31:25 +02003495 goto err_free_kfifo;
3496 }
3497 if (minor == -1)
Joe Perches50f581a2011-03-29 15:21:48 -07003498 pr_info("device allocated minor is %d\n",
3499 sonypi_misc_device.minor);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003500
3501 return 0;
3502
3503err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -08003504 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003505 return error;
3506}
3507
3508static void sonypi_compat_exit(void)
3509{
3510 misc_deregister(&sonypi_misc_device);
Stefani Seibold45465482009-12-21 14:37:26 -08003511 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003512}
3513#else
3514static int sonypi_compat_init(void) { return 0; }
3515static void sonypi_compat_exit(void) { }
3516static void sonypi_compat_report_event(u8 event) { }
Mattia Dongilia64e62a2007-05-01 11:19:53 +09003517#endif /* CONFIG_SONYPI_COMPAT */
malattia@linux.it7b153f32007-04-09 19:31:25 +02003518
malattia@linux.it1549ee62007-04-09 10:19:08 +02003519/*
malattia@linux.it33a04452007-04-09 19:26:03 +02003520 * ACPI callbacks
malattia@linux.it1549ee62007-04-09 10:19:08 +02003521 */
malattia@linux.it33a04452007-04-09 19:26:03 +02003522static acpi_status
3523sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
3524{
3525 u32 i;
3526 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
3527
3528 switch (resource->type) {
3529 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003530 {
3531 /* start IO enumeration */
3532 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
3533 if (!ioport)
3534 return AE_ERROR;
3535
3536 list_add(&ioport->list, &dev->ioports);
3537 return AE_OK;
3538 }
3539
malattia@linux.it33a04452007-04-09 19:26:03 +02003540 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003541 /* end IO enumeration */
malattia@linux.it33a04452007-04-09 19:26:03 +02003542 return AE_OK;
3543
3544 case ACPI_RESOURCE_TYPE_IRQ:
3545 {
3546 struct acpi_resource_irq *p = &resource->data.irq;
3547 struct sony_pic_irq *interrupt = NULL;
3548 if (!p || !p->interrupt_count) {
3549 /*
3550 * IRQ descriptors may have no IRQ# bits set,
3551 * particularly those those w/ _STA disabled
3552 */
3553 dprintk("Blank IRQ resource\n");
3554 return AE_OK;
3555 }
3556 for (i = 0; i < p->interrupt_count; i++) {
3557 if (!p->interrupts[i]) {
Joe Perches50f581a2011-03-29 15:21:48 -07003558 pr_warn("Invalid IRQ %d\n",
3559 p->interrupts[i]);
malattia@linux.it33a04452007-04-09 19:26:03 +02003560 continue;
3561 }
3562 interrupt = kzalloc(sizeof(*interrupt),
3563 GFP_KERNEL);
3564 if (!interrupt)
3565 return AE_ERROR;
3566
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003567 list_add(&interrupt->list, &dev->interrupts);
malattia@linux.it33a04452007-04-09 19:26:03 +02003568 interrupt->irq.triggering = p->triggering;
3569 interrupt->irq.polarity = p->polarity;
3570 interrupt->irq.sharable = p->sharable;
3571 interrupt->irq.interrupt_count = 1;
3572 interrupt->irq.interrupts[0] = p->interrupts[i];
3573 }
3574 return AE_OK;
3575 }
3576 case ACPI_RESOURCE_TYPE_IO:
3577 {
3578 struct acpi_resource_io *io = &resource->data.io;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003579 struct sony_pic_ioport *ioport =
3580 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
malattia@linux.it33a04452007-04-09 19:26:03 +02003581 if (!io) {
3582 dprintk("Blank IO resource\n");
3583 return AE_OK;
3584 }
3585
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003586 if (!ioport->io1.minimum) {
3587 memcpy(&ioport->io1, io, sizeof(*io));
3588 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
3589 ioport->io1.address_length);
3590 }
3591 else if (!ioport->io2.minimum) {
3592 memcpy(&ioport->io2, io, sizeof(*io));
3593 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
3594 ioport->io2.address_length);
3595 }
3596 else {
Joe Perches50f581a2011-03-29 15:21:48 -07003597 pr_err("Unknown SPIC Type, more than 2 IO Ports\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003598 return AE_ERROR;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003599 }
malattia@linux.it33a04452007-04-09 19:26:03 +02003600 return AE_OK;
3601 }
3602 default:
3603 dprintk("Resource %d isn't an IRQ nor an IO port\n",
Joe Perches50f581a2011-03-29 15:21:48 -07003604 resource->type);
malattia@linux.it33a04452007-04-09 19:26:03 +02003605
3606 case ACPI_RESOURCE_TYPE_END_TAG:
3607 return AE_OK;
3608 }
3609 return AE_CTRL_TERMINATE;
3610}
3611
3612static int sony_pic_possible_resources(struct acpi_device *device)
3613{
3614 int result = 0;
3615 acpi_status status = AE_OK;
3616
3617 if (!device)
3618 return -EINVAL;
3619
3620 /* get device status */
3621 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
3622 dprintk("Evaluating _STA\n");
3623 result = acpi_bus_get_status(device);
3624 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07003625 pr_warn("Unable to read status\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003626 goto end;
3627 }
3628
3629 if (!device->status.enabled)
3630 dprintk("Device disabled\n");
3631 else
3632 dprintk("Device enabled\n");
3633
3634 /*
3635 * Query and parse 'method'
3636 */
3637 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
3638 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
3639 sony_pic_read_possible_resource, &spic_dev);
3640 if (ACPI_FAILURE(status)) {
Joe Perches50f581a2011-03-29 15:21:48 -07003641 pr_warn("Failure evaluating %s\n", METHOD_NAME__PRS);
malattia@linux.it33a04452007-04-09 19:26:03 +02003642 result = -ENODEV;
3643 }
3644end:
3645 return result;
3646}
3647
3648/*
3649 * Disable the spic device by calling its _DIS method
3650 */
3651static int sony_pic_disable(struct acpi_device *device)
3652{
Matthew Garrett6158d3a2008-10-29 14:01:03 -07003653 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
3654 NULL);
3655
3656 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
malattia@linux.it33a04452007-04-09 19:26:03 +02003657 return -ENXIO;
3658
3659 dprintk("Device disabled\n");
3660 return 0;
3661}
3662
3663
3664/*
3665 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
3666 *
3667 * Call _SRS to set current resources
3668 */
3669static int sony_pic_enable(struct acpi_device *device,
3670 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
3671{
3672 acpi_status status;
3673 int result = 0;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003674 /* Type 1 resource layout is:
3675 * IO
3676 * IO
3677 * IRQNoFlags
3678 * End
3679 *
3680 * Type 2 and 3 resource layout is:
3681 * IO
3682 * IRQNoFlags
3683 * End
3684 */
malattia@linux.it33a04452007-04-09 19:26:03 +02003685 struct {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003686 struct acpi_resource res1;
3687 struct acpi_resource res2;
3688 struct acpi_resource res3;
3689 struct acpi_resource res4;
malattia@linux.it33a04452007-04-09 19:26:03 +02003690 } *resource;
3691 struct acpi_buffer buffer = { 0, NULL };
3692
3693 if (!ioport || !irq)
3694 return -EINVAL;
3695
3696 /* init acpi_buffer */
3697 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
3698 if (!resource)
3699 return -ENOMEM;
3700
3701 buffer.length = sizeof(*resource) + 1;
3702 buffer.pointer = resource;
3703
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003704 /* setup Type 1 resources */
Mattia Dongili31df7142009-09-16 00:05:29 +09003705 if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003706
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003707 /* setup io resources */
3708 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
3709 resource->res1.length = sizeof(struct acpi_resource);
3710 memcpy(&resource->res1.data.io, &ioport->io1,
3711 sizeof(struct acpi_resource_io));
malattia@linux.it33a04452007-04-09 19:26:03 +02003712
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003713 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
3714 resource->res2.length = sizeof(struct acpi_resource);
3715 memcpy(&resource->res2.data.io, &ioport->io2,
3716 sizeof(struct acpi_resource_io));
3717
3718 /* setup irq resource */
3719 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
3720 resource->res3.length = sizeof(struct acpi_resource);
3721 memcpy(&resource->res3.data.irq, &irq->irq,
3722 sizeof(struct acpi_resource_irq));
3723 /* we requested a shared irq */
3724 resource->res3.data.irq.sharable = ACPI_SHARED;
3725
3726 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
3727
3728 }
3729 /* setup Type 2/3 resources */
3730 else {
3731 /* setup io resource */
3732 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
3733 resource->res1.length = sizeof(struct acpi_resource);
3734 memcpy(&resource->res1.data.io, &ioport->io1,
3735 sizeof(struct acpi_resource_io));
3736
3737 /* setup irq resource */
3738 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
3739 resource->res2.length = sizeof(struct acpi_resource);
3740 memcpy(&resource->res2.data.irq, &irq->irq,
3741 sizeof(struct acpi_resource_irq));
3742 /* we requested a shared irq */
3743 resource->res2.data.irq.sharable = ACPI_SHARED;
3744
3745 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
3746 }
malattia@linux.it33a04452007-04-09 19:26:03 +02003747
3748 /* Attempt to set the resource */
3749 dprintk("Evaluating _SRS\n");
3750 status = acpi_set_current_resources(device->handle, &buffer);
3751
3752 /* check for total failure */
3753 if (ACPI_FAILURE(status)) {
Joe Perches50f581a2011-03-29 15:21:48 -07003754 pr_err("Error evaluating _SRS\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003755 result = -ENODEV;
3756 goto end;
3757 }
3758
3759 /* Necessary device initializations calls (from sonypi) */
3760 sony_pic_call1(0x82);
3761 sony_pic_call2(0x81, 0xff);
3762 sony_pic_call1(compat ? 0x92 : 0x82);
3763
3764end:
3765 kfree(resource);
3766 return result;
3767}
3768
3769/*****************
3770 *
3771 * ISR: some event is available
3772 *
3773 *****************/
3774static irqreturn_t sony_pic_irq(int irq, void *dev_id)
3775{
3776 int i, j;
malattia@linux.it33a04452007-04-09 19:26:03 +02003777 u8 ev = 0;
3778 u8 data_mask = 0;
3779 u8 device_event = 0;
3780
3781 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
3782
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003783 ev = inb_p(dev->cur_ioport->io1.minimum);
3784 if (dev->cur_ioport->io2.minimum)
3785 data_mask = inb_p(dev->cur_ioport->io2.minimum);
3786 else
Mattia Dongilide920432008-01-14 18:05:43 +09003787 data_mask = inb_p(dev->cur_ioport->io1.minimum +
Mattia Dongili31df7142009-09-16 00:05:29 +09003788 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003789
Mattia Dongili22a17782007-07-16 02:34:39 +09003790 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
Mattia Dongilide920432008-01-14 18:05:43 +09003791 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09003792 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003793
3794 if (ev == 0x00 || ev == 0xff)
3795 return IRQ_HANDLED;
3796
Mattia Dongili31df7142009-09-16 00:05:29 +09003797 for (i = 0; dev->event_types[i].mask; i++) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003798
Mattia Dongili31df7142009-09-16 00:05:29 +09003799 if ((data_mask & dev->event_types[i].data) !=
3800 dev->event_types[i].data)
malattia@linux.it33a04452007-04-09 19:26:03 +02003801 continue;
3802
Mattia Dongili31df7142009-09-16 00:05:29 +09003803 if (!(mask & dev->event_types[i].mask))
malattia@linux.it33a04452007-04-09 19:26:03 +02003804 continue;
3805
Mattia Dongili31df7142009-09-16 00:05:29 +09003806 for (j = 0; dev->event_types[i].events[j].event; j++) {
3807 if (ev == dev->event_types[i].events[j].data) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003808 device_event =
Mattia Dongili31df7142009-09-16 00:05:29 +09003809 dev->event_types[i].events[j].event;
Mattia Dongili4eeb5022011-02-19 11:52:27 +09003810 /* some events may require ignoring */
3811 if (!device_event)
3812 return IRQ_HANDLED;
malattia@linux.it33a04452007-04-09 19:26:03 +02003813 goto found;
3814 }
3815 }
3816 }
Mattia Dongili3eb87492008-01-14 18:05:45 +09003817 /* Still not able to decode the event try to pass
3818 * it over to the minidriver
3819 */
Mattia Dongili31df7142009-09-16 00:05:29 +09003820 if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
Mattia Dongili3eb87492008-01-14 18:05:45 +09003821 return IRQ_HANDLED;
3822
Mattia Dongilide920432008-01-14 18:05:43 +09003823 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
3824 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09003825 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003826 return IRQ_HANDLED;
3827
3828found:
malattia@linux.it1549ee62007-04-09 10:19:08 +02003829 sony_laptop_report_input_event(device_event);
Mattia Dongilide920432008-01-14 18:05:43 +09003830 acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003831 sonypi_compat_report_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02003832 return IRQ_HANDLED;
3833}
3834
3835/*****************
3836 *
3837 * ACPI driver
3838 *
3839 *****************/
3840static int sony_pic_remove(struct acpi_device *device, int type)
3841{
3842 struct sony_pic_ioport *io, *tmp_io;
3843 struct sony_pic_irq *irq, *tmp_irq;
3844
3845 if (sony_pic_disable(device)) {
Joe Perches50f581a2011-03-29 15:21:48 -07003846 pr_err("Couldn't disable device\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003847 return -ENXIO;
3848 }
3849
3850 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003851 release_region(spic_dev.cur_ioport->io1.minimum,
3852 spic_dev.cur_ioport->io1.address_length);
3853 if (spic_dev.cur_ioport->io2.minimum)
3854 release_region(spic_dev.cur_ioport->io2.minimum,
3855 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02003856
Mattia Dongili015a9162007-08-12 16:20:27 +09003857 sonypi_compat_exit();
3858
malattia@linux.it1549ee62007-04-09 10:19:08 +02003859 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02003860
malattia@linux.it49a11de2007-04-09 19:28:56 +02003861 /* pf attrs */
3862 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
3863 sony_pf_remove();
3864
malattia@linux.it33a04452007-04-09 19:26:03 +02003865 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
3866 list_del(&io->list);
3867 kfree(io);
3868 }
3869 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
3870 list_del(&irq->list);
3871 kfree(irq);
3872 }
3873 spic_dev.cur_ioport = NULL;
3874 spic_dev.cur_irq = NULL;
3875
malattia@linux.itf6119b02007-04-09 19:31:06 +02003876 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003877 return 0;
3878}
3879
3880static int sony_pic_add(struct acpi_device *device)
3881{
3882 int result;
3883 struct sony_pic_ioport *io, *tmp_io;
3884 struct sony_pic_irq *irq, *tmp_irq;
3885
Joe Perches50f581a2011-03-29 15:21:48 -07003886 pr_info("%s v%s\n", SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.it33a04452007-04-09 19:26:03 +02003887
3888 spic_dev.acpi_dev = device;
3889 strcpy(acpi_device_class(device), "sony/hotkey");
Mattia Dongilide920432008-01-14 18:05:43 +09003890 sony_pic_detect_device_type(&spic_dev);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003891 mutex_init(&spic_dev.lock);
malattia@linux.it33a04452007-04-09 19:26:03 +02003892
3893 /* read _PRS resources */
3894 result = sony_pic_possible_resources(device);
3895 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07003896 pr_err("Unable to read possible resources\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003897 goto err_free_resources;
3898 }
3899
3900 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05003901 result = sony_laptop_setup_input(device);
malattia@linux.it33a04452007-04-09 19:26:03 +02003902 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07003903 pr_err("Unable to create input devices\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003904 goto err_free_resources;
3905 }
3906
Mattia Dongili015a9162007-08-12 16:20:27 +09003907 if (sonypi_compat_init())
3908 goto err_remove_input;
3909
malattia@linux.it33a04452007-04-09 19:26:03 +02003910 /* request io port */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003911 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
3912 if (request_region(io->io1.minimum, io->io1.address_length,
Lucas De Marchic8440332011-03-17 17:18:22 -03003913 "Sony Programmable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003914 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
3915 io->io1.minimum, io->io1.maximum,
3916 io->io1.address_length);
3917 /* Type 1 have 2 ioports */
3918 if (io->io2.minimum) {
3919 if (request_region(io->io2.minimum,
3920 io->io2.address_length,
Lucas De Marchic8440332011-03-17 17:18:22 -03003921 "Sony Programmable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003922 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
3923 io->io2.minimum, io->io2.maximum,
3924 io->io2.address_length);
3925 spic_dev.cur_ioport = io;
3926 break;
3927 }
3928 else {
3929 dprintk("Unable to get I/O port2: "
3930 "0x%.4x (0x%.4x) + 0x%.2x\n",
3931 io->io2.minimum, io->io2.maximum,
3932 io->io2.address_length);
3933 release_region(io->io1.minimum,
3934 io->io1.address_length);
3935 }
3936 }
3937 else {
3938 spic_dev.cur_ioport = io;
3939 break;
3940 }
malattia@linux.it33a04452007-04-09 19:26:03 +02003941 }
3942 }
3943 if (!spic_dev.cur_ioport) {
Joe Perches50f581a2011-03-29 15:21:48 -07003944 pr_err("Failed to request_region\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003945 result = -ENODEV;
Mattia Dongili015a9162007-08-12 16:20:27 +09003946 goto err_remove_compat;
malattia@linux.it33a04452007-04-09 19:26:03 +02003947 }
3948
3949 /* request IRQ */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003950 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003951 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
Yong Zhang6a0d89c2011-10-22 17:56:50 +08003952 0, "sony-laptop", &spic_dev)) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003953 dprintk("IRQ: %d - triggering: %d - "
3954 "polarity: %d - shr: %d\n",
3955 irq->irq.interrupts[0],
3956 irq->irq.triggering,
3957 irq->irq.polarity,
3958 irq->irq.sharable);
3959 spic_dev.cur_irq = irq;
3960 break;
3961 }
3962 }
3963 if (!spic_dev.cur_irq) {
Joe Perches50f581a2011-03-29 15:21:48 -07003964 pr_err("Failed to request_irq\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003965 result = -ENODEV;
3966 goto err_release_region;
3967 }
3968
3969 /* set resource status _SRS */
3970 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
3971 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07003972 pr_err("Couldn't enable device\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003973 goto err_free_irq;
3974 }
3975
malattia@linux.it49a11de2007-04-09 19:28:56 +02003976 spic_dev.bluetooth_power = -1;
3977 /* create device attributes */
3978 result = sony_pf_add();
3979 if (result)
3980 goto err_disable_device;
3981
3982 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
3983 if (result)
3984 goto err_remove_pf;
3985
malattia@linux.it33a04452007-04-09 19:26:03 +02003986 return 0;
3987
malattia@linux.it49a11de2007-04-09 19:28:56 +02003988err_remove_pf:
3989 sony_pf_remove();
3990
3991err_disable_device:
3992 sony_pic_disable(device);
3993
malattia@linux.it33a04452007-04-09 19:26:03 +02003994err_free_irq:
3995 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
3996
3997err_release_region:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003998 release_region(spic_dev.cur_ioport->io1.minimum,
3999 spic_dev.cur_ioport->io1.address_length);
4000 if (spic_dev.cur_ioport->io2.minimum)
4001 release_region(spic_dev.cur_ioport->io2.minimum,
4002 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02004003
Mattia Dongili015a9162007-08-12 16:20:27 +09004004err_remove_compat:
4005 sonypi_compat_exit();
4006
malattia@linux.it33a04452007-04-09 19:26:03 +02004007err_remove_input:
malattia@linux.it1549ee62007-04-09 10:19:08 +02004008 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02004009
4010err_free_resources:
4011 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
4012 list_del(&io->list);
4013 kfree(io);
4014 }
4015 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
4016 list_del(&irq->list);
4017 kfree(irq);
4018 }
4019 spic_dev.cur_ioport = NULL;
4020 spic_dev.cur_irq = NULL;
4021
4022 return result;
4023}
4024
4025static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
4026{
4027 if (sony_pic_disable(device))
4028 return -ENXIO;
4029 return 0;
4030}
4031
4032static int sony_pic_resume(struct acpi_device *device)
4033{
4034 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
4035 return 0;
4036}
4037
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004038static const struct acpi_device_id sony_pic_device_ids[] = {
4039 {SONY_PIC_HID, 0},
4040 {"", 0},
4041};
4042
malattia@linux.it33a04452007-04-09 19:26:03 +02004043static struct acpi_driver sony_pic_driver = {
4044 .name = SONY_PIC_DRIVER_NAME,
4045 .class = SONY_PIC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004046 .ids = sony_pic_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02004047 .owner = THIS_MODULE,
4048 .ops = {
4049 .add = sony_pic_add,
4050 .remove = sony_pic_remove,
4051 .suspend = sony_pic_suspend,
4052 .resume = sony_pic_resume,
4053 },
4054};
4055
4056static struct dmi_system_id __initdata sonypi_dmi_table[] = {
4057 {
4058 .ident = "Sony Vaio",
4059 .matches = {
4060 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
4061 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
4062 },
4063 },
4064 {
4065 .ident = "Sony Vaio",
4066 .matches = {
4067 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
4068 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
4069 },
4070 },
4071 { }
4072};
4073
malattia@linux.it59b19102007-04-09 10:19:04 +02004074static int __init sony_laptop_init(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01004075{
malattia@linux.it33a04452007-04-09 19:26:03 +02004076 int result;
4077
4078 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
4079 result = acpi_bus_register_driver(&sony_pic_driver);
4080 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07004081 pr_err("Unable to register SPIC driver\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004082 goto out;
4083 }
Alan Jenkins5e6f9722009-09-16 00:05:32 +09004084 spic_drv_registered = 1;
malattia@linux.it33a04452007-04-09 19:26:03 +02004085 }
4086
4087 result = acpi_bus_register_driver(&sony_nc_driver);
4088 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07004089 pr_err("Unable to register SNC driver\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004090 goto out_unregister_pic;
4091 }
4092
4093 return 0;
4094
4095out_unregister_pic:
Alan Jenkins5e6f9722009-09-16 00:05:32 +09004096 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02004097 acpi_bus_unregister_driver(&sony_pic_driver);
4098out:
4099 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +01004100}
4101
malattia@linux.it59b19102007-04-09 10:19:04 +02004102static void __exit sony_laptop_exit(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01004103{
malattia@linux.it59b19102007-04-09 10:19:04 +02004104 acpi_bus_unregister_driver(&sony_nc_driver);
Alan Jenkins5e6f9722009-09-16 00:05:32 +09004105 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02004106 acpi_bus_unregister_driver(&sony_pic_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +01004107}
4108
malattia@linux.it59b19102007-04-09 10:19:04 +02004109module_init(sony_laptop_init);
4110module_exit(sony_laptop_exit);