blob: d6c53c622b6c1a445bbf9439de8163f6580877ab [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);
144
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900145enum sony_nc_rfkill {
146 SONY_WIFI,
147 SONY_BLUETOOTH,
148 SONY_WWAN,
149 SONY_WIMAX,
Johannes Berg19d337d2009-06-02 13:01:37 +0200150 N_SONY_RFKILL,
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900151};
152
Mattia Dongilid5a664a2009-12-17 00:08:35 +0900153static int sony_rfkill_handle;
Johannes Berg19d337d2009-06-02 13:01:37 +0200154static struct rfkill *sony_rfkill_devices[N_SONY_RFKILL];
155static int sony_rfkill_address[N_SONY_RFKILL] = {0x300, 0x500, 0x700, 0x900};
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900156static void sony_nc_rfkill_update(void);
157
malattia@linux.it1549ee62007-04-09 10:19:08 +0200158/*********** Input Devices ***********/
159
160#define SONY_LAPTOP_BUF_SIZE 128
161struct sony_laptop_input_s {
162 atomic_t users;
163 struct input_dev *jog_dev;
164 struct input_dev *key_dev;
Stefani Seibold45465482009-12-21 14:37:26 -0800165 struct kfifo fifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200166 spinlock_t fifo_lock;
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800167 struct timer_list release_key_timer;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200168};
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900169
malattia@linux.it1549ee62007-04-09 10:19:08 +0200170static struct sony_laptop_input_s sony_laptop_input = {
171 .users = ATOMIC_INIT(0),
172};
173
174struct sony_laptop_keypress {
175 struct input_dev *dev;
176 int key;
177};
178
Mattia Dongilibc57f862007-07-20 02:01:57 +0900179/* Correspondance table between sonypi events
180 * and input layer indexes in the keymap
181 */
182static int sony_laptop_input_index[] = {
Mattia Dongili3eb87492008-01-14 18:05:45 +0900183 -1, /* 0 no event */
184 -1, /* 1 SONYPI_EVENT_JOGDIAL_DOWN */
185 -1, /* 2 SONYPI_EVENT_JOGDIAL_UP */
186 -1, /* 3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
187 -1, /* 4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */
188 -1, /* 5 SONYPI_EVENT_JOGDIAL_PRESSED */
189 -1, /* 6 SONYPI_EVENT_JOGDIAL_RELEASED */
190 0, /* 7 SONYPI_EVENT_CAPTURE_PRESSED */
191 1, /* 8 SONYPI_EVENT_CAPTURE_RELEASED */
192 2, /* 9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
193 3, /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
194 4, /* 11 SONYPI_EVENT_FNKEY_ESC */
195 5, /* 12 SONYPI_EVENT_FNKEY_F1 */
196 6, /* 13 SONYPI_EVENT_FNKEY_F2 */
197 7, /* 14 SONYPI_EVENT_FNKEY_F3 */
198 8, /* 15 SONYPI_EVENT_FNKEY_F4 */
199 9, /* 16 SONYPI_EVENT_FNKEY_F5 */
200 10, /* 17 SONYPI_EVENT_FNKEY_F6 */
201 11, /* 18 SONYPI_EVENT_FNKEY_F7 */
202 12, /* 19 SONYPI_EVENT_FNKEY_F8 */
203 13, /* 20 SONYPI_EVENT_FNKEY_F9 */
204 14, /* 21 SONYPI_EVENT_FNKEY_F10 */
205 15, /* 22 SONYPI_EVENT_FNKEY_F11 */
206 16, /* 23 SONYPI_EVENT_FNKEY_F12 */
207 17, /* 24 SONYPI_EVENT_FNKEY_1 */
208 18, /* 25 SONYPI_EVENT_FNKEY_2 */
209 19, /* 26 SONYPI_EVENT_FNKEY_D */
210 20, /* 27 SONYPI_EVENT_FNKEY_E */
211 21, /* 28 SONYPI_EVENT_FNKEY_F */
212 22, /* 29 SONYPI_EVENT_FNKEY_S */
213 23, /* 30 SONYPI_EVENT_FNKEY_B */
214 24, /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */
215 25, /* 32 SONYPI_EVENT_PKEY_P1 */
216 26, /* 33 SONYPI_EVENT_PKEY_P2 */
217 27, /* 34 SONYPI_EVENT_PKEY_P3 */
218 28, /* 35 SONYPI_EVENT_BACK_PRESSED */
219 -1, /* 36 SONYPI_EVENT_LID_CLOSED */
220 -1, /* 37 SONYPI_EVENT_LID_OPENED */
221 29, /* 38 SONYPI_EVENT_BLUETOOTH_ON */
222 30, /* 39 SONYPI_EVENT_BLUETOOTH_OFF */
223 31, /* 40 SONYPI_EVENT_HELP_PRESSED */
224 32, /* 41 SONYPI_EVENT_FNKEY_ONLY */
225 33, /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
226 34, /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */
227 35, /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
228 36, /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
229 37, /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
230 38, /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */
231 39, /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
232 40, /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
233 41, /* 50 SONYPI_EVENT_ZOOM_PRESSED */
234 42, /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */
235 43, /* 52 SONYPI_EVENT_MEYE_FACE */
236 44, /* 53 SONYPI_EVENT_MEYE_OPPOSITE */
237 45, /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */
238 46, /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */
239 -1, /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */
240 -1, /* 57 SONYPI_EVENT_BATTERY_INSERT */
241 -1, /* 58 SONYPI_EVENT_BATTERY_REMOVE */
242 -1, /* 59 SONYPI_EVENT_FNKEY_RELEASED */
243 47, /* 60 SONYPI_EVENT_WIRELESS_ON */
244 48, /* 61 SONYPI_EVENT_WIRELESS_OFF */
245 49, /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */
246 50, /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900247 51, /* 64 SONYPI_EVENT_CD_EJECT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900248 52, /* 65 SONYPI_EVENT_MODEKEY_PRESSED */
249 53, /* 66 SONYPI_EVENT_PKEY_P4 */
250 54, /* 67 SONYPI_EVENT_PKEY_P5 */
251 55, /* 68 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900252 56, /* 69 SONYPI_EVENT_VOLUME_INC_PRESSED */
253 57, /* 70 SONYPI_EVENT_VOLUME_DEC_PRESSED */
254 -1, /* 71 SONYPI_EVENT_BRIGHTNESS_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900255 58, /* 72 SONYPI_EVENT_MEDIA_PRESSED */
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900256 59, /* 72 SONYPI_EVENT_VENDOR_PRESSED */
Mattia Dongilibc57f862007-07-20 02:01:57 +0900257};
258
259static int sony_laptop_input_keycode_map[] = {
260 KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
261 KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
262 KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
263 KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
264 KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
265 KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
266 KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
267 KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
268 KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
269 KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
270 KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
271 KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
272 KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
273 KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
274 KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
275 KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
276 KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
277 KEY_FN_F1, /* 17 SONYPI_EVENT_FNKEY_1 */
278 KEY_FN_F2, /* 18 SONYPI_EVENT_FNKEY_2 */
279 KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
280 KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
281 KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
282 KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
283 KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
284 KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
285 KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
286 KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
287 KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
288 KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
289 KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
290 KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
291 KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
292 KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
293 KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
294 KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
295 KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
296 KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
297 KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
298 KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
299 KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
300 KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
301 KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
302 BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
303 KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
304 KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
305 KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
306 KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
307 KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
308 KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
Mattia Dongili3eb87492008-01-14 18:05:45 +0900309 KEY_ZOOMIN, /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900310 KEY_ZOOMOUT, /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900311 KEY_EJECTCD, /* 51 SONYPI_EVENT_CD_EJECT_PRESSED */
312 KEY_F13, /* 52 SONYPI_EVENT_MODEKEY_PRESSED */
313 KEY_PROG4, /* 53 SONYPI_EVENT_PKEY_P4 */
314 KEY_F14, /* 54 SONYPI_EVENT_PKEY_P5 */
315 KEY_F15, /* 55 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900316 KEY_VOLUMEUP, /* 56 SONYPI_EVENT_VOLUME_INC_PRESSED */
317 KEY_VOLUMEDOWN, /* 57 SONYPI_EVENT_VOLUME_DEC_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900318 KEY_MEDIA, /* 58 SONYPI_EVENT_MEDIA_PRESSED */
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900319 KEY_VENDOR, /* 59 SONYPI_EVENT_VENDOR_PRESSED */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200320};
321
322/* release buttons after a short delay if pressed */
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800323static void do_sony_laptop_release_key(unsigned long unused)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200324{
325 struct sony_laptop_keypress kp;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800326 unsigned long flags;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200327
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800328 spin_lock_irqsave(&sony_laptop_input.fifo_lock, flags);
329
330 if (kfifo_out(&sony_laptop_input.fifo,
331 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200332 input_report_key(kp.dev, kp.key, 0);
333 input_sync(kp.dev);
334 }
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800335
336 /* If there is something in the fifo schedule next release. */
337 if (kfifo_len(&sony_laptop_input.fifo) != 0)
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800338 mod_timer(&sony_laptop_input.release_key_timer,
339 jiffies + msecs_to_jiffies(10));
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800340
341 spin_unlock_irqrestore(&sony_laptop_input.fifo_lock, flags);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200342}
malattia@linux.it1549ee62007-04-09 10:19:08 +0200343
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200344/* forward event to the input subsystem */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200345static void sony_laptop_report_input_event(u8 event)
346{
347 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
348 struct input_dev *key_dev = sony_laptop_input.key_dev;
349 struct sony_laptop_keypress kp = { NULL };
John Hughes747a5622011-11-16 19:51:57 +0100350 int scancode = -1;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200351
Almer S. Tigelaara83021a2009-04-12 11:26:28 +0000352 if (event == SONYPI_EVENT_FNKEY_RELEASED ||
353 event == SONYPI_EVENT_ANYBUTTON_RELEASED) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200354 /* Nothing, not all VAIOs generate this event */
355 return;
356 }
357
358 /* report events */
359 switch (event) {
360 /* jog_dev events */
361 case SONYPI_EVENT_JOGDIAL_UP:
362 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
363 input_report_rel(jog_dev, REL_WHEEL, 1);
364 input_sync(jog_dev);
365 return;
366
367 case SONYPI_EVENT_JOGDIAL_DOWN:
368 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
369 input_report_rel(jog_dev, REL_WHEEL, -1);
370 input_sync(jog_dev);
371 return;
372
373 /* key_dev events */
374 case SONYPI_EVENT_JOGDIAL_PRESSED:
375 kp.key = BTN_MIDDLE;
376 kp.dev = jog_dev;
377 break;
378
379 default:
Adrian Bunkd399d132008-02-20 00:59:03 +0200380 if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
Mattia Dongilibc57f862007-07-20 02:01:57 +0900381 dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
382 break;
383 }
John Hughes747a5622011-11-16 19:51:57 +0100384 if ((scancode = sony_laptop_input_index[event]) != -1) {
385 kp.key = sony_laptop_input_keycode_map[scancode];
Mattia Dongilibc57f862007-07-20 02:01:57 +0900386 if (kp.key != KEY_UNKNOWN)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200387 kp.dev = key_dev;
Mattia Dongilibc57f862007-07-20 02:01:57 +0900388 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200389 break;
390 }
391
392 if (kp.dev) {
John Hughes747a5622011-11-16 19:51:57 +0100393 /* if we have a scancode we emit it so we can always
394 remap the key */
395 if (scancode != -1)
396 input_event(kp.dev, EV_MSC, MSC_SCAN, scancode);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200397 input_report_key(kp.dev, kp.key, 1);
398 input_sync(kp.dev);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200399
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800400 /* schedule key release */
401 kfifo_in_locked(&sony_laptop_input.fifo,
402 (unsigned char *)&kp, sizeof(kp),
403 &sony_laptop_input.fifo_lock);
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800404 mod_timer(&sony_laptop_input.release_key_timer,
405 jiffies + msecs_to_jiffies(10));
malattia@linux.it1549ee62007-04-09 10:19:08 +0200406 } else
407 dprintk("unknown input event %.2x\n", event);
408}
409
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500410static int sony_laptop_setup_input(struct acpi_device *acpi_device)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200411{
412 struct input_dev *jog_dev;
413 struct input_dev *key_dev;
414 int i;
415 int error;
416
417 /* don't run again if already initialized */
418 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
419 return 0;
420
421 /* kfifo */
422 spin_lock_init(&sony_laptop_input.fifo_lock);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800423 error = kfifo_alloc(&sony_laptop_input.fifo,
424 SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -0800425 if (error) {
Joe Perches50f581a2011-03-29 15:21:48 -0700426 pr_err("kfifo_alloc failed\n");
malattia@linux.it1549ee62007-04-09 10:19:08 +0200427 goto err_dec_users;
428 }
429
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800430 setup_timer(&sony_laptop_input.release_key_timer,
431 do_sony_laptop_release_key, 0);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200432
433 /* input keys */
434 key_dev = input_allocate_device();
435 if (!key_dev) {
436 error = -ENOMEM;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800437 goto err_free_kfifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200438 }
439
440 key_dev->name = "Sony Vaio Keys";
441 key_dev->id.bustype = BUS_ISA;
442 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500443 key_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200444
445 /* Initialize the Input Drivers: special keys */
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800446 input_set_capability(key_dev, EV_MSC, MSC_SCAN);
447
448 __set_bit(EV_KEY, key_dev->evbit);
Mattia Dongilibc57f862007-07-20 02:01:57 +0900449 key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
450 key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
451 key_dev->keycode = &sony_laptop_input_keycode_map;
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800452 for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++)
453 __set_bit(sony_laptop_input_keycode_map[i], key_dev->keybit);
454 __clear_bit(KEY_RESERVED, key_dev->keybit);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200455
456 error = input_register_device(key_dev);
457 if (error)
458 goto err_free_keydev;
459
460 sony_laptop_input.key_dev = key_dev;
461
462 /* jogdial */
463 jog_dev = input_allocate_device();
464 if (!jog_dev) {
465 error = -ENOMEM;
466 goto err_unregister_keydev;
467 }
468
469 jog_dev->name = "Sony Vaio Jogdial";
470 jog_dev->id.bustype = BUS_ISA;
471 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
John Hughes747a5622011-11-16 19:51:57 +0100472 jog_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200473
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800474 input_set_capability(jog_dev, EV_KEY, BTN_MIDDLE);
475 input_set_capability(jog_dev, EV_REL, REL_WHEEL);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200476
477 error = input_register_device(jog_dev);
478 if (error)
479 goto err_free_jogdev;
480
481 sony_laptop_input.jog_dev = jog_dev;
482
483 return 0;
484
485err_free_jogdev:
486 input_free_device(jog_dev);
487
488err_unregister_keydev:
489 input_unregister_device(key_dev);
490 /* to avoid kref underflow below at input_free_device */
491 key_dev = NULL;
492
493err_free_keydev:
494 input_free_device(key_dev);
495
malattia@linux.it1549ee62007-04-09 10:19:08 +0200496err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -0800497 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200498
499err_dec_users:
500 atomic_dec(&sony_laptop_input.users);
501 return error;
502}
503
504static void sony_laptop_remove_input(void)
505{
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800506 struct sony_laptop_keypress kp = { NULL };
507
508 /* Cleanup only after the last user has gone */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200509 if (!atomic_dec_and_test(&sony_laptop_input.users))
510 return;
511
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800512 del_timer_sync(&sony_laptop_input.release_key_timer);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800513
514 /*
515 * Generate key-up events for remaining keys. Note that we don't
516 * need locking since nobody is adding new events to the kfifo.
517 */
518 while (kfifo_out(&sony_laptop_input.fifo,
519 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
520 input_report_key(kp.dev, kp.key, 0);
521 input_sync(kp.dev);
522 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200523
524 /* destroy input devs */
525 input_unregister_device(sony_laptop_input.key_dev);
526 sony_laptop_input.key_dev = NULL;
527
528 if (sony_laptop_input.jog_dev) {
529 input_unregister_device(sony_laptop_input.jog_dev);
530 sony_laptop_input.jog_dev = NULL;
531 }
532
Stefani Seibold45465482009-12-21 14:37:26 -0800533 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200534}
535
malattia@linux.it56b87562007-04-09 10:19:05 +0200536/*********** Platform Device ***********/
537
538static atomic_t sony_pf_users = ATOMIC_INIT(0);
539static struct platform_driver sony_pf_driver = {
540 .driver = {
541 .name = "sony-laptop",
542 .owner = THIS_MODULE,
543 }
544};
545static struct platform_device *sony_pf_device;
546
547static int sony_pf_add(void)
548{
549 int ret = 0;
550
551 /* don't run again if already initialized */
552 if (atomic_add_return(1, &sony_pf_users) > 1)
553 return 0;
554
555 ret = platform_driver_register(&sony_pf_driver);
556 if (ret)
557 goto out;
558
559 sony_pf_device = platform_device_alloc("sony-laptop", -1);
560 if (!sony_pf_device) {
561 ret = -ENOMEM;
562 goto out_platform_registered;
563 }
564
565 ret = platform_device_add(sony_pf_device);
566 if (ret)
567 goto out_platform_alloced;
568
569 return 0;
570
571 out_platform_alloced:
572 platform_device_put(sony_pf_device);
573 sony_pf_device = NULL;
574 out_platform_registered:
575 platform_driver_unregister(&sony_pf_driver);
576 out:
577 atomic_dec(&sony_pf_users);
578 return ret;
579}
580
581static void sony_pf_remove(void)
582{
583 /* deregister only after the last user has gone */
584 if (!atomic_dec_and_test(&sony_pf_users))
585 return;
586
Axel Lin08db2b32010-07-01 10:18:01 +0800587 platform_device_unregister(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +0200588 platform_driver_unregister(&sony_pf_driver);
589}
590
591/*********** SNC (SNY5001) Device ***********/
592
malattia@linux.it33a04452007-04-09 19:26:03 +0200593/* the device uses 1-based values, while the backlight subsystem uses
594 0-based values */
595#define SONY_MAX_BRIGHTNESS 8
596
597#define SNC_VALIDATE_IN 0
598#define SNC_VALIDATE_OUT 1
599
malattia@linux.it59b19102007-04-09 10:19:04 +0200600static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500601 char *);
malattia@linux.it59b19102007-04-09 10:19:04 +0200602static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500603 const char *, size_t);
Mattia Dongili156c2212007-02-12 22:01:07 +0100604static int boolean_validate(const int, const int);
605static int brightness_default_validate(const int, const int);
606
malattia@linux.it59b19102007-04-09 10:19:04 +0200607struct sony_nc_value {
Len Browna02d1c12007-02-07 15:34:02 -0500608 char *name; /* name of the entry */
609 char **acpiget; /* names of the ACPI get function */
610 char **acpiset; /* names of the ACPI set function */
Mattia Dongili156c2212007-02-12 22:01:07 +0100611 int (*validate)(const int, const int); /* input/output validation */
Len Browna02d1c12007-02-07 15:34:02 -0500612 int value; /* current setting */
613 int valid; /* Has ever been set */
614 int debug; /* active only in debug mode ? */
Lucas De Marchic8440332011-03-17 17:18:22 -0300615 struct device_attribute devattr; /* sysfs attribute */
Stelian Pop7f09c432007-01-13 23:04:31 +0100616};
617
malattia@linux.it59b19102007-04-09 10:19:04 +0200618#define SNC_HANDLE_NAMES(_name, _values...) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100619 static char *snc_##_name[] = { _values, NULL }
620
malattia@linux.it59b19102007-04-09 10:19:04 +0200621#define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100622 { \
623 .name = __stringify(_name), \
624 .acpiget = _getters, \
625 .acpiset = _setters, \
Mattia Dongili156c2212007-02-12 22:01:07 +0100626 .validate = _validate, \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100627 .debug = _debug, \
malattia@linux.it59b19102007-04-09 10:19:04 +0200628 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100629 }
630
malattia@linux.it59b19102007-04-09 10:19:04 +0200631#define SNC_HANDLE_NULL { .name = NULL }
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100632
malattia@linux.it59b19102007-04-09 10:19:04 +0200633SNC_HANDLE_NAMES(fnkey_get, "GHKE");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100634
malattia@linux.it59b19102007-04-09 10:19:04 +0200635SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
636SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100637
malattia@linux.it59b19102007-04-09 10:19:04 +0200638SNC_HANDLE_NAMES(cdpower_get, "GCDP");
639SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100640
malattia@linux.it59b19102007-04-09 10:19:04 +0200641SNC_HANDLE_NAMES(audiopower_get, "GAZP");
642SNC_HANDLE_NAMES(audiopower_set, "AZPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100643
malattia@linux.it59b19102007-04-09 10:19:04 +0200644SNC_HANDLE_NAMES(lanpower_get, "GLNP");
645SNC_HANDLE_NAMES(lanpower_set, "LNPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100646
Mattia Dongili044847e2007-07-16 02:34:33 +0900647SNC_HANDLE_NAMES(lidstate_get, "GLID");
648
649SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
650SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
651
652SNC_HANDLE_NAMES(gainbass_get, "GMGB");
653SNC_HANDLE_NAMES(gainbass_set, "CMGB");
654
malattia@linux.it59b19102007-04-09 10:19:04 +0200655SNC_HANDLE_NAMES(PID_get, "GPID");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100656
malattia@linux.it59b19102007-04-09 10:19:04 +0200657SNC_HANDLE_NAMES(CTR_get, "GCTR");
658SNC_HANDLE_NAMES(CTR_set, "SCTR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100659
malattia@linux.it59b19102007-04-09 10:19:04 +0200660SNC_HANDLE_NAMES(PCR_get, "GPCR");
661SNC_HANDLE_NAMES(PCR_set, "SPCR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100662
malattia@linux.it59b19102007-04-09 10:19:04 +0200663SNC_HANDLE_NAMES(CMI_get, "GCMI");
664SNC_HANDLE_NAMES(CMI_set, "SCMI");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100665
malattia@linux.it59b19102007-04-09 10:19:04 +0200666static struct sony_nc_value sony_nc_values[] = {
667 SNC_HANDLE(brightness_default, snc_brightness_def_get,
Mattia Dongili156c2212007-02-12 22:01:07 +0100668 snc_brightness_def_set, brightness_default_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200669 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
670 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
671 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100672 boolean_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200673 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100674 boolean_validate, 1),
Mattia Dongili044847e2007-07-16 02:34:33 +0900675 SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
676 boolean_validate, 0),
677 SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
678 boolean_validate, 0),
679 SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
680 boolean_validate, 0),
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100681 /* unknown methods */
malattia@linux.it59b19102007-04-09 10:19:04 +0200682 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
683 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
684 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
685 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
686 SNC_HANDLE_NULL
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100687};
688
malattia@linux.it59b19102007-04-09 10:19:04 +0200689static acpi_handle sony_nc_acpi_handle;
690static struct acpi_device *sony_nc_acpi_device = NULL;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100691
Mattia Dongilid78865c2007-02-07 20:01:56 +0100692/*
693 * acpi_evaluate_object wrappers
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900694 * all useful calls into SNC methods take one or zero parameters and return
695 * integers or arrays.
Mattia Dongilid78865c2007-02-07 20:01:56 +0100696 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900697static union acpi_object *__call_snc_method(acpi_handle handle, char *method,
698 u64 *value)
Stelian Pop7f09c432007-01-13 23:04:31 +0100699{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900700 union acpi_object *result = NULL;
701 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
Stelian Pop7f09c432007-01-13 23:04:31 +0100702 acpi_status status;
703
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900704 if (value) {
705 struct acpi_object_list params;
706 union acpi_object in;
707 in.type = ACPI_TYPE_INTEGER;
708 in.integer.value = *value;
709 params.count = 1;
710 params.pointer = &in;
711 status = acpi_evaluate_object(handle, method, &params, &output);
712 } else
713 status = acpi_evaluate_object(handle, method, NULL, &output);
Stelian Pop7f09c432007-01-13 23:04:31 +0100714
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900715 if (ACPI_FAILURE(status)) {
716 pr_err("Failed to evaluate [%s]\n", method);
717 return NULL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100718 }
719
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900720 result = (union acpi_object *) output.pointer;
721 if (!result)
722 dprintk("No return object [%s]\n", method);
Stelian Pop7f09c432007-01-13 23:04:31 +0100723
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900724 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +0100725}
726
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900727static int sony_nc_int_call(acpi_handle handle, char *name, int *value,
728 int *result)
Stelian Pop7f09c432007-01-13 23:04:31 +0100729{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900730 union acpi_object *object = NULL;
731 if (value) {
732 u64 v = *value;
733 object = __call_snc_method(handle, name, &v);
734 } else
735 object = __call_snc_method(handle, name, NULL);
Stelian Pop7f09c432007-01-13 23:04:31 +0100736
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900737 if (!object)
738 return -EINVAL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100739
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900740 if (object->type != ACPI_TYPE_INTEGER) {
741 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
742 ACPI_TYPE_INTEGER, object->type);
743 kfree(object);
744 return -EINVAL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100745 }
746
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900747 if (result)
748 *result = object->integer.value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100749
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900750 kfree(object);
751 return 0;
752}
753
754#define MIN(a, b) (a > b ? b : a)
755static int sony_nc_buffer_call(acpi_handle handle, char *name, u64 *value,
756 void *buffer, size_t buflen)
757{
758 size_t len = len;
759 union acpi_object *object = __call_snc_method(handle, name, value);
760
761 if (!object)
762 return -EINVAL;
763
764 if (object->type == ACPI_TYPE_BUFFER)
765 len = MIN(buflen, object->buffer.length);
766
767 else if (object->type == ACPI_TYPE_INTEGER)
768 len = MIN(buflen, sizeof(object->integer.value));
769
770 else {
771 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
772 ACPI_TYPE_BUFFER, object->type);
773 kfree(object);
774 return -EINVAL;
775 }
776
777 memcpy(buffer, object->buffer.pointer, len);
778 kfree(object);
779 return 0;
Stelian Pop7f09c432007-01-13 23:04:31 +0100780}
781
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900782struct sony_nc_handles {
783 u16 cap[0x10];
784 struct device_attribute devattr;
785};
786
Dan Carpenterf11113b2011-02-26 15:54:27 +0300787static struct sony_nc_handles *handles;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900788
789static ssize_t sony_nc_handles_show(struct device *dev,
790 struct device_attribute *attr, char *buffer)
791{
792 ssize_t len = 0;
793 int i;
794
795 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
796 len += snprintf(buffer + len, PAGE_SIZE - len, "0x%.4x ",
797 handles->cap[i]);
798 }
799 len += snprintf(buffer + len, PAGE_SIZE - len, "\n");
800
801 return len;
802}
803
804static int sony_nc_handles_setup(struct platform_device *pd)
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900805{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900806 int i, r, result, arg;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900807
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900808 handles = kzalloc(sizeof(*handles), GFP_KERNEL);
Dan Carpenter31f00752011-02-26 15:55:24 +0300809 if (!handles)
810 return -ENOMEM;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900811
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900812 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900813 arg = i + 0x20;
814 r = sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg,
815 &result);
816 if (!r) {
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900817 dprintk("caching handle 0x%.4x (offset: 0x%.2x)\n",
818 result, i);
819 handles->cap[i] = result;
Mattia Dongili56e6e712011-02-19 11:52:25 +0900820 }
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900821 }
822
Mattia Dongili855b8bc2011-04-05 23:38:35 +0900823 if (debug) {
824 sysfs_attr_init(&handles->devattr.attr);
825 handles->devattr.attr.name = "handles";
826 handles->devattr.attr.mode = S_IRUGO;
827 handles->devattr.show = sony_nc_handles_show;
828
829 /* allow reading capabilities via sysfs */
830 if (device_create_file(&pd->dev, &handles->devattr)) {
831 kfree(handles);
832 handles = NULL;
833 return -1;
834 }
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900835 }
836
837 return 0;
838}
839
840static int sony_nc_handles_cleanup(struct platform_device *pd)
841{
842 if (handles) {
Mattia Dongili855b8bc2011-04-05 23:38:35 +0900843 if (debug)
844 device_remove_file(&pd->dev, &handles->devattr);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900845 kfree(handles);
846 handles = NULL;
847 }
848 return 0;
849}
850
851static int sony_find_snc_handle(int handle)
852{
853 int i;
Mattia Dongilifef34862011-04-02 19:00:44 +0900854
855 /* not initialized yet, return early */
856 if (!handles)
857 return -1;
858
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900859 for (i = 0; i < 0x10; i++) {
860 if (handles->cap[i] == handle) {
861 dprintk("found handle 0x%.4x (offset: 0x%.2x)\n",
862 handle, i);
863 return i;
864 }
865 }
Mattia Dongili56e6e712011-02-19 11:52:25 +0900866 dprintk("handle 0x%.4x not found\n", handle);
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900867 return -1;
868}
869
870static int sony_call_snc_handle(int handle, int argument, int *result)
871{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900872 int arg, ret = 0;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900873 int offset = sony_find_snc_handle(handle);
874
875 if (offset < 0)
876 return -1;
877
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900878 arg = offset | argument;
879 ret = sony_nc_int_call(sony_nc_acpi_handle, "SN07", &arg, result);
880 dprintk("called SN07 with 0x%.4x (result: 0x%.4x)\n", arg, *result);
Mattia Dongili56e6e712011-02-19 11:52:25 +0900881 return ret;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900882}
883
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100884/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200885 * sony_nc_values input/output validate functions
Mattia Dongili156c2212007-02-12 22:01:07 +0100886 */
887
888/* brightness_default_validate:
889 *
890 * manipulate input output values to keep consistency with the
891 * backlight framework for which brightness values are 0-based.
892 */
893static int brightness_default_validate(const int direction, const int value)
894{
895 switch (direction) {
896 case SNC_VALIDATE_OUT:
897 return value - 1;
898 case SNC_VALIDATE_IN:
899 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
900 return value + 1;
901 }
902 return -EINVAL;
903}
904
905/* boolean_validate:
906 *
907 * on input validate boolean values 0/1, on output just pass the
908 * received value.
909 */
910static int boolean_validate(const int direction, const int value)
911{
912 if (direction == SNC_VALIDATE_IN) {
913 if (value != 0 && value != 1)
914 return -EINVAL;
915 }
916 return value;
917}
918
919/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200920 * Sysfs show/store common to all sony_nc_values
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100921 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200922static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
Len Browna02d1c12007-02-07 15:34:02 -0500923 char *buffer)
Stelian Pop7f09c432007-01-13 23:04:31 +0100924{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900925 int value, ret = 0;
malattia@linux.it59b19102007-04-09 10:19:04 +0200926 struct sony_nc_value *item =
927 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100928
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100929 if (!*item->acpiget)
Stelian Pop7f09c432007-01-13 23:04:31 +0100930 return -EIO;
931
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900932 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiget, NULL,
933 &value);
934 if (ret < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100935 return -EIO;
936
Mattia Dongili156c2212007-02-12 22:01:07 +0100937 if (item->validate)
938 value = item->validate(SNC_VALIDATE_OUT, value);
939
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100940 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
Stelian Pop7f09c432007-01-13 23:04:31 +0100941}
942
malattia@linux.it59b19102007-04-09 10:19:04 +0200943static ssize_t sony_nc_sysfs_store(struct device *dev,
Len Browna02d1c12007-02-07 15:34:02 -0500944 struct device_attribute *attr,
945 const char *buffer, size_t count)
Stelian Pop7f09c432007-01-13 23:04:31 +0100946{
Mattia Dongili9e123372012-05-19 22:35:47 +0900947 unsigned long value = 0;
948 int ret = 0;
malattia@linux.it59b19102007-04-09 10:19:04 +0200949 struct sony_nc_value *item =
950 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100951
952 if (!item->acpiset)
953 return -EIO;
954
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100955 if (count > 31)
956 return -EINVAL;
957
Mattia Dongili9e123372012-05-19 22:35:47 +0900958 if (kstrtoul(buffer, 10, &value))
959 return -EINVAL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100960
Mattia Dongili156c2212007-02-12 22:01:07 +0100961 if (item->validate)
962 value = item->validate(SNC_VALIDATE_IN, value);
963
964 if (value < 0)
965 return value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100966
Mattia Dongili9e123372012-05-19 22:35:47 +0900967 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
968 (int *)&value, NULL);
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900969 if (ret < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100970 return -EIO;
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900971
Andrew Morton3f4f4612007-01-13 23:04:32 +0100972 item->value = value;
973 item->valid = 1;
Stelian Pop7f09c432007-01-13 23:04:31 +0100974 return count;
975}
976
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100977
Mattia Dongilid78865c2007-02-07 20:01:56 +0100978/*
979 * Backlight device
980 */
Mattia Dongili62d2f232011-05-09 10:20:29 -0400981struct sony_backlight_props {
982 struct backlight_device *dev;
983 int handle;
984 u8 offset;
985 u8 maxlvl;
986};
987struct sony_backlight_props sony_bl_props;
988
Mattia Dongilid78865c2007-02-07 20:01:56 +0100989static int sony_backlight_update_status(struct backlight_device *bd)
Andrew Morton3f4f4612007-01-13 23:04:32 +0100990{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900991 int arg = bd->props.brightness + 1;
992 return sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &arg, NULL);
Andrew Morton3f4f4612007-01-13 23:04:32 +0100993}
994
Mattia Dongilid78865c2007-02-07 20:01:56 +0100995static int sony_backlight_get_brightness(struct backlight_device *bd)
996{
997 int value;
998
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900999 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL, &value))
Mattia Dongilid78865c2007-02-07 20:01:56 +01001000 return 0;
1001 /* brightness levels are 1-based, while backlight ones are 0-based */
1002 return value - 1;
1003}
1004
Mattia Dongili7751ab82011-02-19 11:52:32 +09001005static int sony_nc_get_brightness_ng(struct backlight_device *bd)
1006{
1007 int result;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001008 struct sony_backlight_props *sdev =
1009 (struct sony_backlight_props *)bl_get_data(bd);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001010
Mattia Dongili62d2f232011-05-09 10:20:29 -04001011 sony_call_snc_handle(sdev->handle, 0x0200, &result);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001012
Mattia Dongili62d2f232011-05-09 10:20:29 -04001013 return (result & 0xff) - sdev->offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001014}
1015
1016static int sony_nc_update_status_ng(struct backlight_device *bd)
1017{
1018 int value, result;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001019 struct sony_backlight_props *sdev =
1020 (struct sony_backlight_props *)bl_get_data(bd);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001021
Mattia Dongili62d2f232011-05-09 10:20:29 -04001022 value = bd->props.brightness + sdev->offset;
1023 if (sony_call_snc_handle(sdev->handle, 0x0100 | (value << 16), &result))
Mattia Dongili6192fa72011-04-05 23:38:36 +09001024 return -EIO;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001025
Mattia Dongili6192fa72011-04-05 23:38:36 +09001026 return value;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001027}
1028
Lionel Debrouxacc24722010-11-16 14:14:02 +01001029static const struct backlight_ops sony_backlight_ops = {
Mattia Dongili7751ab82011-02-19 11:52:32 +09001030 .options = BL_CORE_SUSPENDRESUME,
Len Browna02d1c12007-02-07 15:34:02 -05001031 .update_status = sony_backlight_update_status,
1032 .get_brightness = sony_backlight_get_brightness,
Mattia Dongilid78865c2007-02-07 20:01:56 +01001033};
Mattia Dongili7751ab82011-02-19 11:52:32 +09001034static const struct backlight_ops sony_backlight_ng_ops = {
1035 .options = BL_CORE_SUSPENDRESUME,
1036 .update_status = sony_nc_update_status_ng,
1037 .get_brightness = sony_nc_get_brightness_ng,
1038};
Mattia Dongilid78865c2007-02-07 20:01:56 +01001039
1040/*
Mattia Dongili6315fd12007-07-16 02:34:35 +09001041 * New SNC-only Vaios event mapping to driver known keys
1042 */
1043struct sony_nc_event {
1044 u8 data;
1045 u8 event;
1046};
1047
Matthew Garrett45c79422009-03-26 21:58:16 +09001048static struct sony_nc_event sony_100_events[] = {
Matthew Garrett9b578962009-03-26 21:58:14 +09001049 { 0x90, SONYPI_EVENT_PKEY_P1 },
1050 { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthias Welwarsky6479efb2009-04-01 22:10:45 +09001051 { 0x91, SONYPI_EVENT_PKEY_P2 },
Matthew Garrett9b578962009-03-26 21:58:14 +09001052 { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001053 { 0x81, SONYPI_EVENT_FNKEY_F1 },
1054 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
Anton Veretenenkof5acf5e2009-03-26 22:44:26 +09001055 { 0x82, SONYPI_EVENT_FNKEY_F2 },
1056 { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
1057 { 0x83, SONYPI_EVENT_FNKEY_F3 },
1058 { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
1059 { 0x84, SONYPI_EVENT_FNKEY_F4 },
1060 { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001061 { 0x85, SONYPI_EVENT_FNKEY_F5 },
1062 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
1063 { 0x86, SONYPI_EVENT_FNKEY_F6 },
1064 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
1065 { 0x87, SONYPI_EVENT_FNKEY_F7 },
1066 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +09001067 { 0x89, SONYPI_EVENT_FNKEY_F9 },
1068 { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001069 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
1070 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
1071 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
1072 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +09001073 { 0x9d, SONYPI_EVENT_ZOOM_PRESSED },
1074 { 0x1d, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +09001075 { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
1076 { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili4f924ba2009-12-17 00:08:33 +09001077 { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
1078 { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +09001079 { 0xa4, SONYPI_EVENT_CD_EJECT_PRESSED },
1080 { 0x24, SONYPI_EVENT_ANYBUTTON_RELEASED },
1081 { 0xa5, SONYPI_EVENT_VENDOR_PRESSED },
1082 { 0x25, SONYPI_EVENT_ANYBUTTON_RELEASED },
1083 { 0xa6, SONYPI_EVENT_HELP_PRESSED },
1084 { 0x26, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001085 { 0, 0 },
1086};
1087
Matthew Garrett45c79422009-03-26 21:58:16 +09001088static struct sony_nc_event sony_127_events[] = {
1089 { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
1090 { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
1091 { 0x82, SONYPI_EVENT_PKEY_P1 },
1092 { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
1093 { 0x83, SONYPI_EVENT_PKEY_P2 },
1094 { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
1095 { 0x84, SONYPI_EVENT_PKEY_P3 },
1096 { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
1097 { 0x85, SONYPI_EVENT_PKEY_P4 },
1098 { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
1099 { 0x86, SONYPI_EVENT_PKEY_P5 },
1100 { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett45c79422009-03-26 21:58:16 +09001101 { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
1102 { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
1103 { 0, 0 },
1104};
1105
Mattia Dongili6315fd12007-07-16 02:34:35 +09001106/*
Mattia Dongilid78865c2007-02-07 20:01:56 +01001107 * ACPI callbacks
1108 */
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001109static void sony_nc_notify(struct acpi_device *device, u32 event)
Stelian Pop7f09c432007-01-13 23:04:31 +01001110{
Mattia Dongili6315fd12007-07-16 02:34:35 +09001111 u32 ev = event;
Mattia Dongili6315fd12007-07-16 02:34:35 +09001112
Matthew Garrett9b578962009-03-26 21:58:14 +09001113 if (ev >= 0x90) {
1114 /* New-style event */
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001115 int result;
Matthew Garrett45c79422009-03-26 21:58:16 +09001116 int key_handle = 0;
Matthew Garrett9b578962009-03-26 21:58:14 +09001117 ev -= 0x90;
Mattia Dongili6315fd12007-07-16 02:34:35 +09001118
Matthew Garrett45c79422009-03-26 21:58:16 +09001119 if (sony_find_snc_handle(0x100) == ev)
1120 key_handle = 0x100;
1121 if (sony_find_snc_handle(0x127) == ev)
1122 key_handle = 0x127;
Matthew Garrett9b578962009-03-26 21:58:14 +09001123
Matthias Welwarsky6479efb2009-04-01 22:10:45 +09001124 if (key_handle) {
Matthew Garrett45c79422009-03-26 21:58:16 +09001125 struct sony_nc_event *key_event;
1126
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001127 if (sony_call_snc_handle(key_handle, 0x200, &result)) {
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001128 dprintk("sony_nc_notify, unable to decode"
Matthew Garrett45c79422009-03-26 21:58:16 +09001129 " event 0x%.2x 0x%.2x\n", key_handle,
1130 ev);
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001131 /* restore the original event */
1132 ev = event;
1133 } else {
Matthew Garrett9b578962009-03-26 21:58:14 +09001134 ev = result & 0xFF;
1135
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001136 if (key_handle == 0x100)
1137 key_event = sony_100_events;
1138 else
1139 key_event = sony_127_events;
Matthew Garrett45c79422009-03-26 21:58:16 +09001140
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001141 for (; key_event->data; key_event++) {
1142 if (key_event->data == ev) {
1143 ev = key_event->event;
1144 break;
1145 }
Matthew Garrett9b578962009-03-26 21:58:14 +09001146 }
Mattia Dongili6315fd12007-07-16 02:34:35 +09001147
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001148 if (!key_event->data)
Joe Perches50f581a2011-03-29 15:21:48 -07001149 pr_info("Unknown event: 0x%x 0x%x\n",
1150 key_handle, ev);
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001151 else
1152 sony_laptop_report_input_event(ev);
Matthew Garrett45c79422009-03-26 21:58:16 +09001153 }
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001154 } else if (sony_find_snc_handle(sony_rfkill_handle) == ev) {
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001155 sony_nc_rfkill_update();
1156 return;
Matthew Garrett9b578962009-03-26 21:58:14 +09001157 }
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001158 } else
1159 sony_laptop_report_input_event(ev);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001160
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001161 dprintk("sony_nc_notify, event: 0x%.2x\n", ev);
Len Brown14e04fb2007-08-23 15:20:26 -04001162 acpi_bus_generate_proc_event(sony_nc_acpi_device, 1, ev);
Stelian Pop7f09c432007-01-13 23:04:31 +01001163}
1164
1165static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
1166 void *context, void **return_value)
1167{
Lin Ming30823732008-12-16 16:59:35 +08001168 struct acpi_device_info *info;
Stelian Pop7f09c432007-01-13 23:04:31 +01001169
Bob Moore15b8dd52009-06-29 13:39:29 +08001170 if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
Joe Perches50f581a2011-03-29 15:21:48 -07001171 pr_warn("method: name: %4.4s, args %X\n",
Lin Ming30823732008-12-16 16:59:35 +08001172 (char *)&info->name, info->param_count);
1173
Bob Moore15b8dd52009-06-29 13:39:29 +08001174 kfree(info);
Lin Ming30823732008-12-16 16:59:35 +08001175 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001176
1177 return AE_OK;
1178}
1179
Mattia Dongilid78865c2007-02-07 20:01:56 +01001180/*
1181 * ACPI device
1182 */
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001183static int sony_nc_function_setup(struct acpi_device *device)
1184{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001185 int result, arg;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001186
1187 /* Enable all events */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001188 arg = 0xffff;
1189 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &arg, &result);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001190
1191 /* Setup hotkeys */
1192 sony_call_snc_handle(0x0100, 0, &result);
1193 sony_call_snc_handle(0x0101, 0, &result);
1194 sony_call_snc_handle(0x0102, 0x100, &result);
Almer S. Tigelaar560e84a2009-04-12 11:26:27 +00001195 sony_call_snc_handle(0x0127, 0, &result);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001196
1197 return 0;
1198}
1199
malattia@linux.it59b19102007-04-09 10:19:04 +02001200static int sony_nc_resume(struct acpi_device *device)
Mattia Dongilid78865c2007-02-07 20:01:56 +01001201{
malattia@linux.it59b19102007-04-09 10:19:04 +02001202 struct sony_nc_value *item;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001203 acpi_handle handle;
Mattia Dongilid78865c2007-02-07 20:01:56 +01001204
malattia@linux.it59b19102007-04-09 10:19:04 +02001205 for (item = sony_nc_values; item->name; item++) {
Mattia Dongilid78865c2007-02-07 20:01:56 +01001206 int ret;
1207
1208 if (!item->valid)
1209 continue;
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001210 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
1211 &item->value, NULL);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001212 if (ret < 0) {
Joe Perches50f581a2011-03-29 15:21:48 -07001213 pr_err("%s: %d\n", __func__, ret);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001214 break;
1215 }
1216 }
Mattia Dongili6315fd12007-07-16 02:34:35 +09001217
Matthew Garrett82734bf2009-03-26 21:58:13 +09001218 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1219 &handle))) {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001220 int arg = 1;
1221 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
Matthew Garrett82734bf2009-03-26 21:58:13 +09001222 dprintk("ECON Method failed\n");
1223 }
1224
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001225 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1226 &handle))) {
1227 dprintk("Doing SNC setup\n");
1228 sony_nc_function_setup(device);
1229 }
1230
Alan Jenkinsa0d97d62009-09-25 10:18:21 +01001231 /* re-read rfkill state */
1232 sony_nc_rfkill_update();
1233
Marco Chiapperodf410d52011-04-05 23:38:34 +09001234 /* restore kbd backlight states */
1235 sony_nc_kbd_backlight_resume();
1236
Mattia Dongilid78865c2007-02-07 20:01:56 +01001237 return 0;
1238}
1239
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001240static void sony_nc_rfkill_cleanup(void)
1241{
1242 int i;
1243
Johannes Berg19d337d2009-06-02 13:01:37 +02001244 for (i = 0; i < N_SONY_RFKILL; i++) {
1245 if (sony_rfkill_devices[i]) {
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001246 rfkill_unregister(sony_rfkill_devices[i]);
Johannes Berg19d337d2009-06-02 13:01:37 +02001247 rfkill_destroy(sony_rfkill_devices[i]);
1248 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001249 }
1250}
1251
Johannes Berg19d337d2009-06-02 13:01:37 +02001252static int sony_nc_rfkill_set(void *data, bool blocked)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001253{
1254 int result;
1255 int argument = sony_rfkill_address[(long) data] + 0x100;
1256
Johannes Berg19d337d2009-06-02 13:01:37 +02001257 if (!blocked)
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001258 argument |= 0x030000;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001259
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001260 return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001261}
1262
Johannes Berg19d337d2009-06-02 13:01:37 +02001263static const struct rfkill_ops sony_rfkill_ops = {
1264 .set_block = sony_nc_rfkill_set,
1265};
1266
1267static int sony_nc_setup_rfkill(struct acpi_device *device,
1268 enum sony_nc_rfkill nc_type)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001269{
1270 int err = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001271 struct rfkill *rfk;
1272 enum rfkill_type type;
1273 const char *name;
Alan Jenkins50fab072009-09-24 20:15:24 +01001274 int result;
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001275 bool hwblock, swblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001276
Johannes Berg19d337d2009-06-02 13:01:37 +02001277 switch (nc_type) {
1278 case SONY_WIFI:
1279 type = RFKILL_TYPE_WLAN;
1280 name = "sony-wifi";
1281 break;
1282 case SONY_BLUETOOTH:
1283 type = RFKILL_TYPE_BLUETOOTH;
1284 name = "sony-bluetooth";
1285 break;
1286 case SONY_WWAN:
1287 type = RFKILL_TYPE_WWAN;
1288 name = "sony-wwan";
1289 break;
1290 case SONY_WIMAX:
1291 type = RFKILL_TYPE_WIMAX;
1292 name = "sony-wimax";
1293 break;
1294 default:
1295 return -EINVAL;
Mattia Dongili53005a02009-04-12 11:26:31 +00001296 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001297
Johannes Berg19d337d2009-06-02 13:01:37 +02001298 rfk = rfkill_alloc(name, &device->dev, type,
1299 &sony_rfkill_ops, (void *)nc_type);
1300 if (!rfk)
1301 return -ENOMEM;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001302
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001303 if (sony_call_snc_handle(sony_rfkill_handle, 0x200, &result) < 0) {
1304 rfkill_destroy(rfk);
1305 return -1;
1306 }
Alan Jenkins50fab072009-09-24 20:15:24 +01001307 hwblock = !(result & 0x1);
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001308
1309 if (sony_call_snc_handle(sony_rfkill_handle,
1310 sony_rfkill_address[nc_type],
1311 &result) < 0) {
1312 rfkill_destroy(rfk);
1313 return -1;
1314 }
1315 swblock = !(result & 0x2);
1316
1317 rfkill_init_sw_state(rfk, swblock);
Alan Jenkins50fab072009-09-24 20:15:24 +01001318 rfkill_set_hw_state(rfk, hwblock);
1319
Johannes Berg19d337d2009-06-02 13:01:37 +02001320 err = rfkill_register(rfk);
1321 if (err) {
1322 rfkill_destroy(rfk);
1323 return err;
Mattia Dongili53005a02009-04-12 11:26:31 +00001324 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001325 sony_rfkill_devices[nc_type] = rfk;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001326 return err;
1327}
1328
Randy Dunlapa46a7802011-01-08 19:56:44 -08001329static void sony_nc_rfkill_update(void)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001330{
Johannes Berg19d337d2009-06-02 13:01:37 +02001331 enum sony_nc_rfkill i;
1332 int result;
1333 bool hwblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001334
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001335 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001336 hwblock = !(result & 0x1);
1337
1338 for (i = 0; i < N_SONY_RFKILL; i++) {
1339 int argument = sony_rfkill_address[i];
1340
1341 if (!sony_rfkill_devices[i])
1342 continue;
1343
1344 if (hwblock) {
Johannes Berge1f8a192009-06-11 12:08:15 +02001345 if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
1346 /* we already know we're blocked */
1347 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001348 continue;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001349 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001350
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001351 sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001352 rfkill_set_states(sony_rfkill_devices[i],
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001353 !(result & 0x2), false);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001354 }
1355}
1356
Mattia Dongili528809c2009-12-17 00:08:36 +09001357static void sony_nc_rfkill_setup(struct acpi_device *device)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001358{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001359 u64 offset;
1360 int i;
1361 unsigned char buffer[32] = { 0 };
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001362
Mattia Dongili528809c2009-12-17 00:08:36 +09001363 offset = sony_find_snc_handle(0x124);
1364 if (offset == -1) {
1365 offset = sony_find_snc_handle(0x135);
1366 if (offset == -1)
1367 return;
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001368 else
1369 sony_rfkill_handle = 0x135;
1370 } else
1371 sony_rfkill_handle = 0x124;
Mattia Dongili528809c2009-12-17 00:08:36 +09001372 dprintk("Found rkfill handle: 0x%.4x\n", sony_rfkill_handle);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001373
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001374 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
1375 32);
1376 if (i < 0)
Mattia Dongili528809c2009-12-17 00:08:36 +09001377 return;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001378
Mattia Dongili528809c2009-12-17 00:08:36 +09001379 /* the buffer is filled with magic numbers describing the devices
1380 * available, 0xff terminates the enumeration
1381 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001382 for (i = 0; i < ARRAY_SIZE(buffer); i++) {
Dmitry Torokhovc14973f2010-01-10 00:15:44 -08001383
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001384 if (buffer[i] == 0xff)
Dmitry Torokhovc14973f2010-01-10 00:15:44 -08001385 break;
1386
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001387 dprintk("Radio devices, looking at 0x%.2x\n", buffer[i]);
Mattia Dongili528809c2009-12-17 00:08:36 +09001388
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001389 if (buffer[i] == 0 && !sony_rfkill_devices[SONY_WIFI])
Mattia Dongili528809c2009-12-17 00:08:36 +09001390 sony_nc_setup_rfkill(device, SONY_WIFI);
1391
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001392 if (buffer[i] == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
Mattia Dongili528809c2009-12-17 00:08:36 +09001393 sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
1394
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001395 if ((0xf0 & buffer[i]) == 0x20 &&
Mattia Dongili528809c2009-12-17 00:08:36 +09001396 !sony_rfkill_devices[SONY_WWAN])
1397 sony_nc_setup_rfkill(device, SONY_WWAN);
1398
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001399 if (buffer[i] == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
Mattia Dongili528809c2009-12-17 00:08:36 +09001400 sony_nc_setup_rfkill(device, SONY_WIMAX);
1401 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001402}
1403
Mattia Dongilibf155712011-02-19 11:52:31 +09001404/* Keyboard backlight feature */
1405#define KBDBL_HANDLER 0x137
1406#define KBDBL_PRESENT 0xB00
1407#define SET_MODE 0xC00
Marco Chiapperodf410d52011-04-05 23:38:34 +09001408#define SET_STATE 0xD00
Mattia Dongilibf155712011-02-19 11:52:31 +09001409#define SET_TIMEOUT 0xE00
1410
1411struct kbd_backlight {
1412 int mode;
1413 int timeout;
1414 struct device_attribute mode_attr;
1415 struct device_attribute timeout_attr;
1416};
1417
Dan Carpenterf11113b2011-02-26 15:54:27 +03001418static struct kbd_backlight *kbdbl_handle;
Mattia Dongilibf155712011-02-19 11:52:31 +09001419
1420static ssize_t __sony_nc_kbd_backlight_mode_set(u8 value)
1421{
1422 int result;
1423
1424 if (value > 1)
1425 return -EINVAL;
1426
1427 if (sony_call_snc_handle(KBDBL_HANDLER,
1428 (value << 0x10) | SET_MODE, &result))
1429 return -EIO;
1430
Marco Chiapperodf410d52011-04-05 23:38:34 +09001431 /* Try to turn the light on/off immediately */
1432 sony_call_snc_handle(KBDBL_HANDLER, (value << 0x10) | SET_STATE,
1433 &result);
1434
Mattia Dongilibf155712011-02-19 11:52:31 +09001435 kbdbl_handle->mode = value;
1436
1437 return 0;
1438}
1439
1440static ssize_t sony_nc_kbd_backlight_mode_store(struct device *dev,
1441 struct device_attribute *attr,
1442 const char *buffer, size_t count)
1443{
1444 int ret = 0;
1445 unsigned long value;
1446
1447 if (count > 31)
1448 return -EINVAL;
1449
Mattia Dongili9e123372012-05-19 22:35:47 +09001450 if (kstrtoul(buffer, 10, &value))
Mattia Dongilibf155712011-02-19 11:52:31 +09001451 return -EINVAL;
1452
1453 ret = __sony_nc_kbd_backlight_mode_set(value);
1454 if (ret < 0)
1455 return ret;
1456
1457 return count;
1458}
1459
1460static ssize_t sony_nc_kbd_backlight_mode_show(struct device *dev,
1461 struct device_attribute *attr, char *buffer)
1462{
1463 ssize_t count = 0;
1464 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_handle->mode);
1465 return count;
1466}
1467
1468static int __sony_nc_kbd_backlight_timeout_set(u8 value)
1469{
1470 int result;
1471
1472 if (value > 3)
1473 return -EINVAL;
1474
1475 if (sony_call_snc_handle(KBDBL_HANDLER,
1476 (value << 0x10) | SET_TIMEOUT, &result))
1477 return -EIO;
1478
1479 kbdbl_handle->timeout = value;
1480
1481 return 0;
1482}
1483
1484static ssize_t sony_nc_kbd_backlight_timeout_store(struct device *dev,
1485 struct device_attribute *attr,
1486 const char *buffer, size_t count)
1487{
1488 int ret = 0;
1489 unsigned long value;
1490
1491 if (count > 31)
1492 return -EINVAL;
1493
Mattia Dongili9e123372012-05-19 22:35:47 +09001494 if (kstrtoul(buffer, 10, &value))
Mattia Dongilibf155712011-02-19 11:52:31 +09001495 return -EINVAL;
1496
1497 ret = __sony_nc_kbd_backlight_timeout_set(value);
1498 if (ret < 0)
1499 return ret;
1500
1501 return count;
1502}
1503
1504static ssize_t sony_nc_kbd_backlight_timeout_show(struct device *dev,
1505 struct device_attribute *attr, char *buffer)
1506{
1507 ssize_t count = 0;
1508 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_handle->timeout);
1509 return count;
1510}
1511
1512static int sony_nc_kbd_backlight_setup(struct platform_device *pd)
1513{
1514 int result;
1515
Marco Chiapperodf410d52011-04-05 23:38:34 +09001516 if (sony_call_snc_handle(KBDBL_HANDLER, KBDBL_PRESENT, &result))
Mattia Dongilibf155712011-02-19 11:52:31 +09001517 return 0;
1518 if (!(result & 0x02))
1519 return 0;
1520
1521 kbdbl_handle = kzalloc(sizeof(*kbdbl_handle), GFP_KERNEL);
Dan Carpenter31f00752011-02-26 15:55:24 +03001522 if (!kbdbl_handle)
1523 return -ENOMEM;
Mattia Dongilibf155712011-02-19 11:52:31 +09001524
1525 sysfs_attr_init(&kbdbl_handle->mode_attr.attr);
1526 kbdbl_handle->mode_attr.attr.name = "kbd_backlight";
1527 kbdbl_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
1528 kbdbl_handle->mode_attr.show = sony_nc_kbd_backlight_mode_show;
1529 kbdbl_handle->mode_attr.store = sony_nc_kbd_backlight_mode_store;
1530
1531 sysfs_attr_init(&kbdbl_handle->timeout_attr.attr);
1532 kbdbl_handle->timeout_attr.attr.name = "kbd_backlight_timeout";
1533 kbdbl_handle->timeout_attr.attr.mode = S_IRUGO | S_IWUSR;
1534 kbdbl_handle->timeout_attr.show = sony_nc_kbd_backlight_timeout_show;
1535 kbdbl_handle->timeout_attr.store = sony_nc_kbd_backlight_timeout_store;
1536
1537 if (device_create_file(&pd->dev, &kbdbl_handle->mode_attr))
1538 goto outkzalloc;
1539
1540 if (device_create_file(&pd->dev, &kbdbl_handle->timeout_attr))
1541 goto outmode;
1542
1543 __sony_nc_kbd_backlight_mode_set(kbd_backlight);
1544 __sony_nc_kbd_backlight_timeout_set(kbd_backlight_timeout);
1545
1546 return 0;
1547
1548outmode:
1549 device_remove_file(&pd->dev, &kbdbl_handle->mode_attr);
1550outkzalloc:
1551 kfree(kbdbl_handle);
1552 kbdbl_handle = NULL;
1553 return -1;
1554}
1555
1556static int sony_nc_kbd_backlight_cleanup(struct platform_device *pd)
1557{
1558 if (kbdbl_handle) {
Marco Chiapperodf410d52011-04-05 23:38:34 +09001559 int result;
1560
Mattia Dongilibf155712011-02-19 11:52:31 +09001561 device_remove_file(&pd->dev, &kbdbl_handle->mode_attr);
1562 device_remove_file(&pd->dev, &kbdbl_handle->timeout_attr);
Marco Chiapperodf410d52011-04-05 23:38:34 +09001563
1564 /* restore the default hw behaviour */
1565 sony_call_snc_handle(KBDBL_HANDLER, 0x1000 | SET_MODE, &result);
1566 sony_call_snc_handle(KBDBL_HANDLER, SET_TIMEOUT, &result);
1567
Mattia Dongilibf155712011-02-19 11:52:31 +09001568 kfree(kbdbl_handle);
1569 }
1570 return 0;
1571}
1572
Marco Chiapperodf410d52011-04-05 23:38:34 +09001573static void sony_nc_kbd_backlight_resume(void)
1574{
1575 int ignore = 0;
1576
1577 if (!kbdbl_handle)
1578 return;
1579
1580 if (kbdbl_handle->mode == 0)
1581 sony_call_snc_handle(KBDBL_HANDLER, SET_MODE, &ignore);
1582
1583 if (kbdbl_handle->timeout != 0)
1584 sony_call_snc_handle(KBDBL_HANDLER,
1585 (kbdbl_handle->timeout << 0x10) | SET_TIMEOUT,
1586 &ignore);
1587}
1588
Mattia Dongili62d2f232011-05-09 10:20:29 -04001589static void sony_nc_backlight_ng_read_limits(int handle,
1590 struct sony_backlight_props *props)
1591{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001592 u64 offset;
1593 int i;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001594 u8 min = 0xff, max = 0x00;
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001595 unsigned char buffer[32] = { 0 };
Mattia Dongili62d2f232011-05-09 10:20:29 -04001596
1597 props->handle = handle;
1598 props->offset = 0;
1599 props->maxlvl = 0xff;
1600
1601 offset = sony_find_snc_handle(handle);
1602 if (offset < 0)
1603 return;
1604
1605 /* try to read the boundaries from ACPI tables, if we fail the above
1606 * defaults should be reasonable
1607 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001608 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
1609 32);
1610 if (i < 0)
Mattia Dongili62d2f232011-05-09 10:20:29 -04001611 return;
1612
Mattia Dongili62d2f232011-05-09 10:20:29 -04001613 /* the buffer lists brightness levels available, brightness levels are
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001614 * from position 0 to 8 in the array, other values are used by ALS
1615 * control.
Mattia Dongili62d2f232011-05-09 10:20:29 -04001616 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001617 for (i = 0; i < 9 && i < ARRAY_SIZE(buffer); i++) {
Mattia Dongili62d2f232011-05-09 10:20:29 -04001618
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001619 dprintk("Brightness level: %d\n", buffer[i]);
Mattia Dongili62d2f232011-05-09 10:20:29 -04001620
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001621 if (!buffer[i])
Mattia Dongili62d2f232011-05-09 10:20:29 -04001622 break;
1623
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001624 if (buffer[i] > max)
1625 max = buffer[i];
1626 if (buffer[i] < min)
1627 min = buffer[i];
Mattia Dongili62d2f232011-05-09 10:20:29 -04001628 }
1629 props->offset = min;
1630 props->maxlvl = max;
1631 dprintk("Brightness levels: min=%d max=%d\n", props->offset,
1632 props->maxlvl);
Mattia Dongili62d2f232011-05-09 10:20:29 -04001633}
1634
Mattia Dongili7751ab82011-02-19 11:52:32 +09001635static void sony_nc_backlight_setup(void)
1636{
1637 acpi_handle unused;
1638 int max_brightness = 0;
1639 const struct backlight_ops *ops = NULL;
1640 struct backlight_properties props;
1641
1642 if (sony_find_snc_handle(0x12f) != -1) {
Mattia Dongili7751ab82011-02-19 11:52:32 +09001643 ops = &sony_backlight_ng_ops;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001644 sony_nc_backlight_ng_read_limits(0x12f, &sony_bl_props);
1645 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001646
1647 } else if (sony_find_snc_handle(0x137) != -1) {
Mattia Dongili7751ab82011-02-19 11:52:32 +09001648 ops = &sony_backlight_ng_ops;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001649 sony_nc_backlight_ng_read_limits(0x137, &sony_bl_props);
1650 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001651
1652 } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
1653 &unused))) {
1654 ops = &sony_backlight_ops;
1655 max_brightness = SONY_MAX_BRIGHTNESS - 1;
1656
1657 } else
1658 return;
1659
1660 memset(&props, 0, sizeof(struct backlight_properties));
1661 props.type = BACKLIGHT_PLATFORM;
1662 props.max_brightness = max_brightness;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001663 sony_bl_props.dev = backlight_device_register("sony", NULL,
1664 &sony_bl_props,
1665 ops, &props);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001666
Mattia Dongili62d2f232011-05-09 10:20:29 -04001667 if (IS_ERR(sony_bl_props.dev)) {
Joe Perches50f581a2011-03-29 15:21:48 -07001668 pr_warn("unable to register backlight device\n");
Mattia Dongili62d2f232011-05-09 10:20:29 -04001669 sony_bl_props.dev = NULL;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001670 } else
Mattia Dongili62d2f232011-05-09 10:20:29 -04001671 sony_bl_props.dev->props.brightness =
1672 ops->get_brightness(sony_bl_props.dev);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001673}
1674
1675static void sony_nc_backlight_cleanup(void)
1676{
Mattia Dongili62d2f232011-05-09 10:20:29 -04001677 if (sony_bl_props.dev)
1678 backlight_device_unregister(sony_bl_props.dev);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001679}
1680
malattia@linux.it59b19102007-04-09 10:19:04 +02001681static int sony_nc_add(struct acpi_device *device)
Stelian Pop7f09c432007-01-13 23:04:31 +01001682{
1683 acpi_status status;
Andrew Morton8607c672007-03-06 02:29:42 -08001684 int result = 0;
Alessandro Guido50f62af2007-01-13 23:04:34 +01001685 acpi_handle handle;
malattia@linux.it56b87562007-04-09 10:19:05 +02001686 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01001687
Joe Perches50f581a2011-03-29 15:21:48 -07001688 pr_info("%s v%s\n", SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.itf6119b02007-04-09 19:31:06 +02001689
malattia@linux.it59b19102007-04-09 10:19:04 +02001690 sony_nc_acpi_device = device;
malattia@linux.it33a04452007-04-09 19:26:03 +02001691 strcpy(acpi_device_class(device), "sony/hotkey");
Stelian Popc5611622007-01-13 23:04:37 +01001692
malattia@linux.it59b19102007-04-09 10:19:04 +02001693 sony_nc_acpi_handle = device->handle;
Stelian Pop7f09c432007-01-13 23:04:31 +01001694
Mattia Dongilib25b7322007-07-16 02:34:36 +09001695 /* read device status */
1696 result = acpi_bus_get_status(device);
1697 /* bail IFF the above call was successful and the device is not present */
1698 if (!result && !device->status.present) {
1699 dprintk("Device not present\n");
1700 result = -ENODEV;
1701 goto outwalk;
1702 }
1703
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001704 result = sony_pf_add();
1705 if (result)
1706 goto outpresent;
1707
Stelian Pop7f09c432007-01-13 23:04:31 +01001708 if (debug) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001709 status = acpi_walk_namespace(ACPI_TYPE_METHOD,
1710 sony_nc_acpi_handle, 1, sony_walk_callback,
1711 NULL, NULL, NULL);
Stelian Pop7f09c432007-01-13 23:04:31 +01001712 if (ACPI_FAILURE(status)) {
Joe Perches50f581a2011-03-29 15:21:48 -07001713 pr_warn("unable to walk acpi resources\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001714 result = -ENODEV;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001715 goto outpresent;
Stelian Pop7f09c432007-01-13 23:04:31 +01001716 }
Stelian Popc5611622007-01-13 23:04:37 +01001717 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001718
Matthew Garrett82734bf2009-03-26 21:58:13 +09001719 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1720 &handle))) {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001721 int arg = 1;
1722 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
Matthew Garrett82734bf2009-03-26 21:58:13 +09001723 dprintk("ECON Method failed\n");
1724 }
1725
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001726 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1727 &handle))) {
1728 dprintk("Doing SNC setup\n");
Dan Carpenter7227ded2011-02-26 15:54:57 +03001729 result = sony_nc_handles_setup(sony_pf_device);
1730 if (result)
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001731 goto outpresent;
Dan Carpenter7227ded2011-02-26 15:54:57 +03001732 result = sony_nc_kbd_backlight_setup(sony_pf_device);
1733 if (result)
Mattia Dongilibf155712011-02-19 11:52:31 +09001734 goto outsnc;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001735 sony_nc_function_setup(device);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001736 sony_nc_rfkill_setup(device);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001737 }
1738
malattia@linux.it1549ee62007-04-09 10:19:08 +02001739 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05001740 result = sony_laptop_setup_input(device);
malattia@linux.it1549ee62007-04-09 10:19:08 +02001741 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07001742 pr_err("Unable to create input devices\n");
Mattia Dongilibf155712011-02-19 11:52:31 +09001743 goto outkbdbacklight;
malattia@linux.it1549ee62007-04-09 10:19:08 +02001744 }
1745
Alessandro Guido38cfc142008-11-12 23:03:28 +01001746 if (acpi_video_backlight_support()) {
Joe Perches50f581a2011-03-29 15:21:48 -07001747 pr_info("brightness ignored, must be controlled by ACPI video driver\n");
Mattia Dongili7751ab82011-02-19 11:52:32 +09001748 } else {
1749 sony_nc_backlight_setup();
Alessandro Guido50f62af2007-01-13 23:04:34 +01001750 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001751
malattia@linux.it56b87562007-04-09 10:19:05 +02001752 /* create sony_pf sysfs attributes related to the SNC device */
1753 for (item = sony_nc_values; item->name; ++item) {
1754
1755 if (!debug && item->debug)
1756 continue;
1757
1758 /* find the available acpiget as described in the DSDT */
1759 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
1760 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1761 *item->acpiget,
1762 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02001763 dprintk("Found %s getter: %s\n",
1764 item->name, *item->acpiget);
malattia@linux.it56b87562007-04-09 10:19:05 +02001765 item->devattr.attr.mode |= S_IRUGO;
1766 break;
1767 }
1768 }
1769
1770 /* find the available acpiset as described in the DSDT */
1771 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
1772 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1773 *item->acpiset,
1774 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02001775 dprintk("Found %s setter: %s\n",
1776 item->name, *item->acpiset);
malattia@linux.it56b87562007-04-09 10:19:05 +02001777 item->devattr.attr.mode |= S_IWUSR;
1778 break;
1779 }
1780 }
1781
1782 if (item->devattr.attr.mode != 0) {
1783 result =
1784 device_create_file(&sony_pf_device->dev,
1785 &item->devattr);
1786 if (result)
1787 goto out_sysfs;
1788 }
1789 }
1790
Stelian Pop7f09c432007-01-13 23:04:31 +01001791 return 0;
1792
malattia@linux.it56b87562007-04-09 10:19:05 +02001793 out_sysfs:
1794 for (item = sony_nc_values; item->name; ++item) {
1795 device_remove_file(&sony_pf_device->dev, &item->devattr);
1796 }
Mattia Dongili7751ab82011-02-19 11:52:32 +09001797 sony_nc_backlight_cleanup();
Mattia Dongili7df03b82007-01-13 23:04:41 +01001798
malattia@linux.it1549ee62007-04-09 10:19:08 +02001799 sony_laptop_remove_input();
1800
Mattia Dongilibf155712011-02-19 11:52:31 +09001801 outkbdbacklight:
1802 sony_nc_kbd_backlight_cleanup(sony_pf_device);
1803
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001804 outsnc:
1805 sony_nc_handles_cleanup(sony_pf_device);
1806
1807 outpresent:
1808 sony_pf_remove();
1809
Len Browna02d1c12007-02-07 15:34:02 -05001810 outwalk:
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001811 sony_nc_rfkill_cleanup();
Stelian Pop7f09c432007-01-13 23:04:31 +01001812 return result;
1813}
1814
malattia@linux.it59b19102007-04-09 10:19:04 +02001815static int sony_nc_remove(struct acpi_device *device, int type)
Stelian Pop7f09c432007-01-13 23:04:31 +01001816{
malattia@linux.it56b87562007-04-09 10:19:05 +02001817 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01001818
Mattia Dongili7751ab82011-02-19 11:52:32 +09001819 sony_nc_backlight_cleanup();
Alessandro Guido50f62af2007-01-13 23:04:34 +01001820
malattia@linux.it59b19102007-04-09 10:19:04 +02001821 sony_nc_acpi_device = NULL;
Stelian Popc5611622007-01-13 23:04:37 +01001822
malattia@linux.it56b87562007-04-09 10:19:05 +02001823 for (item = sony_nc_values; item->name; ++item) {
1824 device_remove_file(&sony_pf_device->dev, &item->devattr);
1825 }
1826
Mattia Dongilibf155712011-02-19 11:52:31 +09001827 sony_nc_kbd_backlight_cleanup(sony_pf_device);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001828 sony_nc_handles_cleanup(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +02001829 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +02001830 sony_laptop_remove_input();
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001831 sony_nc_rfkill_cleanup();
malattia@linux.itf6119b02007-04-09 19:31:06 +02001832 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001833
1834 return 0;
1835}
1836
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001837static const struct acpi_device_id sony_device_ids[] = {
1838 {SONY_NC_HID, 0},
1839 {SONY_PIC_HID, 0},
1840 {"", 0},
1841};
1842MODULE_DEVICE_TABLE(acpi, sony_device_ids);
1843
1844static const struct acpi_device_id sony_nc_device_ids[] = {
1845 {SONY_NC_HID, 0},
1846 {"", 0},
1847};
1848
malattia@linux.it59b19102007-04-09 10:19:04 +02001849static struct acpi_driver sony_nc_driver = {
1850 .name = SONY_NC_DRIVER_NAME,
1851 .class = SONY_NC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001852 .ids = sony_nc_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02001853 .owner = THIS_MODULE,
Len Browna02d1c12007-02-07 15:34:02 -05001854 .ops = {
malattia@linux.it59b19102007-04-09 10:19:04 +02001855 .add = sony_nc_add,
1856 .remove = sony_nc_remove,
1857 .resume = sony_nc_resume,
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001858 .notify = sony_nc_notify,
Len Browna02d1c12007-02-07 15:34:02 -05001859 },
Andrew Morton3f4f4612007-01-13 23:04:32 +01001860};
1861
malattia@linux.it33a04452007-04-09 19:26:03 +02001862/*********** SPIC (SNY6001) Device ***********/
1863
1864#define SONYPI_DEVICE_TYPE1 0x00000001
1865#define SONYPI_DEVICE_TYPE2 0x00000002
1866#define SONYPI_DEVICE_TYPE3 0x00000004
1867
Mattia Dongili22a17782007-07-16 02:34:39 +09001868#define SONYPI_TYPE1_OFFSET 0x04
1869#define SONYPI_TYPE2_OFFSET 0x12
1870#define SONYPI_TYPE3_OFFSET 0x12
malattia@linux.it33a04452007-04-09 19:26:03 +02001871
malattia@linux.it33a04452007-04-09 19:26:03 +02001872struct sony_pic_ioport {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001873 struct acpi_resource_io io1;
1874 struct acpi_resource_io io2;
malattia@linux.it33a04452007-04-09 19:26:03 +02001875 struct list_head list;
1876};
1877
1878struct sony_pic_irq {
1879 struct acpi_resource_irq irq;
1880 struct list_head list;
1881};
1882
Mattia Dongilide920432008-01-14 18:05:43 +09001883struct sonypi_eventtypes {
1884 u8 data;
1885 unsigned long mask;
1886 struct sonypi_event *events;
1887};
1888
malattia@linux.it33a04452007-04-09 19:26:03 +02001889struct sony_pic_dev {
Mattia Dongili31df7142009-09-16 00:05:29 +09001890 struct acpi_device *acpi_dev;
1891 struct sony_pic_irq *cur_irq;
1892 struct sony_pic_ioport *cur_ioport;
1893 struct list_head interrupts;
1894 struct list_head ioports;
1895 struct mutex lock;
1896 struct sonypi_eventtypes *event_types;
1897 int (*handle_irq)(const u8, const u8);
1898 int model;
1899 u16 evport_offset;
1900 u8 camera_power;
1901 u8 bluetooth_power;
1902 u8 wwan_power;
malattia@linux.it33a04452007-04-09 19:26:03 +02001903};
1904
1905static struct sony_pic_dev spic_dev = {
1906 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
1907 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
1908};
1909
Alan Jenkins5e6f9722009-09-16 00:05:32 +09001910static int spic_drv_registered;
1911
malattia@linux.it33a04452007-04-09 19:26:03 +02001912/* Event masks */
1913#define SONYPI_JOGGER_MASK 0x00000001
1914#define SONYPI_CAPTURE_MASK 0x00000002
1915#define SONYPI_FNKEY_MASK 0x00000004
1916#define SONYPI_BLUETOOTH_MASK 0x00000008
1917#define SONYPI_PKEY_MASK 0x00000010
1918#define SONYPI_BACK_MASK 0x00000020
1919#define SONYPI_HELP_MASK 0x00000040
1920#define SONYPI_LID_MASK 0x00000080
1921#define SONYPI_ZOOM_MASK 0x00000100
1922#define SONYPI_THUMBPHRASE_MASK 0x00000200
1923#define SONYPI_MEYE_MASK 0x00000400
1924#define SONYPI_MEMORYSTICK_MASK 0x00000800
1925#define SONYPI_BATTERY_MASK 0x00001000
1926#define SONYPI_WIRELESS_MASK 0x00002000
1927
1928struct sonypi_event {
1929 u8 data;
1930 u8 event;
1931};
1932
1933/* The set of possible button release events */
1934static struct sonypi_event sonypi_releaseev[] = {
1935 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
1936 { 0, 0 }
1937};
1938
1939/* The set of possible jogger events */
1940static struct sonypi_event sonypi_joggerev[] = {
1941 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
1942 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
1943 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
1944 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
1945 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
1946 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
1947 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
1948 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
1949 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
1950 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
1951 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
1952 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
1953 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
1954 { 0, 0 }
1955};
1956
1957/* The set of possible capture button events */
1958static struct sonypi_event sonypi_captureev[] = {
1959 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
1960 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09001961 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02001962 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
1963 { 0, 0 }
1964};
1965
1966/* The set of possible fnkeys events */
1967static struct sonypi_event sonypi_fnkeyev[] = {
1968 { 0x10, SONYPI_EVENT_FNKEY_ESC },
1969 { 0x11, SONYPI_EVENT_FNKEY_F1 },
1970 { 0x12, SONYPI_EVENT_FNKEY_F2 },
1971 { 0x13, SONYPI_EVENT_FNKEY_F3 },
1972 { 0x14, SONYPI_EVENT_FNKEY_F4 },
1973 { 0x15, SONYPI_EVENT_FNKEY_F5 },
1974 { 0x16, SONYPI_EVENT_FNKEY_F6 },
1975 { 0x17, SONYPI_EVENT_FNKEY_F7 },
1976 { 0x18, SONYPI_EVENT_FNKEY_F8 },
1977 { 0x19, SONYPI_EVENT_FNKEY_F9 },
1978 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
1979 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
1980 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
1981 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
1982 { 0x21, SONYPI_EVENT_FNKEY_1 },
1983 { 0x22, SONYPI_EVENT_FNKEY_2 },
1984 { 0x31, SONYPI_EVENT_FNKEY_D },
1985 { 0x32, SONYPI_EVENT_FNKEY_E },
1986 { 0x33, SONYPI_EVENT_FNKEY_F },
1987 { 0x34, SONYPI_EVENT_FNKEY_S },
1988 { 0x35, SONYPI_EVENT_FNKEY_B },
1989 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
1990 { 0, 0 }
1991};
1992
1993/* The set of possible program key events */
1994static struct sonypi_event sonypi_pkeyev[] = {
1995 { 0x01, SONYPI_EVENT_PKEY_P1 },
1996 { 0x02, SONYPI_EVENT_PKEY_P2 },
1997 { 0x04, SONYPI_EVENT_PKEY_P3 },
Harald Jenny1cae7102009-03-26 21:58:18 +09001998 { 0x20, SONYPI_EVENT_PKEY_P1 },
malattia@linux.it33a04452007-04-09 19:26:03 +02001999 { 0, 0 }
2000};
2001
2002/* The set of possible bluetooth events */
2003static struct sonypi_event sonypi_blueev[] = {
2004 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
2005 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
2006 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
2007 { 0, 0 }
2008};
2009
2010/* The set of possible wireless events */
2011static struct sonypi_event sonypi_wlessev[] = {
Mattia Dongili4eeb5022011-02-19 11:52:27 +09002012 { 0x59, SONYPI_EVENT_IGNORE },
2013 { 0x5a, SONYPI_EVENT_IGNORE },
malattia@linux.it33a04452007-04-09 19:26:03 +02002014 { 0, 0 }
2015};
2016
2017/* The set of possible back button events */
2018static struct sonypi_event sonypi_backev[] = {
2019 { 0x20, SONYPI_EVENT_BACK_PRESSED },
2020 { 0, 0 }
2021};
2022
2023/* The set of possible help button events */
2024static struct sonypi_event sonypi_helpev[] = {
2025 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
2026 { 0, 0 }
2027};
2028
2029
2030/* The set of possible lid events */
2031static struct sonypi_event sonypi_lidev[] = {
2032 { 0x51, SONYPI_EVENT_LID_CLOSED },
2033 { 0x50, SONYPI_EVENT_LID_OPENED },
2034 { 0, 0 }
2035};
2036
2037/* The set of possible zoom events */
2038static struct sonypi_event sonypi_zoomev[] = {
2039 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002040 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
2041 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
Harald Jenny1cae7102009-03-26 21:58:18 +09002042 { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02002043 { 0, 0 }
2044};
2045
2046/* The set of possible thumbphrase events */
2047static struct sonypi_event sonypi_thumbphraseev[] = {
2048 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
2049 { 0, 0 }
2050};
2051
2052/* The set of possible motioneye camera events */
2053static struct sonypi_event sonypi_meyeev[] = {
2054 { 0x00, SONYPI_EVENT_MEYE_FACE },
2055 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
2056 { 0, 0 }
2057};
2058
2059/* The set of possible memorystick events */
2060static struct sonypi_event sonypi_memorystickev[] = {
2061 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
2062 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
2063 { 0, 0 }
2064};
2065
2066/* The set of possible battery events */
2067static struct sonypi_event sonypi_batteryev[] = {
2068 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
2069 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
2070 { 0, 0 }
2071};
2072
Harald Jenny1cae7102009-03-26 21:58:18 +09002073/* The set of possible volume events */
2074static struct sonypi_event sonypi_volumeev[] = {
2075 { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
2076 { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
2077 { 0, 0 }
2078};
2079
2080/* The set of possible brightness events */
2081static struct sonypi_event sonypi_brightnessev[] = {
2082 { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
2083 { 0, 0 }
2084};
2085
Mattia Dongilide920432008-01-14 18:05:43 +09002086static struct sonypi_eventtypes type1_events[] = {
2087 { 0, 0xffffffff, sonypi_releaseev },
2088 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
2089 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
2090 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
2091 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
2092 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2093 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
2094 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
2095 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2096 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
2097 { 0 },
2098};
2099static struct sonypi_eventtypes type2_events[] = {
2100 { 0, 0xffffffff, sonypi_releaseev },
2101 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
2102 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
2103 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
2104 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2105 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
2106 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
2107 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
2108 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
2109 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
2110 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
2111 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2112 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
2113 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
2114 { 0 },
2115};
2116static struct sonypi_eventtypes type3_events[] = {
2117 { 0, 0xffffffff, sonypi_releaseev },
2118 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2119 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
2120 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2121 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
2122 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002123 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
2124 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
2125 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
Harald Jenny1cae7102009-03-26 21:58:18 +09002126 { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
2127 { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002128 { 0 },
Mattia Dongilide920432008-01-14 18:05:43 +09002129};
2130
Mattia Dongili3eb87492008-01-14 18:05:45 +09002131/* low level spic calls */
malattia@linux.it33a04452007-04-09 19:26:03 +02002132#define ITERATIONS_LONG 10000
2133#define ITERATIONS_SHORT 10
2134#define wait_on_command(command, iterations) { \
2135 unsigned int n = iterations; \
2136 while (--n && (command)) \
2137 udelay(1); \
2138 if (!n) \
2139 dprintk("command failed at %s : %s (line %d)\n", \
Harvey Harrison6e574192008-04-29 00:59:20 -07002140 __FILE__, __func__, __LINE__); \
malattia@linux.it33a04452007-04-09 19:26:03 +02002141}
2142
2143static u8 sony_pic_call1(u8 dev)
2144{
2145 u8 v1, v2;
2146
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002147 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002148 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002149 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2150 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
2151 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002152 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02002153 return v2;
2154}
2155
2156static u8 sony_pic_call2(u8 dev, u8 fn)
2157{
2158 u8 v1;
2159
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002160 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002161 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002162 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2163 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002164 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002165 outb(fn, spic_dev.cur_ioport->io1.minimum);
2166 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002167 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02002168 return v1;
2169}
2170
malattia@linux.it49a11de2007-04-09 19:28:56 +02002171static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
2172{
2173 u8 v1;
2174
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002175 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2176 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2177 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2178 outb(fn, spic_dev.cur_ioport->io1.minimum);
2179 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2180 outb(v, spic_dev.cur_ioport->io1.minimum);
2181 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002182 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
2183 dev, fn, v, v1);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002184 return v1;
2185}
2186
Mattia Dongili3eb87492008-01-14 18:05:45 +09002187/*
2188 * minidrivers for SPIC models
2189 */
Mattia Dongilie93c8a62009-03-26 21:58:17 +09002190static int type3_handle_irq(const u8 data_mask, const u8 ev)
Mattia Dongili3eb87492008-01-14 18:05:45 +09002191{
2192 /*
2193 * 0x31 could mean we have to take some extra action and wait for
Mattia Dongilie93c8a62009-03-26 21:58:17 +09002194 * the next irq for some Type3 models, it will generate a new
Mattia Dongili3eb87492008-01-14 18:05:45 +09002195 * irq and we can read new data from the device:
2196 * - 0x5c and 0x5f requires 0xA0
2197 * - 0x61 requires 0xB3
2198 */
2199 if (data_mask == 0x31) {
2200 if (ev == 0x5c || ev == 0x5f)
2201 sony_pic_call1(0xA0);
2202 else if (ev == 0x61)
2203 sony_pic_call1(0xB3);
2204 return 0;
2205 }
2206 return 1;
2207}
2208
Mattia Dongili3eb87492008-01-14 18:05:45 +09002209static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
2210{
2211 struct pci_dev *pcidev;
2212
2213 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2214 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
2215 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002216 dev->model = SONYPI_DEVICE_TYPE1;
2217 dev->evport_offset = SONYPI_TYPE1_OFFSET;
2218 dev->event_types = type1_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002219 goto out;
2220 }
2221
2222 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2223 PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
2224 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002225 dev->model = SONYPI_DEVICE_TYPE2;
2226 dev->evport_offset = SONYPI_TYPE2_OFFSET;
2227 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002228 goto out;
2229 }
2230
2231 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2232 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
2233 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002234 dev->model = SONYPI_DEVICE_TYPE3;
2235 dev->handle_irq = type3_handle_irq;
2236 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2237 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002238 goto out;
2239 }
2240
2241 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2242 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
2243 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002244 dev->model = SONYPI_DEVICE_TYPE3;
2245 dev->handle_irq = type3_handle_irq;
2246 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2247 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002248 goto out;
2249 }
2250
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09002251 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2252 PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
2253 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002254 dev->model = SONYPI_DEVICE_TYPE3;
2255 dev->handle_irq = type3_handle_irq;
2256 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2257 dev->event_types = type3_events;
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09002258 goto out;
2259 }
2260
Mattia Dongili3eb87492008-01-14 18:05:45 +09002261 /* default */
Mattia Dongili31df7142009-09-16 00:05:29 +09002262 dev->model = SONYPI_DEVICE_TYPE2;
2263 dev->evport_offset = SONYPI_TYPE2_OFFSET;
2264 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002265
2266out:
2267 if (pcidev)
2268 pci_dev_put(pcidev);
2269
Joe Perches50f581a2011-03-29 15:21:48 -07002270 pr_info("detected Type%d model\n",
2271 dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
2272 dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
Mattia Dongili3eb87492008-01-14 18:05:45 +09002273}
2274
malattia@linux.it49a11de2007-04-09 19:28:56 +02002275/* camera tests and poweron/poweroff */
2276#define SONYPI_CAMERA_PICTURE 5
malattia@linux.it49a11de2007-04-09 19:28:56 +02002277#define SONYPI_CAMERA_CONTROL 0x10
malattia@linux.ite3646322007-04-28 23:34:22 +09002278
2279#define SONYPI_CAMERA_BRIGHTNESS 0
2280#define SONYPI_CAMERA_CONTRAST 1
2281#define SONYPI_CAMERA_HUE 2
2282#define SONYPI_CAMERA_COLOR 3
2283#define SONYPI_CAMERA_SHARPNESS 4
2284
2285#define SONYPI_CAMERA_EXPOSURE_MASK 0xC
2286#define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
2287#define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
2288#define SONYPI_CAMERA_MUTE_MASK 0x40
2289
2290/* the rest don't need a loop until not 0xff */
2291#define SONYPI_CAMERA_AGC 6
2292#define SONYPI_CAMERA_AGC_MASK 0x30
2293#define SONYPI_CAMERA_SHUTTER_MASK 0x7
2294
2295#define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
2296#define SONYPI_CAMERA_CONTROL 0x10
2297
2298#define SONYPI_CAMERA_STATUS 7
2299#define SONYPI_CAMERA_STATUS_READY 0x2
2300#define SONYPI_CAMERA_STATUS_POSITION 0x4
2301
2302#define SONYPI_DIRECTION_BACKWARDS 0x4
2303
2304#define SONYPI_CAMERA_REVISION 8
2305#define SONYPI_CAMERA_ROMVERSION 9
malattia@linux.it49a11de2007-04-09 19:28:56 +02002306
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002307static int __sony_pic_camera_ready(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002308{
2309 u8 v;
2310
2311 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
2312 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
2313}
2314
malattia@linux.ite3646322007-04-28 23:34:22 +09002315static int __sony_pic_camera_off(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002316{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002317 if (!camera) {
Joe Perches50f581a2011-03-29 15:21:48 -07002318 pr_warn("camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002319 return -ENODEV;
2320 }
2321
malattia@linux.it49a11de2007-04-09 19:28:56 +02002322 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
2323 SONYPI_CAMERA_MUTE_MASK),
2324 ITERATIONS_SHORT);
2325
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002326 if (spic_dev.camera_power) {
2327 sony_pic_call2(0x91, 0);
2328 spic_dev.camera_power = 0;
2329 }
2330 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002331}
2332
malattia@linux.ite3646322007-04-28 23:34:22 +09002333static int __sony_pic_camera_on(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002334{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002335 int i, j, x;
2336
2337 if (!camera) {
Joe Perches50f581a2011-03-29 15:21:48 -07002338 pr_warn("camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002339 return -ENODEV;
2340 }
malattia@linux.it49a11de2007-04-09 19:28:56 +02002341
2342 if (spic_dev.camera_power)
malattia@linux.ite3646322007-04-28 23:34:22 +09002343 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002344
2345 for (j = 5; j > 0; j--) {
2346
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002347 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002348 msleep(10);
2349 sony_pic_call1(0x93);
2350
2351 for (i = 400; i > 0; i--) {
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002352 if (__sony_pic_camera_ready())
malattia@linux.it49a11de2007-04-09 19:28:56 +02002353 break;
2354 msleep(10);
2355 }
2356 if (i)
2357 break;
2358 }
2359
2360 if (j == 0) {
Joe Perches50f581a2011-03-29 15:21:48 -07002361 pr_warn("failed to power on camera\n");
malattia@linux.ite3646322007-04-28 23:34:22 +09002362 return -ENODEV;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002363 }
2364
2365 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
2366 0x5a),
2367 ITERATIONS_SHORT);
2368
2369 spic_dev.camera_power = 1;
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002370 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002371}
2372
malattia@linux.ite3646322007-04-28 23:34:22 +09002373/* External camera command (exported to the motion eye v4l driver) */
2374int sony_pic_camera_command(int command, u8 value)
2375{
2376 if (!camera)
2377 return -EIO;
2378
2379 mutex_lock(&spic_dev.lock);
2380
2381 switch (command) {
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002382 case SONY_PIC_COMMAND_SETCAMERA:
malattia@linux.ite3646322007-04-28 23:34:22 +09002383 if (value)
2384 __sony_pic_camera_on();
2385 else
2386 __sony_pic_camera_off();
2387 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002388 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09002389 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
2390 ITERATIONS_SHORT);
2391 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002392 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
malattia@linux.ite3646322007-04-28 23:34:22 +09002393 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
2394 ITERATIONS_SHORT);
2395 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002396 case SONY_PIC_COMMAND_SETCAMERAHUE:
malattia@linux.ite3646322007-04-28 23:34:22 +09002397 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
2398 ITERATIONS_SHORT);
2399 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002400 case SONY_PIC_COMMAND_SETCAMERACOLOR:
malattia@linux.ite3646322007-04-28 23:34:22 +09002401 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
2402 ITERATIONS_SHORT);
2403 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002404 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09002405 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
2406 ITERATIONS_SHORT);
2407 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002408 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
malattia@linux.ite3646322007-04-28 23:34:22 +09002409 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
2410 ITERATIONS_SHORT);
2411 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002412 case SONY_PIC_COMMAND_SETCAMERAAGC:
malattia@linux.ite3646322007-04-28 23:34:22 +09002413 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
2414 ITERATIONS_SHORT);
2415 break;
2416 default:
Joe Perches50f581a2011-03-29 15:21:48 -07002417 pr_err("sony_pic_camera_command invalid: %d\n", command);
malattia@linux.ite3646322007-04-28 23:34:22 +09002418 break;
2419 }
2420 mutex_unlock(&spic_dev.lock);
2421 return 0;
2422}
2423EXPORT_SYMBOL(sony_pic_camera_command);
2424
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002425/* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002426static void __sony_pic_set_wwanpower(u8 state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002427{
2428 state = !!state;
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002429 if (spic_dev.wwan_power == state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002430 return;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002431 sony_pic_call2(0xB0, state);
Sergey Yanovich3ad1b762009-03-26 21:58:21 +09002432 sony_pic_call1(0x82);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002433 spic_dev.wwan_power = state;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002434}
2435
2436static ssize_t sony_pic_wwanpower_store(struct device *dev,
2437 struct device_attribute *attr,
2438 const char *buffer, size_t count)
2439{
2440 unsigned long value;
2441 if (count > 31)
2442 return -EINVAL;
2443
Mattia Dongili9e123372012-05-19 22:35:47 +09002444 if (kstrtoul(buffer, 10, &value))
2445 return -EINVAL;
2446
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002447 mutex_lock(&spic_dev.lock);
2448 __sony_pic_set_wwanpower(value);
2449 mutex_unlock(&spic_dev.lock);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002450
2451 return count;
2452}
2453
2454static ssize_t sony_pic_wwanpower_show(struct device *dev,
2455 struct device_attribute *attr, char *buffer)
2456{
2457 ssize_t count;
2458 mutex_lock(&spic_dev.lock);
2459 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
2460 mutex_unlock(&spic_dev.lock);
2461 return count;
2462}
2463
malattia@linux.it49a11de2007-04-09 19:28:56 +02002464/* bluetooth subsystem power state */
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002465static void __sony_pic_set_bluetoothpower(u8 state)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002466{
2467 state = !!state;
2468 if (spic_dev.bluetooth_power == state)
2469 return;
2470 sony_pic_call2(0x96, state);
2471 sony_pic_call1(0x82);
2472 spic_dev.bluetooth_power = state;
2473}
2474
2475static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
2476 struct device_attribute *attr,
2477 const char *buffer, size_t count)
2478{
2479 unsigned long value;
2480 if (count > 31)
2481 return -EINVAL;
2482
Mattia Dongili9e123372012-05-19 22:35:47 +09002483 if (kstrtoul(buffer, 10, &value))
2484 return -EINVAL;
2485
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002486 mutex_lock(&spic_dev.lock);
2487 __sony_pic_set_bluetoothpower(value);
2488 mutex_unlock(&spic_dev.lock);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002489
2490 return count;
2491}
2492
2493static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
2494 struct device_attribute *attr, char *buffer)
2495{
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002496 ssize_t count = 0;
2497 mutex_lock(&spic_dev.lock);
2498 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
2499 mutex_unlock(&spic_dev.lock);
2500 return count;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002501}
2502
2503/* fan speed */
2504/* FAN0 information (reverse engineered from ACPI tables) */
2505#define SONY_PIC_FAN0_STATUS 0x93
malattia@linux.it7b153f32007-04-09 19:31:25 +02002506static int sony_pic_set_fanspeed(unsigned long value)
2507{
2508 return ec_write(SONY_PIC_FAN0_STATUS, value);
2509}
2510
2511static int sony_pic_get_fanspeed(u8 *value)
2512{
2513 return ec_read(SONY_PIC_FAN0_STATUS, value);
2514}
2515
malattia@linux.it49a11de2007-04-09 19:28:56 +02002516static ssize_t sony_pic_fanspeed_store(struct device *dev,
2517 struct device_attribute *attr,
2518 const char *buffer, size_t count)
2519{
2520 unsigned long value;
2521 if (count > 31)
2522 return -EINVAL;
2523
Mattia Dongili9e123372012-05-19 22:35:47 +09002524 if (kstrtoul(buffer, 10, &value))
2525 return -EINVAL;
2526
malattia@linux.it7b153f32007-04-09 19:31:25 +02002527 if (sony_pic_set_fanspeed(value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02002528 return -EIO;
2529
2530 return count;
2531}
2532
2533static ssize_t sony_pic_fanspeed_show(struct device *dev,
2534 struct device_attribute *attr, char *buffer)
2535{
2536 u8 value = 0;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002537 if (sony_pic_get_fanspeed(&value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02002538 return -EIO;
2539
2540 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
2541}
2542
2543#define SPIC_ATTR(_name, _mode) \
2544struct device_attribute spic_attr_##_name = __ATTR(_name, \
2545 _mode, sony_pic_## _name ##_show, \
2546 sony_pic_## _name ##_store)
2547
malattia@linux.it49a11de2007-04-09 19:28:56 +02002548static SPIC_ATTR(bluetoothpower, 0644);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002549static SPIC_ATTR(wwanpower, 0644);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002550static SPIC_ATTR(fanspeed, 0644);
2551
2552static struct attribute *spic_attributes[] = {
malattia@linux.it49a11de2007-04-09 19:28:56 +02002553 &spic_attr_bluetoothpower.attr,
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002554 &spic_attr_wwanpower.attr,
malattia@linux.it49a11de2007-04-09 19:28:56 +02002555 &spic_attr_fanspeed.attr,
2556 NULL
2557};
2558
2559static struct attribute_group spic_attribute_group = {
2560 .attrs = spic_attributes
2561};
2562
malattia@linux.it7b153f32007-04-09 19:31:25 +02002563/******** SONYPI compatibility **********/
Mattia Dongilia64e62a2007-05-01 11:19:53 +09002564#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +02002565
2566/* battery / brightness / temperature addresses */
2567#define SONYPI_BAT_FLAGS 0x81
2568#define SONYPI_LCD_LIGHT 0x96
2569#define SONYPI_BAT1_PCTRM 0xa0
2570#define SONYPI_BAT1_LEFT 0xa2
2571#define SONYPI_BAT1_MAXRT 0xa4
2572#define SONYPI_BAT2_PCTRM 0xa8
2573#define SONYPI_BAT2_LEFT 0xaa
2574#define SONYPI_BAT2_MAXRT 0xac
2575#define SONYPI_BAT1_MAXTK 0xb0
2576#define SONYPI_BAT1_FULL 0xb2
2577#define SONYPI_BAT2_MAXTK 0xb8
2578#define SONYPI_BAT2_FULL 0xba
2579#define SONYPI_TEMP_STATUS 0xC1
2580
2581struct sonypi_compat_s {
2582 struct fasync_struct *fifo_async;
Stefani Seibold45465482009-12-21 14:37:26 -08002583 struct kfifo fifo;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002584 spinlock_t fifo_lock;
2585 wait_queue_head_t fifo_proc_list;
2586 atomic_t open_count;
2587};
2588static struct sonypi_compat_s sonypi_compat = {
2589 .open_count = ATOMIC_INIT(0),
2590};
2591
2592static int sonypi_misc_fasync(int fd, struct file *filp, int on)
2593{
Jonathan Corbet60aa4922009-02-01 14:52:56 -07002594 return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002595}
2596
2597static int sonypi_misc_release(struct inode *inode, struct file *file)
2598{
malattia@linux.it7b153f32007-04-09 19:31:25 +02002599 atomic_dec(&sonypi_compat.open_count);
2600 return 0;
2601}
2602
2603static int sonypi_misc_open(struct inode *inode, struct file *file)
2604{
2605 /* Flush input queue on first open */
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002606 unsigned long flags;
2607
Stefani Seibold45465482009-12-21 14:37:26 -08002608 spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002609
malattia@linux.it7b153f32007-04-09 19:31:25 +02002610 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
Stefani Seibolde64c0262009-12-21 14:37:28 -08002611 kfifo_reset(&sonypi_compat.fifo);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002612
Stefani Seibold45465482009-12-21 14:37:26 -08002613 spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002614
malattia@linux.it7b153f32007-04-09 19:31:25 +02002615 return 0;
2616}
2617
2618static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
2619 size_t count, loff_t *pos)
2620{
2621 ssize_t ret;
2622 unsigned char c;
2623
Stefani Seibold45465482009-12-21 14:37:26 -08002624 if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
malattia@linux.it7b153f32007-04-09 19:31:25 +02002625 (file->f_flags & O_NONBLOCK))
2626 return -EAGAIN;
2627
2628 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
Stefani Seibold45465482009-12-21 14:37:26 -08002629 kfifo_len(&sonypi_compat.fifo) != 0);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002630 if (ret)
2631 return ret;
2632
2633 while (ret < count &&
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002634 (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002635 &sonypi_compat.fifo_lock) == sizeof(c))) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02002636 if (put_user(c, buf++))
2637 return -EFAULT;
2638 ret++;
2639 }
2640
2641 if (ret > 0) {
2642 struct inode *inode = file->f_path.dentry->d_inode;
2643 inode->i_atime = current_fs_time(inode->i_sb);
2644 }
2645
2646 return ret;
2647}
2648
2649static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
2650{
2651 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
Stefani Seibold45465482009-12-21 14:37:26 -08002652 if (kfifo_len(&sonypi_compat.fifo))
malattia@linux.it7b153f32007-04-09 19:31:25 +02002653 return POLLIN | POLLRDNORM;
2654 return 0;
2655}
2656
2657static int ec_read16(u8 addr, u16 *value)
2658{
2659 u8 val_lb, val_hb;
2660 if (ec_read(addr, &val_lb))
2661 return -1;
2662 if (ec_read(addr + 1, &val_hb))
2663 return -1;
2664 *value = val_lb | (val_hb << 8);
2665 return 0;
2666}
2667
Alan Cox2b24ef02009-03-26 21:58:19 +09002668static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
2669 unsigned long arg)
malattia@linux.it7b153f32007-04-09 19:31:25 +02002670{
2671 int ret = 0;
2672 void __user *argp = (void __user *)arg;
2673 u8 val8;
2674 u16 val16;
2675 int value;
2676
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002677 mutex_lock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002678 switch (cmd) {
2679 case SONYPI_IOCGBRT:
Mattia Dongili62d2f232011-05-09 10:20:29 -04002680 if (sony_bl_props.dev == NULL) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02002681 ret = -EIO;
2682 break;
2683 }
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002684 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL,
2685 &value)) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02002686 ret = -EIO;
2687 break;
2688 }
2689 val8 = ((value & 0xff) - 1) << 5;
2690 if (copy_to_user(argp, &val8, sizeof(val8)))
2691 ret = -EFAULT;
2692 break;
2693 case SONYPI_IOCSBRT:
Mattia Dongili62d2f232011-05-09 10:20:29 -04002694 if (sony_bl_props.dev == NULL) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02002695 ret = -EIO;
2696 break;
2697 }
2698 if (copy_from_user(&val8, argp, sizeof(val8))) {
2699 ret = -EFAULT;
2700 break;
2701 }
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002702 value = (val8 >> 5) + 1;
2703 if (sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &value,
2704 NULL)) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02002705 ret = -EIO;
2706 break;
2707 }
2708 /* sync the backlight device status */
Mattia Dongili62d2f232011-05-09 10:20:29 -04002709 sony_bl_props.dev->props.brightness =
2710 sony_backlight_get_brightness(sony_bl_props.dev);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002711 break;
2712 case SONYPI_IOCGBAT1CAP:
2713 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
2714 ret = -EIO;
2715 break;
2716 }
2717 if (copy_to_user(argp, &val16, sizeof(val16)))
2718 ret = -EFAULT;
2719 break;
2720 case SONYPI_IOCGBAT1REM:
2721 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
2722 ret = -EIO;
2723 break;
2724 }
2725 if (copy_to_user(argp, &val16, sizeof(val16)))
2726 ret = -EFAULT;
2727 break;
2728 case SONYPI_IOCGBAT2CAP:
2729 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
2730 ret = -EIO;
2731 break;
2732 }
2733 if (copy_to_user(argp, &val16, sizeof(val16)))
2734 ret = -EFAULT;
2735 break;
2736 case SONYPI_IOCGBAT2REM:
2737 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
2738 ret = -EIO;
2739 break;
2740 }
2741 if (copy_to_user(argp, &val16, sizeof(val16)))
2742 ret = -EFAULT;
2743 break;
2744 case SONYPI_IOCGBATFLAGS:
2745 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
2746 ret = -EIO;
2747 break;
2748 }
2749 val8 &= 0x07;
2750 if (copy_to_user(argp, &val8, sizeof(val8)))
2751 ret = -EFAULT;
2752 break;
2753 case SONYPI_IOCGBLUE:
2754 val8 = spic_dev.bluetooth_power;
2755 if (copy_to_user(argp, &val8, sizeof(val8)))
2756 ret = -EFAULT;
2757 break;
2758 case SONYPI_IOCSBLUE:
2759 if (copy_from_user(&val8, argp, sizeof(val8))) {
2760 ret = -EFAULT;
2761 break;
2762 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002763 __sony_pic_set_bluetoothpower(val8);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002764 break;
2765 /* FAN Controls */
2766 case SONYPI_IOCGFAN:
2767 if (sony_pic_get_fanspeed(&val8)) {
2768 ret = -EIO;
2769 break;
2770 }
2771 if (copy_to_user(argp, &val8, sizeof(val8)))
2772 ret = -EFAULT;
2773 break;
2774 case SONYPI_IOCSFAN:
2775 if (copy_from_user(&val8, argp, sizeof(val8))) {
2776 ret = -EFAULT;
2777 break;
2778 }
2779 if (sony_pic_set_fanspeed(val8))
2780 ret = -EIO;
2781 break;
2782 /* GET Temperature (useful under APM) */
2783 case SONYPI_IOCGTEMP:
2784 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
2785 ret = -EIO;
2786 break;
2787 }
2788 if (copy_to_user(argp, &val8, sizeof(val8)))
2789 ret = -EFAULT;
2790 break;
2791 default:
2792 ret = -EINVAL;
2793 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002794 mutex_unlock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002795 return ret;
2796}
2797
2798static const struct file_operations sonypi_misc_fops = {
2799 .owner = THIS_MODULE,
2800 .read = sonypi_misc_read,
2801 .poll = sonypi_misc_poll,
2802 .open = sonypi_misc_open,
2803 .release = sonypi_misc_release,
2804 .fasync = sonypi_misc_fasync,
Alan Cox2b24ef02009-03-26 21:58:19 +09002805 .unlocked_ioctl = sonypi_misc_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002806 .llseek = noop_llseek,
malattia@linux.it7b153f32007-04-09 19:31:25 +02002807};
2808
2809static struct miscdevice sonypi_misc_device = {
2810 .minor = MISC_DYNAMIC_MINOR,
2811 .name = "sonypi",
2812 .fops = &sonypi_misc_fops,
2813};
2814
2815static void sonypi_compat_report_event(u8 event)
2816{
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002817 kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002818 sizeof(event), &sonypi_compat.fifo_lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002819 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
2820 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
2821}
2822
2823static int sonypi_compat_init(void)
2824{
2825 int error;
2826
2827 spin_lock_init(&sonypi_compat.fifo_lock);
Stefani Seibold45465482009-12-21 14:37:26 -08002828 error =
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002829 kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -08002830 if (error) {
Joe Perches50f581a2011-03-29 15:21:48 -07002831 pr_err("kfifo_alloc failed\n");
Stefani Seibold45465482009-12-21 14:37:26 -08002832 return error;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002833 }
2834
2835 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002836
2837 if (minor != -1)
2838 sonypi_misc_device.minor = minor;
2839 error = misc_register(&sonypi_misc_device);
2840 if (error) {
Joe Perches50f581a2011-03-29 15:21:48 -07002841 pr_err("misc_register failed\n");
malattia@linux.it7b153f32007-04-09 19:31:25 +02002842 goto err_free_kfifo;
2843 }
2844 if (minor == -1)
Joe Perches50f581a2011-03-29 15:21:48 -07002845 pr_info("device allocated minor is %d\n",
2846 sonypi_misc_device.minor);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002847
2848 return 0;
2849
2850err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -08002851 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002852 return error;
2853}
2854
2855static void sonypi_compat_exit(void)
2856{
2857 misc_deregister(&sonypi_misc_device);
Stefani Seibold45465482009-12-21 14:37:26 -08002858 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002859}
2860#else
2861static int sonypi_compat_init(void) { return 0; }
2862static void sonypi_compat_exit(void) { }
2863static void sonypi_compat_report_event(u8 event) { }
Mattia Dongilia64e62a2007-05-01 11:19:53 +09002864#endif /* CONFIG_SONYPI_COMPAT */
malattia@linux.it7b153f32007-04-09 19:31:25 +02002865
malattia@linux.it1549ee62007-04-09 10:19:08 +02002866/*
malattia@linux.it33a04452007-04-09 19:26:03 +02002867 * ACPI callbacks
malattia@linux.it1549ee62007-04-09 10:19:08 +02002868 */
malattia@linux.it33a04452007-04-09 19:26:03 +02002869static acpi_status
2870sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
2871{
2872 u32 i;
2873 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
2874
2875 switch (resource->type) {
2876 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002877 {
2878 /* start IO enumeration */
2879 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
2880 if (!ioport)
2881 return AE_ERROR;
2882
2883 list_add(&ioport->list, &dev->ioports);
2884 return AE_OK;
2885 }
2886
malattia@linux.it33a04452007-04-09 19:26:03 +02002887 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002888 /* end IO enumeration */
malattia@linux.it33a04452007-04-09 19:26:03 +02002889 return AE_OK;
2890
2891 case ACPI_RESOURCE_TYPE_IRQ:
2892 {
2893 struct acpi_resource_irq *p = &resource->data.irq;
2894 struct sony_pic_irq *interrupt = NULL;
2895 if (!p || !p->interrupt_count) {
2896 /*
2897 * IRQ descriptors may have no IRQ# bits set,
2898 * particularly those those w/ _STA disabled
2899 */
2900 dprintk("Blank IRQ resource\n");
2901 return AE_OK;
2902 }
2903 for (i = 0; i < p->interrupt_count; i++) {
2904 if (!p->interrupts[i]) {
Joe Perches50f581a2011-03-29 15:21:48 -07002905 pr_warn("Invalid IRQ %d\n",
2906 p->interrupts[i]);
malattia@linux.it33a04452007-04-09 19:26:03 +02002907 continue;
2908 }
2909 interrupt = kzalloc(sizeof(*interrupt),
2910 GFP_KERNEL);
2911 if (!interrupt)
2912 return AE_ERROR;
2913
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002914 list_add(&interrupt->list, &dev->interrupts);
malattia@linux.it33a04452007-04-09 19:26:03 +02002915 interrupt->irq.triggering = p->triggering;
2916 interrupt->irq.polarity = p->polarity;
2917 interrupt->irq.sharable = p->sharable;
2918 interrupt->irq.interrupt_count = 1;
2919 interrupt->irq.interrupts[0] = p->interrupts[i];
2920 }
2921 return AE_OK;
2922 }
2923 case ACPI_RESOURCE_TYPE_IO:
2924 {
2925 struct acpi_resource_io *io = &resource->data.io;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002926 struct sony_pic_ioport *ioport =
2927 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
malattia@linux.it33a04452007-04-09 19:26:03 +02002928 if (!io) {
2929 dprintk("Blank IO resource\n");
2930 return AE_OK;
2931 }
2932
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002933 if (!ioport->io1.minimum) {
2934 memcpy(&ioport->io1, io, sizeof(*io));
2935 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
2936 ioport->io1.address_length);
2937 }
2938 else if (!ioport->io2.minimum) {
2939 memcpy(&ioport->io2, io, sizeof(*io));
2940 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
2941 ioport->io2.address_length);
2942 }
2943 else {
Joe Perches50f581a2011-03-29 15:21:48 -07002944 pr_err("Unknown SPIC Type, more than 2 IO Ports\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002945 return AE_ERROR;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002946 }
malattia@linux.it33a04452007-04-09 19:26:03 +02002947 return AE_OK;
2948 }
2949 default:
2950 dprintk("Resource %d isn't an IRQ nor an IO port\n",
Joe Perches50f581a2011-03-29 15:21:48 -07002951 resource->type);
malattia@linux.it33a04452007-04-09 19:26:03 +02002952
2953 case ACPI_RESOURCE_TYPE_END_TAG:
2954 return AE_OK;
2955 }
2956 return AE_CTRL_TERMINATE;
2957}
2958
2959static int sony_pic_possible_resources(struct acpi_device *device)
2960{
2961 int result = 0;
2962 acpi_status status = AE_OK;
2963
2964 if (!device)
2965 return -EINVAL;
2966
2967 /* get device status */
2968 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
2969 dprintk("Evaluating _STA\n");
2970 result = acpi_bus_get_status(device);
2971 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07002972 pr_warn("Unable to read status\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002973 goto end;
2974 }
2975
2976 if (!device->status.enabled)
2977 dprintk("Device disabled\n");
2978 else
2979 dprintk("Device enabled\n");
2980
2981 /*
2982 * Query and parse 'method'
2983 */
2984 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
2985 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
2986 sony_pic_read_possible_resource, &spic_dev);
2987 if (ACPI_FAILURE(status)) {
Joe Perches50f581a2011-03-29 15:21:48 -07002988 pr_warn("Failure evaluating %s\n", METHOD_NAME__PRS);
malattia@linux.it33a04452007-04-09 19:26:03 +02002989 result = -ENODEV;
2990 }
2991end:
2992 return result;
2993}
2994
2995/*
2996 * Disable the spic device by calling its _DIS method
2997 */
2998static int sony_pic_disable(struct acpi_device *device)
2999{
Matthew Garrett6158d3a2008-10-29 14:01:03 -07003000 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
3001 NULL);
3002
3003 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
malattia@linux.it33a04452007-04-09 19:26:03 +02003004 return -ENXIO;
3005
3006 dprintk("Device disabled\n");
3007 return 0;
3008}
3009
3010
3011/*
3012 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
3013 *
3014 * Call _SRS to set current resources
3015 */
3016static int sony_pic_enable(struct acpi_device *device,
3017 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
3018{
3019 acpi_status status;
3020 int result = 0;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003021 /* Type 1 resource layout is:
3022 * IO
3023 * IO
3024 * IRQNoFlags
3025 * End
3026 *
3027 * Type 2 and 3 resource layout is:
3028 * IO
3029 * IRQNoFlags
3030 * End
3031 */
malattia@linux.it33a04452007-04-09 19:26:03 +02003032 struct {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003033 struct acpi_resource res1;
3034 struct acpi_resource res2;
3035 struct acpi_resource res3;
3036 struct acpi_resource res4;
malattia@linux.it33a04452007-04-09 19:26:03 +02003037 } *resource;
3038 struct acpi_buffer buffer = { 0, NULL };
3039
3040 if (!ioport || !irq)
3041 return -EINVAL;
3042
3043 /* init acpi_buffer */
3044 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
3045 if (!resource)
3046 return -ENOMEM;
3047
3048 buffer.length = sizeof(*resource) + 1;
3049 buffer.pointer = resource;
3050
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003051 /* setup Type 1 resources */
Mattia Dongili31df7142009-09-16 00:05:29 +09003052 if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003053
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003054 /* setup io resources */
3055 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
3056 resource->res1.length = sizeof(struct acpi_resource);
3057 memcpy(&resource->res1.data.io, &ioport->io1,
3058 sizeof(struct acpi_resource_io));
malattia@linux.it33a04452007-04-09 19:26:03 +02003059
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003060 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
3061 resource->res2.length = sizeof(struct acpi_resource);
3062 memcpy(&resource->res2.data.io, &ioport->io2,
3063 sizeof(struct acpi_resource_io));
3064
3065 /* setup irq resource */
3066 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
3067 resource->res3.length = sizeof(struct acpi_resource);
3068 memcpy(&resource->res3.data.irq, &irq->irq,
3069 sizeof(struct acpi_resource_irq));
3070 /* we requested a shared irq */
3071 resource->res3.data.irq.sharable = ACPI_SHARED;
3072
3073 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
3074
3075 }
3076 /* setup Type 2/3 resources */
3077 else {
3078 /* setup io resource */
3079 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
3080 resource->res1.length = sizeof(struct acpi_resource);
3081 memcpy(&resource->res1.data.io, &ioport->io1,
3082 sizeof(struct acpi_resource_io));
3083
3084 /* setup irq resource */
3085 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
3086 resource->res2.length = sizeof(struct acpi_resource);
3087 memcpy(&resource->res2.data.irq, &irq->irq,
3088 sizeof(struct acpi_resource_irq));
3089 /* we requested a shared irq */
3090 resource->res2.data.irq.sharable = ACPI_SHARED;
3091
3092 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
3093 }
malattia@linux.it33a04452007-04-09 19:26:03 +02003094
3095 /* Attempt to set the resource */
3096 dprintk("Evaluating _SRS\n");
3097 status = acpi_set_current_resources(device->handle, &buffer);
3098
3099 /* check for total failure */
3100 if (ACPI_FAILURE(status)) {
Joe Perches50f581a2011-03-29 15:21:48 -07003101 pr_err("Error evaluating _SRS\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003102 result = -ENODEV;
3103 goto end;
3104 }
3105
3106 /* Necessary device initializations calls (from sonypi) */
3107 sony_pic_call1(0x82);
3108 sony_pic_call2(0x81, 0xff);
3109 sony_pic_call1(compat ? 0x92 : 0x82);
3110
3111end:
3112 kfree(resource);
3113 return result;
3114}
3115
3116/*****************
3117 *
3118 * ISR: some event is available
3119 *
3120 *****************/
3121static irqreturn_t sony_pic_irq(int irq, void *dev_id)
3122{
3123 int i, j;
malattia@linux.it33a04452007-04-09 19:26:03 +02003124 u8 ev = 0;
3125 u8 data_mask = 0;
3126 u8 device_event = 0;
3127
3128 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
3129
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003130 ev = inb_p(dev->cur_ioport->io1.minimum);
3131 if (dev->cur_ioport->io2.minimum)
3132 data_mask = inb_p(dev->cur_ioport->io2.minimum);
3133 else
Mattia Dongilide920432008-01-14 18:05:43 +09003134 data_mask = inb_p(dev->cur_ioport->io1.minimum +
Mattia Dongili31df7142009-09-16 00:05:29 +09003135 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003136
Mattia Dongili22a17782007-07-16 02:34:39 +09003137 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
Mattia Dongilide920432008-01-14 18:05:43 +09003138 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09003139 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003140
3141 if (ev == 0x00 || ev == 0xff)
3142 return IRQ_HANDLED;
3143
Mattia Dongili31df7142009-09-16 00:05:29 +09003144 for (i = 0; dev->event_types[i].mask; i++) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003145
Mattia Dongili31df7142009-09-16 00:05:29 +09003146 if ((data_mask & dev->event_types[i].data) !=
3147 dev->event_types[i].data)
malattia@linux.it33a04452007-04-09 19:26:03 +02003148 continue;
3149
Mattia Dongili31df7142009-09-16 00:05:29 +09003150 if (!(mask & dev->event_types[i].mask))
malattia@linux.it33a04452007-04-09 19:26:03 +02003151 continue;
3152
Mattia Dongili31df7142009-09-16 00:05:29 +09003153 for (j = 0; dev->event_types[i].events[j].event; j++) {
3154 if (ev == dev->event_types[i].events[j].data) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003155 device_event =
Mattia Dongili31df7142009-09-16 00:05:29 +09003156 dev->event_types[i].events[j].event;
Mattia Dongili4eeb5022011-02-19 11:52:27 +09003157 /* some events may require ignoring */
3158 if (!device_event)
3159 return IRQ_HANDLED;
malattia@linux.it33a04452007-04-09 19:26:03 +02003160 goto found;
3161 }
3162 }
3163 }
Mattia Dongili3eb87492008-01-14 18:05:45 +09003164 /* Still not able to decode the event try to pass
3165 * it over to the minidriver
3166 */
Mattia Dongili31df7142009-09-16 00:05:29 +09003167 if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
Mattia Dongili3eb87492008-01-14 18:05:45 +09003168 return IRQ_HANDLED;
3169
Mattia Dongilide920432008-01-14 18:05:43 +09003170 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
3171 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09003172 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003173 return IRQ_HANDLED;
3174
3175found:
malattia@linux.it1549ee62007-04-09 10:19:08 +02003176 sony_laptop_report_input_event(device_event);
Mattia Dongilide920432008-01-14 18:05:43 +09003177 acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003178 sonypi_compat_report_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02003179 return IRQ_HANDLED;
3180}
3181
3182/*****************
3183 *
3184 * ACPI driver
3185 *
3186 *****************/
3187static int sony_pic_remove(struct acpi_device *device, int type)
3188{
3189 struct sony_pic_ioport *io, *tmp_io;
3190 struct sony_pic_irq *irq, *tmp_irq;
3191
3192 if (sony_pic_disable(device)) {
Joe Perches50f581a2011-03-29 15:21:48 -07003193 pr_err("Couldn't disable device\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003194 return -ENXIO;
3195 }
3196
3197 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003198 release_region(spic_dev.cur_ioport->io1.minimum,
3199 spic_dev.cur_ioport->io1.address_length);
3200 if (spic_dev.cur_ioport->io2.minimum)
3201 release_region(spic_dev.cur_ioport->io2.minimum,
3202 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02003203
Mattia Dongili015a9162007-08-12 16:20:27 +09003204 sonypi_compat_exit();
3205
malattia@linux.it1549ee62007-04-09 10:19:08 +02003206 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02003207
malattia@linux.it49a11de2007-04-09 19:28:56 +02003208 /* pf attrs */
3209 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
3210 sony_pf_remove();
3211
malattia@linux.it33a04452007-04-09 19:26:03 +02003212 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
3213 list_del(&io->list);
3214 kfree(io);
3215 }
3216 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
3217 list_del(&irq->list);
3218 kfree(irq);
3219 }
3220 spic_dev.cur_ioport = NULL;
3221 spic_dev.cur_irq = NULL;
3222
malattia@linux.itf6119b02007-04-09 19:31:06 +02003223 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003224 return 0;
3225}
3226
3227static int sony_pic_add(struct acpi_device *device)
3228{
3229 int result;
3230 struct sony_pic_ioport *io, *tmp_io;
3231 struct sony_pic_irq *irq, *tmp_irq;
3232
Joe Perches50f581a2011-03-29 15:21:48 -07003233 pr_info("%s v%s\n", SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.it33a04452007-04-09 19:26:03 +02003234
3235 spic_dev.acpi_dev = device;
3236 strcpy(acpi_device_class(device), "sony/hotkey");
Mattia Dongilide920432008-01-14 18:05:43 +09003237 sony_pic_detect_device_type(&spic_dev);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003238 mutex_init(&spic_dev.lock);
malattia@linux.it33a04452007-04-09 19:26:03 +02003239
3240 /* read _PRS resources */
3241 result = sony_pic_possible_resources(device);
3242 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07003243 pr_err("Unable to read possible resources\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003244 goto err_free_resources;
3245 }
3246
3247 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05003248 result = sony_laptop_setup_input(device);
malattia@linux.it33a04452007-04-09 19:26:03 +02003249 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07003250 pr_err("Unable to create input devices\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003251 goto err_free_resources;
3252 }
3253
Mattia Dongili015a9162007-08-12 16:20:27 +09003254 if (sonypi_compat_init())
3255 goto err_remove_input;
3256
malattia@linux.it33a04452007-04-09 19:26:03 +02003257 /* request io port */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003258 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
3259 if (request_region(io->io1.minimum, io->io1.address_length,
Lucas De Marchic8440332011-03-17 17:18:22 -03003260 "Sony Programmable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003261 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
3262 io->io1.minimum, io->io1.maximum,
3263 io->io1.address_length);
3264 /* Type 1 have 2 ioports */
3265 if (io->io2.minimum) {
3266 if (request_region(io->io2.minimum,
3267 io->io2.address_length,
Lucas De Marchic8440332011-03-17 17:18:22 -03003268 "Sony Programmable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003269 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
3270 io->io2.minimum, io->io2.maximum,
3271 io->io2.address_length);
3272 spic_dev.cur_ioport = io;
3273 break;
3274 }
3275 else {
3276 dprintk("Unable to get I/O port2: "
3277 "0x%.4x (0x%.4x) + 0x%.2x\n",
3278 io->io2.minimum, io->io2.maximum,
3279 io->io2.address_length);
3280 release_region(io->io1.minimum,
3281 io->io1.address_length);
3282 }
3283 }
3284 else {
3285 spic_dev.cur_ioport = io;
3286 break;
3287 }
malattia@linux.it33a04452007-04-09 19:26:03 +02003288 }
3289 }
3290 if (!spic_dev.cur_ioport) {
Joe Perches50f581a2011-03-29 15:21:48 -07003291 pr_err("Failed to request_region\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003292 result = -ENODEV;
Mattia Dongili015a9162007-08-12 16:20:27 +09003293 goto err_remove_compat;
malattia@linux.it33a04452007-04-09 19:26:03 +02003294 }
3295
3296 /* request IRQ */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003297 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003298 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
Yong Zhang6a0d89c2011-10-22 17:56:50 +08003299 0, "sony-laptop", &spic_dev)) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003300 dprintk("IRQ: %d - triggering: %d - "
3301 "polarity: %d - shr: %d\n",
3302 irq->irq.interrupts[0],
3303 irq->irq.triggering,
3304 irq->irq.polarity,
3305 irq->irq.sharable);
3306 spic_dev.cur_irq = irq;
3307 break;
3308 }
3309 }
3310 if (!spic_dev.cur_irq) {
Joe Perches50f581a2011-03-29 15:21:48 -07003311 pr_err("Failed to request_irq\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003312 result = -ENODEV;
3313 goto err_release_region;
3314 }
3315
3316 /* set resource status _SRS */
3317 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
3318 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07003319 pr_err("Couldn't enable device\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003320 goto err_free_irq;
3321 }
3322
malattia@linux.it49a11de2007-04-09 19:28:56 +02003323 spic_dev.bluetooth_power = -1;
3324 /* create device attributes */
3325 result = sony_pf_add();
3326 if (result)
3327 goto err_disable_device;
3328
3329 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
3330 if (result)
3331 goto err_remove_pf;
3332
malattia@linux.it33a04452007-04-09 19:26:03 +02003333 return 0;
3334
malattia@linux.it49a11de2007-04-09 19:28:56 +02003335err_remove_pf:
3336 sony_pf_remove();
3337
3338err_disable_device:
3339 sony_pic_disable(device);
3340
malattia@linux.it33a04452007-04-09 19:26:03 +02003341err_free_irq:
3342 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
3343
3344err_release_region:
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 +09003351err_remove_compat:
3352 sonypi_compat_exit();
3353
malattia@linux.it33a04452007-04-09 19:26:03 +02003354err_remove_input:
malattia@linux.it1549ee62007-04-09 10:19:08 +02003355 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02003356
3357err_free_resources:
3358 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
3359 list_del(&io->list);
3360 kfree(io);
3361 }
3362 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
3363 list_del(&irq->list);
3364 kfree(irq);
3365 }
3366 spic_dev.cur_ioport = NULL;
3367 spic_dev.cur_irq = NULL;
3368
3369 return result;
3370}
3371
3372static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
3373{
3374 if (sony_pic_disable(device))
3375 return -ENXIO;
3376 return 0;
3377}
3378
3379static int sony_pic_resume(struct acpi_device *device)
3380{
3381 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
3382 return 0;
3383}
3384
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003385static const struct acpi_device_id sony_pic_device_ids[] = {
3386 {SONY_PIC_HID, 0},
3387 {"", 0},
3388};
3389
malattia@linux.it33a04452007-04-09 19:26:03 +02003390static struct acpi_driver sony_pic_driver = {
3391 .name = SONY_PIC_DRIVER_NAME,
3392 .class = SONY_PIC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003393 .ids = sony_pic_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02003394 .owner = THIS_MODULE,
3395 .ops = {
3396 .add = sony_pic_add,
3397 .remove = sony_pic_remove,
3398 .suspend = sony_pic_suspend,
3399 .resume = sony_pic_resume,
3400 },
3401};
3402
3403static struct dmi_system_id __initdata sonypi_dmi_table[] = {
3404 {
3405 .ident = "Sony Vaio",
3406 .matches = {
3407 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
3408 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
3409 },
3410 },
3411 {
3412 .ident = "Sony Vaio",
3413 .matches = {
3414 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
3415 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
3416 },
3417 },
3418 { }
3419};
3420
malattia@linux.it59b19102007-04-09 10:19:04 +02003421static int __init sony_laptop_init(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01003422{
malattia@linux.it33a04452007-04-09 19:26:03 +02003423 int result;
3424
3425 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
3426 result = acpi_bus_register_driver(&sony_pic_driver);
3427 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07003428 pr_err("Unable to register SPIC driver\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003429 goto out;
3430 }
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003431 spic_drv_registered = 1;
malattia@linux.it33a04452007-04-09 19:26:03 +02003432 }
3433
3434 result = acpi_bus_register_driver(&sony_nc_driver);
3435 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07003436 pr_err("Unable to register SNC driver\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003437 goto out_unregister_pic;
3438 }
3439
3440 return 0;
3441
3442out_unregister_pic:
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003443 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02003444 acpi_bus_unregister_driver(&sony_pic_driver);
3445out:
3446 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +01003447}
3448
malattia@linux.it59b19102007-04-09 10:19:04 +02003449static void __exit sony_laptop_exit(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01003450{
malattia@linux.it59b19102007-04-09 10:19:04 +02003451 acpi_bus_unregister_driver(&sony_nc_driver);
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003452 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02003453 acpi_bus_unregister_driver(&sony_pic_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +01003454}
3455
malattia@linux.it59b19102007-04-09 10:19:04 +02003456module_init(sony_laptop_init);
3457module_exit(sony_laptop_exit);