blob: 4420353cfb6811eefe6c616f7e84442fa5fc8d93 [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
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900147enum sony_nc_rfkill {
148 SONY_WIFI,
149 SONY_BLUETOOTH,
150 SONY_WWAN,
151 SONY_WIMAX,
Johannes Berg19d337d2009-06-02 13:01:37 +0200152 N_SONY_RFKILL,
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900153};
154
Mattia Dongilid5a664a2009-12-17 00:08:35 +0900155static int sony_rfkill_handle;
Johannes Berg19d337d2009-06-02 13:01:37 +0200156static struct rfkill *sony_rfkill_devices[N_SONY_RFKILL];
157static int sony_rfkill_address[N_SONY_RFKILL] = {0x300, 0x500, 0x700, 0x900};
Marco Chiappero5fe801a2012-05-19 22:35:48 +0900158static void sony_nc_rfkill_setup(struct acpi_device *device);
159static void sony_nc_rfkill_cleanup(void);
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900160static void sony_nc_rfkill_update(void);
161
malattia@linux.it1549ee62007-04-09 10:19:08 +0200162/*********** Input Devices ***********/
163
164#define SONY_LAPTOP_BUF_SIZE 128
165struct sony_laptop_input_s {
166 atomic_t users;
167 struct input_dev *jog_dev;
168 struct input_dev *key_dev;
Stefani Seibold45465482009-12-21 14:37:26 -0800169 struct kfifo fifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200170 spinlock_t fifo_lock;
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800171 struct timer_list release_key_timer;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200172};
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900173
malattia@linux.it1549ee62007-04-09 10:19:08 +0200174static struct sony_laptop_input_s sony_laptop_input = {
175 .users = ATOMIC_INIT(0),
176};
177
178struct sony_laptop_keypress {
179 struct input_dev *dev;
180 int key;
181};
182
Mattia Dongilibc57f862007-07-20 02:01:57 +0900183/* Correspondance table between sonypi events
184 * and input layer indexes in the keymap
185 */
186static int sony_laptop_input_index[] = {
Mattia Dongili3eb87492008-01-14 18:05:45 +0900187 -1, /* 0 no event */
188 -1, /* 1 SONYPI_EVENT_JOGDIAL_DOWN */
189 -1, /* 2 SONYPI_EVENT_JOGDIAL_UP */
190 -1, /* 3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
191 -1, /* 4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */
192 -1, /* 5 SONYPI_EVENT_JOGDIAL_PRESSED */
193 -1, /* 6 SONYPI_EVENT_JOGDIAL_RELEASED */
194 0, /* 7 SONYPI_EVENT_CAPTURE_PRESSED */
195 1, /* 8 SONYPI_EVENT_CAPTURE_RELEASED */
196 2, /* 9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
197 3, /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
198 4, /* 11 SONYPI_EVENT_FNKEY_ESC */
199 5, /* 12 SONYPI_EVENT_FNKEY_F1 */
200 6, /* 13 SONYPI_EVENT_FNKEY_F2 */
201 7, /* 14 SONYPI_EVENT_FNKEY_F3 */
202 8, /* 15 SONYPI_EVENT_FNKEY_F4 */
203 9, /* 16 SONYPI_EVENT_FNKEY_F5 */
204 10, /* 17 SONYPI_EVENT_FNKEY_F6 */
205 11, /* 18 SONYPI_EVENT_FNKEY_F7 */
206 12, /* 19 SONYPI_EVENT_FNKEY_F8 */
207 13, /* 20 SONYPI_EVENT_FNKEY_F9 */
208 14, /* 21 SONYPI_EVENT_FNKEY_F10 */
209 15, /* 22 SONYPI_EVENT_FNKEY_F11 */
210 16, /* 23 SONYPI_EVENT_FNKEY_F12 */
211 17, /* 24 SONYPI_EVENT_FNKEY_1 */
212 18, /* 25 SONYPI_EVENT_FNKEY_2 */
213 19, /* 26 SONYPI_EVENT_FNKEY_D */
214 20, /* 27 SONYPI_EVENT_FNKEY_E */
215 21, /* 28 SONYPI_EVENT_FNKEY_F */
216 22, /* 29 SONYPI_EVENT_FNKEY_S */
217 23, /* 30 SONYPI_EVENT_FNKEY_B */
218 24, /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */
219 25, /* 32 SONYPI_EVENT_PKEY_P1 */
220 26, /* 33 SONYPI_EVENT_PKEY_P2 */
221 27, /* 34 SONYPI_EVENT_PKEY_P3 */
222 28, /* 35 SONYPI_EVENT_BACK_PRESSED */
223 -1, /* 36 SONYPI_EVENT_LID_CLOSED */
224 -1, /* 37 SONYPI_EVENT_LID_OPENED */
225 29, /* 38 SONYPI_EVENT_BLUETOOTH_ON */
226 30, /* 39 SONYPI_EVENT_BLUETOOTH_OFF */
227 31, /* 40 SONYPI_EVENT_HELP_PRESSED */
228 32, /* 41 SONYPI_EVENT_FNKEY_ONLY */
229 33, /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
230 34, /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */
231 35, /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
232 36, /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
233 37, /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
234 38, /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */
235 39, /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
236 40, /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
237 41, /* 50 SONYPI_EVENT_ZOOM_PRESSED */
238 42, /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */
239 43, /* 52 SONYPI_EVENT_MEYE_FACE */
240 44, /* 53 SONYPI_EVENT_MEYE_OPPOSITE */
241 45, /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */
242 46, /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */
243 -1, /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */
244 -1, /* 57 SONYPI_EVENT_BATTERY_INSERT */
245 -1, /* 58 SONYPI_EVENT_BATTERY_REMOVE */
246 -1, /* 59 SONYPI_EVENT_FNKEY_RELEASED */
247 47, /* 60 SONYPI_EVENT_WIRELESS_ON */
248 48, /* 61 SONYPI_EVENT_WIRELESS_OFF */
249 49, /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */
250 50, /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900251 51, /* 64 SONYPI_EVENT_CD_EJECT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900252 52, /* 65 SONYPI_EVENT_MODEKEY_PRESSED */
253 53, /* 66 SONYPI_EVENT_PKEY_P4 */
254 54, /* 67 SONYPI_EVENT_PKEY_P5 */
255 55, /* 68 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900256 56, /* 69 SONYPI_EVENT_VOLUME_INC_PRESSED */
257 57, /* 70 SONYPI_EVENT_VOLUME_DEC_PRESSED */
258 -1, /* 71 SONYPI_EVENT_BRIGHTNESS_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900259 58, /* 72 SONYPI_EVENT_MEDIA_PRESSED */
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900260 59, /* 72 SONYPI_EVENT_VENDOR_PRESSED */
Mattia Dongilibc57f862007-07-20 02:01:57 +0900261};
262
263static int sony_laptop_input_keycode_map[] = {
264 KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
265 KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
266 KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
267 KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
268 KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
269 KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
270 KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
271 KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
272 KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
273 KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
274 KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
275 KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
276 KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
277 KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
278 KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
279 KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
280 KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
281 KEY_FN_F1, /* 17 SONYPI_EVENT_FNKEY_1 */
282 KEY_FN_F2, /* 18 SONYPI_EVENT_FNKEY_2 */
283 KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
284 KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
285 KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
286 KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
287 KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
288 KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
289 KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
290 KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
291 KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
292 KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
293 KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
294 KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
295 KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
296 KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
297 KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
298 KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
299 KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
300 KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
301 KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
302 KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
303 KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
304 KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
305 KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
306 BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
307 KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
308 KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
309 KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
310 KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
311 KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
312 KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
Mattia Dongili3eb87492008-01-14 18:05:45 +0900313 KEY_ZOOMIN, /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900314 KEY_ZOOMOUT, /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900315 KEY_EJECTCD, /* 51 SONYPI_EVENT_CD_EJECT_PRESSED */
316 KEY_F13, /* 52 SONYPI_EVENT_MODEKEY_PRESSED */
317 KEY_PROG4, /* 53 SONYPI_EVENT_PKEY_P4 */
318 KEY_F14, /* 54 SONYPI_EVENT_PKEY_P5 */
319 KEY_F15, /* 55 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900320 KEY_VOLUMEUP, /* 56 SONYPI_EVENT_VOLUME_INC_PRESSED */
321 KEY_VOLUMEDOWN, /* 57 SONYPI_EVENT_VOLUME_DEC_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900322 KEY_MEDIA, /* 58 SONYPI_EVENT_MEDIA_PRESSED */
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900323 KEY_VENDOR, /* 59 SONYPI_EVENT_VENDOR_PRESSED */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200324};
325
326/* release buttons after a short delay if pressed */
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800327static void do_sony_laptop_release_key(unsigned long unused)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200328{
329 struct sony_laptop_keypress kp;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800330 unsigned long flags;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200331
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800332 spin_lock_irqsave(&sony_laptop_input.fifo_lock, flags);
333
334 if (kfifo_out(&sony_laptop_input.fifo,
335 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200336 input_report_key(kp.dev, kp.key, 0);
337 input_sync(kp.dev);
338 }
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800339
340 /* If there is something in the fifo schedule next release. */
341 if (kfifo_len(&sony_laptop_input.fifo) != 0)
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800342 mod_timer(&sony_laptop_input.release_key_timer,
343 jiffies + msecs_to_jiffies(10));
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800344
345 spin_unlock_irqrestore(&sony_laptop_input.fifo_lock, flags);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200346}
malattia@linux.it1549ee62007-04-09 10:19:08 +0200347
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200348/* forward event to the input subsystem */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200349static void sony_laptop_report_input_event(u8 event)
350{
351 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
352 struct input_dev *key_dev = sony_laptop_input.key_dev;
353 struct sony_laptop_keypress kp = { NULL };
John Hughes747a5622011-11-16 19:51:57 +0100354 int scancode = -1;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200355
Almer S. Tigelaara83021a2009-04-12 11:26:28 +0000356 if (event == SONYPI_EVENT_FNKEY_RELEASED ||
357 event == SONYPI_EVENT_ANYBUTTON_RELEASED) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200358 /* Nothing, not all VAIOs generate this event */
359 return;
360 }
361
362 /* report events */
363 switch (event) {
364 /* jog_dev events */
365 case SONYPI_EVENT_JOGDIAL_UP:
366 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
367 input_report_rel(jog_dev, REL_WHEEL, 1);
368 input_sync(jog_dev);
369 return;
370
371 case SONYPI_EVENT_JOGDIAL_DOWN:
372 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
373 input_report_rel(jog_dev, REL_WHEEL, -1);
374 input_sync(jog_dev);
375 return;
376
377 /* key_dev events */
378 case SONYPI_EVENT_JOGDIAL_PRESSED:
379 kp.key = BTN_MIDDLE;
380 kp.dev = jog_dev;
381 break;
382
383 default:
Adrian Bunkd399d132008-02-20 00:59:03 +0200384 if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
Mattia Dongilibc57f862007-07-20 02:01:57 +0900385 dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
386 break;
387 }
John Hughes747a5622011-11-16 19:51:57 +0100388 if ((scancode = sony_laptop_input_index[event]) != -1) {
389 kp.key = sony_laptop_input_keycode_map[scancode];
Mattia Dongilibc57f862007-07-20 02:01:57 +0900390 if (kp.key != KEY_UNKNOWN)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200391 kp.dev = key_dev;
Mattia Dongilibc57f862007-07-20 02:01:57 +0900392 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200393 break;
394 }
395
396 if (kp.dev) {
John Hughes747a5622011-11-16 19:51:57 +0100397 /* if we have a scancode we emit it so we can always
398 remap the key */
399 if (scancode != -1)
400 input_event(kp.dev, EV_MSC, MSC_SCAN, scancode);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200401 input_report_key(kp.dev, kp.key, 1);
402 input_sync(kp.dev);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200403
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800404 /* schedule key release */
405 kfifo_in_locked(&sony_laptop_input.fifo,
406 (unsigned char *)&kp, sizeof(kp),
407 &sony_laptop_input.fifo_lock);
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800408 mod_timer(&sony_laptop_input.release_key_timer,
409 jiffies + msecs_to_jiffies(10));
malattia@linux.it1549ee62007-04-09 10:19:08 +0200410 } else
411 dprintk("unknown input event %.2x\n", event);
412}
413
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500414static int sony_laptop_setup_input(struct acpi_device *acpi_device)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200415{
416 struct input_dev *jog_dev;
417 struct input_dev *key_dev;
418 int i;
419 int error;
420
421 /* don't run again if already initialized */
422 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
423 return 0;
424
425 /* kfifo */
426 spin_lock_init(&sony_laptop_input.fifo_lock);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800427 error = kfifo_alloc(&sony_laptop_input.fifo,
428 SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -0800429 if (error) {
Joe Perches50f581a2011-03-29 15:21:48 -0700430 pr_err("kfifo_alloc failed\n");
malattia@linux.it1549ee62007-04-09 10:19:08 +0200431 goto err_dec_users;
432 }
433
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800434 setup_timer(&sony_laptop_input.release_key_timer,
435 do_sony_laptop_release_key, 0);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200436
437 /* input keys */
438 key_dev = input_allocate_device();
439 if (!key_dev) {
440 error = -ENOMEM;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800441 goto err_free_kfifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200442 }
443
444 key_dev->name = "Sony Vaio Keys";
445 key_dev->id.bustype = BUS_ISA;
446 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500447 key_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200448
449 /* Initialize the Input Drivers: special keys */
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800450 input_set_capability(key_dev, EV_MSC, MSC_SCAN);
451
452 __set_bit(EV_KEY, key_dev->evbit);
Mattia Dongilibc57f862007-07-20 02:01:57 +0900453 key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
454 key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
455 key_dev->keycode = &sony_laptop_input_keycode_map;
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800456 for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++)
457 __set_bit(sony_laptop_input_keycode_map[i], key_dev->keybit);
458 __clear_bit(KEY_RESERVED, key_dev->keybit);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200459
460 error = input_register_device(key_dev);
461 if (error)
462 goto err_free_keydev;
463
464 sony_laptop_input.key_dev = key_dev;
465
466 /* jogdial */
467 jog_dev = input_allocate_device();
468 if (!jog_dev) {
469 error = -ENOMEM;
470 goto err_unregister_keydev;
471 }
472
473 jog_dev->name = "Sony Vaio Jogdial";
474 jog_dev->id.bustype = BUS_ISA;
475 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
John Hughes747a5622011-11-16 19:51:57 +0100476 jog_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200477
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800478 input_set_capability(jog_dev, EV_KEY, BTN_MIDDLE);
479 input_set_capability(jog_dev, EV_REL, REL_WHEEL);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200480
481 error = input_register_device(jog_dev);
482 if (error)
483 goto err_free_jogdev;
484
485 sony_laptop_input.jog_dev = jog_dev;
486
487 return 0;
488
489err_free_jogdev:
490 input_free_device(jog_dev);
491
492err_unregister_keydev:
493 input_unregister_device(key_dev);
494 /* to avoid kref underflow below at input_free_device */
495 key_dev = NULL;
496
497err_free_keydev:
498 input_free_device(key_dev);
499
malattia@linux.it1549ee62007-04-09 10:19:08 +0200500err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -0800501 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200502
503err_dec_users:
504 atomic_dec(&sony_laptop_input.users);
505 return error;
506}
507
508static void sony_laptop_remove_input(void)
509{
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800510 struct sony_laptop_keypress kp = { NULL };
511
512 /* Cleanup only after the last user has gone */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200513 if (!atomic_dec_and_test(&sony_laptop_input.users))
514 return;
515
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800516 del_timer_sync(&sony_laptop_input.release_key_timer);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800517
518 /*
519 * Generate key-up events for remaining keys. Note that we don't
520 * need locking since nobody is adding new events to the kfifo.
521 */
522 while (kfifo_out(&sony_laptop_input.fifo,
523 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
524 input_report_key(kp.dev, kp.key, 0);
525 input_sync(kp.dev);
526 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200527
528 /* destroy input devs */
529 input_unregister_device(sony_laptop_input.key_dev);
530 sony_laptop_input.key_dev = NULL;
531
532 if (sony_laptop_input.jog_dev) {
533 input_unregister_device(sony_laptop_input.jog_dev);
534 sony_laptop_input.jog_dev = NULL;
535 }
536
Stefani Seibold45465482009-12-21 14:37:26 -0800537 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200538}
539
malattia@linux.it56b87562007-04-09 10:19:05 +0200540/*********** Platform Device ***********/
541
542static atomic_t sony_pf_users = ATOMIC_INIT(0);
543static struct platform_driver sony_pf_driver = {
544 .driver = {
545 .name = "sony-laptop",
546 .owner = THIS_MODULE,
547 }
548};
549static struct platform_device *sony_pf_device;
550
551static int sony_pf_add(void)
552{
553 int ret = 0;
554
555 /* don't run again if already initialized */
556 if (atomic_add_return(1, &sony_pf_users) > 1)
557 return 0;
558
559 ret = platform_driver_register(&sony_pf_driver);
560 if (ret)
561 goto out;
562
563 sony_pf_device = platform_device_alloc("sony-laptop", -1);
564 if (!sony_pf_device) {
565 ret = -ENOMEM;
566 goto out_platform_registered;
567 }
568
569 ret = platform_device_add(sony_pf_device);
570 if (ret)
571 goto out_platform_alloced;
572
573 return 0;
574
575 out_platform_alloced:
576 platform_device_put(sony_pf_device);
577 sony_pf_device = NULL;
578 out_platform_registered:
579 platform_driver_unregister(&sony_pf_driver);
580 out:
581 atomic_dec(&sony_pf_users);
582 return ret;
583}
584
585static void sony_pf_remove(void)
586{
587 /* deregister only after the last user has gone */
588 if (!atomic_dec_and_test(&sony_pf_users))
589 return;
590
Axel Lin08db2b32010-07-01 10:18:01 +0800591 platform_device_unregister(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +0200592 platform_driver_unregister(&sony_pf_driver);
593}
594
595/*********** SNC (SNY5001) Device ***********/
596
malattia@linux.it33a04452007-04-09 19:26:03 +0200597/* the device uses 1-based values, while the backlight subsystem uses
598 0-based values */
599#define SONY_MAX_BRIGHTNESS 8
600
601#define SNC_VALIDATE_IN 0
602#define SNC_VALIDATE_OUT 1
603
malattia@linux.it59b19102007-04-09 10:19:04 +0200604static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500605 char *);
malattia@linux.it59b19102007-04-09 10:19:04 +0200606static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500607 const char *, size_t);
Mattia Dongili156c2212007-02-12 22:01:07 +0100608static int boolean_validate(const int, const int);
609static int brightness_default_validate(const int, const int);
610
malattia@linux.it59b19102007-04-09 10:19:04 +0200611struct sony_nc_value {
Len Browna02d1c12007-02-07 15:34:02 -0500612 char *name; /* name of the entry */
613 char **acpiget; /* names of the ACPI get function */
614 char **acpiset; /* names of the ACPI set function */
Mattia Dongili156c2212007-02-12 22:01:07 +0100615 int (*validate)(const int, const int); /* input/output validation */
Len Browna02d1c12007-02-07 15:34:02 -0500616 int value; /* current setting */
617 int valid; /* Has ever been set */
618 int debug; /* active only in debug mode ? */
Lucas De Marchic8440332011-03-17 17:18:22 -0300619 struct device_attribute devattr; /* sysfs attribute */
Stelian Pop7f09c432007-01-13 23:04:31 +0100620};
621
malattia@linux.it59b19102007-04-09 10:19:04 +0200622#define SNC_HANDLE_NAMES(_name, _values...) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100623 static char *snc_##_name[] = { _values, NULL }
624
malattia@linux.it59b19102007-04-09 10:19:04 +0200625#define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100626 { \
627 .name = __stringify(_name), \
628 .acpiget = _getters, \
629 .acpiset = _setters, \
Mattia Dongili156c2212007-02-12 22:01:07 +0100630 .validate = _validate, \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100631 .debug = _debug, \
malattia@linux.it59b19102007-04-09 10:19:04 +0200632 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100633 }
634
malattia@linux.it59b19102007-04-09 10:19:04 +0200635#define SNC_HANDLE_NULL { .name = NULL }
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100636
malattia@linux.it59b19102007-04-09 10:19:04 +0200637SNC_HANDLE_NAMES(fnkey_get, "GHKE");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100638
malattia@linux.it59b19102007-04-09 10:19:04 +0200639SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
640SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100641
malattia@linux.it59b19102007-04-09 10:19:04 +0200642SNC_HANDLE_NAMES(cdpower_get, "GCDP");
643SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100644
malattia@linux.it59b19102007-04-09 10:19:04 +0200645SNC_HANDLE_NAMES(audiopower_get, "GAZP");
646SNC_HANDLE_NAMES(audiopower_set, "AZPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100647
malattia@linux.it59b19102007-04-09 10:19:04 +0200648SNC_HANDLE_NAMES(lanpower_get, "GLNP");
649SNC_HANDLE_NAMES(lanpower_set, "LNPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100650
Mattia Dongili044847e2007-07-16 02:34:33 +0900651SNC_HANDLE_NAMES(lidstate_get, "GLID");
652
653SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
654SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
655
656SNC_HANDLE_NAMES(gainbass_get, "GMGB");
657SNC_HANDLE_NAMES(gainbass_set, "CMGB");
658
malattia@linux.it59b19102007-04-09 10:19:04 +0200659SNC_HANDLE_NAMES(PID_get, "GPID");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100660
malattia@linux.it59b19102007-04-09 10:19:04 +0200661SNC_HANDLE_NAMES(CTR_get, "GCTR");
662SNC_HANDLE_NAMES(CTR_set, "SCTR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100663
malattia@linux.it59b19102007-04-09 10:19:04 +0200664SNC_HANDLE_NAMES(PCR_get, "GPCR");
665SNC_HANDLE_NAMES(PCR_set, "SPCR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100666
malattia@linux.it59b19102007-04-09 10:19:04 +0200667SNC_HANDLE_NAMES(CMI_get, "GCMI");
668SNC_HANDLE_NAMES(CMI_set, "SCMI");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100669
malattia@linux.it59b19102007-04-09 10:19:04 +0200670static struct sony_nc_value sony_nc_values[] = {
671 SNC_HANDLE(brightness_default, snc_brightness_def_get,
Mattia Dongili156c2212007-02-12 22:01:07 +0100672 snc_brightness_def_set, brightness_default_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200673 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
674 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
675 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100676 boolean_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200677 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100678 boolean_validate, 1),
Mattia Dongili044847e2007-07-16 02:34:33 +0900679 SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
680 boolean_validate, 0),
681 SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
682 boolean_validate, 0),
683 SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
684 boolean_validate, 0),
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100685 /* unknown methods */
malattia@linux.it59b19102007-04-09 10:19:04 +0200686 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
687 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
688 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
689 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
690 SNC_HANDLE_NULL
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100691};
692
malattia@linux.it59b19102007-04-09 10:19:04 +0200693static acpi_handle sony_nc_acpi_handle;
694static struct acpi_device *sony_nc_acpi_device = NULL;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100695
Mattia Dongilid78865c2007-02-07 20:01:56 +0100696/*
697 * acpi_evaluate_object wrappers
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900698 * all useful calls into SNC methods take one or zero parameters and return
699 * integers or arrays.
Mattia Dongilid78865c2007-02-07 20:01:56 +0100700 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900701static union acpi_object *__call_snc_method(acpi_handle handle, char *method,
702 u64 *value)
Stelian Pop7f09c432007-01-13 23:04:31 +0100703{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900704 union acpi_object *result = NULL;
705 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
Stelian Pop7f09c432007-01-13 23:04:31 +0100706 acpi_status status;
707
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900708 if (value) {
709 struct acpi_object_list params;
710 union acpi_object in;
711 in.type = ACPI_TYPE_INTEGER;
712 in.integer.value = *value;
713 params.count = 1;
714 params.pointer = &in;
715 status = acpi_evaluate_object(handle, method, &params, &output);
716 } else
717 status = acpi_evaluate_object(handle, method, NULL, &output);
Stelian Pop7f09c432007-01-13 23:04:31 +0100718
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900719 if (ACPI_FAILURE(status)) {
720 pr_err("Failed to evaluate [%s]\n", method);
721 return NULL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100722 }
723
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900724 result = (union acpi_object *) output.pointer;
725 if (!result)
726 dprintk("No return object [%s]\n", method);
Stelian Pop7f09c432007-01-13 23:04:31 +0100727
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900728 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +0100729}
730
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900731static int sony_nc_int_call(acpi_handle handle, char *name, int *value,
732 int *result)
Stelian Pop7f09c432007-01-13 23:04:31 +0100733{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900734 union acpi_object *object = NULL;
735 if (value) {
736 u64 v = *value;
737 object = __call_snc_method(handle, name, &v);
738 } else
739 object = __call_snc_method(handle, name, NULL);
Stelian Pop7f09c432007-01-13 23:04:31 +0100740
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900741 if (!object)
742 return -EINVAL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100743
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900744 if (object->type != ACPI_TYPE_INTEGER) {
745 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
746 ACPI_TYPE_INTEGER, object->type);
747 kfree(object);
748 return -EINVAL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100749 }
750
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900751 if (result)
752 *result = object->integer.value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100753
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900754 kfree(object);
755 return 0;
756}
757
758#define MIN(a, b) (a > b ? b : a)
759static int sony_nc_buffer_call(acpi_handle handle, char *name, u64 *value,
760 void *buffer, size_t buflen)
761{
762 size_t len = len;
763 union acpi_object *object = __call_snc_method(handle, name, value);
764
765 if (!object)
766 return -EINVAL;
767
768 if (object->type == ACPI_TYPE_BUFFER)
769 len = MIN(buflen, object->buffer.length);
770
771 else if (object->type == ACPI_TYPE_INTEGER)
772 len = MIN(buflen, sizeof(object->integer.value));
773
774 else {
775 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
776 ACPI_TYPE_BUFFER, object->type);
777 kfree(object);
778 return -EINVAL;
779 }
780
781 memcpy(buffer, object->buffer.pointer, len);
782 kfree(object);
783 return 0;
Stelian Pop7f09c432007-01-13 23:04:31 +0100784}
785
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900786struct sony_nc_handles {
787 u16 cap[0x10];
788 struct device_attribute devattr;
789};
790
Dan Carpenterf11113b2011-02-26 15:54:27 +0300791static struct sony_nc_handles *handles;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900792
793static ssize_t sony_nc_handles_show(struct device *dev,
794 struct device_attribute *attr, char *buffer)
795{
796 ssize_t len = 0;
797 int i;
798
799 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
800 len += snprintf(buffer + len, PAGE_SIZE - len, "0x%.4x ",
801 handles->cap[i]);
802 }
803 len += snprintf(buffer + len, PAGE_SIZE - len, "\n");
804
805 return len;
806}
807
808static int sony_nc_handles_setup(struct platform_device *pd)
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900809{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900810 int i, r, result, arg;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900811
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900812 handles = kzalloc(sizeof(*handles), GFP_KERNEL);
Dan Carpenter31f00752011-02-26 15:55:24 +0300813 if (!handles)
814 return -ENOMEM;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900815
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900816 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900817 arg = i + 0x20;
818 r = sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg,
819 &result);
820 if (!r) {
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900821 dprintk("caching handle 0x%.4x (offset: 0x%.2x)\n",
822 result, i);
823 handles->cap[i] = result;
Mattia Dongili56e6e712011-02-19 11:52:25 +0900824 }
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900825 }
826
Mattia Dongili855b8bc2011-04-05 23:38:35 +0900827 if (debug) {
828 sysfs_attr_init(&handles->devattr.attr);
829 handles->devattr.attr.name = "handles";
830 handles->devattr.attr.mode = S_IRUGO;
831 handles->devattr.show = sony_nc_handles_show;
832
833 /* allow reading capabilities via sysfs */
834 if (device_create_file(&pd->dev, &handles->devattr)) {
835 kfree(handles);
836 handles = NULL;
837 return -1;
838 }
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900839 }
840
841 return 0;
842}
843
844static int sony_nc_handles_cleanup(struct platform_device *pd)
845{
846 if (handles) {
Mattia Dongili855b8bc2011-04-05 23:38:35 +0900847 if (debug)
848 device_remove_file(&pd->dev, &handles->devattr);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900849 kfree(handles);
850 handles = NULL;
851 }
852 return 0;
853}
854
855static int sony_find_snc_handle(int handle)
856{
857 int i;
Mattia Dongilifef34862011-04-02 19:00:44 +0900858
859 /* not initialized yet, return early */
860 if (!handles)
861 return -1;
862
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900863 for (i = 0; i < 0x10; i++) {
864 if (handles->cap[i] == handle) {
865 dprintk("found handle 0x%.4x (offset: 0x%.2x)\n",
866 handle, i);
867 return i;
868 }
869 }
Mattia Dongili56e6e712011-02-19 11:52:25 +0900870 dprintk("handle 0x%.4x not found\n", handle);
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900871 return -1;
872}
873
874static int sony_call_snc_handle(int handle, int argument, int *result)
875{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900876 int arg, ret = 0;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900877 int offset = sony_find_snc_handle(handle);
878
879 if (offset < 0)
880 return -1;
881
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900882 arg = offset | argument;
883 ret = sony_nc_int_call(sony_nc_acpi_handle, "SN07", &arg, result);
884 dprintk("called SN07 with 0x%.4x (result: 0x%.4x)\n", arg, *result);
Mattia Dongili56e6e712011-02-19 11:52:25 +0900885 return ret;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900886}
887
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100888/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200889 * sony_nc_values input/output validate functions
Mattia Dongili156c2212007-02-12 22:01:07 +0100890 */
891
892/* brightness_default_validate:
893 *
894 * manipulate input output values to keep consistency with the
895 * backlight framework for which brightness values are 0-based.
896 */
897static int brightness_default_validate(const int direction, const int value)
898{
899 switch (direction) {
900 case SNC_VALIDATE_OUT:
901 return value - 1;
902 case SNC_VALIDATE_IN:
903 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
904 return value + 1;
905 }
906 return -EINVAL;
907}
908
909/* boolean_validate:
910 *
911 * on input validate boolean values 0/1, on output just pass the
912 * received value.
913 */
914static int boolean_validate(const int direction, const int value)
915{
916 if (direction == SNC_VALIDATE_IN) {
917 if (value != 0 && value != 1)
918 return -EINVAL;
919 }
920 return value;
921}
922
923/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200924 * Sysfs show/store common to all sony_nc_values
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100925 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200926static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
Len Browna02d1c12007-02-07 15:34:02 -0500927 char *buffer)
Stelian Pop7f09c432007-01-13 23:04:31 +0100928{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900929 int value, ret = 0;
malattia@linux.it59b19102007-04-09 10:19:04 +0200930 struct sony_nc_value *item =
931 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100932
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100933 if (!*item->acpiget)
Stelian Pop7f09c432007-01-13 23:04:31 +0100934 return -EIO;
935
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900936 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiget, NULL,
937 &value);
938 if (ret < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100939 return -EIO;
940
Mattia Dongili156c2212007-02-12 22:01:07 +0100941 if (item->validate)
942 value = item->validate(SNC_VALIDATE_OUT, value);
943
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100944 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
Stelian Pop7f09c432007-01-13 23:04:31 +0100945}
946
malattia@linux.it59b19102007-04-09 10:19:04 +0200947static ssize_t sony_nc_sysfs_store(struct device *dev,
Len Browna02d1c12007-02-07 15:34:02 -0500948 struct device_attribute *attr,
949 const char *buffer, size_t count)
Stelian Pop7f09c432007-01-13 23:04:31 +0100950{
Mattia Dongili9e123372012-05-19 22:35:47 +0900951 unsigned long value = 0;
952 int ret = 0;
malattia@linux.it59b19102007-04-09 10:19:04 +0200953 struct sony_nc_value *item =
954 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100955
956 if (!item->acpiset)
957 return -EIO;
958
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100959 if (count > 31)
960 return -EINVAL;
961
Mattia Dongili9e123372012-05-19 22:35:47 +0900962 if (kstrtoul(buffer, 10, &value))
963 return -EINVAL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100964
Mattia Dongili156c2212007-02-12 22:01:07 +0100965 if (item->validate)
966 value = item->validate(SNC_VALIDATE_IN, value);
967
968 if (value < 0)
969 return value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100970
Mattia Dongili9e123372012-05-19 22:35:47 +0900971 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
972 (int *)&value, NULL);
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900973 if (ret < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100974 return -EIO;
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900975
Andrew Morton3f4f4612007-01-13 23:04:32 +0100976 item->value = value;
977 item->valid = 1;
Stelian Pop7f09c432007-01-13 23:04:31 +0100978 return count;
979}
980
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100981
Mattia Dongilid78865c2007-02-07 20:01:56 +0100982/*
983 * Backlight device
984 */
Mattia Dongili62d2f232011-05-09 10:20:29 -0400985struct sony_backlight_props {
986 struct backlight_device *dev;
987 int handle;
988 u8 offset;
989 u8 maxlvl;
990};
991struct sony_backlight_props sony_bl_props;
992
Mattia Dongilid78865c2007-02-07 20:01:56 +0100993static int sony_backlight_update_status(struct backlight_device *bd)
Andrew Morton3f4f4612007-01-13 23:04:32 +0100994{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900995 int arg = bd->props.brightness + 1;
996 return sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &arg, NULL);
Andrew Morton3f4f4612007-01-13 23:04:32 +0100997}
998
Mattia Dongilid78865c2007-02-07 20:01:56 +0100999static int sony_backlight_get_brightness(struct backlight_device *bd)
1000{
1001 int value;
1002
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001003 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL, &value))
Mattia Dongilid78865c2007-02-07 20:01:56 +01001004 return 0;
1005 /* brightness levels are 1-based, while backlight ones are 0-based */
1006 return value - 1;
1007}
1008
Mattia Dongili7751ab82011-02-19 11:52:32 +09001009static int sony_nc_get_brightness_ng(struct backlight_device *bd)
1010{
1011 int result;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001012 struct sony_backlight_props *sdev =
1013 (struct sony_backlight_props *)bl_get_data(bd);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001014
Mattia Dongili62d2f232011-05-09 10:20:29 -04001015 sony_call_snc_handle(sdev->handle, 0x0200, &result);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001016
Mattia Dongili62d2f232011-05-09 10:20:29 -04001017 return (result & 0xff) - sdev->offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001018}
1019
1020static int sony_nc_update_status_ng(struct backlight_device *bd)
1021{
1022 int value, result;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001023 struct sony_backlight_props *sdev =
1024 (struct sony_backlight_props *)bl_get_data(bd);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001025
Mattia Dongili62d2f232011-05-09 10:20:29 -04001026 value = bd->props.brightness + sdev->offset;
1027 if (sony_call_snc_handle(sdev->handle, 0x0100 | (value << 16), &result))
Mattia Dongili6192fa72011-04-05 23:38:36 +09001028 return -EIO;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001029
Mattia Dongili6192fa72011-04-05 23:38:36 +09001030 return value;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001031}
1032
Lionel Debrouxacc24722010-11-16 14:14:02 +01001033static const struct backlight_ops sony_backlight_ops = {
Mattia Dongili7751ab82011-02-19 11:52:32 +09001034 .options = BL_CORE_SUSPENDRESUME,
Len Browna02d1c12007-02-07 15:34:02 -05001035 .update_status = sony_backlight_update_status,
1036 .get_brightness = sony_backlight_get_brightness,
Mattia Dongilid78865c2007-02-07 20:01:56 +01001037};
Mattia Dongili7751ab82011-02-19 11:52:32 +09001038static const struct backlight_ops sony_backlight_ng_ops = {
1039 .options = BL_CORE_SUSPENDRESUME,
1040 .update_status = sony_nc_update_status_ng,
1041 .get_brightness = sony_nc_get_brightness_ng,
1042};
Mattia Dongilid78865c2007-02-07 20:01:56 +01001043
1044/*
Mattia Dongili6315fd12007-07-16 02:34:35 +09001045 * New SNC-only Vaios event mapping to driver known keys
1046 */
1047struct sony_nc_event {
1048 u8 data;
1049 u8 event;
1050};
1051
Matthew Garrett45c79422009-03-26 21:58:16 +09001052static struct sony_nc_event sony_100_events[] = {
Matthew Garrett9b578962009-03-26 21:58:14 +09001053 { 0x90, SONYPI_EVENT_PKEY_P1 },
1054 { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthias Welwarsky6479efb2009-04-01 22:10:45 +09001055 { 0x91, SONYPI_EVENT_PKEY_P2 },
Matthew Garrett9b578962009-03-26 21:58:14 +09001056 { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001057 { 0x81, SONYPI_EVENT_FNKEY_F1 },
1058 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
Anton Veretenenkof5acf5e2009-03-26 22:44:26 +09001059 { 0x82, SONYPI_EVENT_FNKEY_F2 },
1060 { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
1061 { 0x83, SONYPI_EVENT_FNKEY_F3 },
1062 { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
1063 { 0x84, SONYPI_EVENT_FNKEY_F4 },
1064 { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001065 { 0x85, SONYPI_EVENT_FNKEY_F5 },
1066 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
1067 { 0x86, SONYPI_EVENT_FNKEY_F6 },
1068 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
1069 { 0x87, SONYPI_EVENT_FNKEY_F7 },
1070 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +09001071 { 0x89, SONYPI_EVENT_FNKEY_F9 },
1072 { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001073 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
1074 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
1075 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
1076 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +09001077 { 0x9d, SONYPI_EVENT_ZOOM_PRESSED },
1078 { 0x1d, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +09001079 { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
1080 { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili4f924ba2009-12-17 00:08:33 +09001081 { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
1082 { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +09001083 { 0xa4, SONYPI_EVENT_CD_EJECT_PRESSED },
1084 { 0x24, SONYPI_EVENT_ANYBUTTON_RELEASED },
1085 { 0xa5, SONYPI_EVENT_VENDOR_PRESSED },
1086 { 0x25, SONYPI_EVENT_ANYBUTTON_RELEASED },
1087 { 0xa6, SONYPI_EVENT_HELP_PRESSED },
1088 { 0x26, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001089 { 0, 0 },
1090};
1091
Matthew Garrett45c79422009-03-26 21:58:16 +09001092static struct sony_nc_event sony_127_events[] = {
1093 { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
1094 { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
1095 { 0x82, SONYPI_EVENT_PKEY_P1 },
1096 { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
1097 { 0x83, SONYPI_EVENT_PKEY_P2 },
1098 { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
1099 { 0x84, SONYPI_EVENT_PKEY_P3 },
1100 { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
1101 { 0x85, SONYPI_EVENT_PKEY_P4 },
1102 { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
1103 { 0x86, SONYPI_EVENT_PKEY_P5 },
1104 { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett45c79422009-03-26 21:58:16 +09001105 { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
1106 { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
1107 { 0, 0 },
1108};
1109
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001110static int sony_nc_hotkeys_decode(u32 event, unsigned int handle)
1111{
1112 int ret = -EINVAL;
1113 unsigned int result = 0;
1114 struct sony_nc_event *key_event;
1115
1116 if (sony_call_snc_handle(handle, 0x200, &result)) {
1117 dprintk("Unable to decode event 0x%.2x 0x%.2x\n", handle,
1118 event);
1119 return -EINVAL;
1120 }
1121
1122 result &= 0xFF;
1123
1124 if (handle == 0x0100)
1125 key_event = sony_100_events;
1126 else
1127 key_event = sony_127_events;
1128
1129 for (; key_event->data; key_event++) {
1130 if (key_event->data == result) {
1131 ret = key_event->event;
1132 break;
1133 }
1134 }
1135
1136 if (!key_event->data)
1137 pr_info("Unknown hotkey 0x%.2x/0x%.2x (handle 0x%.2x)\n",
1138 event, result, handle);
1139
1140 return ret;
1141}
1142
Mattia Dongili6315fd12007-07-16 02:34:35 +09001143/*
Mattia Dongilid78865c2007-02-07 20:01:56 +01001144 * ACPI callbacks
1145 */
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001146static void sony_nc_notify(struct acpi_device *device, u32 event)
Stelian Pop7f09c432007-01-13 23:04:31 +01001147{
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001148 u32 real_ev = event;
1149 u8 ev_type = 0;
1150 dprintk("sony_nc_notify, event: 0x%.2x\n", event);
Mattia Dongili6315fd12007-07-16 02:34:35 +09001151
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001152 if (event >= 0x90) {
1153 unsigned int result = 0;
1154 unsigned int arg = 0;
1155 unsigned int handle = 0;
1156 unsigned int offset = event - 0x90;
Mattia Dongili6315fd12007-07-16 02:34:35 +09001157
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001158 if (offset >= ARRAY_SIZE(handles->cap)) {
1159 pr_err("Event 0x%x outside of capabilities list\n",
1160 event);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001161 return;
Matthew Garrett9b578962009-03-26 21:58:14 +09001162 }
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001163 handle = handles->cap[offset];
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001164
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001165 /* list of handles known for generating events */
1166 switch (handle) {
1167 /* hotkey event */
1168 case 0x0100:
1169 case 0x0127:
1170 ev_type = 1;
1171 real_ev = sony_nc_hotkeys_decode(event, handle);
1172
1173 if (real_ev > 0)
1174 sony_laptop_report_input_event(real_ev);
1175 else
1176 /* restore the original event for reporting */
1177 real_ev = event;
1178
1179 break;
1180
1181 /* wlan switch */
1182 case 0x0124:
1183 case 0x0135:
1184 /* events on this handle are reported when the
1185 * switch changes position or for battery
1186 * events. We'll notify both of them but only
1187 * update the rfkill device status when the
1188 * switch is moved.
1189 */
1190 ev_type = 2;
1191 sony_call_snc_handle(handle, 0x0100, &result);
1192 real_ev = result & 0x03;
1193
1194 /* hw switch event */
1195 if (real_ev == 1)
1196 sony_nc_rfkill_update();
1197
1198 break;
1199
1200 default:
1201 dprintk("Unknown event 0x%x for handle 0x%x\n",
1202 event, handle);
1203 break;
1204 }
1205
1206 /* clear the event (and the event reason when present) */
1207 arg = 1 << offset;
1208 sony_nc_int_call(sony_nc_acpi_handle, "SN05", &arg, &result);
1209
1210 } else {
1211 /* old style event */
1212 ev_type = 1;
1213 sony_laptop_report_input_event(real_ev);
1214 }
1215
1216 acpi_bus_generate_proc_event(sony_nc_acpi_device, ev_type, real_ev);
1217
1218 acpi_bus_generate_netlink_event(sony_nc_acpi_device->pnp.device_class,
1219 dev_name(&sony_nc_acpi_device->dev), ev_type, real_ev);
Stelian Pop7f09c432007-01-13 23:04:31 +01001220}
1221
1222static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
1223 void *context, void **return_value)
1224{
Lin Ming30823732008-12-16 16:59:35 +08001225 struct acpi_device_info *info;
Stelian Pop7f09c432007-01-13 23:04:31 +01001226
Bob Moore15b8dd52009-06-29 13:39:29 +08001227 if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
Joe Perches50f581a2011-03-29 15:21:48 -07001228 pr_warn("method: name: %4.4s, args %X\n",
Lin Ming30823732008-12-16 16:59:35 +08001229 (char *)&info->name, info->param_count);
1230
Bob Moore15b8dd52009-06-29 13:39:29 +08001231 kfree(info);
Lin Ming30823732008-12-16 16:59:35 +08001232 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001233
1234 return AE_OK;
1235}
1236
Mattia Dongilid78865c2007-02-07 20:01:56 +01001237/*
1238 * ACPI device
1239 */
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001240static void sony_nc_function_setup(struct acpi_device *device,
1241 struct platform_device *pf_device)
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001242{
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001243 unsigned int i, result, bitmask, arg;
1244
1245 if (!handles)
1246 return;
1247
1248 /* setup found handles here */
1249 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1250 unsigned int handle = handles->cap[i];
1251
1252 if (!handle)
1253 continue;
1254
1255 dprintk("setting up handle 0x%.4x\n", handle);
1256
1257 switch (handle) {
1258 case 0x0100:
1259 case 0x0101:
1260 case 0x0127:
1261 /* setup hotkeys */
1262 sony_call_snc_handle(handle, 0, &result);
1263 break;
1264 case 0x0102:
1265 /* setup hotkeys */
1266 sony_call_snc_handle(handle, 0x100, &result);
1267 break;
1268 case 0x0124:
1269 case 0x0135:
1270 sony_nc_rfkill_setup(device);
1271 break;
1272 case 0x0137:
1273 sony_nc_kbd_backlight_setup(pf_device);
1274 break;
1275 default:
1276 continue;
1277 }
1278 }
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001279
1280 /* Enable all events */
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001281 arg = 0x10;
1282 if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1283 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1284 &result);
1285}
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001286
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001287static void sony_nc_function_cleanup(struct platform_device *pd)
1288{
1289 unsigned int i, result, bitmask, handle;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001290
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001291 /* get enabled events and disable them */
1292 sony_nc_int_call(sony_nc_acpi_handle, "SN01", NULL, &bitmask);
1293 sony_nc_int_call(sony_nc_acpi_handle, "SN03", &bitmask, &result);
1294
1295 /* cleanup handles here */
1296 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1297
1298 handle = handles->cap[i];
1299
1300 if (!handle)
1301 continue;
1302
1303 switch (handle) {
1304 case 0x0124:
1305 case 0x0135:
1306 sony_nc_rfkill_cleanup();
1307 break;
1308 case 0x0137:
1309 sony_nc_kbd_backlight_cleanup(pd);
1310 break;
1311 default:
1312 continue;
1313 }
1314 }
1315
1316 /* finally cleanup the handles list */
1317 sony_nc_handles_cleanup(pd);
1318}
1319
1320static void sony_nc_function_resume(void)
1321{
1322 unsigned int i, result, bitmask, arg;
1323
1324 dprintk("Resuming SNC device\n");
1325
1326 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1327 unsigned int handle = handles->cap[i];
1328
1329 if (!handle)
1330 continue;
1331
1332 switch (handle) {
1333 case 0x0100:
1334 case 0x0101:
1335 case 0x0127:
1336 /* re-enable hotkeys */
1337 sony_call_snc_handle(handle, 0, &result);
1338 break;
1339 case 0x0102:
1340 /* re-enable hotkeys */
1341 sony_call_snc_handle(handle, 0x100, &result);
1342 break;
1343 case 0x0124:
1344 case 0x0135:
1345 sony_nc_rfkill_update();
1346 break;
1347 case 0x0137:
1348 sony_nc_kbd_backlight_resume();
1349 break;
1350 default:
1351 continue;
1352 }
1353 }
1354
1355 /* Enable all events */
1356 arg = 0x10;
1357 if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1358 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1359 &result);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001360}
1361
malattia@linux.it59b19102007-04-09 10:19:04 +02001362static int sony_nc_resume(struct acpi_device *device)
Mattia Dongilid78865c2007-02-07 20:01:56 +01001363{
malattia@linux.it59b19102007-04-09 10:19:04 +02001364 struct sony_nc_value *item;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001365 acpi_handle handle;
Mattia Dongilid78865c2007-02-07 20:01:56 +01001366
malattia@linux.it59b19102007-04-09 10:19:04 +02001367 for (item = sony_nc_values; item->name; item++) {
Mattia Dongilid78865c2007-02-07 20:01:56 +01001368 int ret;
1369
1370 if (!item->valid)
1371 continue;
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001372 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
1373 &item->value, NULL);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001374 if (ret < 0) {
Joe Perches50f581a2011-03-29 15:21:48 -07001375 pr_err("%s: %d\n", __func__, ret);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001376 break;
1377 }
1378 }
Mattia Dongili6315fd12007-07-16 02:34:35 +09001379
Matthew Garrett82734bf2009-03-26 21:58:13 +09001380 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1381 &handle))) {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001382 int arg = 1;
1383 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
Matthew Garrett82734bf2009-03-26 21:58:13 +09001384 dprintk("ECON Method failed\n");
1385 }
1386
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001387 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001388 &handle)))
1389 sony_nc_function_resume();
Marco Chiapperodf410d52011-04-05 23:38:34 +09001390
Mattia Dongilid78865c2007-02-07 20:01:56 +01001391 return 0;
1392}
1393
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001394static void sony_nc_rfkill_cleanup(void)
1395{
1396 int i;
1397
Johannes Berg19d337d2009-06-02 13:01:37 +02001398 for (i = 0; i < N_SONY_RFKILL; i++) {
1399 if (sony_rfkill_devices[i]) {
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001400 rfkill_unregister(sony_rfkill_devices[i]);
Johannes Berg19d337d2009-06-02 13:01:37 +02001401 rfkill_destroy(sony_rfkill_devices[i]);
1402 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001403 }
1404}
1405
Johannes Berg19d337d2009-06-02 13:01:37 +02001406static int sony_nc_rfkill_set(void *data, bool blocked)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001407{
1408 int result;
1409 int argument = sony_rfkill_address[(long) data] + 0x100;
1410
Johannes Berg19d337d2009-06-02 13:01:37 +02001411 if (!blocked)
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001412 argument |= 0x030000;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001413
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001414 return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001415}
1416
Johannes Berg19d337d2009-06-02 13:01:37 +02001417static const struct rfkill_ops sony_rfkill_ops = {
1418 .set_block = sony_nc_rfkill_set,
1419};
1420
1421static int sony_nc_setup_rfkill(struct acpi_device *device,
1422 enum sony_nc_rfkill nc_type)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001423{
1424 int err = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001425 struct rfkill *rfk;
1426 enum rfkill_type type;
1427 const char *name;
Alan Jenkins50fab072009-09-24 20:15:24 +01001428 int result;
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001429 bool hwblock, swblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001430
Johannes Berg19d337d2009-06-02 13:01:37 +02001431 switch (nc_type) {
1432 case SONY_WIFI:
1433 type = RFKILL_TYPE_WLAN;
1434 name = "sony-wifi";
1435 break;
1436 case SONY_BLUETOOTH:
1437 type = RFKILL_TYPE_BLUETOOTH;
1438 name = "sony-bluetooth";
1439 break;
1440 case SONY_WWAN:
1441 type = RFKILL_TYPE_WWAN;
1442 name = "sony-wwan";
1443 break;
1444 case SONY_WIMAX:
1445 type = RFKILL_TYPE_WIMAX;
1446 name = "sony-wimax";
1447 break;
1448 default:
1449 return -EINVAL;
Mattia Dongili53005a02009-04-12 11:26:31 +00001450 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001451
Johannes Berg19d337d2009-06-02 13:01:37 +02001452 rfk = rfkill_alloc(name, &device->dev, type,
1453 &sony_rfkill_ops, (void *)nc_type);
1454 if (!rfk)
1455 return -ENOMEM;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001456
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001457 if (sony_call_snc_handle(sony_rfkill_handle, 0x200, &result) < 0) {
1458 rfkill_destroy(rfk);
1459 return -1;
1460 }
Alan Jenkins50fab072009-09-24 20:15:24 +01001461 hwblock = !(result & 0x1);
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001462
1463 if (sony_call_snc_handle(sony_rfkill_handle,
1464 sony_rfkill_address[nc_type],
1465 &result) < 0) {
1466 rfkill_destroy(rfk);
1467 return -1;
1468 }
1469 swblock = !(result & 0x2);
1470
1471 rfkill_init_sw_state(rfk, swblock);
Alan Jenkins50fab072009-09-24 20:15:24 +01001472 rfkill_set_hw_state(rfk, hwblock);
1473
Johannes Berg19d337d2009-06-02 13:01:37 +02001474 err = rfkill_register(rfk);
1475 if (err) {
1476 rfkill_destroy(rfk);
1477 return err;
Mattia Dongili53005a02009-04-12 11:26:31 +00001478 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001479 sony_rfkill_devices[nc_type] = rfk;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001480 return err;
1481}
1482
Randy Dunlapa46a7802011-01-08 19:56:44 -08001483static void sony_nc_rfkill_update(void)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001484{
Johannes Berg19d337d2009-06-02 13:01:37 +02001485 enum sony_nc_rfkill i;
1486 int result;
1487 bool hwblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001488
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001489 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001490 hwblock = !(result & 0x1);
1491
1492 for (i = 0; i < N_SONY_RFKILL; i++) {
1493 int argument = sony_rfkill_address[i];
1494
1495 if (!sony_rfkill_devices[i])
1496 continue;
1497
1498 if (hwblock) {
Johannes Berge1f8a192009-06-11 12:08:15 +02001499 if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
1500 /* we already know we're blocked */
1501 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001502 continue;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001503 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001504
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001505 sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001506 rfkill_set_states(sony_rfkill_devices[i],
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001507 !(result & 0x2), false);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001508 }
1509}
1510
Mattia Dongili528809c2009-12-17 00:08:36 +09001511static void sony_nc_rfkill_setup(struct acpi_device *device)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001512{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001513 u64 offset;
1514 int i;
1515 unsigned char buffer[32] = { 0 };
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001516
Mattia Dongili528809c2009-12-17 00:08:36 +09001517 offset = sony_find_snc_handle(0x124);
1518 if (offset == -1) {
1519 offset = sony_find_snc_handle(0x135);
1520 if (offset == -1)
1521 return;
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001522 else
1523 sony_rfkill_handle = 0x135;
1524 } else
1525 sony_rfkill_handle = 0x124;
Mattia Dongili528809c2009-12-17 00:08:36 +09001526 dprintk("Found rkfill handle: 0x%.4x\n", sony_rfkill_handle);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001527
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001528 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
1529 32);
1530 if (i < 0)
Mattia Dongili528809c2009-12-17 00:08:36 +09001531 return;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001532
Mattia Dongili528809c2009-12-17 00:08:36 +09001533 /* the buffer is filled with magic numbers describing the devices
1534 * available, 0xff terminates the enumeration
1535 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001536 for (i = 0; i < ARRAY_SIZE(buffer); i++) {
Dmitry Torokhovc14973f2010-01-10 00:15:44 -08001537
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001538 if (buffer[i] == 0xff)
Dmitry Torokhovc14973f2010-01-10 00:15:44 -08001539 break;
1540
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001541 dprintk("Radio devices, looking at 0x%.2x\n", buffer[i]);
Mattia Dongili528809c2009-12-17 00:08:36 +09001542
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001543 if (buffer[i] == 0 && !sony_rfkill_devices[SONY_WIFI])
Mattia Dongili528809c2009-12-17 00:08:36 +09001544 sony_nc_setup_rfkill(device, SONY_WIFI);
1545
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001546 if (buffer[i] == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
Mattia Dongili528809c2009-12-17 00:08:36 +09001547 sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
1548
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001549 if ((0xf0 & buffer[i]) == 0x20 &&
Mattia Dongili528809c2009-12-17 00:08:36 +09001550 !sony_rfkill_devices[SONY_WWAN])
1551 sony_nc_setup_rfkill(device, SONY_WWAN);
1552
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001553 if (buffer[i] == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
Mattia Dongili528809c2009-12-17 00:08:36 +09001554 sony_nc_setup_rfkill(device, SONY_WIMAX);
1555 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001556}
1557
Mattia Dongilibf155712011-02-19 11:52:31 +09001558/* Keyboard backlight feature */
1559#define KBDBL_HANDLER 0x137
1560#define KBDBL_PRESENT 0xB00
1561#define SET_MODE 0xC00
Marco Chiapperodf410d52011-04-05 23:38:34 +09001562#define SET_STATE 0xD00
Mattia Dongilibf155712011-02-19 11:52:31 +09001563#define SET_TIMEOUT 0xE00
1564
1565struct kbd_backlight {
1566 int mode;
1567 int timeout;
1568 struct device_attribute mode_attr;
1569 struct device_attribute timeout_attr;
1570};
1571
Dan Carpenterf11113b2011-02-26 15:54:27 +03001572static struct kbd_backlight *kbdbl_handle;
Mattia Dongilibf155712011-02-19 11:52:31 +09001573
1574static ssize_t __sony_nc_kbd_backlight_mode_set(u8 value)
1575{
1576 int result;
1577
1578 if (value > 1)
1579 return -EINVAL;
1580
1581 if (sony_call_snc_handle(KBDBL_HANDLER,
1582 (value << 0x10) | SET_MODE, &result))
1583 return -EIO;
1584
Marco Chiapperodf410d52011-04-05 23:38:34 +09001585 /* Try to turn the light on/off immediately */
1586 sony_call_snc_handle(KBDBL_HANDLER, (value << 0x10) | SET_STATE,
1587 &result);
1588
Mattia Dongilibf155712011-02-19 11:52:31 +09001589 kbdbl_handle->mode = value;
1590
1591 return 0;
1592}
1593
1594static ssize_t sony_nc_kbd_backlight_mode_store(struct device *dev,
1595 struct device_attribute *attr,
1596 const char *buffer, size_t count)
1597{
1598 int ret = 0;
1599 unsigned long value;
1600
1601 if (count > 31)
1602 return -EINVAL;
1603
Mattia Dongili9e123372012-05-19 22:35:47 +09001604 if (kstrtoul(buffer, 10, &value))
Mattia Dongilibf155712011-02-19 11:52:31 +09001605 return -EINVAL;
1606
1607 ret = __sony_nc_kbd_backlight_mode_set(value);
1608 if (ret < 0)
1609 return ret;
1610
1611 return count;
1612}
1613
1614static ssize_t sony_nc_kbd_backlight_mode_show(struct device *dev,
1615 struct device_attribute *attr, char *buffer)
1616{
1617 ssize_t count = 0;
1618 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_handle->mode);
1619 return count;
1620}
1621
1622static int __sony_nc_kbd_backlight_timeout_set(u8 value)
1623{
1624 int result;
1625
1626 if (value > 3)
1627 return -EINVAL;
1628
1629 if (sony_call_snc_handle(KBDBL_HANDLER,
1630 (value << 0x10) | SET_TIMEOUT, &result))
1631 return -EIO;
1632
1633 kbdbl_handle->timeout = value;
1634
1635 return 0;
1636}
1637
1638static ssize_t sony_nc_kbd_backlight_timeout_store(struct device *dev,
1639 struct device_attribute *attr,
1640 const char *buffer, size_t count)
1641{
1642 int ret = 0;
1643 unsigned long value;
1644
1645 if (count > 31)
1646 return -EINVAL;
1647
Mattia Dongili9e123372012-05-19 22:35:47 +09001648 if (kstrtoul(buffer, 10, &value))
Mattia Dongilibf155712011-02-19 11:52:31 +09001649 return -EINVAL;
1650
1651 ret = __sony_nc_kbd_backlight_timeout_set(value);
1652 if (ret < 0)
1653 return ret;
1654
1655 return count;
1656}
1657
1658static ssize_t sony_nc_kbd_backlight_timeout_show(struct device *dev,
1659 struct device_attribute *attr, char *buffer)
1660{
1661 ssize_t count = 0;
1662 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_handle->timeout);
1663 return count;
1664}
1665
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001666static void sony_nc_kbd_backlight_setup(struct platform_device *pd)
Mattia Dongilibf155712011-02-19 11:52:31 +09001667{
1668 int result;
1669
Marco Chiapperodf410d52011-04-05 23:38:34 +09001670 if (sony_call_snc_handle(KBDBL_HANDLER, KBDBL_PRESENT, &result))
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001671 return;
Mattia Dongilibf155712011-02-19 11:52:31 +09001672 if (!(result & 0x02))
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001673 return;
Mattia Dongilibf155712011-02-19 11:52:31 +09001674
1675 kbdbl_handle = kzalloc(sizeof(*kbdbl_handle), GFP_KERNEL);
Dan Carpenter31f00752011-02-26 15:55:24 +03001676 if (!kbdbl_handle)
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001677 return;
Mattia Dongilibf155712011-02-19 11:52:31 +09001678
1679 sysfs_attr_init(&kbdbl_handle->mode_attr.attr);
1680 kbdbl_handle->mode_attr.attr.name = "kbd_backlight";
1681 kbdbl_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
1682 kbdbl_handle->mode_attr.show = sony_nc_kbd_backlight_mode_show;
1683 kbdbl_handle->mode_attr.store = sony_nc_kbd_backlight_mode_store;
1684
1685 sysfs_attr_init(&kbdbl_handle->timeout_attr.attr);
1686 kbdbl_handle->timeout_attr.attr.name = "kbd_backlight_timeout";
1687 kbdbl_handle->timeout_attr.attr.mode = S_IRUGO | S_IWUSR;
1688 kbdbl_handle->timeout_attr.show = sony_nc_kbd_backlight_timeout_show;
1689 kbdbl_handle->timeout_attr.store = sony_nc_kbd_backlight_timeout_store;
1690
1691 if (device_create_file(&pd->dev, &kbdbl_handle->mode_attr))
1692 goto outkzalloc;
1693
1694 if (device_create_file(&pd->dev, &kbdbl_handle->timeout_attr))
1695 goto outmode;
1696
1697 __sony_nc_kbd_backlight_mode_set(kbd_backlight);
1698 __sony_nc_kbd_backlight_timeout_set(kbd_backlight_timeout);
1699
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001700 return;
Mattia Dongilibf155712011-02-19 11:52:31 +09001701
1702outmode:
1703 device_remove_file(&pd->dev, &kbdbl_handle->mode_attr);
1704outkzalloc:
1705 kfree(kbdbl_handle);
1706 kbdbl_handle = NULL;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001707 return;
Mattia Dongilibf155712011-02-19 11:52:31 +09001708}
1709
1710static int sony_nc_kbd_backlight_cleanup(struct platform_device *pd)
1711{
1712 if (kbdbl_handle) {
Marco Chiapperodf410d52011-04-05 23:38:34 +09001713 int result;
1714
Mattia Dongilibf155712011-02-19 11:52:31 +09001715 device_remove_file(&pd->dev, &kbdbl_handle->mode_attr);
1716 device_remove_file(&pd->dev, &kbdbl_handle->timeout_attr);
Marco Chiapperodf410d52011-04-05 23:38:34 +09001717
1718 /* restore the default hw behaviour */
1719 sony_call_snc_handle(KBDBL_HANDLER, 0x1000 | SET_MODE, &result);
1720 sony_call_snc_handle(KBDBL_HANDLER, SET_TIMEOUT, &result);
1721
Mattia Dongilibf155712011-02-19 11:52:31 +09001722 kfree(kbdbl_handle);
1723 }
1724 return 0;
1725}
1726
Marco Chiapperodf410d52011-04-05 23:38:34 +09001727static void sony_nc_kbd_backlight_resume(void)
1728{
1729 int ignore = 0;
1730
1731 if (!kbdbl_handle)
1732 return;
1733
1734 if (kbdbl_handle->mode == 0)
1735 sony_call_snc_handle(KBDBL_HANDLER, SET_MODE, &ignore);
1736
1737 if (kbdbl_handle->timeout != 0)
1738 sony_call_snc_handle(KBDBL_HANDLER,
1739 (kbdbl_handle->timeout << 0x10) | SET_TIMEOUT,
1740 &ignore);
1741}
1742
Mattia Dongili62d2f232011-05-09 10:20:29 -04001743static void sony_nc_backlight_ng_read_limits(int handle,
1744 struct sony_backlight_props *props)
1745{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001746 u64 offset;
1747 int i;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001748 u8 min = 0xff, max = 0x00;
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001749 unsigned char buffer[32] = { 0 };
Mattia Dongili62d2f232011-05-09 10:20:29 -04001750
1751 props->handle = handle;
1752 props->offset = 0;
1753 props->maxlvl = 0xff;
1754
1755 offset = sony_find_snc_handle(handle);
1756 if (offset < 0)
1757 return;
1758
1759 /* try to read the boundaries from ACPI tables, if we fail the above
1760 * defaults should be reasonable
1761 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001762 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
1763 32);
1764 if (i < 0)
Mattia Dongili62d2f232011-05-09 10:20:29 -04001765 return;
1766
Mattia Dongili62d2f232011-05-09 10:20:29 -04001767 /* the buffer lists brightness levels available, brightness levels are
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001768 * from position 0 to 8 in the array, other values are used by ALS
1769 * control.
Mattia Dongili62d2f232011-05-09 10:20:29 -04001770 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001771 for (i = 0; i < 9 && i < ARRAY_SIZE(buffer); i++) {
Mattia Dongili62d2f232011-05-09 10:20:29 -04001772
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001773 dprintk("Brightness level: %d\n", buffer[i]);
Mattia Dongili62d2f232011-05-09 10:20:29 -04001774
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001775 if (!buffer[i])
Mattia Dongili62d2f232011-05-09 10:20:29 -04001776 break;
1777
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001778 if (buffer[i] > max)
1779 max = buffer[i];
1780 if (buffer[i] < min)
1781 min = buffer[i];
Mattia Dongili62d2f232011-05-09 10:20:29 -04001782 }
1783 props->offset = min;
1784 props->maxlvl = max;
1785 dprintk("Brightness levels: min=%d max=%d\n", props->offset,
1786 props->maxlvl);
Mattia Dongili62d2f232011-05-09 10:20:29 -04001787}
1788
Mattia Dongili7751ab82011-02-19 11:52:32 +09001789static void sony_nc_backlight_setup(void)
1790{
1791 acpi_handle unused;
1792 int max_brightness = 0;
1793 const struct backlight_ops *ops = NULL;
1794 struct backlight_properties props;
1795
1796 if (sony_find_snc_handle(0x12f) != -1) {
Mattia Dongili7751ab82011-02-19 11:52:32 +09001797 ops = &sony_backlight_ng_ops;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001798 sony_nc_backlight_ng_read_limits(0x12f, &sony_bl_props);
1799 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001800
1801 } else if (sony_find_snc_handle(0x137) != -1) {
Mattia Dongili7751ab82011-02-19 11:52:32 +09001802 ops = &sony_backlight_ng_ops;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001803 sony_nc_backlight_ng_read_limits(0x137, &sony_bl_props);
1804 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001805
1806 } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
1807 &unused))) {
1808 ops = &sony_backlight_ops;
1809 max_brightness = SONY_MAX_BRIGHTNESS - 1;
1810
1811 } else
1812 return;
1813
1814 memset(&props, 0, sizeof(struct backlight_properties));
1815 props.type = BACKLIGHT_PLATFORM;
1816 props.max_brightness = max_brightness;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001817 sony_bl_props.dev = backlight_device_register("sony", NULL,
1818 &sony_bl_props,
1819 ops, &props);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001820
Mattia Dongili62d2f232011-05-09 10:20:29 -04001821 if (IS_ERR(sony_bl_props.dev)) {
Joe Perches50f581a2011-03-29 15:21:48 -07001822 pr_warn("unable to register backlight device\n");
Mattia Dongili62d2f232011-05-09 10:20:29 -04001823 sony_bl_props.dev = NULL;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001824 } else
Mattia Dongili62d2f232011-05-09 10:20:29 -04001825 sony_bl_props.dev->props.brightness =
1826 ops->get_brightness(sony_bl_props.dev);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001827}
1828
1829static void sony_nc_backlight_cleanup(void)
1830{
Mattia Dongili62d2f232011-05-09 10:20:29 -04001831 if (sony_bl_props.dev)
1832 backlight_device_unregister(sony_bl_props.dev);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001833}
1834
malattia@linux.it59b19102007-04-09 10:19:04 +02001835static int sony_nc_add(struct acpi_device *device)
Stelian Pop7f09c432007-01-13 23:04:31 +01001836{
1837 acpi_status status;
Andrew Morton8607c672007-03-06 02:29:42 -08001838 int result = 0;
Alessandro Guido50f62af2007-01-13 23:04:34 +01001839 acpi_handle handle;
malattia@linux.it56b87562007-04-09 10:19:05 +02001840 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01001841
Joe Perches50f581a2011-03-29 15:21:48 -07001842 pr_info("%s v%s\n", SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.itf6119b02007-04-09 19:31:06 +02001843
malattia@linux.it59b19102007-04-09 10:19:04 +02001844 sony_nc_acpi_device = device;
malattia@linux.it33a04452007-04-09 19:26:03 +02001845 strcpy(acpi_device_class(device), "sony/hotkey");
Stelian Popc5611622007-01-13 23:04:37 +01001846
malattia@linux.it59b19102007-04-09 10:19:04 +02001847 sony_nc_acpi_handle = device->handle;
Stelian Pop7f09c432007-01-13 23:04:31 +01001848
Mattia Dongilib25b7322007-07-16 02:34:36 +09001849 /* read device status */
1850 result = acpi_bus_get_status(device);
1851 /* bail IFF the above call was successful and the device is not present */
1852 if (!result && !device->status.present) {
1853 dprintk("Device not present\n");
1854 result = -ENODEV;
1855 goto outwalk;
1856 }
1857
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001858 result = sony_pf_add();
1859 if (result)
1860 goto outpresent;
1861
Stelian Pop7f09c432007-01-13 23:04:31 +01001862 if (debug) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001863 status = acpi_walk_namespace(ACPI_TYPE_METHOD,
1864 sony_nc_acpi_handle, 1, sony_walk_callback,
1865 NULL, NULL, NULL);
Stelian Pop7f09c432007-01-13 23:04:31 +01001866 if (ACPI_FAILURE(status)) {
Joe Perches50f581a2011-03-29 15:21:48 -07001867 pr_warn("unable to walk acpi resources\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001868 result = -ENODEV;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001869 goto outpresent;
Stelian Pop7f09c432007-01-13 23:04:31 +01001870 }
Stelian Popc5611622007-01-13 23:04:37 +01001871 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001872
Matthew Garrett82734bf2009-03-26 21:58:13 +09001873 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1874 &handle))) {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001875 int arg = 1;
1876 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
Matthew Garrett82734bf2009-03-26 21:58:13 +09001877 dprintk("ECON Method failed\n");
1878 }
1879
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001880 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1881 &handle))) {
1882 dprintk("Doing SNC setup\n");
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001883 /* retrieve the available handles */
Dan Carpenter7227ded2011-02-26 15:54:57 +03001884 result = sony_nc_handles_setup(sony_pf_device);
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001885 if (!result)
1886 sony_nc_function_setup(device, sony_pf_device);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001887 }
1888
malattia@linux.it1549ee62007-04-09 10:19:08 +02001889 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05001890 result = sony_laptop_setup_input(device);
malattia@linux.it1549ee62007-04-09 10:19:08 +02001891 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07001892 pr_err("Unable to create input devices\n");
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001893 goto outsnc;
malattia@linux.it1549ee62007-04-09 10:19:08 +02001894 }
1895
Alessandro Guido38cfc142008-11-12 23:03:28 +01001896 if (acpi_video_backlight_support()) {
Joe Perches50f581a2011-03-29 15:21:48 -07001897 pr_info("brightness ignored, must be controlled by ACPI video driver\n");
Mattia Dongili7751ab82011-02-19 11:52:32 +09001898 } else {
1899 sony_nc_backlight_setup();
Alessandro Guido50f62af2007-01-13 23:04:34 +01001900 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001901
malattia@linux.it56b87562007-04-09 10:19:05 +02001902 /* create sony_pf sysfs attributes related to the SNC device */
1903 for (item = sony_nc_values; item->name; ++item) {
1904
1905 if (!debug && item->debug)
1906 continue;
1907
1908 /* find the available acpiget as described in the DSDT */
1909 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
1910 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1911 *item->acpiget,
1912 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02001913 dprintk("Found %s getter: %s\n",
1914 item->name, *item->acpiget);
malattia@linux.it56b87562007-04-09 10:19:05 +02001915 item->devattr.attr.mode |= S_IRUGO;
1916 break;
1917 }
1918 }
1919
1920 /* find the available acpiset as described in the DSDT */
1921 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
1922 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1923 *item->acpiset,
1924 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02001925 dprintk("Found %s setter: %s\n",
1926 item->name, *item->acpiset);
malattia@linux.it56b87562007-04-09 10:19:05 +02001927 item->devattr.attr.mode |= S_IWUSR;
1928 break;
1929 }
1930 }
1931
1932 if (item->devattr.attr.mode != 0) {
1933 result =
1934 device_create_file(&sony_pf_device->dev,
1935 &item->devattr);
1936 if (result)
1937 goto out_sysfs;
1938 }
1939 }
1940
Stelian Pop7f09c432007-01-13 23:04:31 +01001941 return 0;
1942
malattia@linux.it56b87562007-04-09 10:19:05 +02001943 out_sysfs:
1944 for (item = sony_nc_values; item->name; ++item) {
1945 device_remove_file(&sony_pf_device->dev, &item->devattr);
1946 }
Mattia Dongili7751ab82011-02-19 11:52:32 +09001947 sony_nc_backlight_cleanup();
Mattia Dongili7df03b82007-01-13 23:04:41 +01001948
malattia@linux.it1549ee62007-04-09 10:19:08 +02001949 sony_laptop_remove_input();
1950
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001951 outsnc:
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001952 sony_nc_function_cleanup(sony_pf_device);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001953 sony_nc_handles_cleanup(sony_pf_device);
1954
1955 outpresent:
1956 sony_pf_remove();
1957
Len Browna02d1c12007-02-07 15:34:02 -05001958 outwalk:
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001959 sony_nc_rfkill_cleanup();
Stelian Pop7f09c432007-01-13 23:04:31 +01001960 return result;
1961}
1962
malattia@linux.it59b19102007-04-09 10:19:04 +02001963static int sony_nc_remove(struct acpi_device *device, int type)
Stelian Pop7f09c432007-01-13 23:04:31 +01001964{
malattia@linux.it56b87562007-04-09 10:19:05 +02001965 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01001966
Mattia Dongili7751ab82011-02-19 11:52:32 +09001967 sony_nc_backlight_cleanup();
Alessandro Guido50f62af2007-01-13 23:04:34 +01001968
malattia@linux.it59b19102007-04-09 10:19:04 +02001969 sony_nc_acpi_device = NULL;
Stelian Popc5611622007-01-13 23:04:37 +01001970
malattia@linux.it56b87562007-04-09 10:19:05 +02001971 for (item = sony_nc_values; item->name; ++item) {
1972 device_remove_file(&sony_pf_device->dev, &item->devattr);
1973 }
1974
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001975 sony_nc_function_cleanup(sony_pf_device);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001976 sony_nc_handles_cleanup(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +02001977 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +02001978 sony_laptop_remove_input();
malattia@linux.itf6119b02007-04-09 19:31:06 +02001979 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001980
1981 return 0;
1982}
1983
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001984static const struct acpi_device_id sony_device_ids[] = {
1985 {SONY_NC_HID, 0},
1986 {SONY_PIC_HID, 0},
1987 {"", 0},
1988};
1989MODULE_DEVICE_TABLE(acpi, sony_device_ids);
1990
1991static const struct acpi_device_id sony_nc_device_ids[] = {
1992 {SONY_NC_HID, 0},
1993 {"", 0},
1994};
1995
malattia@linux.it59b19102007-04-09 10:19:04 +02001996static struct acpi_driver sony_nc_driver = {
1997 .name = SONY_NC_DRIVER_NAME,
1998 .class = SONY_NC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001999 .ids = sony_nc_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02002000 .owner = THIS_MODULE,
Len Browna02d1c12007-02-07 15:34:02 -05002001 .ops = {
malattia@linux.it59b19102007-04-09 10:19:04 +02002002 .add = sony_nc_add,
2003 .remove = sony_nc_remove,
2004 .resume = sony_nc_resume,
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00002005 .notify = sony_nc_notify,
Len Browna02d1c12007-02-07 15:34:02 -05002006 },
Andrew Morton3f4f4612007-01-13 23:04:32 +01002007};
2008
malattia@linux.it33a04452007-04-09 19:26:03 +02002009/*********** SPIC (SNY6001) Device ***********/
2010
2011#define SONYPI_DEVICE_TYPE1 0x00000001
2012#define SONYPI_DEVICE_TYPE2 0x00000002
2013#define SONYPI_DEVICE_TYPE3 0x00000004
2014
Mattia Dongili22a17782007-07-16 02:34:39 +09002015#define SONYPI_TYPE1_OFFSET 0x04
2016#define SONYPI_TYPE2_OFFSET 0x12
2017#define SONYPI_TYPE3_OFFSET 0x12
malattia@linux.it33a04452007-04-09 19:26:03 +02002018
malattia@linux.it33a04452007-04-09 19:26:03 +02002019struct sony_pic_ioport {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002020 struct acpi_resource_io io1;
2021 struct acpi_resource_io io2;
malattia@linux.it33a04452007-04-09 19:26:03 +02002022 struct list_head list;
2023};
2024
2025struct sony_pic_irq {
2026 struct acpi_resource_irq irq;
2027 struct list_head list;
2028};
2029
Mattia Dongilide920432008-01-14 18:05:43 +09002030struct sonypi_eventtypes {
2031 u8 data;
2032 unsigned long mask;
2033 struct sonypi_event *events;
2034};
2035
malattia@linux.it33a04452007-04-09 19:26:03 +02002036struct sony_pic_dev {
Mattia Dongili31df7142009-09-16 00:05:29 +09002037 struct acpi_device *acpi_dev;
2038 struct sony_pic_irq *cur_irq;
2039 struct sony_pic_ioport *cur_ioport;
2040 struct list_head interrupts;
2041 struct list_head ioports;
2042 struct mutex lock;
2043 struct sonypi_eventtypes *event_types;
2044 int (*handle_irq)(const u8, const u8);
2045 int model;
2046 u16 evport_offset;
2047 u8 camera_power;
2048 u8 bluetooth_power;
2049 u8 wwan_power;
malattia@linux.it33a04452007-04-09 19:26:03 +02002050};
2051
2052static struct sony_pic_dev spic_dev = {
2053 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
2054 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
2055};
2056
Alan Jenkins5e6f9722009-09-16 00:05:32 +09002057static int spic_drv_registered;
2058
malattia@linux.it33a04452007-04-09 19:26:03 +02002059/* Event masks */
2060#define SONYPI_JOGGER_MASK 0x00000001
2061#define SONYPI_CAPTURE_MASK 0x00000002
2062#define SONYPI_FNKEY_MASK 0x00000004
2063#define SONYPI_BLUETOOTH_MASK 0x00000008
2064#define SONYPI_PKEY_MASK 0x00000010
2065#define SONYPI_BACK_MASK 0x00000020
2066#define SONYPI_HELP_MASK 0x00000040
2067#define SONYPI_LID_MASK 0x00000080
2068#define SONYPI_ZOOM_MASK 0x00000100
2069#define SONYPI_THUMBPHRASE_MASK 0x00000200
2070#define SONYPI_MEYE_MASK 0x00000400
2071#define SONYPI_MEMORYSTICK_MASK 0x00000800
2072#define SONYPI_BATTERY_MASK 0x00001000
2073#define SONYPI_WIRELESS_MASK 0x00002000
2074
2075struct sonypi_event {
2076 u8 data;
2077 u8 event;
2078};
2079
2080/* The set of possible button release events */
2081static struct sonypi_event sonypi_releaseev[] = {
2082 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
2083 { 0, 0 }
2084};
2085
2086/* The set of possible jogger events */
2087static struct sonypi_event sonypi_joggerev[] = {
2088 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
2089 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
2090 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
2091 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
2092 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
2093 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
2094 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
2095 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
2096 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
2097 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
2098 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
2099 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
2100 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
2101 { 0, 0 }
2102};
2103
2104/* The set of possible capture button events */
2105static struct sonypi_event sonypi_captureev[] = {
2106 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
2107 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002108 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02002109 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
2110 { 0, 0 }
2111};
2112
2113/* The set of possible fnkeys events */
2114static struct sonypi_event sonypi_fnkeyev[] = {
2115 { 0x10, SONYPI_EVENT_FNKEY_ESC },
2116 { 0x11, SONYPI_EVENT_FNKEY_F1 },
2117 { 0x12, SONYPI_EVENT_FNKEY_F2 },
2118 { 0x13, SONYPI_EVENT_FNKEY_F3 },
2119 { 0x14, SONYPI_EVENT_FNKEY_F4 },
2120 { 0x15, SONYPI_EVENT_FNKEY_F5 },
2121 { 0x16, SONYPI_EVENT_FNKEY_F6 },
2122 { 0x17, SONYPI_EVENT_FNKEY_F7 },
2123 { 0x18, SONYPI_EVENT_FNKEY_F8 },
2124 { 0x19, SONYPI_EVENT_FNKEY_F9 },
2125 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
2126 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
2127 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
2128 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
2129 { 0x21, SONYPI_EVENT_FNKEY_1 },
2130 { 0x22, SONYPI_EVENT_FNKEY_2 },
2131 { 0x31, SONYPI_EVENT_FNKEY_D },
2132 { 0x32, SONYPI_EVENT_FNKEY_E },
2133 { 0x33, SONYPI_EVENT_FNKEY_F },
2134 { 0x34, SONYPI_EVENT_FNKEY_S },
2135 { 0x35, SONYPI_EVENT_FNKEY_B },
2136 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
2137 { 0, 0 }
2138};
2139
2140/* The set of possible program key events */
2141static struct sonypi_event sonypi_pkeyev[] = {
2142 { 0x01, SONYPI_EVENT_PKEY_P1 },
2143 { 0x02, SONYPI_EVENT_PKEY_P2 },
2144 { 0x04, SONYPI_EVENT_PKEY_P3 },
Harald Jenny1cae7102009-03-26 21:58:18 +09002145 { 0x20, SONYPI_EVENT_PKEY_P1 },
malattia@linux.it33a04452007-04-09 19:26:03 +02002146 { 0, 0 }
2147};
2148
2149/* The set of possible bluetooth events */
2150static struct sonypi_event sonypi_blueev[] = {
2151 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
2152 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
2153 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
2154 { 0, 0 }
2155};
2156
2157/* The set of possible wireless events */
2158static struct sonypi_event sonypi_wlessev[] = {
Mattia Dongili4eeb5022011-02-19 11:52:27 +09002159 { 0x59, SONYPI_EVENT_IGNORE },
2160 { 0x5a, SONYPI_EVENT_IGNORE },
malattia@linux.it33a04452007-04-09 19:26:03 +02002161 { 0, 0 }
2162};
2163
2164/* The set of possible back button events */
2165static struct sonypi_event sonypi_backev[] = {
2166 { 0x20, SONYPI_EVENT_BACK_PRESSED },
2167 { 0, 0 }
2168};
2169
2170/* The set of possible help button events */
2171static struct sonypi_event sonypi_helpev[] = {
2172 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
2173 { 0, 0 }
2174};
2175
2176
2177/* The set of possible lid events */
2178static struct sonypi_event sonypi_lidev[] = {
2179 { 0x51, SONYPI_EVENT_LID_CLOSED },
2180 { 0x50, SONYPI_EVENT_LID_OPENED },
2181 { 0, 0 }
2182};
2183
2184/* The set of possible zoom events */
2185static struct sonypi_event sonypi_zoomev[] = {
2186 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002187 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
2188 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
Harald Jenny1cae7102009-03-26 21:58:18 +09002189 { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02002190 { 0, 0 }
2191};
2192
2193/* The set of possible thumbphrase events */
2194static struct sonypi_event sonypi_thumbphraseev[] = {
2195 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
2196 { 0, 0 }
2197};
2198
2199/* The set of possible motioneye camera events */
2200static struct sonypi_event sonypi_meyeev[] = {
2201 { 0x00, SONYPI_EVENT_MEYE_FACE },
2202 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
2203 { 0, 0 }
2204};
2205
2206/* The set of possible memorystick events */
2207static struct sonypi_event sonypi_memorystickev[] = {
2208 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
2209 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
2210 { 0, 0 }
2211};
2212
2213/* The set of possible battery events */
2214static struct sonypi_event sonypi_batteryev[] = {
2215 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
2216 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
2217 { 0, 0 }
2218};
2219
Harald Jenny1cae7102009-03-26 21:58:18 +09002220/* The set of possible volume events */
2221static struct sonypi_event sonypi_volumeev[] = {
2222 { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
2223 { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
2224 { 0, 0 }
2225};
2226
2227/* The set of possible brightness events */
2228static struct sonypi_event sonypi_brightnessev[] = {
2229 { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
2230 { 0, 0 }
2231};
2232
Mattia Dongilide920432008-01-14 18:05:43 +09002233static struct sonypi_eventtypes type1_events[] = {
2234 { 0, 0xffffffff, sonypi_releaseev },
2235 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
2236 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
2237 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
2238 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
2239 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2240 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
2241 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
2242 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2243 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
2244 { 0 },
2245};
2246static struct sonypi_eventtypes type2_events[] = {
2247 { 0, 0xffffffff, sonypi_releaseev },
2248 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
2249 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
2250 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
2251 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2252 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
2253 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
2254 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
2255 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
2256 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
2257 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
2258 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2259 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
2260 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
2261 { 0 },
2262};
2263static struct sonypi_eventtypes type3_events[] = {
2264 { 0, 0xffffffff, sonypi_releaseev },
2265 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2266 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
2267 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2268 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
2269 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002270 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
2271 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
2272 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
Harald Jenny1cae7102009-03-26 21:58:18 +09002273 { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
2274 { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002275 { 0 },
Mattia Dongilide920432008-01-14 18:05:43 +09002276};
2277
Mattia Dongili3eb87492008-01-14 18:05:45 +09002278/* low level spic calls */
malattia@linux.it33a04452007-04-09 19:26:03 +02002279#define ITERATIONS_LONG 10000
2280#define ITERATIONS_SHORT 10
2281#define wait_on_command(command, iterations) { \
2282 unsigned int n = iterations; \
2283 while (--n && (command)) \
2284 udelay(1); \
2285 if (!n) \
2286 dprintk("command failed at %s : %s (line %d)\n", \
Harvey Harrison6e574192008-04-29 00:59:20 -07002287 __FILE__, __func__, __LINE__); \
malattia@linux.it33a04452007-04-09 19:26:03 +02002288}
2289
2290static u8 sony_pic_call1(u8 dev)
2291{
2292 u8 v1, v2;
2293
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002294 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002295 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002296 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2297 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
2298 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002299 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02002300 return v2;
2301}
2302
2303static u8 sony_pic_call2(u8 dev, u8 fn)
2304{
2305 u8 v1;
2306
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002307 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002308 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002309 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2310 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002311 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002312 outb(fn, spic_dev.cur_ioport->io1.minimum);
2313 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002314 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02002315 return v1;
2316}
2317
malattia@linux.it49a11de2007-04-09 19:28:56 +02002318static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
2319{
2320 u8 v1;
2321
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002322 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2323 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2324 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2325 outb(fn, spic_dev.cur_ioport->io1.minimum);
2326 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2327 outb(v, spic_dev.cur_ioport->io1.minimum);
2328 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002329 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
2330 dev, fn, v, v1);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002331 return v1;
2332}
2333
Mattia Dongili3eb87492008-01-14 18:05:45 +09002334/*
2335 * minidrivers for SPIC models
2336 */
Mattia Dongilie93c8a62009-03-26 21:58:17 +09002337static int type3_handle_irq(const u8 data_mask, const u8 ev)
Mattia Dongili3eb87492008-01-14 18:05:45 +09002338{
2339 /*
2340 * 0x31 could mean we have to take some extra action and wait for
Mattia Dongilie93c8a62009-03-26 21:58:17 +09002341 * the next irq for some Type3 models, it will generate a new
Mattia Dongili3eb87492008-01-14 18:05:45 +09002342 * irq and we can read new data from the device:
2343 * - 0x5c and 0x5f requires 0xA0
2344 * - 0x61 requires 0xB3
2345 */
2346 if (data_mask == 0x31) {
2347 if (ev == 0x5c || ev == 0x5f)
2348 sony_pic_call1(0xA0);
2349 else if (ev == 0x61)
2350 sony_pic_call1(0xB3);
2351 return 0;
2352 }
2353 return 1;
2354}
2355
Mattia Dongili3eb87492008-01-14 18:05:45 +09002356static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
2357{
2358 struct pci_dev *pcidev;
2359
2360 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2361 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
2362 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002363 dev->model = SONYPI_DEVICE_TYPE1;
2364 dev->evport_offset = SONYPI_TYPE1_OFFSET;
2365 dev->event_types = type1_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002366 goto out;
2367 }
2368
2369 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2370 PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
2371 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002372 dev->model = SONYPI_DEVICE_TYPE2;
2373 dev->evport_offset = SONYPI_TYPE2_OFFSET;
2374 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002375 goto out;
2376 }
2377
2378 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2379 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
2380 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002381 dev->model = SONYPI_DEVICE_TYPE3;
2382 dev->handle_irq = type3_handle_irq;
2383 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2384 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002385 goto out;
2386 }
2387
2388 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2389 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
2390 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002391 dev->model = SONYPI_DEVICE_TYPE3;
2392 dev->handle_irq = type3_handle_irq;
2393 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2394 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002395 goto out;
2396 }
2397
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09002398 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2399 PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
2400 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002401 dev->model = SONYPI_DEVICE_TYPE3;
2402 dev->handle_irq = type3_handle_irq;
2403 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2404 dev->event_types = type3_events;
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09002405 goto out;
2406 }
2407
Mattia Dongili3eb87492008-01-14 18:05:45 +09002408 /* default */
Mattia Dongili31df7142009-09-16 00:05:29 +09002409 dev->model = SONYPI_DEVICE_TYPE2;
2410 dev->evport_offset = SONYPI_TYPE2_OFFSET;
2411 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002412
2413out:
2414 if (pcidev)
2415 pci_dev_put(pcidev);
2416
Joe Perches50f581a2011-03-29 15:21:48 -07002417 pr_info("detected Type%d model\n",
2418 dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
2419 dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
Mattia Dongili3eb87492008-01-14 18:05:45 +09002420}
2421
malattia@linux.it49a11de2007-04-09 19:28:56 +02002422/* camera tests and poweron/poweroff */
2423#define SONYPI_CAMERA_PICTURE 5
malattia@linux.it49a11de2007-04-09 19:28:56 +02002424#define SONYPI_CAMERA_CONTROL 0x10
malattia@linux.ite3646322007-04-28 23:34:22 +09002425
2426#define SONYPI_CAMERA_BRIGHTNESS 0
2427#define SONYPI_CAMERA_CONTRAST 1
2428#define SONYPI_CAMERA_HUE 2
2429#define SONYPI_CAMERA_COLOR 3
2430#define SONYPI_CAMERA_SHARPNESS 4
2431
2432#define SONYPI_CAMERA_EXPOSURE_MASK 0xC
2433#define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
2434#define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
2435#define SONYPI_CAMERA_MUTE_MASK 0x40
2436
2437/* the rest don't need a loop until not 0xff */
2438#define SONYPI_CAMERA_AGC 6
2439#define SONYPI_CAMERA_AGC_MASK 0x30
2440#define SONYPI_CAMERA_SHUTTER_MASK 0x7
2441
2442#define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
2443#define SONYPI_CAMERA_CONTROL 0x10
2444
2445#define SONYPI_CAMERA_STATUS 7
2446#define SONYPI_CAMERA_STATUS_READY 0x2
2447#define SONYPI_CAMERA_STATUS_POSITION 0x4
2448
2449#define SONYPI_DIRECTION_BACKWARDS 0x4
2450
2451#define SONYPI_CAMERA_REVISION 8
2452#define SONYPI_CAMERA_ROMVERSION 9
malattia@linux.it49a11de2007-04-09 19:28:56 +02002453
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002454static int __sony_pic_camera_ready(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002455{
2456 u8 v;
2457
2458 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
2459 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
2460}
2461
malattia@linux.ite3646322007-04-28 23:34:22 +09002462static int __sony_pic_camera_off(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002463{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002464 if (!camera) {
Joe Perches50f581a2011-03-29 15:21:48 -07002465 pr_warn("camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002466 return -ENODEV;
2467 }
2468
malattia@linux.it49a11de2007-04-09 19:28:56 +02002469 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
2470 SONYPI_CAMERA_MUTE_MASK),
2471 ITERATIONS_SHORT);
2472
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002473 if (spic_dev.camera_power) {
2474 sony_pic_call2(0x91, 0);
2475 spic_dev.camera_power = 0;
2476 }
2477 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002478}
2479
malattia@linux.ite3646322007-04-28 23:34:22 +09002480static int __sony_pic_camera_on(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002481{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002482 int i, j, x;
2483
2484 if (!camera) {
Joe Perches50f581a2011-03-29 15:21:48 -07002485 pr_warn("camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002486 return -ENODEV;
2487 }
malattia@linux.it49a11de2007-04-09 19:28:56 +02002488
2489 if (spic_dev.camera_power)
malattia@linux.ite3646322007-04-28 23:34:22 +09002490 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002491
2492 for (j = 5; j > 0; j--) {
2493
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002494 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002495 msleep(10);
2496 sony_pic_call1(0x93);
2497
2498 for (i = 400; i > 0; i--) {
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002499 if (__sony_pic_camera_ready())
malattia@linux.it49a11de2007-04-09 19:28:56 +02002500 break;
2501 msleep(10);
2502 }
2503 if (i)
2504 break;
2505 }
2506
2507 if (j == 0) {
Joe Perches50f581a2011-03-29 15:21:48 -07002508 pr_warn("failed to power on camera\n");
malattia@linux.ite3646322007-04-28 23:34:22 +09002509 return -ENODEV;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002510 }
2511
2512 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
2513 0x5a),
2514 ITERATIONS_SHORT);
2515
2516 spic_dev.camera_power = 1;
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002517 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002518}
2519
malattia@linux.ite3646322007-04-28 23:34:22 +09002520/* External camera command (exported to the motion eye v4l driver) */
2521int sony_pic_camera_command(int command, u8 value)
2522{
2523 if (!camera)
2524 return -EIO;
2525
2526 mutex_lock(&spic_dev.lock);
2527
2528 switch (command) {
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002529 case SONY_PIC_COMMAND_SETCAMERA:
malattia@linux.ite3646322007-04-28 23:34:22 +09002530 if (value)
2531 __sony_pic_camera_on();
2532 else
2533 __sony_pic_camera_off();
2534 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002535 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09002536 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
2537 ITERATIONS_SHORT);
2538 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002539 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
malattia@linux.ite3646322007-04-28 23:34:22 +09002540 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
2541 ITERATIONS_SHORT);
2542 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002543 case SONY_PIC_COMMAND_SETCAMERAHUE:
malattia@linux.ite3646322007-04-28 23:34:22 +09002544 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
2545 ITERATIONS_SHORT);
2546 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002547 case SONY_PIC_COMMAND_SETCAMERACOLOR:
malattia@linux.ite3646322007-04-28 23:34:22 +09002548 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
2549 ITERATIONS_SHORT);
2550 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002551 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09002552 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
2553 ITERATIONS_SHORT);
2554 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002555 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
malattia@linux.ite3646322007-04-28 23:34:22 +09002556 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
2557 ITERATIONS_SHORT);
2558 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002559 case SONY_PIC_COMMAND_SETCAMERAAGC:
malattia@linux.ite3646322007-04-28 23:34:22 +09002560 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
2561 ITERATIONS_SHORT);
2562 break;
2563 default:
Joe Perches50f581a2011-03-29 15:21:48 -07002564 pr_err("sony_pic_camera_command invalid: %d\n", command);
malattia@linux.ite3646322007-04-28 23:34:22 +09002565 break;
2566 }
2567 mutex_unlock(&spic_dev.lock);
2568 return 0;
2569}
2570EXPORT_SYMBOL(sony_pic_camera_command);
2571
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002572/* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002573static void __sony_pic_set_wwanpower(u8 state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002574{
2575 state = !!state;
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002576 if (spic_dev.wwan_power == state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002577 return;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002578 sony_pic_call2(0xB0, state);
Sergey Yanovich3ad1b762009-03-26 21:58:21 +09002579 sony_pic_call1(0x82);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002580 spic_dev.wwan_power = state;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002581}
2582
2583static ssize_t sony_pic_wwanpower_store(struct device *dev,
2584 struct device_attribute *attr,
2585 const char *buffer, size_t count)
2586{
2587 unsigned long value;
2588 if (count > 31)
2589 return -EINVAL;
2590
Mattia Dongili9e123372012-05-19 22:35:47 +09002591 if (kstrtoul(buffer, 10, &value))
2592 return -EINVAL;
2593
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002594 mutex_lock(&spic_dev.lock);
2595 __sony_pic_set_wwanpower(value);
2596 mutex_unlock(&spic_dev.lock);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002597
2598 return count;
2599}
2600
2601static ssize_t sony_pic_wwanpower_show(struct device *dev,
2602 struct device_attribute *attr, char *buffer)
2603{
2604 ssize_t count;
2605 mutex_lock(&spic_dev.lock);
2606 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
2607 mutex_unlock(&spic_dev.lock);
2608 return count;
2609}
2610
malattia@linux.it49a11de2007-04-09 19:28:56 +02002611/* bluetooth subsystem power state */
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002612static void __sony_pic_set_bluetoothpower(u8 state)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002613{
2614 state = !!state;
2615 if (spic_dev.bluetooth_power == state)
2616 return;
2617 sony_pic_call2(0x96, state);
2618 sony_pic_call1(0x82);
2619 spic_dev.bluetooth_power = state;
2620}
2621
2622static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
2623 struct device_attribute *attr,
2624 const char *buffer, size_t count)
2625{
2626 unsigned long value;
2627 if (count > 31)
2628 return -EINVAL;
2629
Mattia Dongili9e123372012-05-19 22:35:47 +09002630 if (kstrtoul(buffer, 10, &value))
2631 return -EINVAL;
2632
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002633 mutex_lock(&spic_dev.lock);
2634 __sony_pic_set_bluetoothpower(value);
2635 mutex_unlock(&spic_dev.lock);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002636
2637 return count;
2638}
2639
2640static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
2641 struct device_attribute *attr, char *buffer)
2642{
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002643 ssize_t count = 0;
2644 mutex_lock(&spic_dev.lock);
2645 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
2646 mutex_unlock(&spic_dev.lock);
2647 return count;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002648}
2649
2650/* fan speed */
2651/* FAN0 information (reverse engineered from ACPI tables) */
2652#define SONY_PIC_FAN0_STATUS 0x93
malattia@linux.it7b153f32007-04-09 19:31:25 +02002653static int sony_pic_set_fanspeed(unsigned long value)
2654{
2655 return ec_write(SONY_PIC_FAN0_STATUS, value);
2656}
2657
2658static int sony_pic_get_fanspeed(u8 *value)
2659{
2660 return ec_read(SONY_PIC_FAN0_STATUS, value);
2661}
2662
malattia@linux.it49a11de2007-04-09 19:28:56 +02002663static ssize_t sony_pic_fanspeed_store(struct device *dev,
2664 struct device_attribute *attr,
2665 const char *buffer, size_t count)
2666{
2667 unsigned long value;
2668 if (count > 31)
2669 return -EINVAL;
2670
Mattia Dongili9e123372012-05-19 22:35:47 +09002671 if (kstrtoul(buffer, 10, &value))
2672 return -EINVAL;
2673
malattia@linux.it7b153f32007-04-09 19:31:25 +02002674 if (sony_pic_set_fanspeed(value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02002675 return -EIO;
2676
2677 return count;
2678}
2679
2680static ssize_t sony_pic_fanspeed_show(struct device *dev,
2681 struct device_attribute *attr, char *buffer)
2682{
2683 u8 value = 0;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002684 if (sony_pic_get_fanspeed(&value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02002685 return -EIO;
2686
2687 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
2688}
2689
2690#define SPIC_ATTR(_name, _mode) \
2691struct device_attribute spic_attr_##_name = __ATTR(_name, \
2692 _mode, sony_pic_## _name ##_show, \
2693 sony_pic_## _name ##_store)
2694
malattia@linux.it49a11de2007-04-09 19:28:56 +02002695static SPIC_ATTR(bluetoothpower, 0644);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002696static SPIC_ATTR(wwanpower, 0644);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002697static SPIC_ATTR(fanspeed, 0644);
2698
2699static struct attribute *spic_attributes[] = {
malattia@linux.it49a11de2007-04-09 19:28:56 +02002700 &spic_attr_bluetoothpower.attr,
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002701 &spic_attr_wwanpower.attr,
malattia@linux.it49a11de2007-04-09 19:28:56 +02002702 &spic_attr_fanspeed.attr,
2703 NULL
2704};
2705
2706static struct attribute_group spic_attribute_group = {
2707 .attrs = spic_attributes
2708};
2709
malattia@linux.it7b153f32007-04-09 19:31:25 +02002710/******** SONYPI compatibility **********/
Mattia Dongilia64e62a2007-05-01 11:19:53 +09002711#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +02002712
2713/* battery / brightness / temperature addresses */
2714#define SONYPI_BAT_FLAGS 0x81
2715#define SONYPI_LCD_LIGHT 0x96
2716#define SONYPI_BAT1_PCTRM 0xa0
2717#define SONYPI_BAT1_LEFT 0xa2
2718#define SONYPI_BAT1_MAXRT 0xa4
2719#define SONYPI_BAT2_PCTRM 0xa8
2720#define SONYPI_BAT2_LEFT 0xaa
2721#define SONYPI_BAT2_MAXRT 0xac
2722#define SONYPI_BAT1_MAXTK 0xb0
2723#define SONYPI_BAT1_FULL 0xb2
2724#define SONYPI_BAT2_MAXTK 0xb8
2725#define SONYPI_BAT2_FULL 0xba
2726#define SONYPI_TEMP_STATUS 0xC1
2727
2728struct sonypi_compat_s {
2729 struct fasync_struct *fifo_async;
Stefani Seibold45465482009-12-21 14:37:26 -08002730 struct kfifo fifo;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002731 spinlock_t fifo_lock;
2732 wait_queue_head_t fifo_proc_list;
2733 atomic_t open_count;
2734};
2735static struct sonypi_compat_s sonypi_compat = {
2736 .open_count = ATOMIC_INIT(0),
2737};
2738
2739static int sonypi_misc_fasync(int fd, struct file *filp, int on)
2740{
Jonathan Corbet60aa4922009-02-01 14:52:56 -07002741 return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002742}
2743
2744static int sonypi_misc_release(struct inode *inode, struct file *file)
2745{
malattia@linux.it7b153f32007-04-09 19:31:25 +02002746 atomic_dec(&sonypi_compat.open_count);
2747 return 0;
2748}
2749
2750static int sonypi_misc_open(struct inode *inode, struct file *file)
2751{
2752 /* Flush input queue on first open */
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002753 unsigned long flags;
2754
Stefani Seibold45465482009-12-21 14:37:26 -08002755 spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002756
malattia@linux.it7b153f32007-04-09 19:31:25 +02002757 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
Stefani Seibolde64c0262009-12-21 14:37:28 -08002758 kfifo_reset(&sonypi_compat.fifo);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002759
Stefani Seibold45465482009-12-21 14:37:26 -08002760 spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002761
malattia@linux.it7b153f32007-04-09 19:31:25 +02002762 return 0;
2763}
2764
2765static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
2766 size_t count, loff_t *pos)
2767{
2768 ssize_t ret;
2769 unsigned char c;
2770
Stefani Seibold45465482009-12-21 14:37:26 -08002771 if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
malattia@linux.it7b153f32007-04-09 19:31:25 +02002772 (file->f_flags & O_NONBLOCK))
2773 return -EAGAIN;
2774
2775 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
Stefani Seibold45465482009-12-21 14:37:26 -08002776 kfifo_len(&sonypi_compat.fifo) != 0);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002777 if (ret)
2778 return ret;
2779
2780 while (ret < count &&
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002781 (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002782 &sonypi_compat.fifo_lock) == sizeof(c))) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02002783 if (put_user(c, buf++))
2784 return -EFAULT;
2785 ret++;
2786 }
2787
2788 if (ret > 0) {
2789 struct inode *inode = file->f_path.dentry->d_inode;
2790 inode->i_atime = current_fs_time(inode->i_sb);
2791 }
2792
2793 return ret;
2794}
2795
2796static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
2797{
2798 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
Stefani Seibold45465482009-12-21 14:37:26 -08002799 if (kfifo_len(&sonypi_compat.fifo))
malattia@linux.it7b153f32007-04-09 19:31:25 +02002800 return POLLIN | POLLRDNORM;
2801 return 0;
2802}
2803
2804static int ec_read16(u8 addr, u16 *value)
2805{
2806 u8 val_lb, val_hb;
2807 if (ec_read(addr, &val_lb))
2808 return -1;
2809 if (ec_read(addr + 1, &val_hb))
2810 return -1;
2811 *value = val_lb | (val_hb << 8);
2812 return 0;
2813}
2814
Alan Cox2b24ef02009-03-26 21:58:19 +09002815static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
2816 unsigned long arg)
malattia@linux.it7b153f32007-04-09 19:31:25 +02002817{
2818 int ret = 0;
2819 void __user *argp = (void __user *)arg;
2820 u8 val8;
2821 u16 val16;
2822 int value;
2823
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002824 mutex_lock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002825 switch (cmd) {
2826 case SONYPI_IOCGBRT:
Mattia Dongili62d2f232011-05-09 10:20:29 -04002827 if (sony_bl_props.dev == NULL) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02002828 ret = -EIO;
2829 break;
2830 }
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002831 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL,
2832 &value)) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02002833 ret = -EIO;
2834 break;
2835 }
2836 val8 = ((value & 0xff) - 1) << 5;
2837 if (copy_to_user(argp, &val8, sizeof(val8)))
2838 ret = -EFAULT;
2839 break;
2840 case SONYPI_IOCSBRT:
Mattia Dongili62d2f232011-05-09 10:20:29 -04002841 if (sony_bl_props.dev == NULL) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02002842 ret = -EIO;
2843 break;
2844 }
2845 if (copy_from_user(&val8, argp, sizeof(val8))) {
2846 ret = -EFAULT;
2847 break;
2848 }
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002849 value = (val8 >> 5) + 1;
2850 if (sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &value,
2851 NULL)) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02002852 ret = -EIO;
2853 break;
2854 }
2855 /* sync the backlight device status */
Mattia Dongili62d2f232011-05-09 10:20:29 -04002856 sony_bl_props.dev->props.brightness =
2857 sony_backlight_get_brightness(sony_bl_props.dev);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002858 break;
2859 case SONYPI_IOCGBAT1CAP:
2860 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
2861 ret = -EIO;
2862 break;
2863 }
2864 if (copy_to_user(argp, &val16, sizeof(val16)))
2865 ret = -EFAULT;
2866 break;
2867 case SONYPI_IOCGBAT1REM:
2868 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
2869 ret = -EIO;
2870 break;
2871 }
2872 if (copy_to_user(argp, &val16, sizeof(val16)))
2873 ret = -EFAULT;
2874 break;
2875 case SONYPI_IOCGBAT2CAP:
2876 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
2877 ret = -EIO;
2878 break;
2879 }
2880 if (copy_to_user(argp, &val16, sizeof(val16)))
2881 ret = -EFAULT;
2882 break;
2883 case SONYPI_IOCGBAT2REM:
2884 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
2885 ret = -EIO;
2886 break;
2887 }
2888 if (copy_to_user(argp, &val16, sizeof(val16)))
2889 ret = -EFAULT;
2890 break;
2891 case SONYPI_IOCGBATFLAGS:
2892 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
2893 ret = -EIO;
2894 break;
2895 }
2896 val8 &= 0x07;
2897 if (copy_to_user(argp, &val8, sizeof(val8)))
2898 ret = -EFAULT;
2899 break;
2900 case SONYPI_IOCGBLUE:
2901 val8 = spic_dev.bluetooth_power;
2902 if (copy_to_user(argp, &val8, sizeof(val8)))
2903 ret = -EFAULT;
2904 break;
2905 case SONYPI_IOCSBLUE:
2906 if (copy_from_user(&val8, argp, sizeof(val8))) {
2907 ret = -EFAULT;
2908 break;
2909 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002910 __sony_pic_set_bluetoothpower(val8);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002911 break;
2912 /* FAN Controls */
2913 case SONYPI_IOCGFAN:
2914 if (sony_pic_get_fanspeed(&val8)) {
2915 ret = -EIO;
2916 break;
2917 }
2918 if (copy_to_user(argp, &val8, sizeof(val8)))
2919 ret = -EFAULT;
2920 break;
2921 case SONYPI_IOCSFAN:
2922 if (copy_from_user(&val8, argp, sizeof(val8))) {
2923 ret = -EFAULT;
2924 break;
2925 }
2926 if (sony_pic_set_fanspeed(val8))
2927 ret = -EIO;
2928 break;
2929 /* GET Temperature (useful under APM) */
2930 case SONYPI_IOCGTEMP:
2931 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
2932 ret = -EIO;
2933 break;
2934 }
2935 if (copy_to_user(argp, &val8, sizeof(val8)))
2936 ret = -EFAULT;
2937 break;
2938 default:
2939 ret = -EINVAL;
2940 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002941 mutex_unlock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002942 return ret;
2943}
2944
2945static const struct file_operations sonypi_misc_fops = {
2946 .owner = THIS_MODULE,
2947 .read = sonypi_misc_read,
2948 .poll = sonypi_misc_poll,
2949 .open = sonypi_misc_open,
2950 .release = sonypi_misc_release,
2951 .fasync = sonypi_misc_fasync,
Alan Cox2b24ef02009-03-26 21:58:19 +09002952 .unlocked_ioctl = sonypi_misc_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002953 .llseek = noop_llseek,
malattia@linux.it7b153f32007-04-09 19:31:25 +02002954};
2955
2956static struct miscdevice sonypi_misc_device = {
2957 .minor = MISC_DYNAMIC_MINOR,
2958 .name = "sonypi",
2959 .fops = &sonypi_misc_fops,
2960};
2961
2962static void sonypi_compat_report_event(u8 event)
2963{
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002964 kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002965 sizeof(event), &sonypi_compat.fifo_lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002966 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
2967 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
2968}
2969
2970static int sonypi_compat_init(void)
2971{
2972 int error;
2973
2974 spin_lock_init(&sonypi_compat.fifo_lock);
Stefani Seibold45465482009-12-21 14:37:26 -08002975 error =
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002976 kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -08002977 if (error) {
Joe Perches50f581a2011-03-29 15:21:48 -07002978 pr_err("kfifo_alloc failed\n");
Stefani Seibold45465482009-12-21 14:37:26 -08002979 return error;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002980 }
2981
2982 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002983
2984 if (minor != -1)
2985 sonypi_misc_device.minor = minor;
2986 error = misc_register(&sonypi_misc_device);
2987 if (error) {
Joe Perches50f581a2011-03-29 15:21:48 -07002988 pr_err("misc_register failed\n");
malattia@linux.it7b153f32007-04-09 19:31:25 +02002989 goto err_free_kfifo;
2990 }
2991 if (minor == -1)
Joe Perches50f581a2011-03-29 15:21:48 -07002992 pr_info("device allocated minor is %d\n",
2993 sonypi_misc_device.minor);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002994
2995 return 0;
2996
2997err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -08002998 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002999 return error;
3000}
3001
3002static void sonypi_compat_exit(void)
3003{
3004 misc_deregister(&sonypi_misc_device);
Stefani Seibold45465482009-12-21 14:37:26 -08003005 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003006}
3007#else
3008static int sonypi_compat_init(void) { return 0; }
3009static void sonypi_compat_exit(void) { }
3010static void sonypi_compat_report_event(u8 event) { }
Mattia Dongilia64e62a2007-05-01 11:19:53 +09003011#endif /* CONFIG_SONYPI_COMPAT */
malattia@linux.it7b153f32007-04-09 19:31:25 +02003012
malattia@linux.it1549ee62007-04-09 10:19:08 +02003013/*
malattia@linux.it33a04452007-04-09 19:26:03 +02003014 * ACPI callbacks
malattia@linux.it1549ee62007-04-09 10:19:08 +02003015 */
malattia@linux.it33a04452007-04-09 19:26:03 +02003016static acpi_status
3017sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
3018{
3019 u32 i;
3020 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
3021
3022 switch (resource->type) {
3023 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003024 {
3025 /* start IO enumeration */
3026 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
3027 if (!ioport)
3028 return AE_ERROR;
3029
3030 list_add(&ioport->list, &dev->ioports);
3031 return AE_OK;
3032 }
3033
malattia@linux.it33a04452007-04-09 19:26:03 +02003034 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003035 /* end IO enumeration */
malattia@linux.it33a04452007-04-09 19:26:03 +02003036 return AE_OK;
3037
3038 case ACPI_RESOURCE_TYPE_IRQ:
3039 {
3040 struct acpi_resource_irq *p = &resource->data.irq;
3041 struct sony_pic_irq *interrupt = NULL;
3042 if (!p || !p->interrupt_count) {
3043 /*
3044 * IRQ descriptors may have no IRQ# bits set,
3045 * particularly those those w/ _STA disabled
3046 */
3047 dprintk("Blank IRQ resource\n");
3048 return AE_OK;
3049 }
3050 for (i = 0; i < p->interrupt_count; i++) {
3051 if (!p->interrupts[i]) {
Joe Perches50f581a2011-03-29 15:21:48 -07003052 pr_warn("Invalid IRQ %d\n",
3053 p->interrupts[i]);
malattia@linux.it33a04452007-04-09 19:26:03 +02003054 continue;
3055 }
3056 interrupt = kzalloc(sizeof(*interrupt),
3057 GFP_KERNEL);
3058 if (!interrupt)
3059 return AE_ERROR;
3060
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003061 list_add(&interrupt->list, &dev->interrupts);
malattia@linux.it33a04452007-04-09 19:26:03 +02003062 interrupt->irq.triggering = p->triggering;
3063 interrupt->irq.polarity = p->polarity;
3064 interrupt->irq.sharable = p->sharable;
3065 interrupt->irq.interrupt_count = 1;
3066 interrupt->irq.interrupts[0] = p->interrupts[i];
3067 }
3068 return AE_OK;
3069 }
3070 case ACPI_RESOURCE_TYPE_IO:
3071 {
3072 struct acpi_resource_io *io = &resource->data.io;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003073 struct sony_pic_ioport *ioport =
3074 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
malattia@linux.it33a04452007-04-09 19:26:03 +02003075 if (!io) {
3076 dprintk("Blank IO resource\n");
3077 return AE_OK;
3078 }
3079
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003080 if (!ioport->io1.minimum) {
3081 memcpy(&ioport->io1, io, sizeof(*io));
3082 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
3083 ioport->io1.address_length);
3084 }
3085 else if (!ioport->io2.minimum) {
3086 memcpy(&ioport->io2, io, sizeof(*io));
3087 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
3088 ioport->io2.address_length);
3089 }
3090 else {
Joe Perches50f581a2011-03-29 15:21:48 -07003091 pr_err("Unknown SPIC Type, more than 2 IO Ports\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003092 return AE_ERROR;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003093 }
malattia@linux.it33a04452007-04-09 19:26:03 +02003094 return AE_OK;
3095 }
3096 default:
3097 dprintk("Resource %d isn't an IRQ nor an IO port\n",
Joe Perches50f581a2011-03-29 15:21:48 -07003098 resource->type);
malattia@linux.it33a04452007-04-09 19:26:03 +02003099
3100 case ACPI_RESOURCE_TYPE_END_TAG:
3101 return AE_OK;
3102 }
3103 return AE_CTRL_TERMINATE;
3104}
3105
3106static int sony_pic_possible_resources(struct acpi_device *device)
3107{
3108 int result = 0;
3109 acpi_status status = AE_OK;
3110
3111 if (!device)
3112 return -EINVAL;
3113
3114 /* get device status */
3115 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
3116 dprintk("Evaluating _STA\n");
3117 result = acpi_bus_get_status(device);
3118 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07003119 pr_warn("Unable to read status\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003120 goto end;
3121 }
3122
3123 if (!device->status.enabled)
3124 dprintk("Device disabled\n");
3125 else
3126 dprintk("Device enabled\n");
3127
3128 /*
3129 * Query and parse 'method'
3130 */
3131 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
3132 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
3133 sony_pic_read_possible_resource, &spic_dev);
3134 if (ACPI_FAILURE(status)) {
Joe Perches50f581a2011-03-29 15:21:48 -07003135 pr_warn("Failure evaluating %s\n", METHOD_NAME__PRS);
malattia@linux.it33a04452007-04-09 19:26:03 +02003136 result = -ENODEV;
3137 }
3138end:
3139 return result;
3140}
3141
3142/*
3143 * Disable the spic device by calling its _DIS method
3144 */
3145static int sony_pic_disable(struct acpi_device *device)
3146{
Matthew Garrett6158d3a2008-10-29 14:01:03 -07003147 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
3148 NULL);
3149
3150 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
malattia@linux.it33a04452007-04-09 19:26:03 +02003151 return -ENXIO;
3152
3153 dprintk("Device disabled\n");
3154 return 0;
3155}
3156
3157
3158/*
3159 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
3160 *
3161 * Call _SRS to set current resources
3162 */
3163static int sony_pic_enable(struct acpi_device *device,
3164 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
3165{
3166 acpi_status status;
3167 int result = 0;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003168 /* Type 1 resource layout is:
3169 * IO
3170 * IO
3171 * IRQNoFlags
3172 * End
3173 *
3174 * Type 2 and 3 resource layout is:
3175 * IO
3176 * IRQNoFlags
3177 * End
3178 */
malattia@linux.it33a04452007-04-09 19:26:03 +02003179 struct {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003180 struct acpi_resource res1;
3181 struct acpi_resource res2;
3182 struct acpi_resource res3;
3183 struct acpi_resource res4;
malattia@linux.it33a04452007-04-09 19:26:03 +02003184 } *resource;
3185 struct acpi_buffer buffer = { 0, NULL };
3186
3187 if (!ioport || !irq)
3188 return -EINVAL;
3189
3190 /* init acpi_buffer */
3191 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
3192 if (!resource)
3193 return -ENOMEM;
3194
3195 buffer.length = sizeof(*resource) + 1;
3196 buffer.pointer = resource;
3197
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003198 /* setup Type 1 resources */
Mattia Dongili31df7142009-09-16 00:05:29 +09003199 if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003200
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003201 /* setup io resources */
3202 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
3203 resource->res1.length = sizeof(struct acpi_resource);
3204 memcpy(&resource->res1.data.io, &ioport->io1,
3205 sizeof(struct acpi_resource_io));
malattia@linux.it33a04452007-04-09 19:26:03 +02003206
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003207 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
3208 resource->res2.length = sizeof(struct acpi_resource);
3209 memcpy(&resource->res2.data.io, &ioport->io2,
3210 sizeof(struct acpi_resource_io));
3211
3212 /* setup irq resource */
3213 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
3214 resource->res3.length = sizeof(struct acpi_resource);
3215 memcpy(&resource->res3.data.irq, &irq->irq,
3216 sizeof(struct acpi_resource_irq));
3217 /* we requested a shared irq */
3218 resource->res3.data.irq.sharable = ACPI_SHARED;
3219
3220 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
3221
3222 }
3223 /* setup Type 2/3 resources */
3224 else {
3225 /* setup io resource */
3226 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
3227 resource->res1.length = sizeof(struct acpi_resource);
3228 memcpy(&resource->res1.data.io, &ioport->io1,
3229 sizeof(struct acpi_resource_io));
3230
3231 /* setup irq resource */
3232 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
3233 resource->res2.length = sizeof(struct acpi_resource);
3234 memcpy(&resource->res2.data.irq, &irq->irq,
3235 sizeof(struct acpi_resource_irq));
3236 /* we requested a shared irq */
3237 resource->res2.data.irq.sharable = ACPI_SHARED;
3238
3239 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
3240 }
malattia@linux.it33a04452007-04-09 19:26:03 +02003241
3242 /* Attempt to set the resource */
3243 dprintk("Evaluating _SRS\n");
3244 status = acpi_set_current_resources(device->handle, &buffer);
3245
3246 /* check for total failure */
3247 if (ACPI_FAILURE(status)) {
Joe Perches50f581a2011-03-29 15:21:48 -07003248 pr_err("Error evaluating _SRS\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003249 result = -ENODEV;
3250 goto end;
3251 }
3252
3253 /* Necessary device initializations calls (from sonypi) */
3254 sony_pic_call1(0x82);
3255 sony_pic_call2(0x81, 0xff);
3256 sony_pic_call1(compat ? 0x92 : 0x82);
3257
3258end:
3259 kfree(resource);
3260 return result;
3261}
3262
3263/*****************
3264 *
3265 * ISR: some event is available
3266 *
3267 *****************/
3268static irqreturn_t sony_pic_irq(int irq, void *dev_id)
3269{
3270 int i, j;
malattia@linux.it33a04452007-04-09 19:26:03 +02003271 u8 ev = 0;
3272 u8 data_mask = 0;
3273 u8 device_event = 0;
3274
3275 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
3276
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003277 ev = inb_p(dev->cur_ioport->io1.minimum);
3278 if (dev->cur_ioport->io2.minimum)
3279 data_mask = inb_p(dev->cur_ioport->io2.minimum);
3280 else
Mattia Dongilide920432008-01-14 18:05:43 +09003281 data_mask = inb_p(dev->cur_ioport->io1.minimum +
Mattia Dongili31df7142009-09-16 00:05:29 +09003282 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003283
Mattia Dongili22a17782007-07-16 02:34:39 +09003284 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
Mattia Dongilide920432008-01-14 18:05:43 +09003285 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09003286 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003287
3288 if (ev == 0x00 || ev == 0xff)
3289 return IRQ_HANDLED;
3290
Mattia Dongili31df7142009-09-16 00:05:29 +09003291 for (i = 0; dev->event_types[i].mask; i++) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003292
Mattia Dongili31df7142009-09-16 00:05:29 +09003293 if ((data_mask & dev->event_types[i].data) !=
3294 dev->event_types[i].data)
malattia@linux.it33a04452007-04-09 19:26:03 +02003295 continue;
3296
Mattia Dongili31df7142009-09-16 00:05:29 +09003297 if (!(mask & dev->event_types[i].mask))
malattia@linux.it33a04452007-04-09 19:26:03 +02003298 continue;
3299
Mattia Dongili31df7142009-09-16 00:05:29 +09003300 for (j = 0; dev->event_types[i].events[j].event; j++) {
3301 if (ev == dev->event_types[i].events[j].data) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003302 device_event =
Mattia Dongili31df7142009-09-16 00:05:29 +09003303 dev->event_types[i].events[j].event;
Mattia Dongili4eeb5022011-02-19 11:52:27 +09003304 /* some events may require ignoring */
3305 if (!device_event)
3306 return IRQ_HANDLED;
malattia@linux.it33a04452007-04-09 19:26:03 +02003307 goto found;
3308 }
3309 }
3310 }
Mattia Dongili3eb87492008-01-14 18:05:45 +09003311 /* Still not able to decode the event try to pass
3312 * it over to the minidriver
3313 */
Mattia Dongili31df7142009-09-16 00:05:29 +09003314 if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
Mattia Dongili3eb87492008-01-14 18:05:45 +09003315 return IRQ_HANDLED;
3316
Mattia Dongilide920432008-01-14 18:05:43 +09003317 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
3318 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09003319 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003320 return IRQ_HANDLED;
3321
3322found:
malattia@linux.it1549ee62007-04-09 10:19:08 +02003323 sony_laptop_report_input_event(device_event);
Mattia Dongilide920432008-01-14 18:05:43 +09003324 acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003325 sonypi_compat_report_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02003326 return IRQ_HANDLED;
3327}
3328
3329/*****************
3330 *
3331 * ACPI driver
3332 *
3333 *****************/
3334static int sony_pic_remove(struct acpi_device *device, int type)
3335{
3336 struct sony_pic_ioport *io, *tmp_io;
3337 struct sony_pic_irq *irq, *tmp_irq;
3338
3339 if (sony_pic_disable(device)) {
Joe Perches50f581a2011-03-29 15:21:48 -07003340 pr_err("Couldn't disable device\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003341 return -ENXIO;
3342 }
3343
3344 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003345 release_region(spic_dev.cur_ioport->io1.minimum,
3346 spic_dev.cur_ioport->io1.address_length);
3347 if (spic_dev.cur_ioport->io2.minimum)
3348 release_region(spic_dev.cur_ioport->io2.minimum,
3349 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02003350
Mattia Dongili015a9162007-08-12 16:20:27 +09003351 sonypi_compat_exit();
3352
malattia@linux.it1549ee62007-04-09 10:19:08 +02003353 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02003354
malattia@linux.it49a11de2007-04-09 19:28:56 +02003355 /* pf attrs */
3356 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
3357 sony_pf_remove();
3358
malattia@linux.it33a04452007-04-09 19:26:03 +02003359 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
3360 list_del(&io->list);
3361 kfree(io);
3362 }
3363 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
3364 list_del(&irq->list);
3365 kfree(irq);
3366 }
3367 spic_dev.cur_ioport = NULL;
3368 spic_dev.cur_irq = NULL;
3369
malattia@linux.itf6119b02007-04-09 19:31:06 +02003370 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003371 return 0;
3372}
3373
3374static int sony_pic_add(struct acpi_device *device)
3375{
3376 int result;
3377 struct sony_pic_ioport *io, *tmp_io;
3378 struct sony_pic_irq *irq, *tmp_irq;
3379
Joe Perches50f581a2011-03-29 15:21:48 -07003380 pr_info("%s v%s\n", SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.it33a04452007-04-09 19:26:03 +02003381
3382 spic_dev.acpi_dev = device;
3383 strcpy(acpi_device_class(device), "sony/hotkey");
Mattia Dongilide920432008-01-14 18:05:43 +09003384 sony_pic_detect_device_type(&spic_dev);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003385 mutex_init(&spic_dev.lock);
malattia@linux.it33a04452007-04-09 19:26:03 +02003386
3387 /* read _PRS resources */
3388 result = sony_pic_possible_resources(device);
3389 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07003390 pr_err("Unable to read possible resources\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003391 goto err_free_resources;
3392 }
3393
3394 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05003395 result = sony_laptop_setup_input(device);
malattia@linux.it33a04452007-04-09 19:26:03 +02003396 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07003397 pr_err("Unable to create input devices\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003398 goto err_free_resources;
3399 }
3400
Mattia Dongili015a9162007-08-12 16:20:27 +09003401 if (sonypi_compat_init())
3402 goto err_remove_input;
3403
malattia@linux.it33a04452007-04-09 19:26:03 +02003404 /* request io port */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003405 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
3406 if (request_region(io->io1.minimum, io->io1.address_length,
Lucas De Marchic8440332011-03-17 17:18:22 -03003407 "Sony Programmable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003408 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
3409 io->io1.minimum, io->io1.maximum,
3410 io->io1.address_length);
3411 /* Type 1 have 2 ioports */
3412 if (io->io2.minimum) {
3413 if (request_region(io->io2.minimum,
3414 io->io2.address_length,
Lucas De Marchic8440332011-03-17 17:18:22 -03003415 "Sony Programmable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003416 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
3417 io->io2.minimum, io->io2.maximum,
3418 io->io2.address_length);
3419 spic_dev.cur_ioport = io;
3420 break;
3421 }
3422 else {
3423 dprintk("Unable to get I/O port2: "
3424 "0x%.4x (0x%.4x) + 0x%.2x\n",
3425 io->io2.minimum, io->io2.maximum,
3426 io->io2.address_length);
3427 release_region(io->io1.minimum,
3428 io->io1.address_length);
3429 }
3430 }
3431 else {
3432 spic_dev.cur_ioport = io;
3433 break;
3434 }
malattia@linux.it33a04452007-04-09 19:26:03 +02003435 }
3436 }
3437 if (!spic_dev.cur_ioport) {
Joe Perches50f581a2011-03-29 15:21:48 -07003438 pr_err("Failed to request_region\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003439 result = -ENODEV;
Mattia Dongili015a9162007-08-12 16:20:27 +09003440 goto err_remove_compat;
malattia@linux.it33a04452007-04-09 19:26:03 +02003441 }
3442
3443 /* request IRQ */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003444 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003445 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
Yong Zhang6a0d89c2011-10-22 17:56:50 +08003446 0, "sony-laptop", &spic_dev)) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003447 dprintk("IRQ: %d - triggering: %d - "
3448 "polarity: %d - shr: %d\n",
3449 irq->irq.interrupts[0],
3450 irq->irq.triggering,
3451 irq->irq.polarity,
3452 irq->irq.sharable);
3453 spic_dev.cur_irq = irq;
3454 break;
3455 }
3456 }
3457 if (!spic_dev.cur_irq) {
Joe Perches50f581a2011-03-29 15:21:48 -07003458 pr_err("Failed to request_irq\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003459 result = -ENODEV;
3460 goto err_release_region;
3461 }
3462
3463 /* set resource status _SRS */
3464 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
3465 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07003466 pr_err("Couldn't enable device\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003467 goto err_free_irq;
3468 }
3469
malattia@linux.it49a11de2007-04-09 19:28:56 +02003470 spic_dev.bluetooth_power = -1;
3471 /* create device attributes */
3472 result = sony_pf_add();
3473 if (result)
3474 goto err_disable_device;
3475
3476 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
3477 if (result)
3478 goto err_remove_pf;
3479
malattia@linux.it33a04452007-04-09 19:26:03 +02003480 return 0;
3481
malattia@linux.it49a11de2007-04-09 19:28:56 +02003482err_remove_pf:
3483 sony_pf_remove();
3484
3485err_disable_device:
3486 sony_pic_disable(device);
3487
malattia@linux.it33a04452007-04-09 19:26:03 +02003488err_free_irq:
3489 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
3490
3491err_release_region:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003492 release_region(spic_dev.cur_ioport->io1.minimum,
3493 spic_dev.cur_ioport->io1.address_length);
3494 if (spic_dev.cur_ioport->io2.minimum)
3495 release_region(spic_dev.cur_ioport->io2.minimum,
3496 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02003497
Mattia Dongili015a9162007-08-12 16:20:27 +09003498err_remove_compat:
3499 sonypi_compat_exit();
3500
malattia@linux.it33a04452007-04-09 19:26:03 +02003501err_remove_input:
malattia@linux.it1549ee62007-04-09 10:19:08 +02003502 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02003503
3504err_free_resources:
3505 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
3506 list_del(&io->list);
3507 kfree(io);
3508 }
3509 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
3510 list_del(&irq->list);
3511 kfree(irq);
3512 }
3513 spic_dev.cur_ioport = NULL;
3514 spic_dev.cur_irq = NULL;
3515
3516 return result;
3517}
3518
3519static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
3520{
3521 if (sony_pic_disable(device))
3522 return -ENXIO;
3523 return 0;
3524}
3525
3526static int sony_pic_resume(struct acpi_device *device)
3527{
3528 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
3529 return 0;
3530}
3531
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003532static const struct acpi_device_id sony_pic_device_ids[] = {
3533 {SONY_PIC_HID, 0},
3534 {"", 0},
3535};
3536
malattia@linux.it33a04452007-04-09 19:26:03 +02003537static struct acpi_driver sony_pic_driver = {
3538 .name = SONY_PIC_DRIVER_NAME,
3539 .class = SONY_PIC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003540 .ids = sony_pic_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02003541 .owner = THIS_MODULE,
3542 .ops = {
3543 .add = sony_pic_add,
3544 .remove = sony_pic_remove,
3545 .suspend = sony_pic_suspend,
3546 .resume = sony_pic_resume,
3547 },
3548};
3549
3550static struct dmi_system_id __initdata sonypi_dmi_table[] = {
3551 {
3552 .ident = "Sony Vaio",
3553 .matches = {
3554 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
3555 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
3556 },
3557 },
3558 {
3559 .ident = "Sony Vaio",
3560 .matches = {
3561 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
3562 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
3563 },
3564 },
3565 { }
3566};
3567
malattia@linux.it59b19102007-04-09 10:19:04 +02003568static int __init sony_laptop_init(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01003569{
malattia@linux.it33a04452007-04-09 19:26:03 +02003570 int result;
3571
3572 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
3573 result = acpi_bus_register_driver(&sony_pic_driver);
3574 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07003575 pr_err("Unable to register SPIC driver\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003576 goto out;
3577 }
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003578 spic_drv_registered = 1;
malattia@linux.it33a04452007-04-09 19:26:03 +02003579 }
3580
3581 result = acpi_bus_register_driver(&sony_nc_driver);
3582 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07003583 pr_err("Unable to register SNC driver\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003584 goto out_unregister_pic;
3585 }
3586
3587 return 0;
3588
3589out_unregister_pic:
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003590 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02003591 acpi_bus_unregister_driver(&sony_pic_driver);
3592out:
3593 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +01003594}
3595
malattia@linux.it59b19102007-04-09 10:19:04 +02003596static void __exit sony_laptop_exit(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01003597{
malattia@linux.it59b19102007-04-09 10:19:04 +02003598 acpi_bus_unregister_driver(&sony_nc_driver);
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003599 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02003600 acpi_bus_unregister_driver(&sony_pic_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +01003601}
3602
malattia@linux.it59b19102007-04-09 10:19:04 +02003603module_init(sony_laptop_init);
3604module_exit(sony_laptop_exit);