blob: 516dd22bbb26dc2e3c67005646db1b174a0e02f5 [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
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900730struct sony_nc_handles {
731 u16 cap[0x10];
732 struct device_attribute devattr;
733};
734
735struct sony_nc_handles *handles;
736
737static ssize_t sony_nc_handles_show(struct device *dev,
738 struct device_attribute *attr, char *buffer)
739{
740 ssize_t len = 0;
741 int i;
742
743 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
744 len += snprintf(buffer + len, PAGE_SIZE - len, "0x%.4x ",
745 handles->cap[i]);
746 }
747 len += snprintf(buffer + len, PAGE_SIZE - len, "\n");
748
749 return len;
750}
751
752static int sony_nc_handles_setup(struct platform_device *pd)
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900753{
754 int i;
755 int result;
756
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900757 handles = kzalloc(sizeof(*handles), GFP_KERNEL);
758
759 sysfs_attr_init(&handles->devattr.attr);
760 handles->devattr.attr.name = "handles";
761 handles->devattr.attr.mode = S_IRUGO;
762 handles->devattr.show = sony_nc_handles_show;
763
764 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
765 if (!acpi_callsetfunc(sony_nc_acpi_handle,
766 "SN00", i + 0x20, &result)) {
767 dprintk("caching handle 0x%.4x (offset: 0x%.2x)\n",
768 result, i);
769 handles->cap[i] = result;
Mattia Dongili56e6e712011-02-19 11:52:25 +0900770 }
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900771 }
772
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900773 /* allow reading capabilities via sysfs */
774 if (device_create_file(&pd->dev, &handles->devattr)) {
775 kfree(handles);
776 handles = NULL;
777 return -1;
778 }
779
780 return 0;
781}
782
783static int sony_nc_handles_cleanup(struct platform_device *pd)
784{
785 if (handles) {
786 device_remove_file(&pd->dev, &handles->devattr);
787 kfree(handles);
788 handles = NULL;
789 }
790 return 0;
791}
792
793static int sony_find_snc_handle(int handle)
794{
795 int i;
796 for (i = 0; i < 0x10; i++) {
797 if (handles->cap[i] == handle) {
798 dprintk("found handle 0x%.4x (offset: 0x%.2x)\n",
799 handle, i);
800 return i;
801 }
802 }
Mattia Dongili56e6e712011-02-19 11:52:25 +0900803 dprintk("handle 0x%.4x not found\n", handle);
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900804 return -1;
805}
806
807static int sony_call_snc_handle(int handle, int argument, int *result)
808{
Mattia Dongili56e6e712011-02-19 11:52:25 +0900809 int ret = 0;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900810 int offset = sony_find_snc_handle(handle);
811
812 if (offset < 0)
813 return -1;
814
Mattia Dongili56e6e712011-02-19 11:52:25 +0900815 ret = acpi_callsetfunc(sony_nc_acpi_handle, "SN07", offset | argument,
816 result);
817 dprintk("called SN07 with 0x%.4x (result: 0x%.4x)\n", offset | argument,
818 *result);
819 return ret;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900820}
821
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100822/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200823 * sony_nc_values input/output validate functions
Mattia Dongili156c2212007-02-12 22:01:07 +0100824 */
825
826/* brightness_default_validate:
827 *
828 * manipulate input output values to keep consistency with the
829 * backlight framework for which brightness values are 0-based.
830 */
831static int brightness_default_validate(const int direction, const int value)
832{
833 switch (direction) {
834 case SNC_VALIDATE_OUT:
835 return value - 1;
836 case SNC_VALIDATE_IN:
837 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
838 return value + 1;
839 }
840 return -EINVAL;
841}
842
843/* boolean_validate:
844 *
845 * on input validate boolean values 0/1, on output just pass the
846 * received value.
847 */
848static int boolean_validate(const int direction, const int value)
849{
850 if (direction == SNC_VALIDATE_IN) {
851 if (value != 0 && value != 1)
852 return -EINVAL;
853 }
854 return value;
855}
856
857/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200858 * Sysfs show/store common to all sony_nc_values
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100859 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200860static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
Len Browna02d1c12007-02-07 15:34:02 -0500861 char *buffer)
Stelian Pop7f09c432007-01-13 23:04:31 +0100862{
Stelian Pop7f09c432007-01-13 23:04:31 +0100863 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200864 struct sony_nc_value *item =
865 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100866
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100867 if (!*item->acpiget)
Stelian Pop7f09c432007-01-13 23:04:31 +0100868 return -EIO;
869
malattia@linux.it59b19102007-04-09 10:19:04 +0200870 if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100871 return -EIO;
872
Mattia Dongili156c2212007-02-12 22:01:07 +0100873 if (item->validate)
874 value = item->validate(SNC_VALIDATE_OUT, value);
875
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100876 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
Stelian Pop7f09c432007-01-13 23:04:31 +0100877}
878
malattia@linux.it59b19102007-04-09 10:19:04 +0200879static ssize_t sony_nc_sysfs_store(struct device *dev,
Len Browna02d1c12007-02-07 15:34:02 -0500880 struct device_attribute *attr,
881 const char *buffer, size_t count)
Stelian Pop7f09c432007-01-13 23:04:31 +0100882{
Stelian Pop7f09c432007-01-13 23:04:31 +0100883 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200884 struct sony_nc_value *item =
885 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100886
887 if (!item->acpiset)
888 return -EIO;
889
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100890 if (count > 31)
891 return -EINVAL;
892
893 value = simple_strtoul(buffer, NULL, 10);
Stelian Pop7f09c432007-01-13 23:04:31 +0100894
Mattia Dongili156c2212007-02-12 22:01:07 +0100895 if (item->validate)
896 value = item->validate(SNC_VALIDATE_IN, value);
897
898 if (value < 0)
899 return value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100900
malattia@linux.it59b19102007-04-09 10:19:04 +0200901 if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100902 return -EIO;
Andrew Morton3f4f4612007-01-13 23:04:32 +0100903 item->value = value;
904 item->valid = 1;
Stelian Pop7f09c432007-01-13 23:04:31 +0100905 return count;
906}
907
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100908
Mattia Dongilid78865c2007-02-07 20:01:56 +0100909/*
910 * Backlight device
911 */
912static int sony_backlight_update_status(struct backlight_device *bd)
Andrew Morton3f4f4612007-01-13 23:04:32 +0100913{
malattia@linux.it59b19102007-04-09 10:19:04 +0200914 return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
Richard Purdie321709c2007-02-10 15:04:08 +0000915 bd->props.brightness + 1, NULL);
Andrew Morton3f4f4612007-01-13 23:04:32 +0100916}
917
Mattia Dongilid78865c2007-02-07 20:01:56 +0100918static int sony_backlight_get_brightness(struct backlight_device *bd)
919{
920 int value;
921
malattia@linux.it59b19102007-04-09 10:19:04 +0200922 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
Mattia Dongilid78865c2007-02-07 20:01:56 +0100923 return 0;
924 /* brightness levels are 1-based, while backlight ones are 0-based */
925 return value - 1;
926}
927
928static struct backlight_device *sony_backlight_device;
Lionel Debrouxacc24722010-11-16 14:14:02 +0100929static const struct backlight_ops sony_backlight_ops = {
Len Browna02d1c12007-02-07 15:34:02 -0500930 .update_status = sony_backlight_update_status,
931 .get_brightness = sony_backlight_get_brightness,
Mattia Dongilid78865c2007-02-07 20:01:56 +0100932};
933
934/*
Mattia Dongili6315fd12007-07-16 02:34:35 +0900935 * New SNC-only Vaios event mapping to driver known keys
936 */
937struct sony_nc_event {
938 u8 data;
939 u8 event;
940};
941
Matthew Garrett45c79422009-03-26 21:58:16 +0900942static struct sony_nc_event sony_100_events[] = {
Matthew Garrett9b578962009-03-26 21:58:14 +0900943 { 0x90, SONYPI_EVENT_PKEY_P1 },
944 { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthias Welwarsky6479efb2009-04-01 22:10:45 +0900945 { 0x91, SONYPI_EVENT_PKEY_P2 },
Matthew Garrett9b578962009-03-26 21:58:14 +0900946 { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +0900947 { 0x81, SONYPI_EVENT_FNKEY_F1 },
948 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
Anton Veretenenkof5acf5e2009-03-26 22:44:26 +0900949 { 0x82, SONYPI_EVENT_FNKEY_F2 },
950 { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
951 { 0x83, SONYPI_EVENT_FNKEY_F3 },
952 { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
953 { 0x84, SONYPI_EVENT_FNKEY_F4 },
954 { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +0900955 { 0x85, SONYPI_EVENT_FNKEY_F5 },
956 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
957 { 0x86, SONYPI_EVENT_FNKEY_F6 },
958 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
959 { 0x87, SONYPI_EVENT_FNKEY_F7 },
960 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +0900961 { 0x89, SONYPI_EVENT_FNKEY_F9 },
962 { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +0900963 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
964 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
965 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
966 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900967 { 0x9d, SONYPI_EVENT_ZOOM_PRESSED },
968 { 0x1d, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +0900969 { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
970 { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900971 { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
972 { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900973 { 0xa4, SONYPI_EVENT_CD_EJECT_PRESSED },
974 { 0x24, SONYPI_EVENT_ANYBUTTON_RELEASED },
975 { 0xa5, SONYPI_EVENT_VENDOR_PRESSED },
976 { 0x25, SONYPI_EVENT_ANYBUTTON_RELEASED },
977 { 0xa6, SONYPI_EVENT_HELP_PRESSED },
978 { 0x26, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +0900979 { 0, 0 },
980};
981
Matthew Garrett45c79422009-03-26 21:58:16 +0900982static struct sony_nc_event sony_127_events[] = {
983 { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
984 { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
985 { 0x82, SONYPI_EVENT_PKEY_P1 },
986 { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
987 { 0x83, SONYPI_EVENT_PKEY_P2 },
988 { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
989 { 0x84, SONYPI_EVENT_PKEY_P3 },
990 { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
991 { 0x85, SONYPI_EVENT_PKEY_P4 },
992 { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
993 { 0x86, SONYPI_EVENT_PKEY_P5 },
994 { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett45c79422009-03-26 21:58:16 +0900995 { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
996 { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
997 { 0, 0 },
998};
999
Mattia Dongili6315fd12007-07-16 02:34:35 +09001000/*
Mattia Dongilid78865c2007-02-07 20:01:56 +01001001 * ACPI callbacks
1002 */
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001003static void sony_nc_notify(struct acpi_device *device, u32 event)
Stelian Pop7f09c432007-01-13 23:04:31 +01001004{
Mattia Dongili6315fd12007-07-16 02:34:35 +09001005 u32 ev = event;
Mattia Dongili6315fd12007-07-16 02:34:35 +09001006
Matthew Garrett9b578962009-03-26 21:58:14 +09001007 if (ev >= 0x90) {
1008 /* New-style event */
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001009 int result;
Matthew Garrett45c79422009-03-26 21:58:16 +09001010 int key_handle = 0;
Matthew Garrett9b578962009-03-26 21:58:14 +09001011 ev -= 0x90;
Mattia Dongili6315fd12007-07-16 02:34:35 +09001012
Matthew Garrett45c79422009-03-26 21:58:16 +09001013 if (sony_find_snc_handle(0x100) == ev)
1014 key_handle = 0x100;
1015 if (sony_find_snc_handle(0x127) == ev)
1016 key_handle = 0x127;
Matthew Garrett9b578962009-03-26 21:58:14 +09001017
Matthias Welwarsky6479efb2009-04-01 22:10:45 +09001018 if (key_handle) {
Matthew Garrett45c79422009-03-26 21:58:16 +09001019 struct sony_nc_event *key_event;
1020
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001021 if (sony_call_snc_handle(key_handle, 0x200, &result)) {
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001022 dprintk("sony_nc_notify, unable to decode"
Matthew Garrett45c79422009-03-26 21:58:16 +09001023 " event 0x%.2x 0x%.2x\n", key_handle,
1024 ev);
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001025 /* restore the original event */
1026 ev = event;
1027 } else {
Matthew Garrett9b578962009-03-26 21:58:14 +09001028 ev = result & 0xFF;
1029
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001030 if (key_handle == 0x100)
1031 key_event = sony_100_events;
1032 else
1033 key_event = sony_127_events;
Matthew Garrett45c79422009-03-26 21:58:16 +09001034
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001035 for (; key_event->data; key_event++) {
1036 if (key_event->data == ev) {
1037 ev = key_event->event;
1038 break;
1039 }
Matthew Garrett9b578962009-03-26 21:58:14 +09001040 }
Mattia Dongili6315fd12007-07-16 02:34:35 +09001041
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001042 if (!key_event->data)
Mattia Dongilid6697932011-02-19 11:52:28 +09001043 pr_info(DRV_PFX
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001044 "Unknown event: 0x%x 0x%x\n",
1045 key_handle,
1046 ev);
1047 else
1048 sony_laptop_report_input_event(ev);
Matthew Garrett45c79422009-03-26 21:58:16 +09001049 }
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001050 } else if (sony_find_snc_handle(sony_rfkill_handle) == ev) {
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001051 sony_nc_rfkill_update();
1052 return;
Matthew Garrett9b578962009-03-26 21:58:14 +09001053 }
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001054 } else
1055 sony_laptop_report_input_event(ev);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001056
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001057 dprintk("sony_nc_notify, event: 0x%.2x\n", ev);
Len Brown14e04fb2007-08-23 15:20:26 -04001058 acpi_bus_generate_proc_event(sony_nc_acpi_device, 1, ev);
Stelian Pop7f09c432007-01-13 23:04:31 +01001059}
1060
1061static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
1062 void *context, void **return_value)
1063{
Lin Ming30823732008-12-16 16:59:35 +08001064 struct acpi_device_info *info;
Stelian Pop7f09c432007-01-13 23:04:31 +01001065
Bob Moore15b8dd52009-06-29 13:39:29 +08001066 if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001067 pr_warn(DRV_PFX "method: name: %4.4s, args %X\n",
Lin Ming30823732008-12-16 16:59:35 +08001068 (char *)&info->name, info->param_count);
1069
Bob Moore15b8dd52009-06-29 13:39:29 +08001070 kfree(info);
Lin Ming30823732008-12-16 16:59:35 +08001071 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001072
1073 return AE_OK;
1074}
1075
Mattia Dongilid78865c2007-02-07 20:01:56 +01001076/*
1077 * ACPI device
1078 */
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001079static int sony_nc_function_setup(struct acpi_device *device)
1080{
1081 int result;
1082
1083 /* Enable all events */
1084 acpi_callsetfunc(sony_nc_acpi_handle, "SN02", 0xffff, &result);
1085
1086 /* Setup hotkeys */
1087 sony_call_snc_handle(0x0100, 0, &result);
1088 sony_call_snc_handle(0x0101, 0, &result);
1089 sony_call_snc_handle(0x0102, 0x100, &result);
Almer S. Tigelaar560e84a2009-04-12 11:26:27 +00001090 sony_call_snc_handle(0x0127, 0, &result);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001091
1092 return 0;
1093}
1094
malattia@linux.it59b19102007-04-09 10:19:04 +02001095static int sony_nc_resume(struct acpi_device *device)
Mattia Dongilid78865c2007-02-07 20:01:56 +01001096{
malattia@linux.it59b19102007-04-09 10:19:04 +02001097 struct sony_nc_value *item;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001098 acpi_handle handle;
Mattia Dongilid78865c2007-02-07 20:01:56 +01001099
malattia@linux.it59b19102007-04-09 10:19:04 +02001100 for (item = sony_nc_values; item->name; item++) {
Mattia Dongilid78865c2007-02-07 20:01:56 +01001101 int ret;
1102
1103 if (!item->valid)
1104 continue;
malattia@linux.it59b19102007-04-09 10:19:04 +02001105 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
Len Browna02d1c12007-02-07 15:34:02 -05001106 item->value, NULL);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001107 if (ret < 0) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001108 pr_err(DRV_PFX "%s: %d\n", __func__, ret);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001109 break;
1110 }
1111 }
Mattia Dongili6315fd12007-07-16 02:34:35 +09001112
Matthew Garrett82734bf2009-03-26 21:58:13 +09001113 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1114 &handle))) {
1115 if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
1116 dprintk("ECON Method failed\n");
1117 }
1118
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001119 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1120 &handle))) {
1121 dprintk("Doing SNC setup\n");
1122 sony_nc_function_setup(device);
1123 }
1124
Mattia Dongilie84a02b2007-08-04 00:22:30 +09001125 /* set the last requested brightness level */
1126 if (sony_backlight_device &&
Mattia Dongilic35d4b32009-04-12 11:26:30 +00001127 sony_backlight_update_status(sony_backlight_device) < 0)
Mattia Dongilid6697932011-02-19 11:52:28 +09001128 pr_warn(DRV_PFX "unable to restore brightness level\n");
Mattia Dongilie84a02b2007-08-04 00:22:30 +09001129
Alan Jenkinsa0d97d62009-09-25 10:18:21 +01001130 /* re-read rfkill state */
1131 sony_nc_rfkill_update();
1132
Mattia Dongilid78865c2007-02-07 20:01:56 +01001133 return 0;
1134}
1135
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001136static void sony_nc_rfkill_cleanup(void)
1137{
1138 int i;
1139
Johannes Berg19d337d2009-06-02 13:01:37 +02001140 for (i = 0; i < N_SONY_RFKILL; i++) {
1141 if (sony_rfkill_devices[i]) {
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001142 rfkill_unregister(sony_rfkill_devices[i]);
Johannes Berg19d337d2009-06-02 13:01:37 +02001143 rfkill_destroy(sony_rfkill_devices[i]);
1144 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001145 }
1146}
1147
Johannes Berg19d337d2009-06-02 13:01:37 +02001148static int sony_nc_rfkill_set(void *data, bool blocked)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001149{
1150 int result;
1151 int argument = sony_rfkill_address[(long) data] + 0x100;
1152
Johannes Berg19d337d2009-06-02 13:01:37 +02001153 if (!blocked)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001154 argument |= 0xff0000;
1155
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001156 return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001157}
1158
Johannes Berg19d337d2009-06-02 13:01:37 +02001159static const struct rfkill_ops sony_rfkill_ops = {
1160 .set_block = sony_nc_rfkill_set,
1161};
1162
1163static int sony_nc_setup_rfkill(struct acpi_device *device,
1164 enum sony_nc_rfkill nc_type)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001165{
1166 int err = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001167 struct rfkill *rfk;
1168 enum rfkill_type type;
1169 const char *name;
Alan Jenkins50fab072009-09-24 20:15:24 +01001170 int result;
1171 bool hwblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001172
Johannes Berg19d337d2009-06-02 13:01:37 +02001173 switch (nc_type) {
1174 case SONY_WIFI:
1175 type = RFKILL_TYPE_WLAN;
1176 name = "sony-wifi";
1177 break;
1178 case SONY_BLUETOOTH:
1179 type = RFKILL_TYPE_BLUETOOTH;
1180 name = "sony-bluetooth";
1181 break;
1182 case SONY_WWAN:
1183 type = RFKILL_TYPE_WWAN;
1184 name = "sony-wwan";
1185 break;
1186 case SONY_WIMAX:
1187 type = RFKILL_TYPE_WIMAX;
1188 name = "sony-wimax";
1189 break;
1190 default:
1191 return -EINVAL;
Mattia Dongili53005a02009-04-12 11:26:31 +00001192 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001193
Johannes Berg19d337d2009-06-02 13:01:37 +02001194 rfk = rfkill_alloc(name, &device->dev, type,
1195 &sony_rfkill_ops, (void *)nc_type);
1196 if (!rfk)
1197 return -ENOMEM;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001198
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001199 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
Alan Jenkins50fab072009-09-24 20:15:24 +01001200 hwblock = !(result & 0x1);
1201 rfkill_set_hw_state(rfk, hwblock);
1202
Johannes Berg19d337d2009-06-02 13:01:37 +02001203 err = rfkill_register(rfk);
1204 if (err) {
1205 rfkill_destroy(rfk);
1206 return err;
Mattia Dongili53005a02009-04-12 11:26:31 +00001207 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001208 sony_rfkill_devices[nc_type] = rfk;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001209 return err;
1210}
1211
Randy Dunlapa46a7802011-01-08 19:56:44 -08001212static void sony_nc_rfkill_update(void)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001213{
Johannes Berg19d337d2009-06-02 13:01:37 +02001214 enum sony_nc_rfkill i;
1215 int result;
1216 bool hwblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001217
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001218 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001219 hwblock = !(result & 0x1);
1220
1221 for (i = 0; i < N_SONY_RFKILL; i++) {
1222 int argument = sony_rfkill_address[i];
1223
1224 if (!sony_rfkill_devices[i])
1225 continue;
1226
1227 if (hwblock) {
Johannes Berge1f8a192009-06-11 12:08:15 +02001228 if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
1229 /* we already know we're blocked */
1230 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001231 continue;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001232 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001233
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001234 sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001235 rfkill_set_states(sony_rfkill_devices[i],
1236 !(result & 0xf), false);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001237 }
1238}
1239
Mattia Dongili528809c2009-12-17 00:08:36 +09001240static void sony_nc_rfkill_setup(struct acpi_device *device)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001241{
Mattia Dongili528809c2009-12-17 00:08:36 +09001242 int offset;
1243 u8 dev_code, i;
1244 acpi_status status;
1245 struct acpi_object_list params;
1246 union acpi_object in_obj;
1247 union acpi_object *device_enum;
1248 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001249
Mattia Dongili528809c2009-12-17 00:08:36 +09001250 offset = sony_find_snc_handle(0x124);
1251 if (offset == -1) {
1252 offset = sony_find_snc_handle(0x135);
1253 if (offset == -1)
1254 return;
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001255 else
1256 sony_rfkill_handle = 0x135;
1257 } else
1258 sony_rfkill_handle = 0x124;
Mattia Dongili528809c2009-12-17 00:08:36 +09001259 dprintk("Found rkfill handle: 0x%.4x\n", sony_rfkill_handle);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001260
Mattia Dongili528809c2009-12-17 00:08:36 +09001261 /* need to read the whole buffer returned by the acpi call to SN06
1262 * here otherwise we may miss some features
1263 */
1264 params.count = 1;
1265 params.pointer = &in_obj;
1266 in_obj.type = ACPI_TYPE_INTEGER;
1267 in_obj.integer.value = offset;
1268 status = acpi_evaluate_object(sony_nc_acpi_handle, "SN06", &params,
1269 &buffer);
1270 if (ACPI_FAILURE(status)) {
1271 dprintk("Radio device enumeration failed\n");
1272 return;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001273 }
1274
Mattia Dongili528809c2009-12-17 00:08:36 +09001275 device_enum = (union acpi_object *) buffer.pointer;
Mattia Dongilid6697932011-02-19 11:52:28 +09001276 if (!device_enum || device_enum->type != ACPI_TYPE_BUFFER) {
1277 pr_err(DRV_PFX "Invalid SN06 return object 0x%.2x\n",
1278 device_enum->type);
Mattia Dongili528809c2009-12-17 00:08:36 +09001279 goto out_no_enum;
1280 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001281
Mattia Dongili528809c2009-12-17 00:08:36 +09001282 /* the buffer is filled with magic numbers describing the devices
1283 * available, 0xff terminates the enumeration
1284 */
Dmitry Torokhovc14973f2010-01-10 00:15:44 -08001285 for (i = 0; i < device_enum->buffer.length; i++) {
1286
1287 dev_code = *(device_enum->buffer.pointer + i);
1288 if (dev_code == 0xff)
1289 break;
1290
Mattia Dongili528809c2009-12-17 00:08:36 +09001291 dprintk("Radio devices, looking at 0x%.2x\n", dev_code);
1292
1293 if (dev_code == 0 && !sony_rfkill_devices[SONY_WIFI])
1294 sony_nc_setup_rfkill(device, SONY_WIFI);
1295
1296 if (dev_code == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
1297 sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
1298
1299 if ((0xf0 & dev_code) == 0x20 &&
1300 !sony_rfkill_devices[SONY_WWAN])
1301 sony_nc_setup_rfkill(device, SONY_WWAN);
1302
1303 if (dev_code == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
1304 sony_nc_setup_rfkill(device, SONY_WIMAX);
1305 }
1306
1307out_no_enum:
1308 kfree(buffer.pointer);
1309 return;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001310}
1311
malattia@linux.it59b19102007-04-09 10:19:04 +02001312static int sony_nc_add(struct acpi_device *device)
Stelian Pop7f09c432007-01-13 23:04:31 +01001313{
1314 acpi_status status;
Andrew Morton8607c672007-03-06 02:29:42 -08001315 int result = 0;
Alessandro Guido50f62af2007-01-13 23:04:34 +01001316 acpi_handle handle;
malattia@linux.it56b87562007-04-09 10:19:05 +02001317 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01001318
Mattia Dongilid6697932011-02-19 11:52:28 +09001319 pr_info(DRV_PFX "%s v%s.\n", SONY_NC_DRIVER_NAME,
1320 SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.itf6119b02007-04-09 19:31:06 +02001321
malattia@linux.it59b19102007-04-09 10:19:04 +02001322 sony_nc_acpi_device = device;
malattia@linux.it33a04452007-04-09 19:26:03 +02001323 strcpy(acpi_device_class(device), "sony/hotkey");
Stelian Popc5611622007-01-13 23:04:37 +01001324
malattia@linux.it59b19102007-04-09 10:19:04 +02001325 sony_nc_acpi_handle = device->handle;
Stelian Pop7f09c432007-01-13 23:04:31 +01001326
Mattia Dongilib25b7322007-07-16 02:34:36 +09001327 /* read device status */
1328 result = acpi_bus_get_status(device);
1329 /* bail IFF the above call was successful and the device is not present */
1330 if (!result && !device->status.present) {
1331 dprintk("Device not present\n");
1332 result = -ENODEV;
1333 goto outwalk;
1334 }
1335
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001336 result = sony_pf_add();
1337 if (result)
1338 goto outpresent;
1339
Stelian Pop7f09c432007-01-13 23:04:31 +01001340 if (debug) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001341 status = acpi_walk_namespace(ACPI_TYPE_METHOD,
1342 sony_nc_acpi_handle, 1, sony_walk_callback,
1343 NULL, NULL, NULL);
Stelian Pop7f09c432007-01-13 23:04:31 +01001344 if (ACPI_FAILURE(status)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001345 pr_warn(DRV_PFX "unable to walk acpi resources\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001346 result = -ENODEV;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001347 goto outpresent;
Stelian Pop7f09c432007-01-13 23:04:31 +01001348 }
Stelian Popc5611622007-01-13 23:04:37 +01001349 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001350
Matthew Garrett82734bf2009-03-26 21:58:13 +09001351 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1352 &handle))) {
1353 if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
1354 dprintk("ECON Method failed\n");
1355 }
1356
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001357 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1358 &handle))) {
1359 dprintk("Doing SNC setup\n");
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001360 if (sony_nc_handles_setup(sony_pf_device))
1361 goto outpresent;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001362 sony_nc_function_setup(device);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001363 sony_nc_rfkill_setup(device);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001364 }
1365
malattia@linux.it1549ee62007-04-09 10:19:08 +02001366 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05001367 result = sony_laptop_setup_input(device);
malattia@linux.it1549ee62007-04-09 10:19:08 +02001368 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001369 pr_err(DRV_PFX "Unable to create input devices.\n");
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001370 goto outsnc;
malattia@linux.it1549ee62007-04-09 10:19:08 +02001371 }
1372
Alessandro Guido38cfc142008-11-12 23:03:28 +01001373 if (acpi_video_backlight_support()) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001374 pr_info(DRV_PFX "brightness ignored, must be "
Thomas Renninger540b8bb2008-08-01 17:38:02 +02001375 "controlled by ACPI video driver\n");
1376 } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
1377 &handle))) {
Matthew Garrettbb7ca742011-03-22 16:30:21 -07001378 struct backlight_properties props;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05001379 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -07001380 props.type = BACKLIGHT_PLATFORM;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05001381 props.max_brightness = SONY_MAX_BRIGHTNESS - 1;
Alessandro Guido50f62af2007-01-13 23:04:34 +01001382 sony_backlight_device = backlight_device_register("sony", NULL,
Len Browna02d1c12007-02-07 15:34:02 -05001383 NULL,
Matthew Garretta19a6ee2010-02-17 16:39:44 -05001384 &sony_backlight_ops,
1385 &props);
Mattia Dongili7df03b82007-01-13 23:04:41 +01001386
Len Browna02d1c12007-02-07 15:34:02 -05001387 if (IS_ERR(sony_backlight_device)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001388 pr_warning(DRV_PFX "unable to register backlight device\n");
Mattia Dongili7df03b82007-01-13 23:04:41 +01001389 sony_backlight_device = NULL;
Richard Purdie321709c2007-02-10 15:04:08 +00001390 } else {
1391 sony_backlight_device->props.brightness =
Len Browna02d1c12007-02-07 15:34:02 -05001392 sony_backlight_get_brightness
1393 (sony_backlight_device);
Richard Purdie321709c2007-02-10 15:04:08 +00001394 }
1395
Alessandro Guido50f62af2007-01-13 23:04:34 +01001396 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001397
malattia@linux.it56b87562007-04-09 10:19:05 +02001398 /* create sony_pf sysfs attributes related to the SNC device */
1399 for (item = sony_nc_values; item->name; ++item) {
1400
1401 if (!debug && item->debug)
1402 continue;
1403
1404 /* find the available acpiget as described in the DSDT */
1405 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
1406 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1407 *item->acpiget,
1408 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02001409 dprintk("Found %s getter: %s\n",
1410 item->name, *item->acpiget);
malattia@linux.it56b87562007-04-09 10:19:05 +02001411 item->devattr.attr.mode |= S_IRUGO;
1412 break;
1413 }
1414 }
1415
1416 /* find the available acpiset as described in the DSDT */
1417 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
1418 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1419 *item->acpiset,
1420 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02001421 dprintk("Found %s setter: %s\n",
1422 item->name, *item->acpiset);
malattia@linux.it56b87562007-04-09 10:19:05 +02001423 item->devattr.attr.mode |= S_IWUSR;
1424 break;
1425 }
1426 }
1427
1428 if (item->devattr.attr.mode != 0) {
1429 result =
1430 device_create_file(&sony_pf_device->dev,
1431 &item->devattr);
1432 if (result)
1433 goto out_sysfs;
1434 }
1435 }
1436
Stelian Pop7f09c432007-01-13 23:04:31 +01001437 return 0;
1438
malattia@linux.it56b87562007-04-09 10:19:05 +02001439 out_sysfs:
1440 for (item = sony_nc_values; item->name; ++item) {
1441 device_remove_file(&sony_pf_device->dev, &item->devattr);
1442 }
Mattia Dongili7df03b82007-01-13 23:04:41 +01001443 if (sony_backlight_device)
1444 backlight_device_unregister(sony_backlight_device);
1445
malattia@linux.it1549ee62007-04-09 10:19:08 +02001446 sony_laptop_remove_input();
1447
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001448 outsnc:
1449 sony_nc_handles_cleanup(sony_pf_device);
1450
1451 outpresent:
1452 sony_pf_remove();
1453
Len Browna02d1c12007-02-07 15:34:02 -05001454 outwalk:
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001455 sony_nc_rfkill_cleanup();
Stelian Pop7f09c432007-01-13 23:04:31 +01001456 return result;
1457}
1458
malattia@linux.it59b19102007-04-09 10:19:04 +02001459static int sony_nc_remove(struct acpi_device *device, int type)
Stelian Pop7f09c432007-01-13 23:04:31 +01001460{
malattia@linux.it56b87562007-04-09 10:19:05 +02001461 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01001462
Alessandro Guido50f62af2007-01-13 23:04:34 +01001463 if (sony_backlight_device)
1464 backlight_device_unregister(sony_backlight_device);
1465
malattia@linux.it59b19102007-04-09 10:19:04 +02001466 sony_nc_acpi_device = NULL;
Stelian Popc5611622007-01-13 23:04:37 +01001467
malattia@linux.it56b87562007-04-09 10:19:05 +02001468 for (item = sony_nc_values; item->name; ++item) {
1469 device_remove_file(&sony_pf_device->dev, &item->devattr);
1470 }
1471
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001472 sony_nc_handles_cleanup(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +02001473 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +02001474 sony_laptop_remove_input();
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001475 sony_nc_rfkill_cleanup();
malattia@linux.itf6119b02007-04-09 19:31:06 +02001476 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001477
1478 return 0;
1479}
1480
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001481static const struct acpi_device_id sony_device_ids[] = {
1482 {SONY_NC_HID, 0},
1483 {SONY_PIC_HID, 0},
1484 {"", 0},
1485};
1486MODULE_DEVICE_TABLE(acpi, sony_device_ids);
1487
1488static const struct acpi_device_id sony_nc_device_ids[] = {
1489 {SONY_NC_HID, 0},
1490 {"", 0},
1491};
1492
malattia@linux.it59b19102007-04-09 10:19:04 +02001493static struct acpi_driver sony_nc_driver = {
1494 .name = SONY_NC_DRIVER_NAME,
1495 .class = SONY_NC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001496 .ids = sony_nc_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02001497 .owner = THIS_MODULE,
Len Browna02d1c12007-02-07 15:34:02 -05001498 .ops = {
malattia@linux.it59b19102007-04-09 10:19:04 +02001499 .add = sony_nc_add,
1500 .remove = sony_nc_remove,
1501 .resume = sony_nc_resume,
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001502 .notify = sony_nc_notify,
Len Browna02d1c12007-02-07 15:34:02 -05001503 },
Andrew Morton3f4f4612007-01-13 23:04:32 +01001504};
1505
malattia@linux.it33a04452007-04-09 19:26:03 +02001506/*********** SPIC (SNY6001) Device ***********/
1507
1508#define SONYPI_DEVICE_TYPE1 0x00000001
1509#define SONYPI_DEVICE_TYPE2 0x00000002
1510#define SONYPI_DEVICE_TYPE3 0x00000004
1511
Mattia Dongili22a17782007-07-16 02:34:39 +09001512#define SONYPI_TYPE1_OFFSET 0x04
1513#define SONYPI_TYPE2_OFFSET 0x12
1514#define SONYPI_TYPE3_OFFSET 0x12
malattia@linux.it33a04452007-04-09 19:26:03 +02001515
malattia@linux.it33a04452007-04-09 19:26:03 +02001516struct sony_pic_ioport {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001517 struct acpi_resource_io io1;
1518 struct acpi_resource_io io2;
malattia@linux.it33a04452007-04-09 19:26:03 +02001519 struct list_head list;
1520};
1521
1522struct sony_pic_irq {
1523 struct acpi_resource_irq irq;
1524 struct list_head list;
1525};
1526
Mattia Dongilide920432008-01-14 18:05:43 +09001527struct sonypi_eventtypes {
1528 u8 data;
1529 unsigned long mask;
1530 struct sonypi_event *events;
1531};
1532
malattia@linux.it33a04452007-04-09 19:26:03 +02001533struct sony_pic_dev {
Mattia Dongili31df7142009-09-16 00:05:29 +09001534 struct acpi_device *acpi_dev;
1535 struct sony_pic_irq *cur_irq;
1536 struct sony_pic_ioport *cur_ioport;
1537 struct list_head interrupts;
1538 struct list_head ioports;
1539 struct mutex lock;
1540 struct sonypi_eventtypes *event_types;
1541 int (*handle_irq)(const u8, const u8);
1542 int model;
1543 u16 evport_offset;
1544 u8 camera_power;
1545 u8 bluetooth_power;
1546 u8 wwan_power;
malattia@linux.it33a04452007-04-09 19:26:03 +02001547};
1548
1549static struct sony_pic_dev spic_dev = {
1550 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
1551 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
1552};
1553
Alan Jenkins5e6f9722009-09-16 00:05:32 +09001554static int spic_drv_registered;
1555
malattia@linux.it33a04452007-04-09 19:26:03 +02001556/* Event masks */
1557#define SONYPI_JOGGER_MASK 0x00000001
1558#define SONYPI_CAPTURE_MASK 0x00000002
1559#define SONYPI_FNKEY_MASK 0x00000004
1560#define SONYPI_BLUETOOTH_MASK 0x00000008
1561#define SONYPI_PKEY_MASK 0x00000010
1562#define SONYPI_BACK_MASK 0x00000020
1563#define SONYPI_HELP_MASK 0x00000040
1564#define SONYPI_LID_MASK 0x00000080
1565#define SONYPI_ZOOM_MASK 0x00000100
1566#define SONYPI_THUMBPHRASE_MASK 0x00000200
1567#define SONYPI_MEYE_MASK 0x00000400
1568#define SONYPI_MEMORYSTICK_MASK 0x00000800
1569#define SONYPI_BATTERY_MASK 0x00001000
1570#define SONYPI_WIRELESS_MASK 0x00002000
1571
1572struct sonypi_event {
1573 u8 data;
1574 u8 event;
1575};
1576
1577/* The set of possible button release events */
1578static struct sonypi_event sonypi_releaseev[] = {
1579 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
1580 { 0, 0 }
1581};
1582
1583/* The set of possible jogger events */
1584static struct sonypi_event sonypi_joggerev[] = {
1585 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
1586 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
1587 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
1588 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
1589 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
1590 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
1591 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
1592 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
1593 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
1594 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
1595 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
1596 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
1597 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
1598 { 0, 0 }
1599};
1600
1601/* The set of possible capture button events */
1602static struct sonypi_event sonypi_captureev[] = {
1603 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
1604 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09001605 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02001606 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
1607 { 0, 0 }
1608};
1609
1610/* The set of possible fnkeys events */
1611static struct sonypi_event sonypi_fnkeyev[] = {
1612 { 0x10, SONYPI_EVENT_FNKEY_ESC },
1613 { 0x11, SONYPI_EVENT_FNKEY_F1 },
1614 { 0x12, SONYPI_EVENT_FNKEY_F2 },
1615 { 0x13, SONYPI_EVENT_FNKEY_F3 },
1616 { 0x14, SONYPI_EVENT_FNKEY_F4 },
1617 { 0x15, SONYPI_EVENT_FNKEY_F5 },
1618 { 0x16, SONYPI_EVENT_FNKEY_F6 },
1619 { 0x17, SONYPI_EVENT_FNKEY_F7 },
1620 { 0x18, SONYPI_EVENT_FNKEY_F8 },
1621 { 0x19, SONYPI_EVENT_FNKEY_F9 },
1622 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
1623 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
1624 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
1625 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
1626 { 0x21, SONYPI_EVENT_FNKEY_1 },
1627 { 0x22, SONYPI_EVENT_FNKEY_2 },
1628 { 0x31, SONYPI_EVENT_FNKEY_D },
1629 { 0x32, SONYPI_EVENT_FNKEY_E },
1630 { 0x33, SONYPI_EVENT_FNKEY_F },
1631 { 0x34, SONYPI_EVENT_FNKEY_S },
1632 { 0x35, SONYPI_EVENT_FNKEY_B },
1633 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
1634 { 0, 0 }
1635};
1636
1637/* The set of possible program key events */
1638static struct sonypi_event sonypi_pkeyev[] = {
1639 { 0x01, SONYPI_EVENT_PKEY_P1 },
1640 { 0x02, SONYPI_EVENT_PKEY_P2 },
1641 { 0x04, SONYPI_EVENT_PKEY_P3 },
Harald Jenny1cae7102009-03-26 21:58:18 +09001642 { 0x20, SONYPI_EVENT_PKEY_P1 },
malattia@linux.it33a04452007-04-09 19:26:03 +02001643 { 0, 0 }
1644};
1645
1646/* The set of possible bluetooth events */
1647static struct sonypi_event sonypi_blueev[] = {
1648 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
1649 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
1650 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
1651 { 0, 0 }
1652};
1653
1654/* The set of possible wireless events */
1655static struct sonypi_event sonypi_wlessev[] = {
Mattia Dongili4eeb5022011-02-19 11:52:27 +09001656 { 0x59, SONYPI_EVENT_IGNORE },
1657 { 0x5a, SONYPI_EVENT_IGNORE },
malattia@linux.it33a04452007-04-09 19:26:03 +02001658 { 0, 0 }
1659};
1660
1661/* The set of possible back button events */
1662static struct sonypi_event sonypi_backev[] = {
1663 { 0x20, SONYPI_EVENT_BACK_PRESSED },
1664 { 0, 0 }
1665};
1666
1667/* The set of possible help button events */
1668static struct sonypi_event sonypi_helpev[] = {
1669 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
1670 { 0, 0 }
1671};
1672
1673
1674/* The set of possible lid events */
1675static struct sonypi_event sonypi_lidev[] = {
1676 { 0x51, SONYPI_EVENT_LID_CLOSED },
1677 { 0x50, SONYPI_EVENT_LID_OPENED },
1678 { 0, 0 }
1679};
1680
1681/* The set of possible zoom events */
1682static struct sonypi_event sonypi_zoomev[] = {
1683 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09001684 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
1685 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
Harald Jenny1cae7102009-03-26 21:58:18 +09001686 { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02001687 { 0, 0 }
1688};
1689
1690/* The set of possible thumbphrase events */
1691static struct sonypi_event sonypi_thumbphraseev[] = {
1692 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
1693 { 0, 0 }
1694};
1695
1696/* The set of possible motioneye camera events */
1697static struct sonypi_event sonypi_meyeev[] = {
1698 { 0x00, SONYPI_EVENT_MEYE_FACE },
1699 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
1700 { 0, 0 }
1701};
1702
1703/* The set of possible memorystick events */
1704static struct sonypi_event sonypi_memorystickev[] = {
1705 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
1706 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
1707 { 0, 0 }
1708};
1709
1710/* The set of possible battery events */
1711static struct sonypi_event sonypi_batteryev[] = {
1712 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
1713 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
1714 { 0, 0 }
1715};
1716
Harald Jenny1cae7102009-03-26 21:58:18 +09001717/* The set of possible volume events */
1718static struct sonypi_event sonypi_volumeev[] = {
1719 { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
1720 { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
1721 { 0, 0 }
1722};
1723
1724/* The set of possible brightness events */
1725static struct sonypi_event sonypi_brightnessev[] = {
1726 { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
1727 { 0, 0 }
1728};
1729
Mattia Dongilide920432008-01-14 18:05:43 +09001730static struct sonypi_eventtypes type1_events[] = {
1731 { 0, 0xffffffff, sonypi_releaseev },
1732 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
1733 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
1734 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
1735 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
1736 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1737 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1738 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
1739 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1740 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
1741 { 0 },
1742};
1743static struct sonypi_eventtypes type2_events[] = {
1744 { 0, 0xffffffff, sonypi_releaseev },
1745 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
1746 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
1747 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
1748 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1749 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1750 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
1751 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
1752 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
1753 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
1754 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
1755 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1756 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1757 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1758 { 0 },
1759};
1760static struct sonypi_eventtypes type3_events[] = {
1761 { 0, 0xffffffff, sonypi_releaseev },
1762 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1763 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
1764 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1765 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1766 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09001767 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
1768 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
1769 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
Harald Jenny1cae7102009-03-26 21:58:18 +09001770 { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
1771 { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09001772 { 0 },
Mattia Dongilide920432008-01-14 18:05:43 +09001773};
1774
Mattia Dongili3eb87492008-01-14 18:05:45 +09001775/* low level spic calls */
malattia@linux.it33a04452007-04-09 19:26:03 +02001776#define ITERATIONS_LONG 10000
1777#define ITERATIONS_SHORT 10
1778#define wait_on_command(command, iterations) { \
1779 unsigned int n = iterations; \
1780 while (--n && (command)) \
1781 udelay(1); \
1782 if (!n) \
1783 dprintk("command failed at %s : %s (line %d)\n", \
Harvey Harrison6e574192008-04-29 00:59:20 -07001784 __FILE__, __func__, __LINE__); \
malattia@linux.it33a04452007-04-09 19:26:03 +02001785}
1786
1787static u8 sony_pic_call1(u8 dev)
1788{
1789 u8 v1, v2;
1790
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001791 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02001792 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001793 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1794 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
1795 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09001796 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02001797 return v2;
1798}
1799
1800static u8 sony_pic_call2(u8 dev, u8 fn)
1801{
1802 u8 v1;
1803
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001804 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02001805 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001806 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1807 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02001808 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001809 outb(fn, spic_dev.cur_ioport->io1.minimum);
1810 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09001811 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02001812 return v1;
1813}
1814
malattia@linux.it49a11de2007-04-09 19:28:56 +02001815static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
1816{
1817 u8 v1;
1818
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001819 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1820 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1821 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1822 outb(fn, spic_dev.cur_ioport->io1.minimum);
1823 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1824 outb(v, spic_dev.cur_ioport->io1.minimum);
1825 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09001826 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
1827 dev, fn, v, v1);
malattia@linux.it49a11de2007-04-09 19:28:56 +02001828 return v1;
1829}
1830
Mattia Dongili3eb87492008-01-14 18:05:45 +09001831/*
1832 * minidrivers for SPIC models
1833 */
Mattia Dongilie93c8a62009-03-26 21:58:17 +09001834static int type3_handle_irq(const u8 data_mask, const u8 ev)
Mattia Dongili3eb87492008-01-14 18:05:45 +09001835{
1836 /*
1837 * 0x31 could mean we have to take some extra action and wait for
Mattia Dongilie93c8a62009-03-26 21:58:17 +09001838 * the next irq for some Type3 models, it will generate a new
Mattia Dongili3eb87492008-01-14 18:05:45 +09001839 * irq and we can read new data from the device:
1840 * - 0x5c and 0x5f requires 0xA0
1841 * - 0x61 requires 0xB3
1842 */
1843 if (data_mask == 0x31) {
1844 if (ev == 0x5c || ev == 0x5f)
1845 sony_pic_call1(0xA0);
1846 else if (ev == 0x61)
1847 sony_pic_call1(0xB3);
1848 return 0;
1849 }
1850 return 1;
1851}
1852
Mattia Dongili3eb87492008-01-14 18:05:45 +09001853static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
1854{
1855 struct pci_dev *pcidev;
1856
1857 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1858 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
1859 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09001860 dev->model = SONYPI_DEVICE_TYPE1;
1861 dev->evport_offset = SONYPI_TYPE1_OFFSET;
1862 dev->event_types = type1_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09001863 goto out;
1864 }
1865
1866 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1867 PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
1868 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09001869 dev->model = SONYPI_DEVICE_TYPE2;
1870 dev->evport_offset = SONYPI_TYPE2_OFFSET;
1871 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09001872 goto out;
1873 }
1874
1875 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1876 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
1877 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09001878 dev->model = SONYPI_DEVICE_TYPE3;
1879 dev->handle_irq = type3_handle_irq;
1880 dev->evport_offset = SONYPI_TYPE3_OFFSET;
1881 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09001882 goto out;
1883 }
1884
1885 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1886 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
1887 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09001888 dev->model = SONYPI_DEVICE_TYPE3;
1889 dev->handle_irq = type3_handle_irq;
1890 dev->evport_offset = SONYPI_TYPE3_OFFSET;
1891 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09001892 goto out;
1893 }
1894
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09001895 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1896 PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
1897 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09001898 dev->model = SONYPI_DEVICE_TYPE3;
1899 dev->handle_irq = type3_handle_irq;
1900 dev->evport_offset = SONYPI_TYPE3_OFFSET;
1901 dev->event_types = type3_events;
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09001902 goto out;
1903 }
1904
Mattia Dongili3eb87492008-01-14 18:05:45 +09001905 /* default */
Mattia Dongili31df7142009-09-16 00:05:29 +09001906 dev->model = SONYPI_DEVICE_TYPE2;
1907 dev->evport_offset = SONYPI_TYPE2_OFFSET;
1908 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09001909
1910out:
1911 if (pcidev)
1912 pci_dev_put(pcidev);
1913
Mattia Dongilid6697932011-02-19 11:52:28 +09001914 pr_info(DRV_PFX "detected Type%d model\n",
Mattia Dongili31df7142009-09-16 00:05:29 +09001915 dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
1916 dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
Mattia Dongili3eb87492008-01-14 18:05:45 +09001917}
1918
malattia@linux.it49a11de2007-04-09 19:28:56 +02001919/* camera tests and poweron/poweroff */
1920#define SONYPI_CAMERA_PICTURE 5
malattia@linux.it49a11de2007-04-09 19:28:56 +02001921#define SONYPI_CAMERA_CONTROL 0x10
malattia@linux.ite3646322007-04-28 23:34:22 +09001922
1923#define SONYPI_CAMERA_BRIGHTNESS 0
1924#define SONYPI_CAMERA_CONTRAST 1
1925#define SONYPI_CAMERA_HUE 2
1926#define SONYPI_CAMERA_COLOR 3
1927#define SONYPI_CAMERA_SHARPNESS 4
1928
1929#define SONYPI_CAMERA_EXPOSURE_MASK 0xC
1930#define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
1931#define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
1932#define SONYPI_CAMERA_MUTE_MASK 0x40
1933
1934/* the rest don't need a loop until not 0xff */
1935#define SONYPI_CAMERA_AGC 6
1936#define SONYPI_CAMERA_AGC_MASK 0x30
1937#define SONYPI_CAMERA_SHUTTER_MASK 0x7
1938
1939#define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
1940#define SONYPI_CAMERA_CONTROL 0x10
1941
1942#define SONYPI_CAMERA_STATUS 7
1943#define SONYPI_CAMERA_STATUS_READY 0x2
1944#define SONYPI_CAMERA_STATUS_POSITION 0x4
1945
1946#define SONYPI_DIRECTION_BACKWARDS 0x4
1947
1948#define SONYPI_CAMERA_REVISION 8
1949#define SONYPI_CAMERA_ROMVERSION 9
malattia@linux.it49a11de2007-04-09 19:28:56 +02001950
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001951static int __sony_pic_camera_ready(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001952{
1953 u8 v;
1954
1955 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
1956 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
1957}
1958
malattia@linux.ite3646322007-04-28 23:34:22 +09001959static int __sony_pic_camera_off(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001960{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001961 if (!camera) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001962 pr_warn(DRV_PFX "camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001963 return -ENODEV;
1964 }
1965
malattia@linux.it49a11de2007-04-09 19:28:56 +02001966 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
1967 SONYPI_CAMERA_MUTE_MASK),
1968 ITERATIONS_SHORT);
1969
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001970 if (spic_dev.camera_power) {
1971 sony_pic_call2(0x91, 0);
1972 spic_dev.camera_power = 0;
1973 }
1974 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001975}
1976
malattia@linux.ite3646322007-04-28 23:34:22 +09001977static int __sony_pic_camera_on(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001978{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001979 int i, j, x;
1980
1981 if (!camera) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001982 pr_warn(DRV_PFX "camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001983 return -ENODEV;
1984 }
malattia@linux.it49a11de2007-04-09 19:28:56 +02001985
1986 if (spic_dev.camera_power)
malattia@linux.ite3646322007-04-28 23:34:22 +09001987 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001988
1989 for (j = 5; j > 0; j--) {
1990
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001991 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001992 msleep(10);
1993 sony_pic_call1(0x93);
1994
1995 for (i = 400; i > 0; i--) {
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001996 if (__sony_pic_camera_ready())
malattia@linux.it49a11de2007-04-09 19:28:56 +02001997 break;
1998 msleep(10);
1999 }
2000 if (i)
2001 break;
2002 }
2003
2004 if (j == 0) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002005 pr_warn(DRV_PFX "failed to power on camera\n");
malattia@linux.ite3646322007-04-28 23:34:22 +09002006 return -ENODEV;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002007 }
2008
2009 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
2010 0x5a),
2011 ITERATIONS_SHORT);
2012
2013 spic_dev.camera_power = 1;
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002014 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002015}
2016
malattia@linux.ite3646322007-04-28 23:34:22 +09002017/* External camera command (exported to the motion eye v4l driver) */
2018int sony_pic_camera_command(int command, u8 value)
2019{
2020 if (!camera)
2021 return -EIO;
2022
2023 mutex_lock(&spic_dev.lock);
2024
2025 switch (command) {
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002026 case SONY_PIC_COMMAND_SETCAMERA:
malattia@linux.ite3646322007-04-28 23:34:22 +09002027 if (value)
2028 __sony_pic_camera_on();
2029 else
2030 __sony_pic_camera_off();
2031 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002032 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09002033 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
2034 ITERATIONS_SHORT);
2035 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002036 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
malattia@linux.ite3646322007-04-28 23:34:22 +09002037 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
2038 ITERATIONS_SHORT);
2039 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002040 case SONY_PIC_COMMAND_SETCAMERAHUE:
malattia@linux.ite3646322007-04-28 23:34:22 +09002041 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
2042 ITERATIONS_SHORT);
2043 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002044 case SONY_PIC_COMMAND_SETCAMERACOLOR:
malattia@linux.ite3646322007-04-28 23:34:22 +09002045 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
2046 ITERATIONS_SHORT);
2047 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002048 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09002049 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
2050 ITERATIONS_SHORT);
2051 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002052 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
malattia@linux.ite3646322007-04-28 23:34:22 +09002053 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
2054 ITERATIONS_SHORT);
2055 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002056 case SONY_PIC_COMMAND_SETCAMERAAGC:
malattia@linux.ite3646322007-04-28 23:34:22 +09002057 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
2058 ITERATIONS_SHORT);
2059 break;
2060 default:
Mattia Dongilid6697932011-02-19 11:52:28 +09002061 pr_err(DRV_PFX "sony_pic_camera_command invalid: %d\n",
malattia@linux.ite3646322007-04-28 23:34:22 +09002062 command);
2063 break;
2064 }
2065 mutex_unlock(&spic_dev.lock);
2066 return 0;
2067}
2068EXPORT_SYMBOL(sony_pic_camera_command);
2069
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002070/* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002071static void __sony_pic_set_wwanpower(u8 state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002072{
2073 state = !!state;
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002074 if (spic_dev.wwan_power == state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002075 return;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002076 sony_pic_call2(0xB0, state);
Sergey Yanovich3ad1b762009-03-26 21:58:21 +09002077 sony_pic_call1(0x82);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002078 spic_dev.wwan_power = state;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002079}
2080
2081static ssize_t sony_pic_wwanpower_store(struct device *dev,
2082 struct device_attribute *attr,
2083 const char *buffer, size_t count)
2084{
2085 unsigned long value;
2086 if (count > 31)
2087 return -EINVAL;
2088
2089 value = simple_strtoul(buffer, NULL, 10);
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002090 mutex_lock(&spic_dev.lock);
2091 __sony_pic_set_wwanpower(value);
2092 mutex_unlock(&spic_dev.lock);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002093
2094 return count;
2095}
2096
2097static ssize_t sony_pic_wwanpower_show(struct device *dev,
2098 struct device_attribute *attr, char *buffer)
2099{
2100 ssize_t count;
2101 mutex_lock(&spic_dev.lock);
2102 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
2103 mutex_unlock(&spic_dev.lock);
2104 return count;
2105}
2106
malattia@linux.it49a11de2007-04-09 19:28:56 +02002107/* bluetooth subsystem power state */
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002108static void __sony_pic_set_bluetoothpower(u8 state)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002109{
2110 state = !!state;
2111 if (spic_dev.bluetooth_power == state)
2112 return;
2113 sony_pic_call2(0x96, state);
2114 sony_pic_call1(0x82);
2115 spic_dev.bluetooth_power = state;
2116}
2117
2118static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
2119 struct device_attribute *attr,
2120 const char *buffer, size_t count)
2121{
2122 unsigned long value;
2123 if (count > 31)
2124 return -EINVAL;
2125
2126 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002127 mutex_lock(&spic_dev.lock);
2128 __sony_pic_set_bluetoothpower(value);
2129 mutex_unlock(&spic_dev.lock);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002130
2131 return count;
2132}
2133
2134static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
2135 struct device_attribute *attr, char *buffer)
2136{
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002137 ssize_t count = 0;
2138 mutex_lock(&spic_dev.lock);
2139 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
2140 mutex_unlock(&spic_dev.lock);
2141 return count;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002142}
2143
2144/* fan speed */
2145/* FAN0 information (reverse engineered from ACPI tables) */
2146#define SONY_PIC_FAN0_STATUS 0x93
malattia@linux.it7b153f32007-04-09 19:31:25 +02002147static int sony_pic_set_fanspeed(unsigned long value)
2148{
2149 return ec_write(SONY_PIC_FAN0_STATUS, value);
2150}
2151
2152static int sony_pic_get_fanspeed(u8 *value)
2153{
2154 return ec_read(SONY_PIC_FAN0_STATUS, value);
2155}
2156
malattia@linux.it49a11de2007-04-09 19:28:56 +02002157static ssize_t sony_pic_fanspeed_store(struct device *dev,
2158 struct device_attribute *attr,
2159 const char *buffer, size_t count)
2160{
2161 unsigned long value;
2162 if (count > 31)
2163 return -EINVAL;
2164
2165 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002166 if (sony_pic_set_fanspeed(value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02002167 return -EIO;
2168
2169 return count;
2170}
2171
2172static ssize_t sony_pic_fanspeed_show(struct device *dev,
2173 struct device_attribute *attr, char *buffer)
2174{
2175 u8 value = 0;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002176 if (sony_pic_get_fanspeed(&value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02002177 return -EIO;
2178
2179 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
2180}
2181
2182#define SPIC_ATTR(_name, _mode) \
2183struct device_attribute spic_attr_##_name = __ATTR(_name, \
2184 _mode, sony_pic_## _name ##_show, \
2185 sony_pic_## _name ##_store)
2186
malattia@linux.it49a11de2007-04-09 19:28:56 +02002187static SPIC_ATTR(bluetoothpower, 0644);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002188static SPIC_ATTR(wwanpower, 0644);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002189static SPIC_ATTR(fanspeed, 0644);
2190
2191static struct attribute *spic_attributes[] = {
malattia@linux.it49a11de2007-04-09 19:28:56 +02002192 &spic_attr_bluetoothpower.attr,
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002193 &spic_attr_wwanpower.attr,
malattia@linux.it49a11de2007-04-09 19:28:56 +02002194 &spic_attr_fanspeed.attr,
2195 NULL
2196};
2197
2198static struct attribute_group spic_attribute_group = {
2199 .attrs = spic_attributes
2200};
2201
malattia@linux.it7b153f32007-04-09 19:31:25 +02002202/******** SONYPI compatibility **********/
Mattia Dongilia64e62a2007-05-01 11:19:53 +09002203#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +02002204
2205/* battery / brightness / temperature addresses */
2206#define SONYPI_BAT_FLAGS 0x81
2207#define SONYPI_LCD_LIGHT 0x96
2208#define SONYPI_BAT1_PCTRM 0xa0
2209#define SONYPI_BAT1_LEFT 0xa2
2210#define SONYPI_BAT1_MAXRT 0xa4
2211#define SONYPI_BAT2_PCTRM 0xa8
2212#define SONYPI_BAT2_LEFT 0xaa
2213#define SONYPI_BAT2_MAXRT 0xac
2214#define SONYPI_BAT1_MAXTK 0xb0
2215#define SONYPI_BAT1_FULL 0xb2
2216#define SONYPI_BAT2_MAXTK 0xb8
2217#define SONYPI_BAT2_FULL 0xba
2218#define SONYPI_TEMP_STATUS 0xC1
2219
2220struct sonypi_compat_s {
2221 struct fasync_struct *fifo_async;
Stefani Seibold45465482009-12-21 14:37:26 -08002222 struct kfifo fifo;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002223 spinlock_t fifo_lock;
2224 wait_queue_head_t fifo_proc_list;
2225 atomic_t open_count;
2226};
2227static struct sonypi_compat_s sonypi_compat = {
2228 .open_count = ATOMIC_INIT(0),
2229};
2230
2231static int sonypi_misc_fasync(int fd, struct file *filp, int on)
2232{
Jonathan Corbet60aa4922009-02-01 14:52:56 -07002233 return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002234}
2235
2236static int sonypi_misc_release(struct inode *inode, struct file *file)
2237{
malattia@linux.it7b153f32007-04-09 19:31:25 +02002238 atomic_dec(&sonypi_compat.open_count);
2239 return 0;
2240}
2241
2242static int sonypi_misc_open(struct inode *inode, struct file *file)
2243{
2244 /* Flush input queue on first open */
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002245 unsigned long flags;
2246
Stefani Seibold45465482009-12-21 14:37:26 -08002247 spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002248
malattia@linux.it7b153f32007-04-09 19:31:25 +02002249 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
Stefani Seibolde64c0262009-12-21 14:37:28 -08002250 kfifo_reset(&sonypi_compat.fifo);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002251
Stefani Seibold45465482009-12-21 14:37:26 -08002252 spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002253
malattia@linux.it7b153f32007-04-09 19:31:25 +02002254 return 0;
2255}
2256
2257static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
2258 size_t count, loff_t *pos)
2259{
2260 ssize_t ret;
2261 unsigned char c;
2262
Stefani Seibold45465482009-12-21 14:37:26 -08002263 if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
malattia@linux.it7b153f32007-04-09 19:31:25 +02002264 (file->f_flags & O_NONBLOCK))
2265 return -EAGAIN;
2266
2267 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
Stefani Seibold45465482009-12-21 14:37:26 -08002268 kfifo_len(&sonypi_compat.fifo) != 0);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002269 if (ret)
2270 return ret;
2271
2272 while (ret < count &&
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002273 (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002274 &sonypi_compat.fifo_lock) == sizeof(c))) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02002275 if (put_user(c, buf++))
2276 return -EFAULT;
2277 ret++;
2278 }
2279
2280 if (ret > 0) {
2281 struct inode *inode = file->f_path.dentry->d_inode;
2282 inode->i_atime = current_fs_time(inode->i_sb);
2283 }
2284
2285 return ret;
2286}
2287
2288static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
2289{
2290 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
Stefani Seibold45465482009-12-21 14:37:26 -08002291 if (kfifo_len(&sonypi_compat.fifo))
malattia@linux.it7b153f32007-04-09 19:31:25 +02002292 return POLLIN | POLLRDNORM;
2293 return 0;
2294}
2295
2296static int ec_read16(u8 addr, u16 *value)
2297{
2298 u8 val_lb, val_hb;
2299 if (ec_read(addr, &val_lb))
2300 return -1;
2301 if (ec_read(addr + 1, &val_hb))
2302 return -1;
2303 *value = val_lb | (val_hb << 8);
2304 return 0;
2305}
2306
Alan Cox2b24ef02009-03-26 21:58:19 +09002307static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
2308 unsigned long arg)
malattia@linux.it7b153f32007-04-09 19:31:25 +02002309{
2310 int ret = 0;
2311 void __user *argp = (void __user *)arg;
2312 u8 val8;
2313 u16 val16;
2314 int value;
2315
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002316 mutex_lock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002317 switch (cmd) {
2318 case SONYPI_IOCGBRT:
2319 if (sony_backlight_device == NULL) {
2320 ret = -EIO;
2321 break;
2322 }
2323 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
2324 ret = -EIO;
2325 break;
2326 }
2327 val8 = ((value & 0xff) - 1) << 5;
2328 if (copy_to_user(argp, &val8, sizeof(val8)))
2329 ret = -EFAULT;
2330 break;
2331 case SONYPI_IOCSBRT:
2332 if (sony_backlight_device == NULL) {
2333 ret = -EIO;
2334 break;
2335 }
2336 if (copy_from_user(&val8, argp, sizeof(val8))) {
2337 ret = -EFAULT;
2338 break;
2339 }
2340 if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
2341 (val8 >> 5) + 1, NULL)) {
2342 ret = -EIO;
2343 break;
2344 }
2345 /* sync the backlight device status */
2346 sony_backlight_device->props.brightness =
2347 sony_backlight_get_brightness(sony_backlight_device);
2348 break;
2349 case SONYPI_IOCGBAT1CAP:
2350 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
2351 ret = -EIO;
2352 break;
2353 }
2354 if (copy_to_user(argp, &val16, sizeof(val16)))
2355 ret = -EFAULT;
2356 break;
2357 case SONYPI_IOCGBAT1REM:
2358 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
2359 ret = -EIO;
2360 break;
2361 }
2362 if (copy_to_user(argp, &val16, sizeof(val16)))
2363 ret = -EFAULT;
2364 break;
2365 case SONYPI_IOCGBAT2CAP:
2366 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
2367 ret = -EIO;
2368 break;
2369 }
2370 if (copy_to_user(argp, &val16, sizeof(val16)))
2371 ret = -EFAULT;
2372 break;
2373 case SONYPI_IOCGBAT2REM:
2374 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
2375 ret = -EIO;
2376 break;
2377 }
2378 if (copy_to_user(argp, &val16, sizeof(val16)))
2379 ret = -EFAULT;
2380 break;
2381 case SONYPI_IOCGBATFLAGS:
2382 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
2383 ret = -EIO;
2384 break;
2385 }
2386 val8 &= 0x07;
2387 if (copy_to_user(argp, &val8, sizeof(val8)))
2388 ret = -EFAULT;
2389 break;
2390 case SONYPI_IOCGBLUE:
2391 val8 = spic_dev.bluetooth_power;
2392 if (copy_to_user(argp, &val8, sizeof(val8)))
2393 ret = -EFAULT;
2394 break;
2395 case SONYPI_IOCSBLUE:
2396 if (copy_from_user(&val8, argp, sizeof(val8))) {
2397 ret = -EFAULT;
2398 break;
2399 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002400 __sony_pic_set_bluetoothpower(val8);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002401 break;
2402 /* FAN Controls */
2403 case SONYPI_IOCGFAN:
2404 if (sony_pic_get_fanspeed(&val8)) {
2405 ret = -EIO;
2406 break;
2407 }
2408 if (copy_to_user(argp, &val8, sizeof(val8)))
2409 ret = -EFAULT;
2410 break;
2411 case SONYPI_IOCSFAN:
2412 if (copy_from_user(&val8, argp, sizeof(val8))) {
2413 ret = -EFAULT;
2414 break;
2415 }
2416 if (sony_pic_set_fanspeed(val8))
2417 ret = -EIO;
2418 break;
2419 /* GET Temperature (useful under APM) */
2420 case SONYPI_IOCGTEMP:
2421 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
2422 ret = -EIO;
2423 break;
2424 }
2425 if (copy_to_user(argp, &val8, sizeof(val8)))
2426 ret = -EFAULT;
2427 break;
2428 default:
2429 ret = -EINVAL;
2430 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002431 mutex_unlock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002432 return ret;
2433}
2434
2435static const struct file_operations sonypi_misc_fops = {
2436 .owner = THIS_MODULE,
2437 .read = sonypi_misc_read,
2438 .poll = sonypi_misc_poll,
2439 .open = sonypi_misc_open,
2440 .release = sonypi_misc_release,
2441 .fasync = sonypi_misc_fasync,
Alan Cox2b24ef02009-03-26 21:58:19 +09002442 .unlocked_ioctl = sonypi_misc_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002443 .llseek = noop_llseek,
malattia@linux.it7b153f32007-04-09 19:31:25 +02002444};
2445
2446static struct miscdevice sonypi_misc_device = {
2447 .minor = MISC_DYNAMIC_MINOR,
2448 .name = "sonypi",
2449 .fops = &sonypi_misc_fops,
2450};
2451
2452static void sonypi_compat_report_event(u8 event)
2453{
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002454 kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002455 sizeof(event), &sonypi_compat.fifo_lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002456 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
2457 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
2458}
2459
2460static int sonypi_compat_init(void)
2461{
2462 int error;
2463
2464 spin_lock_init(&sonypi_compat.fifo_lock);
Stefani Seibold45465482009-12-21 14:37:26 -08002465 error =
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002466 kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -08002467 if (error) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002468 pr_err(DRV_PFX "kfifo_alloc failed\n");
Stefani Seibold45465482009-12-21 14:37:26 -08002469 return error;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002470 }
2471
2472 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002473
2474 if (minor != -1)
2475 sonypi_misc_device.minor = minor;
2476 error = misc_register(&sonypi_misc_device);
2477 if (error) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002478 pr_err(DRV_PFX "misc_register failed\n");
malattia@linux.it7b153f32007-04-09 19:31:25 +02002479 goto err_free_kfifo;
2480 }
2481 if (minor == -1)
Mattia Dongilid6697932011-02-19 11:52:28 +09002482 pr_info(DRV_PFX "device allocated minor is %d\n",
malattia@linux.it7b153f32007-04-09 19:31:25 +02002483 sonypi_misc_device.minor);
2484
2485 return 0;
2486
2487err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -08002488 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002489 return error;
2490}
2491
2492static void sonypi_compat_exit(void)
2493{
2494 misc_deregister(&sonypi_misc_device);
Stefani Seibold45465482009-12-21 14:37:26 -08002495 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002496}
2497#else
2498static int sonypi_compat_init(void) { return 0; }
2499static void sonypi_compat_exit(void) { }
2500static void sonypi_compat_report_event(u8 event) { }
Mattia Dongilia64e62a2007-05-01 11:19:53 +09002501#endif /* CONFIG_SONYPI_COMPAT */
malattia@linux.it7b153f32007-04-09 19:31:25 +02002502
malattia@linux.it1549ee62007-04-09 10:19:08 +02002503/*
malattia@linux.it33a04452007-04-09 19:26:03 +02002504 * ACPI callbacks
malattia@linux.it1549ee62007-04-09 10:19:08 +02002505 */
malattia@linux.it33a04452007-04-09 19:26:03 +02002506static acpi_status
2507sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
2508{
2509 u32 i;
2510 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
2511
2512 switch (resource->type) {
2513 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002514 {
2515 /* start IO enumeration */
2516 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
2517 if (!ioport)
2518 return AE_ERROR;
2519
2520 list_add(&ioport->list, &dev->ioports);
2521 return AE_OK;
2522 }
2523
malattia@linux.it33a04452007-04-09 19:26:03 +02002524 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002525 /* end IO enumeration */
malattia@linux.it33a04452007-04-09 19:26:03 +02002526 return AE_OK;
2527
2528 case ACPI_RESOURCE_TYPE_IRQ:
2529 {
2530 struct acpi_resource_irq *p = &resource->data.irq;
2531 struct sony_pic_irq *interrupt = NULL;
2532 if (!p || !p->interrupt_count) {
2533 /*
2534 * IRQ descriptors may have no IRQ# bits set,
2535 * particularly those those w/ _STA disabled
2536 */
2537 dprintk("Blank IRQ resource\n");
2538 return AE_OK;
2539 }
2540 for (i = 0; i < p->interrupt_count; i++) {
2541 if (!p->interrupts[i]) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002542 pr_warn(DRV_PFX "Invalid IRQ %d\n",
malattia@linux.it33a04452007-04-09 19:26:03 +02002543 p->interrupts[i]);
2544 continue;
2545 }
2546 interrupt = kzalloc(sizeof(*interrupt),
2547 GFP_KERNEL);
2548 if (!interrupt)
2549 return AE_ERROR;
2550
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002551 list_add(&interrupt->list, &dev->interrupts);
malattia@linux.it33a04452007-04-09 19:26:03 +02002552 interrupt->irq.triggering = p->triggering;
2553 interrupt->irq.polarity = p->polarity;
2554 interrupt->irq.sharable = p->sharable;
2555 interrupt->irq.interrupt_count = 1;
2556 interrupt->irq.interrupts[0] = p->interrupts[i];
2557 }
2558 return AE_OK;
2559 }
2560 case ACPI_RESOURCE_TYPE_IO:
2561 {
2562 struct acpi_resource_io *io = &resource->data.io;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002563 struct sony_pic_ioport *ioport =
2564 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
malattia@linux.it33a04452007-04-09 19:26:03 +02002565 if (!io) {
2566 dprintk("Blank IO resource\n");
2567 return AE_OK;
2568 }
2569
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002570 if (!ioport->io1.minimum) {
2571 memcpy(&ioport->io1, io, sizeof(*io));
2572 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
2573 ioport->io1.address_length);
2574 }
2575 else if (!ioport->io2.minimum) {
2576 memcpy(&ioport->io2, io, sizeof(*io));
2577 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
2578 ioport->io2.address_length);
2579 }
2580 else {
Mattia Dongilid6697932011-02-19 11:52:28 +09002581 pr_err(DRV_PFX "Unknown SPIC Type, more than 2 IO Ports\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002582 return AE_ERROR;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002583 }
malattia@linux.it33a04452007-04-09 19:26:03 +02002584 return AE_OK;
2585 }
2586 default:
2587 dprintk("Resource %d isn't an IRQ nor an IO port\n",
2588 resource->type);
2589
2590 case ACPI_RESOURCE_TYPE_END_TAG:
2591 return AE_OK;
2592 }
2593 return AE_CTRL_TERMINATE;
2594}
2595
2596static int sony_pic_possible_resources(struct acpi_device *device)
2597{
2598 int result = 0;
2599 acpi_status status = AE_OK;
2600
2601 if (!device)
2602 return -EINVAL;
2603
2604 /* get device status */
2605 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
2606 dprintk("Evaluating _STA\n");
2607 result = acpi_bus_get_status(device);
2608 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002609 pr_warn(DRV_PFX "Unable to read status\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002610 goto end;
2611 }
2612
2613 if (!device->status.enabled)
2614 dprintk("Device disabled\n");
2615 else
2616 dprintk("Device enabled\n");
2617
2618 /*
2619 * Query and parse 'method'
2620 */
2621 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
2622 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
2623 sony_pic_read_possible_resource, &spic_dev);
2624 if (ACPI_FAILURE(status)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002625 pr_warn(DRV_PFX "Failure evaluating %s\n",
malattia@linux.it33a04452007-04-09 19:26:03 +02002626 METHOD_NAME__PRS);
2627 result = -ENODEV;
2628 }
2629end:
2630 return result;
2631}
2632
2633/*
2634 * Disable the spic device by calling its _DIS method
2635 */
2636static int sony_pic_disable(struct acpi_device *device)
2637{
Matthew Garrett6158d3a2008-10-29 14:01:03 -07002638 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
2639 NULL);
2640
2641 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
malattia@linux.it33a04452007-04-09 19:26:03 +02002642 return -ENXIO;
2643
2644 dprintk("Device disabled\n");
2645 return 0;
2646}
2647
2648
2649/*
2650 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
2651 *
2652 * Call _SRS to set current resources
2653 */
2654static int sony_pic_enable(struct acpi_device *device,
2655 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
2656{
2657 acpi_status status;
2658 int result = 0;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002659 /* Type 1 resource layout is:
2660 * IO
2661 * IO
2662 * IRQNoFlags
2663 * End
2664 *
2665 * Type 2 and 3 resource layout is:
2666 * IO
2667 * IRQNoFlags
2668 * End
2669 */
malattia@linux.it33a04452007-04-09 19:26:03 +02002670 struct {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002671 struct acpi_resource res1;
2672 struct acpi_resource res2;
2673 struct acpi_resource res3;
2674 struct acpi_resource res4;
malattia@linux.it33a04452007-04-09 19:26:03 +02002675 } *resource;
2676 struct acpi_buffer buffer = { 0, NULL };
2677
2678 if (!ioport || !irq)
2679 return -EINVAL;
2680
2681 /* init acpi_buffer */
2682 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
2683 if (!resource)
2684 return -ENOMEM;
2685
2686 buffer.length = sizeof(*resource) + 1;
2687 buffer.pointer = resource;
2688
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002689 /* setup Type 1 resources */
Mattia Dongili31df7142009-09-16 00:05:29 +09002690 if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002691
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002692 /* setup io resources */
2693 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2694 resource->res1.length = sizeof(struct acpi_resource);
2695 memcpy(&resource->res1.data.io, &ioport->io1,
2696 sizeof(struct acpi_resource_io));
malattia@linux.it33a04452007-04-09 19:26:03 +02002697
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002698 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
2699 resource->res2.length = sizeof(struct acpi_resource);
2700 memcpy(&resource->res2.data.io, &ioport->io2,
2701 sizeof(struct acpi_resource_io));
2702
2703 /* setup irq resource */
2704 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
2705 resource->res3.length = sizeof(struct acpi_resource);
2706 memcpy(&resource->res3.data.irq, &irq->irq,
2707 sizeof(struct acpi_resource_irq));
2708 /* we requested a shared irq */
2709 resource->res3.data.irq.sharable = ACPI_SHARED;
2710
2711 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
2712
2713 }
2714 /* setup Type 2/3 resources */
2715 else {
2716 /* setup io resource */
2717 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2718 resource->res1.length = sizeof(struct acpi_resource);
2719 memcpy(&resource->res1.data.io, &ioport->io1,
2720 sizeof(struct acpi_resource_io));
2721
2722 /* setup irq resource */
2723 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
2724 resource->res2.length = sizeof(struct acpi_resource);
2725 memcpy(&resource->res2.data.irq, &irq->irq,
2726 sizeof(struct acpi_resource_irq));
2727 /* we requested a shared irq */
2728 resource->res2.data.irq.sharable = ACPI_SHARED;
2729
2730 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
2731 }
malattia@linux.it33a04452007-04-09 19:26:03 +02002732
2733 /* Attempt to set the resource */
2734 dprintk("Evaluating _SRS\n");
2735 status = acpi_set_current_resources(device->handle, &buffer);
2736
2737 /* check for total failure */
2738 if (ACPI_FAILURE(status)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002739 pr_err(DRV_PFX "Error evaluating _SRS\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002740 result = -ENODEV;
2741 goto end;
2742 }
2743
2744 /* Necessary device initializations calls (from sonypi) */
2745 sony_pic_call1(0x82);
2746 sony_pic_call2(0x81, 0xff);
2747 sony_pic_call1(compat ? 0x92 : 0x82);
2748
2749end:
2750 kfree(resource);
2751 return result;
2752}
2753
2754/*****************
2755 *
2756 * ISR: some event is available
2757 *
2758 *****************/
2759static irqreturn_t sony_pic_irq(int irq, void *dev_id)
2760{
2761 int i, j;
malattia@linux.it33a04452007-04-09 19:26:03 +02002762 u8 ev = 0;
2763 u8 data_mask = 0;
2764 u8 device_event = 0;
2765
2766 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
2767
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002768 ev = inb_p(dev->cur_ioport->io1.minimum);
2769 if (dev->cur_ioport->io2.minimum)
2770 data_mask = inb_p(dev->cur_ioport->io2.minimum);
2771 else
Mattia Dongilide920432008-01-14 18:05:43 +09002772 data_mask = inb_p(dev->cur_ioport->io1.minimum +
Mattia Dongili31df7142009-09-16 00:05:29 +09002773 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02002774
Mattia Dongili22a17782007-07-16 02:34:39 +09002775 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
Mattia Dongilide920432008-01-14 18:05:43 +09002776 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09002777 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02002778
2779 if (ev == 0x00 || ev == 0xff)
2780 return IRQ_HANDLED;
2781
Mattia Dongili31df7142009-09-16 00:05:29 +09002782 for (i = 0; dev->event_types[i].mask; i++) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002783
Mattia Dongili31df7142009-09-16 00:05:29 +09002784 if ((data_mask & dev->event_types[i].data) !=
2785 dev->event_types[i].data)
malattia@linux.it33a04452007-04-09 19:26:03 +02002786 continue;
2787
Mattia Dongili31df7142009-09-16 00:05:29 +09002788 if (!(mask & dev->event_types[i].mask))
malattia@linux.it33a04452007-04-09 19:26:03 +02002789 continue;
2790
Mattia Dongili31df7142009-09-16 00:05:29 +09002791 for (j = 0; dev->event_types[i].events[j].event; j++) {
2792 if (ev == dev->event_types[i].events[j].data) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002793 device_event =
Mattia Dongili31df7142009-09-16 00:05:29 +09002794 dev->event_types[i].events[j].event;
Mattia Dongili4eeb5022011-02-19 11:52:27 +09002795 /* some events may require ignoring */
2796 if (!device_event)
2797 return IRQ_HANDLED;
malattia@linux.it33a04452007-04-09 19:26:03 +02002798 goto found;
2799 }
2800 }
2801 }
Mattia Dongili3eb87492008-01-14 18:05:45 +09002802 /* Still not able to decode the event try to pass
2803 * it over to the minidriver
2804 */
Mattia Dongili31df7142009-09-16 00:05:29 +09002805 if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
Mattia Dongili3eb87492008-01-14 18:05:45 +09002806 return IRQ_HANDLED;
2807
Mattia Dongilide920432008-01-14 18:05:43 +09002808 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
2809 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09002810 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02002811 return IRQ_HANDLED;
2812
2813found:
malattia@linux.it1549ee62007-04-09 10:19:08 +02002814 sony_laptop_report_input_event(device_event);
Mattia Dongilide920432008-01-14 18:05:43 +09002815 acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002816 sonypi_compat_report_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02002817 return IRQ_HANDLED;
2818}
2819
2820/*****************
2821 *
2822 * ACPI driver
2823 *
2824 *****************/
2825static int sony_pic_remove(struct acpi_device *device, int type)
2826{
2827 struct sony_pic_ioport *io, *tmp_io;
2828 struct sony_pic_irq *irq, *tmp_irq;
2829
2830 if (sony_pic_disable(device)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002831 pr_err(DRV_PFX "Couldn't disable device.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002832 return -ENXIO;
2833 }
2834
2835 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002836 release_region(spic_dev.cur_ioport->io1.minimum,
2837 spic_dev.cur_ioport->io1.address_length);
2838 if (spic_dev.cur_ioport->io2.minimum)
2839 release_region(spic_dev.cur_ioport->io2.minimum,
2840 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02002841
Mattia Dongili015a9162007-08-12 16:20:27 +09002842 sonypi_compat_exit();
2843
malattia@linux.it1549ee62007-04-09 10:19:08 +02002844 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02002845
malattia@linux.it49a11de2007-04-09 19:28:56 +02002846 /* pf attrs */
2847 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2848 sony_pf_remove();
2849
malattia@linux.it33a04452007-04-09 19:26:03 +02002850 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2851 list_del(&io->list);
2852 kfree(io);
2853 }
2854 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2855 list_del(&irq->list);
2856 kfree(irq);
2857 }
2858 spic_dev.cur_ioport = NULL;
2859 spic_dev.cur_irq = NULL;
2860
malattia@linux.itf6119b02007-04-09 19:31:06 +02002861 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002862 return 0;
2863}
2864
2865static int sony_pic_add(struct acpi_device *device)
2866{
2867 int result;
2868 struct sony_pic_ioport *io, *tmp_io;
2869 struct sony_pic_irq *irq, *tmp_irq;
2870
Mattia Dongilid6697932011-02-19 11:52:28 +09002871 pr_info(DRV_PFX "%s v%s.\n", SONY_PIC_DRIVER_NAME,
2872 SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.it33a04452007-04-09 19:26:03 +02002873
2874 spic_dev.acpi_dev = device;
2875 strcpy(acpi_device_class(device), "sony/hotkey");
Mattia Dongilide920432008-01-14 18:05:43 +09002876 sony_pic_detect_device_type(&spic_dev);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002877 mutex_init(&spic_dev.lock);
malattia@linux.it33a04452007-04-09 19:26:03 +02002878
2879 /* read _PRS resources */
2880 result = sony_pic_possible_resources(device);
2881 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002882 pr_err(DRV_PFX "Unable to read possible resources.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002883 goto err_free_resources;
2884 }
2885
2886 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05002887 result = sony_laptop_setup_input(device);
malattia@linux.it33a04452007-04-09 19:26:03 +02002888 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002889 pr_err(DRV_PFX "Unable to create input devices.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002890 goto err_free_resources;
2891 }
2892
Mattia Dongili015a9162007-08-12 16:20:27 +09002893 if (sonypi_compat_init())
2894 goto err_remove_input;
2895
malattia@linux.it33a04452007-04-09 19:26:03 +02002896 /* request io port */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002897 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
2898 if (request_region(io->io1.minimum, io->io1.address_length,
malattia@linux.it33a04452007-04-09 19:26:03 +02002899 "Sony Programable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002900 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
2901 io->io1.minimum, io->io1.maximum,
2902 io->io1.address_length);
2903 /* Type 1 have 2 ioports */
2904 if (io->io2.minimum) {
2905 if (request_region(io->io2.minimum,
2906 io->io2.address_length,
2907 "Sony Programable I/O Device")) {
2908 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
2909 io->io2.minimum, io->io2.maximum,
2910 io->io2.address_length);
2911 spic_dev.cur_ioport = io;
2912 break;
2913 }
2914 else {
2915 dprintk("Unable to get I/O port2: "
2916 "0x%.4x (0x%.4x) + 0x%.2x\n",
2917 io->io2.minimum, io->io2.maximum,
2918 io->io2.address_length);
2919 release_region(io->io1.minimum,
2920 io->io1.address_length);
2921 }
2922 }
2923 else {
2924 spic_dev.cur_ioport = io;
2925 break;
2926 }
malattia@linux.it33a04452007-04-09 19:26:03 +02002927 }
2928 }
2929 if (!spic_dev.cur_ioport) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002930 pr_err(DRV_PFX "Failed to request_region.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002931 result = -ENODEV;
Mattia Dongili015a9162007-08-12 16:20:27 +09002932 goto err_remove_compat;
malattia@linux.it33a04452007-04-09 19:26:03 +02002933 }
2934
2935 /* request IRQ */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002936 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002937 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
Mattia Dongilid1e0de92009-09-16 00:05:30 +09002938 IRQF_DISABLED, "sony-laptop", &spic_dev)) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002939 dprintk("IRQ: %d - triggering: %d - "
2940 "polarity: %d - shr: %d\n",
2941 irq->irq.interrupts[0],
2942 irq->irq.triggering,
2943 irq->irq.polarity,
2944 irq->irq.sharable);
2945 spic_dev.cur_irq = irq;
2946 break;
2947 }
2948 }
2949 if (!spic_dev.cur_irq) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002950 pr_err(DRV_PFX "Failed to request_irq.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002951 result = -ENODEV;
2952 goto err_release_region;
2953 }
2954
2955 /* set resource status _SRS */
2956 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2957 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002958 pr_err(DRV_PFX "Couldn't enable device.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002959 goto err_free_irq;
2960 }
2961
malattia@linux.it49a11de2007-04-09 19:28:56 +02002962 spic_dev.bluetooth_power = -1;
2963 /* create device attributes */
2964 result = sony_pf_add();
2965 if (result)
2966 goto err_disable_device;
2967
2968 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2969 if (result)
2970 goto err_remove_pf;
2971
malattia@linux.it33a04452007-04-09 19:26:03 +02002972 return 0;
2973
malattia@linux.it49a11de2007-04-09 19:28:56 +02002974err_remove_pf:
2975 sony_pf_remove();
2976
2977err_disable_device:
2978 sony_pic_disable(device);
2979
malattia@linux.it33a04452007-04-09 19:26:03 +02002980err_free_irq:
2981 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2982
2983err_release_region:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002984 release_region(spic_dev.cur_ioport->io1.minimum,
2985 spic_dev.cur_ioport->io1.address_length);
2986 if (spic_dev.cur_ioport->io2.minimum)
2987 release_region(spic_dev.cur_ioport->io2.minimum,
2988 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02002989
Mattia Dongili015a9162007-08-12 16:20:27 +09002990err_remove_compat:
2991 sonypi_compat_exit();
2992
malattia@linux.it33a04452007-04-09 19:26:03 +02002993err_remove_input:
malattia@linux.it1549ee62007-04-09 10:19:08 +02002994 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02002995
2996err_free_resources:
2997 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2998 list_del(&io->list);
2999 kfree(io);
3000 }
3001 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
3002 list_del(&irq->list);
3003 kfree(irq);
3004 }
3005 spic_dev.cur_ioport = NULL;
3006 spic_dev.cur_irq = NULL;
3007
3008 return result;
3009}
3010
3011static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
3012{
3013 if (sony_pic_disable(device))
3014 return -ENXIO;
3015 return 0;
3016}
3017
3018static int sony_pic_resume(struct acpi_device *device)
3019{
3020 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
3021 return 0;
3022}
3023
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003024static const struct acpi_device_id sony_pic_device_ids[] = {
3025 {SONY_PIC_HID, 0},
3026 {"", 0},
3027};
3028
malattia@linux.it33a04452007-04-09 19:26:03 +02003029static struct acpi_driver sony_pic_driver = {
3030 .name = SONY_PIC_DRIVER_NAME,
3031 .class = SONY_PIC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003032 .ids = sony_pic_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02003033 .owner = THIS_MODULE,
3034 .ops = {
3035 .add = sony_pic_add,
3036 .remove = sony_pic_remove,
3037 .suspend = sony_pic_suspend,
3038 .resume = sony_pic_resume,
3039 },
3040};
3041
3042static struct dmi_system_id __initdata sonypi_dmi_table[] = {
3043 {
3044 .ident = "Sony Vaio",
3045 .matches = {
3046 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
3047 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
3048 },
3049 },
3050 {
3051 .ident = "Sony Vaio",
3052 .matches = {
3053 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
3054 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
3055 },
3056 },
3057 { }
3058};
3059
malattia@linux.it59b19102007-04-09 10:19:04 +02003060static int __init sony_laptop_init(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01003061{
malattia@linux.it33a04452007-04-09 19:26:03 +02003062 int result;
3063
3064 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
3065 result = acpi_bus_register_driver(&sony_pic_driver);
3066 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003067 pr_err(DRV_PFX "Unable to register SPIC driver.");
malattia@linux.it33a04452007-04-09 19:26:03 +02003068 goto out;
3069 }
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003070 spic_drv_registered = 1;
malattia@linux.it33a04452007-04-09 19:26:03 +02003071 }
3072
3073 result = acpi_bus_register_driver(&sony_nc_driver);
3074 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003075 pr_err(DRV_PFX "Unable to register SNC driver.");
malattia@linux.it33a04452007-04-09 19:26:03 +02003076 goto out_unregister_pic;
3077 }
3078
3079 return 0;
3080
3081out_unregister_pic:
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003082 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02003083 acpi_bus_unregister_driver(&sony_pic_driver);
3084out:
3085 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +01003086}
3087
malattia@linux.it59b19102007-04-09 10:19:04 +02003088static void __exit sony_laptop_exit(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01003089{
malattia@linux.it59b19102007-04-09 10:19:04 +02003090 acpi_bus_unregister_driver(&sony_nc_driver);
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003091 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02003092 acpi_bus_unregister_driver(&sony_pic_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +01003093}
3094
malattia@linux.it59b19102007-04-09 10:19:04 +02003095module_init(sony_laptop_init);
3096module_exit(sony_laptop_exit);