blob: 1387c5f9c24d926aef958528cc031a5478475e1f [file] [log] [blame]
Stelian Pop7f09c432007-01-13 23:04:31 +01001/*
malattia@linux.it33a04452007-04-09 19:26:03 +02002 * ACPI Sony Notebook Control Driver (SNC and SPIC)
Stelian Pop7f09c432007-01-13 23:04:31 +01003 *
4 * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
Mattia Dongili37418342009-03-26 21:58:24 +09005 * Copyright (C) 2007-2009 Mattia Dongili <malattia@linux.it>
Stelian Pop7f09c432007-01-13 23:04:31 +01006 *
7 * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
8 * which are copyrighted by their respective authors.
9 *
malattia@linux.it33a04452007-04-09 19:26:03 +020010 * The SNY6001 driver part is based on the sonypi driver which includes
11 * material from:
12 *
13 * Copyright (C) 2001-2005 Stelian Pop <stelian@popies.net>
14 *
15 * Copyright (C) 2005 Narayanan R S <nars@kadamba.org>
16 *
Marcin Garskidb9551702007-10-19 23:22:11 +020017 * Copyright (C) 2001-2002 AlcĂ´ve <www.alcove.com>
malattia@linux.it33a04452007-04-09 19:26:03 +020018 *
19 * Copyright (C) 2001 Michael Ashley <m.ashley@unsw.edu.au>
20 *
21 * Copyright (C) 2001 Junichi Morita <jun1m@mars.dti.ne.jp>
22 *
23 * Copyright (C) 2000 Takaya Kinjo <t-kinjo@tc4.so-net.ne.jp>
24 *
25 * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
26 *
27 * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
28 *
Stelian Pop7f09c432007-01-13 23:04:31 +010029 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
33 *
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
38 *
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
42 *
43 */
44
45#include <linux/kernel.h>
46#include <linux/module.h>
47#include <linux/moduleparam.h>
48#include <linux/init.h>
49#include <linux/types.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010050#include <linux/backlight.h>
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010051#include <linux/platform_device.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010052#include <linux/err.h>
malattia@linux.it33a04452007-04-09 19:26:03 +020053#include <linux/dmi.h>
54#include <linux/pci.h>
55#include <linux/interrupt.h>
56#include <linux/delay.h>
57#include <linux/input.h>
58#include <linux/kfifo.h>
59#include <linux/workqueue.h>
60#include <linux/acpi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090061#include <linux/slab.h>
Stelian Pop7f09c432007-01-13 23:04:31 +010062#include <acpi/acpi_drivers.h>
63#include <acpi/acpi_bus.h>
64#include <asm/uaccess.h>
malattia@linux.it33a04452007-04-09 19:26:03 +020065#include <linux/sonypi.h>
malattia@linux.it1ce82c12007-04-28 23:34:36 +090066#include <linux/sony-laptop.h>
Matthew Garrett6cc056b2009-03-26 21:58:15 +090067#include <linux/rfkill.h>
Mattia Dongilia64e62a2007-05-01 11:19:53 +090068#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +020069#include <linux/poll.h>
70#include <linux/miscdevice.h>
71#endif
Stelian Pop7f09c432007-01-13 23:04:31 +010072
malattia@linux.it33a04452007-04-09 19:26:03 +020073#define DRV_PFX "sony-laptop: "
malattia@linux.itb9a218b2007-04-09 10:19:06 +020074#define dprintk(msg...) do { \
malattia@linux.itf6119b02007-04-09 19:31:06 +020075 if (debug) printk(KERN_WARNING DRV_PFX msg); \
malattia@linux.itb9a218b2007-04-09 10:19:06 +020076} while (0)
77
Mattia Dongili425ef5d2008-01-14 18:05:44 +090078#define SONY_LAPTOP_DRIVER_VERSION "0.6"
Alessandro Guido50f62af2007-01-13 23:04:34 +010079
malattia@linux.it33a04452007-04-09 19:26:03 +020080#define SONY_NC_CLASS "sony-nc"
81#define SONY_NC_HID "SNY5001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020082#define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
malattia@linux.it33a04452007-04-09 19:26:03 +020083
84#define SONY_PIC_CLASS "sony-pic"
85#define SONY_PIC_HID "SNY6001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020086#define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
Stelian Pop7f09c432007-01-13 23:04:31 +010087
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010088MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
malattia@linux.it33a04452007-04-09 19:26:03 +020089MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
Stelian Pop7f09c432007-01-13 23:04:31 +010090MODULE_LICENSE("GPL");
malattia@linux.it33a04452007-04-09 19:26:03 +020091MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
Stelian Pop7f09c432007-01-13 23:04:31 +010092
93static int debug;
94module_param(debug, int, 0);
95MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
Len Browna02d1c12007-02-07 15:34:02 -050096 "the development of this driver");
Stelian Pop7f09c432007-01-13 23:04:31 +010097
malattia@linux.it33a04452007-04-09 19:26:03 +020098static int no_spic; /* = 0 */
99module_param(no_spic, int, 0444);
100MODULE_PARM_DESC(no_spic,
101 "set this if you don't want to enable the SPIC device");
102
103static int compat; /* = 0 */
104module_param(compat, int, 0444);
105MODULE_PARM_DESC(compat,
malattia@linux.it7b153f32007-04-09 19:31:25 +0200106 "set this if you want to enable backward compatibility mode");
malattia@linux.it33a04452007-04-09 19:26:03 +0200107
108static unsigned long mask = 0xffffffff;
109module_param(mask, ulong, 0644);
110MODULE_PARM_DESC(mask,
111 "set this to the mask of event you want to enable (see doc)");
112
malattia@linux.it5f3d2892007-04-28 23:18:45 +0900113static int camera; /* = 0 */
114module_param(camera, int, 0444);
115MODULE_PARM_DESC(camera,
116 "set this to 1 to enable Motion Eye camera controls "
117 "(only use it if you have a C1VE or C1VN model)");
118
Mattia Dongilia64e62a2007-05-01 11:19:53 +0900119#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +0200120static int minor = -1;
121module_param(minor, int, 0);
122MODULE_PARM_DESC(minor,
123 "minor number of the misc device for the SPIC compatibility code, "
124 "default is -1 (automatic)");
125#endif
126
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900127enum sony_nc_rfkill {
128 SONY_WIFI,
129 SONY_BLUETOOTH,
130 SONY_WWAN,
131 SONY_WIMAX,
Johannes Berg19d337d2009-06-02 13:01:37 +0200132 N_SONY_RFKILL,
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900133};
134
Mattia Dongilid5a664a2009-12-17 00:08:35 +0900135static int sony_rfkill_handle;
Johannes Berg19d337d2009-06-02 13:01:37 +0200136static struct rfkill *sony_rfkill_devices[N_SONY_RFKILL];
137static int sony_rfkill_address[N_SONY_RFKILL] = {0x300, 0x500, 0x700, 0x900};
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900138static void sony_nc_rfkill_update(void);
139
malattia@linux.it1549ee62007-04-09 10:19:08 +0200140/*********** Input Devices ***********/
141
142#define SONY_LAPTOP_BUF_SIZE 128
143struct sony_laptop_input_s {
144 atomic_t users;
145 struct input_dev *jog_dev;
146 struct input_dev *key_dev;
Stefani Seibold45465482009-12-21 14:37:26 -0800147 struct kfifo fifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200148 spinlock_t fifo_lock;
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800149 struct timer_list release_key_timer;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200150};
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900151
malattia@linux.it1549ee62007-04-09 10:19:08 +0200152static struct sony_laptop_input_s sony_laptop_input = {
153 .users = ATOMIC_INIT(0),
154};
155
156struct sony_laptop_keypress {
157 struct input_dev *dev;
158 int key;
159};
160
Mattia Dongilibc57f862007-07-20 02:01:57 +0900161/* Correspondance table between sonypi events
162 * and input layer indexes in the keymap
163 */
164static int sony_laptop_input_index[] = {
Mattia Dongili3eb87492008-01-14 18:05:45 +0900165 -1, /* 0 no event */
166 -1, /* 1 SONYPI_EVENT_JOGDIAL_DOWN */
167 -1, /* 2 SONYPI_EVENT_JOGDIAL_UP */
168 -1, /* 3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
169 -1, /* 4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */
170 -1, /* 5 SONYPI_EVENT_JOGDIAL_PRESSED */
171 -1, /* 6 SONYPI_EVENT_JOGDIAL_RELEASED */
172 0, /* 7 SONYPI_EVENT_CAPTURE_PRESSED */
173 1, /* 8 SONYPI_EVENT_CAPTURE_RELEASED */
174 2, /* 9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
175 3, /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
176 4, /* 11 SONYPI_EVENT_FNKEY_ESC */
177 5, /* 12 SONYPI_EVENT_FNKEY_F1 */
178 6, /* 13 SONYPI_EVENT_FNKEY_F2 */
179 7, /* 14 SONYPI_EVENT_FNKEY_F3 */
180 8, /* 15 SONYPI_EVENT_FNKEY_F4 */
181 9, /* 16 SONYPI_EVENT_FNKEY_F5 */
182 10, /* 17 SONYPI_EVENT_FNKEY_F6 */
183 11, /* 18 SONYPI_EVENT_FNKEY_F7 */
184 12, /* 19 SONYPI_EVENT_FNKEY_F8 */
185 13, /* 20 SONYPI_EVENT_FNKEY_F9 */
186 14, /* 21 SONYPI_EVENT_FNKEY_F10 */
187 15, /* 22 SONYPI_EVENT_FNKEY_F11 */
188 16, /* 23 SONYPI_EVENT_FNKEY_F12 */
189 17, /* 24 SONYPI_EVENT_FNKEY_1 */
190 18, /* 25 SONYPI_EVENT_FNKEY_2 */
191 19, /* 26 SONYPI_EVENT_FNKEY_D */
192 20, /* 27 SONYPI_EVENT_FNKEY_E */
193 21, /* 28 SONYPI_EVENT_FNKEY_F */
194 22, /* 29 SONYPI_EVENT_FNKEY_S */
195 23, /* 30 SONYPI_EVENT_FNKEY_B */
196 24, /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */
197 25, /* 32 SONYPI_EVENT_PKEY_P1 */
198 26, /* 33 SONYPI_EVENT_PKEY_P2 */
199 27, /* 34 SONYPI_EVENT_PKEY_P3 */
200 28, /* 35 SONYPI_EVENT_BACK_PRESSED */
201 -1, /* 36 SONYPI_EVENT_LID_CLOSED */
202 -1, /* 37 SONYPI_EVENT_LID_OPENED */
203 29, /* 38 SONYPI_EVENT_BLUETOOTH_ON */
204 30, /* 39 SONYPI_EVENT_BLUETOOTH_OFF */
205 31, /* 40 SONYPI_EVENT_HELP_PRESSED */
206 32, /* 41 SONYPI_EVENT_FNKEY_ONLY */
207 33, /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
208 34, /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */
209 35, /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
210 36, /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
211 37, /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
212 38, /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */
213 39, /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
214 40, /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
215 41, /* 50 SONYPI_EVENT_ZOOM_PRESSED */
216 42, /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */
217 43, /* 52 SONYPI_EVENT_MEYE_FACE */
218 44, /* 53 SONYPI_EVENT_MEYE_OPPOSITE */
219 45, /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */
220 46, /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */
221 -1, /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */
222 -1, /* 57 SONYPI_EVENT_BATTERY_INSERT */
223 -1, /* 58 SONYPI_EVENT_BATTERY_REMOVE */
224 -1, /* 59 SONYPI_EVENT_FNKEY_RELEASED */
225 47, /* 60 SONYPI_EVENT_WIRELESS_ON */
226 48, /* 61 SONYPI_EVENT_WIRELESS_OFF */
227 49, /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */
228 50, /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900229 51, /* 64 SONYPI_EVENT_CD_EJECT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900230 52, /* 65 SONYPI_EVENT_MODEKEY_PRESSED */
231 53, /* 66 SONYPI_EVENT_PKEY_P4 */
232 54, /* 67 SONYPI_EVENT_PKEY_P5 */
233 55, /* 68 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900234 56, /* 69 SONYPI_EVENT_VOLUME_INC_PRESSED */
235 57, /* 70 SONYPI_EVENT_VOLUME_DEC_PRESSED */
236 -1, /* 71 SONYPI_EVENT_BRIGHTNESS_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900237 58, /* 72 SONYPI_EVENT_MEDIA_PRESSED */
Mattia Dongilibc57f862007-07-20 02:01:57 +0900238};
239
240static int sony_laptop_input_keycode_map[] = {
241 KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
242 KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
243 KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
244 KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
245 KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
246 KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
247 KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
248 KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
249 KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
250 KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
251 KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
252 KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
253 KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
254 KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
255 KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
256 KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
257 KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
258 KEY_FN_F1, /* 17 SONYPI_EVENT_FNKEY_1 */
259 KEY_FN_F2, /* 18 SONYPI_EVENT_FNKEY_2 */
260 KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
261 KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
262 KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
263 KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
264 KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
265 KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
266 KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
267 KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
268 KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
269 KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
270 KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
271 KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
272 KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
273 KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
274 KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
275 KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
276 KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
277 KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
278 KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
279 KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
280 KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
281 KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
282 KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
283 BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
284 KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
285 KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
286 KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
287 KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
288 KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
289 KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
Mattia Dongili3eb87492008-01-14 18:05:45 +0900290 KEY_ZOOMIN, /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900291 KEY_ZOOMOUT, /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900292 KEY_EJECTCD, /* 51 SONYPI_EVENT_CD_EJECT_PRESSED */
293 KEY_F13, /* 52 SONYPI_EVENT_MODEKEY_PRESSED */
294 KEY_PROG4, /* 53 SONYPI_EVENT_PKEY_P4 */
295 KEY_F14, /* 54 SONYPI_EVENT_PKEY_P5 */
296 KEY_F15, /* 55 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900297 KEY_VOLUMEUP, /* 56 SONYPI_EVENT_VOLUME_INC_PRESSED */
298 KEY_VOLUMEDOWN, /* 57 SONYPI_EVENT_VOLUME_DEC_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900299 KEY_MEDIA, /* 58 SONYPI_EVENT_MEDIA_PRESSED */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200300};
301
302/* release buttons after a short delay if pressed */
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800303static void do_sony_laptop_release_key(unsigned long unused)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200304{
305 struct sony_laptop_keypress kp;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800306 unsigned long flags;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200307
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800308 spin_lock_irqsave(&sony_laptop_input.fifo_lock, flags);
309
310 if (kfifo_out(&sony_laptop_input.fifo,
311 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200312 input_report_key(kp.dev, kp.key, 0);
313 input_sync(kp.dev);
314 }
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800315
316 /* If there is something in the fifo schedule next release. */
317 if (kfifo_len(&sony_laptop_input.fifo) != 0)
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800318 mod_timer(&sony_laptop_input.release_key_timer,
319 jiffies + msecs_to_jiffies(10));
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800320
321 spin_unlock_irqrestore(&sony_laptop_input.fifo_lock, flags);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200322}
malattia@linux.it1549ee62007-04-09 10:19:08 +0200323
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200324/* forward event to the input subsystem */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200325static void sony_laptop_report_input_event(u8 event)
326{
327 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
328 struct input_dev *key_dev = sony_laptop_input.key_dev;
329 struct sony_laptop_keypress kp = { NULL };
malattia@linux.it1549ee62007-04-09 10:19:08 +0200330
Almer S. Tigelaara83021a2009-04-12 11:26:28 +0000331 if (event == SONYPI_EVENT_FNKEY_RELEASED ||
332 event == SONYPI_EVENT_ANYBUTTON_RELEASED) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200333 /* Nothing, not all VAIOs generate this event */
334 return;
335 }
336
337 /* report events */
338 switch (event) {
339 /* jog_dev events */
340 case SONYPI_EVENT_JOGDIAL_UP:
341 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
342 input_report_rel(jog_dev, REL_WHEEL, 1);
343 input_sync(jog_dev);
344 return;
345
346 case SONYPI_EVENT_JOGDIAL_DOWN:
347 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
348 input_report_rel(jog_dev, REL_WHEEL, -1);
349 input_sync(jog_dev);
350 return;
351
352 /* key_dev events */
353 case SONYPI_EVENT_JOGDIAL_PRESSED:
354 kp.key = BTN_MIDDLE;
355 kp.dev = jog_dev;
356 break;
357
358 default:
Adrian Bunkd399d132008-02-20 00:59:03 +0200359 if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
Mattia Dongilibc57f862007-07-20 02:01:57 +0900360 dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
361 break;
362 }
363 if (sony_laptop_input_index[event] != -1) {
364 kp.key = sony_laptop_input_keycode_map[sony_laptop_input_index[event]];
365 if (kp.key != KEY_UNKNOWN)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200366 kp.dev = key_dev;
Mattia Dongilibc57f862007-07-20 02:01:57 +0900367 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200368 break;
369 }
370
371 if (kp.dev) {
372 input_report_key(kp.dev, kp.key, 1);
Mattia Dongilibc57f862007-07-20 02:01:57 +0900373 /* we emit the scancode so we can always remap the key */
374 input_event(kp.dev, EV_MSC, MSC_SCAN, event);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200375 input_sync(kp.dev);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200376
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800377 /* schedule key release */
378 kfifo_in_locked(&sony_laptop_input.fifo,
379 (unsigned char *)&kp, sizeof(kp),
380 &sony_laptop_input.fifo_lock);
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800381 mod_timer(&sony_laptop_input.release_key_timer,
382 jiffies + msecs_to_jiffies(10));
malattia@linux.it1549ee62007-04-09 10:19:08 +0200383 } else
384 dprintk("unknown input event %.2x\n", event);
385}
386
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500387static int sony_laptop_setup_input(struct acpi_device *acpi_device)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200388{
389 struct input_dev *jog_dev;
390 struct input_dev *key_dev;
391 int i;
392 int error;
393
394 /* don't run again if already initialized */
395 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
396 return 0;
397
398 /* kfifo */
399 spin_lock_init(&sony_laptop_input.fifo_lock);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800400 error = kfifo_alloc(&sony_laptop_input.fifo,
401 SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -0800402 if (error) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200403 printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
malattia@linux.it1549ee62007-04-09 10:19:08 +0200404 goto err_dec_users;
405 }
406
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800407 setup_timer(&sony_laptop_input.release_key_timer,
408 do_sony_laptop_release_key, 0);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200409
410 /* input keys */
411 key_dev = input_allocate_device();
412 if (!key_dev) {
413 error = -ENOMEM;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800414 goto err_free_kfifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200415 }
416
417 key_dev->name = "Sony Vaio Keys";
418 key_dev->id.bustype = BUS_ISA;
419 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500420 key_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200421
422 /* Initialize the Input Drivers: special keys */
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800423 input_set_capability(key_dev, EV_MSC, MSC_SCAN);
424
425 __set_bit(EV_KEY, key_dev->evbit);
Mattia Dongilibc57f862007-07-20 02:01:57 +0900426 key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
427 key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
428 key_dev->keycode = &sony_laptop_input_keycode_map;
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800429 for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++)
430 __set_bit(sony_laptop_input_keycode_map[i], key_dev->keybit);
431 __clear_bit(KEY_RESERVED, key_dev->keybit);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200432
433 error = input_register_device(key_dev);
434 if (error)
435 goto err_free_keydev;
436
437 sony_laptop_input.key_dev = key_dev;
438
439 /* jogdial */
440 jog_dev = input_allocate_device();
441 if (!jog_dev) {
442 error = -ENOMEM;
443 goto err_unregister_keydev;
444 }
445
446 jog_dev->name = "Sony Vaio Jogdial";
447 jog_dev->id.bustype = BUS_ISA;
448 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500449 key_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200450
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800451 input_set_capability(jog_dev, EV_KEY, BTN_MIDDLE);
452 input_set_capability(jog_dev, EV_REL, REL_WHEEL);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200453
454 error = input_register_device(jog_dev);
455 if (error)
456 goto err_free_jogdev;
457
458 sony_laptop_input.jog_dev = jog_dev;
459
460 return 0;
461
462err_free_jogdev:
463 input_free_device(jog_dev);
464
465err_unregister_keydev:
466 input_unregister_device(key_dev);
467 /* to avoid kref underflow below at input_free_device */
468 key_dev = NULL;
469
470err_free_keydev:
471 input_free_device(key_dev);
472
malattia@linux.it1549ee62007-04-09 10:19:08 +0200473err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -0800474 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200475
476err_dec_users:
477 atomic_dec(&sony_laptop_input.users);
478 return error;
479}
480
481static void sony_laptop_remove_input(void)
482{
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800483 struct sony_laptop_keypress kp = { NULL };
484
485 /* Cleanup only after the last user has gone */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200486 if (!atomic_dec_and_test(&sony_laptop_input.users))
487 return;
488
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800489 del_timer_sync(&sony_laptop_input.release_key_timer);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800490
491 /*
492 * Generate key-up events for remaining keys. Note that we don't
493 * need locking since nobody is adding new events to the kfifo.
494 */
495 while (kfifo_out(&sony_laptop_input.fifo,
496 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
497 input_report_key(kp.dev, kp.key, 0);
498 input_sync(kp.dev);
499 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200500
501 /* destroy input devs */
502 input_unregister_device(sony_laptop_input.key_dev);
503 sony_laptop_input.key_dev = NULL;
504
505 if (sony_laptop_input.jog_dev) {
506 input_unregister_device(sony_laptop_input.jog_dev);
507 sony_laptop_input.jog_dev = NULL;
508 }
509
Stefani Seibold45465482009-12-21 14:37:26 -0800510 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200511}
512
malattia@linux.it56b87562007-04-09 10:19:05 +0200513/*********** Platform Device ***********/
514
515static atomic_t sony_pf_users = ATOMIC_INIT(0);
516static struct platform_driver sony_pf_driver = {
517 .driver = {
518 .name = "sony-laptop",
519 .owner = THIS_MODULE,
520 }
521};
522static struct platform_device *sony_pf_device;
523
524static int sony_pf_add(void)
525{
526 int ret = 0;
527
528 /* don't run again if already initialized */
529 if (atomic_add_return(1, &sony_pf_users) > 1)
530 return 0;
531
532 ret = platform_driver_register(&sony_pf_driver);
533 if (ret)
534 goto out;
535
536 sony_pf_device = platform_device_alloc("sony-laptop", -1);
537 if (!sony_pf_device) {
538 ret = -ENOMEM;
539 goto out_platform_registered;
540 }
541
542 ret = platform_device_add(sony_pf_device);
543 if (ret)
544 goto out_platform_alloced;
545
546 return 0;
547
548 out_platform_alloced:
549 platform_device_put(sony_pf_device);
550 sony_pf_device = NULL;
551 out_platform_registered:
552 platform_driver_unregister(&sony_pf_driver);
553 out:
554 atomic_dec(&sony_pf_users);
555 return ret;
556}
557
558static void sony_pf_remove(void)
559{
560 /* deregister only after the last user has gone */
561 if (!atomic_dec_and_test(&sony_pf_users))
562 return;
563
564 platform_device_del(sony_pf_device);
565 platform_device_put(sony_pf_device);
566 platform_driver_unregister(&sony_pf_driver);
567}
568
569/*********** SNC (SNY5001) Device ***********/
570
malattia@linux.it33a04452007-04-09 19:26:03 +0200571/* the device uses 1-based values, while the backlight subsystem uses
572 0-based values */
573#define SONY_MAX_BRIGHTNESS 8
574
575#define SNC_VALIDATE_IN 0
576#define SNC_VALIDATE_OUT 1
577
malattia@linux.it59b19102007-04-09 10:19:04 +0200578static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500579 char *);
malattia@linux.it59b19102007-04-09 10:19:04 +0200580static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500581 const char *, size_t);
Mattia Dongili156c2212007-02-12 22:01:07 +0100582static int boolean_validate(const int, const int);
583static int brightness_default_validate(const int, const int);
584
malattia@linux.it59b19102007-04-09 10:19:04 +0200585struct sony_nc_value {
Len Browna02d1c12007-02-07 15:34:02 -0500586 char *name; /* name of the entry */
587 char **acpiget; /* names of the ACPI get function */
588 char **acpiset; /* names of the ACPI set function */
Mattia Dongili156c2212007-02-12 22:01:07 +0100589 int (*validate)(const int, const int); /* input/output validation */
Len Browna02d1c12007-02-07 15:34:02 -0500590 int value; /* current setting */
591 int valid; /* Has ever been set */
592 int debug; /* active only in debug mode ? */
593 struct device_attribute devattr; /* sysfs atribute */
Stelian Pop7f09c432007-01-13 23:04:31 +0100594};
595
malattia@linux.it59b19102007-04-09 10:19:04 +0200596#define SNC_HANDLE_NAMES(_name, _values...) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100597 static char *snc_##_name[] = { _values, NULL }
598
malattia@linux.it59b19102007-04-09 10:19:04 +0200599#define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100600 { \
601 .name = __stringify(_name), \
602 .acpiget = _getters, \
603 .acpiset = _setters, \
Mattia Dongili156c2212007-02-12 22:01:07 +0100604 .validate = _validate, \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100605 .debug = _debug, \
malattia@linux.it59b19102007-04-09 10:19:04 +0200606 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100607 }
608
malattia@linux.it59b19102007-04-09 10:19:04 +0200609#define SNC_HANDLE_NULL { .name = NULL }
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100610
malattia@linux.it59b19102007-04-09 10:19:04 +0200611SNC_HANDLE_NAMES(fnkey_get, "GHKE");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100612
malattia@linux.it59b19102007-04-09 10:19:04 +0200613SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
614SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100615
malattia@linux.it59b19102007-04-09 10:19:04 +0200616SNC_HANDLE_NAMES(cdpower_get, "GCDP");
617SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100618
malattia@linux.it59b19102007-04-09 10:19:04 +0200619SNC_HANDLE_NAMES(audiopower_get, "GAZP");
620SNC_HANDLE_NAMES(audiopower_set, "AZPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100621
malattia@linux.it59b19102007-04-09 10:19:04 +0200622SNC_HANDLE_NAMES(lanpower_get, "GLNP");
623SNC_HANDLE_NAMES(lanpower_set, "LNPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100624
Mattia Dongili044847e2007-07-16 02:34:33 +0900625SNC_HANDLE_NAMES(lidstate_get, "GLID");
626
627SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
628SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
629
630SNC_HANDLE_NAMES(gainbass_get, "GMGB");
631SNC_HANDLE_NAMES(gainbass_set, "CMGB");
632
malattia@linux.it59b19102007-04-09 10:19:04 +0200633SNC_HANDLE_NAMES(PID_get, "GPID");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100634
malattia@linux.it59b19102007-04-09 10:19:04 +0200635SNC_HANDLE_NAMES(CTR_get, "GCTR");
636SNC_HANDLE_NAMES(CTR_set, "SCTR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100637
malattia@linux.it59b19102007-04-09 10:19:04 +0200638SNC_HANDLE_NAMES(PCR_get, "GPCR");
639SNC_HANDLE_NAMES(PCR_set, "SPCR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100640
malattia@linux.it59b19102007-04-09 10:19:04 +0200641SNC_HANDLE_NAMES(CMI_get, "GCMI");
642SNC_HANDLE_NAMES(CMI_set, "SCMI");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100643
malattia@linux.it59b19102007-04-09 10:19:04 +0200644static struct sony_nc_value sony_nc_values[] = {
645 SNC_HANDLE(brightness_default, snc_brightness_def_get,
Mattia Dongili156c2212007-02-12 22:01:07 +0100646 snc_brightness_def_set, brightness_default_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200647 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
648 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
649 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100650 boolean_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200651 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100652 boolean_validate, 1),
Mattia Dongili044847e2007-07-16 02:34:33 +0900653 SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
654 boolean_validate, 0),
655 SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
656 boolean_validate, 0),
657 SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
658 boolean_validate, 0),
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100659 /* unknown methods */
malattia@linux.it59b19102007-04-09 10:19:04 +0200660 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
661 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
662 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
663 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
664 SNC_HANDLE_NULL
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100665};
666
malattia@linux.it59b19102007-04-09 10:19:04 +0200667static acpi_handle sony_nc_acpi_handle;
668static struct acpi_device *sony_nc_acpi_device = NULL;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100669
Mattia Dongilid78865c2007-02-07 20:01:56 +0100670/*
671 * acpi_evaluate_object wrappers
672 */
Stelian Pop7f09c432007-01-13 23:04:31 +0100673static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
674{
675 struct acpi_buffer output;
676 union acpi_object out_obj;
677 acpi_status status;
678
679 output.length = sizeof(out_obj);
680 output.pointer = &out_obj;
681
682 status = acpi_evaluate_object(handle, name, NULL, &output);
683 if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
684 *result = out_obj.integer.value;
685 return 0;
686 }
687
malattia@linux.itf6119b02007-04-09 19:31:06 +0200688 printk(KERN_WARNING DRV_PFX "acpi_callreadfunc failed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100689
690 return -1;
691}
692
693static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
694 int *result)
695{
696 struct acpi_object_list params;
697 union acpi_object in_obj;
698 struct acpi_buffer output;
699 union acpi_object out_obj;
700 acpi_status status;
701
702 params.count = 1;
703 params.pointer = &in_obj;
704 in_obj.type = ACPI_TYPE_INTEGER;
705 in_obj.integer.value = value;
706
707 output.length = sizeof(out_obj);
708 output.pointer = &out_obj;
709
710 status = acpi_evaluate_object(handle, name, &params, &output);
711 if (status == AE_OK) {
712 if (result != NULL) {
713 if (out_obj.type != ACPI_TYPE_INTEGER) {
malattia@linux.itf6119b02007-04-09 19:31:06 +0200714 printk(KERN_WARNING DRV_PFX "acpi_evaluate_object bad "
Stelian Pop7f09c432007-01-13 23:04:31 +0100715 "return type\n");
716 return -1;
717 }
718 *result = out_obj.integer.value;
719 }
720 return 0;
721 }
722
malattia@linux.itf6119b02007-04-09 19:31:06 +0200723 printk(KERN_WARNING DRV_PFX "acpi_evaluate_object failed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100724
725 return -1;
726}
727
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900728static int sony_find_snc_handle(int handle)
729{
730 int i;
731 int result;
732
733 for (i = 0x20; i < 0x30; i++) {
734 acpi_callsetfunc(sony_nc_acpi_handle, "SN00", i, &result);
735 if (result == handle)
736 return i-0x20;
737 }
738
739 return -1;
740}
741
742static int sony_call_snc_handle(int handle, int argument, int *result)
743{
744 int offset = sony_find_snc_handle(handle);
745
746 if (offset < 0)
747 return -1;
748
749 return acpi_callsetfunc(sony_nc_acpi_handle, "SN07", offset | argument,
750 result);
751}
752
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100753/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200754 * sony_nc_values input/output validate functions
Mattia Dongili156c2212007-02-12 22:01:07 +0100755 */
756
757/* brightness_default_validate:
758 *
759 * manipulate input output values to keep consistency with the
760 * backlight framework for which brightness values are 0-based.
761 */
762static int brightness_default_validate(const int direction, const int value)
763{
764 switch (direction) {
765 case SNC_VALIDATE_OUT:
766 return value - 1;
767 case SNC_VALIDATE_IN:
768 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
769 return value + 1;
770 }
771 return -EINVAL;
772}
773
774/* boolean_validate:
775 *
776 * on input validate boolean values 0/1, on output just pass the
777 * received value.
778 */
779static int boolean_validate(const int direction, const int value)
780{
781 if (direction == SNC_VALIDATE_IN) {
782 if (value != 0 && value != 1)
783 return -EINVAL;
784 }
785 return value;
786}
787
788/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200789 * Sysfs show/store common to all sony_nc_values
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100790 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200791static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
Len Browna02d1c12007-02-07 15:34:02 -0500792 char *buffer)
Stelian Pop7f09c432007-01-13 23:04:31 +0100793{
Stelian Pop7f09c432007-01-13 23:04:31 +0100794 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200795 struct sony_nc_value *item =
796 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100797
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100798 if (!*item->acpiget)
Stelian Pop7f09c432007-01-13 23:04:31 +0100799 return -EIO;
800
malattia@linux.it59b19102007-04-09 10:19:04 +0200801 if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100802 return -EIO;
803
Mattia Dongili156c2212007-02-12 22:01:07 +0100804 if (item->validate)
805 value = item->validate(SNC_VALIDATE_OUT, value);
806
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100807 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
Stelian Pop7f09c432007-01-13 23:04:31 +0100808}
809
malattia@linux.it59b19102007-04-09 10:19:04 +0200810static ssize_t sony_nc_sysfs_store(struct device *dev,
Len Browna02d1c12007-02-07 15:34:02 -0500811 struct device_attribute *attr,
812 const char *buffer, size_t count)
Stelian Pop7f09c432007-01-13 23:04:31 +0100813{
Stelian Pop7f09c432007-01-13 23:04:31 +0100814 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200815 struct sony_nc_value *item =
816 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100817
818 if (!item->acpiset)
819 return -EIO;
820
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100821 if (count > 31)
822 return -EINVAL;
823
824 value = simple_strtoul(buffer, NULL, 10);
Stelian Pop7f09c432007-01-13 23:04:31 +0100825
Mattia Dongili156c2212007-02-12 22:01:07 +0100826 if (item->validate)
827 value = item->validate(SNC_VALIDATE_IN, value);
828
829 if (value < 0)
830 return value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100831
malattia@linux.it59b19102007-04-09 10:19:04 +0200832 if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100833 return -EIO;
Andrew Morton3f4f4612007-01-13 23:04:32 +0100834 item->value = value;
835 item->valid = 1;
Stelian Pop7f09c432007-01-13 23:04:31 +0100836 return count;
837}
838
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100839
Mattia Dongilid78865c2007-02-07 20:01:56 +0100840/*
841 * Backlight device
842 */
843static int sony_backlight_update_status(struct backlight_device *bd)
Andrew Morton3f4f4612007-01-13 23:04:32 +0100844{
malattia@linux.it59b19102007-04-09 10:19:04 +0200845 return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
Richard Purdie321709c2007-02-10 15:04:08 +0000846 bd->props.brightness + 1, NULL);
Andrew Morton3f4f4612007-01-13 23:04:32 +0100847}
848
Mattia Dongilid78865c2007-02-07 20:01:56 +0100849static int sony_backlight_get_brightness(struct backlight_device *bd)
850{
851 int value;
852
malattia@linux.it59b19102007-04-09 10:19:04 +0200853 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
Mattia Dongilid78865c2007-02-07 20:01:56 +0100854 return 0;
855 /* brightness levels are 1-based, while backlight ones are 0-based */
856 return value - 1;
857}
858
859static struct backlight_device *sony_backlight_device;
Richard Purdie321709c2007-02-10 15:04:08 +0000860static struct backlight_ops sony_backlight_ops = {
Len Browna02d1c12007-02-07 15:34:02 -0500861 .update_status = sony_backlight_update_status,
862 .get_brightness = sony_backlight_get_brightness,
Mattia Dongilid78865c2007-02-07 20:01:56 +0100863};
864
865/*
Mattia Dongili6315fd12007-07-16 02:34:35 +0900866 * New SNC-only Vaios event mapping to driver known keys
867 */
868struct sony_nc_event {
869 u8 data;
870 u8 event;
871};
872
Matthew Garrett45c79422009-03-26 21:58:16 +0900873static struct sony_nc_event sony_100_events[] = {
Matthew Garrett9b578962009-03-26 21:58:14 +0900874 { 0x90, SONYPI_EVENT_PKEY_P1 },
875 { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthias Welwarsky6479efb2009-04-01 22:10:45 +0900876 { 0x91, SONYPI_EVENT_PKEY_P2 },
Matthew Garrett9b578962009-03-26 21:58:14 +0900877 { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +0900878 { 0x81, SONYPI_EVENT_FNKEY_F1 },
879 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
Anton Veretenenkof5acf5e2009-03-26 22:44:26 +0900880 { 0x82, SONYPI_EVENT_FNKEY_F2 },
881 { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
882 { 0x83, SONYPI_EVENT_FNKEY_F3 },
883 { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
884 { 0x84, SONYPI_EVENT_FNKEY_F4 },
885 { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +0900886 { 0x85, SONYPI_EVENT_FNKEY_F5 },
887 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
888 { 0x86, SONYPI_EVENT_FNKEY_F6 },
889 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
890 { 0x87, SONYPI_EVENT_FNKEY_F7 },
891 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +0900892 { 0x89, SONYPI_EVENT_FNKEY_F9 },
893 { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +0900894 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
895 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
896 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
897 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +0900898 { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
899 { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900900 { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
901 { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +0900902 { 0, 0 },
903};
904
Matthew Garrett45c79422009-03-26 21:58:16 +0900905static struct sony_nc_event sony_127_events[] = {
906 { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
907 { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
908 { 0x82, SONYPI_EVENT_PKEY_P1 },
909 { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
910 { 0x83, SONYPI_EVENT_PKEY_P2 },
911 { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
912 { 0x84, SONYPI_EVENT_PKEY_P3 },
913 { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
914 { 0x85, SONYPI_EVENT_PKEY_P4 },
915 { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
916 { 0x86, SONYPI_EVENT_PKEY_P5 },
917 { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett45c79422009-03-26 21:58:16 +0900918 { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
919 { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
920 { 0, 0 },
921};
922
Mattia Dongili6315fd12007-07-16 02:34:35 +0900923/*
Mattia Dongilid78865c2007-02-07 20:01:56 +0100924 * ACPI callbacks
925 */
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +0000926static void sony_nc_notify(struct acpi_device *device, u32 event)
Stelian Pop7f09c432007-01-13 23:04:31 +0100927{
Mattia Dongili6315fd12007-07-16 02:34:35 +0900928 u32 ev = event;
Mattia Dongili6315fd12007-07-16 02:34:35 +0900929
Matthew Garrett9b578962009-03-26 21:58:14 +0900930 if (ev >= 0x90) {
931 /* New-style event */
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +0900932 int result;
Matthew Garrett45c79422009-03-26 21:58:16 +0900933 int key_handle = 0;
Matthew Garrett9b578962009-03-26 21:58:14 +0900934 ev -= 0x90;
Mattia Dongili6315fd12007-07-16 02:34:35 +0900935
Matthew Garrett45c79422009-03-26 21:58:16 +0900936 if (sony_find_snc_handle(0x100) == ev)
937 key_handle = 0x100;
938 if (sony_find_snc_handle(0x127) == ev)
939 key_handle = 0x127;
Matthew Garrett9b578962009-03-26 21:58:14 +0900940
Matthias Welwarsky6479efb2009-04-01 22:10:45 +0900941 if (key_handle) {
Matthew Garrett45c79422009-03-26 21:58:16 +0900942 struct sony_nc_event *key_event;
943
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +0900944 if (sony_call_snc_handle(key_handle, 0x200, &result)) {
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +0000945 dprintk("sony_nc_notify, unable to decode"
Matthew Garrett45c79422009-03-26 21:58:16 +0900946 " event 0x%.2x 0x%.2x\n", key_handle,
947 ev);
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +0900948 /* restore the original event */
949 ev = event;
950 } else {
Matthew Garrett9b578962009-03-26 21:58:14 +0900951 ev = result & 0xFF;
952
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +0900953 if (key_handle == 0x100)
954 key_event = sony_100_events;
955 else
956 key_event = sony_127_events;
Matthew Garrett45c79422009-03-26 21:58:16 +0900957
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +0900958 for (; key_event->data; key_event++) {
959 if (key_event->data == ev) {
960 ev = key_event->event;
961 break;
962 }
Matthew Garrett9b578962009-03-26 21:58:14 +0900963 }
Mattia Dongili6315fd12007-07-16 02:34:35 +0900964
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +0900965 if (!key_event->data)
966 printk(KERN_INFO DRV_PFX
967 "Unknown event: 0x%x 0x%x\n",
968 key_handle,
969 ev);
970 else
971 sony_laptop_report_input_event(ev);
Matthew Garrett45c79422009-03-26 21:58:16 +0900972 }
Mattia Dongilid5a664a2009-12-17 00:08:35 +0900973 } else if (sony_find_snc_handle(sony_rfkill_handle) == ev) {
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900974 sony_nc_rfkill_update();
975 return;
Matthew Garrett9b578962009-03-26 21:58:14 +0900976 }
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +0900977 } else
978 sony_laptop_report_input_event(ev);
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900979
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +0000980 dprintk("sony_nc_notify, event: 0x%.2x\n", ev);
Len Brown14e04fb2007-08-23 15:20:26 -0400981 acpi_bus_generate_proc_event(sony_nc_acpi_device, 1, ev);
Stelian Pop7f09c432007-01-13 23:04:31 +0100982}
983
984static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
985 void *context, void **return_value)
986{
Lin Ming30823732008-12-16 16:59:35 +0800987 struct acpi_device_info *info;
Stelian Pop7f09c432007-01-13 23:04:31 +0100988
Bob Moore15b8dd52009-06-29 13:39:29 +0800989 if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
Lin Ming30823732008-12-16 16:59:35 +0800990 printk(KERN_WARNING DRV_PFX "method: name: %4.4s, args %X\n",
991 (char *)&info->name, info->param_count);
992
Bob Moore15b8dd52009-06-29 13:39:29 +0800993 kfree(info);
Lin Ming30823732008-12-16 16:59:35 +0800994 }
Stelian Pop7f09c432007-01-13 23:04:31 +0100995
996 return AE_OK;
997}
998
Mattia Dongilid78865c2007-02-07 20:01:56 +0100999/*
1000 * ACPI device
1001 */
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001002static int sony_nc_function_setup(struct acpi_device *device)
1003{
1004 int result;
1005
1006 /* Enable all events */
1007 acpi_callsetfunc(sony_nc_acpi_handle, "SN02", 0xffff, &result);
1008
1009 /* Setup hotkeys */
1010 sony_call_snc_handle(0x0100, 0, &result);
1011 sony_call_snc_handle(0x0101, 0, &result);
1012 sony_call_snc_handle(0x0102, 0x100, &result);
Almer S. Tigelaar560e84a2009-04-12 11:26:27 +00001013 sony_call_snc_handle(0x0127, 0, &result);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001014
1015 return 0;
1016}
1017
malattia@linux.it59b19102007-04-09 10:19:04 +02001018static int sony_nc_resume(struct acpi_device *device)
Mattia Dongilid78865c2007-02-07 20:01:56 +01001019{
malattia@linux.it59b19102007-04-09 10:19:04 +02001020 struct sony_nc_value *item;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001021 acpi_handle handle;
Mattia Dongilid78865c2007-02-07 20:01:56 +01001022
malattia@linux.it59b19102007-04-09 10:19:04 +02001023 for (item = sony_nc_values; item->name; item++) {
Mattia Dongilid78865c2007-02-07 20:01:56 +01001024 int ret;
1025
1026 if (!item->valid)
1027 continue;
malattia@linux.it59b19102007-04-09 10:19:04 +02001028 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
Len Browna02d1c12007-02-07 15:34:02 -05001029 item->value, NULL);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001030 if (ret < 0) {
Harvey Harrison6e574192008-04-29 00:59:20 -07001031 printk("%s: %d\n", __func__, ret);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001032 break;
1033 }
1034 }
Mattia Dongili6315fd12007-07-16 02:34:35 +09001035
Matthew Garrett82734bf2009-03-26 21:58:13 +09001036 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1037 &handle))) {
1038 if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
1039 dprintk("ECON Method failed\n");
1040 }
1041
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001042 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1043 &handle))) {
1044 dprintk("Doing SNC setup\n");
1045 sony_nc_function_setup(device);
1046 }
1047
Mattia Dongilie84a02b2007-08-04 00:22:30 +09001048 /* set the last requested brightness level */
1049 if (sony_backlight_device &&
Mattia Dongilic35d4b32009-04-12 11:26:30 +00001050 sony_backlight_update_status(sony_backlight_device) < 0)
Adam Jackson0ef9cff2008-10-16 15:15:35 -04001051 printk(KERN_WARNING DRV_PFX "unable to restore brightness level\n");
Mattia Dongilie84a02b2007-08-04 00:22:30 +09001052
Alan Jenkinsa0d97d62009-09-25 10:18:21 +01001053 /* re-read rfkill state */
1054 sony_nc_rfkill_update();
1055
Mattia Dongilid78865c2007-02-07 20:01:56 +01001056 return 0;
1057}
1058
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001059static void sony_nc_rfkill_cleanup(void)
1060{
1061 int i;
1062
Johannes Berg19d337d2009-06-02 13:01:37 +02001063 for (i = 0; i < N_SONY_RFKILL; i++) {
1064 if (sony_rfkill_devices[i]) {
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001065 rfkill_unregister(sony_rfkill_devices[i]);
Johannes Berg19d337d2009-06-02 13:01:37 +02001066 rfkill_destroy(sony_rfkill_devices[i]);
1067 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001068 }
1069}
1070
Johannes Berg19d337d2009-06-02 13:01:37 +02001071static int sony_nc_rfkill_set(void *data, bool blocked)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001072{
1073 int result;
1074 int argument = sony_rfkill_address[(long) data] + 0x100;
1075
Johannes Berg19d337d2009-06-02 13:01:37 +02001076 if (!blocked)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001077 argument |= 0xff0000;
1078
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001079 return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001080}
1081
Johannes Berg19d337d2009-06-02 13:01:37 +02001082static const struct rfkill_ops sony_rfkill_ops = {
1083 .set_block = sony_nc_rfkill_set,
1084};
1085
1086static int sony_nc_setup_rfkill(struct acpi_device *device,
1087 enum sony_nc_rfkill nc_type)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001088{
1089 int err = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001090 struct rfkill *rfk;
1091 enum rfkill_type type;
1092 const char *name;
Alan Jenkins50fab072009-09-24 20:15:24 +01001093 int result;
1094 bool hwblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001095
Johannes Berg19d337d2009-06-02 13:01:37 +02001096 switch (nc_type) {
1097 case SONY_WIFI:
1098 type = RFKILL_TYPE_WLAN;
1099 name = "sony-wifi";
1100 break;
1101 case SONY_BLUETOOTH:
1102 type = RFKILL_TYPE_BLUETOOTH;
1103 name = "sony-bluetooth";
1104 break;
1105 case SONY_WWAN:
1106 type = RFKILL_TYPE_WWAN;
1107 name = "sony-wwan";
1108 break;
1109 case SONY_WIMAX:
1110 type = RFKILL_TYPE_WIMAX;
1111 name = "sony-wimax";
1112 break;
1113 default:
1114 return -EINVAL;
Mattia Dongili53005a02009-04-12 11:26:31 +00001115 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001116
Johannes Berg19d337d2009-06-02 13:01:37 +02001117 rfk = rfkill_alloc(name, &device->dev, type,
1118 &sony_rfkill_ops, (void *)nc_type);
1119 if (!rfk)
1120 return -ENOMEM;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001121
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001122 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
Alan Jenkins50fab072009-09-24 20:15:24 +01001123 hwblock = !(result & 0x1);
1124 rfkill_set_hw_state(rfk, hwblock);
1125
Johannes Berg19d337d2009-06-02 13:01:37 +02001126 err = rfkill_register(rfk);
1127 if (err) {
1128 rfkill_destroy(rfk);
1129 return err;
Mattia Dongili53005a02009-04-12 11:26:31 +00001130 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001131 sony_rfkill_devices[nc_type] = rfk;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001132 return err;
1133}
1134
1135static void sony_nc_rfkill_update()
1136{
Johannes Berg19d337d2009-06-02 13:01:37 +02001137 enum sony_nc_rfkill i;
1138 int result;
1139 bool hwblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001140
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001141 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001142 hwblock = !(result & 0x1);
1143
1144 for (i = 0; i < N_SONY_RFKILL; i++) {
1145 int argument = sony_rfkill_address[i];
1146
1147 if (!sony_rfkill_devices[i])
1148 continue;
1149
1150 if (hwblock) {
Johannes Berge1f8a192009-06-11 12:08:15 +02001151 if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
1152 /* we already know we're blocked */
1153 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001154 continue;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001155 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001156
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001157 sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001158 rfkill_set_states(sony_rfkill_devices[i],
1159 !(result & 0xf), false);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001160 }
1161}
1162
Mattia Dongili528809c2009-12-17 00:08:36 +09001163static void sony_nc_rfkill_setup(struct acpi_device *device)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001164{
Mattia Dongili528809c2009-12-17 00:08:36 +09001165 int offset;
1166 u8 dev_code, i;
1167 acpi_status status;
1168 struct acpi_object_list params;
1169 union acpi_object in_obj;
1170 union acpi_object *device_enum;
1171 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001172
Mattia Dongili528809c2009-12-17 00:08:36 +09001173 offset = sony_find_snc_handle(0x124);
1174 if (offset == -1) {
1175 offset = sony_find_snc_handle(0x135);
1176 if (offset == -1)
1177 return;
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001178 else
1179 sony_rfkill_handle = 0x135;
1180 } else
1181 sony_rfkill_handle = 0x124;
Mattia Dongili528809c2009-12-17 00:08:36 +09001182 dprintk("Found rkfill handle: 0x%.4x\n", sony_rfkill_handle);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001183
Mattia Dongili528809c2009-12-17 00:08:36 +09001184 /* need to read the whole buffer returned by the acpi call to SN06
1185 * here otherwise we may miss some features
1186 */
1187 params.count = 1;
1188 params.pointer = &in_obj;
1189 in_obj.type = ACPI_TYPE_INTEGER;
1190 in_obj.integer.value = offset;
1191 status = acpi_evaluate_object(sony_nc_acpi_handle, "SN06", &params,
1192 &buffer);
1193 if (ACPI_FAILURE(status)) {
1194 dprintk("Radio device enumeration failed\n");
1195 return;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001196 }
1197
Mattia Dongili528809c2009-12-17 00:08:36 +09001198 device_enum = (union acpi_object *) buffer.pointer;
1199 if (!device_enum || device_enum->type != ACPI_TYPE_BUFFER) {
1200 printk(KERN_ERR "Invalid SN06 return object 0x%.2x\n",
1201 device_enum->type);
1202 goto out_no_enum;
1203 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001204
Mattia Dongili528809c2009-12-17 00:08:36 +09001205 /* the buffer is filled with magic numbers describing the devices
1206 * available, 0xff terminates the enumeration
1207 */
Dmitry Torokhovc14973f2010-01-10 00:15:44 -08001208 for (i = 0; i < device_enum->buffer.length; i++) {
1209
1210 dev_code = *(device_enum->buffer.pointer + i);
1211 if (dev_code == 0xff)
1212 break;
1213
Mattia Dongili528809c2009-12-17 00:08:36 +09001214 dprintk("Radio devices, looking at 0x%.2x\n", dev_code);
1215
1216 if (dev_code == 0 && !sony_rfkill_devices[SONY_WIFI])
1217 sony_nc_setup_rfkill(device, SONY_WIFI);
1218
1219 if (dev_code == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
1220 sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
1221
1222 if ((0xf0 & dev_code) == 0x20 &&
1223 !sony_rfkill_devices[SONY_WWAN])
1224 sony_nc_setup_rfkill(device, SONY_WWAN);
1225
1226 if (dev_code == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
1227 sony_nc_setup_rfkill(device, SONY_WIMAX);
1228 }
1229
1230out_no_enum:
1231 kfree(buffer.pointer);
1232 return;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001233}
1234
malattia@linux.it59b19102007-04-09 10:19:04 +02001235static int sony_nc_add(struct acpi_device *device)
Stelian Pop7f09c432007-01-13 23:04:31 +01001236{
1237 acpi_status status;
Andrew Morton8607c672007-03-06 02:29:42 -08001238 int result = 0;
Alessandro Guido50f62af2007-01-13 23:04:34 +01001239 acpi_handle handle;
malattia@linux.it56b87562007-04-09 10:19:05 +02001240 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01001241
malattia@linux.itf6119b02007-04-09 19:31:06 +02001242 printk(KERN_INFO DRV_PFX "%s v%s.\n",
1243 SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
1244
malattia@linux.it59b19102007-04-09 10:19:04 +02001245 sony_nc_acpi_device = device;
malattia@linux.it33a04452007-04-09 19:26:03 +02001246 strcpy(acpi_device_class(device), "sony/hotkey");
Stelian Popc5611622007-01-13 23:04:37 +01001247
malattia@linux.it59b19102007-04-09 10:19:04 +02001248 sony_nc_acpi_handle = device->handle;
Stelian Pop7f09c432007-01-13 23:04:31 +01001249
Mattia Dongilib25b7322007-07-16 02:34:36 +09001250 /* read device status */
1251 result = acpi_bus_get_status(device);
1252 /* bail IFF the above call was successful and the device is not present */
1253 if (!result && !device->status.present) {
1254 dprintk("Device not present\n");
1255 result = -ENODEV;
1256 goto outwalk;
1257 }
1258
Stelian Pop7f09c432007-01-13 23:04:31 +01001259 if (debug) {
malattia@linux.it59b19102007-04-09 10:19:04 +02001260 status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_nc_acpi_handle,
Lin Ming22635762009-11-13 10:06:08 +08001261 1, sony_walk_callback, NULL, NULL, NULL);
Stelian Pop7f09c432007-01-13 23:04:31 +01001262 if (ACPI_FAILURE(status)) {
malattia@linux.itf6119b02007-04-09 19:31:06 +02001263 printk(KERN_WARNING DRV_PFX "unable to walk acpi resources\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001264 result = -ENODEV;
1265 goto outwalk;
1266 }
Stelian Popc5611622007-01-13 23:04:37 +01001267 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001268
Matthew Garrett82734bf2009-03-26 21:58:13 +09001269 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1270 &handle))) {
1271 if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
1272 dprintk("ECON Method failed\n");
1273 }
1274
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001275 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1276 &handle))) {
1277 dprintk("Doing SNC setup\n");
1278 sony_nc_function_setup(device);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001279 sony_nc_rfkill_setup(device);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001280 }
1281
malattia@linux.it1549ee62007-04-09 10:19:08 +02001282 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05001283 result = sony_laptop_setup_input(device);
malattia@linux.it1549ee62007-04-09 10:19:08 +02001284 if (result) {
1285 printk(KERN_ERR DRV_PFX
Alessio Igor Boganiff07a532009-04-01 22:10:44 +09001286 "Unable to create input devices.\n");
malattia@linux.it1549ee62007-04-09 10:19:08 +02001287 goto outwalk;
1288 }
1289
Alessandro Guido38cfc142008-11-12 23:03:28 +01001290 if (acpi_video_backlight_support()) {
Alessandro Guido3fedd902008-11-12 23:13:35 +01001291 printk(KERN_INFO DRV_PFX "brightness ignored, must be "
Thomas Renninger540b8bb2008-08-01 17:38:02 +02001292 "controlled by ACPI video driver\n");
1293 } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
1294 &handle))) {
Matthew Garretta19a6ee2010-02-17 16:39:44 -05001295 struct backlight_properties props;
1296 memset(&props, 0, sizeof(struct backlight_properties));
1297 props.max_brightness = SONY_MAX_BRIGHTNESS - 1;
Alessandro Guido50f62af2007-01-13 23:04:34 +01001298 sony_backlight_device = backlight_device_register("sony", NULL,
Len Browna02d1c12007-02-07 15:34:02 -05001299 NULL,
Matthew Garretta19a6ee2010-02-17 16:39:44 -05001300 &sony_backlight_ops,
1301 &props);
Mattia Dongili7df03b82007-01-13 23:04:41 +01001302
Len Browna02d1c12007-02-07 15:34:02 -05001303 if (IS_ERR(sony_backlight_device)) {
malattia@linux.itf6119b02007-04-09 19:31:06 +02001304 printk(KERN_WARNING DRV_PFX "unable to register backlight device\n");
Mattia Dongili7df03b82007-01-13 23:04:41 +01001305 sony_backlight_device = NULL;
Richard Purdie321709c2007-02-10 15:04:08 +00001306 } else {
1307 sony_backlight_device->props.brightness =
Len Browna02d1c12007-02-07 15:34:02 -05001308 sony_backlight_get_brightness
1309 (sony_backlight_device);
Richard Purdie321709c2007-02-10 15:04:08 +00001310 }
1311
Alessandro Guido50f62af2007-01-13 23:04:34 +01001312 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001313
malattia@linux.it49a11de2007-04-09 19:28:56 +02001314 result = sony_pf_add();
1315 if (result)
Mattia Dongilied3aa4b2007-02-07 20:01:54 +01001316 goto outbacklight;
Stelian Pop7f09c432007-01-13 23:04:31 +01001317
malattia@linux.it56b87562007-04-09 10:19:05 +02001318 /* create sony_pf sysfs attributes related to the SNC device */
1319 for (item = sony_nc_values; item->name; ++item) {
1320
1321 if (!debug && item->debug)
1322 continue;
1323
1324 /* find the available acpiget as described in the DSDT */
1325 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
1326 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1327 *item->acpiget,
1328 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02001329 dprintk("Found %s getter: %s\n",
1330 item->name, *item->acpiget);
malattia@linux.it56b87562007-04-09 10:19:05 +02001331 item->devattr.attr.mode |= S_IRUGO;
1332 break;
1333 }
1334 }
1335
1336 /* find the available acpiset as described in the DSDT */
1337 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
1338 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1339 *item->acpiset,
1340 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02001341 dprintk("Found %s setter: %s\n",
1342 item->name, *item->acpiset);
malattia@linux.it56b87562007-04-09 10:19:05 +02001343 item->devattr.attr.mode |= S_IWUSR;
1344 break;
1345 }
1346 }
1347
1348 if (item->devattr.attr.mode != 0) {
1349 result =
1350 device_create_file(&sony_pf_device->dev,
1351 &item->devattr);
1352 if (result)
1353 goto out_sysfs;
1354 }
1355 }
1356
Stelian Pop7f09c432007-01-13 23:04:31 +01001357 return 0;
1358
malattia@linux.it56b87562007-04-09 10:19:05 +02001359 out_sysfs:
1360 for (item = sony_nc_values; item->name; ++item) {
1361 device_remove_file(&sony_pf_device->dev, &item->devattr);
1362 }
1363 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +02001364
Len Browna02d1c12007-02-07 15:34:02 -05001365 outbacklight:
Mattia Dongili7df03b82007-01-13 23:04:41 +01001366 if (sony_backlight_device)
1367 backlight_device_unregister(sony_backlight_device);
1368
malattia@linux.it1549ee62007-04-09 10:19:08 +02001369 sony_laptop_remove_input();
1370
Len Browna02d1c12007-02-07 15:34:02 -05001371 outwalk:
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001372 sony_nc_rfkill_cleanup();
Stelian Pop7f09c432007-01-13 23:04:31 +01001373 return result;
1374}
1375
malattia@linux.it59b19102007-04-09 10:19:04 +02001376static int sony_nc_remove(struct acpi_device *device, int type)
Stelian Pop7f09c432007-01-13 23:04:31 +01001377{
malattia@linux.it56b87562007-04-09 10:19:05 +02001378 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01001379
Alessandro Guido50f62af2007-01-13 23:04:34 +01001380 if (sony_backlight_device)
1381 backlight_device_unregister(sony_backlight_device);
1382
malattia@linux.it59b19102007-04-09 10:19:04 +02001383 sony_nc_acpi_device = NULL;
Stelian Popc5611622007-01-13 23:04:37 +01001384
malattia@linux.it56b87562007-04-09 10:19:05 +02001385 for (item = sony_nc_values; item->name; ++item) {
1386 device_remove_file(&sony_pf_device->dev, &item->devattr);
1387 }
1388
1389 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +02001390 sony_laptop_remove_input();
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001391 sony_nc_rfkill_cleanup();
malattia@linux.itf6119b02007-04-09 19:31:06 +02001392 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001393
1394 return 0;
1395}
1396
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001397static const struct acpi_device_id sony_device_ids[] = {
1398 {SONY_NC_HID, 0},
1399 {SONY_PIC_HID, 0},
1400 {"", 0},
1401};
1402MODULE_DEVICE_TABLE(acpi, sony_device_ids);
1403
1404static const struct acpi_device_id sony_nc_device_ids[] = {
1405 {SONY_NC_HID, 0},
1406 {"", 0},
1407};
1408
malattia@linux.it59b19102007-04-09 10:19:04 +02001409static struct acpi_driver sony_nc_driver = {
1410 .name = SONY_NC_DRIVER_NAME,
1411 .class = SONY_NC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001412 .ids = sony_nc_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02001413 .owner = THIS_MODULE,
Len Browna02d1c12007-02-07 15:34:02 -05001414 .ops = {
malattia@linux.it59b19102007-04-09 10:19:04 +02001415 .add = sony_nc_add,
1416 .remove = sony_nc_remove,
1417 .resume = sony_nc_resume,
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001418 .notify = sony_nc_notify,
Len Browna02d1c12007-02-07 15:34:02 -05001419 },
Andrew Morton3f4f4612007-01-13 23:04:32 +01001420};
1421
malattia@linux.it33a04452007-04-09 19:26:03 +02001422/*********** SPIC (SNY6001) Device ***********/
1423
1424#define SONYPI_DEVICE_TYPE1 0x00000001
1425#define SONYPI_DEVICE_TYPE2 0x00000002
1426#define SONYPI_DEVICE_TYPE3 0x00000004
Mattia Dongili3eb87492008-01-14 18:05:45 +09001427#define SONYPI_DEVICE_TYPE4 0x00000008
malattia@linux.it33a04452007-04-09 19:26:03 +02001428
Mattia Dongili22a17782007-07-16 02:34:39 +09001429#define SONYPI_TYPE1_OFFSET 0x04
1430#define SONYPI_TYPE2_OFFSET 0x12
1431#define SONYPI_TYPE3_OFFSET 0x12
malattia@linux.it33a04452007-04-09 19:26:03 +02001432
malattia@linux.it33a04452007-04-09 19:26:03 +02001433struct sony_pic_ioport {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001434 struct acpi_resource_io io1;
1435 struct acpi_resource_io io2;
malattia@linux.it33a04452007-04-09 19:26:03 +02001436 struct list_head list;
1437};
1438
1439struct sony_pic_irq {
1440 struct acpi_resource_irq irq;
1441 struct list_head list;
1442};
1443
Mattia Dongilide920432008-01-14 18:05:43 +09001444struct sonypi_eventtypes {
1445 u8 data;
1446 unsigned long mask;
1447 struct sonypi_event *events;
1448};
1449
malattia@linux.it33a04452007-04-09 19:26:03 +02001450struct sony_pic_dev {
Mattia Dongili31df7142009-09-16 00:05:29 +09001451 struct acpi_device *acpi_dev;
1452 struct sony_pic_irq *cur_irq;
1453 struct sony_pic_ioport *cur_ioport;
1454 struct list_head interrupts;
1455 struct list_head ioports;
1456 struct mutex lock;
1457 struct sonypi_eventtypes *event_types;
1458 int (*handle_irq)(const u8, const u8);
1459 int model;
1460 u16 evport_offset;
1461 u8 camera_power;
1462 u8 bluetooth_power;
1463 u8 wwan_power;
malattia@linux.it33a04452007-04-09 19:26:03 +02001464};
1465
1466static struct sony_pic_dev spic_dev = {
1467 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
1468 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
1469};
1470
Alan Jenkins5e6f9722009-09-16 00:05:32 +09001471static int spic_drv_registered;
1472
malattia@linux.it33a04452007-04-09 19:26:03 +02001473/* Event masks */
1474#define SONYPI_JOGGER_MASK 0x00000001
1475#define SONYPI_CAPTURE_MASK 0x00000002
1476#define SONYPI_FNKEY_MASK 0x00000004
1477#define SONYPI_BLUETOOTH_MASK 0x00000008
1478#define SONYPI_PKEY_MASK 0x00000010
1479#define SONYPI_BACK_MASK 0x00000020
1480#define SONYPI_HELP_MASK 0x00000040
1481#define SONYPI_LID_MASK 0x00000080
1482#define SONYPI_ZOOM_MASK 0x00000100
1483#define SONYPI_THUMBPHRASE_MASK 0x00000200
1484#define SONYPI_MEYE_MASK 0x00000400
1485#define SONYPI_MEMORYSTICK_MASK 0x00000800
1486#define SONYPI_BATTERY_MASK 0x00001000
1487#define SONYPI_WIRELESS_MASK 0x00002000
1488
1489struct sonypi_event {
1490 u8 data;
1491 u8 event;
1492};
1493
1494/* The set of possible button release events */
1495static struct sonypi_event sonypi_releaseev[] = {
1496 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
1497 { 0, 0 }
1498};
1499
1500/* The set of possible jogger events */
1501static struct sonypi_event sonypi_joggerev[] = {
1502 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
1503 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
1504 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
1505 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
1506 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
1507 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
1508 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
1509 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
1510 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
1511 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
1512 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
1513 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
1514 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
1515 { 0, 0 }
1516};
1517
1518/* The set of possible capture button events */
1519static struct sonypi_event sonypi_captureev[] = {
1520 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
1521 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09001522 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02001523 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
1524 { 0, 0 }
1525};
1526
1527/* The set of possible fnkeys events */
1528static struct sonypi_event sonypi_fnkeyev[] = {
1529 { 0x10, SONYPI_EVENT_FNKEY_ESC },
1530 { 0x11, SONYPI_EVENT_FNKEY_F1 },
1531 { 0x12, SONYPI_EVENT_FNKEY_F2 },
1532 { 0x13, SONYPI_EVENT_FNKEY_F3 },
1533 { 0x14, SONYPI_EVENT_FNKEY_F4 },
1534 { 0x15, SONYPI_EVENT_FNKEY_F5 },
1535 { 0x16, SONYPI_EVENT_FNKEY_F6 },
1536 { 0x17, SONYPI_EVENT_FNKEY_F7 },
1537 { 0x18, SONYPI_EVENT_FNKEY_F8 },
1538 { 0x19, SONYPI_EVENT_FNKEY_F9 },
1539 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
1540 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
1541 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
1542 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
1543 { 0x21, SONYPI_EVENT_FNKEY_1 },
1544 { 0x22, SONYPI_EVENT_FNKEY_2 },
1545 { 0x31, SONYPI_EVENT_FNKEY_D },
1546 { 0x32, SONYPI_EVENT_FNKEY_E },
1547 { 0x33, SONYPI_EVENT_FNKEY_F },
1548 { 0x34, SONYPI_EVENT_FNKEY_S },
1549 { 0x35, SONYPI_EVENT_FNKEY_B },
1550 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
1551 { 0, 0 }
1552};
1553
1554/* The set of possible program key events */
1555static struct sonypi_event sonypi_pkeyev[] = {
1556 { 0x01, SONYPI_EVENT_PKEY_P1 },
1557 { 0x02, SONYPI_EVENT_PKEY_P2 },
1558 { 0x04, SONYPI_EVENT_PKEY_P3 },
Harald Jenny1cae7102009-03-26 21:58:18 +09001559 { 0x20, SONYPI_EVENT_PKEY_P1 },
malattia@linux.it33a04452007-04-09 19:26:03 +02001560 { 0, 0 }
1561};
1562
1563/* The set of possible bluetooth events */
1564static struct sonypi_event sonypi_blueev[] = {
1565 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
1566 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
1567 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
1568 { 0, 0 }
1569};
1570
1571/* The set of possible wireless events */
1572static struct sonypi_event sonypi_wlessev[] = {
1573 { 0x59, SONYPI_EVENT_WIRELESS_ON },
1574 { 0x5a, SONYPI_EVENT_WIRELESS_OFF },
1575 { 0, 0 }
1576};
1577
1578/* The set of possible back button events */
1579static struct sonypi_event sonypi_backev[] = {
1580 { 0x20, SONYPI_EVENT_BACK_PRESSED },
1581 { 0, 0 }
1582};
1583
1584/* The set of possible help button events */
1585static struct sonypi_event sonypi_helpev[] = {
1586 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
1587 { 0, 0 }
1588};
1589
1590
1591/* The set of possible lid events */
1592static struct sonypi_event sonypi_lidev[] = {
1593 { 0x51, SONYPI_EVENT_LID_CLOSED },
1594 { 0x50, SONYPI_EVENT_LID_OPENED },
1595 { 0, 0 }
1596};
1597
1598/* The set of possible zoom events */
1599static struct sonypi_event sonypi_zoomev[] = {
1600 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09001601 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
1602 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
Harald Jenny1cae7102009-03-26 21:58:18 +09001603 { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02001604 { 0, 0 }
1605};
1606
1607/* The set of possible thumbphrase events */
1608static struct sonypi_event sonypi_thumbphraseev[] = {
1609 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
1610 { 0, 0 }
1611};
1612
1613/* The set of possible motioneye camera events */
1614static struct sonypi_event sonypi_meyeev[] = {
1615 { 0x00, SONYPI_EVENT_MEYE_FACE },
1616 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
1617 { 0, 0 }
1618};
1619
1620/* The set of possible memorystick events */
1621static struct sonypi_event sonypi_memorystickev[] = {
1622 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
1623 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
1624 { 0, 0 }
1625};
1626
1627/* The set of possible battery events */
1628static struct sonypi_event sonypi_batteryev[] = {
1629 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
1630 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
1631 { 0, 0 }
1632};
1633
Harald Jenny1cae7102009-03-26 21:58:18 +09001634/* The set of possible volume events */
1635static struct sonypi_event sonypi_volumeev[] = {
1636 { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
1637 { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
1638 { 0, 0 }
1639};
1640
1641/* The set of possible brightness events */
1642static struct sonypi_event sonypi_brightnessev[] = {
1643 { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
1644 { 0, 0 }
1645};
1646
Mattia Dongilide920432008-01-14 18:05:43 +09001647static struct sonypi_eventtypes type1_events[] = {
1648 { 0, 0xffffffff, sonypi_releaseev },
1649 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
1650 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
1651 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
1652 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
1653 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1654 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1655 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
1656 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1657 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
1658 { 0 },
1659};
1660static struct sonypi_eventtypes type2_events[] = {
1661 { 0, 0xffffffff, sonypi_releaseev },
1662 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
1663 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
1664 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
1665 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1666 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1667 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
1668 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
1669 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
1670 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
1671 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
1672 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1673 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1674 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1675 { 0 },
1676};
1677static struct sonypi_eventtypes type3_events[] = {
1678 { 0, 0xffffffff, sonypi_releaseev },
1679 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1680 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
1681 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1682 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1683 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09001684 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
1685 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
1686 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
Harald Jenny1cae7102009-03-26 21:58:18 +09001687 { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
1688 { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09001689 { 0 },
Mattia Dongilide920432008-01-14 18:05:43 +09001690};
1691
Mattia Dongili3eb87492008-01-14 18:05:45 +09001692/* low level spic calls */
malattia@linux.it33a04452007-04-09 19:26:03 +02001693#define ITERATIONS_LONG 10000
1694#define ITERATIONS_SHORT 10
1695#define wait_on_command(command, iterations) { \
1696 unsigned int n = iterations; \
1697 while (--n && (command)) \
1698 udelay(1); \
1699 if (!n) \
1700 dprintk("command failed at %s : %s (line %d)\n", \
Harvey Harrison6e574192008-04-29 00:59:20 -07001701 __FILE__, __func__, __LINE__); \
malattia@linux.it33a04452007-04-09 19:26:03 +02001702}
1703
1704static u8 sony_pic_call1(u8 dev)
1705{
1706 u8 v1, v2;
1707
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001708 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02001709 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001710 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1711 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
1712 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09001713 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02001714 return v2;
1715}
1716
1717static u8 sony_pic_call2(u8 dev, u8 fn)
1718{
1719 u8 v1;
1720
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001721 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02001722 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001723 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1724 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02001725 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001726 outb(fn, spic_dev.cur_ioport->io1.minimum);
1727 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09001728 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02001729 return v1;
1730}
1731
malattia@linux.it49a11de2007-04-09 19:28:56 +02001732static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
1733{
1734 u8 v1;
1735
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001736 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1737 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1738 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1739 outb(fn, spic_dev.cur_ioport->io1.minimum);
1740 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1741 outb(v, spic_dev.cur_ioport->io1.minimum);
1742 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09001743 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
1744 dev, fn, v, v1);
malattia@linux.it49a11de2007-04-09 19:28:56 +02001745 return v1;
1746}
1747
Mattia Dongili3eb87492008-01-14 18:05:45 +09001748/*
1749 * minidrivers for SPIC models
1750 */
Mattia Dongilie93c8a62009-03-26 21:58:17 +09001751static int type3_handle_irq(const u8 data_mask, const u8 ev)
Mattia Dongili3eb87492008-01-14 18:05:45 +09001752{
1753 /*
1754 * 0x31 could mean we have to take some extra action and wait for
Mattia Dongilie93c8a62009-03-26 21:58:17 +09001755 * the next irq for some Type3 models, it will generate a new
Mattia Dongili3eb87492008-01-14 18:05:45 +09001756 * irq and we can read new data from the device:
1757 * - 0x5c and 0x5f requires 0xA0
1758 * - 0x61 requires 0xB3
1759 */
1760 if (data_mask == 0x31) {
1761 if (ev == 0x5c || ev == 0x5f)
1762 sony_pic_call1(0xA0);
1763 else if (ev == 0x61)
1764 sony_pic_call1(0xB3);
1765 return 0;
1766 }
1767 return 1;
1768}
1769
Mattia Dongili3eb87492008-01-14 18:05:45 +09001770static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
1771{
1772 struct pci_dev *pcidev;
1773
1774 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1775 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
1776 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09001777 dev->model = SONYPI_DEVICE_TYPE1;
1778 dev->evport_offset = SONYPI_TYPE1_OFFSET;
1779 dev->event_types = type1_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09001780 goto out;
1781 }
1782
1783 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1784 PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
1785 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09001786 dev->model = SONYPI_DEVICE_TYPE2;
1787 dev->evport_offset = SONYPI_TYPE2_OFFSET;
1788 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09001789 goto out;
1790 }
1791
1792 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1793 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
1794 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09001795 dev->model = SONYPI_DEVICE_TYPE3;
1796 dev->handle_irq = type3_handle_irq;
1797 dev->evport_offset = SONYPI_TYPE3_OFFSET;
1798 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09001799 goto out;
1800 }
1801
1802 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1803 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
1804 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09001805 dev->model = SONYPI_DEVICE_TYPE3;
1806 dev->handle_irq = type3_handle_irq;
1807 dev->evport_offset = SONYPI_TYPE3_OFFSET;
1808 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09001809 goto out;
1810 }
1811
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09001812 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1813 PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
1814 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09001815 dev->model = SONYPI_DEVICE_TYPE3;
1816 dev->handle_irq = type3_handle_irq;
1817 dev->evport_offset = SONYPI_TYPE3_OFFSET;
1818 dev->event_types = type3_events;
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09001819 goto out;
1820 }
1821
Mattia Dongili3eb87492008-01-14 18:05:45 +09001822 /* default */
Mattia Dongili31df7142009-09-16 00:05:29 +09001823 dev->model = SONYPI_DEVICE_TYPE2;
1824 dev->evport_offset = SONYPI_TYPE2_OFFSET;
1825 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09001826
1827out:
1828 if (pcidev)
1829 pci_dev_put(pcidev);
1830
1831 printk(KERN_INFO DRV_PFX "detected Type%d model\n",
Mattia Dongili31df7142009-09-16 00:05:29 +09001832 dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
1833 dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
Mattia Dongili3eb87492008-01-14 18:05:45 +09001834}
1835
malattia@linux.it49a11de2007-04-09 19:28:56 +02001836/* camera tests and poweron/poweroff */
1837#define SONYPI_CAMERA_PICTURE 5
malattia@linux.it49a11de2007-04-09 19:28:56 +02001838#define SONYPI_CAMERA_CONTROL 0x10
malattia@linux.ite3646322007-04-28 23:34:22 +09001839
1840#define SONYPI_CAMERA_BRIGHTNESS 0
1841#define SONYPI_CAMERA_CONTRAST 1
1842#define SONYPI_CAMERA_HUE 2
1843#define SONYPI_CAMERA_COLOR 3
1844#define SONYPI_CAMERA_SHARPNESS 4
1845
1846#define SONYPI_CAMERA_EXPOSURE_MASK 0xC
1847#define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
1848#define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
1849#define SONYPI_CAMERA_MUTE_MASK 0x40
1850
1851/* the rest don't need a loop until not 0xff */
1852#define SONYPI_CAMERA_AGC 6
1853#define SONYPI_CAMERA_AGC_MASK 0x30
1854#define SONYPI_CAMERA_SHUTTER_MASK 0x7
1855
1856#define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
1857#define SONYPI_CAMERA_CONTROL 0x10
1858
1859#define SONYPI_CAMERA_STATUS 7
1860#define SONYPI_CAMERA_STATUS_READY 0x2
1861#define SONYPI_CAMERA_STATUS_POSITION 0x4
1862
1863#define SONYPI_DIRECTION_BACKWARDS 0x4
1864
1865#define SONYPI_CAMERA_REVISION 8
1866#define SONYPI_CAMERA_ROMVERSION 9
malattia@linux.it49a11de2007-04-09 19:28:56 +02001867
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001868static int __sony_pic_camera_ready(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001869{
1870 u8 v;
1871
1872 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
1873 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
1874}
1875
malattia@linux.ite3646322007-04-28 23:34:22 +09001876static int __sony_pic_camera_off(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001877{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001878 if (!camera) {
1879 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1880 return -ENODEV;
1881 }
1882
malattia@linux.it49a11de2007-04-09 19:28:56 +02001883 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
1884 SONYPI_CAMERA_MUTE_MASK),
1885 ITERATIONS_SHORT);
1886
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001887 if (spic_dev.camera_power) {
1888 sony_pic_call2(0x91, 0);
1889 spic_dev.camera_power = 0;
1890 }
1891 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001892}
1893
malattia@linux.ite3646322007-04-28 23:34:22 +09001894static int __sony_pic_camera_on(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001895{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001896 int i, j, x;
1897
1898 if (!camera) {
1899 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1900 return -ENODEV;
1901 }
malattia@linux.it49a11de2007-04-09 19:28:56 +02001902
1903 if (spic_dev.camera_power)
malattia@linux.ite3646322007-04-28 23:34:22 +09001904 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001905
1906 for (j = 5; j > 0; j--) {
1907
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001908 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001909 msleep(10);
1910 sony_pic_call1(0x93);
1911
1912 for (i = 400; i > 0; i--) {
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001913 if (__sony_pic_camera_ready())
malattia@linux.it49a11de2007-04-09 19:28:56 +02001914 break;
1915 msleep(10);
1916 }
1917 if (i)
1918 break;
1919 }
1920
1921 if (j == 0) {
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001922 printk(KERN_WARNING DRV_PFX "failed to power on camera\n");
malattia@linux.ite3646322007-04-28 23:34:22 +09001923 return -ENODEV;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001924 }
1925
1926 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
1927 0x5a),
1928 ITERATIONS_SHORT);
1929
1930 spic_dev.camera_power = 1;
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001931 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001932}
1933
malattia@linux.ite3646322007-04-28 23:34:22 +09001934/* External camera command (exported to the motion eye v4l driver) */
1935int sony_pic_camera_command(int command, u8 value)
1936{
1937 if (!camera)
1938 return -EIO;
1939
1940 mutex_lock(&spic_dev.lock);
1941
1942 switch (command) {
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001943 case SONY_PIC_COMMAND_SETCAMERA:
malattia@linux.ite3646322007-04-28 23:34:22 +09001944 if (value)
1945 __sony_pic_camera_on();
1946 else
1947 __sony_pic_camera_off();
1948 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001949 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09001950 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
1951 ITERATIONS_SHORT);
1952 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001953 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
malattia@linux.ite3646322007-04-28 23:34:22 +09001954 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
1955 ITERATIONS_SHORT);
1956 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001957 case SONY_PIC_COMMAND_SETCAMERAHUE:
malattia@linux.ite3646322007-04-28 23:34:22 +09001958 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
1959 ITERATIONS_SHORT);
1960 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001961 case SONY_PIC_COMMAND_SETCAMERACOLOR:
malattia@linux.ite3646322007-04-28 23:34:22 +09001962 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
1963 ITERATIONS_SHORT);
1964 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001965 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09001966 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
1967 ITERATIONS_SHORT);
1968 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001969 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
malattia@linux.ite3646322007-04-28 23:34:22 +09001970 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
1971 ITERATIONS_SHORT);
1972 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001973 case SONY_PIC_COMMAND_SETCAMERAAGC:
malattia@linux.ite3646322007-04-28 23:34:22 +09001974 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
1975 ITERATIONS_SHORT);
1976 break;
1977 default:
1978 printk(KERN_ERR DRV_PFX "sony_pic_camera_command invalid: %d\n",
1979 command);
1980 break;
1981 }
1982 mutex_unlock(&spic_dev.lock);
1983 return 0;
1984}
1985EXPORT_SYMBOL(sony_pic_camera_command);
1986
malattia@linux.it9476cdf2007-04-28 23:21:42 +09001987/* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09001988static void __sony_pic_set_wwanpower(u8 state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09001989{
1990 state = !!state;
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09001991 if (spic_dev.wwan_power == state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09001992 return;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09001993 sony_pic_call2(0xB0, state);
Sergey Yanovich3ad1b762009-03-26 21:58:21 +09001994 sony_pic_call1(0x82);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09001995 spic_dev.wwan_power = state;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09001996}
1997
1998static ssize_t sony_pic_wwanpower_store(struct device *dev,
1999 struct device_attribute *attr,
2000 const char *buffer, size_t count)
2001{
2002 unsigned long value;
2003 if (count > 31)
2004 return -EINVAL;
2005
2006 value = simple_strtoul(buffer, NULL, 10);
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002007 mutex_lock(&spic_dev.lock);
2008 __sony_pic_set_wwanpower(value);
2009 mutex_unlock(&spic_dev.lock);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002010
2011 return count;
2012}
2013
2014static ssize_t sony_pic_wwanpower_show(struct device *dev,
2015 struct device_attribute *attr, char *buffer)
2016{
2017 ssize_t count;
2018 mutex_lock(&spic_dev.lock);
2019 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
2020 mutex_unlock(&spic_dev.lock);
2021 return count;
2022}
2023
malattia@linux.it49a11de2007-04-09 19:28:56 +02002024/* bluetooth subsystem power state */
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002025static void __sony_pic_set_bluetoothpower(u8 state)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002026{
2027 state = !!state;
2028 if (spic_dev.bluetooth_power == state)
2029 return;
2030 sony_pic_call2(0x96, state);
2031 sony_pic_call1(0x82);
2032 spic_dev.bluetooth_power = state;
2033}
2034
2035static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
2036 struct device_attribute *attr,
2037 const char *buffer, size_t count)
2038{
2039 unsigned long value;
2040 if (count > 31)
2041 return -EINVAL;
2042
2043 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002044 mutex_lock(&spic_dev.lock);
2045 __sony_pic_set_bluetoothpower(value);
2046 mutex_unlock(&spic_dev.lock);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002047
2048 return count;
2049}
2050
2051static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
2052 struct device_attribute *attr, char *buffer)
2053{
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002054 ssize_t count = 0;
2055 mutex_lock(&spic_dev.lock);
2056 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
2057 mutex_unlock(&spic_dev.lock);
2058 return count;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002059}
2060
2061/* fan speed */
2062/* FAN0 information (reverse engineered from ACPI tables) */
2063#define SONY_PIC_FAN0_STATUS 0x93
malattia@linux.it7b153f32007-04-09 19:31:25 +02002064static int sony_pic_set_fanspeed(unsigned long value)
2065{
2066 return ec_write(SONY_PIC_FAN0_STATUS, value);
2067}
2068
2069static int sony_pic_get_fanspeed(u8 *value)
2070{
2071 return ec_read(SONY_PIC_FAN0_STATUS, value);
2072}
2073
malattia@linux.it49a11de2007-04-09 19:28:56 +02002074static ssize_t sony_pic_fanspeed_store(struct device *dev,
2075 struct device_attribute *attr,
2076 const char *buffer, size_t count)
2077{
2078 unsigned long value;
2079 if (count > 31)
2080 return -EINVAL;
2081
2082 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002083 if (sony_pic_set_fanspeed(value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02002084 return -EIO;
2085
2086 return count;
2087}
2088
2089static ssize_t sony_pic_fanspeed_show(struct device *dev,
2090 struct device_attribute *attr, char *buffer)
2091{
2092 u8 value = 0;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002093 if (sony_pic_get_fanspeed(&value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02002094 return -EIO;
2095
2096 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
2097}
2098
2099#define SPIC_ATTR(_name, _mode) \
2100struct device_attribute spic_attr_##_name = __ATTR(_name, \
2101 _mode, sony_pic_## _name ##_show, \
2102 sony_pic_## _name ##_store)
2103
malattia@linux.it49a11de2007-04-09 19:28:56 +02002104static SPIC_ATTR(bluetoothpower, 0644);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002105static SPIC_ATTR(wwanpower, 0644);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002106static SPIC_ATTR(fanspeed, 0644);
2107
2108static struct attribute *spic_attributes[] = {
malattia@linux.it49a11de2007-04-09 19:28:56 +02002109 &spic_attr_bluetoothpower.attr,
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002110 &spic_attr_wwanpower.attr,
malattia@linux.it49a11de2007-04-09 19:28:56 +02002111 &spic_attr_fanspeed.attr,
2112 NULL
2113};
2114
2115static struct attribute_group spic_attribute_group = {
2116 .attrs = spic_attributes
2117};
2118
malattia@linux.it7b153f32007-04-09 19:31:25 +02002119/******** SONYPI compatibility **********/
Mattia Dongilia64e62a2007-05-01 11:19:53 +09002120#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +02002121
2122/* battery / brightness / temperature addresses */
2123#define SONYPI_BAT_FLAGS 0x81
2124#define SONYPI_LCD_LIGHT 0x96
2125#define SONYPI_BAT1_PCTRM 0xa0
2126#define SONYPI_BAT1_LEFT 0xa2
2127#define SONYPI_BAT1_MAXRT 0xa4
2128#define SONYPI_BAT2_PCTRM 0xa8
2129#define SONYPI_BAT2_LEFT 0xaa
2130#define SONYPI_BAT2_MAXRT 0xac
2131#define SONYPI_BAT1_MAXTK 0xb0
2132#define SONYPI_BAT1_FULL 0xb2
2133#define SONYPI_BAT2_MAXTK 0xb8
2134#define SONYPI_BAT2_FULL 0xba
2135#define SONYPI_TEMP_STATUS 0xC1
2136
2137struct sonypi_compat_s {
2138 struct fasync_struct *fifo_async;
Stefani Seibold45465482009-12-21 14:37:26 -08002139 struct kfifo fifo;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002140 spinlock_t fifo_lock;
2141 wait_queue_head_t fifo_proc_list;
2142 atomic_t open_count;
2143};
2144static struct sonypi_compat_s sonypi_compat = {
2145 .open_count = ATOMIC_INIT(0),
2146};
2147
2148static int sonypi_misc_fasync(int fd, struct file *filp, int on)
2149{
Jonathan Corbet60aa4922009-02-01 14:52:56 -07002150 return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002151}
2152
2153static int sonypi_misc_release(struct inode *inode, struct file *file)
2154{
malattia@linux.it7b153f32007-04-09 19:31:25 +02002155 atomic_dec(&sonypi_compat.open_count);
2156 return 0;
2157}
2158
2159static int sonypi_misc_open(struct inode *inode, struct file *file)
2160{
2161 /* Flush input queue on first open */
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002162 unsigned long flags;
2163
Stefani Seibold45465482009-12-21 14:37:26 -08002164 spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002165
malattia@linux.it7b153f32007-04-09 19:31:25 +02002166 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
Stefani Seibolde64c0262009-12-21 14:37:28 -08002167 kfifo_reset(&sonypi_compat.fifo);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002168
Stefani Seibold45465482009-12-21 14:37:26 -08002169 spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002170
malattia@linux.it7b153f32007-04-09 19:31:25 +02002171 return 0;
2172}
2173
2174static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
2175 size_t count, loff_t *pos)
2176{
2177 ssize_t ret;
2178 unsigned char c;
2179
Stefani Seibold45465482009-12-21 14:37:26 -08002180 if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
malattia@linux.it7b153f32007-04-09 19:31:25 +02002181 (file->f_flags & O_NONBLOCK))
2182 return -EAGAIN;
2183
2184 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
Stefani Seibold45465482009-12-21 14:37:26 -08002185 kfifo_len(&sonypi_compat.fifo) != 0);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002186 if (ret)
2187 return ret;
2188
2189 while (ret < count &&
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002190 (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002191 &sonypi_compat.fifo_lock) == sizeof(c))) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02002192 if (put_user(c, buf++))
2193 return -EFAULT;
2194 ret++;
2195 }
2196
2197 if (ret > 0) {
2198 struct inode *inode = file->f_path.dentry->d_inode;
2199 inode->i_atime = current_fs_time(inode->i_sb);
2200 }
2201
2202 return ret;
2203}
2204
2205static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
2206{
2207 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
Stefani Seibold45465482009-12-21 14:37:26 -08002208 if (kfifo_len(&sonypi_compat.fifo))
malattia@linux.it7b153f32007-04-09 19:31:25 +02002209 return POLLIN | POLLRDNORM;
2210 return 0;
2211}
2212
2213static int ec_read16(u8 addr, u16 *value)
2214{
2215 u8 val_lb, val_hb;
2216 if (ec_read(addr, &val_lb))
2217 return -1;
2218 if (ec_read(addr + 1, &val_hb))
2219 return -1;
2220 *value = val_lb | (val_hb << 8);
2221 return 0;
2222}
2223
Alan Cox2b24ef02009-03-26 21:58:19 +09002224static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
2225 unsigned long arg)
malattia@linux.it7b153f32007-04-09 19:31:25 +02002226{
2227 int ret = 0;
2228 void __user *argp = (void __user *)arg;
2229 u8 val8;
2230 u16 val16;
2231 int value;
2232
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002233 mutex_lock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002234 switch (cmd) {
2235 case SONYPI_IOCGBRT:
2236 if (sony_backlight_device == NULL) {
2237 ret = -EIO;
2238 break;
2239 }
2240 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
2241 ret = -EIO;
2242 break;
2243 }
2244 val8 = ((value & 0xff) - 1) << 5;
2245 if (copy_to_user(argp, &val8, sizeof(val8)))
2246 ret = -EFAULT;
2247 break;
2248 case SONYPI_IOCSBRT:
2249 if (sony_backlight_device == NULL) {
2250 ret = -EIO;
2251 break;
2252 }
2253 if (copy_from_user(&val8, argp, sizeof(val8))) {
2254 ret = -EFAULT;
2255 break;
2256 }
2257 if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
2258 (val8 >> 5) + 1, NULL)) {
2259 ret = -EIO;
2260 break;
2261 }
2262 /* sync the backlight device status */
2263 sony_backlight_device->props.brightness =
2264 sony_backlight_get_brightness(sony_backlight_device);
2265 break;
2266 case SONYPI_IOCGBAT1CAP:
2267 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
2268 ret = -EIO;
2269 break;
2270 }
2271 if (copy_to_user(argp, &val16, sizeof(val16)))
2272 ret = -EFAULT;
2273 break;
2274 case SONYPI_IOCGBAT1REM:
2275 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
2276 ret = -EIO;
2277 break;
2278 }
2279 if (copy_to_user(argp, &val16, sizeof(val16)))
2280 ret = -EFAULT;
2281 break;
2282 case SONYPI_IOCGBAT2CAP:
2283 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
2284 ret = -EIO;
2285 break;
2286 }
2287 if (copy_to_user(argp, &val16, sizeof(val16)))
2288 ret = -EFAULT;
2289 break;
2290 case SONYPI_IOCGBAT2REM:
2291 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
2292 ret = -EIO;
2293 break;
2294 }
2295 if (copy_to_user(argp, &val16, sizeof(val16)))
2296 ret = -EFAULT;
2297 break;
2298 case SONYPI_IOCGBATFLAGS:
2299 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
2300 ret = -EIO;
2301 break;
2302 }
2303 val8 &= 0x07;
2304 if (copy_to_user(argp, &val8, sizeof(val8)))
2305 ret = -EFAULT;
2306 break;
2307 case SONYPI_IOCGBLUE:
2308 val8 = spic_dev.bluetooth_power;
2309 if (copy_to_user(argp, &val8, sizeof(val8)))
2310 ret = -EFAULT;
2311 break;
2312 case SONYPI_IOCSBLUE:
2313 if (copy_from_user(&val8, argp, sizeof(val8))) {
2314 ret = -EFAULT;
2315 break;
2316 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002317 __sony_pic_set_bluetoothpower(val8);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002318 break;
2319 /* FAN Controls */
2320 case SONYPI_IOCGFAN:
2321 if (sony_pic_get_fanspeed(&val8)) {
2322 ret = -EIO;
2323 break;
2324 }
2325 if (copy_to_user(argp, &val8, sizeof(val8)))
2326 ret = -EFAULT;
2327 break;
2328 case SONYPI_IOCSFAN:
2329 if (copy_from_user(&val8, argp, sizeof(val8))) {
2330 ret = -EFAULT;
2331 break;
2332 }
2333 if (sony_pic_set_fanspeed(val8))
2334 ret = -EIO;
2335 break;
2336 /* GET Temperature (useful under APM) */
2337 case SONYPI_IOCGTEMP:
2338 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
2339 ret = -EIO;
2340 break;
2341 }
2342 if (copy_to_user(argp, &val8, sizeof(val8)))
2343 ret = -EFAULT;
2344 break;
2345 default:
2346 ret = -EINVAL;
2347 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002348 mutex_unlock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002349 return ret;
2350}
2351
2352static const struct file_operations sonypi_misc_fops = {
2353 .owner = THIS_MODULE,
2354 .read = sonypi_misc_read,
2355 .poll = sonypi_misc_poll,
2356 .open = sonypi_misc_open,
2357 .release = sonypi_misc_release,
2358 .fasync = sonypi_misc_fasync,
Alan Cox2b24ef02009-03-26 21:58:19 +09002359 .unlocked_ioctl = sonypi_misc_ioctl,
malattia@linux.it7b153f32007-04-09 19:31:25 +02002360};
2361
2362static struct miscdevice sonypi_misc_device = {
2363 .minor = MISC_DYNAMIC_MINOR,
2364 .name = "sonypi",
2365 .fops = &sonypi_misc_fops,
2366};
2367
2368static void sonypi_compat_report_event(u8 event)
2369{
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002370 kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002371 sizeof(event), &sonypi_compat.fifo_lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002372 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
2373 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
2374}
2375
2376static int sonypi_compat_init(void)
2377{
2378 int error;
2379
2380 spin_lock_init(&sonypi_compat.fifo_lock);
Stefani Seibold45465482009-12-21 14:37:26 -08002381 error =
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002382 kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -08002383 if (error) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02002384 printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
Stefani Seibold45465482009-12-21 14:37:26 -08002385 return error;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002386 }
2387
2388 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002389
2390 if (minor != -1)
2391 sonypi_misc_device.minor = minor;
2392 error = misc_register(&sonypi_misc_device);
2393 if (error) {
2394 printk(KERN_ERR DRV_PFX "misc_register failed\n");
2395 goto err_free_kfifo;
2396 }
2397 if (minor == -1)
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002398 printk(KERN_INFO DRV_PFX "device allocated minor is %d\n",
malattia@linux.it7b153f32007-04-09 19:31:25 +02002399 sonypi_misc_device.minor);
2400
2401 return 0;
2402
2403err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -08002404 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002405 return error;
2406}
2407
2408static void sonypi_compat_exit(void)
2409{
2410 misc_deregister(&sonypi_misc_device);
Stefani Seibold45465482009-12-21 14:37:26 -08002411 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002412}
2413#else
2414static int sonypi_compat_init(void) { return 0; }
2415static void sonypi_compat_exit(void) { }
2416static void sonypi_compat_report_event(u8 event) { }
Mattia Dongilia64e62a2007-05-01 11:19:53 +09002417#endif /* CONFIG_SONYPI_COMPAT */
malattia@linux.it7b153f32007-04-09 19:31:25 +02002418
malattia@linux.it1549ee62007-04-09 10:19:08 +02002419/*
malattia@linux.it33a04452007-04-09 19:26:03 +02002420 * ACPI callbacks
malattia@linux.it1549ee62007-04-09 10:19:08 +02002421 */
malattia@linux.it33a04452007-04-09 19:26:03 +02002422static acpi_status
2423sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
2424{
2425 u32 i;
2426 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
2427
2428 switch (resource->type) {
2429 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002430 {
2431 /* start IO enumeration */
2432 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
2433 if (!ioport)
2434 return AE_ERROR;
2435
2436 list_add(&ioport->list, &dev->ioports);
2437 return AE_OK;
2438 }
2439
malattia@linux.it33a04452007-04-09 19:26:03 +02002440 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002441 /* end IO enumeration */
malattia@linux.it33a04452007-04-09 19:26:03 +02002442 return AE_OK;
2443
2444 case ACPI_RESOURCE_TYPE_IRQ:
2445 {
2446 struct acpi_resource_irq *p = &resource->data.irq;
2447 struct sony_pic_irq *interrupt = NULL;
2448 if (!p || !p->interrupt_count) {
2449 /*
2450 * IRQ descriptors may have no IRQ# bits set,
2451 * particularly those those w/ _STA disabled
2452 */
2453 dprintk("Blank IRQ resource\n");
2454 return AE_OK;
2455 }
2456 for (i = 0; i < p->interrupt_count; i++) {
2457 if (!p->interrupts[i]) {
2458 printk(KERN_WARNING DRV_PFX
2459 "Invalid IRQ %d\n",
2460 p->interrupts[i]);
2461 continue;
2462 }
2463 interrupt = kzalloc(sizeof(*interrupt),
2464 GFP_KERNEL);
2465 if (!interrupt)
2466 return AE_ERROR;
2467
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002468 list_add(&interrupt->list, &dev->interrupts);
malattia@linux.it33a04452007-04-09 19:26:03 +02002469 interrupt->irq.triggering = p->triggering;
2470 interrupt->irq.polarity = p->polarity;
2471 interrupt->irq.sharable = p->sharable;
2472 interrupt->irq.interrupt_count = 1;
2473 interrupt->irq.interrupts[0] = p->interrupts[i];
2474 }
2475 return AE_OK;
2476 }
2477 case ACPI_RESOURCE_TYPE_IO:
2478 {
2479 struct acpi_resource_io *io = &resource->data.io;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002480 struct sony_pic_ioport *ioport =
2481 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
malattia@linux.it33a04452007-04-09 19:26:03 +02002482 if (!io) {
2483 dprintk("Blank IO resource\n");
2484 return AE_OK;
2485 }
2486
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002487 if (!ioport->io1.minimum) {
2488 memcpy(&ioport->io1, io, sizeof(*io));
2489 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
2490 ioport->io1.address_length);
2491 }
2492 else if (!ioport->io2.minimum) {
2493 memcpy(&ioport->io2, io, sizeof(*io));
2494 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
2495 ioport->io2.address_length);
2496 }
2497 else {
2498 printk(KERN_ERR DRV_PFX "Unknown SPIC Type, more than 2 IO Ports\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002499 return AE_ERROR;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002500 }
malattia@linux.it33a04452007-04-09 19:26:03 +02002501 return AE_OK;
2502 }
2503 default:
2504 dprintk("Resource %d isn't an IRQ nor an IO port\n",
2505 resource->type);
2506
2507 case ACPI_RESOURCE_TYPE_END_TAG:
2508 return AE_OK;
2509 }
2510 return AE_CTRL_TERMINATE;
2511}
2512
2513static int sony_pic_possible_resources(struct acpi_device *device)
2514{
2515 int result = 0;
2516 acpi_status status = AE_OK;
2517
2518 if (!device)
2519 return -EINVAL;
2520
2521 /* get device status */
2522 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
2523 dprintk("Evaluating _STA\n");
2524 result = acpi_bus_get_status(device);
2525 if (result) {
2526 printk(KERN_WARNING DRV_PFX "Unable to read status\n");
2527 goto end;
2528 }
2529
2530 if (!device->status.enabled)
2531 dprintk("Device disabled\n");
2532 else
2533 dprintk("Device enabled\n");
2534
2535 /*
2536 * Query and parse 'method'
2537 */
2538 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
2539 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
2540 sony_pic_read_possible_resource, &spic_dev);
2541 if (ACPI_FAILURE(status)) {
2542 printk(KERN_WARNING DRV_PFX
2543 "Failure evaluating %s\n",
2544 METHOD_NAME__PRS);
2545 result = -ENODEV;
2546 }
2547end:
2548 return result;
2549}
2550
2551/*
2552 * Disable the spic device by calling its _DIS method
2553 */
2554static int sony_pic_disable(struct acpi_device *device)
2555{
Matthew Garrett6158d3a2008-10-29 14:01:03 -07002556 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
2557 NULL);
2558
2559 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
malattia@linux.it33a04452007-04-09 19:26:03 +02002560 return -ENXIO;
2561
2562 dprintk("Device disabled\n");
2563 return 0;
2564}
2565
2566
2567/*
2568 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
2569 *
2570 * Call _SRS to set current resources
2571 */
2572static int sony_pic_enable(struct acpi_device *device,
2573 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
2574{
2575 acpi_status status;
2576 int result = 0;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002577 /* Type 1 resource layout is:
2578 * IO
2579 * IO
2580 * IRQNoFlags
2581 * End
2582 *
2583 * Type 2 and 3 resource layout is:
2584 * IO
2585 * IRQNoFlags
2586 * End
2587 */
malattia@linux.it33a04452007-04-09 19:26:03 +02002588 struct {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002589 struct acpi_resource res1;
2590 struct acpi_resource res2;
2591 struct acpi_resource res3;
2592 struct acpi_resource res4;
malattia@linux.it33a04452007-04-09 19:26:03 +02002593 } *resource;
2594 struct acpi_buffer buffer = { 0, NULL };
2595
2596 if (!ioport || !irq)
2597 return -EINVAL;
2598
2599 /* init acpi_buffer */
2600 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
2601 if (!resource)
2602 return -ENOMEM;
2603
2604 buffer.length = sizeof(*resource) + 1;
2605 buffer.pointer = resource;
2606
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002607 /* setup Type 1 resources */
Mattia Dongili31df7142009-09-16 00:05:29 +09002608 if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002609
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002610 /* setup io resources */
2611 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2612 resource->res1.length = sizeof(struct acpi_resource);
2613 memcpy(&resource->res1.data.io, &ioport->io1,
2614 sizeof(struct acpi_resource_io));
malattia@linux.it33a04452007-04-09 19:26:03 +02002615
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002616 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
2617 resource->res2.length = sizeof(struct acpi_resource);
2618 memcpy(&resource->res2.data.io, &ioport->io2,
2619 sizeof(struct acpi_resource_io));
2620
2621 /* setup irq resource */
2622 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
2623 resource->res3.length = sizeof(struct acpi_resource);
2624 memcpy(&resource->res3.data.irq, &irq->irq,
2625 sizeof(struct acpi_resource_irq));
2626 /* we requested a shared irq */
2627 resource->res3.data.irq.sharable = ACPI_SHARED;
2628
2629 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
2630
2631 }
2632 /* setup Type 2/3 resources */
2633 else {
2634 /* setup io resource */
2635 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2636 resource->res1.length = sizeof(struct acpi_resource);
2637 memcpy(&resource->res1.data.io, &ioport->io1,
2638 sizeof(struct acpi_resource_io));
2639
2640 /* setup irq resource */
2641 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
2642 resource->res2.length = sizeof(struct acpi_resource);
2643 memcpy(&resource->res2.data.irq, &irq->irq,
2644 sizeof(struct acpi_resource_irq));
2645 /* we requested a shared irq */
2646 resource->res2.data.irq.sharable = ACPI_SHARED;
2647
2648 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
2649 }
malattia@linux.it33a04452007-04-09 19:26:03 +02002650
2651 /* Attempt to set the resource */
2652 dprintk("Evaluating _SRS\n");
2653 status = acpi_set_current_resources(device->handle, &buffer);
2654
2655 /* check for total failure */
2656 if (ACPI_FAILURE(status)) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002657 printk(KERN_ERR DRV_PFX "Error evaluating _SRS\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002658 result = -ENODEV;
2659 goto end;
2660 }
2661
2662 /* Necessary device initializations calls (from sonypi) */
2663 sony_pic_call1(0x82);
2664 sony_pic_call2(0x81, 0xff);
2665 sony_pic_call1(compat ? 0x92 : 0x82);
2666
2667end:
2668 kfree(resource);
2669 return result;
2670}
2671
2672/*****************
2673 *
2674 * ISR: some event is available
2675 *
2676 *****************/
2677static irqreturn_t sony_pic_irq(int irq, void *dev_id)
2678{
2679 int i, j;
malattia@linux.it33a04452007-04-09 19:26:03 +02002680 u8 ev = 0;
2681 u8 data_mask = 0;
2682 u8 device_event = 0;
2683
2684 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
2685
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002686 ev = inb_p(dev->cur_ioport->io1.minimum);
2687 if (dev->cur_ioport->io2.minimum)
2688 data_mask = inb_p(dev->cur_ioport->io2.minimum);
2689 else
Mattia Dongilide920432008-01-14 18:05:43 +09002690 data_mask = inb_p(dev->cur_ioport->io1.minimum +
Mattia Dongili31df7142009-09-16 00:05:29 +09002691 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02002692
Mattia Dongili22a17782007-07-16 02:34:39 +09002693 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
Mattia Dongilide920432008-01-14 18:05:43 +09002694 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09002695 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02002696
2697 if (ev == 0x00 || ev == 0xff)
2698 return IRQ_HANDLED;
2699
Mattia Dongili31df7142009-09-16 00:05:29 +09002700 for (i = 0; dev->event_types[i].mask; i++) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002701
Mattia Dongili31df7142009-09-16 00:05:29 +09002702 if ((data_mask & dev->event_types[i].data) !=
2703 dev->event_types[i].data)
malattia@linux.it33a04452007-04-09 19:26:03 +02002704 continue;
2705
Mattia Dongili31df7142009-09-16 00:05:29 +09002706 if (!(mask & dev->event_types[i].mask))
malattia@linux.it33a04452007-04-09 19:26:03 +02002707 continue;
2708
Mattia Dongili31df7142009-09-16 00:05:29 +09002709 for (j = 0; dev->event_types[i].events[j].event; j++) {
2710 if (ev == dev->event_types[i].events[j].data) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002711 device_event =
Mattia Dongili31df7142009-09-16 00:05:29 +09002712 dev->event_types[i].events[j].event;
malattia@linux.it33a04452007-04-09 19:26:03 +02002713 goto found;
2714 }
2715 }
2716 }
Mattia Dongili3eb87492008-01-14 18:05:45 +09002717 /* Still not able to decode the event try to pass
2718 * it over to the minidriver
2719 */
Mattia Dongili31df7142009-09-16 00:05:29 +09002720 if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
Mattia Dongili3eb87492008-01-14 18:05:45 +09002721 return IRQ_HANDLED;
2722
Mattia Dongilide920432008-01-14 18:05:43 +09002723 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
2724 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09002725 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02002726 return IRQ_HANDLED;
2727
2728found:
malattia@linux.it1549ee62007-04-09 10:19:08 +02002729 sony_laptop_report_input_event(device_event);
Mattia Dongilide920432008-01-14 18:05:43 +09002730 acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002731 sonypi_compat_report_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02002732
2733 return IRQ_HANDLED;
2734}
2735
2736/*****************
2737 *
2738 * ACPI driver
2739 *
2740 *****************/
2741static int sony_pic_remove(struct acpi_device *device, int type)
2742{
2743 struct sony_pic_ioport *io, *tmp_io;
2744 struct sony_pic_irq *irq, *tmp_irq;
2745
2746 if (sony_pic_disable(device)) {
2747 printk(KERN_ERR DRV_PFX "Couldn't disable device.\n");
2748 return -ENXIO;
2749 }
2750
2751 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002752 release_region(spic_dev.cur_ioport->io1.minimum,
2753 spic_dev.cur_ioport->io1.address_length);
2754 if (spic_dev.cur_ioport->io2.minimum)
2755 release_region(spic_dev.cur_ioport->io2.minimum,
2756 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02002757
Mattia Dongili015a9162007-08-12 16:20:27 +09002758 sonypi_compat_exit();
2759
malattia@linux.it1549ee62007-04-09 10:19:08 +02002760 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02002761
malattia@linux.it49a11de2007-04-09 19:28:56 +02002762 /* pf attrs */
2763 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2764 sony_pf_remove();
2765
malattia@linux.it33a04452007-04-09 19:26:03 +02002766 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2767 list_del(&io->list);
2768 kfree(io);
2769 }
2770 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2771 list_del(&irq->list);
2772 kfree(irq);
2773 }
2774 spic_dev.cur_ioport = NULL;
2775 spic_dev.cur_irq = NULL;
2776
malattia@linux.itf6119b02007-04-09 19:31:06 +02002777 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002778 return 0;
2779}
2780
2781static int sony_pic_add(struct acpi_device *device)
2782{
2783 int result;
2784 struct sony_pic_ioport *io, *tmp_io;
2785 struct sony_pic_irq *irq, *tmp_irq;
2786
malattia@linux.itf6119b02007-04-09 19:31:06 +02002787 printk(KERN_INFO DRV_PFX "%s v%s.\n",
2788 SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.it33a04452007-04-09 19:26:03 +02002789
2790 spic_dev.acpi_dev = device;
2791 strcpy(acpi_device_class(device), "sony/hotkey");
Mattia Dongilide920432008-01-14 18:05:43 +09002792 sony_pic_detect_device_type(&spic_dev);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002793 mutex_init(&spic_dev.lock);
malattia@linux.it33a04452007-04-09 19:26:03 +02002794
2795 /* read _PRS resources */
2796 result = sony_pic_possible_resources(device);
2797 if (result) {
2798 printk(KERN_ERR DRV_PFX
Alessio Igor Boganiff07a532009-04-01 22:10:44 +09002799 "Unable to read possible resources.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002800 goto err_free_resources;
2801 }
2802
2803 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05002804 result = sony_laptop_setup_input(device);
malattia@linux.it33a04452007-04-09 19:26:03 +02002805 if (result) {
2806 printk(KERN_ERR DRV_PFX
Alessio Igor Boganiff07a532009-04-01 22:10:44 +09002807 "Unable to create input devices.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002808 goto err_free_resources;
2809 }
2810
Mattia Dongili015a9162007-08-12 16:20:27 +09002811 if (sonypi_compat_init())
2812 goto err_remove_input;
2813
malattia@linux.it33a04452007-04-09 19:26:03 +02002814 /* request io port */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002815 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
2816 if (request_region(io->io1.minimum, io->io1.address_length,
malattia@linux.it33a04452007-04-09 19:26:03 +02002817 "Sony Programable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002818 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
2819 io->io1.minimum, io->io1.maximum,
2820 io->io1.address_length);
2821 /* Type 1 have 2 ioports */
2822 if (io->io2.minimum) {
2823 if (request_region(io->io2.minimum,
2824 io->io2.address_length,
2825 "Sony Programable I/O Device")) {
2826 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
2827 io->io2.minimum, io->io2.maximum,
2828 io->io2.address_length);
2829 spic_dev.cur_ioport = io;
2830 break;
2831 }
2832 else {
2833 dprintk("Unable to get I/O port2: "
2834 "0x%.4x (0x%.4x) + 0x%.2x\n",
2835 io->io2.minimum, io->io2.maximum,
2836 io->io2.address_length);
2837 release_region(io->io1.minimum,
2838 io->io1.address_length);
2839 }
2840 }
2841 else {
2842 spic_dev.cur_ioport = io;
2843 break;
2844 }
malattia@linux.it33a04452007-04-09 19:26:03 +02002845 }
2846 }
2847 if (!spic_dev.cur_ioport) {
2848 printk(KERN_ERR DRV_PFX "Failed to request_region.\n");
2849 result = -ENODEV;
Mattia Dongili015a9162007-08-12 16:20:27 +09002850 goto err_remove_compat;
malattia@linux.it33a04452007-04-09 19:26:03 +02002851 }
2852
2853 /* request IRQ */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002854 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002855 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
Mattia Dongilid1e0de92009-09-16 00:05:30 +09002856 IRQF_DISABLED, "sony-laptop", &spic_dev)) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002857 dprintk("IRQ: %d - triggering: %d - "
2858 "polarity: %d - shr: %d\n",
2859 irq->irq.interrupts[0],
2860 irq->irq.triggering,
2861 irq->irq.polarity,
2862 irq->irq.sharable);
2863 spic_dev.cur_irq = irq;
2864 break;
2865 }
2866 }
2867 if (!spic_dev.cur_irq) {
2868 printk(KERN_ERR DRV_PFX "Failed to request_irq.\n");
2869 result = -ENODEV;
2870 goto err_release_region;
2871 }
2872
2873 /* set resource status _SRS */
2874 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2875 if (result) {
2876 printk(KERN_ERR DRV_PFX "Couldn't enable device.\n");
2877 goto err_free_irq;
2878 }
2879
malattia@linux.it49a11de2007-04-09 19:28:56 +02002880 spic_dev.bluetooth_power = -1;
2881 /* create device attributes */
2882 result = sony_pf_add();
2883 if (result)
2884 goto err_disable_device;
2885
2886 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2887 if (result)
2888 goto err_remove_pf;
2889
malattia@linux.it33a04452007-04-09 19:26:03 +02002890 return 0;
2891
malattia@linux.it49a11de2007-04-09 19:28:56 +02002892err_remove_pf:
2893 sony_pf_remove();
2894
2895err_disable_device:
2896 sony_pic_disable(device);
2897
malattia@linux.it33a04452007-04-09 19:26:03 +02002898err_free_irq:
2899 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2900
2901err_release_region:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002902 release_region(spic_dev.cur_ioport->io1.minimum,
2903 spic_dev.cur_ioport->io1.address_length);
2904 if (spic_dev.cur_ioport->io2.minimum)
2905 release_region(spic_dev.cur_ioport->io2.minimum,
2906 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02002907
Mattia Dongili015a9162007-08-12 16:20:27 +09002908err_remove_compat:
2909 sonypi_compat_exit();
2910
malattia@linux.it33a04452007-04-09 19:26:03 +02002911err_remove_input:
malattia@linux.it1549ee62007-04-09 10:19:08 +02002912 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02002913
2914err_free_resources:
2915 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2916 list_del(&io->list);
2917 kfree(io);
2918 }
2919 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2920 list_del(&irq->list);
2921 kfree(irq);
2922 }
2923 spic_dev.cur_ioport = NULL;
2924 spic_dev.cur_irq = NULL;
2925
2926 return result;
2927}
2928
2929static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
2930{
2931 if (sony_pic_disable(device))
2932 return -ENXIO;
2933 return 0;
2934}
2935
2936static int sony_pic_resume(struct acpi_device *device)
2937{
2938 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2939 return 0;
2940}
2941
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002942static const struct acpi_device_id sony_pic_device_ids[] = {
2943 {SONY_PIC_HID, 0},
2944 {"", 0},
2945};
2946
malattia@linux.it33a04452007-04-09 19:26:03 +02002947static struct acpi_driver sony_pic_driver = {
2948 .name = SONY_PIC_DRIVER_NAME,
2949 .class = SONY_PIC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002950 .ids = sony_pic_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02002951 .owner = THIS_MODULE,
2952 .ops = {
2953 .add = sony_pic_add,
2954 .remove = sony_pic_remove,
2955 .suspend = sony_pic_suspend,
2956 .resume = sony_pic_resume,
2957 },
2958};
2959
2960static struct dmi_system_id __initdata sonypi_dmi_table[] = {
2961 {
2962 .ident = "Sony Vaio",
2963 .matches = {
2964 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2965 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
2966 },
2967 },
2968 {
2969 .ident = "Sony Vaio",
2970 .matches = {
2971 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2972 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
2973 },
2974 },
2975 { }
2976};
2977
malattia@linux.it59b19102007-04-09 10:19:04 +02002978static int __init sony_laptop_init(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01002979{
malattia@linux.it33a04452007-04-09 19:26:03 +02002980 int result;
2981
2982 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
2983 result = acpi_bus_register_driver(&sony_pic_driver);
2984 if (result) {
2985 printk(KERN_ERR DRV_PFX
2986 "Unable to register SPIC driver.");
2987 goto out;
2988 }
Alan Jenkins5e6f9722009-09-16 00:05:32 +09002989 spic_drv_registered = 1;
malattia@linux.it33a04452007-04-09 19:26:03 +02002990 }
2991
2992 result = acpi_bus_register_driver(&sony_nc_driver);
2993 if (result) {
2994 printk(KERN_ERR DRV_PFX "Unable to register SNC driver.");
2995 goto out_unregister_pic;
2996 }
2997
2998 return 0;
2999
3000out_unregister_pic:
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003001 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02003002 acpi_bus_unregister_driver(&sony_pic_driver);
3003out:
3004 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +01003005}
3006
malattia@linux.it59b19102007-04-09 10:19:04 +02003007static void __exit sony_laptop_exit(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01003008{
malattia@linux.it59b19102007-04-09 10:19:04 +02003009 acpi_bus_unregister_driver(&sony_nc_driver);
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003010 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02003011 acpi_bus_unregister_driver(&sony_pic_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +01003012}
3013
malattia@linux.it59b19102007-04-09 10:19:04 +02003014module_init(sony_laptop_init);
3015module_exit(sony_laptop_exit);