blob: 8f709aec4da0ac2ebb81d09e19baea9932506f26 [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 Dongili855b8bc92011-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 Dongili855b8bc92011-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 */
937static int sony_backlight_update_status(struct backlight_device *bd)
Andrew Morton3f4f4612007-01-13 23:04:32 +0100938{
malattia@linux.it59b19102007-04-09 10:19:04 +0200939 return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
Richard Purdie321709c2007-02-10 15:04:08 +0000940 bd->props.brightness + 1, NULL);
Andrew Morton3f4f4612007-01-13 23:04:32 +0100941}
942
Mattia Dongilid78865c2007-02-07 20:01:56 +0100943static int sony_backlight_get_brightness(struct backlight_device *bd)
944{
945 int value;
946
malattia@linux.it59b19102007-04-09 10:19:04 +0200947 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
Mattia Dongilid78865c2007-02-07 20:01:56 +0100948 return 0;
949 /* brightness levels are 1-based, while backlight ones are 0-based */
950 return value - 1;
951}
952
Mattia Dongili7751ab82011-02-19 11:52:32 +0900953static int sony_nc_get_brightness_ng(struct backlight_device *bd)
954{
955 int result;
956 int *handle = (int *)bl_get_data(bd);
957
958 sony_call_snc_handle(*handle, 0x0200, &result);
959
960 return result & 0xff;
961}
962
963static int sony_nc_update_status_ng(struct backlight_device *bd)
964{
965 int value, result;
966 int *handle = (int *)bl_get_data(bd);
967
968 value = bd->props.brightness;
969 sony_call_snc_handle(*handle, 0x0100 | (value << 16), &result);
970
971 return sony_nc_get_brightness_ng(bd);
972}
973
Lionel Debrouxacc24722010-11-16 14:14:02 +0100974static const struct backlight_ops sony_backlight_ops = {
Mattia Dongili7751ab82011-02-19 11:52:32 +0900975 .options = BL_CORE_SUSPENDRESUME,
Len Browna02d1c12007-02-07 15:34:02 -0500976 .update_status = sony_backlight_update_status,
977 .get_brightness = sony_backlight_get_brightness,
Mattia Dongilid78865c2007-02-07 20:01:56 +0100978};
Mattia Dongili7751ab82011-02-19 11:52:32 +0900979static const struct backlight_ops sony_backlight_ng_ops = {
980 .options = BL_CORE_SUSPENDRESUME,
981 .update_status = sony_nc_update_status_ng,
982 .get_brightness = sony_nc_get_brightness_ng,
983};
984static int backlight_ng_handle;
985static struct backlight_device *sony_backlight_device;
Mattia Dongilid78865c2007-02-07 20:01:56 +0100986
987/*
Mattia Dongili6315fd12007-07-16 02:34:35 +0900988 * New SNC-only Vaios event mapping to driver known keys
989 */
990struct sony_nc_event {
991 u8 data;
992 u8 event;
993};
994
Matthew Garrett45c79422009-03-26 21:58:16 +0900995static struct sony_nc_event sony_100_events[] = {
Matthew Garrett9b578962009-03-26 21:58:14 +0900996 { 0x90, SONYPI_EVENT_PKEY_P1 },
997 { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthias Welwarsky6479efb2009-04-01 22:10:45 +0900998 { 0x91, SONYPI_EVENT_PKEY_P2 },
Matthew Garrett9b578962009-03-26 21:58:14 +0900999 { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001000 { 0x81, SONYPI_EVENT_FNKEY_F1 },
1001 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
Anton Veretenenkof5acf5e2009-03-26 22:44:26 +09001002 { 0x82, SONYPI_EVENT_FNKEY_F2 },
1003 { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
1004 { 0x83, SONYPI_EVENT_FNKEY_F3 },
1005 { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
1006 { 0x84, SONYPI_EVENT_FNKEY_F4 },
1007 { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001008 { 0x85, SONYPI_EVENT_FNKEY_F5 },
1009 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
1010 { 0x86, SONYPI_EVENT_FNKEY_F6 },
1011 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
1012 { 0x87, SONYPI_EVENT_FNKEY_F7 },
1013 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +09001014 { 0x89, SONYPI_EVENT_FNKEY_F9 },
1015 { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001016 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
1017 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
1018 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
1019 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +09001020 { 0x9d, SONYPI_EVENT_ZOOM_PRESSED },
1021 { 0x1d, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +09001022 { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
1023 { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili4f924ba2009-12-17 00:08:33 +09001024 { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
1025 { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +09001026 { 0xa4, SONYPI_EVENT_CD_EJECT_PRESSED },
1027 { 0x24, SONYPI_EVENT_ANYBUTTON_RELEASED },
1028 { 0xa5, SONYPI_EVENT_VENDOR_PRESSED },
1029 { 0x25, SONYPI_EVENT_ANYBUTTON_RELEASED },
1030 { 0xa6, SONYPI_EVENT_HELP_PRESSED },
1031 { 0x26, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001032 { 0, 0 },
1033};
1034
Matthew Garrett45c79422009-03-26 21:58:16 +09001035static struct sony_nc_event sony_127_events[] = {
1036 { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
1037 { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
1038 { 0x82, SONYPI_EVENT_PKEY_P1 },
1039 { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
1040 { 0x83, SONYPI_EVENT_PKEY_P2 },
1041 { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
1042 { 0x84, SONYPI_EVENT_PKEY_P3 },
1043 { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
1044 { 0x85, SONYPI_EVENT_PKEY_P4 },
1045 { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
1046 { 0x86, SONYPI_EVENT_PKEY_P5 },
1047 { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett45c79422009-03-26 21:58:16 +09001048 { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
1049 { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
1050 { 0, 0 },
1051};
1052
Mattia Dongili6315fd12007-07-16 02:34:35 +09001053/*
Mattia Dongilid78865c2007-02-07 20:01:56 +01001054 * ACPI callbacks
1055 */
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001056static void sony_nc_notify(struct acpi_device *device, u32 event)
Stelian Pop7f09c432007-01-13 23:04:31 +01001057{
Mattia Dongili6315fd12007-07-16 02:34:35 +09001058 u32 ev = event;
Mattia Dongili6315fd12007-07-16 02:34:35 +09001059
Matthew Garrett9b578962009-03-26 21:58:14 +09001060 if (ev >= 0x90) {
1061 /* New-style event */
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001062 int result;
Matthew Garrett45c79422009-03-26 21:58:16 +09001063 int key_handle = 0;
Matthew Garrett9b578962009-03-26 21:58:14 +09001064 ev -= 0x90;
Mattia Dongili6315fd12007-07-16 02:34:35 +09001065
Matthew Garrett45c79422009-03-26 21:58:16 +09001066 if (sony_find_snc_handle(0x100) == ev)
1067 key_handle = 0x100;
1068 if (sony_find_snc_handle(0x127) == ev)
1069 key_handle = 0x127;
Matthew Garrett9b578962009-03-26 21:58:14 +09001070
Matthias Welwarsky6479efb2009-04-01 22:10:45 +09001071 if (key_handle) {
Matthew Garrett45c79422009-03-26 21:58:16 +09001072 struct sony_nc_event *key_event;
1073
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001074 if (sony_call_snc_handle(key_handle, 0x200, &result)) {
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001075 dprintk("sony_nc_notify, unable to decode"
Matthew Garrett45c79422009-03-26 21:58:16 +09001076 " event 0x%.2x 0x%.2x\n", key_handle,
1077 ev);
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001078 /* restore the original event */
1079 ev = event;
1080 } else {
Matthew Garrett9b578962009-03-26 21:58:14 +09001081 ev = result & 0xFF;
1082
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001083 if (key_handle == 0x100)
1084 key_event = sony_100_events;
1085 else
1086 key_event = sony_127_events;
Matthew Garrett45c79422009-03-26 21:58:16 +09001087
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001088 for (; key_event->data; key_event++) {
1089 if (key_event->data == ev) {
1090 ev = key_event->event;
1091 break;
1092 }
Matthew Garrett9b578962009-03-26 21:58:14 +09001093 }
Mattia Dongili6315fd12007-07-16 02:34:35 +09001094
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001095 if (!key_event->data)
Mattia Dongilid6697932011-02-19 11:52:28 +09001096 pr_info(DRV_PFX
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001097 "Unknown event: 0x%x 0x%x\n",
1098 key_handle,
1099 ev);
1100 else
1101 sony_laptop_report_input_event(ev);
Matthew Garrett45c79422009-03-26 21:58:16 +09001102 }
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001103 } else if (sony_find_snc_handle(sony_rfkill_handle) == ev) {
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001104 sony_nc_rfkill_update();
1105 return;
Matthew Garrett9b578962009-03-26 21:58:14 +09001106 }
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001107 } else
1108 sony_laptop_report_input_event(ev);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001109
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001110 dprintk("sony_nc_notify, event: 0x%.2x\n", ev);
Len Brown14e04fb2007-08-23 15:20:26 -04001111 acpi_bus_generate_proc_event(sony_nc_acpi_device, 1, ev);
Stelian Pop7f09c432007-01-13 23:04:31 +01001112}
1113
1114static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
1115 void *context, void **return_value)
1116{
Lin Ming30823732008-12-16 16:59:35 +08001117 struct acpi_device_info *info;
Stelian Pop7f09c432007-01-13 23:04:31 +01001118
Bob Moore15b8dd52009-06-29 13:39:29 +08001119 if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001120 pr_warn(DRV_PFX "method: name: %4.4s, args %X\n",
Lin Ming30823732008-12-16 16:59:35 +08001121 (char *)&info->name, info->param_count);
1122
Bob Moore15b8dd52009-06-29 13:39:29 +08001123 kfree(info);
Lin Ming30823732008-12-16 16:59:35 +08001124 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001125
1126 return AE_OK;
1127}
1128
Mattia Dongilid78865c2007-02-07 20:01:56 +01001129/*
1130 * ACPI device
1131 */
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001132static int sony_nc_function_setup(struct acpi_device *device)
1133{
1134 int result;
1135
1136 /* Enable all events */
1137 acpi_callsetfunc(sony_nc_acpi_handle, "SN02", 0xffff, &result);
1138
1139 /* Setup hotkeys */
1140 sony_call_snc_handle(0x0100, 0, &result);
1141 sony_call_snc_handle(0x0101, 0, &result);
1142 sony_call_snc_handle(0x0102, 0x100, &result);
Almer S. Tigelaar560e84a2009-04-12 11:26:27 +00001143 sony_call_snc_handle(0x0127, 0, &result);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001144
1145 return 0;
1146}
1147
malattia@linux.it59b19102007-04-09 10:19:04 +02001148static int sony_nc_resume(struct acpi_device *device)
Mattia Dongilid78865c2007-02-07 20:01:56 +01001149{
malattia@linux.it59b19102007-04-09 10:19:04 +02001150 struct sony_nc_value *item;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001151 acpi_handle handle;
Mattia Dongilid78865c2007-02-07 20:01:56 +01001152
malattia@linux.it59b19102007-04-09 10:19:04 +02001153 for (item = sony_nc_values; item->name; item++) {
Mattia Dongilid78865c2007-02-07 20:01:56 +01001154 int ret;
1155
1156 if (!item->valid)
1157 continue;
malattia@linux.it59b19102007-04-09 10:19:04 +02001158 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
Len Browna02d1c12007-02-07 15:34:02 -05001159 item->value, NULL);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001160 if (ret < 0) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001161 pr_err(DRV_PFX "%s: %d\n", __func__, ret);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001162 break;
1163 }
1164 }
Mattia Dongili6315fd12007-07-16 02:34:35 +09001165
Matthew Garrett82734bf2009-03-26 21:58:13 +09001166 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1167 &handle))) {
1168 if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
1169 dprintk("ECON Method failed\n");
1170 }
1171
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001172 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1173 &handle))) {
1174 dprintk("Doing SNC setup\n");
1175 sony_nc_function_setup(device);
1176 }
1177
Alan Jenkinsa0d97d62009-09-25 10:18:21 +01001178 /* re-read rfkill state */
1179 sony_nc_rfkill_update();
1180
Marco Chiapperodf410d52011-04-05 23:38:34 +09001181 /* restore kbd backlight states */
1182 sony_nc_kbd_backlight_resume();
1183
Mattia Dongilid78865c2007-02-07 20:01:56 +01001184 return 0;
1185}
1186
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001187static void sony_nc_rfkill_cleanup(void)
1188{
1189 int i;
1190
Johannes Berg19d337d2009-06-02 13:01:37 +02001191 for (i = 0; i < N_SONY_RFKILL; i++) {
1192 if (sony_rfkill_devices[i]) {
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001193 rfkill_unregister(sony_rfkill_devices[i]);
Johannes Berg19d337d2009-06-02 13:01:37 +02001194 rfkill_destroy(sony_rfkill_devices[i]);
1195 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001196 }
1197}
1198
Johannes Berg19d337d2009-06-02 13:01:37 +02001199static int sony_nc_rfkill_set(void *data, bool blocked)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001200{
1201 int result;
1202 int argument = sony_rfkill_address[(long) data] + 0x100;
1203
Johannes Berg19d337d2009-06-02 13:01:37 +02001204 if (!blocked)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001205 argument |= 0xff0000;
1206
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001207 return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001208}
1209
Johannes Berg19d337d2009-06-02 13:01:37 +02001210static const struct rfkill_ops sony_rfkill_ops = {
1211 .set_block = sony_nc_rfkill_set,
1212};
1213
1214static int sony_nc_setup_rfkill(struct acpi_device *device,
1215 enum sony_nc_rfkill nc_type)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001216{
1217 int err = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001218 struct rfkill *rfk;
1219 enum rfkill_type type;
1220 const char *name;
Alan Jenkins50fab072009-09-24 20:15:24 +01001221 int result;
1222 bool hwblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001223
Johannes Berg19d337d2009-06-02 13:01:37 +02001224 switch (nc_type) {
1225 case SONY_WIFI:
1226 type = RFKILL_TYPE_WLAN;
1227 name = "sony-wifi";
1228 break;
1229 case SONY_BLUETOOTH:
1230 type = RFKILL_TYPE_BLUETOOTH;
1231 name = "sony-bluetooth";
1232 break;
1233 case SONY_WWAN:
1234 type = RFKILL_TYPE_WWAN;
1235 name = "sony-wwan";
1236 break;
1237 case SONY_WIMAX:
1238 type = RFKILL_TYPE_WIMAX;
1239 name = "sony-wimax";
1240 break;
1241 default:
1242 return -EINVAL;
Mattia Dongili53005a02009-04-12 11:26:31 +00001243 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001244
Johannes Berg19d337d2009-06-02 13:01:37 +02001245 rfk = rfkill_alloc(name, &device->dev, type,
1246 &sony_rfkill_ops, (void *)nc_type);
1247 if (!rfk)
1248 return -ENOMEM;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001249
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001250 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
Alan Jenkins50fab072009-09-24 20:15:24 +01001251 hwblock = !(result & 0x1);
1252 rfkill_set_hw_state(rfk, hwblock);
1253
Johannes Berg19d337d2009-06-02 13:01:37 +02001254 err = rfkill_register(rfk);
1255 if (err) {
1256 rfkill_destroy(rfk);
1257 return err;
Mattia Dongili53005a02009-04-12 11:26:31 +00001258 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001259 sony_rfkill_devices[nc_type] = rfk;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001260 return err;
1261}
1262
Randy Dunlapa46a7802011-01-08 19:56:44 -08001263static void sony_nc_rfkill_update(void)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001264{
Johannes Berg19d337d2009-06-02 13:01:37 +02001265 enum sony_nc_rfkill i;
1266 int result;
1267 bool hwblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001268
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001269 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001270 hwblock = !(result & 0x1);
1271
1272 for (i = 0; i < N_SONY_RFKILL; i++) {
1273 int argument = sony_rfkill_address[i];
1274
1275 if (!sony_rfkill_devices[i])
1276 continue;
1277
1278 if (hwblock) {
Johannes Berge1f8a192009-06-11 12:08:15 +02001279 if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
1280 /* we already know we're blocked */
1281 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001282 continue;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001283 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001284
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001285 sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001286 rfkill_set_states(sony_rfkill_devices[i],
1287 !(result & 0xf), false);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001288 }
1289}
1290
Mattia Dongili528809c2009-12-17 00:08:36 +09001291static void sony_nc_rfkill_setup(struct acpi_device *device)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001292{
Mattia Dongili528809c2009-12-17 00:08:36 +09001293 int offset;
1294 u8 dev_code, i;
1295 acpi_status status;
1296 struct acpi_object_list params;
1297 union acpi_object in_obj;
1298 union acpi_object *device_enum;
1299 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001300
Mattia Dongili528809c2009-12-17 00:08:36 +09001301 offset = sony_find_snc_handle(0x124);
1302 if (offset == -1) {
1303 offset = sony_find_snc_handle(0x135);
1304 if (offset == -1)
1305 return;
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001306 else
1307 sony_rfkill_handle = 0x135;
1308 } else
1309 sony_rfkill_handle = 0x124;
Mattia Dongili528809c2009-12-17 00:08:36 +09001310 dprintk("Found rkfill handle: 0x%.4x\n", sony_rfkill_handle);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001311
Mattia Dongili528809c2009-12-17 00:08:36 +09001312 /* need to read the whole buffer returned by the acpi call to SN06
1313 * here otherwise we may miss some features
1314 */
1315 params.count = 1;
1316 params.pointer = &in_obj;
1317 in_obj.type = ACPI_TYPE_INTEGER;
1318 in_obj.integer.value = offset;
1319 status = acpi_evaluate_object(sony_nc_acpi_handle, "SN06", &params,
1320 &buffer);
1321 if (ACPI_FAILURE(status)) {
1322 dprintk("Radio device enumeration failed\n");
1323 return;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001324 }
1325
Mattia Dongili528809c2009-12-17 00:08:36 +09001326 device_enum = (union acpi_object *) buffer.pointer;
Dan Carpenter200140b2011-02-27 17:13:25 +03001327 if (!device_enum) {
1328 pr_err(DRV_PFX "No SN06 return object.");
1329 goto out_no_enum;
1330 }
1331 if (device_enum->type != ACPI_TYPE_BUFFER) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001332 pr_err(DRV_PFX "Invalid SN06 return object 0x%.2x\n",
1333 device_enum->type);
Mattia Dongili528809c2009-12-17 00:08:36 +09001334 goto out_no_enum;
1335 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001336
Mattia Dongili528809c2009-12-17 00:08:36 +09001337 /* the buffer is filled with magic numbers describing the devices
1338 * available, 0xff terminates the enumeration
1339 */
Dmitry Torokhovc14973f2010-01-10 00:15:44 -08001340 for (i = 0; i < device_enum->buffer.length; i++) {
1341
1342 dev_code = *(device_enum->buffer.pointer + i);
1343 if (dev_code == 0xff)
1344 break;
1345
Mattia Dongili528809c2009-12-17 00:08:36 +09001346 dprintk("Radio devices, looking at 0x%.2x\n", dev_code);
1347
1348 if (dev_code == 0 && !sony_rfkill_devices[SONY_WIFI])
1349 sony_nc_setup_rfkill(device, SONY_WIFI);
1350
1351 if (dev_code == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
1352 sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
1353
1354 if ((0xf0 & dev_code) == 0x20 &&
1355 !sony_rfkill_devices[SONY_WWAN])
1356 sony_nc_setup_rfkill(device, SONY_WWAN);
1357
1358 if (dev_code == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
1359 sony_nc_setup_rfkill(device, SONY_WIMAX);
1360 }
1361
1362out_no_enum:
1363 kfree(buffer.pointer);
1364 return;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001365}
1366
Mattia Dongilibf155712011-02-19 11:52:31 +09001367/* Keyboard backlight feature */
1368#define KBDBL_HANDLER 0x137
1369#define KBDBL_PRESENT 0xB00
1370#define SET_MODE 0xC00
Marco Chiapperodf410d52011-04-05 23:38:34 +09001371#define SET_STATE 0xD00
Mattia Dongilibf155712011-02-19 11:52:31 +09001372#define SET_TIMEOUT 0xE00
1373
1374struct kbd_backlight {
1375 int mode;
1376 int timeout;
1377 struct device_attribute mode_attr;
1378 struct device_attribute timeout_attr;
1379};
1380
Dan Carpenterf11113b2011-02-26 15:54:27 +03001381static struct kbd_backlight *kbdbl_handle;
Mattia Dongilibf155712011-02-19 11:52:31 +09001382
1383static ssize_t __sony_nc_kbd_backlight_mode_set(u8 value)
1384{
1385 int result;
1386
1387 if (value > 1)
1388 return -EINVAL;
1389
1390 if (sony_call_snc_handle(KBDBL_HANDLER,
1391 (value << 0x10) | SET_MODE, &result))
1392 return -EIO;
1393
Marco Chiapperodf410d52011-04-05 23:38:34 +09001394 /* Try to turn the light on/off immediately */
1395 sony_call_snc_handle(KBDBL_HANDLER, (value << 0x10) | SET_STATE,
1396 &result);
1397
Mattia Dongilibf155712011-02-19 11:52:31 +09001398 kbdbl_handle->mode = value;
1399
1400 return 0;
1401}
1402
1403static ssize_t sony_nc_kbd_backlight_mode_store(struct device *dev,
1404 struct device_attribute *attr,
1405 const char *buffer, size_t count)
1406{
1407 int ret = 0;
1408 unsigned long value;
1409
1410 if (count > 31)
1411 return -EINVAL;
1412
1413 if (strict_strtoul(buffer, 10, &value))
1414 return -EINVAL;
1415
1416 ret = __sony_nc_kbd_backlight_mode_set(value);
1417 if (ret < 0)
1418 return ret;
1419
1420 return count;
1421}
1422
1423static ssize_t sony_nc_kbd_backlight_mode_show(struct device *dev,
1424 struct device_attribute *attr, char *buffer)
1425{
1426 ssize_t count = 0;
1427 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_handle->mode);
1428 return count;
1429}
1430
1431static int __sony_nc_kbd_backlight_timeout_set(u8 value)
1432{
1433 int result;
1434
1435 if (value > 3)
1436 return -EINVAL;
1437
1438 if (sony_call_snc_handle(KBDBL_HANDLER,
1439 (value << 0x10) | SET_TIMEOUT, &result))
1440 return -EIO;
1441
1442 kbdbl_handle->timeout = value;
1443
1444 return 0;
1445}
1446
1447static ssize_t sony_nc_kbd_backlight_timeout_store(struct device *dev,
1448 struct device_attribute *attr,
1449 const char *buffer, size_t count)
1450{
1451 int ret = 0;
1452 unsigned long value;
1453
1454 if (count > 31)
1455 return -EINVAL;
1456
1457 if (strict_strtoul(buffer, 10, &value))
1458 return -EINVAL;
1459
1460 ret = __sony_nc_kbd_backlight_timeout_set(value);
1461 if (ret < 0)
1462 return ret;
1463
1464 return count;
1465}
1466
1467static ssize_t sony_nc_kbd_backlight_timeout_show(struct device *dev,
1468 struct device_attribute *attr, char *buffer)
1469{
1470 ssize_t count = 0;
1471 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_handle->timeout);
1472 return count;
1473}
1474
1475static int sony_nc_kbd_backlight_setup(struct platform_device *pd)
1476{
1477 int result;
1478
Marco Chiapperodf410d52011-04-05 23:38:34 +09001479 if (sony_call_snc_handle(KBDBL_HANDLER, KBDBL_PRESENT, &result))
Mattia Dongilibf155712011-02-19 11:52:31 +09001480 return 0;
1481 if (!(result & 0x02))
1482 return 0;
1483
1484 kbdbl_handle = kzalloc(sizeof(*kbdbl_handle), GFP_KERNEL);
Dan Carpenter31f00752011-02-26 15:55:24 +03001485 if (!kbdbl_handle)
1486 return -ENOMEM;
Mattia Dongilibf155712011-02-19 11:52:31 +09001487
1488 sysfs_attr_init(&kbdbl_handle->mode_attr.attr);
1489 kbdbl_handle->mode_attr.attr.name = "kbd_backlight";
1490 kbdbl_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
1491 kbdbl_handle->mode_attr.show = sony_nc_kbd_backlight_mode_show;
1492 kbdbl_handle->mode_attr.store = sony_nc_kbd_backlight_mode_store;
1493
1494 sysfs_attr_init(&kbdbl_handle->timeout_attr.attr);
1495 kbdbl_handle->timeout_attr.attr.name = "kbd_backlight_timeout";
1496 kbdbl_handle->timeout_attr.attr.mode = S_IRUGO | S_IWUSR;
1497 kbdbl_handle->timeout_attr.show = sony_nc_kbd_backlight_timeout_show;
1498 kbdbl_handle->timeout_attr.store = sony_nc_kbd_backlight_timeout_store;
1499
1500 if (device_create_file(&pd->dev, &kbdbl_handle->mode_attr))
1501 goto outkzalloc;
1502
1503 if (device_create_file(&pd->dev, &kbdbl_handle->timeout_attr))
1504 goto outmode;
1505
1506 __sony_nc_kbd_backlight_mode_set(kbd_backlight);
1507 __sony_nc_kbd_backlight_timeout_set(kbd_backlight_timeout);
1508
1509 return 0;
1510
1511outmode:
1512 device_remove_file(&pd->dev, &kbdbl_handle->mode_attr);
1513outkzalloc:
1514 kfree(kbdbl_handle);
1515 kbdbl_handle = NULL;
1516 return -1;
1517}
1518
1519static int sony_nc_kbd_backlight_cleanup(struct platform_device *pd)
1520{
1521 if (kbdbl_handle) {
Marco Chiapperodf410d52011-04-05 23:38:34 +09001522 int result;
1523
Mattia Dongilibf155712011-02-19 11:52:31 +09001524 device_remove_file(&pd->dev, &kbdbl_handle->mode_attr);
1525 device_remove_file(&pd->dev, &kbdbl_handle->timeout_attr);
Marco Chiapperodf410d52011-04-05 23:38:34 +09001526
1527 /* restore the default hw behaviour */
1528 sony_call_snc_handle(KBDBL_HANDLER, 0x1000 | SET_MODE, &result);
1529 sony_call_snc_handle(KBDBL_HANDLER, SET_TIMEOUT, &result);
1530
Mattia Dongilibf155712011-02-19 11:52:31 +09001531 kfree(kbdbl_handle);
1532 }
1533 return 0;
1534}
1535
Marco Chiapperodf410d52011-04-05 23:38:34 +09001536static void sony_nc_kbd_backlight_resume(void)
1537{
1538 int ignore = 0;
1539
1540 if (!kbdbl_handle)
1541 return;
1542
1543 if (kbdbl_handle->mode == 0)
1544 sony_call_snc_handle(KBDBL_HANDLER, SET_MODE, &ignore);
1545
1546 if (kbdbl_handle->timeout != 0)
1547 sony_call_snc_handle(KBDBL_HANDLER,
1548 (kbdbl_handle->timeout << 0x10) | SET_TIMEOUT,
1549 &ignore);
1550}
1551
Mattia Dongili7751ab82011-02-19 11:52:32 +09001552static void sony_nc_backlight_setup(void)
1553{
1554 acpi_handle unused;
1555 int max_brightness = 0;
1556 const struct backlight_ops *ops = NULL;
1557 struct backlight_properties props;
1558
1559 if (sony_find_snc_handle(0x12f) != -1) {
1560 backlight_ng_handle = 0x12f;
1561 ops = &sony_backlight_ng_ops;
1562 max_brightness = 0xff;
1563
1564 } else if (sony_find_snc_handle(0x137) != -1) {
1565 backlight_ng_handle = 0x137;
1566 ops = &sony_backlight_ng_ops;
1567 max_brightness = 0xff;
1568
1569 } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
1570 &unused))) {
1571 ops = &sony_backlight_ops;
1572 max_brightness = SONY_MAX_BRIGHTNESS - 1;
1573
1574 } else
1575 return;
1576
1577 memset(&props, 0, sizeof(struct backlight_properties));
1578 props.type = BACKLIGHT_PLATFORM;
1579 props.max_brightness = max_brightness;
1580 sony_backlight_device = backlight_device_register("sony", NULL,
1581 &backlight_ng_handle,
1582 ops, &props);
1583
1584 if (IS_ERR(sony_backlight_device)) {
1585 pr_warning(DRV_PFX "unable to register backlight device\n");
1586 sony_backlight_device = NULL;
1587 } else
1588 sony_backlight_device->props.brightness =
1589 ops->get_brightness(sony_backlight_device);
1590}
1591
1592static void sony_nc_backlight_cleanup(void)
1593{
1594 if (sony_backlight_device)
1595 backlight_device_unregister(sony_backlight_device);
1596}
1597
malattia@linux.it59b19102007-04-09 10:19:04 +02001598static int sony_nc_add(struct acpi_device *device)
Stelian Pop7f09c432007-01-13 23:04:31 +01001599{
1600 acpi_status status;
Andrew Morton8607c672007-03-06 02:29:42 -08001601 int result = 0;
Alessandro Guido50f62af2007-01-13 23:04:34 +01001602 acpi_handle handle;
malattia@linux.it56b87562007-04-09 10:19:05 +02001603 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01001604
Mattia Dongilid6697932011-02-19 11:52:28 +09001605 pr_info(DRV_PFX "%s v%s.\n", SONY_NC_DRIVER_NAME,
1606 SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.itf6119b02007-04-09 19:31:06 +02001607
malattia@linux.it59b19102007-04-09 10:19:04 +02001608 sony_nc_acpi_device = device;
malattia@linux.it33a04452007-04-09 19:26:03 +02001609 strcpy(acpi_device_class(device), "sony/hotkey");
Stelian Popc5611622007-01-13 23:04:37 +01001610
malattia@linux.it59b19102007-04-09 10:19:04 +02001611 sony_nc_acpi_handle = device->handle;
Stelian Pop7f09c432007-01-13 23:04:31 +01001612
Mattia Dongilib25b7322007-07-16 02:34:36 +09001613 /* read device status */
1614 result = acpi_bus_get_status(device);
1615 /* bail IFF the above call was successful and the device is not present */
1616 if (!result && !device->status.present) {
1617 dprintk("Device not present\n");
1618 result = -ENODEV;
1619 goto outwalk;
1620 }
1621
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001622 result = sony_pf_add();
1623 if (result)
1624 goto outpresent;
1625
Stelian Pop7f09c432007-01-13 23:04:31 +01001626 if (debug) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001627 status = acpi_walk_namespace(ACPI_TYPE_METHOD,
1628 sony_nc_acpi_handle, 1, sony_walk_callback,
1629 NULL, NULL, NULL);
Stelian Pop7f09c432007-01-13 23:04:31 +01001630 if (ACPI_FAILURE(status)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001631 pr_warn(DRV_PFX "unable to walk acpi resources\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001632 result = -ENODEV;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001633 goto outpresent;
Stelian Pop7f09c432007-01-13 23:04:31 +01001634 }
Stelian Popc5611622007-01-13 23:04:37 +01001635 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001636
Matthew Garrett82734bf2009-03-26 21:58:13 +09001637 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1638 &handle))) {
1639 if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
1640 dprintk("ECON Method failed\n");
1641 }
1642
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001643 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1644 &handle))) {
1645 dprintk("Doing SNC setup\n");
Dan Carpenter7227ded2011-02-26 15:54:57 +03001646 result = sony_nc_handles_setup(sony_pf_device);
1647 if (result)
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001648 goto outpresent;
Dan Carpenter7227ded2011-02-26 15:54:57 +03001649 result = sony_nc_kbd_backlight_setup(sony_pf_device);
1650 if (result)
Mattia Dongilibf155712011-02-19 11:52:31 +09001651 goto outsnc;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001652 sony_nc_function_setup(device);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001653 sony_nc_rfkill_setup(device);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001654 }
1655
malattia@linux.it1549ee62007-04-09 10:19:08 +02001656 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05001657 result = sony_laptop_setup_input(device);
malattia@linux.it1549ee62007-04-09 10:19:08 +02001658 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001659 pr_err(DRV_PFX "Unable to create input devices.\n");
Mattia Dongilibf155712011-02-19 11:52:31 +09001660 goto outkbdbacklight;
malattia@linux.it1549ee62007-04-09 10:19:08 +02001661 }
1662
Alessandro Guido38cfc142008-11-12 23:03:28 +01001663 if (acpi_video_backlight_support()) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001664 pr_info(DRV_PFX "brightness ignored, must be "
Thomas Renninger540b8bb2008-08-01 17:38:02 +02001665 "controlled by ACPI video driver\n");
Mattia Dongili7751ab82011-02-19 11:52:32 +09001666 } else {
1667 sony_nc_backlight_setup();
Alessandro Guido50f62af2007-01-13 23:04:34 +01001668 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001669
malattia@linux.it56b87562007-04-09 10:19:05 +02001670 /* create sony_pf sysfs attributes related to the SNC device */
1671 for (item = sony_nc_values; item->name; ++item) {
1672
1673 if (!debug && item->debug)
1674 continue;
1675
1676 /* find the available acpiget as described in the DSDT */
1677 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
1678 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1679 *item->acpiget,
1680 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02001681 dprintk("Found %s getter: %s\n",
1682 item->name, *item->acpiget);
malattia@linux.it56b87562007-04-09 10:19:05 +02001683 item->devattr.attr.mode |= S_IRUGO;
1684 break;
1685 }
1686 }
1687
1688 /* find the available acpiset as described in the DSDT */
1689 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
1690 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1691 *item->acpiset,
1692 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02001693 dprintk("Found %s setter: %s\n",
1694 item->name, *item->acpiset);
malattia@linux.it56b87562007-04-09 10:19:05 +02001695 item->devattr.attr.mode |= S_IWUSR;
1696 break;
1697 }
1698 }
1699
1700 if (item->devattr.attr.mode != 0) {
1701 result =
1702 device_create_file(&sony_pf_device->dev,
1703 &item->devattr);
1704 if (result)
1705 goto out_sysfs;
1706 }
1707 }
1708
Stelian Pop7f09c432007-01-13 23:04:31 +01001709 return 0;
1710
malattia@linux.it56b87562007-04-09 10:19:05 +02001711 out_sysfs:
1712 for (item = sony_nc_values; item->name; ++item) {
1713 device_remove_file(&sony_pf_device->dev, &item->devattr);
1714 }
Mattia Dongili7751ab82011-02-19 11:52:32 +09001715 sony_nc_backlight_cleanup();
Mattia Dongili7df03b822007-01-13 23:04:41 +01001716
malattia@linux.it1549ee62007-04-09 10:19:08 +02001717 sony_laptop_remove_input();
1718
Mattia Dongilibf155712011-02-19 11:52:31 +09001719 outkbdbacklight:
1720 sony_nc_kbd_backlight_cleanup(sony_pf_device);
1721
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001722 outsnc:
1723 sony_nc_handles_cleanup(sony_pf_device);
1724
1725 outpresent:
1726 sony_pf_remove();
1727
Len Browna02d1c12007-02-07 15:34:02 -05001728 outwalk:
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001729 sony_nc_rfkill_cleanup();
Stelian Pop7f09c432007-01-13 23:04:31 +01001730 return result;
1731}
1732
malattia@linux.it59b19102007-04-09 10:19:04 +02001733static int sony_nc_remove(struct acpi_device *device, int type)
Stelian Pop7f09c432007-01-13 23:04:31 +01001734{
malattia@linux.it56b87562007-04-09 10:19:05 +02001735 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01001736
Mattia Dongili7751ab82011-02-19 11:52:32 +09001737 sony_nc_backlight_cleanup();
Alessandro Guido50f62af2007-01-13 23:04:34 +01001738
malattia@linux.it59b19102007-04-09 10:19:04 +02001739 sony_nc_acpi_device = NULL;
Stelian Popc5611622007-01-13 23:04:37 +01001740
malattia@linux.it56b87562007-04-09 10:19:05 +02001741 for (item = sony_nc_values; item->name; ++item) {
1742 device_remove_file(&sony_pf_device->dev, &item->devattr);
1743 }
1744
Mattia Dongilibf155712011-02-19 11:52:31 +09001745 sony_nc_kbd_backlight_cleanup(sony_pf_device);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001746 sony_nc_handles_cleanup(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +02001747 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +02001748 sony_laptop_remove_input();
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001749 sony_nc_rfkill_cleanup();
malattia@linux.itf6119b02007-04-09 19:31:06 +02001750 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001751
1752 return 0;
1753}
1754
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001755static const struct acpi_device_id sony_device_ids[] = {
1756 {SONY_NC_HID, 0},
1757 {SONY_PIC_HID, 0},
1758 {"", 0},
1759};
1760MODULE_DEVICE_TABLE(acpi, sony_device_ids);
1761
1762static const struct acpi_device_id sony_nc_device_ids[] = {
1763 {SONY_NC_HID, 0},
1764 {"", 0},
1765};
1766
malattia@linux.it59b19102007-04-09 10:19:04 +02001767static struct acpi_driver sony_nc_driver = {
1768 .name = SONY_NC_DRIVER_NAME,
1769 .class = SONY_NC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001770 .ids = sony_nc_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02001771 .owner = THIS_MODULE,
Len Browna02d1c12007-02-07 15:34:02 -05001772 .ops = {
malattia@linux.it59b19102007-04-09 10:19:04 +02001773 .add = sony_nc_add,
1774 .remove = sony_nc_remove,
1775 .resume = sony_nc_resume,
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001776 .notify = sony_nc_notify,
Len Browna02d1c12007-02-07 15:34:02 -05001777 },
Andrew Morton3f4f4612007-01-13 23:04:32 +01001778};
1779
malattia@linux.it33a04452007-04-09 19:26:03 +02001780/*********** SPIC (SNY6001) Device ***********/
1781
1782#define SONYPI_DEVICE_TYPE1 0x00000001
1783#define SONYPI_DEVICE_TYPE2 0x00000002
1784#define SONYPI_DEVICE_TYPE3 0x00000004
1785
Mattia Dongili22a17782007-07-16 02:34:39 +09001786#define SONYPI_TYPE1_OFFSET 0x04
1787#define SONYPI_TYPE2_OFFSET 0x12
1788#define SONYPI_TYPE3_OFFSET 0x12
malattia@linux.it33a04452007-04-09 19:26:03 +02001789
malattia@linux.it33a04452007-04-09 19:26:03 +02001790struct sony_pic_ioport {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001791 struct acpi_resource_io io1;
1792 struct acpi_resource_io io2;
malattia@linux.it33a04452007-04-09 19:26:03 +02001793 struct list_head list;
1794};
1795
1796struct sony_pic_irq {
1797 struct acpi_resource_irq irq;
1798 struct list_head list;
1799};
1800
Mattia Dongilide920432008-01-14 18:05:43 +09001801struct sonypi_eventtypes {
1802 u8 data;
1803 unsigned long mask;
1804 struct sonypi_event *events;
1805};
1806
malattia@linux.it33a04452007-04-09 19:26:03 +02001807struct sony_pic_dev {
Mattia Dongili31df7142009-09-16 00:05:29 +09001808 struct acpi_device *acpi_dev;
1809 struct sony_pic_irq *cur_irq;
1810 struct sony_pic_ioport *cur_ioport;
1811 struct list_head interrupts;
1812 struct list_head ioports;
1813 struct mutex lock;
1814 struct sonypi_eventtypes *event_types;
1815 int (*handle_irq)(const u8, const u8);
1816 int model;
1817 u16 evport_offset;
1818 u8 camera_power;
1819 u8 bluetooth_power;
1820 u8 wwan_power;
malattia@linux.it33a04452007-04-09 19:26:03 +02001821};
1822
1823static struct sony_pic_dev spic_dev = {
1824 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
1825 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
1826};
1827
Alan Jenkins5e6f9722009-09-16 00:05:32 +09001828static int spic_drv_registered;
1829
malattia@linux.it33a04452007-04-09 19:26:03 +02001830/* Event masks */
1831#define SONYPI_JOGGER_MASK 0x00000001
1832#define SONYPI_CAPTURE_MASK 0x00000002
1833#define SONYPI_FNKEY_MASK 0x00000004
1834#define SONYPI_BLUETOOTH_MASK 0x00000008
1835#define SONYPI_PKEY_MASK 0x00000010
1836#define SONYPI_BACK_MASK 0x00000020
1837#define SONYPI_HELP_MASK 0x00000040
1838#define SONYPI_LID_MASK 0x00000080
1839#define SONYPI_ZOOM_MASK 0x00000100
1840#define SONYPI_THUMBPHRASE_MASK 0x00000200
1841#define SONYPI_MEYE_MASK 0x00000400
1842#define SONYPI_MEMORYSTICK_MASK 0x00000800
1843#define SONYPI_BATTERY_MASK 0x00001000
1844#define SONYPI_WIRELESS_MASK 0x00002000
1845
1846struct sonypi_event {
1847 u8 data;
1848 u8 event;
1849};
1850
1851/* The set of possible button release events */
1852static struct sonypi_event sonypi_releaseev[] = {
1853 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
1854 { 0, 0 }
1855};
1856
1857/* The set of possible jogger events */
1858static struct sonypi_event sonypi_joggerev[] = {
1859 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
1860 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
1861 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
1862 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
1863 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
1864 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
1865 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
1866 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
1867 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
1868 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
1869 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
1870 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
1871 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
1872 { 0, 0 }
1873};
1874
1875/* The set of possible capture button events */
1876static struct sonypi_event sonypi_captureev[] = {
1877 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
1878 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09001879 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02001880 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
1881 { 0, 0 }
1882};
1883
1884/* The set of possible fnkeys events */
1885static struct sonypi_event sonypi_fnkeyev[] = {
1886 { 0x10, SONYPI_EVENT_FNKEY_ESC },
1887 { 0x11, SONYPI_EVENT_FNKEY_F1 },
1888 { 0x12, SONYPI_EVENT_FNKEY_F2 },
1889 { 0x13, SONYPI_EVENT_FNKEY_F3 },
1890 { 0x14, SONYPI_EVENT_FNKEY_F4 },
1891 { 0x15, SONYPI_EVENT_FNKEY_F5 },
1892 { 0x16, SONYPI_EVENT_FNKEY_F6 },
1893 { 0x17, SONYPI_EVENT_FNKEY_F7 },
1894 { 0x18, SONYPI_EVENT_FNKEY_F8 },
1895 { 0x19, SONYPI_EVENT_FNKEY_F9 },
1896 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
1897 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
1898 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
1899 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
1900 { 0x21, SONYPI_EVENT_FNKEY_1 },
1901 { 0x22, SONYPI_EVENT_FNKEY_2 },
1902 { 0x31, SONYPI_EVENT_FNKEY_D },
1903 { 0x32, SONYPI_EVENT_FNKEY_E },
1904 { 0x33, SONYPI_EVENT_FNKEY_F },
1905 { 0x34, SONYPI_EVENT_FNKEY_S },
1906 { 0x35, SONYPI_EVENT_FNKEY_B },
1907 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
1908 { 0, 0 }
1909};
1910
1911/* The set of possible program key events */
1912static struct sonypi_event sonypi_pkeyev[] = {
1913 { 0x01, SONYPI_EVENT_PKEY_P1 },
1914 { 0x02, SONYPI_EVENT_PKEY_P2 },
1915 { 0x04, SONYPI_EVENT_PKEY_P3 },
Harald Jenny1cae7102009-03-26 21:58:18 +09001916 { 0x20, SONYPI_EVENT_PKEY_P1 },
malattia@linux.it33a04452007-04-09 19:26:03 +02001917 { 0, 0 }
1918};
1919
1920/* The set of possible bluetooth events */
1921static struct sonypi_event sonypi_blueev[] = {
1922 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
1923 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
1924 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
1925 { 0, 0 }
1926};
1927
1928/* The set of possible wireless events */
1929static struct sonypi_event sonypi_wlessev[] = {
Mattia Dongili4eeb5022011-02-19 11:52:27 +09001930 { 0x59, SONYPI_EVENT_IGNORE },
1931 { 0x5a, SONYPI_EVENT_IGNORE },
malattia@linux.it33a04452007-04-09 19:26:03 +02001932 { 0, 0 }
1933};
1934
1935/* The set of possible back button events */
1936static struct sonypi_event sonypi_backev[] = {
1937 { 0x20, SONYPI_EVENT_BACK_PRESSED },
1938 { 0, 0 }
1939};
1940
1941/* The set of possible help button events */
1942static struct sonypi_event sonypi_helpev[] = {
1943 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
1944 { 0, 0 }
1945};
1946
1947
1948/* The set of possible lid events */
1949static struct sonypi_event sonypi_lidev[] = {
1950 { 0x51, SONYPI_EVENT_LID_CLOSED },
1951 { 0x50, SONYPI_EVENT_LID_OPENED },
1952 { 0, 0 }
1953};
1954
1955/* The set of possible zoom events */
1956static struct sonypi_event sonypi_zoomev[] = {
1957 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09001958 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
1959 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
Harald Jenny1cae7102009-03-26 21:58:18 +09001960 { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02001961 { 0, 0 }
1962};
1963
1964/* The set of possible thumbphrase events */
1965static struct sonypi_event sonypi_thumbphraseev[] = {
1966 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
1967 { 0, 0 }
1968};
1969
1970/* The set of possible motioneye camera events */
1971static struct sonypi_event sonypi_meyeev[] = {
1972 { 0x00, SONYPI_EVENT_MEYE_FACE },
1973 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
1974 { 0, 0 }
1975};
1976
1977/* The set of possible memorystick events */
1978static struct sonypi_event sonypi_memorystickev[] = {
1979 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
1980 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
1981 { 0, 0 }
1982};
1983
1984/* The set of possible battery events */
1985static struct sonypi_event sonypi_batteryev[] = {
1986 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
1987 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
1988 { 0, 0 }
1989};
1990
Harald Jenny1cae7102009-03-26 21:58:18 +09001991/* The set of possible volume events */
1992static struct sonypi_event sonypi_volumeev[] = {
1993 { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
1994 { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
1995 { 0, 0 }
1996};
1997
1998/* The set of possible brightness events */
1999static struct sonypi_event sonypi_brightnessev[] = {
2000 { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
2001 { 0, 0 }
2002};
2003
Mattia Dongilide920432008-01-14 18:05:43 +09002004static struct sonypi_eventtypes type1_events[] = {
2005 { 0, 0xffffffff, sonypi_releaseev },
2006 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
2007 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
2008 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
2009 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
2010 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2011 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
2012 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
2013 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2014 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
2015 { 0 },
2016};
2017static struct sonypi_eventtypes type2_events[] = {
2018 { 0, 0xffffffff, sonypi_releaseev },
2019 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
2020 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
2021 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
2022 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2023 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
2024 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
2025 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
2026 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
2027 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
2028 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
2029 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2030 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
2031 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
2032 { 0 },
2033};
2034static struct sonypi_eventtypes type3_events[] = {
2035 { 0, 0xffffffff, sonypi_releaseev },
2036 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2037 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
2038 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2039 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
2040 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002041 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
2042 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
2043 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
Harald Jenny1cae7102009-03-26 21:58:18 +09002044 { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
2045 { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002046 { 0 },
Mattia Dongilide920432008-01-14 18:05:43 +09002047};
2048
Mattia Dongili3eb87492008-01-14 18:05:45 +09002049/* low level spic calls */
malattia@linux.it33a04452007-04-09 19:26:03 +02002050#define ITERATIONS_LONG 10000
2051#define ITERATIONS_SHORT 10
2052#define wait_on_command(command, iterations) { \
2053 unsigned int n = iterations; \
2054 while (--n && (command)) \
2055 udelay(1); \
2056 if (!n) \
2057 dprintk("command failed at %s : %s (line %d)\n", \
Harvey Harrison6e574192008-04-29 00:59:20 -07002058 __FILE__, __func__, __LINE__); \
malattia@linux.it33a04452007-04-09 19:26:03 +02002059}
2060
2061static u8 sony_pic_call1(u8 dev)
2062{
2063 u8 v1, v2;
2064
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002065 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002066 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002067 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2068 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
2069 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002070 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02002071 return v2;
2072}
2073
2074static u8 sony_pic_call2(u8 dev, u8 fn)
2075{
2076 u8 v1;
2077
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002078 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002079 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002080 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2081 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002082 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002083 outb(fn, spic_dev.cur_ioport->io1.minimum);
2084 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002085 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02002086 return v1;
2087}
2088
malattia@linux.it49a11de2007-04-09 19:28:56 +02002089static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
2090{
2091 u8 v1;
2092
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002093 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2094 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2095 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2096 outb(fn, spic_dev.cur_ioport->io1.minimum);
2097 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2098 outb(v, spic_dev.cur_ioport->io1.minimum);
2099 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002100 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
2101 dev, fn, v, v1);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002102 return v1;
2103}
2104
Mattia Dongili3eb87492008-01-14 18:05:45 +09002105/*
2106 * minidrivers for SPIC models
2107 */
Mattia Dongilie93c8a62009-03-26 21:58:17 +09002108static int type3_handle_irq(const u8 data_mask, const u8 ev)
Mattia Dongili3eb87492008-01-14 18:05:45 +09002109{
2110 /*
2111 * 0x31 could mean we have to take some extra action and wait for
Mattia Dongilie93c8a62009-03-26 21:58:17 +09002112 * the next irq for some Type3 models, it will generate a new
Mattia Dongili3eb87492008-01-14 18:05:45 +09002113 * irq and we can read new data from the device:
2114 * - 0x5c and 0x5f requires 0xA0
2115 * - 0x61 requires 0xB3
2116 */
2117 if (data_mask == 0x31) {
2118 if (ev == 0x5c || ev == 0x5f)
2119 sony_pic_call1(0xA0);
2120 else if (ev == 0x61)
2121 sony_pic_call1(0xB3);
2122 return 0;
2123 }
2124 return 1;
2125}
2126
Mattia Dongili3eb87492008-01-14 18:05:45 +09002127static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
2128{
2129 struct pci_dev *pcidev;
2130
2131 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2132 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
2133 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002134 dev->model = SONYPI_DEVICE_TYPE1;
2135 dev->evport_offset = SONYPI_TYPE1_OFFSET;
2136 dev->event_types = type1_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002137 goto out;
2138 }
2139
2140 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2141 PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
2142 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002143 dev->model = SONYPI_DEVICE_TYPE2;
2144 dev->evport_offset = SONYPI_TYPE2_OFFSET;
2145 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002146 goto out;
2147 }
2148
2149 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2150 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
2151 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002152 dev->model = SONYPI_DEVICE_TYPE3;
2153 dev->handle_irq = type3_handle_irq;
2154 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2155 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002156 goto out;
2157 }
2158
2159 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2160 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
2161 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002162 dev->model = SONYPI_DEVICE_TYPE3;
2163 dev->handle_irq = type3_handle_irq;
2164 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2165 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002166 goto out;
2167 }
2168
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09002169 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2170 PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
2171 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002172 dev->model = SONYPI_DEVICE_TYPE3;
2173 dev->handle_irq = type3_handle_irq;
2174 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2175 dev->event_types = type3_events;
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09002176 goto out;
2177 }
2178
Mattia Dongili3eb87492008-01-14 18:05:45 +09002179 /* default */
Mattia Dongili31df7142009-09-16 00:05:29 +09002180 dev->model = SONYPI_DEVICE_TYPE2;
2181 dev->evport_offset = SONYPI_TYPE2_OFFSET;
2182 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002183
2184out:
2185 if (pcidev)
2186 pci_dev_put(pcidev);
2187
Mattia Dongilid6697932011-02-19 11:52:28 +09002188 pr_info(DRV_PFX "detected Type%d model\n",
Mattia Dongili31df7142009-09-16 00:05:29 +09002189 dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
2190 dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
Mattia Dongili3eb87492008-01-14 18:05:45 +09002191}
2192
malattia@linux.it49a11de2007-04-09 19:28:56 +02002193/* camera tests and poweron/poweroff */
2194#define SONYPI_CAMERA_PICTURE 5
malattia@linux.it49a11de2007-04-09 19:28:56 +02002195#define SONYPI_CAMERA_CONTROL 0x10
malattia@linux.ite3646322007-04-28 23:34:22 +09002196
2197#define SONYPI_CAMERA_BRIGHTNESS 0
2198#define SONYPI_CAMERA_CONTRAST 1
2199#define SONYPI_CAMERA_HUE 2
2200#define SONYPI_CAMERA_COLOR 3
2201#define SONYPI_CAMERA_SHARPNESS 4
2202
2203#define SONYPI_CAMERA_EXPOSURE_MASK 0xC
2204#define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
2205#define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
2206#define SONYPI_CAMERA_MUTE_MASK 0x40
2207
2208/* the rest don't need a loop until not 0xff */
2209#define SONYPI_CAMERA_AGC 6
2210#define SONYPI_CAMERA_AGC_MASK 0x30
2211#define SONYPI_CAMERA_SHUTTER_MASK 0x7
2212
2213#define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
2214#define SONYPI_CAMERA_CONTROL 0x10
2215
2216#define SONYPI_CAMERA_STATUS 7
2217#define SONYPI_CAMERA_STATUS_READY 0x2
2218#define SONYPI_CAMERA_STATUS_POSITION 0x4
2219
2220#define SONYPI_DIRECTION_BACKWARDS 0x4
2221
2222#define SONYPI_CAMERA_REVISION 8
2223#define SONYPI_CAMERA_ROMVERSION 9
malattia@linux.it49a11de2007-04-09 19:28:56 +02002224
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002225static int __sony_pic_camera_ready(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002226{
2227 u8 v;
2228
2229 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
2230 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
2231}
2232
malattia@linux.ite3646322007-04-28 23:34:22 +09002233static int __sony_pic_camera_off(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002234{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002235 if (!camera) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002236 pr_warn(DRV_PFX "camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002237 return -ENODEV;
2238 }
2239
malattia@linux.it49a11de2007-04-09 19:28:56 +02002240 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
2241 SONYPI_CAMERA_MUTE_MASK),
2242 ITERATIONS_SHORT);
2243
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002244 if (spic_dev.camera_power) {
2245 sony_pic_call2(0x91, 0);
2246 spic_dev.camera_power = 0;
2247 }
2248 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002249}
2250
malattia@linux.ite3646322007-04-28 23:34:22 +09002251static int __sony_pic_camera_on(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002252{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002253 int i, j, x;
2254
2255 if (!camera) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002256 pr_warn(DRV_PFX "camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002257 return -ENODEV;
2258 }
malattia@linux.it49a11de2007-04-09 19:28:56 +02002259
2260 if (spic_dev.camera_power)
malattia@linux.ite3646322007-04-28 23:34:22 +09002261 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002262
2263 for (j = 5; j > 0; j--) {
2264
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002265 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002266 msleep(10);
2267 sony_pic_call1(0x93);
2268
2269 for (i = 400; i > 0; i--) {
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002270 if (__sony_pic_camera_ready())
malattia@linux.it49a11de2007-04-09 19:28:56 +02002271 break;
2272 msleep(10);
2273 }
2274 if (i)
2275 break;
2276 }
2277
2278 if (j == 0) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002279 pr_warn(DRV_PFX "failed to power on camera\n");
malattia@linux.ite3646322007-04-28 23:34:22 +09002280 return -ENODEV;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002281 }
2282
2283 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
2284 0x5a),
2285 ITERATIONS_SHORT);
2286
2287 spic_dev.camera_power = 1;
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002288 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002289}
2290
malattia@linux.ite3646322007-04-28 23:34:22 +09002291/* External camera command (exported to the motion eye v4l driver) */
2292int sony_pic_camera_command(int command, u8 value)
2293{
2294 if (!camera)
2295 return -EIO;
2296
2297 mutex_lock(&spic_dev.lock);
2298
2299 switch (command) {
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002300 case SONY_PIC_COMMAND_SETCAMERA:
malattia@linux.ite3646322007-04-28 23:34:22 +09002301 if (value)
2302 __sony_pic_camera_on();
2303 else
2304 __sony_pic_camera_off();
2305 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002306 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09002307 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
2308 ITERATIONS_SHORT);
2309 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002310 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
malattia@linux.ite3646322007-04-28 23:34:22 +09002311 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
2312 ITERATIONS_SHORT);
2313 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002314 case SONY_PIC_COMMAND_SETCAMERAHUE:
malattia@linux.ite3646322007-04-28 23:34:22 +09002315 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
2316 ITERATIONS_SHORT);
2317 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002318 case SONY_PIC_COMMAND_SETCAMERACOLOR:
malattia@linux.ite3646322007-04-28 23:34:22 +09002319 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
2320 ITERATIONS_SHORT);
2321 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002322 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09002323 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
2324 ITERATIONS_SHORT);
2325 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002326 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
malattia@linux.ite3646322007-04-28 23:34:22 +09002327 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
2328 ITERATIONS_SHORT);
2329 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002330 case SONY_PIC_COMMAND_SETCAMERAAGC:
malattia@linux.ite3646322007-04-28 23:34:22 +09002331 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
2332 ITERATIONS_SHORT);
2333 break;
2334 default:
Mattia Dongilid6697932011-02-19 11:52:28 +09002335 pr_err(DRV_PFX "sony_pic_camera_command invalid: %d\n",
malattia@linux.ite3646322007-04-28 23:34:22 +09002336 command);
2337 break;
2338 }
2339 mutex_unlock(&spic_dev.lock);
2340 return 0;
2341}
2342EXPORT_SYMBOL(sony_pic_camera_command);
2343
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002344/* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002345static void __sony_pic_set_wwanpower(u8 state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002346{
2347 state = !!state;
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002348 if (spic_dev.wwan_power == state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002349 return;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002350 sony_pic_call2(0xB0, state);
Sergey Yanovich3ad1b762009-03-26 21:58:21 +09002351 sony_pic_call1(0x82);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002352 spic_dev.wwan_power = state;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002353}
2354
2355static ssize_t sony_pic_wwanpower_store(struct device *dev,
2356 struct device_attribute *attr,
2357 const char *buffer, size_t count)
2358{
2359 unsigned long value;
2360 if (count > 31)
2361 return -EINVAL;
2362
2363 value = simple_strtoul(buffer, NULL, 10);
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002364 mutex_lock(&spic_dev.lock);
2365 __sony_pic_set_wwanpower(value);
2366 mutex_unlock(&spic_dev.lock);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002367
2368 return count;
2369}
2370
2371static ssize_t sony_pic_wwanpower_show(struct device *dev,
2372 struct device_attribute *attr, char *buffer)
2373{
2374 ssize_t count;
2375 mutex_lock(&spic_dev.lock);
2376 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
2377 mutex_unlock(&spic_dev.lock);
2378 return count;
2379}
2380
malattia@linux.it49a11de2007-04-09 19:28:56 +02002381/* bluetooth subsystem power state */
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002382static void __sony_pic_set_bluetoothpower(u8 state)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002383{
2384 state = !!state;
2385 if (spic_dev.bluetooth_power == state)
2386 return;
2387 sony_pic_call2(0x96, state);
2388 sony_pic_call1(0x82);
2389 spic_dev.bluetooth_power = state;
2390}
2391
2392static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
2393 struct device_attribute *attr,
2394 const char *buffer, size_t count)
2395{
2396 unsigned long value;
2397 if (count > 31)
2398 return -EINVAL;
2399
2400 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002401 mutex_lock(&spic_dev.lock);
2402 __sony_pic_set_bluetoothpower(value);
2403 mutex_unlock(&spic_dev.lock);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002404
2405 return count;
2406}
2407
2408static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
2409 struct device_attribute *attr, char *buffer)
2410{
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002411 ssize_t count = 0;
2412 mutex_lock(&spic_dev.lock);
2413 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
2414 mutex_unlock(&spic_dev.lock);
2415 return count;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002416}
2417
2418/* fan speed */
2419/* FAN0 information (reverse engineered from ACPI tables) */
2420#define SONY_PIC_FAN0_STATUS 0x93
malattia@linux.it7b153f32007-04-09 19:31:25 +02002421static int sony_pic_set_fanspeed(unsigned long value)
2422{
2423 return ec_write(SONY_PIC_FAN0_STATUS, value);
2424}
2425
2426static int sony_pic_get_fanspeed(u8 *value)
2427{
2428 return ec_read(SONY_PIC_FAN0_STATUS, value);
2429}
2430
malattia@linux.it49a11de2007-04-09 19:28:56 +02002431static ssize_t sony_pic_fanspeed_store(struct device *dev,
2432 struct device_attribute *attr,
2433 const char *buffer, size_t count)
2434{
2435 unsigned long value;
2436 if (count > 31)
2437 return -EINVAL;
2438
2439 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002440 if (sony_pic_set_fanspeed(value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02002441 return -EIO;
2442
2443 return count;
2444}
2445
2446static ssize_t sony_pic_fanspeed_show(struct device *dev,
2447 struct device_attribute *attr, char *buffer)
2448{
2449 u8 value = 0;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002450 if (sony_pic_get_fanspeed(&value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02002451 return -EIO;
2452
2453 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
2454}
2455
2456#define SPIC_ATTR(_name, _mode) \
2457struct device_attribute spic_attr_##_name = __ATTR(_name, \
2458 _mode, sony_pic_## _name ##_show, \
2459 sony_pic_## _name ##_store)
2460
malattia@linux.it49a11de2007-04-09 19:28:56 +02002461static SPIC_ATTR(bluetoothpower, 0644);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002462static SPIC_ATTR(wwanpower, 0644);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002463static SPIC_ATTR(fanspeed, 0644);
2464
2465static struct attribute *spic_attributes[] = {
malattia@linux.it49a11de2007-04-09 19:28:56 +02002466 &spic_attr_bluetoothpower.attr,
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002467 &spic_attr_wwanpower.attr,
malattia@linux.it49a11de2007-04-09 19:28:56 +02002468 &spic_attr_fanspeed.attr,
2469 NULL
2470};
2471
2472static struct attribute_group spic_attribute_group = {
2473 .attrs = spic_attributes
2474};
2475
malattia@linux.it7b153f32007-04-09 19:31:25 +02002476/******** SONYPI compatibility **********/
Mattia Dongilia64e62a2007-05-01 11:19:53 +09002477#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +02002478
2479/* battery / brightness / temperature addresses */
2480#define SONYPI_BAT_FLAGS 0x81
2481#define SONYPI_LCD_LIGHT 0x96
2482#define SONYPI_BAT1_PCTRM 0xa0
2483#define SONYPI_BAT1_LEFT 0xa2
2484#define SONYPI_BAT1_MAXRT 0xa4
2485#define SONYPI_BAT2_PCTRM 0xa8
2486#define SONYPI_BAT2_LEFT 0xaa
2487#define SONYPI_BAT2_MAXRT 0xac
2488#define SONYPI_BAT1_MAXTK 0xb0
2489#define SONYPI_BAT1_FULL 0xb2
2490#define SONYPI_BAT2_MAXTK 0xb8
2491#define SONYPI_BAT2_FULL 0xba
2492#define SONYPI_TEMP_STATUS 0xC1
2493
2494struct sonypi_compat_s {
2495 struct fasync_struct *fifo_async;
Stefani Seibold45465482009-12-21 14:37:26 -08002496 struct kfifo fifo;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002497 spinlock_t fifo_lock;
2498 wait_queue_head_t fifo_proc_list;
2499 atomic_t open_count;
2500};
2501static struct sonypi_compat_s sonypi_compat = {
2502 .open_count = ATOMIC_INIT(0),
2503};
2504
2505static int sonypi_misc_fasync(int fd, struct file *filp, int on)
2506{
Jonathan Corbet60aa4922009-02-01 14:52:56 -07002507 return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002508}
2509
2510static int sonypi_misc_release(struct inode *inode, struct file *file)
2511{
malattia@linux.it7b153f32007-04-09 19:31:25 +02002512 atomic_dec(&sonypi_compat.open_count);
2513 return 0;
2514}
2515
2516static int sonypi_misc_open(struct inode *inode, struct file *file)
2517{
2518 /* Flush input queue on first open */
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002519 unsigned long flags;
2520
Stefani Seibold45465482009-12-21 14:37:26 -08002521 spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002522
malattia@linux.it7b153f32007-04-09 19:31:25 +02002523 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
Stefani Seibolde64c0262009-12-21 14:37:28 -08002524 kfifo_reset(&sonypi_compat.fifo);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002525
Stefani Seibold45465482009-12-21 14:37:26 -08002526 spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002527
malattia@linux.it7b153f32007-04-09 19:31:25 +02002528 return 0;
2529}
2530
2531static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
2532 size_t count, loff_t *pos)
2533{
2534 ssize_t ret;
2535 unsigned char c;
2536
Stefani Seibold45465482009-12-21 14:37:26 -08002537 if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
malattia@linux.it7b153f32007-04-09 19:31:25 +02002538 (file->f_flags & O_NONBLOCK))
2539 return -EAGAIN;
2540
2541 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
Stefani Seibold45465482009-12-21 14:37:26 -08002542 kfifo_len(&sonypi_compat.fifo) != 0);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002543 if (ret)
2544 return ret;
2545
2546 while (ret < count &&
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002547 (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002548 &sonypi_compat.fifo_lock) == sizeof(c))) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02002549 if (put_user(c, buf++))
2550 return -EFAULT;
2551 ret++;
2552 }
2553
2554 if (ret > 0) {
2555 struct inode *inode = file->f_path.dentry->d_inode;
2556 inode->i_atime = current_fs_time(inode->i_sb);
2557 }
2558
2559 return ret;
2560}
2561
2562static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
2563{
2564 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
Stefani Seibold45465482009-12-21 14:37:26 -08002565 if (kfifo_len(&sonypi_compat.fifo))
malattia@linux.it7b153f32007-04-09 19:31:25 +02002566 return POLLIN | POLLRDNORM;
2567 return 0;
2568}
2569
2570static int ec_read16(u8 addr, u16 *value)
2571{
2572 u8 val_lb, val_hb;
2573 if (ec_read(addr, &val_lb))
2574 return -1;
2575 if (ec_read(addr + 1, &val_hb))
2576 return -1;
2577 *value = val_lb | (val_hb << 8);
2578 return 0;
2579}
2580
Alan Cox2b24ef02009-03-26 21:58:19 +09002581static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
2582 unsigned long arg)
malattia@linux.it7b153f32007-04-09 19:31:25 +02002583{
2584 int ret = 0;
2585 void __user *argp = (void __user *)arg;
2586 u8 val8;
2587 u16 val16;
2588 int value;
2589
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002590 mutex_lock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002591 switch (cmd) {
2592 case SONYPI_IOCGBRT:
2593 if (sony_backlight_device == NULL) {
2594 ret = -EIO;
2595 break;
2596 }
2597 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
2598 ret = -EIO;
2599 break;
2600 }
2601 val8 = ((value & 0xff) - 1) << 5;
2602 if (copy_to_user(argp, &val8, sizeof(val8)))
2603 ret = -EFAULT;
2604 break;
2605 case SONYPI_IOCSBRT:
2606 if (sony_backlight_device == NULL) {
2607 ret = -EIO;
2608 break;
2609 }
2610 if (copy_from_user(&val8, argp, sizeof(val8))) {
2611 ret = -EFAULT;
2612 break;
2613 }
2614 if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
2615 (val8 >> 5) + 1, NULL)) {
2616 ret = -EIO;
2617 break;
2618 }
2619 /* sync the backlight device status */
2620 sony_backlight_device->props.brightness =
2621 sony_backlight_get_brightness(sony_backlight_device);
2622 break;
2623 case SONYPI_IOCGBAT1CAP:
2624 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
2625 ret = -EIO;
2626 break;
2627 }
2628 if (copy_to_user(argp, &val16, sizeof(val16)))
2629 ret = -EFAULT;
2630 break;
2631 case SONYPI_IOCGBAT1REM:
2632 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
2633 ret = -EIO;
2634 break;
2635 }
2636 if (copy_to_user(argp, &val16, sizeof(val16)))
2637 ret = -EFAULT;
2638 break;
2639 case SONYPI_IOCGBAT2CAP:
2640 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
2641 ret = -EIO;
2642 break;
2643 }
2644 if (copy_to_user(argp, &val16, sizeof(val16)))
2645 ret = -EFAULT;
2646 break;
2647 case SONYPI_IOCGBAT2REM:
2648 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
2649 ret = -EIO;
2650 break;
2651 }
2652 if (copy_to_user(argp, &val16, sizeof(val16)))
2653 ret = -EFAULT;
2654 break;
2655 case SONYPI_IOCGBATFLAGS:
2656 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
2657 ret = -EIO;
2658 break;
2659 }
2660 val8 &= 0x07;
2661 if (copy_to_user(argp, &val8, sizeof(val8)))
2662 ret = -EFAULT;
2663 break;
2664 case SONYPI_IOCGBLUE:
2665 val8 = spic_dev.bluetooth_power;
2666 if (copy_to_user(argp, &val8, sizeof(val8)))
2667 ret = -EFAULT;
2668 break;
2669 case SONYPI_IOCSBLUE:
2670 if (copy_from_user(&val8, argp, sizeof(val8))) {
2671 ret = -EFAULT;
2672 break;
2673 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002674 __sony_pic_set_bluetoothpower(val8);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002675 break;
2676 /* FAN Controls */
2677 case SONYPI_IOCGFAN:
2678 if (sony_pic_get_fanspeed(&val8)) {
2679 ret = -EIO;
2680 break;
2681 }
2682 if (copy_to_user(argp, &val8, sizeof(val8)))
2683 ret = -EFAULT;
2684 break;
2685 case SONYPI_IOCSFAN:
2686 if (copy_from_user(&val8, argp, sizeof(val8))) {
2687 ret = -EFAULT;
2688 break;
2689 }
2690 if (sony_pic_set_fanspeed(val8))
2691 ret = -EIO;
2692 break;
2693 /* GET Temperature (useful under APM) */
2694 case SONYPI_IOCGTEMP:
2695 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
2696 ret = -EIO;
2697 break;
2698 }
2699 if (copy_to_user(argp, &val8, sizeof(val8)))
2700 ret = -EFAULT;
2701 break;
2702 default:
2703 ret = -EINVAL;
2704 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002705 mutex_unlock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002706 return ret;
2707}
2708
2709static const struct file_operations sonypi_misc_fops = {
2710 .owner = THIS_MODULE,
2711 .read = sonypi_misc_read,
2712 .poll = sonypi_misc_poll,
2713 .open = sonypi_misc_open,
2714 .release = sonypi_misc_release,
2715 .fasync = sonypi_misc_fasync,
Alan Cox2b24ef02009-03-26 21:58:19 +09002716 .unlocked_ioctl = sonypi_misc_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002717 .llseek = noop_llseek,
malattia@linux.it7b153f32007-04-09 19:31:25 +02002718};
2719
2720static struct miscdevice sonypi_misc_device = {
2721 .minor = MISC_DYNAMIC_MINOR,
2722 .name = "sonypi",
2723 .fops = &sonypi_misc_fops,
2724};
2725
2726static void sonypi_compat_report_event(u8 event)
2727{
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002728 kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002729 sizeof(event), &sonypi_compat.fifo_lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002730 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
2731 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
2732}
2733
2734static int sonypi_compat_init(void)
2735{
2736 int error;
2737
2738 spin_lock_init(&sonypi_compat.fifo_lock);
Stefani Seibold45465482009-12-21 14:37:26 -08002739 error =
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002740 kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -08002741 if (error) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002742 pr_err(DRV_PFX "kfifo_alloc failed\n");
Stefani Seibold45465482009-12-21 14:37:26 -08002743 return error;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002744 }
2745
2746 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002747
2748 if (minor != -1)
2749 sonypi_misc_device.minor = minor;
2750 error = misc_register(&sonypi_misc_device);
2751 if (error) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002752 pr_err(DRV_PFX "misc_register failed\n");
malattia@linux.it7b153f32007-04-09 19:31:25 +02002753 goto err_free_kfifo;
2754 }
2755 if (minor == -1)
Mattia Dongilid6697932011-02-19 11:52:28 +09002756 pr_info(DRV_PFX "device allocated minor is %d\n",
malattia@linux.it7b153f32007-04-09 19:31:25 +02002757 sonypi_misc_device.minor);
2758
2759 return 0;
2760
2761err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -08002762 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002763 return error;
2764}
2765
2766static void sonypi_compat_exit(void)
2767{
2768 misc_deregister(&sonypi_misc_device);
Stefani Seibold45465482009-12-21 14:37:26 -08002769 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002770}
2771#else
2772static int sonypi_compat_init(void) { return 0; }
2773static void sonypi_compat_exit(void) { }
2774static void sonypi_compat_report_event(u8 event) { }
Mattia Dongilia64e62a2007-05-01 11:19:53 +09002775#endif /* CONFIG_SONYPI_COMPAT */
malattia@linux.it7b153f32007-04-09 19:31:25 +02002776
malattia@linux.it1549ee62007-04-09 10:19:08 +02002777/*
malattia@linux.it33a04452007-04-09 19:26:03 +02002778 * ACPI callbacks
malattia@linux.it1549ee62007-04-09 10:19:08 +02002779 */
malattia@linux.it33a04452007-04-09 19:26:03 +02002780static acpi_status
2781sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
2782{
2783 u32 i;
2784 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
2785
2786 switch (resource->type) {
2787 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002788 {
2789 /* start IO enumeration */
2790 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
2791 if (!ioport)
2792 return AE_ERROR;
2793
2794 list_add(&ioport->list, &dev->ioports);
2795 return AE_OK;
2796 }
2797
malattia@linux.it33a04452007-04-09 19:26:03 +02002798 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002799 /* end IO enumeration */
malattia@linux.it33a04452007-04-09 19:26:03 +02002800 return AE_OK;
2801
2802 case ACPI_RESOURCE_TYPE_IRQ:
2803 {
2804 struct acpi_resource_irq *p = &resource->data.irq;
2805 struct sony_pic_irq *interrupt = NULL;
2806 if (!p || !p->interrupt_count) {
2807 /*
2808 * IRQ descriptors may have no IRQ# bits set,
2809 * particularly those those w/ _STA disabled
2810 */
2811 dprintk("Blank IRQ resource\n");
2812 return AE_OK;
2813 }
2814 for (i = 0; i < p->interrupt_count; i++) {
2815 if (!p->interrupts[i]) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002816 pr_warn(DRV_PFX "Invalid IRQ %d\n",
malattia@linux.it33a04452007-04-09 19:26:03 +02002817 p->interrupts[i]);
2818 continue;
2819 }
2820 interrupt = kzalloc(sizeof(*interrupt),
2821 GFP_KERNEL);
2822 if (!interrupt)
2823 return AE_ERROR;
2824
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002825 list_add(&interrupt->list, &dev->interrupts);
malattia@linux.it33a04452007-04-09 19:26:03 +02002826 interrupt->irq.triggering = p->triggering;
2827 interrupt->irq.polarity = p->polarity;
2828 interrupt->irq.sharable = p->sharable;
2829 interrupt->irq.interrupt_count = 1;
2830 interrupt->irq.interrupts[0] = p->interrupts[i];
2831 }
2832 return AE_OK;
2833 }
2834 case ACPI_RESOURCE_TYPE_IO:
2835 {
2836 struct acpi_resource_io *io = &resource->data.io;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002837 struct sony_pic_ioport *ioport =
2838 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
malattia@linux.it33a04452007-04-09 19:26:03 +02002839 if (!io) {
2840 dprintk("Blank IO resource\n");
2841 return AE_OK;
2842 }
2843
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002844 if (!ioport->io1.minimum) {
2845 memcpy(&ioport->io1, io, sizeof(*io));
2846 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
2847 ioport->io1.address_length);
2848 }
2849 else if (!ioport->io2.minimum) {
2850 memcpy(&ioport->io2, io, sizeof(*io));
2851 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
2852 ioport->io2.address_length);
2853 }
2854 else {
Mattia Dongilid6697932011-02-19 11:52:28 +09002855 pr_err(DRV_PFX "Unknown SPIC Type, more than 2 IO Ports\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002856 return AE_ERROR;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002857 }
malattia@linux.it33a04452007-04-09 19:26:03 +02002858 return AE_OK;
2859 }
2860 default:
2861 dprintk("Resource %d isn't an IRQ nor an IO port\n",
2862 resource->type);
2863
2864 case ACPI_RESOURCE_TYPE_END_TAG:
2865 return AE_OK;
2866 }
2867 return AE_CTRL_TERMINATE;
2868}
2869
2870static int sony_pic_possible_resources(struct acpi_device *device)
2871{
2872 int result = 0;
2873 acpi_status status = AE_OK;
2874
2875 if (!device)
2876 return -EINVAL;
2877
2878 /* get device status */
2879 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
2880 dprintk("Evaluating _STA\n");
2881 result = acpi_bus_get_status(device);
2882 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002883 pr_warn(DRV_PFX "Unable to read status\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002884 goto end;
2885 }
2886
2887 if (!device->status.enabled)
2888 dprintk("Device disabled\n");
2889 else
2890 dprintk("Device enabled\n");
2891
2892 /*
2893 * Query and parse 'method'
2894 */
2895 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
2896 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
2897 sony_pic_read_possible_resource, &spic_dev);
2898 if (ACPI_FAILURE(status)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002899 pr_warn(DRV_PFX "Failure evaluating %s\n",
malattia@linux.it33a04452007-04-09 19:26:03 +02002900 METHOD_NAME__PRS);
2901 result = -ENODEV;
2902 }
2903end:
2904 return result;
2905}
2906
2907/*
2908 * Disable the spic device by calling its _DIS method
2909 */
2910static int sony_pic_disable(struct acpi_device *device)
2911{
Matthew Garrett6158d3a2008-10-29 14:01:03 -07002912 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
2913 NULL);
2914
2915 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
malattia@linux.it33a04452007-04-09 19:26:03 +02002916 return -ENXIO;
2917
2918 dprintk("Device disabled\n");
2919 return 0;
2920}
2921
2922
2923/*
2924 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
2925 *
2926 * Call _SRS to set current resources
2927 */
2928static int sony_pic_enable(struct acpi_device *device,
2929 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
2930{
2931 acpi_status status;
2932 int result = 0;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002933 /* Type 1 resource layout is:
2934 * IO
2935 * IO
2936 * IRQNoFlags
2937 * End
2938 *
2939 * Type 2 and 3 resource layout is:
2940 * IO
2941 * IRQNoFlags
2942 * End
2943 */
malattia@linux.it33a04452007-04-09 19:26:03 +02002944 struct {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002945 struct acpi_resource res1;
2946 struct acpi_resource res2;
2947 struct acpi_resource res3;
2948 struct acpi_resource res4;
malattia@linux.it33a04452007-04-09 19:26:03 +02002949 } *resource;
2950 struct acpi_buffer buffer = { 0, NULL };
2951
2952 if (!ioport || !irq)
2953 return -EINVAL;
2954
2955 /* init acpi_buffer */
2956 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
2957 if (!resource)
2958 return -ENOMEM;
2959
2960 buffer.length = sizeof(*resource) + 1;
2961 buffer.pointer = resource;
2962
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002963 /* setup Type 1 resources */
Mattia Dongili31df7142009-09-16 00:05:29 +09002964 if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002965
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002966 /* setup io resources */
2967 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2968 resource->res1.length = sizeof(struct acpi_resource);
2969 memcpy(&resource->res1.data.io, &ioport->io1,
2970 sizeof(struct acpi_resource_io));
malattia@linux.it33a04452007-04-09 19:26:03 +02002971
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002972 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
2973 resource->res2.length = sizeof(struct acpi_resource);
2974 memcpy(&resource->res2.data.io, &ioport->io2,
2975 sizeof(struct acpi_resource_io));
2976
2977 /* setup irq resource */
2978 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
2979 resource->res3.length = sizeof(struct acpi_resource);
2980 memcpy(&resource->res3.data.irq, &irq->irq,
2981 sizeof(struct acpi_resource_irq));
2982 /* we requested a shared irq */
2983 resource->res3.data.irq.sharable = ACPI_SHARED;
2984
2985 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
2986
2987 }
2988 /* setup Type 2/3 resources */
2989 else {
2990 /* setup io resource */
2991 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2992 resource->res1.length = sizeof(struct acpi_resource);
2993 memcpy(&resource->res1.data.io, &ioport->io1,
2994 sizeof(struct acpi_resource_io));
2995
2996 /* setup irq resource */
2997 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
2998 resource->res2.length = sizeof(struct acpi_resource);
2999 memcpy(&resource->res2.data.irq, &irq->irq,
3000 sizeof(struct acpi_resource_irq));
3001 /* we requested a shared irq */
3002 resource->res2.data.irq.sharable = ACPI_SHARED;
3003
3004 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
3005 }
malattia@linux.it33a04452007-04-09 19:26:03 +02003006
3007 /* Attempt to set the resource */
3008 dprintk("Evaluating _SRS\n");
3009 status = acpi_set_current_resources(device->handle, &buffer);
3010
3011 /* check for total failure */
3012 if (ACPI_FAILURE(status)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003013 pr_err(DRV_PFX "Error evaluating _SRS\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003014 result = -ENODEV;
3015 goto end;
3016 }
3017
3018 /* Necessary device initializations calls (from sonypi) */
3019 sony_pic_call1(0x82);
3020 sony_pic_call2(0x81, 0xff);
3021 sony_pic_call1(compat ? 0x92 : 0x82);
3022
3023end:
3024 kfree(resource);
3025 return result;
3026}
3027
3028/*****************
3029 *
3030 * ISR: some event is available
3031 *
3032 *****************/
3033static irqreturn_t sony_pic_irq(int irq, void *dev_id)
3034{
3035 int i, j;
malattia@linux.it33a04452007-04-09 19:26:03 +02003036 u8 ev = 0;
3037 u8 data_mask = 0;
3038 u8 device_event = 0;
3039
3040 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
3041
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003042 ev = inb_p(dev->cur_ioport->io1.minimum);
3043 if (dev->cur_ioport->io2.minimum)
3044 data_mask = inb_p(dev->cur_ioport->io2.minimum);
3045 else
Mattia Dongilide920432008-01-14 18:05:43 +09003046 data_mask = inb_p(dev->cur_ioport->io1.minimum +
Mattia Dongili31df7142009-09-16 00:05:29 +09003047 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003048
Mattia Dongili22a17782007-07-16 02:34:39 +09003049 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
Mattia Dongilide920432008-01-14 18:05:43 +09003050 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09003051 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003052
3053 if (ev == 0x00 || ev == 0xff)
3054 return IRQ_HANDLED;
3055
Mattia Dongili31df7142009-09-16 00:05:29 +09003056 for (i = 0; dev->event_types[i].mask; i++) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003057
Mattia Dongili31df7142009-09-16 00:05:29 +09003058 if ((data_mask & dev->event_types[i].data) !=
3059 dev->event_types[i].data)
malattia@linux.it33a04452007-04-09 19:26:03 +02003060 continue;
3061
Mattia Dongili31df7142009-09-16 00:05:29 +09003062 if (!(mask & dev->event_types[i].mask))
malattia@linux.it33a04452007-04-09 19:26:03 +02003063 continue;
3064
Mattia Dongili31df7142009-09-16 00:05:29 +09003065 for (j = 0; dev->event_types[i].events[j].event; j++) {
3066 if (ev == dev->event_types[i].events[j].data) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003067 device_event =
Mattia Dongili31df7142009-09-16 00:05:29 +09003068 dev->event_types[i].events[j].event;
Mattia Dongili4eeb5022011-02-19 11:52:27 +09003069 /* some events may require ignoring */
3070 if (!device_event)
3071 return IRQ_HANDLED;
malattia@linux.it33a04452007-04-09 19:26:03 +02003072 goto found;
3073 }
3074 }
3075 }
Mattia Dongili3eb87492008-01-14 18:05:45 +09003076 /* Still not able to decode the event try to pass
3077 * it over to the minidriver
3078 */
Mattia Dongili31df7142009-09-16 00:05:29 +09003079 if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
Mattia Dongili3eb87492008-01-14 18:05:45 +09003080 return IRQ_HANDLED;
3081
Mattia Dongilide920432008-01-14 18:05:43 +09003082 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
3083 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09003084 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003085 return IRQ_HANDLED;
3086
3087found:
malattia@linux.it1549ee62007-04-09 10:19:08 +02003088 sony_laptop_report_input_event(device_event);
Mattia Dongilide920432008-01-14 18:05:43 +09003089 acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003090 sonypi_compat_report_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02003091 return IRQ_HANDLED;
3092}
3093
3094/*****************
3095 *
3096 * ACPI driver
3097 *
3098 *****************/
3099static int sony_pic_remove(struct acpi_device *device, int type)
3100{
3101 struct sony_pic_ioport *io, *tmp_io;
3102 struct sony_pic_irq *irq, *tmp_irq;
3103
3104 if (sony_pic_disable(device)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003105 pr_err(DRV_PFX "Couldn't disable device.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003106 return -ENXIO;
3107 }
3108
3109 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003110 release_region(spic_dev.cur_ioport->io1.minimum,
3111 spic_dev.cur_ioport->io1.address_length);
3112 if (spic_dev.cur_ioport->io2.minimum)
3113 release_region(spic_dev.cur_ioport->io2.minimum,
3114 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02003115
Mattia Dongili015a9162007-08-12 16:20:27 +09003116 sonypi_compat_exit();
3117
malattia@linux.it1549ee62007-04-09 10:19:08 +02003118 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02003119
malattia@linux.it49a11de2007-04-09 19:28:56 +02003120 /* pf attrs */
3121 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
3122 sony_pf_remove();
3123
malattia@linux.it33a04452007-04-09 19:26:03 +02003124 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
3125 list_del(&io->list);
3126 kfree(io);
3127 }
3128 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
3129 list_del(&irq->list);
3130 kfree(irq);
3131 }
3132 spic_dev.cur_ioport = NULL;
3133 spic_dev.cur_irq = NULL;
3134
malattia@linux.itf6119b02007-04-09 19:31:06 +02003135 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003136 return 0;
3137}
3138
3139static int sony_pic_add(struct acpi_device *device)
3140{
3141 int result;
3142 struct sony_pic_ioport *io, *tmp_io;
3143 struct sony_pic_irq *irq, *tmp_irq;
3144
Mattia Dongilid6697932011-02-19 11:52:28 +09003145 pr_info(DRV_PFX "%s v%s.\n", SONY_PIC_DRIVER_NAME,
3146 SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.it33a04452007-04-09 19:26:03 +02003147
3148 spic_dev.acpi_dev = device;
3149 strcpy(acpi_device_class(device), "sony/hotkey");
Mattia Dongilide920432008-01-14 18:05:43 +09003150 sony_pic_detect_device_type(&spic_dev);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003151 mutex_init(&spic_dev.lock);
malattia@linux.it33a04452007-04-09 19:26:03 +02003152
3153 /* read _PRS resources */
3154 result = sony_pic_possible_resources(device);
3155 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003156 pr_err(DRV_PFX "Unable to read possible resources.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003157 goto err_free_resources;
3158 }
3159
3160 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05003161 result = sony_laptop_setup_input(device);
malattia@linux.it33a04452007-04-09 19:26:03 +02003162 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003163 pr_err(DRV_PFX "Unable to create input devices.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003164 goto err_free_resources;
3165 }
3166
Mattia Dongili015a9162007-08-12 16:20:27 +09003167 if (sonypi_compat_init())
3168 goto err_remove_input;
3169
malattia@linux.it33a04452007-04-09 19:26:03 +02003170 /* request io port */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003171 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
3172 if (request_region(io->io1.minimum, io->io1.address_length,
Lucas De Marchic8440332011-03-17 17:18:22 -03003173 "Sony Programmable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003174 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
3175 io->io1.minimum, io->io1.maximum,
3176 io->io1.address_length);
3177 /* Type 1 have 2 ioports */
3178 if (io->io2.minimum) {
3179 if (request_region(io->io2.minimum,
3180 io->io2.address_length,
Lucas De Marchic8440332011-03-17 17:18:22 -03003181 "Sony Programmable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003182 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
3183 io->io2.minimum, io->io2.maximum,
3184 io->io2.address_length);
3185 spic_dev.cur_ioport = io;
3186 break;
3187 }
3188 else {
3189 dprintk("Unable to get I/O port2: "
3190 "0x%.4x (0x%.4x) + 0x%.2x\n",
3191 io->io2.minimum, io->io2.maximum,
3192 io->io2.address_length);
3193 release_region(io->io1.minimum,
3194 io->io1.address_length);
3195 }
3196 }
3197 else {
3198 spic_dev.cur_ioport = io;
3199 break;
3200 }
malattia@linux.it33a04452007-04-09 19:26:03 +02003201 }
3202 }
3203 if (!spic_dev.cur_ioport) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003204 pr_err(DRV_PFX "Failed to request_region.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003205 result = -ENODEV;
Mattia Dongili015a9162007-08-12 16:20:27 +09003206 goto err_remove_compat;
malattia@linux.it33a04452007-04-09 19:26:03 +02003207 }
3208
3209 /* request IRQ */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003210 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003211 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
Mattia Dongilid1e0de92009-09-16 00:05:30 +09003212 IRQF_DISABLED, "sony-laptop", &spic_dev)) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003213 dprintk("IRQ: %d - triggering: %d - "
3214 "polarity: %d - shr: %d\n",
3215 irq->irq.interrupts[0],
3216 irq->irq.triggering,
3217 irq->irq.polarity,
3218 irq->irq.sharable);
3219 spic_dev.cur_irq = irq;
3220 break;
3221 }
3222 }
3223 if (!spic_dev.cur_irq) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003224 pr_err(DRV_PFX "Failed to request_irq.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003225 result = -ENODEV;
3226 goto err_release_region;
3227 }
3228
3229 /* set resource status _SRS */
3230 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
3231 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003232 pr_err(DRV_PFX "Couldn't enable device.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003233 goto err_free_irq;
3234 }
3235
malattia@linux.it49a11de2007-04-09 19:28:56 +02003236 spic_dev.bluetooth_power = -1;
3237 /* create device attributes */
3238 result = sony_pf_add();
3239 if (result)
3240 goto err_disable_device;
3241
3242 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
3243 if (result)
3244 goto err_remove_pf;
3245
malattia@linux.it33a04452007-04-09 19:26:03 +02003246 return 0;
3247
malattia@linux.it49a11de2007-04-09 19:28:56 +02003248err_remove_pf:
3249 sony_pf_remove();
3250
3251err_disable_device:
3252 sony_pic_disable(device);
3253
malattia@linux.it33a04452007-04-09 19:26:03 +02003254err_free_irq:
3255 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
3256
3257err_release_region:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003258 release_region(spic_dev.cur_ioport->io1.minimum,
3259 spic_dev.cur_ioport->io1.address_length);
3260 if (spic_dev.cur_ioport->io2.minimum)
3261 release_region(spic_dev.cur_ioport->io2.minimum,
3262 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02003263
Mattia Dongili015a9162007-08-12 16:20:27 +09003264err_remove_compat:
3265 sonypi_compat_exit();
3266
malattia@linux.it33a04452007-04-09 19:26:03 +02003267err_remove_input:
malattia@linux.it1549ee62007-04-09 10:19:08 +02003268 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02003269
3270err_free_resources:
3271 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
3272 list_del(&io->list);
3273 kfree(io);
3274 }
3275 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
3276 list_del(&irq->list);
3277 kfree(irq);
3278 }
3279 spic_dev.cur_ioport = NULL;
3280 spic_dev.cur_irq = NULL;
3281
3282 return result;
3283}
3284
3285static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
3286{
3287 if (sony_pic_disable(device))
3288 return -ENXIO;
3289 return 0;
3290}
3291
3292static int sony_pic_resume(struct acpi_device *device)
3293{
3294 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
3295 return 0;
3296}
3297
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003298static const struct acpi_device_id sony_pic_device_ids[] = {
3299 {SONY_PIC_HID, 0},
3300 {"", 0},
3301};
3302
malattia@linux.it33a04452007-04-09 19:26:03 +02003303static struct acpi_driver sony_pic_driver = {
3304 .name = SONY_PIC_DRIVER_NAME,
3305 .class = SONY_PIC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003306 .ids = sony_pic_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02003307 .owner = THIS_MODULE,
3308 .ops = {
3309 .add = sony_pic_add,
3310 .remove = sony_pic_remove,
3311 .suspend = sony_pic_suspend,
3312 .resume = sony_pic_resume,
3313 },
3314};
3315
3316static struct dmi_system_id __initdata sonypi_dmi_table[] = {
3317 {
3318 .ident = "Sony Vaio",
3319 .matches = {
3320 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
3321 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
3322 },
3323 },
3324 {
3325 .ident = "Sony Vaio",
3326 .matches = {
3327 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
3328 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
3329 },
3330 },
3331 { }
3332};
3333
malattia@linux.it59b19102007-04-09 10:19:04 +02003334static int __init sony_laptop_init(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01003335{
malattia@linux.it33a04452007-04-09 19:26:03 +02003336 int result;
3337
3338 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
3339 result = acpi_bus_register_driver(&sony_pic_driver);
3340 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003341 pr_err(DRV_PFX "Unable to register SPIC driver.");
malattia@linux.it33a04452007-04-09 19:26:03 +02003342 goto out;
3343 }
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003344 spic_drv_registered = 1;
malattia@linux.it33a04452007-04-09 19:26:03 +02003345 }
3346
3347 result = acpi_bus_register_driver(&sony_nc_driver);
3348 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003349 pr_err(DRV_PFX "Unable to register SNC driver.");
malattia@linux.it33a04452007-04-09 19:26:03 +02003350 goto out_unregister_pic;
3351 }
3352
3353 return 0;
3354
3355out_unregister_pic:
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003356 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02003357 acpi_bus_unregister_driver(&sony_pic_driver);
3358out:
3359 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +01003360}
3361
malattia@linux.it59b19102007-04-09 10:19:04 +02003362static void __exit sony_laptop_exit(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01003363{
malattia@linux.it59b19102007-04-09 10:19:04 +02003364 acpi_bus_unregister_driver(&sony_nc_driver);
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003365 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02003366 acpi_bus_unregister_driver(&sony_pic_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +01003367}
3368
malattia@linux.it59b19102007-04-09 10:19:04 +02003369module_init(sony_laptop_init);
3370module_exit(sony_laptop_exit);