blob: 6fe8cd6e23b5f89fd1efc1145796a9e6c530aa8e [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
45#include <linux/kernel.h>
46#include <linux/module.h>
47#include <linux/moduleparam.h>
48#include <linux/init.h>
49#include <linux/types.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010050#include <linux/backlight.h>
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010051#include <linux/platform_device.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010052#include <linux/err.h>
malattia@linux.it33a04452007-04-09 19:26:03 +020053#include <linux/dmi.h>
54#include <linux/pci.h>
55#include <linux/interrupt.h>
56#include <linux/delay.h>
57#include <linux/input.h>
58#include <linux/kfifo.h>
59#include <linux/workqueue.h>
60#include <linux/acpi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090061#include <linux/slab.h>
Stelian Pop7f09c432007-01-13 23:04:31 +010062#include <acpi/acpi_drivers.h>
63#include <acpi/acpi_bus.h>
64#include <asm/uaccess.h>
malattia@linux.it33a04452007-04-09 19:26:03 +020065#include <linux/sonypi.h>
malattia@linux.it1ce82c12007-04-28 23:34:36 +090066#include <linux/sony-laptop.h>
Matthew Garrett6cc056b2009-03-26 21:58:15 +090067#include <linux/rfkill.h>
Mattia Dongilia64e62a2007-05-01 11:19:53 +090068#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +020069#include <linux/poll.h>
70#include <linux/miscdevice.h>
71#endif
Stelian Pop7f09c432007-01-13 23:04:31 +010072
malattia@linux.it33a04452007-04-09 19:26:03 +020073#define DRV_PFX "sony-laptop: "
Mattia Dongilid6697932011-02-19 11:52:28 +090074#define dprintk(msg...) do { \
75 if (debug) \
76 pr_warn(DRV_PFX msg); \
malattia@linux.itb9a218b2007-04-09 10:19:06 +020077} while (0)
78
Mattia Dongili425ef5d2008-01-14 18:05:44 +090079#define SONY_LAPTOP_DRIVER_VERSION "0.6"
Alessandro Guido50f62af2007-01-13 23:04:34 +010080
malattia@linux.it33a04452007-04-09 19:26:03 +020081#define SONY_NC_CLASS "sony-nc"
82#define SONY_NC_HID "SNY5001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020083#define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
malattia@linux.it33a04452007-04-09 19:26:03 +020084
85#define SONY_PIC_CLASS "sony-pic"
86#define SONY_PIC_HID "SNY6001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020087#define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
Stelian Pop7f09c432007-01-13 23:04:31 +010088
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010089MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
malattia@linux.it33a04452007-04-09 19:26:03 +020090MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
Stelian Pop7f09c432007-01-13 23:04:31 +010091MODULE_LICENSE("GPL");
malattia@linux.it33a04452007-04-09 19:26:03 +020092MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
Stelian Pop7f09c432007-01-13 23:04:31 +010093
94static int debug;
95module_param(debug, int, 0);
96MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
Len Browna02d1c12007-02-07 15:34:02 -050097 "the development of this driver");
Stelian Pop7f09c432007-01-13 23:04:31 +010098
malattia@linux.it33a04452007-04-09 19:26:03 +020099static int no_spic; /* = 0 */
100module_param(no_spic, int, 0444);
101MODULE_PARM_DESC(no_spic,
102 "set this if you don't want to enable the SPIC device");
103
104static int compat; /* = 0 */
105module_param(compat, int, 0444);
106MODULE_PARM_DESC(compat,
malattia@linux.it7b153f32007-04-09 19:31:25 +0200107 "set this if you want to enable backward compatibility mode");
malattia@linux.it33a04452007-04-09 19:26:03 +0200108
109static unsigned long mask = 0xffffffff;
110module_param(mask, ulong, 0644);
111MODULE_PARM_DESC(mask,
112 "set this to the mask of event you want to enable (see doc)");
113
malattia@linux.it5f3d2892007-04-28 23:18:45 +0900114static int camera; /* = 0 */
115module_param(camera, int, 0444);
116MODULE_PARM_DESC(camera,
117 "set this to 1 to enable Motion Eye camera controls "
118 "(only use it if you have a C1VE or C1VN model)");
119
Mattia Dongilia64e62a2007-05-01 11:19:53 +0900120#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +0200121static int minor = -1;
122module_param(minor, int, 0);
123MODULE_PARM_DESC(minor,
124 "minor number of the misc device for the SPIC compatibility code, "
125 "default is -1 (automatic)");
126#endif
127
Mattia Dongilibf155712011-02-19 11:52:31 +0900128static int kbd_backlight; /* = 1 */
129module_param(kbd_backlight, int, 0444);
130MODULE_PARM_DESC(kbd_backlight,
131 "set this to 0 to disable keyboard backlight, "
132 "1 to enable it (default: 0)");
133
134static int kbd_backlight_timeout; /* = 0 */
135module_param(kbd_backlight_timeout, int, 0444);
136MODULE_PARM_DESC(kbd_backlight_timeout,
137 "set this to 0 to set the default 10 seconds timeout, "
138 "1 for 30 seconds, 2 for 60 seconds and 3 to disable timeout "
139 "(default: 0)");
140
Marco Chiapperodf410d52011-04-05 23:38:34 +0900141static void sony_nc_kbd_backlight_resume(void);
142
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900143enum sony_nc_rfkill {
144 SONY_WIFI,
145 SONY_BLUETOOTH,
146 SONY_WWAN,
147 SONY_WIMAX,
Johannes Berg19d337d2009-06-02 13:01:37 +0200148 N_SONY_RFKILL,
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900149};
150
Mattia Dongilid5a664a2009-12-17 00:08:35 +0900151static int sony_rfkill_handle;
Johannes Berg19d337d2009-06-02 13:01:37 +0200152static struct rfkill *sony_rfkill_devices[N_SONY_RFKILL];
153static int sony_rfkill_address[N_SONY_RFKILL] = {0x300, 0x500, 0x700, 0x900};
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900154static void sony_nc_rfkill_update(void);
155
malattia@linux.it1549ee62007-04-09 10:19:08 +0200156/*********** Input Devices ***********/
157
158#define SONY_LAPTOP_BUF_SIZE 128
159struct sony_laptop_input_s {
160 atomic_t users;
161 struct input_dev *jog_dev;
162 struct input_dev *key_dev;
Stefani Seibold45465482009-12-21 14:37:26 -0800163 struct kfifo fifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200164 spinlock_t fifo_lock;
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800165 struct timer_list release_key_timer;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200166};
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900167
malattia@linux.it1549ee62007-04-09 10:19:08 +0200168static struct sony_laptop_input_s sony_laptop_input = {
169 .users = ATOMIC_INIT(0),
170};
171
172struct sony_laptop_keypress {
173 struct input_dev *dev;
174 int key;
175};
176
Mattia Dongilibc57f862007-07-20 02:01:57 +0900177/* Correspondance table between sonypi events
178 * and input layer indexes in the keymap
179 */
180static int sony_laptop_input_index[] = {
Mattia Dongili3eb87492008-01-14 18:05:45 +0900181 -1, /* 0 no event */
182 -1, /* 1 SONYPI_EVENT_JOGDIAL_DOWN */
183 -1, /* 2 SONYPI_EVENT_JOGDIAL_UP */
184 -1, /* 3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
185 -1, /* 4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */
186 -1, /* 5 SONYPI_EVENT_JOGDIAL_PRESSED */
187 -1, /* 6 SONYPI_EVENT_JOGDIAL_RELEASED */
188 0, /* 7 SONYPI_EVENT_CAPTURE_PRESSED */
189 1, /* 8 SONYPI_EVENT_CAPTURE_RELEASED */
190 2, /* 9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
191 3, /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
192 4, /* 11 SONYPI_EVENT_FNKEY_ESC */
193 5, /* 12 SONYPI_EVENT_FNKEY_F1 */
194 6, /* 13 SONYPI_EVENT_FNKEY_F2 */
195 7, /* 14 SONYPI_EVENT_FNKEY_F3 */
196 8, /* 15 SONYPI_EVENT_FNKEY_F4 */
197 9, /* 16 SONYPI_EVENT_FNKEY_F5 */
198 10, /* 17 SONYPI_EVENT_FNKEY_F6 */
199 11, /* 18 SONYPI_EVENT_FNKEY_F7 */
200 12, /* 19 SONYPI_EVENT_FNKEY_F8 */
201 13, /* 20 SONYPI_EVENT_FNKEY_F9 */
202 14, /* 21 SONYPI_EVENT_FNKEY_F10 */
203 15, /* 22 SONYPI_EVENT_FNKEY_F11 */
204 16, /* 23 SONYPI_EVENT_FNKEY_F12 */
205 17, /* 24 SONYPI_EVENT_FNKEY_1 */
206 18, /* 25 SONYPI_EVENT_FNKEY_2 */
207 19, /* 26 SONYPI_EVENT_FNKEY_D */
208 20, /* 27 SONYPI_EVENT_FNKEY_E */
209 21, /* 28 SONYPI_EVENT_FNKEY_F */
210 22, /* 29 SONYPI_EVENT_FNKEY_S */
211 23, /* 30 SONYPI_EVENT_FNKEY_B */
212 24, /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */
213 25, /* 32 SONYPI_EVENT_PKEY_P1 */
214 26, /* 33 SONYPI_EVENT_PKEY_P2 */
215 27, /* 34 SONYPI_EVENT_PKEY_P3 */
216 28, /* 35 SONYPI_EVENT_BACK_PRESSED */
217 -1, /* 36 SONYPI_EVENT_LID_CLOSED */
218 -1, /* 37 SONYPI_EVENT_LID_OPENED */
219 29, /* 38 SONYPI_EVENT_BLUETOOTH_ON */
220 30, /* 39 SONYPI_EVENT_BLUETOOTH_OFF */
221 31, /* 40 SONYPI_EVENT_HELP_PRESSED */
222 32, /* 41 SONYPI_EVENT_FNKEY_ONLY */
223 33, /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
224 34, /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */
225 35, /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
226 36, /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
227 37, /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
228 38, /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */
229 39, /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
230 40, /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
231 41, /* 50 SONYPI_EVENT_ZOOM_PRESSED */
232 42, /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */
233 43, /* 52 SONYPI_EVENT_MEYE_FACE */
234 44, /* 53 SONYPI_EVENT_MEYE_OPPOSITE */
235 45, /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */
236 46, /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */
237 -1, /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */
238 -1, /* 57 SONYPI_EVENT_BATTERY_INSERT */
239 -1, /* 58 SONYPI_EVENT_BATTERY_REMOVE */
240 -1, /* 59 SONYPI_EVENT_FNKEY_RELEASED */
241 47, /* 60 SONYPI_EVENT_WIRELESS_ON */
242 48, /* 61 SONYPI_EVENT_WIRELESS_OFF */
243 49, /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */
244 50, /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900245 51, /* 64 SONYPI_EVENT_CD_EJECT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900246 52, /* 65 SONYPI_EVENT_MODEKEY_PRESSED */
247 53, /* 66 SONYPI_EVENT_PKEY_P4 */
248 54, /* 67 SONYPI_EVENT_PKEY_P5 */
249 55, /* 68 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900250 56, /* 69 SONYPI_EVENT_VOLUME_INC_PRESSED */
251 57, /* 70 SONYPI_EVENT_VOLUME_DEC_PRESSED */
252 -1, /* 71 SONYPI_EVENT_BRIGHTNESS_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900253 58, /* 72 SONYPI_EVENT_MEDIA_PRESSED */
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900254 59, /* 72 SONYPI_EVENT_VENDOR_PRESSED */
Mattia Dongilibc57f862007-07-20 02:01:57 +0900255};
256
257static int sony_laptop_input_keycode_map[] = {
258 KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
259 KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
260 KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
261 KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
262 KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
263 KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
264 KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
265 KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
266 KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
267 KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
268 KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
269 KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
270 KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
271 KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
272 KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
273 KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
274 KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
275 KEY_FN_F1, /* 17 SONYPI_EVENT_FNKEY_1 */
276 KEY_FN_F2, /* 18 SONYPI_EVENT_FNKEY_2 */
277 KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
278 KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
279 KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
280 KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
281 KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
282 KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
283 KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
284 KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
285 KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
286 KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
287 KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
288 KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
289 KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
290 KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
291 KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
292 KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
293 KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
294 KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
295 KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
296 KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
297 KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
298 KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
299 KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
300 BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
301 KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
302 KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
303 KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
304 KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
305 KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
306 KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
Mattia Dongili3eb87492008-01-14 18:05:45 +0900307 KEY_ZOOMIN, /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900308 KEY_ZOOMOUT, /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900309 KEY_EJECTCD, /* 51 SONYPI_EVENT_CD_EJECT_PRESSED */
310 KEY_F13, /* 52 SONYPI_EVENT_MODEKEY_PRESSED */
311 KEY_PROG4, /* 53 SONYPI_EVENT_PKEY_P4 */
312 KEY_F14, /* 54 SONYPI_EVENT_PKEY_P5 */
313 KEY_F15, /* 55 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900314 KEY_VOLUMEUP, /* 56 SONYPI_EVENT_VOLUME_INC_PRESSED */
315 KEY_VOLUMEDOWN, /* 57 SONYPI_EVENT_VOLUME_DEC_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900316 KEY_MEDIA, /* 58 SONYPI_EVENT_MEDIA_PRESSED */
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900317 KEY_VENDOR, /* 59 SONYPI_EVENT_VENDOR_PRESSED */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200318};
319
320/* release buttons after a short delay if pressed */
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800321static void do_sony_laptop_release_key(unsigned long unused)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200322{
323 struct sony_laptop_keypress kp;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800324 unsigned long flags;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200325
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800326 spin_lock_irqsave(&sony_laptop_input.fifo_lock, flags);
327
328 if (kfifo_out(&sony_laptop_input.fifo,
329 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200330 input_report_key(kp.dev, kp.key, 0);
331 input_sync(kp.dev);
332 }
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800333
334 /* If there is something in the fifo schedule next release. */
335 if (kfifo_len(&sony_laptop_input.fifo) != 0)
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800336 mod_timer(&sony_laptop_input.release_key_timer,
337 jiffies + msecs_to_jiffies(10));
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800338
339 spin_unlock_irqrestore(&sony_laptop_input.fifo_lock, flags);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200340}
malattia@linux.it1549ee62007-04-09 10:19:08 +0200341
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200342/* forward event to the input subsystem */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200343static void sony_laptop_report_input_event(u8 event)
344{
345 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
346 struct input_dev *key_dev = sony_laptop_input.key_dev;
347 struct sony_laptop_keypress kp = { NULL };
malattia@linux.it1549ee62007-04-09 10:19:08 +0200348
Almer S. Tigelaara83021a2009-04-12 11:26:28 +0000349 if (event == SONYPI_EVENT_FNKEY_RELEASED ||
350 event == SONYPI_EVENT_ANYBUTTON_RELEASED) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200351 /* Nothing, not all VAIOs generate this event */
352 return;
353 }
354
355 /* report events */
356 switch (event) {
357 /* jog_dev events */
358 case SONYPI_EVENT_JOGDIAL_UP:
359 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
360 input_report_rel(jog_dev, REL_WHEEL, 1);
361 input_sync(jog_dev);
362 return;
363
364 case SONYPI_EVENT_JOGDIAL_DOWN:
365 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
366 input_report_rel(jog_dev, REL_WHEEL, -1);
367 input_sync(jog_dev);
368 return;
369
370 /* key_dev events */
371 case SONYPI_EVENT_JOGDIAL_PRESSED:
372 kp.key = BTN_MIDDLE;
373 kp.dev = jog_dev;
374 break;
375
376 default:
Adrian Bunkd399d132008-02-20 00:59:03 +0200377 if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
Mattia Dongilibc57f862007-07-20 02:01:57 +0900378 dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
379 break;
380 }
381 if (sony_laptop_input_index[event] != -1) {
382 kp.key = sony_laptop_input_keycode_map[sony_laptop_input_index[event]];
383 if (kp.key != KEY_UNKNOWN)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200384 kp.dev = key_dev;
Mattia Dongilibc57f862007-07-20 02:01:57 +0900385 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200386 break;
387 }
388
389 if (kp.dev) {
390 input_report_key(kp.dev, kp.key, 1);
Mattia Dongilibc57f862007-07-20 02:01:57 +0900391 /* we emit the scancode so we can always remap the key */
392 input_event(kp.dev, EV_MSC, MSC_SCAN, event);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200393 input_sync(kp.dev);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200394
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800395 /* schedule key release */
396 kfifo_in_locked(&sony_laptop_input.fifo,
397 (unsigned char *)&kp, sizeof(kp),
398 &sony_laptop_input.fifo_lock);
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800399 mod_timer(&sony_laptop_input.release_key_timer,
400 jiffies + msecs_to_jiffies(10));
malattia@linux.it1549ee62007-04-09 10:19:08 +0200401 } else
402 dprintk("unknown input event %.2x\n", event);
403}
404
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500405static int sony_laptop_setup_input(struct acpi_device *acpi_device)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200406{
407 struct input_dev *jog_dev;
408 struct input_dev *key_dev;
409 int i;
410 int error;
411
412 /* don't run again if already initialized */
413 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
414 return 0;
415
416 /* kfifo */
417 spin_lock_init(&sony_laptop_input.fifo_lock);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800418 error = kfifo_alloc(&sony_laptop_input.fifo,
419 SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -0800420 if (error) {
Mattia Dongilid6697932011-02-19 11:52:28 +0900421 pr_err(DRV_PFX "kfifo_alloc failed\n");
malattia@linux.it1549ee62007-04-09 10:19:08 +0200422 goto err_dec_users;
423 }
424
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800425 setup_timer(&sony_laptop_input.release_key_timer,
426 do_sony_laptop_release_key, 0);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200427
428 /* input keys */
429 key_dev = input_allocate_device();
430 if (!key_dev) {
431 error = -ENOMEM;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800432 goto err_free_kfifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200433 }
434
435 key_dev->name = "Sony Vaio Keys";
436 key_dev->id.bustype = BUS_ISA;
437 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500438 key_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200439
440 /* Initialize the Input Drivers: special keys */
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800441 input_set_capability(key_dev, EV_MSC, MSC_SCAN);
442
443 __set_bit(EV_KEY, key_dev->evbit);
Mattia Dongilibc57f862007-07-20 02:01:57 +0900444 key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
445 key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
446 key_dev->keycode = &sony_laptop_input_keycode_map;
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800447 for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++)
448 __set_bit(sony_laptop_input_keycode_map[i], key_dev->keybit);
449 __clear_bit(KEY_RESERVED, key_dev->keybit);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200450
451 error = input_register_device(key_dev);
452 if (error)
453 goto err_free_keydev;
454
455 sony_laptop_input.key_dev = key_dev;
456
457 /* jogdial */
458 jog_dev = input_allocate_device();
459 if (!jog_dev) {
460 error = -ENOMEM;
461 goto err_unregister_keydev;
462 }
463
464 jog_dev->name = "Sony Vaio Jogdial";
465 jog_dev->id.bustype = BUS_ISA;
466 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500467 key_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200468
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800469 input_set_capability(jog_dev, EV_KEY, BTN_MIDDLE);
470 input_set_capability(jog_dev, EV_REL, REL_WHEEL);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200471
472 error = input_register_device(jog_dev);
473 if (error)
474 goto err_free_jogdev;
475
476 sony_laptop_input.jog_dev = jog_dev;
477
478 return 0;
479
480err_free_jogdev:
481 input_free_device(jog_dev);
482
483err_unregister_keydev:
484 input_unregister_device(key_dev);
485 /* to avoid kref underflow below at input_free_device */
486 key_dev = NULL;
487
488err_free_keydev:
489 input_free_device(key_dev);
490
malattia@linux.it1549ee62007-04-09 10:19:08 +0200491err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -0800492 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200493
494err_dec_users:
495 atomic_dec(&sony_laptop_input.users);
496 return error;
497}
498
499static void sony_laptop_remove_input(void)
500{
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800501 struct sony_laptop_keypress kp = { NULL };
502
503 /* Cleanup only after the last user has gone */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200504 if (!atomic_dec_and_test(&sony_laptop_input.users))
505 return;
506
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800507 del_timer_sync(&sony_laptop_input.release_key_timer);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800508
509 /*
510 * Generate key-up events for remaining keys. Note that we don't
511 * need locking since nobody is adding new events to the kfifo.
512 */
513 while (kfifo_out(&sony_laptop_input.fifo,
514 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
515 input_report_key(kp.dev, kp.key, 0);
516 input_sync(kp.dev);
517 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200518
519 /* destroy input devs */
520 input_unregister_device(sony_laptop_input.key_dev);
521 sony_laptop_input.key_dev = NULL;
522
523 if (sony_laptop_input.jog_dev) {
524 input_unregister_device(sony_laptop_input.jog_dev);
525 sony_laptop_input.jog_dev = NULL;
526 }
527
Stefani Seibold45465482009-12-21 14:37:26 -0800528 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200529}
530
malattia@linux.it56b87562007-04-09 10:19:05 +0200531/*********** Platform Device ***********/
532
533static atomic_t sony_pf_users = ATOMIC_INIT(0);
534static struct platform_driver sony_pf_driver = {
535 .driver = {
536 .name = "sony-laptop",
537 .owner = THIS_MODULE,
538 }
539};
540static struct platform_device *sony_pf_device;
541
542static int sony_pf_add(void)
543{
544 int ret = 0;
545
546 /* don't run again if already initialized */
547 if (atomic_add_return(1, &sony_pf_users) > 1)
548 return 0;
549
550 ret = platform_driver_register(&sony_pf_driver);
551 if (ret)
552 goto out;
553
554 sony_pf_device = platform_device_alloc("sony-laptop", -1);
555 if (!sony_pf_device) {
556 ret = -ENOMEM;
557 goto out_platform_registered;
558 }
559
560 ret = platform_device_add(sony_pf_device);
561 if (ret)
562 goto out_platform_alloced;
563
564 return 0;
565
566 out_platform_alloced:
567 platform_device_put(sony_pf_device);
568 sony_pf_device = NULL;
569 out_platform_registered:
570 platform_driver_unregister(&sony_pf_driver);
571 out:
572 atomic_dec(&sony_pf_users);
573 return ret;
574}
575
576static void sony_pf_remove(void)
577{
578 /* deregister only after the last user has gone */
579 if (!atomic_dec_and_test(&sony_pf_users))
580 return;
581
Axel Lin08db2b32010-07-01 10:18:01 +0800582 platform_device_unregister(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +0200583 platform_driver_unregister(&sony_pf_driver);
584}
585
586/*********** SNC (SNY5001) Device ***********/
587
malattia@linux.it33a04452007-04-09 19:26:03 +0200588/* the device uses 1-based values, while the backlight subsystem uses
589 0-based values */
590#define SONY_MAX_BRIGHTNESS 8
591
592#define SNC_VALIDATE_IN 0
593#define SNC_VALIDATE_OUT 1
594
malattia@linux.it59b19102007-04-09 10:19:04 +0200595static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500596 char *);
malattia@linux.it59b19102007-04-09 10:19:04 +0200597static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500598 const char *, size_t);
Mattia Dongili156c2212007-02-12 22:01:07 +0100599static int boolean_validate(const int, const int);
600static int brightness_default_validate(const int, const int);
601
malattia@linux.it59b19102007-04-09 10:19:04 +0200602struct sony_nc_value {
Len Browna02d1c12007-02-07 15:34:02 -0500603 char *name; /* name of the entry */
604 char **acpiget; /* names of the ACPI get function */
605 char **acpiset; /* names of the ACPI set function */
Mattia Dongili156c2212007-02-12 22:01:07 +0100606 int (*validate)(const int, const int); /* input/output validation */
Len Browna02d1c12007-02-07 15:34:02 -0500607 int value; /* current setting */
608 int valid; /* Has ever been set */
609 int debug; /* active only in debug mode ? */
Lucas De Marchic8440332011-03-17 17:18:22 -0300610 struct device_attribute devattr; /* sysfs attribute */
Stelian Pop7f09c432007-01-13 23:04:31 +0100611};
612
malattia@linux.it59b19102007-04-09 10:19:04 +0200613#define SNC_HANDLE_NAMES(_name, _values...) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100614 static char *snc_##_name[] = { _values, NULL }
615
malattia@linux.it59b19102007-04-09 10:19:04 +0200616#define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100617 { \
618 .name = __stringify(_name), \
619 .acpiget = _getters, \
620 .acpiset = _setters, \
Mattia Dongili156c2212007-02-12 22:01:07 +0100621 .validate = _validate, \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100622 .debug = _debug, \
malattia@linux.it59b19102007-04-09 10:19:04 +0200623 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100624 }
625
malattia@linux.it59b19102007-04-09 10:19:04 +0200626#define SNC_HANDLE_NULL { .name = NULL }
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100627
malattia@linux.it59b19102007-04-09 10:19:04 +0200628SNC_HANDLE_NAMES(fnkey_get, "GHKE");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100629
malattia@linux.it59b19102007-04-09 10:19:04 +0200630SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
631SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100632
malattia@linux.it59b19102007-04-09 10:19:04 +0200633SNC_HANDLE_NAMES(cdpower_get, "GCDP");
634SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100635
malattia@linux.it59b19102007-04-09 10:19:04 +0200636SNC_HANDLE_NAMES(audiopower_get, "GAZP");
637SNC_HANDLE_NAMES(audiopower_set, "AZPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100638
malattia@linux.it59b19102007-04-09 10:19:04 +0200639SNC_HANDLE_NAMES(lanpower_get, "GLNP");
640SNC_HANDLE_NAMES(lanpower_set, "LNPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100641
Mattia Dongili044847e2007-07-16 02:34:33 +0900642SNC_HANDLE_NAMES(lidstate_get, "GLID");
643
644SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
645SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
646
647SNC_HANDLE_NAMES(gainbass_get, "GMGB");
648SNC_HANDLE_NAMES(gainbass_set, "CMGB");
649
malattia@linux.it59b19102007-04-09 10:19:04 +0200650SNC_HANDLE_NAMES(PID_get, "GPID");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100651
malattia@linux.it59b19102007-04-09 10:19:04 +0200652SNC_HANDLE_NAMES(CTR_get, "GCTR");
653SNC_HANDLE_NAMES(CTR_set, "SCTR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100654
malattia@linux.it59b19102007-04-09 10:19:04 +0200655SNC_HANDLE_NAMES(PCR_get, "GPCR");
656SNC_HANDLE_NAMES(PCR_set, "SPCR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100657
malattia@linux.it59b19102007-04-09 10:19:04 +0200658SNC_HANDLE_NAMES(CMI_get, "GCMI");
659SNC_HANDLE_NAMES(CMI_set, "SCMI");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100660
malattia@linux.it59b19102007-04-09 10:19:04 +0200661static struct sony_nc_value sony_nc_values[] = {
662 SNC_HANDLE(brightness_default, snc_brightness_def_get,
Mattia Dongili156c2212007-02-12 22:01:07 +0100663 snc_brightness_def_set, brightness_default_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200664 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
665 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
666 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100667 boolean_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200668 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100669 boolean_validate, 1),
Mattia Dongili044847e2007-07-16 02:34:33 +0900670 SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
671 boolean_validate, 0),
672 SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
673 boolean_validate, 0),
674 SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
675 boolean_validate, 0),
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100676 /* unknown methods */
malattia@linux.it59b19102007-04-09 10:19:04 +0200677 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
678 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
679 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
680 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
681 SNC_HANDLE_NULL
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100682};
683
malattia@linux.it59b19102007-04-09 10:19:04 +0200684static acpi_handle sony_nc_acpi_handle;
685static struct acpi_device *sony_nc_acpi_device = NULL;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100686
Mattia Dongilid78865c2007-02-07 20:01:56 +0100687/*
688 * acpi_evaluate_object wrappers
689 */
Stelian Pop7f09c432007-01-13 23:04:31 +0100690static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
691{
692 struct acpi_buffer output;
693 union acpi_object out_obj;
694 acpi_status status;
695
696 output.length = sizeof(out_obj);
697 output.pointer = &out_obj;
698
699 status = acpi_evaluate_object(handle, name, NULL, &output);
700 if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
701 *result = out_obj.integer.value;
702 return 0;
703 }
704
Mattia Dongilid6697932011-02-19 11:52:28 +0900705 pr_warn(DRV_PFX "acpi_callreadfunc failed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100706
707 return -1;
708}
709
710static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
711 int *result)
712{
713 struct acpi_object_list params;
714 union acpi_object in_obj;
715 struct acpi_buffer output;
716 union acpi_object out_obj;
717 acpi_status status;
718
719 params.count = 1;
720 params.pointer = &in_obj;
721 in_obj.type = ACPI_TYPE_INTEGER;
722 in_obj.integer.value = value;
723
724 output.length = sizeof(out_obj);
725 output.pointer = &out_obj;
726
727 status = acpi_evaluate_object(handle, name, &params, &output);
728 if (status == AE_OK) {
729 if (result != NULL) {
730 if (out_obj.type != ACPI_TYPE_INTEGER) {
Mattia Dongilid6697932011-02-19 11:52:28 +0900731 pr_warn(DRV_PFX "acpi_evaluate_object bad "
Stelian Pop7f09c432007-01-13 23:04:31 +0100732 "return type\n");
733 return -1;
734 }
735 *result = out_obj.integer.value;
736 }
737 return 0;
738 }
739
Mattia Dongilid6697932011-02-19 11:52:28 +0900740 pr_warn(DRV_PFX "acpi_evaluate_object failed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100741
742 return -1;
743}
744
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900745struct sony_nc_handles {
746 u16 cap[0x10];
747 struct device_attribute devattr;
748};
749
Dan Carpenterf11113b2011-02-26 15:54:27 +0300750static struct sony_nc_handles *handles;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900751
752static ssize_t sony_nc_handles_show(struct device *dev,
753 struct device_attribute *attr, char *buffer)
754{
755 ssize_t len = 0;
756 int i;
757
758 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
759 len += snprintf(buffer + len, PAGE_SIZE - len, "0x%.4x ",
760 handles->cap[i]);
761 }
762 len += snprintf(buffer + len, PAGE_SIZE - len, "\n");
763
764 return len;
765}
766
767static int sony_nc_handles_setup(struct platform_device *pd)
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900768{
769 int i;
770 int result;
771
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900772 handles = kzalloc(sizeof(*handles), GFP_KERNEL);
Dan Carpenter31f00752011-02-26 15:55:24 +0300773 if (!handles)
774 return -ENOMEM;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900775
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900776 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
777 if (!acpi_callsetfunc(sony_nc_acpi_handle,
778 "SN00", i + 0x20, &result)) {
779 dprintk("caching handle 0x%.4x (offset: 0x%.2x)\n",
780 result, i);
781 handles->cap[i] = result;
Mattia Dongili56e6e712011-02-19 11:52:25 +0900782 }
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900783 }
784
Mattia Dongili855b8bc2011-04-05 23:38:35 +0900785 if (debug) {
786 sysfs_attr_init(&handles->devattr.attr);
787 handles->devattr.attr.name = "handles";
788 handles->devattr.attr.mode = S_IRUGO;
789 handles->devattr.show = sony_nc_handles_show;
790
791 /* allow reading capabilities via sysfs */
792 if (device_create_file(&pd->dev, &handles->devattr)) {
793 kfree(handles);
794 handles = NULL;
795 return -1;
796 }
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900797 }
798
799 return 0;
800}
801
802static int sony_nc_handles_cleanup(struct platform_device *pd)
803{
804 if (handles) {
Mattia Dongili855b8bc2011-04-05 23:38:35 +0900805 if (debug)
806 device_remove_file(&pd->dev, &handles->devattr);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900807 kfree(handles);
808 handles = NULL;
809 }
810 return 0;
811}
812
813static int sony_find_snc_handle(int handle)
814{
815 int i;
Mattia Dongilifef34862011-04-02 19:00:44 +0900816
817 /* not initialized yet, return early */
818 if (!handles)
819 return -1;
820
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900821 for (i = 0; i < 0x10; i++) {
822 if (handles->cap[i] == handle) {
823 dprintk("found handle 0x%.4x (offset: 0x%.2x)\n",
824 handle, i);
825 return i;
826 }
827 }
Mattia Dongili56e6e712011-02-19 11:52:25 +0900828 dprintk("handle 0x%.4x not found\n", handle);
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900829 return -1;
830}
831
832static int sony_call_snc_handle(int handle, int argument, int *result)
833{
Mattia Dongili56e6e712011-02-19 11:52:25 +0900834 int ret = 0;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900835 int offset = sony_find_snc_handle(handle);
836
837 if (offset < 0)
838 return -1;
839
Mattia Dongili56e6e712011-02-19 11:52:25 +0900840 ret = acpi_callsetfunc(sony_nc_acpi_handle, "SN07", offset | argument,
841 result);
842 dprintk("called SN07 with 0x%.4x (result: 0x%.4x)\n", offset | argument,
843 *result);
844 return ret;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900845}
846
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100847/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200848 * sony_nc_values input/output validate functions
Mattia Dongili156c2212007-02-12 22:01:07 +0100849 */
850
851/* brightness_default_validate:
852 *
853 * manipulate input output values to keep consistency with the
854 * backlight framework for which brightness values are 0-based.
855 */
856static int brightness_default_validate(const int direction, const int value)
857{
858 switch (direction) {
859 case SNC_VALIDATE_OUT:
860 return value - 1;
861 case SNC_VALIDATE_IN:
862 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
863 return value + 1;
864 }
865 return -EINVAL;
866}
867
868/* boolean_validate:
869 *
870 * on input validate boolean values 0/1, on output just pass the
871 * received value.
872 */
873static int boolean_validate(const int direction, const int value)
874{
875 if (direction == SNC_VALIDATE_IN) {
876 if (value != 0 && value != 1)
877 return -EINVAL;
878 }
879 return value;
880}
881
882/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200883 * Sysfs show/store common to all sony_nc_values
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100884 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200885static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
Len Browna02d1c12007-02-07 15:34:02 -0500886 char *buffer)
Stelian Pop7f09c432007-01-13 23:04:31 +0100887{
Stelian Pop7f09c432007-01-13 23:04:31 +0100888 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200889 struct sony_nc_value *item =
890 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100891
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100892 if (!*item->acpiget)
Stelian Pop7f09c432007-01-13 23:04:31 +0100893 return -EIO;
894
malattia@linux.it59b19102007-04-09 10:19:04 +0200895 if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100896 return -EIO;
897
Mattia Dongili156c2212007-02-12 22:01:07 +0100898 if (item->validate)
899 value = item->validate(SNC_VALIDATE_OUT, value);
900
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100901 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
Stelian Pop7f09c432007-01-13 23:04:31 +0100902}
903
malattia@linux.it59b19102007-04-09 10:19:04 +0200904static ssize_t sony_nc_sysfs_store(struct device *dev,
Len Browna02d1c12007-02-07 15:34:02 -0500905 struct device_attribute *attr,
906 const char *buffer, size_t count)
Stelian Pop7f09c432007-01-13 23:04:31 +0100907{
Stelian Pop7f09c432007-01-13 23:04:31 +0100908 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200909 struct sony_nc_value *item =
910 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100911
912 if (!item->acpiset)
913 return -EIO;
914
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100915 if (count > 31)
916 return -EINVAL;
917
918 value = simple_strtoul(buffer, NULL, 10);
Stelian Pop7f09c432007-01-13 23:04:31 +0100919
Mattia Dongili156c2212007-02-12 22:01:07 +0100920 if (item->validate)
921 value = item->validate(SNC_VALIDATE_IN, value);
922
923 if (value < 0)
924 return value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100925
malattia@linux.it59b19102007-04-09 10:19:04 +0200926 if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100927 return -EIO;
Andrew Morton3f4f4612007-01-13 23:04:32 +0100928 item->value = value;
929 item->valid = 1;
Stelian Pop7f09c432007-01-13 23:04:31 +0100930 return count;
931}
932
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100933
Mattia Dongilid78865c2007-02-07 20:01:56 +0100934/*
935 * Backlight device
936 */
Mattia Dongili62d2f232011-05-09 10:20:29 -0400937struct sony_backlight_props {
938 struct backlight_device *dev;
939 int handle;
940 u8 offset;
941 u8 maxlvl;
942};
943struct sony_backlight_props sony_bl_props;
944
Mattia Dongilid78865c2007-02-07 20:01:56 +0100945static int sony_backlight_update_status(struct backlight_device *bd)
Andrew Morton3f4f4612007-01-13 23:04:32 +0100946{
malattia@linux.it59b19102007-04-09 10:19:04 +0200947 return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
Richard Purdie321709c2007-02-10 15:04:08 +0000948 bd->props.brightness + 1, NULL);
Andrew Morton3f4f4612007-01-13 23:04:32 +0100949}
950
Mattia Dongilid78865c2007-02-07 20:01:56 +0100951static int sony_backlight_get_brightness(struct backlight_device *bd)
952{
953 int value;
954
malattia@linux.it59b19102007-04-09 10:19:04 +0200955 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
Mattia Dongilid78865c2007-02-07 20:01:56 +0100956 return 0;
957 /* brightness levels are 1-based, while backlight ones are 0-based */
958 return value - 1;
959}
960
Mattia Dongili7751ab82011-02-19 11:52:32 +0900961static int sony_nc_get_brightness_ng(struct backlight_device *bd)
962{
963 int result;
964 int *handle = (int *)bl_get_data(bd);
Mattia Dongili62d2f232011-05-09 10:20:29 -0400965 struct sony_backlight_props *sdev =
966 (struct sony_backlight_props *)bl_get_data(bd);
Mattia Dongili7751ab82011-02-19 11:52:32 +0900967
Mattia Dongili62d2f232011-05-09 10:20:29 -0400968 sony_call_snc_handle(sdev->handle, 0x0200, &result);
Mattia Dongili7751ab82011-02-19 11:52:32 +0900969
Mattia Dongili62d2f232011-05-09 10:20:29 -0400970 return (result & 0xff) - sdev->offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +0900971}
972
973static int sony_nc_update_status_ng(struct backlight_device *bd)
974{
975 int value, result;
976 int *handle = (int *)bl_get_data(bd);
Mattia Dongili62d2f232011-05-09 10:20:29 -0400977 struct sony_backlight_props *sdev =
978 (struct sony_backlight_props *)bl_get_data(bd);
Mattia Dongili7751ab82011-02-19 11:52:32 +0900979
Mattia Dongili62d2f232011-05-09 10:20:29 -0400980 value = bd->props.brightness + sdev->offset;
981 if (sony_call_snc_handle(sdev->handle, 0x0100 | (value << 16), &result))
Mattia Dongili6192fa72011-04-05 23:38:36 +0900982 return -EIO;
Mattia Dongili7751ab82011-02-19 11:52:32 +0900983
Mattia Dongili6192fa72011-04-05 23:38:36 +0900984 return value;
Mattia Dongili7751ab82011-02-19 11:52:32 +0900985}
986
Lionel Debrouxacc24722010-11-16 14:14:02 +0100987static const struct backlight_ops sony_backlight_ops = {
Mattia Dongili7751ab82011-02-19 11:52:32 +0900988 .options = BL_CORE_SUSPENDRESUME,
Len Browna02d1c12007-02-07 15:34:02 -0500989 .update_status = sony_backlight_update_status,
990 .get_brightness = sony_backlight_get_brightness,
Mattia Dongilid78865c2007-02-07 20:01:56 +0100991};
Mattia Dongili7751ab82011-02-19 11:52:32 +0900992static const struct backlight_ops sony_backlight_ng_ops = {
993 .options = BL_CORE_SUSPENDRESUME,
994 .update_status = sony_nc_update_status_ng,
995 .get_brightness = sony_nc_get_brightness_ng,
996};
Mattia Dongilid78865c2007-02-07 20:01:56 +0100997
998/*
Mattia Dongili6315fd12007-07-16 02:34:35 +0900999 * New SNC-only Vaios event mapping to driver known keys
1000 */
1001struct sony_nc_event {
1002 u8 data;
1003 u8 event;
1004};
1005
Matthew Garrett45c79422009-03-26 21:58:16 +09001006static struct sony_nc_event sony_100_events[] = {
Matthew Garrett9b578962009-03-26 21:58:14 +09001007 { 0x90, SONYPI_EVENT_PKEY_P1 },
1008 { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthias Welwarsky6479efb2009-04-01 22:10:45 +09001009 { 0x91, SONYPI_EVENT_PKEY_P2 },
Matthew Garrett9b578962009-03-26 21:58:14 +09001010 { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001011 { 0x81, SONYPI_EVENT_FNKEY_F1 },
1012 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
Anton Veretenenkof5acf5e2009-03-26 22:44:26 +09001013 { 0x82, SONYPI_EVENT_FNKEY_F2 },
1014 { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
1015 { 0x83, SONYPI_EVENT_FNKEY_F3 },
1016 { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
1017 { 0x84, SONYPI_EVENT_FNKEY_F4 },
1018 { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001019 { 0x85, SONYPI_EVENT_FNKEY_F5 },
1020 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
1021 { 0x86, SONYPI_EVENT_FNKEY_F6 },
1022 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
1023 { 0x87, SONYPI_EVENT_FNKEY_F7 },
1024 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +09001025 { 0x89, SONYPI_EVENT_FNKEY_F9 },
1026 { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001027 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
1028 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
1029 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
1030 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +09001031 { 0x9d, SONYPI_EVENT_ZOOM_PRESSED },
1032 { 0x1d, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +09001033 { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
1034 { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili4f924ba2009-12-17 00:08:33 +09001035 { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
1036 { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +09001037 { 0xa4, SONYPI_EVENT_CD_EJECT_PRESSED },
1038 { 0x24, SONYPI_EVENT_ANYBUTTON_RELEASED },
1039 { 0xa5, SONYPI_EVENT_VENDOR_PRESSED },
1040 { 0x25, SONYPI_EVENT_ANYBUTTON_RELEASED },
1041 { 0xa6, SONYPI_EVENT_HELP_PRESSED },
1042 { 0x26, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001043 { 0, 0 },
1044};
1045
Matthew Garrett45c79422009-03-26 21:58:16 +09001046static struct sony_nc_event sony_127_events[] = {
1047 { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
1048 { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
1049 { 0x82, SONYPI_EVENT_PKEY_P1 },
1050 { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
1051 { 0x83, SONYPI_EVENT_PKEY_P2 },
1052 { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
1053 { 0x84, SONYPI_EVENT_PKEY_P3 },
1054 { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
1055 { 0x85, SONYPI_EVENT_PKEY_P4 },
1056 { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
1057 { 0x86, SONYPI_EVENT_PKEY_P5 },
1058 { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett45c79422009-03-26 21:58:16 +09001059 { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
1060 { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
1061 { 0, 0 },
1062};
1063
Mattia Dongili6315fd12007-07-16 02:34:35 +09001064/*
Mattia Dongilid78865c2007-02-07 20:01:56 +01001065 * ACPI callbacks
1066 */
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001067static void sony_nc_notify(struct acpi_device *device, u32 event)
Stelian Pop7f09c432007-01-13 23:04:31 +01001068{
Mattia Dongili6315fd12007-07-16 02:34:35 +09001069 u32 ev = event;
Mattia Dongili6315fd12007-07-16 02:34:35 +09001070
Matthew Garrett9b578962009-03-26 21:58:14 +09001071 if (ev >= 0x90) {
1072 /* New-style event */
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001073 int result;
Matthew Garrett45c79422009-03-26 21:58:16 +09001074 int key_handle = 0;
Matthew Garrett9b578962009-03-26 21:58:14 +09001075 ev -= 0x90;
Mattia Dongili6315fd12007-07-16 02:34:35 +09001076
Matthew Garrett45c79422009-03-26 21:58:16 +09001077 if (sony_find_snc_handle(0x100) == ev)
1078 key_handle = 0x100;
1079 if (sony_find_snc_handle(0x127) == ev)
1080 key_handle = 0x127;
Matthew Garrett9b578962009-03-26 21:58:14 +09001081
Matthias Welwarsky6479efb2009-04-01 22:10:45 +09001082 if (key_handle) {
Matthew Garrett45c79422009-03-26 21:58:16 +09001083 struct sony_nc_event *key_event;
1084
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001085 if (sony_call_snc_handle(key_handle, 0x200, &result)) {
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001086 dprintk("sony_nc_notify, unable to decode"
Matthew Garrett45c79422009-03-26 21:58:16 +09001087 " event 0x%.2x 0x%.2x\n", key_handle,
1088 ev);
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001089 /* restore the original event */
1090 ev = event;
1091 } else {
Matthew Garrett9b578962009-03-26 21:58:14 +09001092 ev = result & 0xFF;
1093
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001094 if (key_handle == 0x100)
1095 key_event = sony_100_events;
1096 else
1097 key_event = sony_127_events;
Matthew Garrett45c79422009-03-26 21:58:16 +09001098
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001099 for (; key_event->data; key_event++) {
1100 if (key_event->data == ev) {
1101 ev = key_event->event;
1102 break;
1103 }
Matthew Garrett9b578962009-03-26 21:58:14 +09001104 }
Mattia Dongili6315fd12007-07-16 02:34:35 +09001105
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001106 if (!key_event->data)
Mattia Dongilid6697932011-02-19 11:52:28 +09001107 pr_info(DRV_PFX
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001108 "Unknown event: 0x%x 0x%x\n",
1109 key_handle,
1110 ev);
1111 else
1112 sony_laptop_report_input_event(ev);
Matthew Garrett45c79422009-03-26 21:58:16 +09001113 }
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001114 } else if (sony_find_snc_handle(sony_rfkill_handle) == ev) {
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001115 sony_nc_rfkill_update();
1116 return;
Matthew Garrett9b578962009-03-26 21:58:14 +09001117 }
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001118 } else
1119 sony_laptop_report_input_event(ev);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001120
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001121 dprintk("sony_nc_notify, event: 0x%.2x\n", ev);
Len Brown14e04fb2007-08-23 15:20:26 -04001122 acpi_bus_generate_proc_event(sony_nc_acpi_device, 1, ev);
Stelian Pop7f09c432007-01-13 23:04:31 +01001123}
1124
1125static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
1126 void *context, void **return_value)
1127{
Lin Ming30823732008-12-16 16:59:35 +08001128 struct acpi_device_info *info;
Stelian Pop7f09c432007-01-13 23:04:31 +01001129
Bob Moore15b8dd52009-06-29 13:39:29 +08001130 if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001131 pr_warn(DRV_PFX "method: name: %4.4s, args %X\n",
Lin Ming30823732008-12-16 16:59:35 +08001132 (char *)&info->name, info->param_count);
1133
Bob Moore15b8dd52009-06-29 13:39:29 +08001134 kfree(info);
Lin Ming30823732008-12-16 16:59:35 +08001135 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001136
1137 return AE_OK;
1138}
1139
Mattia Dongilid78865c2007-02-07 20:01:56 +01001140/*
1141 * ACPI device
1142 */
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001143static int sony_nc_function_setup(struct acpi_device *device)
1144{
1145 int result;
1146
1147 /* Enable all events */
1148 acpi_callsetfunc(sony_nc_acpi_handle, "SN02", 0xffff, &result);
1149
1150 /* Setup hotkeys */
1151 sony_call_snc_handle(0x0100, 0, &result);
1152 sony_call_snc_handle(0x0101, 0, &result);
1153 sony_call_snc_handle(0x0102, 0x100, &result);
Almer S. Tigelaar560e84a2009-04-12 11:26:27 +00001154 sony_call_snc_handle(0x0127, 0, &result);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001155
1156 return 0;
1157}
1158
malattia@linux.it59b19102007-04-09 10:19:04 +02001159static int sony_nc_resume(struct acpi_device *device)
Mattia Dongilid78865c2007-02-07 20:01:56 +01001160{
malattia@linux.it59b19102007-04-09 10:19:04 +02001161 struct sony_nc_value *item;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001162 acpi_handle handle;
Mattia Dongilid78865c2007-02-07 20:01:56 +01001163
malattia@linux.it59b19102007-04-09 10:19:04 +02001164 for (item = sony_nc_values; item->name; item++) {
Mattia Dongilid78865c2007-02-07 20:01:56 +01001165 int ret;
1166
1167 if (!item->valid)
1168 continue;
malattia@linux.it59b19102007-04-09 10:19:04 +02001169 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
Len Browna02d1c12007-02-07 15:34:02 -05001170 item->value, NULL);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001171 if (ret < 0) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001172 pr_err(DRV_PFX "%s: %d\n", __func__, ret);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001173 break;
1174 }
1175 }
Mattia Dongili6315fd12007-07-16 02:34:35 +09001176
Matthew Garrett82734bf2009-03-26 21:58:13 +09001177 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1178 &handle))) {
1179 if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
1180 dprintk("ECON Method failed\n");
1181 }
1182
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001183 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1184 &handle))) {
1185 dprintk("Doing SNC setup\n");
1186 sony_nc_function_setup(device);
1187 }
1188
Alan Jenkinsa0d97d62009-09-25 10:18:21 +01001189 /* re-read rfkill state */
1190 sony_nc_rfkill_update();
1191
Marco Chiapperodf410d52011-04-05 23:38:34 +09001192 /* restore kbd backlight states */
1193 sony_nc_kbd_backlight_resume();
1194
Mattia Dongilid78865c2007-02-07 20:01:56 +01001195 return 0;
1196}
1197
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001198static void sony_nc_rfkill_cleanup(void)
1199{
1200 int i;
1201
Johannes Berg19d337d2009-06-02 13:01:37 +02001202 for (i = 0; i < N_SONY_RFKILL; i++) {
1203 if (sony_rfkill_devices[i]) {
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001204 rfkill_unregister(sony_rfkill_devices[i]);
Johannes Berg19d337d2009-06-02 13:01:37 +02001205 rfkill_destroy(sony_rfkill_devices[i]);
1206 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001207 }
1208}
1209
Johannes Berg19d337d2009-06-02 13:01:37 +02001210static int sony_nc_rfkill_set(void *data, bool blocked)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001211{
1212 int result;
1213 int argument = sony_rfkill_address[(long) data] + 0x100;
1214
Johannes Berg19d337d2009-06-02 13:01:37 +02001215 if (!blocked)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001216 argument |= 0xff0000;
1217
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001218 return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001219}
1220
Johannes Berg19d337d2009-06-02 13:01:37 +02001221static const struct rfkill_ops sony_rfkill_ops = {
1222 .set_block = sony_nc_rfkill_set,
1223};
1224
1225static int sony_nc_setup_rfkill(struct acpi_device *device,
1226 enum sony_nc_rfkill nc_type)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001227{
1228 int err = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001229 struct rfkill *rfk;
1230 enum rfkill_type type;
1231 const char *name;
Alan Jenkins50fab072009-09-24 20:15:24 +01001232 int result;
1233 bool hwblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001234
Johannes Berg19d337d2009-06-02 13:01:37 +02001235 switch (nc_type) {
1236 case SONY_WIFI:
1237 type = RFKILL_TYPE_WLAN;
1238 name = "sony-wifi";
1239 break;
1240 case SONY_BLUETOOTH:
1241 type = RFKILL_TYPE_BLUETOOTH;
1242 name = "sony-bluetooth";
1243 break;
1244 case SONY_WWAN:
1245 type = RFKILL_TYPE_WWAN;
1246 name = "sony-wwan";
1247 break;
1248 case SONY_WIMAX:
1249 type = RFKILL_TYPE_WIMAX;
1250 name = "sony-wimax";
1251 break;
1252 default:
1253 return -EINVAL;
Mattia Dongili53005a02009-04-12 11:26:31 +00001254 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001255
Johannes Berg19d337d2009-06-02 13:01:37 +02001256 rfk = rfkill_alloc(name, &device->dev, type,
1257 &sony_rfkill_ops, (void *)nc_type);
1258 if (!rfk)
1259 return -ENOMEM;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001260
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001261 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
Alan Jenkins50fab072009-09-24 20:15:24 +01001262 hwblock = !(result & 0x1);
1263 rfkill_set_hw_state(rfk, hwblock);
1264
Johannes Berg19d337d2009-06-02 13:01:37 +02001265 err = rfkill_register(rfk);
1266 if (err) {
1267 rfkill_destroy(rfk);
1268 return err;
Mattia Dongili53005a02009-04-12 11:26:31 +00001269 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001270 sony_rfkill_devices[nc_type] = rfk;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001271 return err;
1272}
1273
Randy Dunlapa46a7802011-01-08 19:56:44 -08001274static void sony_nc_rfkill_update(void)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001275{
Johannes Berg19d337d2009-06-02 13:01:37 +02001276 enum sony_nc_rfkill i;
1277 int result;
1278 bool hwblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001279
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001280 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001281 hwblock = !(result & 0x1);
1282
1283 for (i = 0; i < N_SONY_RFKILL; i++) {
1284 int argument = sony_rfkill_address[i];
1285
1286 if (!sony_rfkill_devices[i])
1287 continue;
1288
1289 if (hwblock) {
Johannes Berge1f8a192009-06-11 12:08:15 +02001290 if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
1291 /* we already know we're blocked */
1292 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001293 continue;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001294 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001295
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001296 sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001297 rfkill_set_states(sony_rfkill_devices[i],
1298 !(result & 0xf), false);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001299 }
1300}
1301
Mattia Dongili528809c2009-12-17 00:08:36 +09001302static void sony_nc_rfkill_setup(struct acpi_device *device)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001303{
Mattia Dongili528809c2009-12-17 00:08:36 +09001304 int offset;
1305 u8 dev_code, i;
1306 acpi_status status;
1307 struct acpi_object_list params;
1308 union acpi_object in_obj;
1309 union acpi_object *device_enum;
1310 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001311
Mattia Dongili528809c2009-12-17 00:08:36 +09001312 offset = sony_find_snc_handle(0x124);
1313 if (offset == -1) {
1314 offset = sony_find_snc_handle(0x135);
1315 if (offset == -1)
1316 return;
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001317 else
1318 sony_rfkill_handle = 0x135;
1319 } else
1320 sony_rfkill_handle = 0x124;
Mattia Dongili528809c2009-12-17 00:08:36 +09001321 dprintk("Found rkfill handle: 0x%.4x\n", sony_rfkill_handle);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001322
Mattia Dongili528809c2009-12-17 00:08:36 +09001323 /* need to read the whole buffer returned by the acpi call to SN06
1324 * here otherwise we may miss some features
1325 */
1326 params.count = 1;
1327 params.pointer = &in_obj;
1328 in_obj.type = ACPI_TYPE_INTEGER;
1329 in_obj.integer.value = offset;
1330 status = acpi_evaluate_object(sony_nc_acpi_handle, "SN06", &params,
1331 &buffer);
1332 if (ACPI_FAILURE(status)) {
1333 dprintk("Radio device enumeration failed\n");
1334 return;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001335 }
1336
Mattia Dongili528809c2009-12-17 00:08:36 +09001337 device_enum = (union acpi_object *) buffer.pointer;
Dan Carpenter200140b2011-02-27 17:13:25 +03001338 if (!device_enum) {
1339 pr_err(DRV_PFX "No SN06 return object.");
1340 goto out_no_enum;
1341 }
1342 if (device_enum->type != ACPI_TYPE_BUFFER) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001343 pr_err(DRV_PFX "Invalid SN06 return object 0x%.2x\n",
1344 device_enum->type);
Mattia Dongili528809c2009-12-17 00:08:36 +09001345 goto out_no_enum;
1346 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001347
Mattia Dongili528809c2009-12-17 00:08:36 +09001348 /* the buffer is filled with magic numbers describing the devices
1349 * available, 0xff terminates the enumeration
1350 */
Dmitry Torokhovc14973f2010-01-10 00:15:44 -08001351 for (i = 0; i < device_enum->buffer.length; i++) {
1352
1353 dev_code = *(device_enum->buffer.pointer + i);
1354 if (dev_code == 0xff)
1355 break;
1356
Mattia Dongili528809c2009-12-17 00:08:36 +09001357 dprintk("Radio devices, looking at 0x%.2x\n", dev_code);
1358
1359 if (dev_code == 0 && !sony_rfkill_devices[SONY_WIFI])
1360 sony_nc_setup_rfkill(device, SONY_WIFI);
1361
1362 if (dev_code == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
1363 sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
1364
1365 if ((0xf0 & dev_code) == 0x20 &&
1366 !sony_rfkill_devices[SONY_WWAN])
1367 sony_nc_setup_rfkill(device, SONY_WWAN);
1368
1369 if (dev_code == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
1370 sony_nc_setup_rfkill(device, SONY_WIMAX);
1371 }
1372
1373out_no_enum:
1374 kfree(buffer.pointer);
1375 return;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001376}
1377
Mattia Dongilibf155712011-02-19 11:52:31 +09001378/* Keyboard backlight feature */
1379#define KBDBL_HANDLER 0x137
1380#define KBDBL_PRESENT 0xB00
1381#define SET_MODE 0xC00
Marco Chiapperodf410d52011-04-05 23:38:34 +09001382#define SET_STATE 0xD00
Mattia Dongilibf155712011-02-19 11:52:31 +09001383#define SET_TIMEOUT 0xE00
1384
1385struct kbd_backlight {
1386 int mode;
1387 int timeout;
1388 struct device_attribute mode_attr;
1389 struct device_attribute timeout_attr;
1390};
1391
Dan Carpenterf11113b2011-02-26 15:54:27 +03001392static struct kbd_backlight *kbdbl_handle;
Mattia Dongilibf155712011-02-19 11:52:31 +09001393
1394static ssize_t __sony_nc_kbd_backlight_mode_set(u8 value)
1395{
1396 int result;
1397
1398 if (value > 1)
1399 return -EINVAL;
1400
1401 if (sony_call_snc_handle(KBDBL_HANDLER,
1402 (value << 0x10) | SET_MODE, &result))
1403 return -EIO;
1404
Marco Chiapperodf410d52011-04-05 23:38:34 +09001405 /* Try to turn the light on/off immediately */
1406 sony_call_snc_handle(KBDBL_HANDLER, (value << 0x10) | SET_STATE,
1407 &result);
1408
Mattia Dongilibf155712011-02-19 11:52:31 +09001409 kbdbl_handle->mode = value;
1410
1411 return 0;
1412}
1413
1414static ssize_t sony_nc_kbd_backlight_mode_store(struct device *dev,
1415 struct device_attribute *attr,
1416 const char *buffer, size_t count)
1417{
1418 int ret = 0;
1419 unsigned long value;
1420
1421 if (count > 31)
1422 return -EINVAL;
1423
1424 if (strict_strtoul(buffer, 10, &value))
1425 return -EINVAL;
1426
1427 ret = __sony_nc_kbd_backlight_mode_set(value);
1428 if (ret < 0)
1429 return ret;
1430
1431 return count;
1432}
1433
1434static ssize_t sony_nc_kbd_backlight_mode_show(struct device *dev,
1435 struct device_attribute *attr, char *buffer)
1436{
1437 ssize_t count = 0;
1438 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_handle->mode);
1439 return count;
1440}
1441
1442static int __sony_nc_kbd_backlight_timeout_set(u8 value)
1443{
1444 int result;
1445
1446 if (value > 3)
1447 return -EINVAL;
1448
1449 if (sony_call_snc_handle(KBDBL_HANDLER,
1450 (value << 0x10) | SET_TIMEOUT, &result))
1451 return -EIO;
1452
1453 kbdbl_handle->timeout = value;
1454
1455 return 0;
1456}
1457
1458static ssize_t sony_nc_kbd_backlight_timeout_store(struct device *dev,
1459 struct device_attribute *attr,
1460 const char *buffer, size_t count)
1461{
1462 int ret = 0;
1463 unsigned long value;
1464
1465 if (count > 31)
1466 return -EINVAL;
1467
1468 if (strict_strtoul(buffer, 10, &value))
1469 return -EINVAL;
1470
1471 ret = __sony_nc_kbd_backlight_timeout_set(value);
1472 if (ret < 0)
1473 return ret;
1474
1475 return count;
1476}
1477
1478static ssize_t sony_nc_kbd_backlight_timeout_show(struct device *dev,
1479 struct device_attribute *attr, char *buffer)
1480{
1481 ssize_t count = 0;
1482 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_handle->timeout);
1483 return count;
1484}
1485
1486static int sony_nc_kbd_backlight_setup(struct platform_device *pd)
1487{
1488 int result;
1489
Marco Chiapperodf410d52011-04-05 23:38:34 +09001490 if (sony_call_snc_handle(KBDBL_HANDLER, KBDBL_PRESENT, &result))
Mattia Dongilibf155712011-02-19 11:52:31 +09001491 return 0;
1492 if (!(result & 0x02))
1493 return 0;
1494
1495 kbdbl_handle = kzalloc(sizeof(*kbdbl_handle), GFP_KERNEL);
Dan Carpenter31f00752011-02-26 15:55:24 +03001496 if (!kbdbl_handle)
1497 return -ENOMEM;
Mattia Dongilibf155712011-02-19 11:52:31 +09001498
1499 sysfs_attr_init(&kbdbl_handle->mode_attr.attr);
1500 kbdbl_handle->mode_attr.attr.name = "kbd_backlight";
1501 kbdbl_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
1502 kbdbl_handle->mode_attr.show = sony_nc_kbd_backlight_mode_show;
1503 kbdbl_handle->mode_attr.store = sony_nc_kbd_backlight_mode_store;
1504
1505 sysfs_attr_init(&kbdbl_handle->timeout_attr.attr);
1506 kbdbl_handle->timeout_attr.attr.name = "kbd_backlight_timeout";
1507 kbdbl_handle->timeout_attr.attr.mode = S_IRUGO | S_IWUSR;
1508 kbdbl_handle->timeout_attr.show = sony_nc_kbd_backlight_timeout_show;
1509 kbdbl_handle->timeout_attr.store = sony_nc_kbd_backlight_timeout_store;
1510
1511 if (device_create_file(&pd->dev, &kbdbl_handle->mode_attr))
1512 goto outkzalloc;
1513
1514 if (device_create_file(&pd->dev, &kbdbl_handle->timeout_attr))
1515 goto outmode;
1516
1517 __sony_nc_kbd_backlight_mode_set(kbd_backlight);
1518 __sony_nc_kbd_backlight_timeout_set(kbd_backlight_timeout);
1519
1520 return 0;
1521
1522outmode:
1523 device_remove_file(&pd->dev, &kbdbl_handle->mode_attr);
1524outkzalloc:
1525 kfree(kbdbl_handle);
1526 kbdbl_handle = NULL;
1527 return -1;
1528}
1529
1530static int sony_nc_kbd_backlight_cleanup(struct platform_device *pd)
1531{
1532 if (kbdbl_handle) {
Marco Chiapperodf410d52011-04-05 23:38:34 +09001533 int result;
1534
Mattia Dongilibf155712011-02-19 11:52:31 +09001535 device_remove_file(&pd->dev, &kbdbl_handle->mode_attr);
1536 device_remove_file(&pd->dev, &kbdbl_handle->timeout_attr);
Marco Chiapperodf410d52011-04-05 23:38:34 +09001537
1538 /* restore the default hw behaviour */
1539 sony_call_snc_handle(KBDBL_HANDLER, 0x1000 | SET_MODE, &result);
1540 sony_call_snc_handle(KBDBL_HANDLER, SET_TIMEOUT, &result);
1541
Mattia Dongilibf155712011-02-19 11:52:31 +09001542 kfree(kbdbl_handle);
1543 }
1544 return 0;
1545}
1546
Marco Chiapperodf410d52011-04-05 23:38:34 +09001547static void sony_nc_kbd_backlight_resume(void)
1548{
1549 int ignore = 0;
1550
1551 if (!kbdbl_handle)
1552 return;
1553
1554 if (kbdbl_handle->mode == 0)
1555 sony_call_snc_handle(KBDBL_HANDLER, SET_MODE, &ignore);
1556
1557 if (kbdbl_handle->timeout != 0)
1558 sony_call_snc_handle(KBDBL_HANDLER,
1559 (kbdbl_handle->timeout << 0x10) | SET_TIMEOUT,
1560 &ignore);
1561}
1562
Mattia Dongili62d2f232011-05-09 10:20:29 -04001563static void sony_nc_backlight_ng_read_limits(int handle,
1564 struct sony_backlight_props *props)
1565{
1566 int offset;
1567 acpi_status status;
1568 u8 brlvl, i;
1569 u8 min = 0xff, max = 0x00;
1570 struct acpi_object_list params;
1571 union acpi_object in_obj;
1572 union acpi_object *lvl_enum;
1573 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1574
1575 props->handle = handle;
1576 props->offset = 0;
1577 props->maxlvl = 0xff;
1578
1579 offset = sony_find_snc_handle(handle);
1580 if (offset < 0)
1581 return;
1582
1583 /* try to read the boundaries from ACPI tables, if we fail the above
1584 * defaults should be reasonable
1585 */
1586 params.count = 1;
1587 params.pointer = &in_obj;
1588 in_obj.type = ACPI_TYPE_INTEGER;
1589 in_obj.integer.value = offset;
1590 status = acpi_evaluate_object(sony_nc_acpi_handle, "SN06", &params,
1591 &buffer);
1592 if (ACPI_FAILURE(status))
1593 return;
1594
1595 lvl_enum = (union acpi_object *) buffer.pointer;
1596 if (!lvl_enum) {
1597 pr_err("No SN06 return object.");
1598 return;
1599 }
1600 if (lvl_enum->type != ACPI_TYPE_BUFFER) {
1601 pr_err("Invalid SN06 return object 0x%.2x\n",
1602 lvl_enum->type);
1603 goto out_invalid;
1604 }
1605
1606 /* the buffer lists brightness levels available, brightness levels are
1607 * from 0 to 8 in the array, other values are used by ALS control.
1608 */
1609 for (i = 0; i < 9 && i < lvl_enum->buffer.length; i++) {
1610
1611 brlvl = *(lvl_enum->buffer.pointer + i);
1612 dprintk("Brightness level: %d\n", brlvl);
1613
1614 if (!brlvl)
1615 break;
1616
1617 if (brlvl > max)
1618 max = brlvl;
1619 if (brlvl < min)
1620 min = brlvl;
1621 }
1622 props->offset = min;
1623 props->maxlvl = max;
1624 dprintk("Brightness levels: min=%d max=%d\n", props->offset,
1625 props->maxlvl);
1626
1627out_invalid:
1628 kfree(buffer.pointer);
1629 return;
1630}
1631
Mattia Dongili7751ab82011-02-19 11:52:32 +09001632static void sony_nc_backlight_setup(void)
1633{
1634 acpi_handle unused;
1635 int max_brightness = 0;
1636 const struct backlight_ops *ops = NULL;
1637 struct backlight_properties props;
1638
1639 if (sony_find_snc_handle(0x12f) != -1) {
Mattia Dongili7751ab82011-02-19 11:52:32 +09001640 ops = &sony_backlight_ng_ops;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001641 sony_nc_backlight_ng_read_limits(0x12f, &sony_bl_props);
1642 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001643
1644 } else if (sony_find_snc_handle(0x137) != -1) {
Mattia Dongili7751ab82011-02-19 11:52:32 +09001645 ops = &sony_backlight_ng_ops;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001646 sony_nc_backlight_ng_read_limits(0x137, &sony_bl_props);
1647 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001648
1649 } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
1650 &unused))) {
1651 ops = &sony_backlight_ops;
1652 max_brightness = SONY_MAX_BRIGHTNESS - 1;
1653
1654 } else
1655 return;
1656
1657 memset(&props, 0, sizeof(struct backlight_properties));
1658 props.type = BACKLIGHT_PLATFORM;
1659 props.max_brightness = max_brightness;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001660 sony_bl_props.dev = backlight_device_register("sony", NULL,
1661 &sony_bl_props,
1662 ops, &props);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001663
Mattia Dongili62d2f232011-05-09 10:20:29 -04001664 if (IS_ERR(sony_bl_props.dev)) {
1665 pr_warn(DRV_PFX "unable to register backlight device\n");
1666 sony_bl_props.dev = NULL;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001667 } else
Mattia Dongili62d2f232011-05-09 10:20:29 -04001668 sony_bl_props.dev->props.brightness =
1669 ops->get_brightness(sony_bl_props.dev);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001670}
1671
1672static void sony_nc_backlight_cleanup(void)
1673{
Mattia Dongili62d2f232011-05-09 10:20:29 -04001674 if (sony_bl_props.dev)
1675 backlight_device_unregister(sony_bl_props.dev);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001676}
1677
malattia@linux.it59b19102007-04-09 10:19:04 +02001678static int sony_nc_add(struct acpi_device *device)
Stelian Pop7f09c432007-01-13 23:04:31 +01001679{
1680 acpi_status status;
Andrew Morton8607c672007-03-06 02:29:42 -08001681 int result = 0;
Alessandro Guido50f62af2007-01-13 23:04:34 +01001682 acpi_handle handle;
malattia@linux.it56b87562007-04-09 10:19:05 +02001683 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01001684
Mattia Dongilid6697932011-02-19 11:52:28 +09001685 pr_info(DRV_PFX "%s v%s.\n", SONY_NC_DRIVER_NAME,
1686 SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.itf6119b02007-04-09 19:31:06 +02001687
malattia@linux.it59b19102007-04-09 10:19:04 +02001688 sony_nc_acpi_device = device;
malattia@linux.it33a04452007-04-09 19:26:03 +02001689 strcpy(acpi_device_class(device), "sony/hotkey");
Stelian Popc5611622007-01-13 23:04:37 +01001690
malattia@linux.it59b19102007-04-09 10:19:04 +02001691 sony_nc_acpi_handle = device->handle;
Stelian Pop7f09c432007-01-13 23:04:31 +01001692
Mattia Dongilib25b7322007-07-16 02:34:36 +09001693 /* read device status */
1694 result = acpi_bus_get_status(device);
1695 /* bail IFF the above call was successful and the device is not present */
1696 if (!result && !device->status.present) {
1697 dprintk("Device not present\n");
1698 result = -ENODEV;
1699 goto outwalk;
1700 }
1701
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001702 result = sony_pf_add();
1703 if (result)
1704 goto outpresent;
1705
Stelian Pop7f09c432007-01-13 23:04:31 +01001706 if (debug) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001707 status = acpi_walk_namespace(ACPI_TYPE_METHOD,
1708 sony_nc_acpi_handle, 1, sony_walk_callback,
1709 NULL, NULL, NULL);
Stelian Pop7f09c432007-01-13 23:04:31 +01001710 if (ACPI_FAILURE(status)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001711 pr_warn(DRV_PFX "unable to walk acpi resources\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001712 result = -ENODEV;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001713 goto outpresent;
Stelian Pop7f09c432007-01-13 23:04:31 +01001714 }
Stelian Popc5611622007-01-13 23:04:37 +01001715 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001716
Matthew Garrett82734bf2009-03-26 21:58:13 +09001717 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1718 &handle))) {
1719 if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
1720 dprintk("ECON Method failed\n");
1721 }
1722
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001723 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1724 &handle))) {
1725 dprintk("Doing SNC setup\n");
Dan Carpenter7227ded2011-02-26 15:54:57 +03001726 result = sony_nc_handles_setup(sony_pf_device);
1727 if (result)
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001728 goto outpresent;
Dan Carpenter7227ded2011-02-26 15:54:57 +03001729 result = sony_nc_kbd_backlight_setup(sony_pf_device);
1730 if (result)
Mattia Dongilibf155712011-02-19 11:52:31 +09001731 goto outsnc;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001732 sony_nc_function_setup(device);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001733 sony_nc_rfkill_setup(device);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001734 }
1735
malattia@linux.it1549ee62007-04-09 10:19:08 +02001736 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05001737 result = sony_laptop_setup_input(device);
malattia@linux.it1549ee62007-04-09 10:19:08 +02001738 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001739 pr_err(DRV_PFX "Unable to create input devices.\n");
Mattia Dongilibf155712011-02-19 11:52:31 +09001740 goto outkbdbacklight;
malattia@linux.it1549ee62007-04-09 10:19:08 +02001741 }
1742
Alessandro Guido38cfc142008-11-12 23:03:28 +01001743 if (acpi_video_backlight_support()) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001744 pr_info(DRV_PFX "brightness ignored, must be "
Thomas Renninger540b8bb2008-08-01 17:38:02 +02001745 "controlled by ACPI video driver\n");
Mattia Dongili7751ab82011-02-19 11:52:32 +09001746 } else {
1747 sony_nc_backlight_setup();
Alessandro Guido50f62af2007-01-13 23:04:34 +01001748 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001749
malattia@linux.it56b87562007-04-09 10:19:05 +02001750 /* create sony_pf sysfs attributes related to the SNC device */
1751 for (item = sony_nc_values; item->name; ++item) {
1752
1753 if (!debug && item->debug)
1754 continue;
1755
1756 /* find the available acpiget as described in the DSDT */
1757 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
1758 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1759 *item->acpiget,
1760 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02001761 dprintk("Found %s getter: %s\n",
1762 item->name, *item->acpiget);
malattia@linux.it56b87562007-04-09 10:19:05 +02001763 item->devattr.attr.mode |= S_IRUGO;
1764 break;
1765 }
1766 }
1767
1768 /* find the available acpiset as described in the DSDT */
1769 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
1770 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1771 *item->acpiset,
1772 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02001773 dprintk("Found %s setter: %s\n",
1774 item->name, *item->acpiset);
malattia@linux.it56b87562007-04-09 10:19:05 +02001775 item->devattr.attr.mode |= S_IWUSR;
1776 break;
1777 }
1778 }
1779
1780 if (item->devattr.attr.mode != 0) {
1781 result =
1782 device_create_file(&sony_pf_device->dev,
1783 &item->devattr);
1784 if (result)
1785 goto out_sysfs;
1786 }
1787 }
1788
Stelian Pop7f09c432007-01-13 23:04:31 +01001789 return 0;
1790
malattia@linux.it56b87562007-04-09 10:19:05 +02001791 out_sysfs:
1792 for (item = sony_nc_values; item->name; ++item) {
1793 device_remove_file(&sony_pf_device->dev, &item->devattr);
1794 }
Mattia Dongili7751ab82011-02-19 11:52:32 +09001795 sony_nc_backlight_cleanup();
Mattia Dongili7df03b82007-01-13 23:04:41 +01001796
malattia@linux.it1549ee62007-04-09 10:19:08 +02001797 sony_laptop_remove_input();
1798
Mattia Dongilibf155712011-02-19 11:52:31 +09001799 outkbdbacklight:
1800 sony_nc_kbd_backlight_cleanup(sony_pf_device);
1801
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001802 outsnc:
1803 sony_nc_handles_cleanup(sony_pf_device);
1804
1805 outpresent:
1806 sony_pf_remove();
1807
Len Browna02d1c12007-02-07 15:34:02 -05001808 outwalk:
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001809 sony_nc_rfkill_cleanup();
Stelian Pop7f09c432007-01-13 23:04:31 +01001810 return result;
1811}
1812
malattia@linux.it59b19102007-04-09 10:19:04 +02001813static int sony_nc_remove(struct acpi_device *device, int type)
Stelian Pop7f09c432007-01-13 23:04:31 +01001814{
malattia@linux.it56b87562007-04-09 10:19:05 +02001815 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01001816
Mattia Dongili7751ab82011-02-19 11:52:32 +09001817 sony_nc_backlight_cleanup();
Alessandro Guido50f62af2007-01-13 23:04:34 +01001818
malattia@linux.it59b19102007-04-09 10:19:04 +02001819 sony_nc_acpi_device = NULL;
Stelian Popc5611622007-01-13 23:04:37 +01001820
malattia@linux.it56b87562007-04-09 10:19:05 +02001821 for (item = sony_nc_values; item->name; ++item) {
1822 device_remove_file(&sony_pf_device->dev, &item->devattr);
1823 }
1824
Mattia Dongilibf155712011-02-19 11:52:31 +09001825 sony_nc_kbd_backlight_cleanup(sony_pf_device);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001826 sony_nc_handles_cleanup(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +02001827 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +02001828 sony_laptop_remove_input();
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001829 sony_nc_rfkill_cleanup();
malattia@linux.itf6119b02007-04-09 19:31:06 +02001830 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001831
1832 return 0;
1833}
1834
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001835static const struct acpi_device_id sony_device_ids[] = {
1836 {SONY_NC_HID, 0},
1837 {SONY_PIC_HID, 0},
1838 {"", 0},
1839};
1840MODULE_DEVICE_TABLE(acpi, sony_device_ids);
1841
1842static const struct acpi_device_id sony_nc_device_ids[] = {
1843 {SONY_NC_HID, 0},
1844 {"", 0},
1845};
1846
malattia@linux.it59b19102007-04-09 10:19:04 +02001847static struct acpi_driver sony_nc_driver = {
1848 .name = SONY_NC_DRIVER_NAME,
1849 .class = SONY_NC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001850 .ids = sony_nc_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02001851 .owner = THIS_MODULE,
Len Browna02d1c12007-02-07 15:34:02 -05001852 .ops = {
malattia@linux.it59b19102007-04-09 10:19:04 +02001853 .add = sony_nc_add,
1854 .remove = sony_nc_remove,
1855 .resume = sony_nc_resume,
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001856 .notify = sony_nc_notify,
Len Browna02d1c12007-02-07 15:34:02 -05001857 },
Andrew Morton3f4f4612007-01-13 23:04:32 +01001858};
1859
malattia@linux.it33a04452007-04-09 19:26:03 +02001860/*********** SPIC (SNY6001) Device ***********/
1861
1862#define SONYPI_DEVICE_TYPE1 0x00000001
1863#define SONYPI_DEVICE_TYPE2 0x00000002
1864#define SONYPI_DEVICE_TYPE3 0x00000004
1865
Mattia Dongili22a17782007-07-16 02:34:39 +09001866#define SONYPI_TYPE1_OFFSET 0x04
1867#define SONYPI_TYPE2_OFFSET 0x12
1868#define SONYPI_TYPE3_OFFSET 0x12
malattia@linux.it33a04452007-04-09 19:26:03 +02001869
malattia@linux.it33a04452007-04-09 19:26:03 +02001870struct sony_pic_ioport {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001871 struct acpi_resource_io io1;
1872 struct acpi_resource_io io2;
malattia@linux.it33a04452007-04-09 19:26:03 +02001873 struct list_head list;
1874};
1875
1876struct sony_pic_irq {
1877 struct acpi_resource_irq irq;
1878 struct list_head list;
1879};
1880
Mattia Dongilide920432008-01-14 18:05:43 +09001881struct sonypi_eventtypes {
1882 u8 data;
1883 unsigned long mask;
1884 struct sonypi_event *events;
1885};
1886
malattia@linux.it33a04452007-04-09 19:26:03 +02001887struct sony_pic_dev {
Mattia Dongili31df7142009-09-16 00:05:29 +09001888 struct acpi_device *acpi_dev;
1889 struct sony_pic_irq *cur_irq;
1890 struct sony_pic_ioport *cur_ioport;
1891 struct list_head interrupts;
1892 struct list_head ioports;
1893 struct mutex lock;
1894 struct sonypi_eventtypes *event_types;
1895 int (*handle_irq)(const u8, const u8);
1896 int model;
1897 u16 evport_offset;
1898 u8 camera_power;
1899 u8 bluetooth_power;
1900 u8 wwan_power;
malattia@linux.it33a04452007-04-09 19:26:03 +02001901};
1902
1903static struct sony_pic_dev spic_dev = {
1904 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
1905 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
1906};
1907
Alan Jenkins5e6f9722009-09-16 00:05:32 +09001908static int spic_drv_registered;
1909
malattia@linux.it33a04452007-04-09 19:26:03 +02001910/* Event masks */
1911#define SONYPI_JOGGER_MASK 0x00000001
1912#define SONYPI_CAPTURE_MASK 0x00000002
1913#define SONYPI_FNKEY_MASK 0x00000004
1914#define SONYPI_BLUETOOTH_MASK 0x00000008
1915#define SONYPI_PKEY_MASK 0x00000010
1916#define SONYPI_BACK_MASK 0x00000020
1917#define SONYPI_HELP_MASK 0x00000040
1918#define SONYPI_LID_MASK 0x00000080
1919#define SONYPI_ZOOM_MASK 0x00000100
1920#define SONYPI_THUMBPHRASE_MASK 0x00000200
1921#define SONYPI_MEYE_MASK 0x00000400
1922#define SONYPI_MEMORYSTICK_MASK 0x00000800
1923#define SONYPI_BATTERY_MASK 0x00001000
1924#define SONYPI_WIRELESS_MASK 0x00002000
1925
1926struct sonypi_event {
1927 u8 data;
1928 u8 event;
1929};
1930
1931/* The set of possible button release events */
1932static struct sonypi_event sonypi_releaseev[] = {
1933 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
1934 { 0, 0 }
1935};
1936
1937/* The set of possible jogger events */
1938static struct sonypi_event sonypi_joggerev[] = {
1939 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
1940 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
1941 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
1942 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
1943 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
1944 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
1945 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
1946 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
1947 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
1948 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
1949 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
1950 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
1951 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
1952 { 0, 0 }
1953};
1954
1955/* The set of possible capture button events */
1956static struct sonypi_event sonypi_captureev[] = {
1957 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
1958 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09001959 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02001960 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
1961 { 0, 0 }
1962};
1963
1964/* The set of possible fnkeys events */
1965static struct sonypi_event sonypi_fnkeyev[] = {
1966 { 0x10, SONYPI_EVENT_FNKEY_ESC },
1967 { 0x11, SONYPI_EVENT_FNKEY_F1 },
1968 { 0x12, SONYPI_EVENT_FNKEY_F2 },
1969 { 0x13, SONYPI_EVENT_FNKEY_F3 },
1970 { 0x14, SONYPI_EVENT_FNKEY_F4 },
1971 { 0x15, SONYPI_EVENT_FNKEY_F5 },
1972 { 0x16, SONYPI_EVENT_FNKEY_F6 },
1973 { 0x17, SONYPI_EVENT_FNKEY_F7 },
1974 { 0x18, SONYPI_EVENT_FNKEY_F8 },
1975 { 0x19, SONYPI_EVENT_FNKEY_F9 },
1976 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
1977 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
1978 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
1979 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
1980 { 0x21, SONYPI_EVENT_FNKEY_1 },
1981 { 0x22, SONYPI_EVENT_FNKEY_2 },
1982 { 0x31, SONYPI_EVENT_FNKEY_D },
1983 { 0x32, SONYPI_EVENT_FNKEY_E },
1984 { 0x33, SONYPI_EVENT_FNKEY_F },
1985 { 0x34, SONYPI_EVENT_FNKEY_S },
1986 { 0x35, SONYPI_EVENT_FNKEY_B },
1987 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
1988 { 0, 0 }
1989};
1990
1991/* The set of possible program key events */
1992static struct sonypi_event sonypi_pkeyev[] = {
1993 { 0x01, SONYPI_EVENT_PKEY_P1 },
1994 { 0x02, SONYPI_EVENT_PKEY_P2 },
1995 { 0x04, SONYPI_EVENT_PKEY_P3 },
Harald Jenny1cae7102009-03-26 21:58:18 +09001996 { 0x20, SONYPI_EVENT_PKEY_P1 },
malattia@linux.it33a04452007-04-09 19:26:03 +02001997 { 0, 0 }
1998};
1999
2000/* The set of possible bluetooth events */
2001static struct sonypi_event sonypi_blueev[] = {
2002 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
2003 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
2004 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
2005 { 0, 0 }
2006};
2007
2008/* The set of possible wireless events */
2009static struct sonypi_event sonypi_wlessev[] = {
Mattia Dongili4eeb5022011-02-19 11:52:27 +09002010 { 0x59, SONYPI_EVENT_IGNORE },
2011 { 0x5a, SONYPI_EVENT_IGNORE },
malattia@linux.it33a04452007-04-09 19:26:03 +02002012 { 0, 0 }
2013};
2014
2015/* The set of possible back button events */
2016static struct sonypi_event sonypi_backev[] = {
2017 { 0x20, SONYPI_EVENT_BACK_PRESSED },
2018 { 0, 0 }
2019};
2020
2021/* The set of possible help button events */
2022static struct sonypi_event sonypi_helpev[] = {
2023 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
2024 { 0, 0 }
2025};
2026
2027
2028/* The set of possible lid events */
2029static struct sonypi_event sonypi_lidev[] = {
2030 { 0x51, SONYPI_EVENT_LID_CLOSED },
2031 { 0x50, SONYPI_EVENT_LID_OPENED },
2032 { 0, 0 }
2033};
2034
2035/* The set of possible zoom events */
2036static struct sonypi_event sonypi_zoomev[] = {
2037 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002038 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
2039 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
Harald Jenny1cae7102009-03-26 21:58:18 +09002040 { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02002041 { 0, 0 }
2042};
2043
2044/* The set of possible thumbphrase events */
2045static struct sonypi_event sonypi_thumbphraseev[] = {
2046 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
2047 { 0, 0 }
2048};
2049
2050/* The set of possible motioneye camera events */
2051static struct sonypi_event sonypi_meyeev[] = {
2052 { 0x00, SONYPI_EVENT_MEYE_FACE },
2053 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
2054 { 0, 0 }
2055};
2056
2057/* The set of possible memorystick events */
2058static struct sonypi_event sonypi_memorystickev[] = {
2059 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
2060 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
2061 { 0, 0 }
2062};
2063
2064/* The set of possible battery events */
2065static struct sonypi_event sonypi_batteryev[] = {
2066 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
2067 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
2068 { 0, 0 }
2069};
2070
Harald Jenny1cae7102009-03-26 21:58:18 +09002071/* The set of possible volume events */
2072static struct sonypi_event sonypi_volumeev[] = {
2073 { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
2074 { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
2075 { 0, 0 }
2076};
2077
2078/* The set of possible brightness events */
2079static struct sonypi_event sonypi_brightnessev[] = {
2080 { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
2081 { 0, 0 }
2082};
2083
Mattia Dongilide920432008-01-14 18:05:43 +09002084static struct sonypi_eventtypes type1_events[] = {
2085 { 0, 0xffffffff, sonypi_releaseev },
2086 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
2087 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
2088 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
2089 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
2090 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2091 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
2092 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
2093 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2094 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
2095 { 0 },
2096};
2097static struct sonypi_eventtypes type2_events[] = {
2098 { 0, 0xffffffff, sonypi_releaseev },
2099 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
2100 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
2101 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
2102 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2103 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
2104 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
2105 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
2106 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
2107 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
2108 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
2109 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2110 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
2111 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
2112 { 0 },
2113};
2114static struct sonypi_eventtypes type3_events[] = {
2115 { 0, 0xffffffff, sonypi_releaseev },
2116 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2117 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
2118 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2119 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
2120 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002121 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
2122 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
2123 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
Harald Jenny1cae7102009-03-26 21:58:18 +09002124 { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
2125 { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002126 { 0 },
Mattia Dongilide920432008-01-14 18:05:43 +09002127};
2128
Mattia Dongili3eb87492008-01-14 18:05:45 +09002129/* low level spic calls */
malattia@linux.it33a04452007-04-09 19:26:03 +02002130#define ITERATIONS_LONG 10000
2131#define ITERATIONS_SHORT 10
2132#define wait_on_command(command, iterations) { \
2133 unsigned int n = iterations; \
2134 while (--n && (command)) \
2135 udelay(1); \
2136 if (!n) \
2137 dprintk("command failed at %s : %s (line %d)\n", \
Harvey Harrison6e574192008-04-29 00:59:20 -07002138 __FILE__, __func__, __LINE__); \
malattia@linux.it33a04452007-04-09 19:26:03 +02002139}
2140
2141static u8 sony_pic_call1(u8 dev)
2142{
2143 u8 v1, v2;
2144
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002145 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002146 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002147 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2148 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
2149 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002150 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02002151 return v2;
2152}
2153
2154static u8 sony_pic_call2(u8 dev, u8 fn)
2155{
2156 u8 v1;
2157
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002158 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002159 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002160 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2161 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002162 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002163 outb(fn, spic_dev.cur_ioport->io1.minimum);
2164 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002165 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02002166 return v1;
2167}
2168
malattia@linux.it49a11de2007-04-09 19:28:56 +02002169static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
2170{
2171 u8 v1;
2172
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002173 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2174 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2175 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2176 outb(fn, spic_dev.cur_ioport->io1.minimum);
2177 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2178 outb(v, spic_dev.cur_ioport->io1.minimum);
2179 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002180 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
2181 dev, fn, v, v1);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002182 return v1;
2183}
2184
Mattia Dongili3eb87492008-01-14 18:05:45 +09002185/*
2186 * minidrivers for SPIC models
2187 */
Mattia Dongilie93c8a62009-03-26 21:58:17 +09002188static int type3_handle_irq(const u8 data_mask, const u8 ev)
Mattia Dongili3eb87492008-01-14 18:05:45 +09002189{
2190 /*
2191 * 0x31 could mean we have to take some extra action and wait for
Mattia Dongilie93c8a62009-03-26 21:58:17 +09002192 * the next irq for some Type3 models, it will generate a new
Mattia Dongili3eb87492008-01-14 18:05:45 +09002193 * irq and we can read new data from the device:
2194 * - 0x5c and 0x5f requires 0xA0
2195 * - 0x61 requires 0xB3
2196 */
2197 if (data_mask == 0x31) {
2198 if (ev == 0x5c || ev == 0x5f)
2199 sony_pic_call1(0xA0);
2200 else if (ev == 0x61)
2201 sony_pic_call1(0xB3);
2202 return 0;
2203 }
2204 return 1;
2205}
2206
Mattia Dongili3eb87492008-01-14 18:05:45 +09002207static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
2208{
2209 struct pci_dev *pcidev;
2210
2211 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2212 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
2213 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002214 dev->model = SONYPI_DEVICE_TYPE1;
2215 dev->evport_offset = SONYPI_TYPE1_OFFSET;
2216 dev->event_types = type1_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002217 goto out;
2218 }
2219
2220 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2221 PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
2222 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002223 dev->model = SONYPI_DEVICE_TYPE2;
2224 dev->evport_offset = SONYPI_TYPE2_OFFSET;
2225 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002226 goto out;
2227 }
2228
2229 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2230 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
2231 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002232 dev->model = SONYPI_DEVICE_TYPE3;
2233 dev->handle_irq = type3_handle_irq;
2234 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2235 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002236 goto out;
2237 }
2238
2239 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2240 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
2241 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002242 dev->model = SONYPI_DEVICE_TYPE3;
2243 dev->handle_irq = type3_handle_irq;
2244 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2245 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002246 goto out;
2247 }
2248
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09002249 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2250 PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
2251 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002252 dev->model = SONYPI_DEVICE_TYPE3;
2253 dev->handle_irq = type3_handle_irq;
2254 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2255 dev->event_types = type3_events;
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09002256 goto out;
2257 }
2258
Mattia Dongili3eb87492008-01-14 18:05:45 +09002259 /* default */
Mattia Dongili31df7142009-09-16 00:05:29 +09002260 dev->model = SONYPI_DEVICE_TYPE2;
2261 dev->evport_offset = SONYPI_TYPE2_OFFSET;
2262 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002263
2264out:
2265 if (pcidev)
2266 pci_dev_put(pcidev);
2267
Mattia Dongilid6697932011-02-19 11:52:28 +09002268 pr_info(DRV_PFX "detected Type%d model\n",
Mattia Dongili31df7142009-09-16 00:05:29 +09002269 dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
2270 dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
Mattia Dongili3eb87492008-01-14 18:05:45 +09002271}
2272
malattia@linux.it49a11de2007-04-09 19:28:56 +02002273/* camera tests and poweron/poweroff */
2274#define SONYPI_CAMERA_PICTURE 5
malattia@linux.it49a11de2007-04-09 19:28:56 +02002275#define SONYPI_CAMERA_CONTROL 0x10
malattia@linux.ite3646322007-04-28 23:34:22 +09002276
2277#define SONYPI_CAMERA_BRIGHTNESS 0
2278#define SONYPI_CAMERA_CONTRAST 1
2279#define SONYPI_CAMERA_HUE 2
2280#define SONYPI_CAMERA_COLOR 3
2281#define SONYPI_CAMERA_SHARPNESS 4
2282
2283#define SONYPI_CAMERA_EXPOSURE_MASK 0xC
2284#define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
2285#define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
2286#define SONYPI_CAMERA_MUTE_MASK 0x40
2287
2288/* the rest don't need a loop until not 0xff */
2289#define SONYPI_CAMERA_AGC 6
2290#define SONYPI_CAMERA_AGC_MASK 0x30
2291#define SONYPI_CAMERA_SHUTTER_MASK 0x7
2292
2293#define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
2294#define SONYPI_CAMERA_CONTROL 0x10
2295
2296#define SONYPI_CAMERA_STATUS 7
2297#define SONYPI_CAMERA_STATUS_READY 0x2
2298#define SONYPI_CAMERA_STATUS_POSITION 0x4
2299
2300#define SONYPI_DIRECTION_BACKWARDS 0x4
2301
2302#define SONYPI_CAMERA_REVISION 8
2303#define SONYPI_CAMERA_ROMVERSION 9
malattia@linux.it49a11de2007-04-09 19:28:56 +02002304
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002305static int __sony_pic_camera_ready(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002306{
2307 u8 v;
2308
2309 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
2310 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
2311}
2312
malattia@linux.ite3646322007-04-28 23:34:22 +09002313static int __sony_pic_camera_off(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002314{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002315 if (!camera) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002316 pr_warn(DRV_PFX "camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002317 return -ENODEV;
2318 }
2319
malattia@linux.it49a11de2007-04-09 19:28:56 +02002320 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
2321 SONYPI_CAMERA_MUTE_MASK),
2322 ITERATIONS_SHORT);
2323
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002324 if (spic_dev.camera_power) {
2325 sony_pic_call2(0x91, 0);
2326 spic_dev.camera_power = 0;
2327 }
2328 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002329}
2330
malattia@linux.ite3646322007-04-28 23:34:22 +09002331static int __sony_pic_camera_on(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002332{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002333 int i, j, x;
2334
2335 if (!camera) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002336 pr_warn(DRV_PFX "camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002337 return -ENODEV;
2338 }
malattia@linux.it49a11de2007-04-09 19:28:56 +02002339
2340 if (spic_dev.camera_power)
malattia@linux.ite3646322007-04-28 23:34:22 +09002341 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002342
2343 for (j = 5; j > 0; j--) {
2344
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002345 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002346 msleep(10);
2347 sony_pic_call1(0x93);
2348
2349 for (i = 400; i > 0; i--) {
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002350 if (__sony_pic_camera_ready())
malattia@linux.it49a11de2007-04-09 19:28:56 +02002351 break;
2352 msleep(10);
2353 }
2354 if (i)
2355 break;
2356 }
2357
2358 if (j == 0) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002359 pr_warn(DRV_PFX "failed to power on camera\n");
malattia@linux.ite3646322007-04-28 23:34:22 +09002360 return -ENODEV;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002361 }
2362
2363 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
2364 0x5a),
2365 ITERATIONS_SHORT);
2366
2367 spic_dev.camera_power = 1;
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002368 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002369}
2370
malattia@linux.ite3646322007-04-28 23:34:22 +09002371/* External camera command (exported to the motion eye v4l driver) */
2372int sony_pic_camera_command(int command, u8 value)
2373{
2374 if (!camera)
2375 return -EIO;
2376
2377 mutex_lock(&spic_dev.lock);
2378
2379 switch (command) {
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002380 case SONY_PIC_COMMAND_SETCAMERA:
malattia@linux.ite3646322007-04-28 23:34:22 +09002381 if (value)
2382 __sony_pic_camera_on();
2383 else
2384 __sony_pic_camera_off();
2385 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002386 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09002387 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
2388 ITERATIONS_SHORT);
2389 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002390 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
malattia@linux.ite3646322007-04-28 23:34:22 +09002391 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
2392 ITERATIONS_SHORT);
2393 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002394 case SONY_PIC_COMMAND_SETCAMERAHUE:
malattia@linux.ite3646322007-04-28 23:34:22 +09002395 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
2396 ITERATIONS_SHORT);
2397 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002398 case SONY_PIC_COMMAND_SETCAMERACOLOR:
malattia@linux.ite3646322007-04-28 23:34:22 +09002399 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
2400 ITERATIONS_SHORT);
2401 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002402 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09002403 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
2404 ITERATIONS_SHORT);
2405 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002406 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
malattia@linux.ite3646322007-04-28 23:34:22 +09002407 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
2408 ITERATIONS_SHORT);
2409 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002410 case SONY_PIC_COMMAND_SETCAMERAAGC:
malattia@linux.ite3646322007-04-28 23:34:22 +09002411 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
2412 ITERATIONS_SHORT);
2413 break;
2414 default:
Mattia Dongilid6697932011-02-19 11:52:28 +09002415 pr_err(DRV_PFX "sony_pic_camera_command invalid: %d\n",
malattia@linux.ite3646322007-04-28 23:34:22 +09002416 command);
2417 break;
2418 }
2419 mutex_unlock(&spic_dev.lock);
2420 return 0;
2421}
2422EXPORT_SYMBOL(sony_pic_camera_command);
2423
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002424/* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002425static void __sony_pic_set_wwanpower(u8 state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002426{
2427 state = !!state;
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002428 if (spic_dev.wwan_power == state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002429 return;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002430 sony_pic_call2(0xB0, state);
Sergey Yanovich3ad1b762009-03-26 21:58:21 +09002431 sony_pic_call1(0x82);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002432 spic_dev.wwan_power = state;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002433}
2434
2435static ssize_t sony_pic_wwanpower_store(struct device *dev,
2436 struct device_attribute *attr,
2437 const char *buffer, size_t count)
2438{
2439 unsigned long value;
2440 if (count > 31)
2441 return -EINVAL;
2442
2443 value = simple_strtoul(buffer, NULL, 10);
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002444 mutex_lock(&spic_dev.lock);
2445 __sony_pic_set_wwanpower(value);
2446 mutex_unlock(&spic_dev.lock);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002447
2448 return count;
2449}
2450
2451static ssize_t sony_pic_wwanpower_show(struct device *dev,
2452 struct device_attribute *attr, char *buffer)
2453{
2454 ssize_t count;
2455 mutex_lock(&spic_dev.lock);
2456 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
2457 mutex_unlock(&spic_dev.lock);
2458 return count;
2459}
2460
malattia@linux.it49a11de2007-04-09 19:28:56 +02002461/* bluetooth subsystem power state */
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002462static void __sony_pic_set_bluetoothpower(u8 state)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002463{
2464 state = !!state;
2465 if (spic_dev.bluetooth_power == state)
2466 return;
2467 sony_pic_call2(0x96, state);
2468 sony_pic_call1(0x82);
2469 spic_dev.bluetooth_power = state;
2470}
2471
2472static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
2473 struct device_attribute *attr,
2474 const char *buffer, size_t count)
2475{
2476 unsigned long value;
2477 if (count > 31)
2478 return -EINVAL;
2479
2480 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002481 mutex_lock(&spic_dev.lock);
2482 __sony_pic_set_bluetoothpower(value);
2483 mutex_unlock(&spic_dev.lock);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002484
2485 return count;
2486}
2487
2488static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
2489 struct device_attribute *attr, char *buffer)
2490{
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002491 ssize_t count = 0;
2492 mutex_lock(&spic_dev.lock);
2493 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
2494 mutex_unlock(&spic_dev.lock);
2495 return count;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002496}
2497
2498/* fan speed */
2499/* FAN0 information (reverse engineered from ACPI tables) */
2500#define SONY_PIC_FAN0_STATUS 0x93
malattia@linux.it7b153f32007-04-09 19:31:25 +02002501static int sony_pic_set_fanspeed(unsigned long value)
2502{
2503 return ec_write(SONY_PIC_FAN0_STATUS, value);
2504}
2505
2506static int sony_pic_get_fanspeed(u8 *value)
2507{
2508 return ec_read(SONY_PIC_FAN0_STATUS, value);
2509}
2510
malattia@linux.it49a11de2007-04-09 19:28:56 +02002511static ssize_t sony_pic_fanspeed_store(struct device *dev,
2512 struct device_attribute *attr,
2513 const char *buffer, size_t count)
2514{
2515 unsigned long value;
2516 if (count > 31)
2517 return -EINVAL;
2518
2519 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002520 if (sony_pic_set_fanspeed(value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02002521 return -EIO;
2522
2523 return count;
2524}
2525
2526static ssize_t sony_pic_fanspeed_show(struct device *dev,
2527 struct device_attribute *attr, char *buffer)
2528{
2529 u8 value = 0;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002530 if (sony_pic_get_fanspeed(&value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02002531 return -EIO;
2532
2533 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
2534}
2535
2536#define SPIC_ATTR(_name, _mode) \
2537struct device_attribute spic_attr_##_name = __ATTR(_name, \
2538 _mode, sony_pic_## _name ##_show, \
2539 sony_pic_## _name ##_store)
2540
malattia@linux.it49a11de2007-04-09 19:28:56 +02002541static SPIC_ATTR(bluetoothpower, 0644);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002542static SPIC_ATTR(wwanpower, 0644);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002543static SPIC_ATTR(fanspeed, 0644);
2544
2545static struct attribute *spic_attributes[] = {
malattia@linux.it49a11de2007-04-09 19:28:56 +02002546 &spic_attr_bluetoothpower.attr,
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002547 &spic_attr_wwanpower.attr,
malattia@linux.it49a11de2007-04-09 19:28:56 +02002548 &spic_attr_fanspeed.attr,
2549 NULL
2550};
2551
2552static struct attribute_group spic_attribute_group = {
2553 .attrs = spic_attributes
2554};
2555
malattia@linux.it7b153f32007-04-09 19:31:25 +02002556/******** SONYPI compatibility **********/
Mattia Dongilia64e62a2007-05-01 11:19:53 +09002557#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +02002558
2559/* battery / brightness / temperature addresses */
2560#define SONYPI_BAT_FLAGS 0x81
2561#define SONYPI_LCD_LIGHT 0x96
2562#define SONYPI_BAT1_PCTRM 0xa0
2563#define SONYPI_BAT1_LEFT 0xa2
2564#define SONYPI_BAT1_MAXRT 0xa4
2565#define SONYPI_BAT2_PCTRM 0xa8
2566#define SONYPI_BAT2_LEFT 0xaa
2567#define SONYPI_BAT2_MAXRT 0xac
2568#define SONYPI_BAT1_MAXTK 0xb0
2569#define SONYPI_BAT1_FULL 0xb2
2570#define SONYPI_BAT2_MAXTK 0xb8
2571#define SONYPI_BAT2_FULL 0xba
2572#define SONYPI_TEMP_STATUS 0xC1
2573
2574struct sonypi_compat_s {
2575 struct fasync_struct *fifo_async;
Stefani Seibold45465482009-12-21 14:37:26 -08002576 struct kfifo fifo;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002577 spinlock_t fifo_lock;
2578 wait_queue_head_t fifo_proc_list;
2579 atomic_t open_count;
2580};
2581static struct sonypi_compat_s sonypi_compat = {
2582 .open_count = ATOMIC_INIT(0),
2583};
2584
2585static int sonypi_misc_fasync(int fd, struct file *filp, int on)
2586{
Jonathan Corbet60aa4922009-02-01 14:52:56 -07002587 return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002588}
2589
2590static int sonypi_misc_release(struct inode *inode, struct file *file)
2591{
malattia@linux.it7b153f32007-04-09 19:31:25 +02002592 atomic_dec(&sonypi_compat.open_count);
2593 return 0;
2594}
2595
2596static int sonypi_misc_open(struct inode *inode, struct file *file)
2597{
2598 /* Flush input queue on first open */
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002599 unsigned long flags;
2600
Stefani Seibold45465482009-12-21 14:37:26 -08002601 spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002602
malattia@linux.it7b153f32007-04-09 19:31:25 +02002603 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
Stefani Seibolde64c0262009-12-21 14:37:28 -08002604 kfifo_reset(&sonypi_compat.fifo);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002605
Stefani Seibold45465482009-12-21 14:37:26 -08002606 spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002607
malattia@linux.it7b153f32007-04-09 19:31:25 +02002608 return 0;
2609}
2610
2611static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
2612 size_t count, loff_t *pos)
2613{
2614 ssize_t ret;
2615 unsigned char c;
2616
Stefani Seibold45465482009-12-21 14:37:26 -08002617 if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
malattia@linux.it7b153f32007-04-09 19:31:25 +02002618 (file->f_flags & O_NONBLOCK))
2619 return -EAGAIN;
2620
2621 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
Stefani Seibold45465482009-12-21 14:37:26 -08002622 kfifo_len(&sonypi_compat.fifo) != 0);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002623 if (ret)
2624 return ret;
2625
2626 while (ret < count &&
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002627 (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002628 &sonypi_compat.fifo_lock) == sizeof(c))) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02002629 if (put_user(c, buf++))
2630 return -EFAULT;
2631 ret++;
2632 }
2633
2634 if (ret > 0) {
2635 struct inode *inode = file->f_path.dentry->d_inode;
2636 inode->i_atime = current_fs_time(inode->i_sb);
2637 }
2638
2639 return ret;
2640}
2641
2642static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
2643{
2644 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
Stefani Seibold45465482009-12-21 14:37:26 -08002645 if (kfifo_len(&sonypi_compat.fifo))
malattia@linux.it7b153f32007-04-09 19:31:25 +02002646 return POLLIN | POLLRDNORM;
2647 return 0;
2648}
2649
2650static int ec_read16(u8 addr, u16 *value)
2651{
2652 u8 val_lb, val_hb;
2653 if (ec_read(addr, &val_lb))
2654 return -1;
2655 if (ec_read(addr + 1, &val_hb))
2656 return -1;
2657 *value = val_lb | (val_hb << 8);
2658 return 0;
2659}
2660
Alan Cox2b24ef02009-03-26 21:58:19 +09002661static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
2662 unsigned long arg)
malattia@linux.it7b153f32007-04-09 19:31:25 +02002663{
2664 int ret = 0;
2665 void __user *argp = (void __user *)arg;
2666 u8 val8;
2667 u16 val16;
2668 int value;
2669
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002670 mutex_lock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002671 switch (cmd) {
2672 case SONYPI_IOCGBRT:
Mattia Dongili62d2f232011-05-09 10:20:29 -04002673 if (sony_bl_props.dev == NULL) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02002674 ret = -EIO;
2675 break;
2676 }
2677 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
2678 ret = -EIO;
2679 break;
2680 }
2681 val8 = ((value & 0xff) - 1) << 5;
2682 if (copy_to_user(argp, &val8, sizeof(val8)))
2683 ret = -EFAULT;
2684 break;
2685 case SONYPI_IOCSBRT:
Mattia Dongili62d2f232011-05-09 10:20:29 -04002686 if (sony_bl_props.dev == NULL) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02002687 ret = -EIO;
2688 break;
2689 }
2690 if (copy_from_user(&val8, argp, sizeof(val8))) {
2691 ret = -EFAULT;
2692 break;
2693 }
2694 if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
2695 (val8 >> 5) + 1, NULL)) {
2696 ret = -EIO;
2697 break;
2698 }
2699 /* sync the backlight device status */
Mattia Dongili62d2f232011-05-09 10:20:29 -04002700 sony_bl_props.dev->props.brightness =
2701 sony_backlight_get_brightness(sony_bl_props.dev);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002702 break;
2703 case SONYPI_IOCGBAT1CAP:
2704 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
2705 ret = -EIO;
2706 break;
2707 }
2708 if (copy_to_user(argp, &val16, sizeof(val16)))
2709 ret = -EFAULT;
2710 break;
2711 case SONYPI_IOCGBAT1REM:
2712 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
2713 ret = -EIO;
2714 break;
2715 }
2716 if (copy_to_user(argp, &val16, sizeof(val16)))
2717 ret = -EFAULT;
2718 break;
2719 case SONYPI_IOCGBAT2CAP:
2720 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
2721 ret = -EIO;
2722 break;
2723 }
2724 if (copy_to_user(argp, &val16, sizeof(val16)))
2725 ret = -EFAULT;
2726 break;
2727 case SONYPI_IOCGBAT2REM:
2728 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
2729 ret = -EIO;
2730 break;
2731 }
2732 if (copy_to_user(argp, &val16, sizeof(val16)))
2733 ret = -EFAULT;
2734 break;
2735 case SONYPI_IOCGBATFLAGS:
2736 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
2737 ret = -EIO;
2738 break;
2739 }
2740 val8 &= 0x07;
2741 if (copy_to_user(argp, &val8, sizeof(val8)))
2742 ret = -EFAULT;
2743 break;
2744 case SONYPI_IOCGBLUE:
2745 val8 = spic_dev.bluetooth_power;
2746 if (copy_to_user(argp, &val8, sizeof(val8)))
2747 ret = -EFAULT;
2748 break;
2749 case SONYPI_IOCSBLUE:
2750 if (copy_from_user(&val8, argp, sizeof(val8))) {
2751 ret = -EFAULT;
2752 break;
2753 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002754 __sony_pic_set_bluetoothpower(val8);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002755 break;
2756 /* FAN Controls */
2757 case SONYPI_IOCGFAN:
2758 if (sony_pic_get_fanspeed(&val8)) {
2759 ret = -EIO;
2760 break;
2761 }
2762 if (copy_to_user(argp, &val8, sizeof(val8)))
2763 ret = -EFAULT;
2764 break;
2765 case SONYPI_IOCSFAN:
2766 if (copy_from_user(&val8, argp, sizeof(val8))) {
2767 ret = -EFAULT;
2768 break;
2769 }
2770 if (sony_pic_set_fanspeed(val8))
2771 ret = -EIO;
2772 break;
2773 /* GET Temperature (useful under APM) */
2774 case SONYPI_IOCGTEMP:
2775 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
2776 ret = -EIO;
2777 break;
2778 }
2779 if (copy_to_user(argp, &val8, sizeof(val8)))
2780 ret = -EFAULT;
2781 break;
2782 default:
2783 ret = -EINVAL;
2784 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002785 mutex_unlock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002786 return ret;
2787}
2788
2789static const struct file_operations sonypi_misc_fops = {
2790 .owner = THIS_MODULE,
2791 .read = sonypi_misc_read,
2792 .poll = sonypi_misc_poll,
2793 .open = sonypi_misc_open,
2794 .release = sonypi_misc_release,
2795 .fasync = sonypi_misc_fasync,
Alan Cox2b24ef02009-03-26 21:58:19 +09002796 .unlocked_ioctl = sonypi_misc_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002797 .llseek = noop_llseek,
malattia@linux.it7b153f32007-04-09 19:31:25 +02002798};
2799
2800static struct miscdevice sonypi_misc_device = {
2801 .minor = MISC_DYNAMIC_MINOR,
2802 .name = "sonypi",
2803 .fops = &sonypi_misc_fops,
2804};
2805
2806static void sonypi_compat_report_event(u8 event)
2807{
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002808 kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002809 sizeof(event), &sonypi_compat.fifo_lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002810 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
2811 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
2812}
2813
2814static int sonypi_compat_init(void)
2815{
2816 int error;
2817
2818 spin_lock_init(&sonypi_compat.fifo_lock);
Stefani Seibold45465482009-12-21 14:37:26 -08002819 error =
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002820 kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -08002821 if (error) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002822 pr_err(DRV_PFX "kfifo_alloc failed\n");
Stefani Seibold45465482009-12-21 14:37:26 -08002823 return error;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002824 }
2825
2826 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002827
2828 if (minor != -1)
2829 sonypi_misc_device.minor = minor;
2830 error = misc_register(&sonypi_misc_device);
2831 if (error) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002832 pr_err(DRV_PFX "misc_register failed\n");
malattia@linux.it7b153f32007-04-09 19:31:25 +02002833 goto err_free_kfifo;
2834 }
2835 if (minor == -1)
Mattia Dongilid6697932011-02-19 11:52:28 +09002836 pr_info(DRV_PFX "device allocated minor is %d\n",
malattia@linux.it7b153f32007-04-09 19:31:25 +02002837 sonypi_misc_device.minor);
2838
2839 return 0;
2840
2841err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -08002842 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002843 return error;
2844}
2845
2846static void sonypi_compat_exit(void)
2847{
2848 misc_deregister(&sonypi_misc_device);
Stefani Seibold45465482009-12-21 14:37:26 -08002849 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002850}
2851#else
2852static int sonypi_compat_init(void) { return 0; }
2853static void sonypi_compat_exit(void) { }
2854static void sonypi_compat_report_event(u8 event) { }
Mattia Dongilia64e62a2007-05-01 11:19:53 +09002855#endif /* CONFIG_SONYPI_COMPAT */
malattia@linux.it7b153f32007-04-09 19:31:25 +02002856
malattia@linux.it1549ee62007-04-09 10:19:08 +02002857/*
malattia@linux.it33a04452007-04-09 19:26:03 +02002858 * ACPI callbacks
malattia@linux.it1549ee62007-04-09 10:19:08 +02002859 */
malattia@linux.it33a04452007-04-09 19:26:03 +02002860static acpi_status
2861sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
2862{
2863 u32 i;
2864 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
2865
2866 switch (resource->type) {
2867 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002868 {
2869 /* start IO enumeration */
2870 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
2871 if (!ioport)
2872 return AE_ERROR;
2873
2874 list_add(&ioport->list, &dev->ioports);
2875 return AE_OK;
2876 }
2877
malattia@linux.it33a04452007-04-09 19:26:03 +02002878 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002879 /* end IO enumeration */
malattia@linux.it33a04452007-04-09 19:26:03 +02002880 return AE_OK;
2881
2882 case ACPI_RESOURCE_TYPE_IRQ:
2883 {
2884 struct acpi_resource_irq *p = &resource->data.irq;
2885 struct sony_pic_irq *interrupt = NULL;
2886 if (!p || !p->interrupt_count) {
2887 /*
2888 * IRQ descriptors may have no IRQ# bits set,
2889 * particularly those those w/ _STA disabled
2890 */
2891 dprintk("Blank IRQ resource\n");
2892 return AE_OK;
2893 }
2894 for (i = 0; i < p->interrupt_count; i++) {
2895 if (!p->interrupts[i]) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002896 pr_warn(DRV_PFX "Invalid IRQ %d\n",
malattia@linux.it33a04452007-04-09 19:26:03 +02002897 p->interrupts[i]);
2898 continue;
2899 }
2900 interrupt = kzalloc(sizeof(*interrupt),
2901 GFP_KERNEL);
2902 if (!interrupt)
2903 return AE_ERROR;
2904
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002905 list_add(&interrupt->list, &dev->interrupts);
malattia@linux.it33a04452007-04-09 19:26:03 +02002906 interrupt->irq.triggering = p->triggering;
2907 interrupt->irq.polarity = p->polarity;
2908 interrupt->irq.sharable = p->sharable;
2909 interrupt->irq.interrupt_count = 1;
2910 interrupt->irq.interrupts[0] = p->interrupts[i];
2911 }
2912 return AE_OK;
2913 }
2914 case ACPI_RESOURCE_TYPE_IO:
2915 {
2916 struct acpi_resource_io *io = &resource->data.io;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002917 struct sony_pic_ioport *ioport =
2918 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
malattia@linux.it33a04452007-04-09 19:26:03 +02002919 if (!io) {
2920 dprintk("Blank IO resource\n");
2921 return AE_OK;
2922 }
2923
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002924 if (!ioport->io1.minimum) {
2925 memcpy(&ioport->io1, io, sizeof(*io));
2926 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
2927 ioport->io1.address_length);
2928 }
2929 else if (!ioport->io2.minimum) {
2930 memcpy(&ioport->io2, io, sizeof(*io));
2931 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
2932 ioport->io2.address_length);
2933 }
2934 else {
Mattia Dongilid6697932011-02-19 11:52:28 +09002935 pr_err(DRV_PFX "Unknown SPIC Type, more than 2 IO Ports\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002936 return AE_ERROR;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002937 }
malattia@linux.it33a04452007-04-09 19:26:03 +02002938 return AE_OK;
2939 }
2940 default:
2941 dprintk("Resource %d isn't an IRQ nor an IO port\n",
2942 resource->type);
2943
2944 case ACPI_RESOURCE_TYPE_END_TAG:
2945 return AE_OK;
2946 }
2947 return AE_CTRL_TERMINATE;
2948}
2949
2950static int sony_pic_possible_resources(struct acpi_device *device)
2951{
2952 int result = 0;
2953 acpi_status status = AE_OK;
2954
2955 if (!device)
2956 return -EINVAL;
2957
2958 /* get device status */
2959 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
2960 dprintk("Evaluating _STA\n");
2961 result = acpi_bus_get_status(device);
2962 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002963 pr_warn(DRV_PFX "Unable to read status\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002964 goto end;
2965 }
2966
2967 if (!device->status.enabled)
2968 dprintk("Device disabled\n");
2969 else
2970 dprintk("Device enabled\n");
2971
2972 /*
2973 * Query and parse 'method'
2974 */
2975 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
2976 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
2977 sony_pic_read_possible_resource, &spic_dev);
2978 if (ACPI_FAILURE(status)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002979 pr_warn(DRV_PFX "Failure evaluating %s\n",
malattia@linux.it33a04452007-04-09 19:26:03 +02002980 METHOD_NAME__PRS);
2981 result = -ENODEV;
2982 }
2983end:
2984 return result;
2985}
2986
2987/*
2988 * Disable the spic device by calling its _DIS method
2989 */
2990static int sony_pic_disable(struct acpi_device *device)
2991{
Matthew Garrett6158d3a2008-10-29 14:01:03 -07002992 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
2993 NULL);
2994
2995 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
malattia@linux.it33a04452007-04-09 19:26:03 +02002996 return -ENXIO;
2997
2998 dprintk("Device disabled\n");
2999 return 0;
3000}
3001
3002
3003/*
3004 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
3005 *
3006 * Call _SRS to set current resources
3007 */
3008static int sony_pic_enable(struct acpi_device *device,
3009 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
3010{
3011 acpi_status status;
3012 int result = 0;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003013 /* Type 1 resource layout is:
3014 * IO
3015 * IO
3016 * IRQNoFlags
3017 * End
3018 *
3019 * Type 2 and 3 resource layout is:
3020 * IO
3021 * IRQNoFlags
3022 * End
3023 */
malattia@linux.it33a04452007-04-09 19:26:03 +02003024 struct {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003025 struct acpi_resource res1;
3026 struct acpi_resource res2;
3027 struct acpi_resource res3;
3028 struct acpi_resource res4;
malattia@linux.it33a04452007-04-09 19:26:03 +02003029 } *resource;
3030 struct acpi_buffer buffer = { 0, NULL };
3031
3032 if (!ioport || !irq)
3033 return -EINVAL;
3034
3035 /* init acpi_buffer */
3036 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
3037 if (!resource)
3038 return -ENOMEM;
3039
3040 buffer.length = sizeof(*resource) + 1;
3041 buffer.pointer = resource;
3042
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003043 /* setup Type 1 resources */
Mattia Dongili31df7142009-09-16 00:05:29 +09003044 if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003045
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003046 /* setup io resources */
3047 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
3048 resource->res1.length = sizeof(struct acpi_resource);
3049 memcpy(&resource->res1.data.io, &ioport->io1,
3050 sizeof(struct acpi_resource_io));
malattia@linux.it33a04452007-04-09 19:26:03 +02003051
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003052 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
3053 resource->res2.length = sizeof(struct acpi_resource);
3054 memcpy(&resource->res2.data.io, &ioport->io2,
3055 sizeof(struct acpi_resource_io));
3056
3057 /* setup irq resource */
3058 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
3059 resource->res3.length = sizeof(struct acpi_resource);
3060 memcpy(&resource->res3.data.irq, &irq->irq,
3061 sizeof(struct acpi_resource_irq));
3062 /* we requested a shared irq */
3063 resource->res3.data.irq.sharable = ACPI_SHARED;
3064
3065 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
3066
3067 }
3068 /* setup Type 2/3 resources */
3069 else {
3070 /* setup io resource */
3071 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
3072 resource->res1.length = sizeof(struct acpi_resource);
3073 memcpy(&resource->res1.data.io, &ioport->io1,
3074 sizeof(struct acpi_resource_io));
3075
3076 /* setup irq resource */
3077 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
3078 resource->res2.length = sizeof(struct acpi_resource);
3079 memcpy(&resource->res2.data.irq, &irq->irq,
3080 sizeof(struct acpi_resource_irq));
3081 /* we requested a shared irq */
3082 resource->res2.data.irq.sharable = ACPI_SHARED;
3083
3084 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
3085 }
malattia@linux.it33a04452007-04-09 19:26:03 +02003086
3087 /* Attempt to set the resource */
3088 dprintk("Evaluating _SRS\n");
3089 status = acpi_set_current_resources(device->handle, &buffer);
3090
3091 /* check for total failure */
3092 if (ACPI_FAILURE(status)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003093 pr_err(DRV_PFX "Error evaluating _SRS\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003094 result = -ENODEV;
3095 goto end;
3096 }
3097
3098 /* Necessary device initializations calls (from sonypi) */
3099 sony_pic_call1(0x82);
3100 sony_pic_call2(0x81, 0xff);
3101 sony_pic_call1(compat ? 0x92 : 0x82);
3102
3103end:
3104 kfree(resource);
3105 return result;
3106}
3107
3108/*****************
3109 *
3110 * ISR: some event is available
3111 *
3112 *****************/
3113static irqreturn_t sony_pic_irq(int irq, void *dev_id)
3114{
3115 int i, j;
malattia@linux.it33a04452007-04-09 19:26:03 +02003116 u8 ev = 0;
3117 u8 data_mask = 0;
3118 u8 device_event = 0;
3119
3120 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
3121
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003122 ev = inb_p(dev->cur_ioport->io1.minimum);
3123 if (dev->cur_ioport->io2.minimum)
3124 data_mask = inb_p(dev->cur_ioport->io2.minimum);
3125 else
Mattia Dongilide920432008-01-14 18:05:43 +09003126 data_mask = inb_p(dev->cur_ioport->io1.minimum +
Mattia Dongili31df7142009-09-16 00:05:29 +09003127 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003128
Mattia Dongili22a17782007-07-16 02:34:39 +09003129 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
Mattia Dongilide920432008-01-14 18:05:43 +09003130 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09003131 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003132
3133 if (ev == 0x00 || ev == 0xff)
3134 return IRQ_HANDLED;
3135
Mattia Dongili31df7142009-09-16 00:05:29 +09003136 for (i = 0; dev->event_types[i].mask; i++) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003137
Mattia Dongili31df7142009-09-16 00:05:29 +09003138 if ((data_mask & dev->event_types[i].data) !=
3139 dev->event_types[i].data)
malattia@linux.it33a04452007-04-09 19:26:03 +02003140 continue;
3141
Mattia Dongili31df7142009-09-16 00:05:29 +09003142 if (!(mask & dev->event_types[i].mask))
malattia@linux.it33a04452007-04-09 19:26:03 +02003143 continue;
3144
Mattia Dongili31df7142009-09-16 00:05:29 +09003145 for (j = 0; dev->event_types[i].events[j].event; j++) {
3146 if (ev == dev->event_types[i].events[j].data) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003147 device_event =
Mattia Dongili31df7142009-09-16 00:05:29 +09003148 dev->event_types[i].events[j].event;
Mattia Dongili4eeb5022011-02-19 11:52:27 +09003149 /* some events may require ignoring */
3150 if (!device_event)
3151 return IRQ_HANDLED;
malattia@linux.it33a04452007-04-09 19:26:03 +02003152 goto found;
3153 }
3154 }
3155 }
Mattia Dongili3eb87492008-01-14 18:05:45 +09003156 /* Still not able to decode the event try to pass
3157 * it over to the minidriver
3158 */
Mattia Dongili31df7142009-09-16 00:05:29 +09003159 if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
Mattia Dongili3eb87492008-01-14 18:05:45 +09003160 return IRQ_HANDLED;
3161
Mattia Dongilide920432008-01-14 18:05:43 +09003162 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
3163 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09003164 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003165 return IRQ_HANDLED;
3166
3167found:
malattia@linux.it1549ee62007-04-09 10:19:08 +02003168 sony_laptop_report_input_event(device_event);
Mattia Dongilide920432008-01-14 18:05:43 +09003169 acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003170 sonypi_compat_report_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02003171 return IRQ_HANDLED;
3172}
3173
3174/*****************
3175 *
3176 * ACPI driver
3177 *
3178 *****************/
3179static int sony_pic_remove(struct acpi_device *device, int type)
3180{
3181 struct sony_pic_ioport *io, *tmp_io;
3182 struct sony_pic_irq *irq, *tmp_irq;
3183
3184 if (sony_pic_disable(device)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003185 pr_err(DRV_PFX "Couldn't disable device.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003186 return -ENXIO;
3187 }
3188
3189 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003190 release_region(spic_dev.cur_ioport->io1.minimum,
3191 spic_dev.cur_ioport->io1.address_length);
3192 if (spic_dev.cur_ioport->io2.minimum)
3193 release_region(spic_dev.cur_ioport->io2.minimum,
3194 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02003195
Mattia Dongili015a9162007-08-12 16:20:27 +09003196 sonypi_compat_exit();
3197
malattia@linux.it1549ee62007-04-09 10:19:08 +02003198 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02003199
malattia@linux.it49a11de2007-04-09 19:28:56 +02003200 /* pf attrs */
3201 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
3202 sony_pf_remove();
3203
malattia@linux.it33a04452007-04-09 19:26:03 +02003204 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
3205 list_del(&io->list);
3206 kfree(io);
3207 }
3208 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
3209 list_del(&irq->list);
3210 kfree(irq);
3211 }
3212 spic_dev.cur_ioport = NULL;
3213 spic_dev.cur_irq = NULL;
3214
malattia@linux.itf6119b02007-04-09 19:31:06 +02003215 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003216 return 0;
3217}
3218
3219static int sony_pic_add(struct acpi_device *device)
3220{
3221 int result;
3222 struct sony_pic_ioport *io, *tmp_io;
3223 struct sony_pic_irq *irq, *tmp_irq;
3224
Mattia Dongilid6697932011-02-19 11:52:28 +09003225 pr_info(DRV_PFX "%s v%s.\n", SONY_PIC_DRIVER_NAME,
3226 SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.it33a04452007-04-09 19:26:03 +02003227
3228 spic_dev.acpi_dev = device;
3229 strcpy(acpi_device_class(device), "sony/hotkey");
Mattia Dongilide920432008-01-14 18:05:43 +09003230 sony_pic_detect_device_type(&spic_dev);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003231 mutex_init(&spic_dev.lock);
malattia@linux.it33a04452007-04-09 19:26:03 +02003232
3233 /* read _PRS resources */
3234 result = sony_pic_possible_resources(device);
3235 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003236 pr_err(DRV_PFX "Unable to read possible resources.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003237 goto err_free_resources;
3238 }
3239
3240 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05003241 result = sony_laptop_setup_input(device);
malattia@linux.it33a04452007-04-09 19:26:03 +02003242 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003243 pr_err(DRV_PFX "Unable to create input devices.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003244 goto err_free_resources;
3245 }
3246
Mattia Dongili015a9162007-08-12 16:20:27 +09003247 if (sonypi_compat_init())
3248 goto err_remove_input;
3249
malattia@linux.it33a04452007-04-09 19:26:03 +02003250 /* request io port */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003251 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
3252 if (request_region(io->io1.minimum, io->io1.address_length,
Lucas De Marchic8440332011-03-17 17:18:22 -03003253 "Sony Programmable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003254 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
3255 io->io1.minimum, io->io1.maximum,
3256 io->io1.address_length);
3257 /* Type 1 have 2 ioports */
3258 if (io->io2.minimum) {
3259 if (request_region(io->io2.minimum,
3260 io->io2.address_length,
Lucas De Marchic8440332011-03-17 17:18:22 -03003261 "Sony Programmable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003262 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
3263 io->io2.minimum, io->io2.maximum,
3264 io->io2.address_length);
3265 spic_dev.cur_ioport = io;
3266 break;
3267 }
3268 else {
3269 dprintk("Unable to get I/O port2: "
3270 "0x%.4x (0x%.4x) + 0x%.2x\n",
3271 io->io2.minimum, io->io2.maximum,
3272 io->io2.address_length);
3273 release_region(io->io1.minimum,
3274 io->io1.address_length);
3275 }
3276 }
3277 else {
3278 spic_dev.cur_ioport = io;
3279 break;
3280 }
malattia@linux.it33a04452007-04-09 19:26:03 +02003281 }
3282 }
3283 if (!spic_dev.cur_ioport) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003284 pr_err(DRV_PFX "Failed to request_region.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003285 result = -ENODEV;
Mattia Dongili015a9162007-08-12 16:20:27 +09003286 goto err_remove_compat;
malattia@linux.it33a04452007-04-09 19:26:03 +02003287 }
3288
3289 /* request IRQ */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003290 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003291 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
Mattia Dongilid1e0de92009-09-16 00:05:30 +09003292 IRQF_DISABLED, "sony-laptop", &spic_dev)) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003293 dprintk("IRQ: %d - triggering: %d - "
3294 "polarity: %d - shr: %d\n",
3295 irq->irq.interrupts[0],
3296 irq->irq.triggering,
3297 irq->irq.polarity,
3298 irq->irq.sharable);
3299 spic_dev.cur_irq = irq;
3300 break;
3301 }
3302 }
3303 if (!spic_dev.cur_irq) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003304 pr_err(DRV_PFX "Failed to request_irq.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003305 result = -ENODEV;
3306 goto err_release_region;
3307 }
3308
3309 /* set resource status _SRS */
3310 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
3311 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003312 pr_err(DRV_PFX "Couldn't enable device.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003313 goto err_free_irq;
3314 }
3315
malattia@linux.it49a11de2007-04-09 19:28:56 +02003316 spic_dev.bluetooth_power = -1;
3317 /* create device attributes */
3318 result = sony_pf_add();
3319 if (result)
3320 goto err_disable_device;
3321
3322 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
3323 if (result)
3324 goto err_remove_pf;
3325
malattia@linux.it33a04452007-04-09 19:26:03 +02003326 return 0;
3327
malattia@linux.it49a11de2007-04-09 19:28:56 +02003328err_remove_pf:
3329 sony_pf_remove();
3330
3331err_disable_device:
3332 sony_pic_disable(device);
3333
malattia@linux.it33a04452007-04-09 19:26:03 +02003334err_free_irq:
3335 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
3336
3337err_release_region:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003338 release_region(spic_dev.cur_ioport->io1.minimum,
3339 spic_dev.cur_ioport->io1.address_length);
3340 if (spic_dev.cur_ioport->io2.minimum)
3341 release_region(spic_dev.cur_ioport->io2.minimum,
3342 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02003343
Mattia Dongili015a9162007-08-12 16:20:27 +09003344err_remove_compat:
3345 sonypi_compat_exit();
3346
malattia@linux.it33a04452007-04-09 19:26:03 +02003347err_remove_input:
malattia@linux.it1549ee62007-04-09 10:19:08 +02003348 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02003349
3350err_free_resources:
3351 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
3352 list_del(&io->list);
3353 kfree(io);
3354 }
3355 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
3356 list_del(&irq->list);
3357 kfree(irq);
3358 }
3359 spic_dev.cur_ioport = NULL;
3360 spic_dev.cur_irq = NULL;
3361
3362 return result;
3363}
3364
3365static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
3366{
3367 if (sony_pic_disable(device))
3368 return -ENXIO;
3369 return 0;
3370}
3371
3372static int sony_pic_resume(struct acpi_device *device)
3373{
3374 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
3375 return 0;
3376}
3377
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003378static const struct acpi_device_id sony_pic_device_ids[] = {
3379 {SONY_PIC_HID, 0},
3380 {"", 0},
3381};
3382
malattia@linux.it33a04452007-04-09 19:26:03 +02003383static struct acpi_driver sony_pic_driver = {
3384 .name = SONY_PIC_DRIVER_NAME,
3385 .class = SONY_PIC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003386 .ids = sony_pic_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02003387 .owner = THIS_MODULE,
3388 .ops = {
3389 .add = sony_pic_add,
3390 .remove = sony_pic_remove,
3391 .suspend = sony_pic_suspend,
3392 .resume = sony_pic_resume,
3393 },
3394};
3395
3396static struct dmi_system_id __initdata sonypi_dmi_table[] = {
3397 {
3398 .ident = "Sony Vaio",
3399 .matches = {
3400 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
3401 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
3402 },
3403 },
3404 {
3405 .ident = "Sony Vaio",
3406 .matches = {
3407 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
3408 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
3409 },
3410 },
3411 { }
3412};
3413
malattia@linux.it59b19102007-04-09 10:19:04 +02003414static int __init sony_laptop_init(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01003415{
malattia@linux.it33a04452007-04-09 19:26:03 +02003416 int result;
3417
3418 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
3419 result = acpi_bus_register_driver(&sony_pic_driver);
3420 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003421 pr_err(DRV_PFX "Unable to register SPIC driver.");
malattia@linux.it33a04452007-04-09 19:26:03 +02003422 goto out;
3423 }
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003424 spic_drv_registered = 1;
malattia@linux.it33a04452007-04-09 19:26:03 +02003425 }
3426
3427 result = acpi_bus_register_driver(&sony_nc_driver);
3428 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003429 pr_err(DRV_PFX "Unable to register SNC driver.");
malattia@linux.it33a04452007-04-09 19:26:03 +02003430 goto out_unregister_pic;
3431 }
3432
3433 return 0;
3434
3435out_unregister_pic:
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003436 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02003437 acpi_bus_unregister_driver(&sony_pic_driver);
3438out:
3439 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +01003440}
3441
malattia@linux.it59b19102007-04-09 10:19:04 +02003442static void __exit sony_laptop_exit(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01003443{
malattia@linux.it59b19102007-04-09 10:19:04 +02003444 acpi_bus_unregister_driver(&sony_nc_driver);
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003445 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02003446 acpi_bus_unregister_driver(&sony_pic_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +01003447}
3448
malattia@linux.it59b19102007-04-09 10:19:04 +02003449module_init(sony_laptop_init);
3450module_exit(sony_laptop_exit);