blob: 89d66c70d8280c4f998f75902c0d15a790ca3513 [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 Garskidb955172007-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
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900128enum sony_nc_rfkill {
129 SONY_WIFI,
130 SONY_BLUETOOTH,
131 SONY_WWAN,
132 SONY_WIMAX,
Johannes Berg19d337d2009-06-02 13:01:37 +0200133 N_SONY_RFKILL,
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900134};
135
Mattia Dongilid5a664a2009-12-17 00:08:35 +0900136static int sony_rfkill_handle;
Johannes Berg19d337d2009-06-02 13:01:37 +0200137static struct rfkill *sony_rfkill_devices[N_SONY_RFKILL];
138static int sony_rfkill_address[N_SONY_RFKILL] = {0x300, 0x500, 0x700, 0x900};
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900139static void sony_nc_rfkill_update(void);
140
malattia@linux.it1549ee62007-04-09 10:19:08 +0200141/*********** Input Devices ***********/
142
143#define SONY_LAPTOP_BUF_SIZE 128
144struct sony_laptop_input_s {
145 atomic_t users;
146 struct input_dev *jog_dev;
147 struct input_dev *key_dev;
Stefani Seibold45465482009-12-21 14:37:26 -0800148 struct kfifo fifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200149 spinlock_t fifo_lock;
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800150 struct timer_list release_key_timer;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200151};
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900152
malattia@linux.it1549ee62007-04-09 10:19:08 +0200153static struct sony_laptop_input_s sony_laptop_input = {
154 .users = ATOMIC_INIT(0),
155};
156
157struct sony_laptop_keypress {
158 struct input_dev *dev;
159 int key;
160};
161
Mattia Dongilibc57f862007-07-20 02:01:57 +0900162/* Correspondance table between sonypi events
163 * and input layer indexes in the keymap
164 */
165static int sony_laptop_input_index[] = {
Mattia Dongili3eb87492008-01-14 18:05:45 +0900166 -1, /* 0 no event */
167 -1, /* 1 SONYPI_EVENT_JOGDIAL_DOWN */
168 -1, /* 2 SONYPI_EVENT_JOGDIAL_UP */
169 -1, /* 3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
170 -1, /* 4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */
171 -1, /* 5 SONYPI_EVENT_JOGDIAL_PRESSED */
172 -1, /* 6 SONYPI_EVENT_JOGDIAL_RELEASED */
173 0, /* 7 SONYPI_EVENT_CAPTURE_PRESSED */
174 1, /* 8 SONYPI_EVENT_CAPTURE_RELEASED */
175 2, /* 9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
176 3, /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
177 4, /* 11 SONYPI_EVENT_FNKEY_ESC */
178 5, /* 12 SONYPI_EVENT_FNKEY_F1 */
179 6, /* 13 SONYPI_EVENT_FNKEY_F2 */
180 7, /* 14 SONYPI_EVENT_FNKEY_F3 */
181 8, /* 15 SONYPI_EVENT_FNKEY_F4 */
182 9, /* 16 SONYPI_EVENT_FNKEY_F5 */
183 10, /* 17 SONYPI_EVENT_FNKEY_F6 */
184 11, /* 18 SONYPI_EVENT_FNKEY_F7 */
185 12, /* 19 SONYPI_EVENT_FNKEY_F8 */
186 13, /* 20 SONYPI_EVENT_FNKEY_F9 */
187 14, /* 21 SONYPI_EVENT_FNKEY_F10 */
188 15, /* 22 SONYPI_EVENT_FNKEY_F11 */
189 16, /* 23 SONYPI_EVENT_FNKEY_F12 */
190 17, /* 24 SONYPI_EVENT_FNKEY_1 */
191 18, /* 25 SONYPI_EVENT_FNKEY_2 */
192 19, /* 26 SONYPI_EVENT_FNKEY_D */
193 20, /* 27 SONYPI_EVENT_FNKEY_E */
194 21, /* 28 SONYPI_EVENT_FNKEY_F */
195 22, /* 29 SONYPI_EVENT_FNKEY_S */
196 23, /* 30 SONYPI_EVENT_FNKEY_B */
197 24, /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */
198 25, /* 32 SONYPI_EVENT_PKEY_P1 */
199 26, /* 33 SONYPI_EVENT_PKEY_P2 */
200 27, /* 34 SONYPI_EVENT_PKEY_P3 */
201 28, /* 35 SONYPI_EVENT_BACK_PRESSED */
202 -1, /* 36 SONYPI_EVENT_LID_CLOSED */
203 -1, /* 37 SONYPI_EVENT_LID_OPENED */
204 29, /* 38 SONYPI_EVENT_BLUETOOTH_ON */
205 30, /* 39 SONYPI_EVENT_BLUETOOTH_OFF */
206 31, /* 40 SONYPI_EVENT_HELP_PRESSED */
207 32, /* 41 SONYPI_EVENT_FNKEY_ONLY */
208 33, /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
209 34, /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */
210 35, /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
211 36, /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
212 37, /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
213 38, /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */
214 39, /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
215 40, /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
216 41, /* 50 SONYPI_EVENT_ZOOM_PRESSED */
217 42, /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */
218 43, /* 52 SONYPI_EVENT_MEYE_FACE */
219 44, /* 53 SONYPI_EVENT_MEYE_OPPOSITE */
220 45, /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */
221 46, /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */
222 -1, /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */
223 -1, /* 57 SONYPI_EVENT_BATTERY_INSERT */
224 -1, /* 58 SONYPI_EVENT_BATTERY_REMOVE */
225 -1, /* 59 SONYPI_EVENT_FNKEY_RELEASED */
226 47, /* 60 SONYPI_EVENT_WIRELESS_ON */
227 48, /* 61 SONYPI_EVENT_WIRELESS_OFF */
228 49, /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */
229 50, /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900230 51, /* 64 SONYPI_EVENT_CD_EJECT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900231 52, /* 65 SONYPI_EVENT_MODEKEY_PRESSED */
232 53, /* 66 SONYPI_EVENT_PKEY_P4 */
233 54, /* 67 SONYPI_EVENT_PKEY_P5 */
234 55, /* 68 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900235 56, /* 69 SONYPI_EVENT_VOLUME_INC_PRESSED */
236 57, /* 70 SONYPI_EVENT_VOLUME_DEC_PRESSED */
237 -1, /* 71 SONYPI_EVENT_BRIGHTNESS_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900238 58, /* 72 SONYPI_EVENT_MEDIA_PRESSED */
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900239 59, /* 72 SONYPI_EVENT_VENDOR_PRESSED */
Mattia Dongilibc57f862007-07-20 02:01:57 +0900240};
241
242static int sony_laptop_input_keycode_map[] = {
243 KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
244 KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
245 KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
246 KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
247 KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
248 KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
249 KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
250 KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
251 KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
252 KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
253 KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
254 KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
255 KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
256 KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
257 KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
258 KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
259 KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
260 KEY_FN_F1, /* 17 SONYPI_EVENT_FNKEY_1 */
261 KEY_FN_F2, /* 18 SONYPI_EVENT_FNKEY_2 */
262 KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
263 KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
264 KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
265 KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
266 KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
267 KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
268 KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
269 KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
270 KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
271 KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
272 KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
273 KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
274 KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
275 KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
276 KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
277 KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
278 KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
279 KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
280 KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
281 KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
282 KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
283 KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
284 KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
285 BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
286 KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
287 KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
288 KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
289 KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
290 KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
291 KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
Mattia Dongili3eb87492008-01-14 18:05:45 +0900292 KEY_ZOOMIN, /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900293 KEY_ZOOMOUT, /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900294 KEY_EJECTCD, /* 51 SONYPI_EVENT_CD_EJECT_PRESSED */
295 KEY_F13, /* 52 SONYPI_EVENT_MODEKEY_PRESSED */
296 KEY_PROG4, /* 53 SONYPI_EVENT_PKEY_P4 */
297 KEY_F14, /* 54 SONYPI_EVENT_PKEY_P5 */
298 KEY_F15, /* 55 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900299 KEY_VOLUMEUP, /* 56 SONYPI_EVENT_VOLUME_INC_PRESSED */
300 KEY_VOLUMEDOWN, /* 57 SONYPI_EVENT_VOLUME_DEC_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900301 KEY_MEDIA, /* 58 SONYPI_EVENT_MEDIA_PRESSED */
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900302 KEY_VENDOR, /* 59 SONYPI_EVENT_VENDOR_PRESSED */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200303};
304
305/* release buttons after a short delay if pressed */
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800306static void do_sony_laptop_release_key(unsigned long unused)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200307{
308 struct sony_laptop_keypress kp;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800309 unsigned long flags;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200310
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800311 spin_lock_irqsave(&sony_laptop_input.fifo_lock, flags);
312
313 if (kfifo_out(&sony_laptop_input.fifo,
314 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200315 input_report_key(kp.dev, kp.key, 0);
316 input_sync(kp.dev);
317 }
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800318
319 /* If there is something in the fifo schedule next release. */
320 if (kfifo_len(&sony_laptop_input.fifo) != 0)
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800321 mod_timer(&sony_laptop_input.release_key_timer,
322 jiffies + msecs_to_jiffies(10));
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800323
324 spin_unlock_irqrestore(&sony_laptop_input.fifo_lock, flags);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200325}
malattia@linux.it1549ee62007-04-09 10:19:08 +0200326
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200327/* forward event to the input subsystem */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200328static void sony_laptop_report_input_event(u8 event)
329{
330 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
331 struct input_dev *key_dev = sony_laptop_input.key_dev;
332 struct sony_laptop_keypress kp = { NULL };
malattia@linux.it1549ee62007-04-09 10:19:08 +0200333
Almer S. Tigelaara83021a2009-04-12 11:26:28 +0000334 if (event == SONYPI_EVENT_FNKEY_RELEASED ||
335 event == SONYPI_EVENT_ANYBUTTON_RELEASED) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200336 /* Nothing, not all VAIOs generate this event */
337 return;
338 }
339
340 /* report events */
341 switch (event) {
342 /* jog_dev events */
343 case SONYPI_EVENT_JOGDIAL_UP:
344 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
345 input_report_rel(jog_dev, REL_WHEEL, 1);
346 input_sync(jog_dev);
347 return;
348
349 case SONYPI_EVENT_JOGDIAL_DOWN:
350 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
351 input_report_rel(jog_dev, REL_WHEEL, -1);
352 input_sync(jog_dev);
353 return;
354
355 /* key_dev events */
356 case SONYPI_EVENT_JOGDIAL_PRESSED:
357 kp.key = BTN_MIDDLE;
358 kp.dev = jog_dev;
359 break;
360
361 default:
Adrian Bunkd399d132008-02-20 00:59:03 +0200362 if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
Mattia Dongilibc57f862007-07-20 02:01:57 +0900363 dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
364 break;
365 }
366 if (sony_laptop_input_index[event] != -1) {
367 kp.key = sony_laptop_input_keycode_map[sony_laptop_input_index[event]];
368 if (kp.key != KEY_UNKNOWN)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200369 kp.dev = key_dev;
Mattia Dongilibc57f862007-07-20 02:01:57 +0900370 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200371 break;
372 }
373
374 if (kp.dev) {
375 input_report_key(kp.dev, kp.key, 1);
Mattia Dongilibc57f862007-07-20 02:01:57 +0900376 /* we emit the scancode so we can always remap the key */
377 input_event(kp.dev, EV_MSC, MSC_SCAN, event);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200378 input_sync(kp.dev);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200379
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800380 /* schedule key release */
381 kfifo_in_locked(&sony_laptop_input.fifo,
382 (unsigned char *)&kp, sizeof(kp),
383 &sony_laptop_input.fifo_lock);
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800384 mod_timer(&sony_laptop_input.release_key_timer,
385 jiffies + msecs_to_jiffies(10));
malattia@linux.it1549ee62007-04-09 10:19:08 +0200386 } else
387 dprintk("unknown input event %.2x\n", event);
388}
389
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500390static int sony_laptop_setup_input(struct acpi_device *acpi_device)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200391{
392 struct input_dev *jog_dev;
393 struct input_dev *key_dev;
394 int i;
395 int error;
396
397 /* don't run again if already initialized */
398 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
399 return 0;
400
401 /* kfifo */
402 spin_lock_init(&sony_laptop_input.fifo_lock);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800403 error = kfifo_alloc(&sony_laptop_input.fifo,
404 SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -0800405 if (error) {
Mattia Dongilid6697932011-02-19 11:52:28 +0900406 pr_err(DRV_PFX "kfifo_alloc failed\n");
malattia@linux.it1549ee62007-04-09 10:19:08 +0200407 goto err_dec_users;
408 }
409
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800410 setup_timer(&sony_laptop_input.release_key_timer,
411 do_sony_laptop_release_key, 0);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200412
413 /* input keys */
414 key_dev = input_allocate_device();
415 if (!key_dev) {
416 error = -ENOMEM;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800417 goto err_free_kfifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200418 }
419
420 key_dev->name = "Sony Vaio Keys";
421 key_dev->id.bustype = BUS_ISA;
422 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500423 key_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200424
425 /* Initialize the Input Drivers: special keys */
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800426 input_set_capability(key_dev, EV_MSC, MSC_SCAN);
427
428 __set_bit(EV_KEY, key_dev->evbit);
Mattia Dongilibc57f862007-07-20 02:01:57 +0900429 key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
430 key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
431 key_dev->keycode = &sony_laptop_input_keycode_map;
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800432 for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++)
433 __set_bit(sony_laptop_input_keycode_map[i], key_dev->keybit);
434 __clear_bit(KEY_RESERVED, key_dev->keybit);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200435
436 error = input_register_device(key_dev);
437 if (error)
438 goto err_free_keydev;
439
440 sony_laptop_input.key_dev = key_dev;
441
442 /* jogdial */
443 jog_dev = input_allocate_device();
444 if (!jog_dev) {
445 error = -ENOMEM;
446 goto err_unregister_keydev;
447 }
448
449 jog_dev->name = "Sony Vaio Jogdial";
450 jog_dev->id.bustype = BUS_ISA;
451 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500452 key_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200453
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800454 input_set_capability(jog_dev, EV_KEY, BTN_MIDDLE);
455 input_set_capability(jog_dev, EV_REL, REL_WHEEL);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200456
457 error = input_register_device(jog_dev);
458 if (error)
459 goto err_free_jogdev;
460
461 sony_laptop_input.jog_dev = jog_dev;
462
463 return 0;
464
465err_free_jogdev:
466 input_free_device(jog_dev);
467
468err_unregister_keydev:
469 input_unregister_device(key_dev);
470 /* to avoid kref underflow below at input_free_device */
471 key_dev = NULL;
472
473err_free_keydev:
474 input_free_device(key_dev);
475
malattia@linux.it1549ee62007-04-09 10:19:08 +0200476err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -0800477 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200478
479err_dec_users:
480 atomic_dec(&sony_laptop_input.users);
481 return error;
482}
483
484static void sony_laptop_remove_input(void)
485{
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800486 struct sony_laptop_keypress kp = { NULL };
487
488 /* Cleanup only after the last user has gone */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200489 if (!atomic_dec_and_test(&sony_laptop_input.users))
490 return;
491
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800492 del_timer_sync(&sony_laptop_input.release_key_timer);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800493
494 /*
495 * Generate key-up events for remaining keys. Note that we don't
496 * need locking since nobody is adding new events to the kfifo.
497 */
498 while (kfifo_out(&sony_laptop_input.fifo,
499 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
500 input_report_key(kp.dev, kp.key, 0);
501 input_sync(kp.dev);
502 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200503
504 /* destroy input devs */
505 input_unregister_device(sony_laptop_input.key_dev);
506 sony_laptop_input.key_dev = NULL;
507
508 if (sony_laptop_input.jog_dev) {
509 input_unregister_device(sony_laptop_input.jog_dev);
510 sony_laptop_input.jog_dev = NULL;
511 }
512
Stefani Seibold45465482009-12-21 14:37:26 -0800513 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200514}
515
malattia@linux.it56b87562007-04-09 10:19:05 +0200516/*********** Platform Device ***********/
517
518static atomic_t sony_pf_users = ATOMIC_INIT(0);
519static struct platform_driver sony_pf_driver = {
520 .driver = {
521 .name = "sony-laptop",
522 .owner = THIS_MODULE,
523 }
524};
525static struct platform_device *sony_pf_device;
526
527static int sony_pf_add(void)
528{
529 int ret = 0;
530
531 /* don't run again if already initialized */
532 if (atomic_add_return(1, &sony_pf_users) > 1)
533 return 0;
534
535 ret = platform_driver_register(&sony_pf_driver);
536 if (ret)
537 goto out;
538
539 sony_pf_device = platform_device_alloc("sony-laptop", -1);
540 if (!sony_pf_device) {
541 ret = -ENOMEM;
542 goto out_platform_registered;
543 }
544
545 ret = platform_device_add(sony_pf_device);
546 if (ret)
547 goto out_platform_alloced;
548
549 return 0;
550
551 out_platform_alloced:
552 platform_device_put(sony_pf_device);
553 sony_pf_device = NULL;
554 out_platform_registered:
555 platform_driver_unregister(&sony_pf_driver);
556 out:
557 atomic_dec(&sony_pf_users);
558 return ret;
559}
560
561static void sony_pf_remove(void)
562{
563 /* deregister only after the last user has gone */
564 if (!atomic_dec_and_test(&sony_pf_users))
565 return;
566
Axel Lin08db2b32010-07-01 10:18:01 +0800567 platform_device_unregister(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +0200568 platform_driver_unregister(&sony_pf_driver);
569}
570
571/*********** SNC (SNY5001) Device ***********/
572
malattia@linux.it33a04452007-04-09 19:26:03 +0200573/* the device uses 1-based values, while the backlight subsystem uses
574 0-based values */
575#define SONY_MAX_BRIGHTNESS 8
576
577#define SNC_VALIDATE_IN 0
578#define SNC_VALIDATE_OUT 1
579
malattia@linux.it59b19102007-04-09 10:19:04 +0200580static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500581 char *);
malattia@linux.it59b19102007-04-09 10:19:04 +0200582static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500583 const char *, size_t);
Mattia Dongili156c2212007-02-12 22:01:07 +0100584static int boolean_validate(const int, const int);
585static int brightness_default_validate(const int, const int);
586
malattia@linux.it59b19102007-04-09 10:19:04 +0200587struct sony_nc_value {
Len Browna02d1c12007-02-07 15:34:02 -0500588 char *name; /* name of the entry */
589 char **acpiget; /* names of the ACPI get function */
590 char **acpiset; /* names of the ACPI set function */
Mattia Dongili156c2212007-02-12 22:01:07 +0100591 int (*validate)(const int, const int); /* input/output validation */
Len Browna02d1c12007-02-07 15:34:02 -0500592 int value; /* current setting */
593 int valid; /* Has ever been set */
594 int debug; /* active only in debug mode ? */
595 struct device_attribute devattr; /* sysfs atribute */
Stelian Pop7f09c432007-01-13 23:04:31 +0100596};
597
malattia@linux.it59b19102007-04-09 10:19:04 +0200598#define SNC_HANDLE_NAMES(_name, _values...) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100599 static char *snc_##_name[] = { _values, NULL }
600
malattia@linux.it59b19102007-04-09 10:19:04 +0200601#define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100602 { \
603 .name = __stringify(_name), \
604 .acpiget = _getters, \
605 .acpiset = _setters, \
Mattia Dongili156c2212007-02-12 22:01:07 +0100606 .validate = _validate, \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100607 .debug = _debug, \
malattia@linux.it59b19102007-04-09 10:19:04 +0200608 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100609 }
610
malattia@linux.it59b19102007-04-09 10:19:04 +0200611#define SNC_HANDLE_NULL { .name = NULL }
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100612
malattia@linux.it59b19102007-04-09 10:19:04 +0200613SNC_HANDLE_NAMES(fnkey_get, "GHKE");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100614
malattia@linux.it59b19102007-04-09 10:19:04 +0200615SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
616SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100617
malattia@linux.it59b19102007-04-09 10:19:04 +0200618SNC_HANDLE_NAMES(cdpower_get, "GCDP");
619SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100620
malattia@linux.it59b19102007-04-09 10:19:04 +0200621SNC_HANDLE_NAMES(audiopower_get, "GAZP");
622SNC_HANDLE_NAMES(audiopower_set, "AZPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100623
malattia@linux.it59b19102007-04-09 10:19:04 +0200624SNC_HANDLE_NAMES(lanpower_get, "GLNP");
625SNC_HANDLE_NAMES(lanpower_set, "LNPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100626
Mattia Dongili044847e2007-07-16 02:34:33 +0900627SNC_HANDLE_NAMES(lidstate_get, "GLID");
628
629SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
630SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
631
632SNC_HANDLE_NAMES(gainbass_get, "GMGB");
633SNC_HANDLE_NAMES(gainbass_set, "CMGB");
634
malattia@linux.it59b19102007-04-09 10:19:04 +0200635SNC_HANDLE_NAMES(PID_get, "GPID");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100636
malattia@linux.it59b19102007-04-09 10:19:04 +0200637SNC_HANDLE_NAMES(CTR_get, "GCTR");
638SNC_HANDLE_NAMES(CTR_set, "SCTR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100639
malattia@linux.it59b19102007-04-09 10:19:04 +0200640SNC_HANDLE_NAMES(PCR_get, "GPCR");
641SNC_HANDLE_NAMES(PCR_set, "SPCR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100642
malattia@linux.it59b19102007-04-09 10:19:04 +0200643SNC_HANDLE_NAMES(CMI_get, "GCMI");
644SNC_HANDLE_NAMES(CMI_set, "SCMI");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100645
malattia@linux.it59b19102007-04-09 10:19:04 +0200646static struct sony_nc_value sony_nc_values[] = {
647 SNC_HANDLE(brightness_default, snc_brightness_def_get,
Mattia Dongili156c2212007-02-12 22:01:07 +0100648 snc_brightness_def_set, brightness_default_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200649 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
650 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
651 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100652 boolean_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200653 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100654 boolean_validate, 1),
Mattia Dongili044847e2007-07-16 02:34:33 +0900655 SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
656 boolean_validate, 0),
657 SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
658 boolean_validate, 0),
659 SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
660 boolean_validate, 0),
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100661 /* unknown methods */
malattia@linux.it59b19102007-04-09 10:19:04 +0200662 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
663 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
664 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
665 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
666 SNC_HANDLE_NULL
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100667};
668
malattia@linux.it59b19102007-04-09 10:19:04 +0200669static acpi_handle sony_nc_acpi_handle;
670static struct acpi_device *sony_nc_acpi_device = NULL;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100671
Mattia Dongilid78865c2007-02-07 20:01:56 +0100672/*
673 * acpi_evaluate_object wrappers
674 */
Stelian Pop7f09c432007-01-13 23:04:31 +0100675static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
676{
677 struct acpi_buffer output;
678 union acpi_object out_obj;
679 acpi_status status;
680
681 output.length = sizeof(out_obj);
682 output.pointer = &out_obj;
683
684 status = acpi_evaluate_object(handle, name, NULL, &output);
685 if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
686 *result = out_obj.integer.value;
687 return 0;
688 }
689
Mattia Dongilid6697932011-02-19 11:52:28 +0900690 pr_warn(DRV_PFX "acpi_callreadfunc failed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100691
692 return -1;
693}
694
695static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
696 int *result)
697{
698 struct acpi_object_list params;
699 union acpi_object in_obj;
700 struct acpi_buffer output;
701 union acpi_object out_obj;
702 acpi_status status;
703
704 params.count = 1;
705 params.pointer = &in_obj;
706 in_obj.type = ACPI_TYPE_INTEGER;
707 in_obj.integer.value = value;
708
709 output.length = sizeof(out_obj);
710 output.pointer = &out_obj;
711
712 status = acpi_evaluate_object(handle, name, &params, &output);
713 if (status == AE_OK) {
714 if (result != NULL) {
715 if (out_obj.type != ACPI_TYPE_INTEGER) {
Mattia Dongilid6697932011-02-19 11:52:28 +0900716 pr_warn(DRV_PFX "acpi_evaluate_object bad "
Stelian Pop7f09c432007-01-13 23:04:31 +0100717 "return type\n");
718 return -1;
719 }
720 *result = out_obj.integer.value;
721 }
722 return 0;
723 }
724
Mattia Dongilid6697932011-02-19 11:52:28 +0900725 pr_warn(DRV_PFX "acpi_evaluate_object failed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100726
727 return -1;
728}
729
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900730static int sony_find_snc_handle(int handle)
731{
732 int i;
733 int result;
734
735 for (i = 0x20; i < 0x30; i++) {
736 acpi_callsetfunc(sony_nc_acpi_handle, "SN00", i, &result);
Mattia Dongili56e6e712011-02-19 11:52:25 +0900737 if (result == handle) {
738 dprintk("found handle 0x%.4x (offset: 0x%.2x)\n",
739 handle, i - 0x20);
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900740 return i-0x20;
Mattia Dongili56e6e712011-02-19 11:52:25 +0900741 }
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900742 }
743
Mattia Dongili56e6e712011-02-19 11:52:25 +0900744 dprintk("handle 0x%.4x not found\n", handle);
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900745 return -1;
746}
747
748static int sony_call_snc_handle(int handle, int argument, int *result)
749{
Mattia Dongili56e6e712011-02-19 11:52:25 +0900750 int ret = 0;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900751 int offset = sony_find_snc_handle(handle);
752
753 if (offset < 0)
754 return -1;
755
Mattia Dongili56e6e712011-02-19 11:52:25 +0900756 ret = acpi_callsetfunc(sony_nc_acpi_handle, "SN07", offset | argument,
757 result);
758 dprintk("called SN07 with 0x%.4x (result: 0x%.4x)\n", offset | argument,
759 *result);
760 return ret;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900761}
762
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100763/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200764 * sony_nc_values input/output validate functions
Mattia Dongili156c2212007-02-12 22:01:07 +0100765 */
766
767/* brightness_default_validate:
768 *
769 * manipulate input output values to keep consistency with the
770 * backlight framework for which brightness values are 0-based.
771 */
772static int brightness_default_validate(const int direction, const int value)
773{
774 switch (direction) {
775 case SNC_VALIDATE_OUT:
776 return value - 1;
777 case SNC_VALIDATE_IN:
778 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
779 return value + 1;
780 }
781 return -EINVAL;
782}
783
784/* boolean_validate:
785 *
786 * on input validate boolean values 0/1, on output just pass the
787 * received value.
788 */
789static int boolean_validate(const int direction, const int value)
790{
791 if (direction == SNC_VALIDATE_IN) {
792 if (value != 0 && value != 1)
793 return -EINVAL;
794 }
795 return value;
796}
797
798/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200799 * Sysfs show/store common to all sony_nc_values
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100800 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200801static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
Len Browna02d1c12007-02-07 15:34:02 -0500802 char *buffer)
Stelian Pop7f09c432007-01-13 23:04:31 +0100803{
Stelian Pop7f09c432007-01-13 23:04:31 +0100804 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200805 struct sony_nc_value *item =
806 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100807
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100808 if (!*item->acpiget)
Stelian Pop7f09c432007-01-13 23:04:31 +0100809 return -EIO;
810
malattia@linux.it59b19102007-04-09 10:19:04 +0200811 if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100812 return -EIO;
813
Mattia Dongili156c2212007-02-12 22:01:07 +0100814 if (item->validate)
815 value = item->validate(SNC_VALIDATE_OUT, value);
816
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100817 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
Stelian Pop7f09c432007-01-13 23:04:31 +0100818}
819
malattia@linux.it59b19102007-04-09 10:19:04 +0200820static ssize_t sony_nc_sysfs_store(struct device *dev,
Len Browna02d1c12007-02-07 15:34:02 -0500821 struct device_attribute *attr,
822 const char *buffer, size_t count)
Stelian Pop7f09c432007-01-13 23:04:31 +0100823{
Stelian Pop7f09c432007-01-13 23:04:31 +0100824 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200825 struct sony_nc_value *item =
826 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100827
828 if (!item->acpiset)
829 return -EIO;
830
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100831 if (count > 31)
832 return -EINVAL;
833
834 value = simple_strtoul(buffer, NULL, 10);
Stelian Pop7f09c432007-01-13 23:04:31 +0100835
Mattia Dongili156c2212007-02-12 22:01:07 +0100836 if (item->validate)
837 value = item->validate(SNC_VALIDATE_IN, value);
838
839 if (value < 0)
840 return value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100841
malattia@linux.it59b19102007-04-09 10:19:04 +0200842 if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100843 return -EIO;
Andrew Morton3f4f4612007-01-13 23:04:32 +0100844 item->value = value;
845 item->valid = 1;
Stelian Pop7f09c432007-01-13 23:04:31 +0100846 return count;
847}
848
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100849
Mattia Dongilid78865c2007-02-07 20:01:56 +0100850/*
851 * Backlight device
852 */
853static int sony_backlight_update_status(struct backlight_device *bd)
Andrew Morton3f4f4612007-01-13 23:04:32 +0100854{
malattia@linux.it59b19102007-04-09 10:19:04 +0200855 return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
Richard Purdie321709c2007-02-10 15:04:08 +0000856 bd->props.brightness + 1, NULL);
Andrew Morton3f4f4612007-01-13 23:04:32 +0100857}
858
Mattia Dongilid78865c2007-02-07 20:01:56 +0100859static int sony_backlight_get_brightness(struct backlight_device *bd)
860{
861 int value;
862
malattia@linux.it59b19102007-04-09 10:19:04 +0200863 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
Mattia Dongilid78865c2007-02-07 20:01:56 +0100864 return 0;
865 /* brightness levels are 1-based, while backlight ones are 0-based */
866 return value - 1;
867}
868
869static struct backlight_device *sony_backlight_device;
Lionel Debrouxacc24722010-11-16 14:14:02 +0100870static const struct backlight_ops sony_backlight_ops = {
Len Browna02d1c12007-02-07 15:34:02 -0500871 .update_status = sony_backlight_update_status,
872 .get_brightness = sony_backlight_get_brightness,
Mattia Dongilid78865c2007-02-07 20:01:56 +0100873};
874
875/*
Mattia Dongili6315fd12007-07-16 02:34:35 +0900876 * New SNC-only Vaios event mapping to driver known keys
877 */
878struct sony_nc_event {
879 u8 data;
880 u8 event;
881};
882
Matthew Garrett45c79422009-03-26 21:58:16 +0900883static struct sony_nc_event sony_100_events[] = {
Matthew Garrett9b578962009-03-26 21:58:14 +0900884 { 0x90, SONYPI_EVENT_PKEY_P1 },
885 { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthias Welwarsky6479efb2009-04-01 22:10:45 +0900886 { 0x91, SONYPI_EVENT_PKEY_P2 },
Matthew Garrett9b578962009-03-26 21:58:14 +0900887 { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +0900888 { 0x81, SONYPI_EVENT_FNKEY_F1 },
889 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
Anton Veretenenkof5acf5e2009-03-26 22:44:26 +0900890 { 0x82, SONYPI_EVENT_FNKEY_F2 },
891 { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
892 { 0x83, SONYPI_EVENT_FNKEY_F3 },
893 { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
894 { 0x84, SONYPI_EVENT_FNKEY_F4 },
895 { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +0900896 { 0x85, SONYPI_EVENT_FNKEY_F5 },
897 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
898 { 0x86, SONYPI_EVENT_FNKEY_F6 },
899 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
900 { 0x87, SONYPI_EVENT_FNKEY_F7 },
901 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +0900902 { 0x89, SONYPI_EVENT_FNKEY_F9 },
903 { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +0900904 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
905 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
906 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
907 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900908 { 0x9d, SONYPI_EVENT_ZOOM_PRESSED },
909 { 0x1d, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +0900910 { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
911 { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900912 { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
913 { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900914 { 0xa4, SONYPI_EVENT_CD_EJECT_PRESSED },
915 { 0x24, SONYPI_EVENT_ANYBUTTON_RELEASED },
916 { 0xa5, SONYPI_EVENT_VENDOR_PRESSED },
917 { 0x25, SONYPI_EVENT_ANYBUTTON_RELEASED },
918 { 0xa6, SONYPI_EVENT_HELP_PRESSED },
919 { 0x26, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +0900920 { 0, 0 },
921};
922
Matthew Garrett45c79422009-03-26 21:58:16 +0900923static struct sony_nc_event sony_127_events[] = {
924 { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
925 { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
926 { 0x82, SONYPI_EVENT_PKEY_P1 },
927 { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
928 { 0x83, SONYPI_EVENT_PKEY_P2 },
929 { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
930 { 0x84, SONYPI_EVENT_PKEY_P3 },
931 { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
932 { 0x85, SONYPI_EVENT_PKEY_P4 },
933 { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
934 { 0x86, SONYPI_EVENT_PKEY_P5 },
935 { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett45c79422009-03-26 21:58:16 +0900936 { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
937 { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
938 { 0, 0 },
939};
940
Mattia Dongili6315fd12007-07-16 02:34:35 +0900941/*
Mattia Dongilid78865c2007-02-07 20:01:56 +0100942 * ACPI callbacks
943 */
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +0000944static void sony_nc_notify(struct acpi_device *device, u32 event)
Stelian Pop7f09c432007-01-13 23:04:31 +0100945{
Mattia Dongili6315fd12007-07-16 02:34:35 +0900946 u32 ev = event;
Mattia Dongili6315fd12007-07-16 02:34:35 +0900947
Matthew Garrett9b578962009-03-26 21:58:14 +0900948 if (ev >= 0x90) {
949 /* New-style event */
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +0900950 int result;
Matthew Garrett45c79422009-03-26 21:58:16 +0900951 int key_handle = 0;
Matthew Garrett9b578962009-03-26 21:58:14 +0900952 ev -= 0x90;
Mattia Dongili6315fd12007-07-16 02:34:35 +0900953
Matthew Garrett45c79422009-03-26 21:58:16 +0900954 if (sony_find_snc_handle(0x100) == ev)
955 key_handle = 0x100;
956 if (sony_find_snc_handle(0x127) == ev)
957 key_handle = 0x127;
Matthew Garrett9b578962009-03-26 21:58:14 +0900958
Matthias Welwarsky6479efb2009-04-01 22:10:45 +0900959 if (key_handle) {
Matthew Garrett45c79422009-03-26 21:58:16 +0900960 struct sony_nc_event *key_event;
961
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +0900962 if (sony_call_snc_handle(key_handle, 0x200, &result)) {
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +0000963 dprintk("sony_nc_notify, unable to decode"
Matthew Garrett45c79422009-03-26 21:58:16 +0900964 " event 0x%.2x 0x%.2x\n", key_handle,
965 ev);
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +0900966 /* restore the original event */
967 ev = event;
968 } else {
Matthew Garrett9b578962009-03-26 21:58:14 +0900969 ev = result & 0xFF;
970
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +0900971 if (key_handle == 0x100)
972 key_event = sony_100_events;
973 else
974 key_event = sony_127_events;
Matthew Garrett45c79422009-03-26 21:58:16 +0900975
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +0900976 for (; key_event->data; key_event++) {
977 if (key_event->data == ev) {
978 ev = key_event->event;
979 break;
980 }
Matthew Garrett9b578962009-03-26 21:58:14 +0900981 }
Mattia Dongili6315fd12007-07-16 02:34:35 +0900982
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +0900983 if (!key_event->data)
Mattia Dongilid6697932011-02-19 11:52:28 +0900984 pr_info(DRV_PFX
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +0900985 "Unknown event: 0x%x 0x%x\n",
986 key_handle,
987 ev);
988 else
989 sony_laptop_report_input_event(ev);
Matthew Garrett45c79422009-03-26 21:58:16 +0900990 }
Mattia Dongilid5a664a2009-12-17 00:08:35 +0900991 } else if (sony_find_snc_handle(sony_rfkill_handle) == ev) {
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900992 sony_nc_rfkill_update();
993 return;
Matthew Garrett9b578962009-03-26 21:58:14 +0900994 }
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +0900995 } else
996 sony_laptop_report_input_event(ev);
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900997
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +0000998 dprintk("sony_nc_notify, event: 0x%.2x\n", ev);
Len Brown14e04fb2007-08-23 15:20:26 -0400999 acpi_bus_generate_proc_event(sony_nc_acpi_device, 1, ev);
Stelian Pop7f09c432007-01-13 23:04:31 +01001000}
1001
1002static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
1003 void *context, void **return_value)
1004{
Lin Ming30823732008-12-16 16:59:35 +08001005 struct acpi_device_info *info;
Stelian Pop7f09c432007-01-13 23:04:31 +01001006
Bob Moore15b8dd52009-06-29 13:39:29 +08001007 if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001008 pr_warn(DRV_PFX "method: name: %4.4s, args %X\n",
Lin Ming30823732008-12-16 16:59:35 +08001009 (char *)&info->name, info->param_count);
1010
Bob Moore15b8dd52009-06-29 13:39:29 +08001011 kfree(info);
Lin Ming30823732008-12-16 16:59:35 +08001012 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001013
1014 return AE_OK;
1015}
1016
Mattia Dongilid78865c2007-02-07 20:01:56 +01001017/*
1018 * ACPI device
1019 */
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001020static int sony_nc_function_setup(struct acpi_device *device)
1021{
1022 int result;
1023
1024 /* Enable all events */
1025 acpi_callsetfunc(sony_nc_acpi_handle, "SN02", 0xffff, &result);
1026
1027 /* Setup hotkeys */
1028 sony_call_snc_handle(0x0100, 0, &result);
1029 sony_call_snc_handle(0x0101, 0, &result);
1030 sony_call_snc_handle(0x0102, 0x100, &result);
Almer S. Tigelaar560e84a2009-04-12 11:26:27 +00001031 sony_call_snc_handle(0x0127, 0, &result);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001032
1033 return 0;
1034}
1035
malattia@linux.it59b19102007-04-09 10:19:04 +02001036static int sony_nc_resume(struct acpi_device *device)
Mattia Dongilid78865c2007-02-07 20:01:56 +01001037{
malattia@linux.it59b19102007-04-09 10:19:04 +02001038 struct sony_nc_value *item;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001039 acpi_handle handle;
Mattia Dongilid78865c2007-02-07 20:01:56 +01001040
malattia@linux.it59b19102007-04-09 10:19:04 +02001041 for (item = sony_nc_values; item->name; item++) {
Mattia Dongilid78865c2007-02-07 20:01:56 +01001042 int ret;
1043
1044 if (!item->valid)
1045 continue;
malattia@linux.it59b19102007-04-09 10:19:04 +02001046 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
Len Browna02d1c12007-02-07 15:34:02 -05001047 item->value, NULL);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001048 if (ret < 0) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001049 pr_err(DRV_PFX "%s: %d\n", __func__, ret);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001050 break;
1051 }
1052 }
Mattia Dongili6315fd12007-07-16 02:34:35 +09001053
Matthew Garrett82734bf2009-03-26 21:58:13 +09001054 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1055 &handle))) {
1056 if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
1057 dprintk("ECON Method failed\n");
1058 }
1059
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001060 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1061 &handle))) {
1062 dprintk("Doing SNC setup\n");
1063 sony_nc_function_setup(device);
1064 }
1065
Mattia Dongilie84a02b2007-08-04 00:22:30 +09001066 /* set the last requested brightness level */
1067 if (sony_backlight_device &&
Mattia Dongilic35d4b32009-04-12 11:26:30 +00001068 sony_backlight_update_status(sony_backlight_device) < 0)
Mattia Dongilid6697932011-02-19 11:52:28 +09001069 pr_warn(DRV_PFX "unable to restore brightness level\n");
Mattia Dongilie84a02b2007-08-04 00:22:30 +09001070
Alan Jenkinsa0d97d62009-09-25 10:18:21 +01001071 /* re-read rfkill state */
1072 sony_nc_rfkill_update();
1073
Mattia Dongilid78865c2007-02-07 20:01:56 +01001074 return 0;
1075}
1076
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001077static void sony_nc_rfkill_cleanup(void)
1078{
1079 int i;
1080
Johannes Berg19d337d2009-06-02 13:01:37 +02001081 for (i = 0; i < N_SONY_RFKILL; i++) {
1082 if (sony_rfkill_devices[i]) {
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001083 rfkill_unregister(sony_rfkill_devices[i]);
Johannes Berg19d337d2009-06-02 13:01:37 +02001084 rfkill_destroy(sony_rfkill_devices[i]);
1085 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001086 }
1087}
1088
Johannes Berg19d337d2009-06-02 13:01:37 +02001089static int sony_nc_rfkill_set(void *data, bool blocked)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001090{
1091 int result;
1092 int argument = sony_rfkill_address[(long) data] + 0x100;
1093
Johannes Berg19d337d2009-06-02 13:01:37 +02001094 if (!blocked)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001095 argument |= 0xff0000;
1096
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001097 return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001098}
1099
Johannes Berg19d337d2009-06-02 13:01:37 +02001100static const struct rfkill_ops sony_rfkill_ops = {
1101 .set_block = sony_nc_rfkill_set,
1102};
1103
1104static int sony_nc_setup_rfkill(struct acpi_device *device,
1105 enum sony_nc_rfkill nc_type)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001106{
1107 int err = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001108 struct rfkill *rfk;
1109 enum rfkill_type type;
1110 const char *name;
Alan Jenkins50fab072009-09-24 20:15:24 +01001111 int result;
1112 bool hwblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001113
Johannes Berg19d337d2009-06-02 13:01:37 +02001114 switch (nc_type) {
1115 case SONY_WIFI:
1116 type = RFKILL_TYPE_WLAN;
1117 name = "sony-wifi";
1118 break;
1119 case SONY_BLUETOOTH:
1120 type = RFKILL_TYPE_BLUETOOTH;
1121 name = "sony-bluetooth";
1122 break;
1123 case SONY_WWAN:
1124 type = RFKILL_TYPE_WWAN;
1125 name = "sony-wwan";
1126 break;
1127 case SONY_WIMAX:
1128 type = RFKILL_TYPE_WIMAX;
1129 name = "sony-wimax";
1130 break;
1131 default:
1132 return -EINVAL;
Mattia Dongili53005a02009-04-12 11:26:31 +00001133 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001134
Johannes Berg19d337d2009-06-02 13:01:37 +02001135 rfk = rfkill_alloc(name, &device->dev, type,
1136 &sony_rfkill_ops, (void *)nc_type);
1137 if (!rfk)
1138 return -ENOMEM;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001139
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001140 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
Alan Jenkins50fab072009-09-24 20:15:24 +01001141 hwblock = !(result & 0x1);
1142 rfkill_set_hw_state(rfk, hwblock);
1143
Johannes Berg19d337d2009-06-02 13:01:37 +02001144 err = rfkill_register(rfk);
1145 if (err) {
1146 rfkill_destroy(rfk);
1147 return err;
Mattia Dongili53005a02009-04-12 11:26:31 +00001148 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001149 sony_rfkill_devices[nc_type] = rfk;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001150 return err;
1151}
1152
Randy Dunlapa46a7802011-01-08 19:56:44 -08001153static void sony_nc_rfkill_update(void)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001154{
Johannes Berg19d337d2009-06-02 13:01:37 +02001155 enum sony_nc_rfkill i;
1156 int result;
1157 bool hwblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001158
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001159 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001160 hwblock = !(result & 0x1);
1161
1162 for (i = 0; i < N_SONY_RFKILL; i++) {
1163 int argument = sony_rfkill_address[i];
1164
1165 if (!sony_rfkill_devices[i])
1166 continue;
1167
1168 if (hwblock) {
Johannes Berge1f8a192009-06-11 12:08:15 +02001169 if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
1170 /* we already know we're blocked */
1171 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001172 continue;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001173 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001174
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001175 sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001176 rfkill_set_states(sony_rfkill_devices[i],
1177 !(result & 0xf), false);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001178 }
1179}
1180
Mattia Dongili528809c2009-12-17 00:08:36 +09001181static void sony_nc_rfkill_setup(struct acpi_device *device)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001182{
Mattia Dongili528809c2009-12-17 00:08:36 +09001183 int offset;
1184 u8 dev_code, i;
1185 acpi_status status;
1186 struct acpi_object_list params;
1187 union acpi_object in_obj;
1188 union acpi_object *device_enum;
1189 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001190
Mattia Dongili528809c2009-12-17 00:08:36 +09001191 offset = sony_find_snc_handle(0x124);
1192 if (offset == -1) {
1193 offset = sony_find_snc_handle(0x135);
1194 if (offset == -1)
1195 return;
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001196 else
1197 sony_rfkill_handle = 0x135;
1198 } else
1199 sony_rfkill_handle = 0x124;
Mattia Dongili528809c2009-12-17 00:08:36 +09001200 dprintk("Found rkfill handle: 0x%.4x\n", sony_rfkill_handle);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001201
Mattia Dongili528809c2009-12-17 00:08:36 +09001202 /* need to read the whole buffer returned by the acpi call to SN06
1203 * here otherwise we may miss some features
1204 */
1205 params.count = 1;
1206 params.pointer = &in_obj;
1207 in_obj.type = ACPI_TYPE_INTEGER;
1208 in_obj.integer.value = offset;
1209 status = acpi_evaluate_object(sony_nc_acpi_handle, "SN06", &params,
1210 &buffer);
1211 if (ACPI_FAILURE(status)) {
1212 dprintk("Radio device enumeration failed\n");
1213 return;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001214 }
1215
Mattia Dongili528809c2009-12-17 00:08:36 +09001216 device_enum = (union acpi_object *) buffer.pointer;
Mattia Dongilid6697932011-02-19 11:52:28 +09001217 if (!device_enum || device_enum->type != ACPI_TYPE_BUFFER) {
1218 pr_err(DRV_PFX "Invalid SN06 return object 0x%.2x\n",
1219 device_enum->type);
Mattia Dongili528809c2009-12-17 00:08:36 +09001220 goto out_no_enum;
1221 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001222
Mattia Dongili528809c2009-12-17 00:08:36 +09001223 /* the buffer is filled with magic numbers describing the devices
1224 * available, 0xff terminates the enumeration
1225 */
Dmitry Torokhovc14973f2010-01-10 00:15:44 -08001226 for (i = 0; i < device_enum->buffer.length; i++) {
1227
1228 dev_code = *(device_enum->buffer.pointer + i);
1229 if (dev_code == 0xff)
1230 break;
1231
Mattia Dongili528809c2009-12-17 00:08:36 +09001232 dprintk("Radio devices, looking at 0x%.2x\n", dev_code);
1233
1234 if (dev_code == 0 && !sony_rfkill_devices[SONY_WIFI])
1235 sony_nc_setup_rfkill(device, SONY_WIFI);
1236
1237 if (dev_code == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
1238 sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
1239
1240 if ((0xf0 & dev_code) == 0x20 &&
1241 !sony_rfkill_devices[SONY_WWAN])
1242 sony_nc_setup_rfkill(device, SONY_WWAN);
1243
1244 if (dev_code == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
1245 sony_nc_setup_rfkill(device, SONY_WIMAX);
1246 }
1247
1248out_no_enum:
1249 kfree(buffer.pointer);
1250 return;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001251}
1252
malattia@linux.it59b19102007-04-09 10:19:04 +02001253static int sony_nc_add(struct acpi_device *device)
Stelian Pop7f09c432007-01-13 23:04:31 +01001254{
1255 acpi_status status;
Andrew Morton8607c672007-03-06 02:29:42 -08001256 int result = 0;
Alessandro Guido50f62af2007-01-13 23:04:34 +01001257 acpi_handle handle;
malattia@linux.it56b87562007-04-09 10:19:05 +02001258 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01001259
Mattia Dongilid6697932011-02-19 11:52:28 +09001260 pr_info(DRV_PFX "%s v%s.\n", SONY_NC_DRIVER_NAME,
1261 SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.itf6119b02007-04-09 19:31:06 +02001262
malattia@linux.it59b19102007-04-09 10:19:04 +02001263 sony_nc_acpi_device = device;
malattia@linux.it33a04452007-04-09 19:26:03 +02001264 strcpy(acpi_device_class(device), "sony/hotkey");
Stelian Popc5611622007-01-13 23:04:37 +01001265
malattia@linux.it59b19102007-04-09 10:19:04 +02001266 sony_nc_acpi_handle = device->handle;
Stelian Pop7f09c432007-01-13 23:04:31 +01001267
Mattia Dongilib25b7322007-07-16 02:34:36 +09001268 /* read device status */
1269 result = acpi_bus_get_status(device);
1270 /* bail IFF the above call was successful and the device is not present */
1271 if (!result && !device->status.present) {
1272 dprintk("Device not present\n");
1273 result = -ENODEV;
1274 goto outwalk;
1275 }
1276
Stelian Pop7f09c432007-01-13 23:04:31 +01001277 if (debug) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001278 status = acpi_walk_namespace(ACPI_TYPE_METHOD,
1279 sony_nc_acpi_handle, 1, sony_walk_callback,
1280 NULL, NULL, NULL);
Stelian Pop7f09c432007-01-13 23:04:31 +01001281 if (ACPI_FAILURE(status)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001282 pr_warn(DRV_PFX "unable to walk acpi resources\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001283 result = -ENODEV;
1284 goto outwalk;
1285 }
Stelian Popc5611622007-01-13 23:04:37 +01001286 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001287
Matthew Garrett82734bf2009-03-26 21:58:13 +09001288 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1289 &handle))) {
1290 if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
1291 dprintk("ECON Method failed\n");
1292 }
1293
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001294 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1295 &handle))) {
1296 dprintk("Doing SNC setup\n");
1297 sony_nc_function_setup(device);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001298 sony_nc_rfkill_setup(device);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001299 }
1300
malattia@linux.it1549ee62007-04-09 10:19:08 +02001301 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05001302 result = sony_laptop_setup_input(device);
malattia@linux.it1549ee62007-04-09 10:19:08 +02001303 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001304 pr_err(DRV_PFX "Unable to create input devices.\n");
malattia@linux.it1549ee62007-04-09 10:19:08 +02001305 goto outwalk;
1306 }
1307
Alessandro Guido38cfc142008-11-12 23:03:28 +01001308 if (acpi_video_backlight_support()) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001309 pr_info(DRV_PFX "brightness ignored, must be "
Thomas Renninger540b8bb2008-08-01 17:38:02 +02001310 "controlled by ACPI video driver\n");
1311 } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
1312 &handle))) {
Matthew Garrettbb7ca742011-03-22 16:30:21 -07001313 struct backlight_properties props;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05001314 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -07001315 props.type = BACKLIGHT_PLATFORM;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05001316 props.max_brightness = SONY_MAX_BRIGHTNESS - 1;
Alessandro Guido50f62af2007-01-13 23:04:34 +01001317 sony_backlight_device = backlight_device_register("sony", NULL,
Len Browna02d1c12007-02-07 15:34:02 -05001318 NULL,
Matthew Garretta19a6ee2010-02-17 16:39:44 -05001319 &sony_backlight_ops,
1320 &props);
Mattia Dongili7df03b82007-01-13 23:04:41 +01001321
Len Browna02d1c12007-02-07 15:34:02 -05001322 if (IS_ERR(sony_backlight_device)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001323 pr_warning(DRV_PFX "unable to register backlight device\n");
Mattia Dongili7df03b82007-01-13 23:04:41 +01001324 sony_backlight_device = NULL;
Richard Purdie321709c2007-02-10 15:04:08 +00001325 } else {
1326 sony_backlight_device->props.brightness =
Len Browna02d1c12007-02-07 15:34:02 -05001327 sony_backlight_get_brightness
1328 (sony_backlight_device);
Richard Purdie321709c2007-02-10 15:04:08 +00001329 }
1330
Alessandro Guido50f62af2007-01-13 23:04:34 +01001331 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001332
malattia@linux.it49a11de2007-04-09 19:28:56 +02001333 result = sony_pf_add();
1334 if (result)
Mattia Dongilied3aa4b2007-02-07 20:01:54 +01001335 goto outbacklight;
Stelian Pop7f09c432007-01-13 23:04:31 +01001336
malattia@linux.it56b87562007-04-09 10:19:05 +02001337 /* create sony_pf sysfs attributes related to the SNC device */
1338 for (item = sony_nc_values; item->name; ++item) {
1339
1340 if (!debug && item->debug)
1341 continue;
1342
1343 /* find the available acpiget as described in the DSDT */
1344 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
1345 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1346 *item->acpiget,
1347 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02001348 dprintk("Found %s getter: %s\n",
1349 item->name, *item->acpiget);
malattia@linux.it56b87562007-04-09 10:19:05 +02001350 item->devattr.attr.mode |= S_IRUGO;
1351 break;
1352 }
1353 }
1354
1355 /* find the available acpiset as described in the DSDT */
1356 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
1357 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1358 *item->acpiset,
1359 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02001360 dprintk("Found %s setter: %s\n",
1361 item->name, *item->acpiset);
malattia@linux.it56b87562007-04-09 10:19:05 +02001362 item->devattr.attr.mode |= S_IWUSR;
1363 break;
1364 }
1365 }
1366
1367 if (item->devattr.attr.mode != 0) {
1368 result =
1369 device_create_file(&sony_pf_device->dev,
1370 &item->devattr);
1371 if (result)
1372 goto out_sysfs;
1373 }
1374 }
1375
Stelian Pop7f09c432007-01-13 23:04:31 +01001376 return 0;
1377
malattia@linux.it56b87562007-04-09 10:19:05 +02001378 out_sysfs:
1379 for (item = sony_nc_values; item->name; ++item) {
1380 device_remove_file(&sony_pf_device->dev, &item->devattr);
1381 }
1382 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +02001383
Len Browna02d1c12007-02-07 15:34:02 -05001384 outbacklight:
Mattia Dongili7df03b82007-01-13 23:04:41 +01001385 if (sony_backlight_device)
1386 backlight_device_unregister(sony_backlight_device);
1387
malattia@linux.it1549ee62007-04-09 10:19:08 +02001388 sony_laptop_remove_input();
1389
Len Browna02d1c12007-02-07 15:34:02 -05001390 outwalk:
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001391 sony_nc_rfkill_cleanup();
Stelian Pop7f09c432007-01-13 23:04:31 +01001392 return result;
1393}
1394
malattia@linux.it59b19102007-04-09 10:19:04 +02001395static int sony_nc_remove(struct acpi_device *device, int type)
Stelian Pop7f09c432007-01-13 23:04:31 +01001396{
malattia@linux.it56b87562007-04-09 10:19:05 +02001397 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01001398
Alessandro Guido50f62af2007-01-13 23:04:34 +01001399 if (sony_backlight_device)
1400 backlight_device_unregister(sony_backlight_device);
1401
malattia@linux.it59b19102007-04-09 10:19:04 +02001402 sony_nc_acpi_device = NULL;
Stelian Popc5611622007-01-13 23:04:37 +01001403
malattia@linux.it56b87562007-04-09 10:19:05 +02001404 for (item = sony_nc_values; item->name; ++item) {
1405 device_remove_file(&sony_pf_device->dev, &item->devattr);
1406 }
1407
1408 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +02001409 sony_laptop_remove_input();
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001410 sony_nc_rfkill_cleanup();
malattia@linux.itf6119b02007-04-09 19:31:06 +02001411 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001412
1413 return 0;
1414}
1415
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001416static const struct acpi_device_id sony_device_ids[] = {
1417 {SONY_NC_HID, 0},
1418 {SONY_PIC_HID, 0},
1419 {"", 0},
1420};
1421MODULE_DEVICE_TABLE(acpi, sony_device_ids);
1422
1423static const struct acpi_device_id sony_nc_device_ids[] = {
1424 {SONY_NC_HID, 0},
1425 {"", 0},
1426};
1427
malattia@linux.it59b19102007-04-09 10:19:04 +02001428static struct acpi_driver sony_nc_driver = {
1429 .name = SONY_NC_DRIVER_NAME,
1430 .class = SONY_NC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001431 .ids = sony_nc_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02001432 .owner = THIS_MODULE,
Len Browna02d1c12007-02-07 15:34:02 -05001433 .ops = {
malattia@linux.it59b19102007-04-09 10:19:04 +02001434 .add = sony_nc_add,
1435 .remove = sony_nc_remove,
1436 .resume = sony_nc_resume,
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001437 .notify = sony_nc_notify,
Len Browna02d1c12007-02-07 15:34:02 -05001438 },
Andrew Morton3f4f4612007-01-13 23:04:32 +01001439};
1440
malattia@linux.it33a04452007-04-09 19:26:03 +02001441/*********** SPIC (SNY6001) Device ***********/
1442
1443#define SONYPI_DEVICE_TYPE1 0x00000001
1444#define SONYPI_DEVICE_TYPE2 0x00000002
1445#define SONYPI_DEVICE_TYPE3 0x00000004
Mattia Dongili3eb87492008-01-14 18:05:45 +09001446#define SONYPI_DEVICE_TYPE4 0x00000008
malattia@linux.it33a04452007-04-09 19:26:03 +02001447
Mattia Dongili22a17782007-07-16 02:34:39 +09001448#define SONYPI_TYPE1_OFFSET 0x04
1449#define SONYPI_TYPE2_OFFSET 0x12
1450#define SONYPI_TYPE3_OFFSET 0x12
malattia@linux.it33a04452007-04-09 19:26:03 +02001451
malattia@linux.it33a04452007-04-09 19:26:03 +02001452struct sony_pic_ioport {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001453 struct acpi_resource_io io1;
1454 struct acpi_resource_io io2;
malattia@linux.it33a04452007-04-09 19:26:03 +02001455 struct list_head list;
1456};
1457
1458struct sony_pic_irq {
1459 struct acpi_resource_irq irq;
1460 struct list_head list;
1461};
1462
Mattia Dongilide920432008-01-14 18:05:43 +09001463struct sonypi_eventtypes {
1464 u8 data;
1465 unsigned long mask;
1466 struct sonypi_event *events;
1467};
1468
malattia@linux.it33a04452007-04-09 19:26:03 +02001469struct sony_pic_dev {
Mattia Dongili31df7142009-09-16 00:05:29 +09001470 struct acpi_device *acpi_dev;
1471 struct sony_pic_irq *cur_irq;
1472 struct sony_pic_ioport *cur_ioport;
1473 struct list_head interrupts;
1474 struct list_head ioports;
1475 struct mutex lock;
1476 struct sonypi_eventtypes *event_types;
1477 int (*handle_irq)(const u8, const u8);
1478 int model;
1479 u16 evport_offset;
1480 u8 camera_power;
1481 u8 bluetooth_power;
1482 u8 wwan_power;
malattia@linux.it33a04452007-04-09 19:26:03 +02001483};
1484
1485static struct sony_pic_dev spic_dev = {
1486 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
1487 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
1488};
1489
Alan Jenkins5e6f9722009-09-16 00:05:32 +09001490static int spic_drv_registered;
1491
malattia@linux.it33a04452007-04-09 19:26:03 +02001492/* Event masks */
1493#define SONYPI_JOGGER_MASK 0x00000001
1494#define SONYPI_CAPTURE_MASK 0x00000002
1495#define SONYPI_FNKEY_MASK 0x00000004
1496#define SONYPI_BLUETOOTH_MASK 0x00000008
1497#define SONYPI_PKEY_MASK 0x00000010
1498#define SONYPI_BACK_MASK 0x00000020
1499#define SONYPI_HELP_MASK 0x00000040
1500#define SONYPI_LID_MASK 0x00000080
1501#define SONYPI_ZOOM_MASK 0x00000100
1502#define SONYPI_THUMBPHRASE_MASK 0x00000200
1503#define SONYPI_MEYE_MASK 0x00000400
1504#define SONYPI_MEMORYSTICK_MASK 0x00000800
1505#define SONYPI_BATTERY_MASK 0x00001000
1506#define SONYPI_WIRELESS_MASK 0x00002000
1507
1508struct sonypi_event {
1509 u8 data;
1510 u8 event;
1511};
1512
1513/* The set of possible button release events */
1514static struct sonypi_event sonypi_releaseev[] = {
1515 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
1516 { 0, 0 }
1517};
1518
1519/* The set of possible jogger events */
1520static struct sonypi_event sonypi_joggerev[] = {
1521 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
1522 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
1523 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
1524 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
1525 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
1526 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
1527 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
1528 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
1529 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
1530 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
1531 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
1532 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
1533 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
1534 { 0, 0 }
1535};
1536
1537/* The set of possible capture button events */
1538static struct sonypi_event sonypi_captureev[] = {
1539 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
1540 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09001541 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02001542 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
1543 { 0, 0 }
1544};
1545
1546/* The set of possible fnkeys events */
1547static struct sonypi_event sonypi_fnkeyev[] = {
1548 { 0x10, SONYPI_EVENT_FNKEY_ESC },
1549 { 0x11, SONYPI_EVENT_FNKEY_F1 },
1550 { 0x12, SONYPI_EVENT_FNKEY_F2 },
1551 { 0x13, SONYPI_EVENT_FNKEY_F3 },
1552 { 0x14, SONYPI_EVENT_FNKEY_F4 },
1553 { 0x15, SONYPI_EVENT_FNKEY_F5 },
1554 { 0x16, SONYPI_EVENT_FNKEY_F6 },
1555 { 0x17, SONYPI_EVENT_FNKEY_F7 },
1556 { 0x18, SONYPI_EVENT_FNKEY_F8 },
1557 { 0x19, SONYPI_EVENT_FNKEY_F9 },
1558 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
1559 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
1560 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
1561 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
1562 { 0x21, SONYPI_EVENT_FNKEY_1 },
1563 { 0x22, SONYPI_EVENT_FNKEY_2 },
1564 { 0x31, SONYPI_EVENT_FNKEY_D },
1565 { 0x32, SONYPI_EVENT_FNKEY_E },
1566 { 0x33, SONYPI_EVENT_FNKEY_F },
1567 { 0x34, SONYPI_EVENT_FNKEY_S },
1568 { 0x35, SONYPI_EVENT_FNKEY_B },
1569 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
1570 { 0, 0 }
1571};
1572
1573/* The set of possible program key events */
1574static struct sonypi_event sonypi_pkeyev[] = {
1575 { 0x01, SONYPI_EVENT_PKEY_P1 },
1576 { 0x02, SONYPI_EVENT_PKEY_P2 },
1577 { 0x04, SONYPI_EVENT_PKEY_P3 },
Harald Jenny1cae7102009-03-26 21:58:18 +09001578 { 0x20, SONYPI_EVENT_PKEY_P1 },
malattia@linux.it33a04452007-04-09 19:26:03 +02001579 { 0, 0 }
1580};
1581
1582/* The set of possible bluetooth events */
1583static struct sonypi_event sonypi_blueev[] = {
1584 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
1585 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
1586 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
1587 { 0, 0 }
1588};
1589
1590/* The set of possible wireless events */
1591static struct sonypi_event sonypi_wlessev[] = {
Mattia Dongili4eeb5022011-02-19 11:52:27 +09001592 { 0x59, SONYPI_EVENT_IGNORE },
1593 { 0x5a, SONYPI_EVENT_IGNORE },
malattia@linux.it33a04452007-04-09 19:26:03 +02001594 { 0, 0 }
1595};
1596
1597/* The set of possible back button events */
1598static struct sonypi_event sonypi_backev[] = {
1599 { 0x20, SONYPI_EVENT_BACK_PRESSED },
1600 { 0, 0 }
1601};
1602
1603/* The set of possible help button events */
1604static struct sonypi_event sonypi_helpev[] = {
1605 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
1606 { 0, 0 }
1607};
1608
1609
1610/* The set of possible lid events */
1611static struct sonypi_event sonypi_lidev[] = {
1612 { 0x51, SONYPI_EVENT_LID_CLOSED },
1613 { 0x50, SONYPI_EVENT_LID_OPENED },
1614 { 0, 0 }
1615};
1616
1617/* The set of possible zoom events */
1618static struct sonypi_event sonypi_zoomev[] = {
1619 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09001620 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
1621 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
Harald Jenny1cae7102009-03-26 21:58:18 +09001622 { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02001623 { 0, 0 }
1624};
1625
1626/* The set of possible thumbphrase events */
1627static struct sonypi_event sonypi_thumbphraseev[] = {
1628 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
1629 { 0, 0 }
1630};
1631
1632/* The set of possible motioneye camera events */
1633static struct sonypi_event sonypi_meyeev[] = {
1634 { 0x00, SONYPI_EVENT_MEYE_FACE },
1635 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
1636 { 0, 0 }
1637};
1638
1639/* The set of possible memorystick events */
1640static struct sonypi_event sonypi_memorystickev[] = {
1641 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
1642 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
1643 { 0, 0 }
1644};
1645
1646/* The set of possible battery events */
1647static struct sonypi_event sonypi_batteryev[] = {
1648 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
1649 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
1650 { 0, 0 }
1651};
1652
Harald Jenny1cae7102009-03-26 21:58:18 +09001653/* The set of possible volume events */
1654static struct sonypi_event sonypi_volumeev[] = {
1655 { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
1656 { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
1657 { 0, 0 }
1658};
1659
1660/* The set of possible brightness events */
1661static struct sonypi_event sonypi_brightnessev[] = {
1662 { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
1663 { 0, 0 }
1664};
1665
Mattia Dongilide920432008-01-14 18:05:43 +09001666static struct sonypi_eventtypes type1_events[] = {
1667 { 0, 0xffffffff, sonypi_releaseev },
1668 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
1669 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
1670 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
1671 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
1672 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1673 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1674 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
1675 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1676 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
1677 { 0 },
1678};
1679static struct sonypi_eventtypes type2_events[] = {
1680 { 0, 0xffffffff, sonypi_releaseev },
1681 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
1682 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
1683 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
1684 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1685 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1686 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
1687 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
1688 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
1689 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
1690 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
1691 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1692 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1693 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1694 { 0 },
1695};
1696static struct sonypi_eventtypes type3_events[] = {
1697 { 0, 0xffffffff, sonypi_releaseev },
1698 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1699 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
1700 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1701 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1702 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09001703 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
1704 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
1705 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
Harald Jenny1cae7102009-03-26 21:58:18 +09001706 { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
1707 { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09001708 { 0 },
Mattia Dongilide920432008-01-14 18:05:43 +09001709};
1710
Mattia Dongili3eb87492008-01-14 18:05:45 +09001711/* low level spic calls */
malattia@linux.it33a04452007-04-09 19:26:03 +02001712#define ITERATIONS_LONG 10000
1713#define ITERATIONS_SHORT 10
1714#define wait_on_command(command, iterations) { \
1715 unsigned int n = iterations; \
1716 while (--n && (command)) \
1717 udelay(1); \
1718 if (!n) \
1719 dprintk("command failed at %s : %s (line %d)\n", \
Harvey Harrison6e574192008-04-29 00:59:20 -07001720 __FILE__, __func__, __LINE__); \
malattia@linux.it33a04452007-04-09 19:26:03 +02001721}
1722
1723static u8 sony_pic_call1(u8 dev)
1724{
1725 u8 v1, v2;
1726
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001727 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02001728 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001729 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1730 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
1731 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09001732 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02001733 return v2;
1734}
1735
1736static u8 sony_pic_call2(u8 dev, u8 fn)
1737{
1738 u8 v1;
1739
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001740 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02001741 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001742 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1743 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02001744 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001745 outb(fn, spic_dev.cur_ioport->io1.minimum);
1746 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09001747 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02001748 return v1;
1749}
1750
malattia@linux.it49a11de2007-04-09 19:28:56 +02001751static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
1752{
1753 u8 v1;
1754
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001755 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1756 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1757 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1758 outb(fn, spic_dev.cur_ioport->io1.minimum);
1759 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1760 outb(v, spic_dev.cur_ioport->io1.minimum);
1761 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09001762 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
1763 dev, fn, v, v1);
malattia@linux.it49a11de2007-04-09 19:28:56 +02001764 return v1;
1765}
1766
Mattia Dongili3eb87492008-01-14 18:05:45 +09001767/*
1768 * minidrivers for SPIC models
1769 */
Mattia Dongilie93c8a62009-03-26 21:58:17 +09001770static int type3_handle_irq(const u8 data_mask, const u8 ev)
Mattia Dongili3eb87492008-01-14 18:05:45 +09001771{
1772 /*
1773 * 0x31 could mean we have to take some extra action and wait for
Mattia Dongilie93c8a62009-03-26 21:58:17 +09001774 * the next irq for some Type3 models, it will generate a new
Mattia Dongili3eb87492008-01-14 18:05:45 +09001775 * irq and we can read new data from the device:
1776 * - 0x5c and 0x5f requires 0xA0
1777 * - 0x61 requires 0xB3
1778 */
1779 if (data_mask == 0x31) {
1780 if (ev == 0x5c || ev == 0x5f)
1781 sony_pic_call1(0xA0);
1782 else if (ev == 0x61)
1783 sony_pic_call1(0xB3);
1784 return 0;
1785 }
1786 return 1;
1787}
1788
Mattia Dongili3eb87492008-01-14 18:05:45 +09001789static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
1790{
1791 struct pci_dev *pcidev;
1792
1793 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1794 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
1795 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09001796 dev->model = SONYPI_DEVICE_TYPE1;
1797 dev->evport_offset = SONYPI_TYPE1_OFFSET;
1798 dev->event_types = type1_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09001799 goto out;
1800 }
1801
1802 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1803 PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
1804 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09001805 dev->model = SONYPI_DEVICE_TYPE2;
1806 dev->evport_offset = SONYPI_TYPE2_OFFSET;
1807 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09001808 goto out;
1809 }
1810
1811 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1812 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
1813 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09001814 dev->model = SONYPI_DEVICE_TYPE3;
1815 dev->handle_irq = type3_handle_irq;
1816 dev->evport_offset = SONYPI_TYPE3_OFFSET;
1817 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09001818 goto out;
1819 }
1820
1821 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1822 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
1823 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09001824 dev->model = SONYPI_DEVICE_TYPE3;
1825 dev->handle_irq = type3_handle_irq;
1826 dev->evport_offset = SONYPI_TYPE3_OFFSET;
1827 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09001828 goto out;
1829 }
1830
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09001831 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1832 PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
1833 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09001834 dev->model = SONYPI_DEVICE_TYPE3;
1835 dev->handle_irq = type3_handle_irq;
1836 dev->evport_offset = SONYPI_TYPE3_OFFSET;
1837 dev->event_types = type3_events;
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09001838 goto out;
1839 }
1840
Mattia Dongili3eb87492008-01-14 18:05:45 +09001841 /* default */
Mattia Dongili31df7142009-09-16 00:05:29 +09001842 dev->model = SONYPI_DEVICE_TYPE2;
1843 dev->evport_offset = SONYPI_TYPE2_OFFSET;
1844 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09001845
1846out:
1847 if (pcidev)
1848 pci_dev_put(pcidev);
1849
Mattia Dongilid6697932011-02-19 11:52:28 +09001850 pr_info(DRV_PFX "detected Type%d model\n",
Mattia Dongili31df7142009-09-16 00:05:29 +09001851 dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
1852 dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
Mattia Dongili3eb87492008-01-14 18:05:45 +09001853}
1854
malattia@linux.it49a11de2007-04-09 19:28:56 +02001855/* camera tests and poweron/poweroff */
1856#define SONYPI_CAMERA_PICTURE 5
malattia@linux.it49a11de2007-04-09 19:28:56 +02001857#define SONYPI_CAMERA_CONTROL 0x10
malattia@linux.ite3646322007-04-28 23:34:22 +09001858
1859#define SONYPI_CAMERA_BRIGHTNESS 0
1860#define SONYPI_CAMERA_CONTRAST 1
1861#define SONYPI_CAMERA_HUE 2
1862#define SONYPI_CAMERA_COLOR 3
1863#define SONYPI_CAMERA_SHARPNESS 4
1864
1865#define SONYPI_CAMERA_EXPOSURE_MASK 0xC
1866#define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
1867#define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
1868#define SONYPI_CAMERA_MUTE_MASK 0x40
1869
1870/* the rest don't need a loop until not 0xff */
1871#define SONYPI_CAMERA_AGC 6
1872#define SONYPI_CAMERA_AGC_MASK 0x30
1873#define SONYPI_CAMERA_SHUTTER_MASK 0x7
1874
1875#define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
1876#define SONYPI_CAMERA_CONTROL 0x10
1877
1878#define SONYPI_CAMERA_STATUS 7
1879#define SONYPI_CAMERA_STATUS_READY 0x2
1880#define SONYPI_CAMERA_STATUS_POSITION 0x4
1881
1882#define SONYPI_DIRECTION_BACKWARDS 0x4
1883
1884#define SONYPI_CAMERA_REVISION 8
1885#define SONYPI_CAMERA_ROMVERSION 9
malattia@linux.it49a11de2007-04-09 19:28:56 +02001886
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001887static int __sony_pic_camera_ready(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001888{
1889 u8 v;
1890
1891 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
1892 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
1893}
1894
malattia@linux.ite3646322007-04-28 23:34:22 +09001895static int __sony_pic_camera_off(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001896{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001897 if (!camera) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001898 pr_warn(DRV_PFX "camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001899 return -ENODEV;
1900 }
1901
malattia@linux.it49a11de2007-04-09 19:28:56 +02001902 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
1903 SONYPI_CAMERA_MUTE_MASK),
1904 ITERATIONS_SHORT);
1905
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001906 if (spic_dev.camera_power) {
1907 sony_pic_call2(0x91, 0);
1908 spic_dev.camera_power = 0;
1909 }
1910 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001911}
1912
malattia@linux.ite3646322007-04-28 23:34:22 +09001913static int __sony_pic_camera_on(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001914{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001915 int i, j, x;
1916
1917 if (!camera) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001918 pr_warn(DRV_PFX "camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001919 return -ENODEV;
1920 }
malattia@linux.it49a11de2007-04-09 19:28:56 +02001921
1922 if (spic_dev.camera_power)
malattia@linux.ite3646322007-04-28 23:34:22 +09001923 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001924
1925 for (j = 5; j > 0; j--) {
1926
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001927 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001928 msleep(10);
1929 sony_pic_call1(0x93);
1930
1931 for (i = 400; i > 0; i--) {
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001932 if (__sony_pic_camera_ready())
malattia@linux.it49a11de2007-04-09 19:28:56 +02001933 break;
1934 msleep(10);
1935 }
1936 if (i)
1937 break;
1938 }
1939
1940 if (j == 0) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001941 pr_warn(DRV_PFX "failed to power on camera\n");
malattia@linux.ite3646322007-04-28 23:34:22 +09001942 return -ENODEV;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001943 }
1944
1945 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
1946 0x5a),
1947 ITERATIONS_SHORT);
1948
1949 spic_dev.camera_power = 1;
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001950 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001951}
1952
malattia@linux.ite3646322007-04-28 23:34:22 +09001953/* External camera command (exported to the motion eye v4l driver) */
1954int sony_pic_camera_command(int command, u8 value)
1955{
1956 if (!camera)
1957 return -EIO;
1958
1959 mutex_lock(&spic_dev.lock);
1960
1961 switch (command) {
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001962 case SONY_PIC_COMMAND_SETCAMERA:
malattia@linux.ite3646322007-04-28 23:34:22 +09001963 if (value)
1964 __sony_pic_camera_on();
1965 else
1966 __sony_pic_camera_off();
1967 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001968 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09001969 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
1970 ITERATIONS_SHORT);
1971 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001972 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
malattia@linux.ite3646322007-04-28 23:34:22 +09001973 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
1974 ITERATIONS_SHORT);
1975 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001976 case SONY_PIC_COMMAND_SETCAMERAHUE:
malattia@linux.ite3646322007-04-28 23:34:22 +09001977 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
1978 ITERATIONS_SHORT);
1979 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001980 case SONY_PIC_COMMAND_SETCAMERACOLOR:
malattia@linux.ite3646322007-04-28 23:34:22 +09001981 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
1982 ITERATIONS_SHORT);
1983 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001984 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09001985 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
1986 ITERATIONS_SHORT);
1987 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001988 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
malattia@linux.ite3646322007-04-28 23:34:22 +09001989 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
1990 ITERATIONS_SHORT);
1991 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001992 case SONY_PIC_COMMAND_SETCAMERAAGC:
malattia@linux.ite3646322007-04-28 23:34:22 +09001993 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
1994 ITERATIONS_SHORT);
1995 break;
1996 default:
Mattia Dongilid6697932011-02-19 11:52:28 +09001997 pr_err(DRV_PFX "sony_pic_camera_command invalid: %d\n",
malattia@linux.ite3646322007-04-28 23:34:22 +09001998 command);
1999 break;
2000 }
2001 mutex_unlock(&spic_dev.lock);
2002 return 0;
2003}
2004EXPORT_SYMBOL(sony_pic_camera_command);
2005
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002006/* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002007static void __sony_pic_set_wwanpower(u8 state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002008{
2009 state = !!state;
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002010 if (spic_dev.wwan_power == state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002011 return;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002012 sony_pic_call2(0xB0, state);
Sergey Yanovich3ad1b762009-03-26 21:58:21 +09002013 sony_pic_call1(0x82);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002014 spic_dev.wwan_power = state;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002015}
2016
2017static ssize_t sony_pic_wwanpower_store(struct device *dev,
2018 struct device_attribute *attr,
2019 const char *buffer, size_t count)
2020{
2021 unsigned long value;
2022 if (count > 31)
2023 return -EINVAL;
2024
2025 value = simple_strtoul(buffer, NULL, 10);
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002026 mutex_lock(&spic_dev.lock);
2027 __sony_pic_set_wwanpower(value);
2028 mutex_unlock(&spic_dev.lock);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002029
2030 return count;
2031}
2032
2033static ssize_t sony_pic_wwanpower_show(struct device *dev,
2034 struct device_attribute *attr, char *buffer)
2035{
2036 ssize_t count;
2037 mutex_lock(&spic_dev.lock);
2038 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
2039 mutex_unlock(&spic_dev.lock);
2040 return count;
2041}
2042
malattia@linux.it49a11de2007-04-09 19:28:56 +02002043/* bluetooth subsystem power state */
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002044static void __sony_pic_set_bluetoothpower(u8 state)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002045{
2046 state = !!state;
2047 if (spic_dev.bluetooth_power == state)
2048 return;
2049 sony_pic_call2(0x96, state);
2050 sony_pic_call1(0x82);
2051 spic_dev.bluetooth_power = state;
2052}
2053
2054static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
2055 struct device_attribute *attr,
2056 const char *buffer, size_t count)
2057{
2058 unsigned long value;
2059 if (count > 31)
2060 return -EINVAL;
2061
2062 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002063 mutex_lock(&spic_dev.lock);
2064 __sony_pic_set_bluetoothpower(value);
2065 mutex_unlock(&spic_dev.lock);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002066
2067 return count;
2068}
2069
2070static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
2071 struct device_attribute *attr, char *buffer)
2072{
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002073 ssize_t count = 0;
2074 mutex_lock(&spic_dev.lock);
2075 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
2076 mutex_unlock(&spic_dev.lock);
2077 return count;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002078}
2079
2080/* fan speed */
2081/* FAN0 information (reverse engineered from ACPI tables) */
2082#define SONY_PIC_FAN0_STATUS 0x93
malattia@linux.it7b153f32007-04-09 19:31:25 +02002083static int sony_pic_set_fanspeed(unsigned long value)
2084{
2085 return ec_write(SONY_PIC_FAN0_STATUS, value);
2086}
2087
2088static int sony_pic_get_fanspeed(u8 *value)
2089{
2090 return ec_read(SONY_PIC_FAN0_STATUS, value);
2091}
2092
malattia@linux.it49a11de2007-04-09 19:28:56 +02002093static ssize_t sony_pic_fanspeed_store(struct device *dev,
2094 struct device_attribute *attr,
2095 const char *buffer, size_t count)
2096{
2097 unsigned long value;
2098 if (count > 31)
2099 return -EINVAL;
2100
2101 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002102 if (sony_pic_set_fanspeed(value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02002103 return -EIO;
2104
2105 return count;
2106}
2107
2108static ssize_t sony_pic_fanspeed_show(struct device *dev,
2109 struct device_attribute *attr, char *buffer)
2110{
2111 u8 value = 0;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002112 if (sony_pic_get_fanspeed(&value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02002113 return -EIO;
2114
2115 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
2116}
2117
2118#define SPIC_ATTR(_name, _mode) \
2119struct device_attribute spic_attr_##_name = __ATTR(_name, \
2120 _mode, sony_pic_## _name ##_show, \
2121 sony_pic_## _name ##_store)
2122
malattia@linux.it49a11de2007-04-09 19:28:56 +02002123static SPIC_ATTR(bluetoothpower, 0644);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002124static SPIC_ATTR(wwanpower, 0644);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002125static SPIC_ATTR(fanspeed, 0644);
2126
2127static struct attribute *spic_attributes[] = {
malattia@linux.it49a11de2007-04-09 19:28:56 +02002128 &spic_attr_bluetoothpower.attr,
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002129 &spic_attr_wwanpower.attr,
malattia@linux.it49a11de2007-04-09 19:28:56 +02002130 &spic_attr_fanspeed.attr,
2131 NULL
2132};
2133
2134static struct attribute_group spic_attribute_group = {
2135 .attrs = spic_attributes
2136};
2137
malattia@linux.it7b153f32007-04-09 19:31:25 +02002138/******** SONYPI compatibility **********/
Mattia Dongilia64e62a2007-05-01 11:19:53 +09002139#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +02002140
2141/* battery / brightness / temperature addresses */
2142#define SONYPI_BAT_FLAGS 0x81
2143#define SONYPI_LCD_LIGHT 0x96
2144#define SONYPI_BAT1_PCTRM 0xa0
2145#define SONYPI_BAT1_LEFT 0xa2
2146#define SONYPI_BAT1_MAXRT 0xa4
2147#define SONYPI_BAT2_PCTRM 0xa8
2148#define SONYPI_BAT2_LEFT 0xaa
2149#define SONYPI_BAT2_MAXRT 0xac
2150#define SONYPI_BAT1_MAXTK 0xb0
2151#define SONYPI_BAT1_FULL 0xb2
2152#define SONYPI_BAT2_MAXTK 0xb8
2153#define SONYPI_BAT2_FULL 0xba
2154#define SONYPI_TEMP_STATUS 0xC1
2155
2156struct sonypi_compat_s {
2157 struct fasync_struct *fifo_async;
Stefani Seibold45465482009-12-21 14:37:26 -08002158 struct kfifo fifo;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002159 spinlock_t fifo_lock;
2160 wait_queue_head_t fifo_proc_list;
2161 atomic_t open_count;
2162};
2163static struct sonypi_compat_s sonypi_compat = {
2164 .open_count = ATOMIC_INIT(0),
2165};
2166
2167static int sonypi_misc_fasync(int fd, struct file *filp, int on)
2168{
Jonathan Corbet60aa4922009-02-01 14:52:56 -07002169 return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002170}
2171
2172static int sonypi_misc_release(struct inode *inode, struct file *file)
2173{
malattia@linux.it7b153f32007-04-09 19:31:25 +02002174 atomic_dec(&sonypi_compat.open_count);
2175 return 0;
2176}
2177
2178static int sonypi_misc_open(struct inode *inode, struct file *file)
2179{
2180 /* Flush input queue on first open */
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002181 unsigned long flags;
2182
Stefani Seibold45465482009-12-21 14:37:26 -08002183 spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002184
malattia@linux.it7b153f32007-04-09 19:31:25 +02002185 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
Stefani Seibolde64c0262009-12-21 14:37:28 -08002186 kfifo_reset(&sonypi_compat.fifo);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002187
Stefani Seibold45465482009-12-21 14:37:26 -08002188 spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002189
malattia@linux.it7b153f32007-04-09 19:31:25 +02002190 return 0;
2191}
2192
2193static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
2194 size_t count, loff_t *pos)
2195{
2196 ssize_t ret;
2197 unsigned char c;
2198
Stefani Seibold45465482009-12-21 14:37:26 -08002199 if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
malattia@linux.it7b153f32007-04-09 19:31:25 +02002200 (file->f_flags & O_NONBLOCK))
2201 return -EAGAIN;
2202
2203 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
Stefani Seibold45465482009-12-21 14:37:26 -08002204 kfifo_len(&sonypi_compat.fifo) != 0);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002205 if (ret)
2206 return ret;
2207
2208 while (ret < count &&
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002209 (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002210 &sonypi_compat.fifo_lock) == sizeof(c))) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02002211 if (put_user(c, buf++))
2212 return -EFAULT;
2213 ret++;
2214 }
2215
2216 if (ret > 0) {
2217 struct inode *inode = file->f_path.dentry->d_inode;
2218 inode->i_atime = current_fs_time(inode->i_sb);
2219 }
2220
2221 return ret;
2222}
2223
2224static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
2225{
2226 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
Stefani Seibold45465482009-12-21 14:37:26 -08002227 if (kfifo_len(&sonypi_compat.fifo))
malattia@linux.it7b153f32007-04-09 19:31:25 +02002228 return POLLIN | POLLRDNORM;
2229 return 0;
2230}
2231
2232static int ec_read16(u8 addr, u16 *value)
2233{
2234 u8 val_lb, val_hb;
2235 if (ec_read(addr, &val_lb))
2236 return -1;
2237 if (ec_read(addr + 1, &val_hb))
2238 return -1;
2239 *value = val_lb | (val_hb << 8);
2240 return 0;
2241}
2242
Alan Cox2b24ef02009-03-26 21:58:19 +09002243static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
2244 unsigned long arg)
malattia@linux.it7b153f32007-04-09 19:31:25 +02002245{
2246 int ret = 0;
2247 void __user *argp = (void __user *)arg;
2248 u8 val8;
2249 u16 val16;
2250 int value;
2251
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002252 mutex_lock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002253 switch (cmd) {
2254 case SONYPI_IOCGBRT:
2255 if (sony_backlight_device == NULL) {
2256 ret = -EIO;
2257 break;
2258 }
2259 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
2260 ret = -EIO;
2261 break;
2262 }
2263 val8 = ((value & 0xff) - 1) << 5;
2264 if (copy_to_user(argp, &val8, sizeof(val8)))
2265 ret = -EFAULT;
2266 break;
2267 case SONYPI_IOCSBRT:
2268 if (sony_backlight_device == NULL) {
2269 ret = -EIO;
2270 break;
2271 }
2272 if (copy_from_user(&val8, argp, sizeof(val8))) {
2273 ret = -EFAULT;
2274 break;
2275 }
2276 if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
2277 (val8 >> 5) + 1, NULL)) {
2278 ret = -EIO;
2279 break;
2280 }
2281 /* sync the backlight device status */
2282 sony_backlight_device->props.brightness =
2283 sony_backlight_get_brightness(sony_backlight_device);
2284 break;
2285 case SONYPI_IOCGBAT1CAP:
2286 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
2287 ret = -EIO;
2288 break;
2289 }
2290 if (copy_to_user(argp, &val16, sizeof(val16)))
2291 ret = -EFAULT;
2292 break;
2293 case SONYPI_IOCGBAT1REM:
2294 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
2295 ret = -EIO;
2296 break;
2297 }
2298 if (copy_to_user(argp, &val16, sizeof(val16)))
2299 ret = -EFAULT;
2300 break;
2301 case SONYPI_IOCGBAT2CAP:
2302 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
2303 ret = -EIO;
2304 break;
2305 }
2306 if (copy_to_user(argp, &val16, sizeof(val16)))
2307 ret = -EFAULT;
2308 break;
2309 case SONYPI_IOCGBAT2REM:
2310 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
2311 ret = -EIO;
2312 break;
2313 }
2314 if (copy_to_user(argp, &val16, sizeof(val16)))
2315 ret = -EFAULT;
2316 break;
2317 case SONYPI_IOCGBATFLAGS:
2318 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
2319 ret = -EIO;
2320 break;
2321 }
2322 val8 &= 0x07;
2323 if (copy_to_user(argp, &val8, sizeof(val8)))
2324 ret = -EFAULT;
2325 break;
2326 case SONYPI_IOCGBLUE:
2327 val8 = spic_dev.bluetooth_power;
2328 if (copy_to_user(argp, &val8, sizeof(val8)))
2329 ret = -EFAULT;
2330 break;
2331 case SONYPI_IOCSBLUE:
2332 if (copy_from_user(&val8, argp, sizeof(val8))) {
2333 ret = -EFAULT;
2334 break;
2335 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002336 __sony_pic_set_bluetoothpower(val8);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002337 break;
2338 /* FAN Controls */
2339 case SONYPI_IOCGFAN:
2340 if (sony_pic_get_fanspeed(&val8)) {
2341 ret = -EIO;
2342 break;
2343 }
2344 if (copy_to_user(argp, &val8, sizeof(val8)))
2345 ret = -EFAULT;
2346 break;
2347 case SONYPI_IOCSFAN:
2348 if (copy_from_user(&val8, argp, sizeof(val8))) {
2349 ret = -EFAULT;
2350 break;
2351 }
2352 if (sony_pic_set_fanspeed(val8))
2353 ret = -EIO;
2354 break;
2355 /* GET Temperature (useful under APM) */
2356 case SONYPI_IOCGTEMP:
2357 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
2358 ret = -EIO;
2359 break;
2360 }
2361 if (copy_to_user(argp, &val8, sizeof(val8)))
2362 ret = -EFAULT;
2363 break;
2364 default:
2365 ret = -EINVAL;
2366 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002367 mutex_unlock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002368 return ret;
2369}
2370
2371static const struct file_operations sonypi_misc_fops = {
2372 .owner = THIS_MODULE,
2373 .read = sonypi_misc_read,
2374 .poll = sonypi_misc_poll,
2375 .open = sonypi_misc_open,
2376 .release = sonypi_misc_release,
2377 .fasync = sonypi_misc_fasync,
Alan Cox2b24ef02009-03-26 21:58:19 +09002378 .unlocked_ioctl = sonypi_misc_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002379 .llseek = noop_llseek,
malattia@linux.it7b153f32007-04-09 19:31:25 +02002380};
2381
2382static struct miscdevice sonypi_misc_device = {
2383 .minor = MISC_DYNAMIC_MINOR,
2384 .name = "sonypi",
2385 .fops = &sonypi_misc_fops,
2386};
2387
2388static void sonypi_compat_report_event(u8 event)
2389{
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002390 kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002391 sizeof(event), &sonypi_compat.fifo_lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002392 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
2393 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
2394}
2395
2396static int sonypi_compat_init(void)
2397{
2398 int error;
2399
2400 spin_lock_init(&sonypi_compat.fifo_lock);
Stefani Seibold45465482009-12-21 14:37:26 -08002401 error =
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002402 kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -08002403 if (error) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002404 pr_err(DRV_PFX "kfifo_alloc failed\n");
Stefani Seibold45465482009-12-21 14:37:26 -08002405 return error;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002406 }
2407
2408 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002409
2410 if (minor != -1)
2411 sonypi_misc_device.minor = minor;
2412 error = misc_register(&sonypi_misc_device);
2413 if (error) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002414 pr_err(DRV_PFX "misc_register failed\n");
malattia@linux.it7b153f32007-04-09 19:31:25 +02002415 goto err_free_kfifo;
2416 }
2417 if (minor == -1)
Mattia Dongilid6697932011-02-19 11:52:28 +09002418 pr_info(DRV_PFX "device allocated minor is %d\n",
malattia@linux.it7b153f32007-04-09 19:31:25 +02002419 sonypi_misc_device.minor);
2420
2421 return 0;
2422
2423err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -08002424 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002425 return error;
2426}
2427
2428static void sonypi_compat_exit(void)
2429{
2430 misc_deregister(&sonypi_misc_device);
Stefani Seibold45465482009-12-21 14:37:26 -08002431 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002432}
2433#else
2434static int sonypi_compat_init(void) { return 0; }
2435static void sonypi_compat_exit(void) { }
2436static void sonypi_compat_report_event(u8 event) { }
Mattia Dongilia64e62a2007-05-01 11:19:53 +09002437#endif /* CONFIG_SONYPI_COMPAT */
malattia@linux.it7b153f32007-04-09 19:31:25 +02002438
malattia@linux.it1549ee62007-04-09 10:19:08 +02002439/*
malattia@linux.it33a04452007-04-09 19:26:03 +02002440 * ACPI callbacks
malattia@linux.it1549ee62007-04-09 10:19:08 +02002441 */
malattia@linux.it33a04452007-04-09 19:26:03 +02002442static acpi_status
2443sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
2444{
2445 u32 i;
2446 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
2447
2448 switch (resource->type) {
2449 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002450 {
2451 /* start IO enumeration */
2452 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
2453 if (!ioport)
2454 return AE_ERROR;
2455
2456 list_add(&ioport->list, &dev->ioports);
2457 return AE_OK;
2458 }
2459
malattia@linux.it33a04452007-04-09 19:26:03 +02002460 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002461 /* end IO enumeration */
malattia@linux.it33a04452007-04-09 19:26:03 +02002462 return AE_OK;
2463
2464 case ACPI_RESOURCE_TYPE_IRQ:
2465 {
2466 struct acpi_resource_irq *p = &resource->data.irq;
2467 struct sony_pic_irq *interrupt = NULL;
2468 if (!p || !p->interrupt_count) {
2469 /*
2470 * IRQ descriptors may have no IRQ# bits set,
2471 * particularly those those w/ _STA disabled
2472 */
2473 dprintk("Blank IRQ resource\n");
2474 return AE_OK;
2475 }
2476 for (i = 0; i < p->interrupt_count; i++) {
2477 if (!p->interrupts[i]) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002478 pr_warn(DRV_PFX "Invalid IRQ %d\n",
malattia@linux.it33a04452007-04-09 19:26:03 +02002479 p->interrupts[i]);
2480 continue;
2481 }
2482 interrupt = kzalloc(sizeof(*interrupt),
2483 GFP_KERNEL);
2484 if (!interrupt)
2485 return AE_ERROR;
2486
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002487 list_add(&interrupt->list, &dev->interrupts);
malattia@linux.it33a04452007-04-09 19:26:03 +02002488 interrupt->irq.triggering = p->triggering;
2489 interrupt->irq.polarity = p->polarity;
2490 interrupt->irq.sharable = p->sharable;
2491 interrupt->irq.interrupt_count = 1;
2492 interrupt->irq.interrupts[0] = p->interrupts[i];
2493 }
2494 return AE_OK;
2495 }
2496 case ACPI_RESOURCE_TYPE_IO:
2497 {
2498 struct acpi_resource_io *io = &resource->data.io;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002499 struct sony_pic_ioport *ioport =
2500 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
malattia@linux.it33a04452007-04-09 19:26:03 +02002501 if (!io) {
2502 dprintk("Blank IO resource\n");
2503 return AE_OK;
2504 }
2505
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002506 if (!ioport->io1.minimum) {
2507 memcpy(&ioport->io1, io, sizeof(*io));
2508 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
2509 ioport->io1.address_length);
2510 }
2511 else if (!ioport->io2.minimum) {
2512 memcpy(&ioport->io2, io, sizeof(*io));
2513 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
2514 ioport->io2.address_length);
2515 }
2516 else {
Mattia Dongilid6697932011-02-19 11:52:28 +09002517 pr_err(DRV_PFX "Unknown SPIC Type, more than 2 IO Ports\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002518 return AE_ERROR;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002519 }
malattia@linux.it33a04452007-04-09 19:26:03 +02002520 return AE_OK;
2521 }
2522 default:
2523 dprintk("Resource %d isn't an IRQ nor an IO port\n",
2524 resource->type);
2525
2526 case ACPI_RESOURCE_TYPE_END_TAG:
2527 return AE_OK;
2528 }
2529 return AE_CTRL_TERMINATE;
2530}
2531
2532static int sony_pic_possible_resources(struct acpi_device *device)
2533{
2534 int result = 0;
2535 acpi_status status = AE_OK;
2536
2537 if (!device)
2538 return -EINVAL;
2539
2540 /* get device status */
2541 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
2542 dprintk("Evaluating _STA\n");
2543 result = acpi_bus_get_status(device);
2544 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002545 pr_warn(DRV_PFX "Unable to read status\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002546 goto end;
2547 }
2548
2549 if (!device->status.enabled)
2550 dprintk("Device disabled\n");
2551 else
2552 dprintk("Device enabled\n");
2553
2554 /*
2555 * Query and parse 'method'
2556 */
2557 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
2558 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
2559 sony_pic_read_possible_resource, &spic_dev);
2560 if (ACPI_FAILURE(status)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002561 pr_warn(DRV_PFX "Failure evaluating %s\n",
malattia@linux.it33a04452007-04-09 19:26:03 +02002562 METHOD_NAME__PRS);
2563 result = -ENODEV;
2564 }
2565end:
2566 return result;
2567}
2568
2569/*
2570 * Disable the spic device by calling its _DIS method
2571 */
2572static int sony_pic_disable(struct acpi_device *device)
2573{
Matthew Garrett6158d3a2008-10-29 14:01:03 -07002574 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
2575 NULL);
2576
2577 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
malattia@linux.it33a04452007-04-09 19:26:03 +02002578 return -ENXIO;
2579
2580 dprintk("Device disabled\n");
2581 return 0;
2582}
2583
2584
2585/*
2586 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
2587 *
2588 * Call _SRS to set current resources
2589 */
2590static int sony_pic_enable(struct acpi_device *device,
2591 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
2592{
2593 acpi_status status;
2594 int result = 0;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002595 /* Type 1 resource layout is:
2596 * IO
2597 * IO
2598 * IRQNoFlags
2599 * End
2600 *
2601 * Type 2 and 3 resource layout is:
2602 * IO
2603 * IRQNoFlags
2604 * End
2605 */
malattia@linux.it33a04452007-04-09 19:26:03 +02002606 struct {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002607 struct acpi_resource res1;
2608 struct acpi_resource res2;
2609 struct acpi_resource res3;
2610 struct acpi_resource res4;
malattia@linux.it33a04452007-04-09 19:26:03 +02002611 } *resource;
2612 struct acpi_buffer buffer = { 0, NULL };
2613
2614 if (!ioport || !irq)
2615 return -EINVAL;
2616
2617 /* init acpi_buffer */
2618 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
2619 if (!resource)
2620 return -ENOMEM;
2621
2622 buffer.length = sizeof(*resource) + 1;
2623 buffer.pointer = resource;
2624
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002625 /* setup Type 1 resources */
Mattia Dongili31df7142009-09-16 00:05:29 +09002626 if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002627
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002628 /* setup io resources */
2629 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2630 resource->res1.length = sizeof(struct acpi_resource);
2631 memcpy(&resource->res1.data.io, &ioport->io1,
2632 sizeof(struct acpi_resource_io));
malattia@linux.it33a04452007-04-09 19:26:03 +02002633
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002634 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
2635 resource->res2.length = sizeof(struct acpi_resource);
2636 memcpy(&resource->res2.data.io, &ioport->io2,
2637 sizeof(struct acpi_resource_io));
2638
2639 /* setup irq resource */
2640 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
2641 resource->res3.length = sizeof(struct acpi_resource);
2642 memcpy(&resource->res3.data.irq, &irq->irq,
2643 sizeof(struct acpi_resource_irq));
2644 /* we requested a shared irq */
2645 resource->res3.data.irq.sharable = ACPI_SHARED;
2646
2647 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
2648
2649 }
2650 /* setup Type 2/3 resources */
2651 else {
2652 /* setup io resource */
2653 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2654 resource->res1.length = sizeof(struct acpi_resource);
2655 memcpy(&resource->res1.data.io, &ioport->io1,
2656 sizeof(struct acpi_resource_io));
2657
2658 /* setup irq resource */
2659 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
2660 resource->res2.length = sizeof(struct acpi_resource);
2661 memcpy(&resource->res2.data.irq, &irq->irq,
2662 sizeof(struct acpi_resource_irq));
2663 /* we requested a shared irq */
2664 resource->res2.data.irq.sharable = ACPI_SHARED;
2665
2666 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
2667 }
malattia@linux.it33a04452007-04-09 19:26:03 +02002668
2669 /* Attempt to set the resource */
2670 dprintk("Evaluating _SRS\n");
2671 status = acpi_set_current_resources(device->handle, &buffer);
2672
2673 /* check for total failure */
2674 if (ACPI_FAILURE(status)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002675 pr_err(DRV_PFX "Error evaluating _SRS\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002676 result = -ENODEV;
2677 goto end;
2678 }
2679
2680 /* Necessary device initializations calls (from sonypi) */
2681 sony_pic_call1(0x82);
2682 sony_pic_call2(0x81, 0xff);
2683 sony_pic_call1(compat ? 0x92 : 0x82);
2684
2685end:
2686 kfree(resource);
2687 return result;
2688}
2689
2690/*****************
2691 *
2692 * ISR: some event is available
2693 *
2694 *****************/
2695static irqreturn_t sony_pic_irq(int irq, void *dev_id)
2696{
2697 int i, j;
malattia@linux.it33a04452007-04-09 19:26:03 +02002698 u8 ev = 0;
2699 u8 data_mask = 0;
2700 u8 device_event = 0;
2701
2702 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
2703
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002704 ev = inb_p(dev->cur_ioport->io1.minimum);
2705 if (dev->cur_ioport->io2.minimum)
2706 data_mask = inb_p(dev->cur_ioport->io2.minimum);
2707 else
Mattia Dongilide920432008-01-14 18:05:43 +09002708 data_mask = inb_p(dev->cur_ioport->io1.minimum +
Mattia Dongili31df7142009-09-16 00:05:29 +09002709 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02002710
Mattia Dongili22a17782007-07-16 02:34:39 +09002711 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
Mattia Dongilide920432008-01-14 18:05:43 +09002712 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09002713 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02002714
2715 if (ev == 0x00 || ev == 0xff)
2716 return IRQ_HANDLED;
2717
Mattia Dongili31df7142009-09-16 00:05:29 +09002718 for (i = 0; dev->event_types[i].mask; i++) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002719
Mattia Dongili31df7142009-09-16 00:05:29 +09002720 if ((data_mask & dev->event_types[i].data) !=
2721 dev->event_types[i].data)
malattia@linux.it33a04452007-04-09 19:26:03 +02002722 continue;
2723
Mattia Dongili31df7142009-09-16 00:05:29 +09002724 if (!(mask & dev->event_types[i].mask))
malattia@linux.it33a04452007-04-09 19:26:03 +02002725 continue;
2726
Mattia Dongili31df7142009-09-16 00:05:29 +09002727 for (j = 0; dev->event_types[i].events[j].event; j++) {
2728 if (ev == dev->event_types[i].events[j].data) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002729 device_event =
Mattia Dongili31df7142009-09-16 00:05:29 +09002730 dev->event_types[i].events[j].event;
Mattia Dongili4eeb5022011-02-19 11:52:27 +09002731 /* some events may require ignoring */
2732 if (!device_event)
2733 return IRQ_HANDLED;
malattia@linux.it33a04452007-04-09 19:26:03 +02002734 goto found;
2735 }
2736 }
2737 }
Mattia Dongili3eb87492008-01-14 18:05:45 +09002738 /* Still not able to decode the event try to pass
2739 * it over to the minidriver
2740 */
Mattia Dongili31df7142009-09-16 00:05:29 +09002741 if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
Mattia Dongili3eb87492008-01-14 18:05:45 +09002742 return IRQ_HANDLED;
2743
Mattia Dongilide920432008-01-14 18:05:43 +09002744 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
2745 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09002746 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02002747 return IRQ_HANDLED;
2748
2749found:
malattia@linux.it1549ee62007-04-09 10:19:08 +02002750 sony_laptop_report_input_event(device_event);
Mattia Dongilide920432008-01-14 18:05:43 +09002751 acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002752 sonypi_compat_report_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02002753 return IRQ_HANDLED;
2754}
2755
2756/*****************
2757 *
2758 * ACPI driver
2759 *
2760 *****************/
2761static int sony_pic_remove(struct acpi_device *device, int type)
2762{
2763 struct sony_pic_ioport *io, *tmp_io;
2764 struct sony_pic_irq *irq, *tmp_irq;
2765
2766 if (sony_pic_disable(device)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002767 pr_err(DRV_PFX "Couldn't disable device.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002768 return -ENXIO;
2769 }
2770
2771 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002772 release_region(spic_dev.cur_ioport->io1.minimum,
2773 spic_dev.cur_ioport->io1.address_length);
2774 if (spic_dev.cur_ioport->io2.minimum)
2775 release_region(spic_dev.cur_ioport->io2.minimum,
2776 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02002777
Mattia Dongili015a9162007-08-12 16:20:27 +09002778 sonypi_compat_exit();
2779
malattia@linux.it1549ee62007-04-09 10:19:08 +02002780 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02002781
malattia@linux.it49a11de2007-04-09 19:28:56 +02002782 /* pf attrs */
2783 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2784 sony_pf_remove();
2785
malattia@linux.it33a04452007-04-09 19:26:03 +02002786 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2787 list_del(&io->list);
2788 kfree(io);
2789 }
2790 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2791 list_del(&irq->list);
2792 kfree(irq);
2793 }
2794 spic_dev.cur_ioport = NULL;
2795 spic_dev.cur_irq = NULL;
2796
malattia@linux.itf6119b02007-04-09 19:31:06 +02002797 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002798 return 0;
2799}
2800
2801static int sony_pic_add(struct acpi_device *device)
2802{
2803 int result;
2804 struct sony_pic_ioport *io, *tmp_io;
2805 struct sony_pic_irq *irq, *tmp_irq;
2806
Mattia Dongilid6697932011-02-19 11:52:28 +09002807 pr_info(DRV_PFX "%s v%s.\n", SONY_PIC_DRIVER_NAME,
2808 SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.it33a04452007-04-09 19:26:03 +02002809
2810 spic_dev.acpi_dev = device;
2811 strcpy(acpi_device_class(device), "sony/hotkey");
Mattia Dongilide920432008-01-14 18:05:43 +09002812 sony_pic_detect_device_type(&spic_dev);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002813 mutex_init(&spic_dev.lock);
malattia@linux.it33a04452007-04-09 19:26:03 +02002814
2815 /* read _PRS resources */
2816 result = sony_pic_possible_resources(device);
2817 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002818 pr_err(DRV_PFX "Unable to read possible resources.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002819 goto err_free_resources;
2820 }
2821
2822 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05002823 result = sony_laptop_setup_input(device);
malattia@linux.it33a04452007-04-09 19:26:03 +02002824 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002825 pr_err(DRV_PFX "Unable to create input devices.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002826 goto err_free_resources;
2827 }
2828
Mattia Dongili015a9162007-08-12 16:20:27 +09002829 if (sonypi_compat_init())
2830 goto err_remove_input;
2831
malattia@linux.it33a04452007-04-09 19:26:03 +02002832 /* request io port */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002833 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
2834 if (request_region(io->io1.minimum, io->io1.address_length,
malattia@linux.it33a04452007-04-09 19:26:03 +02002835 "Sony Programable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002836 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
2837 io->io1.minimum, io->io1.maximum,
2838 io->io1.address_length);
2839 /* Type 1 have 2 ioports */
2840 if (io->io2.minimum) {
2841 if (request_region(io->io2.minimum,
2842 io->io2.address_length,
2843 "Sony Programable I/O Device")) {
2844 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
2845 io->io2.minimum, io->io2.maximum,
2846 io->io2.address_length);
2847 spic_dev.cur_ioport = io;
2848 break;
2849 }
2850 else {
2851 dprintk("Unable to get I/O port2: "
2852 "0x%.4x (0x%.4x) + 0x%.2x\n",
2853 io->io2.minimum, io->io2.maximum,
2854 io->io2.address_length);
2855 release_region(io->io1.minimum,
2856 io->io1.address_length);
2857 }
2858 }
2859 else {
2860 spic_dev.cur_ioport = io;
2861 break;
2862 }
malattia@linux.it33a04452007-04-09 19:26:03 +02002863 }
2864 }
2865 if (!spic_dev.cur_ioport) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002866 pr_err(DRV_PFX "Failed to request_region.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002867 result = -ENODEV;
Mattia Dongili015a9162007-08-12 16:20:27 +09002868 goto err_remove_compat;
malattia@linux.it33a04452007-04-09 19:26:03 +02002869 }
2870
2871 /* request IRQ */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002872 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002873 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
Mattia Dongilid1e0de92009-09-16 00:05:30 +09002874 IRQF_DISABLED, "sony-laptop", &spic_dev)) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002875 dprintk("IRQ: %d - triggering: %d - "
2876 "polarity: %d - shr: %d\n",
2877 irq->irq.interrupts[0],
2878 irq->irq.triggering,
2879 irq->irq.polarity,
2880 irq->irq.sharable);
2881 spic_dev.cur_irq = irq;
2882 break;
2883 }
2884 }
2885 if (!spic_dev.cur_irq) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002886 pr_err(DRV_PFX "Failed to request_irq.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002887 result = -ENODEV;
2888 goto err_release_region;
2889 }
2890
2891 /* set resource status _SRS */
2892 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2893 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002894 pr_err(DRV_PFX "Couldn't enable device.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002895 goto err_free_irq;
2896 }
2897
malattia@linux.it49a11de2007-04-09 19:28:56 +02002898 spic_dev.bluetooth_power = -1;
2899 /* create device attributes */
2900 result = sony_pf_add();
2901 if (result)
2902 goto err_disable_device;
2903
2904 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2905 if (result)
2906 goto err_remove_pf;
2907
malattia@linux.it33a04452007-04-09 19:26:03 +02002908 return 0;
2909
malattia@linux.it49a11de2007-04-09 19:28:56 +02002910err_remove_pf:
2911 sony_pf_remove();
2912
2913err_disable_device:
2914 sony_pic_disable(device);
2915
malattia@linux.it33a04452007-04-09 19:26:03 +02002916err_free_irq:
2917 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2918
2919err_release_region:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002920 release_region(spic_dev.cur_ioport->io1.minimum,
2921 spic_dev.cur_ioport->io1.address_length);
2922 if (spic_dev.cur_ioport->io2.minimum)
2923 release_region(spic_dev.cur_ioport->io2.minimum,
2924 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02002925
Mattia Dongili015a9162007-08-12 16:20:27 +09002926err_remove_compat:
2927 sonypi_compat_exit();
2928
malattia@linux.it33a04452007-04-09 19:26:03 +02002929err_remove_input:
malattia@linux.it1549ee62007-04-09 10:19:08 +02002930 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02002931
2932err_free_resources:
2933 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2934 list_del(&io->list);
2935 kfree(io);
2936 }
2937 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2938 list_del(&irq->list);
2939 kfree(irq);
2940 }
2941 spic_dev.cur_ioport = NULL;
2942 spic_dev.cur_irq = NULL;
2943
2944 return result;
2945}
2946
2947static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
2948{
2949 if (sony_pic_disable(device))
2950 return -ENXIO;
2951 return 0;
2952}
2953
2954static int sony_pic_resume(struct acpi_device *device)
2955{
2956 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2957 return 0;
2958}
2959
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002960static const struct acpi_device_id sony_pic_device_ids[] = {
2961 {SONY_PIC_HID, 0},
2962 {"", 0},
2963};
2964
malattia@linux.it33a04452007-04-09 19:26:03 +02002965static struct acpi_driver sony_pic_driver = {
2966 .name = SONY_PIC_DRIVER_NAME,
2967 .class = SONY_PIC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002968 .ids = sony_pic_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02002969 .owner = THIS_MODULE,
2970 .ops = {
2971 .add = sony_pic_add,
2972 .remove = sony_pic_remove,
2973 .suspend = sony_pic_suspend,
2974 .resume = sony_pic_resume,
2975 },
2976};
2977
2978static struct dmi_system_id __initdata sonypi_dmi_table[] = {
2979 {
2980 .ident = "Sony Vaio",
2981 .matches = {
2982 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2983 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
2984 },
2985 },
2986 {
2987 .ident = "Sony Vaio",
2988 .matches = {
2989 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2990 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
2991 },
2992 },
2993 { }
2994};
2995
malattia@linux.it59b19102007-04-09 10:19:04 +02002996static int __init sony_laptop_init(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01002997{
malattia@linux.it33a04452007-04-09 19:26:03 +02002998 int result;
2999
3000 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
3001 result = acpi_bus_register_driver(&sony_pic_driver);
3002 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003003 pr_err(DRV_PFX "Unable to register SPIC driver.");
malattia@linux.it33a04452007-04-09 19:26:03 +02003004 goto out;
3005 }
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003006 spic_drv_registered = 1;
malattia@linux.it33a04452007-04-09 19:26:03 +02003007 }
3008
3009 result = acpi_bus_register_driver(&sony_nc_driver);
3010 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003011 pr_err(DRV_PFX "Unable to register SNC driver.");
malattia@linux.it33a04452007-04-09 19:26:03 +02003012 goto out_unregister_pic;
3013 }
3014
3015 return 0;
3016
3017out_unregister_pic:
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003018 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02003019 acpi_bus_unregister_driver(&sony_pic_driver);
3020out:
3021 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +01003022}
3023
malattia@linux.it59b19102007-04-09 10:19:04 +02003024static void __exit sony_laptop_exit(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01003025{
malattia@linux.it59b19102007-04-09 10:19:04 +02003026 acpi_bus_unregister_driver(&sony_nc_driver);
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003027 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02003028 acpi_bus_unregister_driver(&sony_pic_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +01003029}
3030
malattia@linux.it59b19102007-04-09 10:19:04 +02003031module_init(sony_laptop_init);
3032module_exit(sony_laptop_exit);