blob: 33d9555470c3d86e80dc3244d7fa2852846609ff [file] [log] [blame]
Stelian Pop7f09c432007-01-13 23:04:31 +01001/*
malattia@linux.it33a04452007-04-09 19:26:03 +02002 * ACPI Sony Notebook Control Driver (SNC and SPIC)
Stelian Pop7f09c432007-01-13 23:04:31 +01003 *
4 * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
Mattia Dongili37418342009-03-26 21:58:24 +09005 * Copyright (C) 2007-2009 Mattia Dongili <malattia@linux.it>
Stelian Pop7f09c432007-01-13 23:04:31 +01006 *
7 * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
8 * which are copyrighted by their respective authors.
9 *
malattia@linux.it33a04452007-04-09 19:26:03 +020010 * The SNY6001 driver part is based on the sonypi driver which includes
11 * material from:
12 *
13 * Copyright (C) 2001-2005 Stelian Pop <stelian@popies.net>
14 *
15 * Copyright (C) 2005 Narayanan R S <nars@kadamba.org>
16 *
Marcin Garskidb9551702007-10-19 23:22:11 +020017 * Copyright (C) 2001-2002 AlcĂ´ve <www.alcove.com>
malattia@linux.it33a04452007-04-09 19:26:03 +020018 *
19 * Copyright (C) 2001 Michael Ashley <m.ashley@unsw.edu.au>
20 *
21 * Copyright (C) 2001 Junichi Morita <jun1m@mars.dti.ne.jp>
22 *
23 * Copyright (C) 2000 Takaya Kinjo <t-kinjo@tc4.so-net.ne.jp>
24 *
25 * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
26 *
27 * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
28 *
Stelian Pop7f09c432007-01-13 23:04:31 +010029 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
33 *
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
38 *
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
42 *
43 */
44
45#include <linux/kernel.h>
46#include <linux/module.h>
47#include <linux/moduleparam.h>
48#include <linux/init.h>
49#include <linux/types.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010050#include <linux/backlight.h>
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010051#include <linux/platform_device.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010052#include <linux/err.h>
malattia@linux.it33a04452007-04-09 19:26:03 +020053#include <linux/dmi.h>
54#include <linux/pci.h>
55#include <linux/interrupt.h>
56#include <linux/delay.h>
57#include <linux/input.h>
58#include <linux/kfifo.h>
59#include <linux/workqueue.h>
60#include <linux/acpi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090061#include <linux/slab.h>
Stelian Pop7f09c432007-01-13 23:04:31 +010062#include <acpi/acpi_drivers.h>
63#include <acpi/acpi_bus.h>
64#include <asm/uaccess.h>
malattia@linux.it33a04452007-04-09 19:26:03 +020065#include <linux/sonypi.h>
malattia@linux.it1ce82c12007-04-28 23:34:36 +090066#include <linux/sony-laptop.h>
Matthew Garrett6cc056b2009-03-26 21:58:15 +090067#include <linux/rfkill.h>
Mattia Dongilia64e62a2007-05-01 11:19:53 +090068#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +020069#include <linux/poll.h>
70#include <linux/miscdevice.h>
71#endif
Stelian Pop7f09c432007-01-13 23:04:31 +010072
malattia@linux.it33a04452007-04-09 19:26:03 +020073#define DRV_PFX "sony-laptop: "
Mattia Dongilid6697932011-02-19 11:52:28 +090074#define dprintk(msg...) do { \
75 if (debug) \
76 pr_warn(DRV_PFX msg); \
malattia@linux.itb9a218b2007-04-09 10:19:06 +020077} while (0)
78
Mattia Dongili425ef5d2008-01-14 18:05:44 +090079#define SONY_LAPTOP_DRIVER_VERSION "0.6"
Alessandro Guido50f62af2007-01-13 23:04:34 +010080
malattia@linux.it33a04452007-04-09 19:26:03 +020081#define SONY_NC_CLASS "sony-nc"
82#define SONY_NC_HID "SNY5001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020083#define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
malattia@linux.it33a04452007-04-09 19:26:03 +020084
85#define SONY_PIC_CLASS "sony-pic"
86#define SONY_PIC_HID "SNY6001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020087#define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
Stelian Pop7f09c432007-01-13 23:04:31 +010088
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010089MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
malattia@linux.it33a04452007-04-09 19:26:03 +020090MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
Stelian Pop7f09c432007-01-13 23:04:31 +010091MODULE_LICENSE("GPL");
malattia@linux.it33a04452007-04-09 19:26:03 +020092MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
Stelian Pop7f09c432007-01-13 23:04:31 +010093
94static int debug;
95module_param(debug, int, 0);
96MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
Len Browna02d1c12007-02-07 15:34:02 -050097 "the development of this driver");
Stelian Pop7f09c432007-01-13 23:04:31 +010098
malattia@linux.it33a04452007-04-09 19:26:03 +020099static int no_spic; /* = 0 */
100module_param(no_spic, int, 0444);
101MODULE_PARM_DESC(no_spic,
102 "set this if you don't want to enable the SPIC device");
103
104static int compat; /* = 0 */
105module_param(compat, int, 0444);
106MODULE_PARM_DESC(compat,
malattia@linux.it7b153f32007-04-09 19:31:25 +0200107 "set this if you want to enable backward compatibility mode");
malattia@linux.it33a04452007-04-09 19:26:03 +0200108
109static unsigned long mask = 0xffffffff;
110module_param(mask, ulong, 0644);
111MODULE_PARM_DESC(mask,
112 "set this to the mask of event you want to enable (see doc)");
113
malattia@linux.it5f3d2892007-04-28 23:18:45 +0900114static int camera; /* = 0 */
115module_param(camera, int, 0444);
116MODULE_PARM_DESC(camera,
117 "set this to 1 to enable Motion Eye camera controls "
118 "(only use it if you have a C1VE or C1VN model)");
119
Mattia Dongilia64e62a2007-05-01 11:19:53 +0900120#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +0200121static int minor = -1;
122module_param(minor, int, 0);
123MODULE_PARM_DESC(minor,
124 "minor number of the misc device for the SPIC compatibility code, "
125 "default is -1 (automatic)");
126#endif
127
Mattia Dongilibf155712011-02-19 11:52:31 +0900128static int kbd_backlight; /* = 1 */
129module_param(kbd_backlight, int, 0444);
130MODULE_PARM_DESC(kbd_backlight,
131 "set this to 0 to disable keyboard backlight, "
132 "1 to enable it (default: 0)");
133
134static int kbd_backlight_timeout; /* = 0 */
135module_param(kbd_backlight_timeout, int, 0444);
136MODULE_PARM_DESC(kbd_backlight_timeout,
137 "set this to 0 to set the default 10 seconds timeout, "
138 "1 for 30 seconds, 2 for 60 seconds and 3 to disable timeout "
139 "(default: 0)");
140
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900141enum sony_nc_rfkill {
142 SONY_WIFI,
143 SONY_BLUETOOTH,
144 SONY_WWAN,
145 SONY_WIMAX,
Johannes Berg19d337d2009-06-02 13:01:37 +0200146 N_SONY_RFKILL,
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900147};
148
Mattia Dongilid5a664a2009-12-17 00:08:35 +0900149static int sony_rfkill_handle;
Johannes Berg19d337d2009-06-02 13:01:37 +0200150static struct rfkill *sony_rfkill_devices[N_SONY_RFKILL];
151static int sony_rfkill_address[N_SONY_RFKILL] = {0x300, 0x500, 0x700, 0x900};
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900152static void sony_nc_rfkill_update(void);
153
malattia@linux.it1549ee62007-04-09 10:19:08 +0200154/*********** Input Devices ***********/
155
156#define SONY_LAPTOP_BUF_SIZE 128
157struct sony_laptop_input_s {
158 atomic_t users;
159 struct input_dev *jog_dev;
160 struct input_dev *key_dev;
Stefani Seibold45465482009-12-21 14:37:26 -0800161 struct kfifo fifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200162 spinlock_t fifo_lock;
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800163 struct timer_list release_key_timer;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200164};
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900165
malattia@linux.it1549ee62007-04-09 10:19:08 +0200166static struct sony_laptop_input_s sony_laptop_input = {
167 .users = ATOMIC_INIT(0),
168};
169
170struct sony_laptop_keypress {
171 struct input_dev *dev;
172 int key;
173};
174
Mattia Dongilibc57f862007-07-20 02:01:57 +0900175/* Correspondance table between sonypi events
176 * and input layer indexes in the keymap
177 */
178static int sony_laptop_input_index[] = {
Mattia Dongili3eb87492008-01-14 18:05:45 +0900179 -1, /* 0 no event */
180 -1, /* 1 SONYPI_EVENT_JOGDIAL_DOWN */
181 -1, /* 2 SONYPI_EVENT_JOGDIAL_UP */
182 -1, /* 3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
183 -1, /* 4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */
184 -1, /* 5 SONYPI_EVENT_JOGDIAL_PRESSED */
185 -1, /* 6 SONYPI_EVENT_JOGDIAL_RELEASED */
186 0, /* 7 SONYPI_EVENT_CAPTURE_PRESSED */
187 1, /* 8 SONYPI_EVENT_CAPTURE_RELEASED */
188 2, /* 9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
189 3, /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
190 4, /* 11 SONYPI_EVENT_FNKEY_ESC */
191 5, /* 12 SONYPI_EVENT_FNKEY_F1 */
192 6, /* 13 SONYPI_EVENT_FNKEY_F2 */
193 7, /* 14 SONYPI_EVENT_FNKEY_F3 */
194 8, /* 15 SONYPI_EVENT_FNKEY_F4 */
195 9, /* 16 SONYPI_EVENT_FNKEY_F5 */
196 10, /* 17 SONYPI_EVENT_FNKEY_F6 */
197 11, /* 18 SONYPI_EVENT_FNKEY_F7 */
198 12, /* 19 SONYPI_EVENT_FNKEY_F8 */
199 13, /* 20 SONYPI_EVENT_FNKEY_F9 */
200 14, /* 21 SONYPI_EVENT_FNKEY_F10 */
201 15, /* 22 SONYPI_EVENT_FNKEY_F11 */
202 16, /* 23 SONYPI_EVENT_FNKEY_F12 */
203 17, /* 24 SONYPI_EVENT_FNKEY_1 */
204 18, /* 25 SONYPI_EVENT_FNKEY_2 */
205 19, /* 26 SONYPI_EVENT_FNKEY_D */
206 20, /* 27 SONYPI_EVENT_FNKEY_E */
207 21, /* 28 SONYPI_EVENT_FNKEY_F */
208 22, /* 29 SONYPI_EVENT_FNKEY_S */
209 23, /* 30 SONYPI_EVENT_FNKEY_B */
210 24, /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */
211 25, /* 32 SONYPI_EVENT_PKEY_P1 */
212 26, /* 33 SONYPI_EVENT_PKEY_P2 */
213 27, /* 34 SONYPI_EVENT_PKEY_P3 */
214 28, /* 35 SONYPI_EVENT_BACK_PRESSED */
215 -1, /* 36 SONYPI_EVENT_LID_CLOSED */
216 -1, /* 37 SONYPI_EVENT_LID_OPENED */
217 29, /* 38 SONYPI_EVENT_BLUETOOTH_ON */
218 30, /* 39 SONYPI_EVENT_BLUETOOTH_OFF */
219 31, /* 40 SONYPI_EVENT_HELP_PRESSED */
220 32, /* 41 SONYPI_EVENT_FNKEY_ONLY */
221 33, /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
222 34, /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */
223 35, /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
224 36, /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
225 37, /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
226 38, /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */
227 39, /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
228 40, /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
229 41, /* 50 SONYPI_EVENT_ZOOM_PRESSED */
230 42, /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */
231 43, /* 52 SONYPI_EVENT_MEYE_FACE */
232 44, /* 53 SONYPI_EVENT_MEYE_OPPOSITE */
233 45, /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */
234 46, /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */
235 -1, /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */
236 -1, /* 57 SONYPI_EVENT_BATTERY_INSERT */
237 -1, /* 58 SONYPI_EVENT_BATTERY_REMOVE */
238 -1, /* 59 SONYPI_EVENT_FNKEY_RELEASED */
239 47, /* 60 SONYPI_EVENT_WIRELESS_ON */
240 48, /* 61 SONYPI_EVENT_WIRELESS_OFF */
241 49, /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */
242 50, /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900243 51, /* 64 SONYPI_EVENT_CD_EJECT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900244 52, /* 65 SONYPI_EVENT_MODEKEY_PRESSED */
245 53, /* 66 SONYPI_EVENT_PKEY_P4 */
246 54, /* 67 SONYPI_EVENT_PKEY_P5 */
247 55, /* 68 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900248 56, /* 69 SONYPI_EVENT_VOLUME_INC_PRESSED */
249 57, /* 70 SONYPI_EVENT_VOLUME_DEC_PRESSED */
250 -1, /* 71 SONYPI_EVENT_BRIGHTNESS_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900251 58, /* 72 SONYPI_EVENT_MEDIA_PRESSED */
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900252 59, /* 72 SONYPI_EVENT_VENDOR_PRESSED */
Mattia Dongilibc57f862007-07-20 02:01:57 +0900253};
254
255static int sony_laptop_input_keycode_map[] = {
256 KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
257 KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
258 KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
259 KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
260 KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
261 KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
262 KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
263 KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
264 KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
265 KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
266 KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
267 KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
268 KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
269 KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
270 KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
271 KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
272 KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
273 KEY_FN_F1, /* 17 SONYPI_EVENT_FNKEY_1 */
274 KEY_FN_F2, /* 18 SONYPI_EVENT_FNKEY_2 */
275 KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
276 KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
277 KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
278 KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
279 KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
280 KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
281 KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
282 KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
283 KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
284 KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
285 KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
286 KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
287 KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
288 KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
289 KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
290 KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
291 KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
292 KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
293 KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
294 KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
295 KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
296 KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
297 KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
298 BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
299 KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
300 KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
301 KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
302 KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
303 KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
304 KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
Mattia Dongili3eb87492008-01-14 18:05:45 +0900305 KEY_ZOOMIN, /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900306 KEY_ZOOMOUT, /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900307 KEY_EJECTCD, /* 51 SONYPI_EVENT_CD_EJECT_PRESSED */
308 KEY_F13, /* 52 SONYPI_EVENT_MODEKEY_PRESSED */
309 KEY_PROG4, /* 53 SONYPI_EVENT_PKEY_P4 */
310 KEY_F14, /* 54 SONYPI_EVENT_PKEY_P5 */
311 KEY_F15, /* 55 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900312 KEY_VOLUMEUP, /* 56 SONYPI_EVENT_VOLUME_INC_PRESSED */
313 KEY_VOLUMEDOWN, /* 57 SONYPI_EVENT_VOLUME_DEC_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900314 KEY_MEDIA, /* 58 SONYPI_EVENT_MEDIA_PRESSED */
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900315 KEY_VENDOR, /* 59 SONYPI_EVENT_VENDOR_PRESSED */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200316};
317
318/* release buttons after a short delay if pressed */
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800319static void do_sony_laptop_release_key(unsigned long unused)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200320{
321 struct sony_laptop_keypress kp;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800322 unsigned long flags;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200323
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800324 spin_lock_irqsave(&sony_laptop_input.fifo_lock, flags);
325
326 if (kfifo_out(&sony_laptop_input.fifo,
327 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200328 input_report_key(kp.dev, kp.key, 0);
329 input_sync(kp.dev);
330 }
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800331
332 /* If there is something in the fifo schedule next release. */
333 if (kfifo_len(&sony_laptop_input.fifo) != 0)
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800334 mod_timer(&sony_laptop_input.release_key_timer,
335 jiffies + msecs_to_jiffies(10));
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800336
337 spin_unlock_irqrestore(&sony_laptop_input.fifo_lock, flags);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200338}
malattia@linux.it1549ee62007-04-09 10:19:08 +0200339
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200340/* forward event to the input subsystem */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200341static void sony_laptop_report_input_event(u8 event)
342{
343 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
344 struct input_dev *key_dev = sony_laptop_input.key_dev;
345 struct sony_laptop_keypress kp = { NULL };
malattia@linux.it1549ee62007-04-09 10:19:08 +0200346
Almer S. Tigelaara83021a2009-04-12 11:26:28 +0000347 if (event == SONYPI_EVENT_FNKEY_RELEASED ||
348 event == SONYPI_EVENT_ANYBUTTON_RELEASED) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200349 /* Nothing, not all VAIOs generate this event */
350 return;
351 }
352
353 /* report events */
354 switch (event) {
355 /* jog_dev events */
356 case SONYPI_EVENT_JOGDIAL_UP:
357 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
358 input_report_rel(jog_dev, REL_WHEEL, 1);
359 input_sync(jog_dev);
360 return;
361
362 case SONYPI_EVENT_JOGDIAL_DOWN:
363 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
364 input_report_rel(jog_dev, REL_WHEEL, -1);
365 input_sync(jog_dev);
366 return;
367
368 /* key_dev events */
369 case SONYPI_EVENT_JOGDIAL_PRESSED:
370 kp.key = BTN_MIDDLE;
371 kp.dev = jog_dev;
372 break;
373
374 default:
Adrian Bunkd399d132008-02-20 00:59:03 +0200375 if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
Mattia Dongilibc57f862007-07-20 02:01:57 +0900376 dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
377 break;
378 }
379 if (sony_laptop_input_index[event] != -1) {
380 kp.key = sony_laptop_input_keycode_map[sony_laptop_input_index[event]];
381 if (kp.key != KEY_UNKNOWN)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200382 kp.dev = key_dev;
Mattia Dongilibc57f862007-07-20 02:01:57 +0900383 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200384 break;
385 }
386
387 if (kp.dev) {
388 input_report_key(kp.dev, kp.key, 1);
Mattia Dongilibc57f862007-07-20 02:01:57 +0900389 /* we emit the scancode so we can always remap the key */
390 input_event(kp.dev, EV_MSC, MSC_SCAN, event);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200391 input_sync(kp.dev);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200392
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800393 /* schedule key release */
394 kfifo_in_locked(&sony_laptop_input.fifo,
395 (unsigned char *)&kp, sizeof(kp),
396 &sony_laptop_input.fifo_lock);
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800397 mod_timer(&sony_laptop_input.release_key_timer,
398 jiffies + msecs_to_jiffies(10));
malattia@linux.it1549ee62007-04-09 10:19:08 +0200399 } else
400 dprintk("unknown input event %.2x\n", event);
401}
402
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500403static int sony_laptop_setup_input(struct acpi_device *acpi_device)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200404{
405 struct input_dev *jog_dev;
406 struct input_dev *key_dev;
407 int i;
408 int error;
409
410 /* don't run again if already initialized */
411 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
412 return 0;
413
414 /* kfifo */
415 spin_lock_init(&sony_laptop_input.fifo_lock);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800416 error = kfifo_alloc(&sony_laptop_input.fifo,
417 SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -0800418 if (error) {
Mattia Dongilid6697932011-02-19 11:52:28 +0900419 pr_err(DRV_PFX "kfifo_alloc failed\n");
malattia@linux.it1549ee62007-04-09 10:19:08 +0200420 goto err_dec_users;
421 }
422
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800423 setup_timer(&sony_laptop_input.release_key_timer,
424 do_sony_laptop_release_key, 0);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200425
426 /* input keys */
427 key_dev = input_allocate_device();
428 if (!key_dev) {
429 error = -ENOMEM;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800430 goto err_free_kfifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200431 }
432
433 key_dev->name = "Sony Vaio Keys";
434 key_dev->id.bustype = BUS_ISA;
435 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500436 key_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200437
438 /* Initialize the Input Drivers: special keys */
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800439 input_set_capability(key_dev, EV_MSC, MSC_SCAN);
440
441 __set_bit(EV_KEY, key_dev->evbit);
Mattia Dongilibc57f862007-07-20 02:01:57 +0900442 key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
443 key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
444 key_dev->keycode = &sony_laptop_input_keycode_map;
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800445 for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++)
446 __set_bit(sony_laptop_input_keycode_map[i], key_dev->keybit);
447 __clear_bit(KEY_RESERVED, key_dev->keybit);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200448
449 error = input_register_device(key_dev);
450 if (error)
451 goto err_free_keydev;
452
453 sony_laptop_input.key_dev = key_dev;
454
455 /* jogdial */
456 jog_dev = input_allocate_device();
457 if (!jog_dev) {
458 error = -ENOMEM;
459 goto err_unregister_keydev;
460 }
461
462 jog_dev->name = "Sony Vaio Jogdial";
463 jog_dev->id.bustype = BUS_ISA;
464 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500465 key_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200466
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800467 input_set_capability(jog_dev, EV_KEY, BTN_MIDDLE);
468 input_set_capability(jog_dev, EV_REL, REL_WHEEL);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200469
470 error = input_register_device(jog_dev);
471 if (error)
472 goto err_free_jogdev;
473
474 sony_laptop_input.jog_dev = jog_dev;
475
476 return 0;
477
478err_free_jogdev:
479 input_free_device(jog_dev);
480
481err_unregister_keydev:
482 input_unregister_device(key_dev);
483 /* to avoid kref underflow below at input_free_device */
484 key_dev = NULL;
485
486err_free_keydev:
487 input_free_device(key_dev);
488
malattia@linux.it1549ee62007-04-09 10:19:08 +0200489err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -0800490 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200491
492err_dec_users:
493 atomic_dec(&sony_laptop_input.users);
494 return error;
495}
496
497static void sony_laptop_remove_input(void)
498{
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800499 struct sony_laptop_keypress kp = { NULL };
500
501 /* Cleanup only after the last user has gone */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200502 if (!atomic_dec_and_test(&sony_laptop_input.users))
503 return;
504
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800505 del_timer_sync(&sony_laptop_input.release_key_timer);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800506
507 /*
508 * Generate key-up events for remaining keys. Note that we don't
509 * need locking since nobody is adding new events to the kfifo.
510 */
511 while (kfifo_out(&sony_laptop_input.fifo,
512 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
513 input_report_key(kp.dev, kp.key, 0);
514 input_sync(kp.dev);
515 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200516
517 /* destroy input devs */
518 input_unregister_device(sony_laptop_input.key_dev);
519 sony_laptop_input.key_dev = NULL;
520
521 if (sony_laptop_input.jog_dev) {
522 input_unregister_device(sony_laptop_input.jog_dev);
523 sony_laptop_input.jog_dev = NULL;
524 }
525
Stefani Seibold45465482009-12-21 14:37:26 -0800526 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200527}
528
malattia@linux.it56b87562007-04-09 10:19:05 +0200529/*********** Platform Device ***********/
530
531static atomic_t sony_pf_users = ATOMIC_INIT(0);
532static struct platform_driver sony_pf_driver = {
533 .driver = {
534 .name = "sony-laptop",
535 .owner = THIS_MODULE,
536 }
537};
538static struct platform_device *sony_pf_device;
539
540static int sony_pf_add(void)
541{
542 int ret = 0;
543
544 /* don't run again if already initialized */
545 if (atomic_add_return(1, &sony_pf_users) > 1)
546 return 0;
547
548 ret = platform_driver_register(&sony_pf_driver);
549 if (ret)
550 goto out;
551
552 sony_pf_device = platform_device_alloc("sony-laptop", -1);
553 if (!sony_pf_device) {
554 ret = -ENOMEM;
555 goto out_platform_registered;
556 }
557
558 ret = platform_device_add(sony_pf_device);
559 if (ret)
560 goto out_platform_alloced;
561
562 return 0;
563
564 out_platform_alloced:
565 platform_device_put(sony_pf_device);
566 sony_pf_device = NULL;
567 out_platform_registered:
568 platform_driver_unregister(&sony_pf_driver);
569 out:
570 atomic_dec(&sony_pf_users);
571 return ret;
572}
573
574static void sony_pf_remove(void)
575{
576 /* deregister only after the last user has gone */
577 if (!atomic_dec_and_test(&sony_pf_users))
578 return;
579
Axel Lin08db2b32010-07-01 10:18:01 +0800580 platform_device_unregister(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +0200581 platform_driver_unregister(&sony_pf_driver);
582}
583
584/*********** SNC (SNY5001) Device ***********/
585
malattia@linux.it33a04452007-04-09 19:26:03 +0200586/* the device uses 1-based values, while the backlight subsystem uses
587 0-based values */
588#define SONY_MAX_BRIGHTNESS 8
589
590#define SNC_VALIDATE_IN 0
591#define SNC_VALIDATE_OUT 1
592
malattia@linux.it59b19102007-04-09 10:19:04 +0200593static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500594 char *);
malattia@linux.it59b19102007-04-09 10:19:04 +0200595static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500596 const char *, size_t);
Mattia Dongili156c2212007-02-12 22:01:07 +0100597static int boolean_validate(const int, const int);
598static int brightness_default_validate(const int, const int);
599
malattia@linux.it59b19102007-04-09 10:19:04 +0200600struct sony_nc_value {
Len Browna02d1c12007-02-07 15:34:02 -0500601 char *name; /* name of the entry */
602 char **acpiget; /* names of the ACPI get function */
603 char **acpiset; /* names of the ACPI set function */
Mattia Dongili156c2212007-02-12 22:01:07 +0100604 int (*validate)(const int, const int); /* input/output validation */
Len Browna02d1c12007-02-07 15:34:02 -0500605 int value; /* current setting */
606 int valid; /* Has ever been set */
607 int debug; /* active only in debug mode ? */
Lucas De Marchic8440332011-03-17 17:18:22 -0300608 struct device_attribute devattr; /* sysfs attribute */
Stelian Pop7f09c432007-01-13 23:04:31 +0100609};
610
malattia@linux.it59b19102007-04-09 10:19:04 +0200611#define SNC_HANDLE_NAMES(_name, _values...) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100612 static char *snc_##_name[] = { _values, NULL }
613
malattia@linux.it59b19102007-04-09 10:19:04 +0200614#define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100615 { \
616 .name = __stringify(_name), \
617 .acpiget = _getters, \
618 .acpiset = _setters, \
Mattia Dongili156c2212007-02-12 22:01:07 +0100619 .validate = _validate, \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100620 .debug = _debug, \
malattia@linux.it59b19102007-04-09 10:19:04 +0200621 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100622 }
623
malattia@linux.it59b19102007-04-09 10:19:04 +0200624#define SNC_HANDLE_NULL { .name = NULL }
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100625
malattia@linux.it59b19102007-04-09 10:19:04 +0200626SNC_HANDLE_NAMES(fnkey_get, "GHKE");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100627
malattia@linux.it59b19102007-04-09 10:19:04 +0200628SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
629SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100630
malattia@linux.it59b19102007-04-09 10:19:04 +0200631SNC_HANDLE_NAMES(cdpower_get, "GCDP");
632SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100633
malattia@linux.it59b19102007-04-09 10:19:04 +0200634SNC_HANDLE_NAMES(audiopower_get, "GAZP");
635SNC_HANDLE_NAMES(audiopower_set, "AZPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100636
malattia@linux.it59b19102007-04-09 10:19:04 +0200637SNC_HANDLE_NAMES(lanpower_get, "GLNP");
638SNC_HANDLE_NAMES(lanpower_set, "LNPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100639
Mattia Dongili044847e2007-07-16 02:34:33 +0900640SNC_HANDLE_NAMES(lidstate_get, "GLID");
641
642SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
643SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
644
645SNC_HANDLE_NAMES(gainbass_get, "GMGB");
646SNC_HANDLE_NAMES(gainbass_set, "CMGB");
647
malattia@linux.it59b19102007-04-09 10:19:04 +0200648SNC_HANDLE_NAMES(PID_get, "GPID");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100649
malattia@linux.it59b19102007-04-09 10:19:04 +0200650SNC_HANDLE_NAMES(CTR_get, "GCTR");
651SNC_HANDLE_NAMES(CTR_set, "SCTR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100652
malattia@linux.it59b19102007-04-09 10:19:04 +0200653SNC_HANDLE_NAMES(PCR_get, "GPCR");
654SNC_HANDLE_NAMES(PCR_set, "SPCR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100655
malattia@linux.it59b19102007-04-09 10:19:04 +0200656SNC_HANDLE_NAMES(CMI_get, "GCMI");
657SNC_HANDLE_NAMES(CMI_set, "SCMI");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100658
malattia@linux.it59b19102007-04-09 10:19:04 +0200659static struct sony_nc_value sony_nc_values[] = {
660 SNC_HANDLE(brightness_default, snc_brightness_def_get,
Mattia Dongili156c2212007-02-12 22:01:07 +0100661 snc_brightness_def_set, brightness_default_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200662 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
663 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
664 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100665 boolean_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200666 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100667 boolean_validate, 1),
Mattia Dongili044847e2007-07-16 02:34:33 +0900668 SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
669 boolean_validate, 0),
670 SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
671 boolean_validate, 0),
672 SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
673 boolean_validate, 0),
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100674 /* unknown methods */
malattia@linux.it59b19102007-04-09 10:19:04 +0200675 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
676 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
677 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
678 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
679 SNC_HANDLE_NULL
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100680};
681
malattia@linux.it59b19102007-04-09 10:19:04 +0200682static acpi_handle sony_nc_acpi_handle;
683static struct acpi_device *sony_nc_acpi_device = NULL;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100684
Mattia Dongilid78865c2007-02-07 20:01:56 +0100685/*
686 * acpi_evaluate_object wrappers
687 */
Stelian Pop7f09c432007-01-13 23:04:31 +0100688static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
689{
690 struct acpi_buffer output;
691 union acpi_object out_obj;
692 acpi_status status;
693
694 output.length = sizeof(out_obj);
695 output.pointer = &out_obj;
696
697 status = acpi_evaluate_object(handle, name, NULL, &output);
698 if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
699 *result = out_obj.integer.value;
700 return 0;
701 }
702
Mattia Dongilid6697932011-02-19 11:52:28 +0900703 pr_warn(DRV_PFX "acpi_callreadfunc failed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100704
705 return -1;
706}
707
708static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
709 int *result)
710{
711 struct acpi_object_list params;
712 union acpi_object in_obj;
713 struct acpi_buffer output;
714 union acpi_object out_obj;
715 acpi_status status;
716
717 params.count = 1;
718 params.pointer = &in_obj;
719 in_obj.type = ACPI_TYPE_INTEGER;
720 in_obj.integer.value = value;
721
722 output.length = sizeof(out_obj);
723 output.pointer = &out_obj;
724
725 status = acpi_evaluate_object(handle, name, &params, &output);
726 if (status == AE_OK) {
727 if (result != NULL) {
728 if (out_obj.type != ACPI_TYPE_INTEGER) {
Mattia Dongilid6697932011-02-19 11:52:28 +0900729 pr_warn(DRV_PFX "acpi_evaluate_object bad "
Stelian Pop7f09c432007-01-13 23:04:31 +0100730 "return type\n");
731 return -1;
732 }
733 *result = out_obj.integer.value;
734 }
735 return 0;
736 }
737
Mattia Dongilid6697932011-02-19 11:52:28 +0900738 pr_warn(DRV_PFX "acpi_evaluate_object failed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100739
740 return -1;
741}
742
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900743struct sony_nc_handles {
744 u16 cap[0x10];
745 struct device_attribute devattr;
746};
747
Dan Carpenterf11113b2011-02-26 15:54:27 +0300748static struct sony_nc_handles *handles;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900749
750static ssize_t sony_nc_handles_show(struct device *dev,
751 struct device_attribute *attr, char *buffer)
752{
753 ssize_t len = 0;
754 int i;
755
756 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
757 len += snprintf(buffer + len, PAGE_SIZE - len, "0x%.4x ",
758 handles->cap[i]);
759 }
760 len += snprintf(buffer + len, PAGE_SIZE - len, "\n");
761
762 return len;
763}
764
765static int sony_nc_handles_setup(struct platform_device *pd)
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900766{
767 int i;
768 int result;
769
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900770 handles = kzalloc(sizeof(*handles), GFP_KERNEL);
Dan Carpenter31f00752011-02-26 15:55:24 +0300771 if (!handles)
772 return -ENOMEM;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900773
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900774 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
775 if (!acpi_callsetfunc(sony_nc_acpi_handle,
776 "SN00", i + 0x20, &result)) {
777 dprintk("caching handle 0x%.4x (offset: 0x%.2x)\n",
778 result, i);
779 handles->cap[i] = result;
Mattia Dongili56e6e712011-02-19 11:52:25 +0900780 }
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900781 }
782
Mattia Dongili855b8bc2011-04-05 23:38:35 +0900783 if (debug) {
784 sysfs_attr_init(&handles->devattr.attr);
785 handles->devattr.attr.name = "handles";
786 handles->devattr.attr.mode = S_IRUGO;
787 handles->devattr.show = sony_nc_handles_show;
788
789 /* allow reading capabilities via sysfs */
790 if (device_create_file(&pd->dev, &handles->devattr)) {
791 kfree(handles);
792 handles = NULL;
793 return -1;
794 }
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900795 }
796
797 return 0;
798}
799
800static int sony_nc_handles_cleanup(struct platform_device *pd)
801{
802 if (handles) {
Mattia Dongili855b8bc2011-04-05 23:38:35 +0900803 if (debug)
804 device_remove_file(&pd->dev, &handles->devattr);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900805 kfree(handles);
806 handles = NULL;
807 }
808 return 0;
809}
810
811static int sony_find_snc_handle(int handle)
812{
813 int i;
Mattia Dongilifef34862011-04-02 19:00:44 +0900814
815 /* not initialized yet, return early */
816 if (!handles)
817 return -1;
818
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900819 for (i = 0; i < 0x10; i++) {
820 if (handles->cap[i] == handle) {
821 dprintk("found handle 0x%.4x (offset: 0x%.2x)\n",
822 handle, i);
823 return i;
824 }
825 }
Mattia Dongili56e6e712011-02-19 11:52:25 +0900826 dprintk("handle 0x%.4x not found\n", handle);
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900827 return -1;
828}
829
830static int sony_call_snc_handle(int handle, int argument, int *result)
831{
Mattia Dongili56e6e712011-02-19 11:52:25 +0900832 int ret = 0;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900833 int offset = sony_find_snc_handle(handle);
834
835 if (offset < 0)
836 return -1;
837
Mattia Dongili56e6e712011-02-19 11:52:25 +0900838 ret = acpi_callsetfunc(sony_nc_acpi_handle, "SN07", offset | argument,
839 result);
840 dprintk("called SN07 with 0x%.4x (result: 0x%.4x)\n", offset | argument,
841 *result);
842 return ret;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900843}
844
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100845/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200846 * sony_nc_values input/output validate functions
Mattia Dongili156c2212007-02-12 22:01:07 +0100847 */
848
849/* brightness_default_validate:
850 *
851 * manipulate input output values to keep consistency with the
852 * backlight framework for which brightness values are 0-based.
853 */
854static int brightness_default_validate(const int direction, const int value)
855{
856 switch (direction) {
857 case SNC_VALIDATE_OUT:
858 return value - 1;
859 case SNC_VALIDATE_IN:
860 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
861 return value + 1;
862 }
863 return -EINVAL;
864}
865
866/* boolean_validate:
867 *
868 * on input validate boolean values 0/1, on output just pass the
869 * received value.
870 */
871static int boolean_validate(const int direction, const int value)
872{
873 if (direction == SNC_VALIDATE_IN) {
874 if (value != 0 && value != 1)
875 return -EINVAL;
876 }
877 return value;
878}
879
880/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200881 * Sysfs show/store common to all sony_nc_values
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100882 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200883static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
Len Browna02d1c12007-02-07 15:34:02 -0500884 char *buffer)
Stelian Pop7f09c432007-01-13 23:04:31 +0100885{
Stelian Pop7f09c432007-01-13 23:04:31 +0100886 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200887 struct sony_nc_value *item =
888 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100889
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100890 if (!*item->acpiget)
Stelian Pop7f09c432007-01-13 23:04:31 +0100891 return -EIO;
892
malattia@linux.it59b19102007-04-09 10:19:04 +0200893 if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100894 return -EIO;
895
Mattia Dongili156c2212007-02-12 22:01:07 +0100896 if (item->validate)
897 value = item->validate(SNC_VALIDATE_OUT, value);
898
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100899 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
Stelian Pop7f09c432007-01-13 23:04:31 +0100900}
901
malattia@linux.it59b19102007-04-09 10:19:04 +0200902static ssize_t sony_nc_sysfs_store(struct device *dev,
Len Browna02d1c12007-02-07 15:34:02 -0500903 struct device_attribute *attr,
904 const char *buffer, size_t count)
Stelian Pop7f09c432007-01-13 23:04:31 +0100905{
Stelian Pop7f09c432007-01-13 23:04:31 +0100906 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200907 struct sony_nc_value *item =
908 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100909
910 if (!item->acpiset)
911 return -EIO;
912
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100913 if (count > 31)
914 return -EINVAL;
915
916 value = simple_strtoul(buffer, NULL, 10);
Stelian Pop7f09c432007-01-13 23:04:31 +0100917
Mattia Dongili156c2212007-02-12 22:01:07 +0100918 if (item->validate)
919 value = item->validate(SNC_VALIDATE_IN, value);
920
921 if (value < 0)
922 return value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100923
malattia@linux.it59b19102007-04-09 10:19:04 +0200924 if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100925 return -EIO;
Andrew Morton3f4f4612007-01-13 23:04:32 +0100926 item->value = value;
927 item->valid = 1;
Stelian Pop7f09c432007-01-13 23:04:31 +0100928 return count;
929}
930
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100931
Mattia Dongilid78865c2007-02-07 20:01:56 +0100932/*
933 * Backlight device
934 */
935static int sony_backlight_update_status(struct backlight_device *bd)
Andrew Morton3f4f4612007-01-13 23:04:32 +0100936{
malattia@linux.it59b19102007-04-09 10:19:04 +0200937 return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
Richard Purdie321709c2007-02-10 15:04:08 +0000938 bd->props.brightness + 1, NULL);
Andrew Morton3f4f4612007-01-13 23:04:32 +0100939}
940
Mattia Dongilid78865c2007-02-07 20:01:56 +0100941static int sony_backlight_get_brightness(struct backlight_device *bd)
942{
943 int value;
944
malattia@linux.it59b19102007-04-09 10:19:04 +0200945 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
Mattia Dongilid78865c2007-02-07 20:01:56 +0100946 return 0;
947 /* brightness levels are 1-based, while backlight ones are 0-based */
948 return value - 1;
949}
950
Mattia Dongili7751ab82011-02-19 11:52:32 +0900951static int sony_nc_get_brightness_ng(struct backlight_device *bd)
952{
953 int result;
954 int *handle = (int *)bl_get_data(bd);
955
956 sony_call_snc_handle(*handle, 0x0200, &result);
957
958 return result & 0xff;
959}
960
961static int sony_nc_update_status_ng(struct backlight_device *bd)
962{
963 int value, result;
964 int *handle = (int *)bl_get_data(bd);
965
966 value = bd->props.brightness;
967 sony_call_snc_handle(*handle, 0x0100 | (value << 16), &result);
968
969 return sony_nc_get_brightness_ng(bd);
970}
971
Lionel Debrouxacc24722010-11-16 14:14:02 +0100972static const struct backlight_ops sony_backlight_ops = {
Mattia Dongili7751ab82011-02-19 11:52:32 +0900973 .options = BL_CORE_SUSPENDRESUME,
Len Browna02d1c12007-02-07 15:34:02 -0500974 .update_status = sony_backlight_update_status,
975 .get_brightness = sony_backlight_get_brightness,
Mattia Dongilid78865c2007-02-07 20:01:56 +0100976};
Mattia Dongili7751ab82011-02-19 11:52:32 +0900977static const struct backlight_ops sony_backlight_ng_ops = {
978 .options = BL_CORE_SUSPENDRESUME,
979 .update_status = sony_nc_update_status_ng,
980 .get_brightness = sony_nc_get_brightness_ng,
981};
982static int backlight_ng_handle;
983static struct backlight_device *sony_backlight_device;
Mattia Dongilid78865c2007-02-07 20:01:56 +0100984
985/*
Mattia Dongili6315fd12007-07-16 02:34:35 +0900986 * New SNC-only Vaios event mapping to driver known keys
987 */
988struct sony_nc_event {
989 u8 data;
990 u8 event;
991};
992
Matthew Garrett45c79422009-03-26 21:58:16 +0900993static struct sony_nc_event sony_100_events[] = {
Matthew Garrett9b578962009-03-26 21:58:14 +0900994 { 0x90, SONYPI_EVENT_PKEY_P1 },
995 { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthias Welwarsky6479efb2009-04-01 22:10:45 +0900996 { 0x91, SONYPI_EVENT_PKEY_P2 },
Matthew Garrett9b578962009-03-26 21:58:14 +0900997 { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +0900998 { 0x81, SONYPI_EVENT_FNKEY_F1 },
999 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
Anton Veretenenkof5acf5e2009-03-26 22:44:26 +09001000 { 0x82, SONYPI_EVENT_FNKEY_F2 },
1001 { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
1002 { 0x83, SONYPI_EVENT_FNKEY_F3 },
1003 { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
1004 { 0x84, SONYPI_EVENT_FNKEY_F4 },
1005 { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001006 { 0x85, SONYPI_EVENT_FNKEY_F5 },
1007 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
1008 { 0x86, SONYPI_EVENT_FNKEY_F6 },
1009 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
1010 { 0x87, SONYPI_EVENT_FNKEY_F7 },
1011 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +09001012 { 0x89, SONYPI_EVENT_FNKEY_F9 },
1013 { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001014 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
1015 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
1016 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
1017 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +09001018 { 0x9d, SONYPI_EVENT_ZOOM_PRESSED },
1019 { 0x1d, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +09001020 { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
1021 { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili4f924ba2009-12-17 00:08:33 +09001022 { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
1023 { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +09001024 { 0xa4, SONYPI_EVENT_CD_EJECT_PRESSED },
1025 { 0x24, SONYPI_EVENT_ANYBUTTON_RELEASED },
1026 { 0xa5, SONYPI_EVENT_VENDOR_PRESSED },
1027 { 0x25, SONYPI_EVENT_ANYBUTTON_RELEASED },
1028 { 0xa6, SONYPI_EVENT_HELP_PRESSED },
1029 { 0x26, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001030 { 0, 0 },
1031};
1032
Matthew Garrett45c79422009-03-26 21:58:16 +09001033static struct sony_nc_event sony_127_events[] = {
1034 { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
1035 { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
1036 { 0x82, SONYPI_EVENT_PKEY_P1 },
1037 { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
1038 { 0x83, SONYPI_EVENT_PKEY_P2 },
1039 { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
1040 { 0x84, SONYPI_EVENT_PKEY_P3 },
1041 { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
1042 { 0x85, SONYPI_EVENT_PKEY_P4 },
1043 { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
1044 { 0x86, SONYPI_EVENT_PKEY_P5 },
1045 { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett45c79422009-03-26 21:58:16 +09001046 { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
1047 { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
1048 { 0, 0 },
1049};
1050
Mattia Dongili6315fd12007-07-16 02:34:35 +09001051/*
Mattia Dongilid78865c2007-02-07 20:01:56 +01001052 * ACPI callbacks
1053 */
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001054static void sony_nc_notify(struct acpi_device *device, u32 event)
Stelian Pop7f09c432007-01-13 23:04:31 +01001055{
Mattia Dongili6315fd12007-07-16 02:34:35 +09001056 u32 ev = event;
Mattia Dongili6315fd12007-07-16 02:34:35 +09001057
Matthew Garrett9b578962009-03-26 21:58:14 +09001058 if (ev >= 0x90) {
1059 /* New-style event */
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001060 int result;
Matthew Garrett45c79422009-03-26 21:58:16 +09001061 int key_handle = 0;
Matthew Garrett9b578962009-03-26 21:58:14 +09001062 ev -= 0x90;
Mattia Dongili6315fd12007-07-16 02:34:35 +09001063
Matthew Garrett45c79422009-03-26 21:58:16 +09001064 if (sony_find_snc_handle(0x100) == ev)
1065 key_handle = 0x100;
1066 if (sony_find_snc_handle(0x127) == ev)
1067 key_handle = 0x127;
Matthew Garrett9b578962009-03-26 21:58:14 +09001068
Matthias Welwarsky6479efb2009-04-01 22:10:45 +09001069 if (key_handle) {
Matthew Garrett45c79422009-03-26 21:58:16 +09001070 struct sony_nc_event *key_event;
1071
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001072 if (sony_call_snc_handle(key_handle, 0x200, &result)) {
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001073 dprintk("sony_nc_notify, unable to decode"
Matthew Garrett45c79422009-03-26 21:58:16 +09001074 " event 0x%.2x 0x%.2x\n", key_handle,
1075 ev);
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001076 /* restore the original event */
1077 ev = event;
1078 } else {
Matthew Garrett9b578962009-03-26 21:58:14 +09001079 ev = result & 0xFF;
1080
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001081 if (key_handle == 0x100)
1082 key_event = sony_100_events;
1083 else
1084 key_event = sony_127_events;
Matthew Garrett45c79422009-03-26 21:58:16 +09001085
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001086 for (; key_event->data; key_event++) {
1087 if (key_event->data == ev) {
1088 ev = key_event->event;
1089 break;
1090 }
Matthew Garrett9b578962009-03-26 21:58:14 +09001091 }
Mattia Dongili6315fd12007-07-16 02:34:35 +09001092
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001093 if (!key_event->data)
Mattia Dongilid6697932011-02-19 11:52:28 +09001094 pr_info(DRV_PFX
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001095 "Unknown event: 0x%x 0x%x\n",
1096 key_handle,
1097 ev);
1098 else
1099 sony_laptop_report_input_event(ev);
Matthew Garrett45c79422009-03-26 21:58:16 +09001100 }
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001101 } else if (sony_find_snc_handle(sony_rfkill_handle) == ev) {
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001102 sony_nc_rfkill_update();
1103 return;
Matthew Garrett9b578962009-03-26 21:58:14 +09001104 }
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001105 } else
1106 sony_laptop_report_input_event(ev);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001107
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001108 dprintk("sony_nc_notify, event: 0x%.2x\n", ev);
Len Brown14e04fb2007-08-23 15:20:26 -04001109 acpi_bus_generate_proc_event(sony_nc_acpi_device, 1, ev);
Stelian Pop7f09c432007-01-13 23:04:31 +01001110}
1111
1112static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
1113 void *context, void **return_value)
1114{
Lin Ming30823732008-12-16 16:59:35 +08001115 struct acpi_device_info *info;
Stelian Pop7f09c432007-01-13 23:04:31 +01001116
Bob Moore15b8dd52009-06-29 13:39:29 +08001117 if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001118 pr_warn(DRV_PFX "method: name: %4.4s, args %X\n",
Lin Ming30823732008-12-16 16:59:35 +08001119 (char *)&info->name, info->param_count);
1120
Bob Moore15b8dd52009-06-29 13:39:29 +08001121 kfree(info);
Lin Ming30823732008-12-16 16:59:35 +08001122 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001123
1124 return AE_OK;
1125}
1126
Mattia Dongilid78865c2007-02-07 20:01:56 +01001127/*
1128 * ACPI device
1129 */
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001130static int sony_nc_function_setup(struct acpi_device *device)
1131{
1132 int result;
1133
1134 /* Enable all events */
1135 acpi_callsetfunc(sony_nc_acpi_handle, "SN02", 0xffff, &result);
1136
1137 /* Setup hotkeys */
1138 sony_call_snc_handle(0x0100, 0, &result);
1139 sony_call_snc_handle(0x0101, 0, &result);
1140 sony_call_snc_handle(0x0102, 0x100, &result);
Almer S. Tigelaar560e84a2009-04-12 11:26:27 +00001141 sony_call_snc_handle(0x0127, 0, &result);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001142
1143 return 0;
1144}
1145
malattia@linux.it59b19102007-04-09 10:19:04 +02001146static int sony_nc_resume(struct acpi_device *device)
Mattia Dongilid78865c2007-02-07 20:01:56 +01001147{
malattia@linux.it59b19102007-04-09 10:19:04 +02001148 struct sony_nc_value *item;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001149 acpi_handle handle;
Mattia Dongilid78865c2007-02-07 20:01:56 +01001150
malattia@linux.it59b19102007-04-09 10:19:04 +02001151 for (item = sony_nc_values; item->name; item++) {
Mattia Dongilid78865c2007-02-07 20:01:56 +01001152 int ret;
1153
1154 if (!item->valid)
1155 continue;
malattia@linux.it59b19102007-04-09 10:19:04 +02001156 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
Len Browna02d1c12007-02-07 15:34:02 -05001157 item->value, NULL);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001158 if (ret < 0) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001159 pr_err(DRV_PFX "%s: %d\n", __func__, ret);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001160 break;
1161 }
1162 }
Mattia Dongili6315fd12007-07-16 02:34:35 +09001163
Matthew Garrett82734bf2009-03-26 21:58:13 +09001164 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1165 &handle))) {
1166 if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
1167 dprintk("ECON Method failed\n");
1168 }
1169
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001170 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1171 &handle))) {
1172 dprintk("Doing SNC setup\n");
1173 sony_nc_function_setup(device);
1174 }
1175
Alan Jenkinsa0d97d62009-09-25 10:18:21 +01001176 /* re-read rfkill state */
1177 sony_nc_rfkill_update();
1178
Mattia Dongilid78865c2007-02-07 20:01:56 +01001179 return 0;
1180}
1181
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001182static void sony_nc_rfkill_cleanup(void)
1183{
1184 int i;
1185
Johannes Berg19d337d2009-06-02 13:01:37 +02001186 for (i = 0; i < N_SONY_RFKILL; i++) {
1187 if (sony_rfkill_devices[i]) {
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001188 rfkill_unregister(sony_rfkill_devices[i]);
Johannes Berg19d337d2009-06-02 13:01:37 +02001189 rfkill_destroy(sony_rfkill_devices[i]);
1190 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001191 }
1192}
1193
Johannes Berg19d337d2009-06-02 13:01:37 +02001194static int sony_nc_rfkill_set(void *data, bool blocked)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001195{
1196 int result;
1197 int argument = sony_rfkill_address[(long) data] + 0x100;
1198
Johannes Berg19d337d2009-06-02 13:01:37 +02001199 if (!blocked)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001200 argument |= 0xff0000;
1201
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001202 return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001203}
1204
Johannes Berg19d337d2009-06-02 13:01:37 +02001205static const struct rfkill_ops sony_rfkill_ops = {
1206 .set_block = sony_nc_rfkill_set,
1207};
1208
1209static int sony_nc_setup_rfkill(struct acpi_device *device,
1210 enum sony_nc_rfkill nc_type)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001211{
1212 int err = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001213 struct rfkill *rfk;
1214 enum rfkill_type type;
1215 const char *name;
Alan Jenkins50fab072009-09-24 20:15:24 +01001216 int result;
1217 bool hwblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001218
Johannes Berg19d337d2009-06-02 13:01:37 +02001219 switch (nc_type) {
1220 case SONY_WIFI:
1221 type = RFKILL_TYPE_WLAN;
1222 name = "sony-wifi";
1223 break;
1224 case SONY_BLUETOOTH:
1225 type = RFKILL_TYPE_BLUETOOTH;
1226 name = "sony-bluetooth";
1227 break;
1228 case SONY_WWAN:
1229 type = RFKILL_TYPE_WWAN;
1230 name = "sony-wwan";
1231 break;
1232 case SONY_WIMAX:
1233 type = RFKILL_TYPE_WIMAX;
1234 name = "sony-wimax";
1235 break;
1236 default:
1237 return -EINVAL;
Mattia Dongili53005a02009-04-12 11:26:31 +00001238 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001239
Johannes Berg19d337d2009-06-02 13:01:37 +02001240 rfk = rfkill_alloc(name, &device->dev, type,
1241 &sony_rfkill_ops, (void *)nc_type);
1242 if (!rfk)
1243 return -ENOMEM;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001244
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001245 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
Alan Jenkins50fab072009-09-24 20:15:24 +01001246 hwblock = !(result & 0x1);
1247 rfkill_set_hw_state(rfk, hwblock);
1248
Johannes Berg19d337d2009-06-02 13:01:37 +02001249 err = rfkill_register(rfk);
1250 if (err) {
1251 rfkill_destroy(rfk);
1252 return err;
Mattia Dongili53005a02009-04-12 11:26:31 +00001253 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001254 sony_rfkill_devices[nc_type] = rfk;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001255 return err;
1256}
1257
Randy Dunlapa46a7802011-01-08 19:56:44 -08001258static void sony_nc_rfkill_update(void)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001259{
Johannes Berg19d337d2009-06-02 13:01:37 +02001260 enum sony_nc_rfkill i;
1261 int result;
1262 bool hwblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001263
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001264 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001265 hwblock = !(result & 0x1);
1266
1267 for (i = 0; i < N_SONY_RFKILL; i++) {
1268 int argument = sony_rfkill_address[i];
1269
1270 if (!sony_rfkill_devices[i])
1271 continue;
1272
1273 if (hwblock) {
Johannes Berge1f8a192009-06-11 12:08:15 +02001274 if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
1275 /* we already know we're blocked */
1276 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001277 continue;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001278 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001279
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001280 sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001281 rfkill_set_states(sony_rfkill_devices[i],
1282 !(result & 0xf), false);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001283 }
1284}
1285
Mattia Dongili528809c2009-12-17 00:08:36 +09001286static void sony_nc_rfkill_setup(struct acpi_device *device)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001287{
Mattia Dongili528809c2009-12-17 00:08:36 +09001288 int offset;
1289 u8 dev_code, i;
1290 acpi_status status;
1291 struct acpi_object_list params;
1292 union acpi_object in_obj;
1293 union acpi_object *device_enum;
1294 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001295
Mattia Dongili528809c2009-12-17 00:08:36 +09001296 offset = sony_find_snc_handle(0x124);
1297 if (offset == -1) {
1298 offset = sony_find_snc_handle(0x135);
1299 if (offset == -1)
1300 return;
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001301 else
1302 sony_rfkill_handle = 0x135;
1303 } else
1304 sony_rfkill_handle = 0x124;
Mattia Dongili528809c2009-12-17 00:08:36 +09001305 dprintk("Found rkfill handle: 0x%.4x\n", sony_rfkill_handle);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001306
Mattia Dongili528809c2009-12-17 00:08:36 +09001307 /* need to read the whole buffer returned by the acpi call to SN06
1308 * here otherwise we may miss some features
1309 */
1310 params.count = 1;
1311 params.pointer = &in_obj;
1312 in_obj.type = ACPI_TYPE_INTEGER;
1313 in_obj.integer.value = offset;
1314 status = acpi_evaluate_object(sony_nc_acpi_handle, "SN06", &params,
1315 &buffer);
1316 if (ACPI_FAILURE(status)) {
1317 dprintk("Radio device enumeration failed\n");
1318 return;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001319 }
1320
Mattia Dongili528809c2009-12-17 00:08:36 +09001321 device_enum = (union acpi_object *) buffer.pointer;
Dan Carpenter200140b2011-02-27 17:13:25 +03001322 if (!device_enum) {
1323 pr_err(DRV_PFX "No SN06 return object.");
1324 goto out_no_enum;
1325 }
1326 if (device_enum->type != ACPI_TYPE_BUFFER) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001327 pr_err(DRV_PFX "Invalid SN06 return object 0x%.2x\n",
1328 device_enum->type);
Mattia Dongili528809c2009-12-17 00:08:36 +09001329 goto out_no_enum;
1330 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001331
Mattia Dongili528809c2009-12-17 00:08:36 +09001332 /* the buffer is filled with magic numbers describing the devices
1333 * available, 0xff terminates the enumeration
1334 */
Dmitry Torokhovc14973f2010-01-10 00:15:44 -08001335 for (i = 0; i < device_enum->buffer.length; i++) {
1336
1337 dev_code = *(device_enum->buffer.pointer + i);
1338 if (dev_code == 0xff)
1339 break;
1340
Mattia Dongili528809c2009-12-17 00:08:36 +09001341 dprintk("Radio devices, looking at 0x%.2x\n", dev_code);
1342
1343 if (dev_code == 0 && !sony_rfkill_devices[SONY_WIFI])
1344 sony_nc_setup_rfkill(device, SONY_WIFI);
1345
1346 if (dev_code == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
1347 sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
1348
1349 if ((0xf0 & dev_code) == 0x20 &&
1350 !sony_rfkill_devices[SONY_WWAN])
1351 sony_nc_setup_rfkill(device, SONY_WWAN);
1352
1353 if (dev_code == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
1354 sony_nc_setup_rfkill(device, SONY_WIMAX);
1355 }
1356
1357out_no_enum:
1358 kfree(buffer.pointer);
1359 return;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001360}
1361
Mattia Dongilibf155712011-02-19 11:52:31 +09001362/* Keyboard backlight feature */
1363#define KBDBL_HANDLER 0x137
1364#define KBDBL_PRESENT 0xB00
1365#define SET_MODE 0xC00
1366#define SET_TIMEOUT 0xE00
1367
1368struct kbd_backlight {
1369 int mode;
1370 int timeout;
1371 struct device_attribute mode_attr;
1372 struct device_attribute timeout_attr;
1373};
1374
Dan Carpenterf11113b2011-02-26 15:54:27 +03001375static struct kbd_backlight *kbdbl_handle;
Mattia Dongilibf155712011-02-19 11:52:31 +09001376
1377static ssize_t __sony_nc_kbd_backlight_mode_set(u8 value)
1378{
1379 int result;
1380
1381 if (value > 1)
1382 return -EINVAL;
1383
1384 if (sony_call_snc_handle(KBDBL_HANDLER,
1385 (value << 0x10) | SET_MODE, &result))
1386 return -EIO;
1387
1388 kbdbl_handle->mode = value;
1389
1390 return 0;
1391}
1392
1393static ssize_t sony_nc_kbd_backlight_mode_store(struct device *dev,
1394 struct device_attribute *attr,
1395 const char *buffer, size_t count)
1396{
1397 int ret = 0;
1398 unsigned long value;
1399
1400 if (count > 31)
1401 return -EINVAL;
1402
1403 if (strict_strtoul(buffer, 10, &value))
1404 return -EINVAL;
1405
1406 ret = __sony_nc_kbd_backlight_mode_set(value);
1407 if (ret < 0)
1408 return ret;
1409
1410 return count;
1411}
1412
1413static ssize_t sony_nc_kbd_backlight_mode_show(struct device *dev,
1414 struct device_attribute *attr, char *buffer)
1415{
1416 ssize_t count = 0;
1417 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_handle->mode);
1418 return count;
1419}
1420
1421static int __sony_nc_kbd_backlight_timeout_set(u8 value)
1422{
1423 int result;
1424
1425 if (value > 3)
1426 return -EINVAL;
1427
1428 if (sony_call_snc_handle(KBDBL_HANDLER,
1429 (value << 0x10) | SET_TIMEOUT, &result))
1430 return -EIO;
1431
1432 kbdbl_handle->timeout = value;
1433
1434 return 0;
1435}
1436
1437static ssize_t sony_nc_kbd_backlight_timeout_store(struct device *dev,
1438 struct device_attribute *attr,
1439 const char *buffer, size_t count)
1440{
1441 int ret = 0;
1442 unsigned long value;
1443
1444 if (count > 31)
1445 return -EINVAL;
1446
1447 if (strict_strtoul(buffer, 10, &value))
1448 return -EINVAL;
1449
1450 ret = __sony_nc_kbd_backlight_timeout_set(value);
1451 if (ret < 0)
1452 return ret;
1453
1454 return count;
1455}
1456
1457static ssize_t sony_nc_kbd_backlight_timeout_show(struct device *dev,
1458 struct device_attribute *attr, char *buffer)
1459{
1460 ssize_t count = 0;
1461 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_handle->timeout);
1462 return count;
1463}
1464
1465static int sony_nc_kbd_backlight_setup(struct platform_device *pd)
1466{
1467 int result;
1468
1469 if (sony_call_snc_handle(0x137, KBDBL_PRESENT, &result))
1470 return 0;
1471 if (!(result & 0x02))
1472 return 0;
1473
1474 kbdbl_handle = kzalloc(sizeof(*kbdbl_handle), GFP_KERNEL);
Dan Carpenter31f00752011-02-26 15:55:24 +03001475 if (!kbdbl_handle)
1476 return -ENOMEM;
Mattia Dongilibf155712011-02-19 11:52:31 +09001477
1478 sysfs_attr_init(&kbdbl_handle->mode_attr.attr);
1479 kbdbl_handle->mode_attr.attr.name = "kbd_backlight";
1480 kbdbl_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
1481 kbdbl_handle->mode_attr.show = sony_nc_kbd_backlight_mode_show;
1482 kbdbl_handle->mode_attr.store = sony_nc_kbd_backlight_mode_store;
1483
1484 sysfs_attr_init(&kbdbl_handle->timeout_attr.attr);
1485 kbdbl_handle->timeout_attr.attr.name = "kbd_backlight_timeout";
1486 kbdbl_handle->timeout_attr.attr.mode = S_IRUGO | S_IWUSR;
1487 kbdbl_handle->timeout_attr.show = sony_nc_kbd_backlight_timeout_show;
1488 kbdbl_handle->timeout_attr.store = sony_nc_kbd_backlight_timeout_store;
1489
1490 if (device_create_file(&pd->dev, &kbdbl_handle->mode_attr))
1491 goto outkzalloc;
1492
1493 if (device_create_file(&pd->dev, &kbdbl_handle->timeout_attr))
1494 goto outmode;
1495
1496 __sony_nc_kbd_backlight_mode_set(kbd_backlight);
1497 __sony_nc_kbd_backlight_timeout_set(kbd_backlight_timeout);
1498
1499 return 0;
1500
1501outmode:
1502 device_remove_file(&pd->dev, &kbdbl_handle->mode_attr);
1503outkzalloc:
1504 kfree(kbdbl_handle);
1505 kbdbl_handle = NULL;
1506 return -1;
1507}
1508
1509static int sony_nc_kbd_backlight_cleanup(struct platform_device *pd)
1510{
1511 if (kbdbl_handle) {
1512 device_remove_file(&pd->dev, &kbdbl_handle->mode_attr);
1513 device_remove_file(&pd->dev, &kbdbl_handle->timeout_attr);
1514 kfree(kbdbl_handle);
1515 }
1516 return 0;
1517}
1518
Mattia Dongili7751ab82011-02-19 11:52:32 +09001519static void sony_nc_backlight_setup(void)
1520{
1521 acpi_handle unused;
1522 int max_brightness = 0;
1523 const struct backlight_ops *ops = NULL;
1524 struct backlight_properties props;
1525
1526 if (sony_find_snc_handle(0x12f) != -1) {
1527 backlight_ng_handle = 0x12f;
1528 ops = &sony_backlight_ng_ops;
1529 max_brightness = 0xff;
1530
1531 } else if (sony_find_snc_handle(0x137) != -1) {
1532 backlight_ng_handle = 0x137;
1533 ops = &sony_backlight_ng_ops;
1534 max_brightness = 0xff;
1535
1536 } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
1537 &unused))) {
1538 ops = &sony_backlight_ops;
1539 max_brightness = SONY_MAX_BRIGHTNESS - 1;
1540
1541 } else
1542 return;
1543
1544 memset(&props, 0, sizeof(struct backlight_properties));
1545 props.type = BACKLIGHT_PLATFORM;
1546 props.max_brightness = max_brightness;
1547 sony_backlight_device = backlight_device_register("sony", NULL,
1548 &backlight_ng_handle,
1549 ops, &props);
1550
1551 if (IS_ERR(sony_backlight_device)) {
1552 pr_warning(DRV_PFX "unable to register backlight device\n");
1553 sony_backlight_device = NULL;
1554 } else
1555 sony_backlight_device->props.brightness =
1556 ops->get_brightness(sony_backlight_device);
1557}
1558
1559static void sony_nc_backlight_cleanup(void)
1560{
1561 if (sony_backlight_device)
1562 backlight_device_unregister(sony_backlight_device);
1563}
1564
malattia@linux.it59b19102007-04-09 10:19:04 +02001565static int sony_nc_add(struct acpi_device *device)
Stelian Pop7f09c432007-01-13 23:04:31 +01001566{
1567 acpi_status status;
Andrew Morton8607c672007-03-06 02:29:42 -08001568 int result = 0;
Alessandro Guido50f62af2007-01-13 23:04:34 +01001569 acpi_handle handle;
malattia@linux.it56b87562007-04-09 10:19:05 +02001570 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01001571
Mattia Dongilid6697932011-02-19 11:52:28 +09001572 pr_info(DRV_PFX "%s v%s.\n", SONY_NC_DRIVER_NAME,
1573 SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.itf6119b02007-04-09 19:31:06 +02001574
malattia@linux.it59b19102007-04-09 10:19:04 +02001575 sony_nc_acpi_device = device;
malattia@linux.it33a04452007-04-09 19:26:03 +02001576 strcpy(acpi_device_class(device), "sony/hotkey");
Stelian Popc5611622007-01-13 23:04:37 +01001577
malattia@linux.it59b19102007-04-09 10:19:04 +02001578 sony_nc_acpi_handle = device->handle;
Stelian Pop7f09c432007-01-13 23:04:31 +01001579
Mattia Dongilib25b7322007-07-16 02:34:36 +09001580 /* read device status */
1581 result = acpi_bus_get_status(device);
1582 /* bail IFF the above call was successful and the device is not present */
1583 if (!result && !device->status.present) {
1584 dprintk("Device not present\n");
1585 result = -ENODEV;
1586 goto outwalk;
1587 }
1588
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001589 result = sony_pf_add();
1590 if (result)
1591 goto outpresent;
1592
Stelian Pop7f09c432007-01-13 23:04:31 +01001593 if (debug) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001594 status = acpi_walk_namespace(ACPI_TYPE_METHOD,
1595 sony_nc_acpi_handle, 1, sony_walk_callback,
1596 NULL, NULL, NULL);
Stelian Pop7f09c432007-01-13 23:04:31 +01001597 if (ACPI_FAILURE(status)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001598 pr_warn(DRV_PFX "unable to walk acpi resources\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001599 result = -ENODEV;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001600 goto outpresent;
Stelian Pop7f09c432007-01-13 23:04:31 +01001601 }
Stelian Popc5611622007-01-13 23:04:37 +01001602 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001603
Matthew Garrett82734bf2009-03-26 21:58:13 +09001604 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1605 &handle))) {
1606 if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
1607 dprintk("ECON Method failed\n");
1608 }
1609
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001610 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1611 &handle))) {
1612 dprintk("Doing SNC setup\n");
Dan Carpenter7227ded2011-02-26 15:54:57 +03001613 result = sony_nc_handles_setup(sony_pf_device);
1614 if (result)
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001615 goto outpresent;
Dan Carpenter7227ded2011-02-26 15:54:57 +03001616 result = sony_nc_kbd_backlight_setup(sony_pf_device);
1617 if (result)
Mattia Dongilibf155712011-02-19 11:52:31 +09001618 goto outsnc;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001619 sony_nc_function_setup(device);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001620 sony_nc_rfkill_setup(device);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001621 }
1622
malattia@linux.it1549ee62007-04-09 10:19:08 +02001623 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05001624 result = sony_laptop_setup_input(device);
malattia@linux.it1549ee62007-04-09 10:19:08 +02001625 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001626 pr_err(DRV_PFX "Unable to create input devices.\n");
Mattia Dongilibf155712011-02-19 11:52:31 +09001627 goto outkbdbacklight;
malattia@linux.it1549ee62007-04-09 10:19:08 +02001628 }
1629
Alessandro Guido38cfc142008-11-12 23:03:28 +01001630 if (acpi_video_backlight_support()) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001631 pr_info(DRV_PFX "brightness ignored, must be "
Thomas Renninger540b8bb2008-08-01 17:38:02 +02001632 "controlled by ACPI video driver\n");
Mattia Dongili7751ab82011-02-19 11:52:32 +09001633 } else {
1634 sony_nc_backlight_setup();
Alessandro Guido50f62af2007-01-13 23:04:34 +01001635 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001636
malattia@linux.it56b87562007-04-09 10:19:05 +02001637 /* create sony_pf sysfs attributes related to the SNC device */
1638 for (item = sony_nc_values; item->name; ++item) {
1639
1640 if (!debug && item->debug)
1641 continue;
1642
1643 /* find the available acpiget as described in the DSDT */
1644 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
1645 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1646 *item->acpiget,
1647 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02001648 dprintk("Found %s getter: %s\n",
1649 item->name, *item->acpiget);
malattia@linux.it56b87562007-04-09 10:19:05 +02001650 item->devattr.attr.mode |= S_IRUGO;
1651 break;
1652 }
1653 }
1654
1655 /* find the available acpiset as described in the DSDT */
1656 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
1657 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1658 *item->acpiset,
1659 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02001660 dprintk("Found %s setter: %s\n",
1661 item->name, *item->acpiset);
malattia@linux.it56b87562007-04-09 10:19:05 +02001662 item->devattr.attr.mode |= S_IWUSR;
1663 break;
1664 }
1665 }
1666
1667 if (item->devattr.attr.mode != 0) {
1668 result =
1669 device_create_file(&sony_pf_device->dev,
1670 &item->devattr);
1671 if (result)
1672 goto out_sysfs;
1673 }
1674 }
1675
Stelian Pop7f09c432007-01-13 23:04:31 +01001676 return 0;
1677
malattia@linux.it56b87562007-04-09 10:19:05 +02001678 out_sysfs:
1679 for (item = sony_nc_values; item->name; ++item) {
1680 device_remove_file(&sony_pf_device->dev, &item->devattr);
1681 }
Mattia Dongili7751ab82011-02-19 11:52:32 +09001682 sony_nc_backlight_cleanup();
Mattia Dongili7df03b82007-01-13 23:04:41 +01001683
malattia@linux.it1549ee62007-04-09 10:19:08 +02001684 sony_laptop_remove_input();
1685
Mattia Dongilibf155712011-02-19 11:52:31 +09001686 outkbdbacklight:
1687 sony_nc_kbd_backlight_cleanup(sony_pf_device);
1688
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001689 outsnc:
1690 sony_nc_handles_cleanup(sony_pf_device);
1691
1692 outpresent:
1693 sony_pf_remove();
1694
Len Browna02d1c12007-02-07 15:34:02 -05001695 outwalk:
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001696 sony_nc_rfkill_cleanup();
Stelian Pop7f09c432007-01-13 23:04:31 +01001697 return result;
1698}
1699
malattia@linux.it59b19102007-04-09 10:19:04 +02001700static int sony_nc_remove(struct acpi_device *device, int type)
Stelian Pop7f09c432007-01-13 23:04:31 +01001701{
malattia@linux.it56b87562007-04-09 10:19:05 +02001702 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01001703
Mattia Dongili7751ab82011-02-19 11:52:32 +09001704 sony_nc_backlight_cleanup();
Alessandro Guido50f62af2007-01-13 23:04:34 +01001705
malattia@linux.it59b19102007-04-09 10:19:04 +02001706 sony_nc_acpi_device = NULL;
Stelian Popc5611622007-01-13 23:04:37 +01001707
malattia@linux.it56b87562007-04-09 10:19:05 +02001708 for (item = sony_nc_values; item->name; ++item) {
1709 device_remove_file(&sony_pf_device->dev, &item->devattr);
1710 }
1711
Mattia Dongilibf155712011-02-19 11:52:31 +09001712 sony_nc_kbd_backlight_cleanup(sony_pf_device);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001713 sony_nc_handles_cleanup(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +02001714 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +02001715 sony_laptop_remove_input();
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001716 sony_nc_rfkill_cleanup();
malattia@linux.itf6119b02007-04-09 19:31:06 +02001717 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001718
1719 return 0;
1720}
1721
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001722static const struct acpi_device_id sony_device_ids[] = {
1723 {SONY_NC_HID, 0},
1724 {SONY_PIC_HID, 0},
1725 {"", 0},
1726};
1727MODULE_DEVICE_TABLE(acpi, sony_device_ids);
1728
1729static const struct acpi_device_id sony_nc_device_ids[] = {
1730 {SONY_NC_HID, 0},
1731 {"", 0},
1732};
1733
malattia@linux.it59b19102007-04-09 10:19:04 +02001734static struct acpi_driver sony_nc_driver = {
1735 .name = SONY_NC_DRIVER_NAME,
1736 .class = SONY_NC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001737 .ids = sony_nc_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02001738 .owner = THIS_MODULE,
Len Browna02d1c12007-02-07 15:34:02 -05001739 .ops = {
malattia@linux.it59b19102007-04-09 10:19:04 +02001740 .add = sony_nc_add,
1741 .remove = sony_nc_remove,
1742 .resume = sony_nc_resume,
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001743 .notify = sony_nc_notify,
Len Browna02d1c12007-02-07 15:34:02 -05001744 },
Andrew Morton3f4f4612007-01-13 23:04:32 +01001745};
1746
malattia@linux.it33a04452007-04-09 19:26:03 +02001747/*********** SPIC (SNY6001) Device ***********/
1748
1749#define SONYPI_DEVICE_TYPE1 0x00000001
1750#define SONYPI_DEVICE_TYPE2 0x00000002
1751#define SONYPI_DEVICE_TYPE3 0x00000004
1752
Mattia Dongili22a17782007-07-16 02:34:39 +09001753#define SONYPI_TYPE1_OFFSET 0x04
1754#define SONYPI_TYPE2_OFFSET 0x12
1755#define SONYPI_TYPE3_OFFSET 0x12
malattia@linux.it33a04452007-04-09 19:26:03 +02001756
malattia@linux.it33a04452007-04-09 19:26:03 +02001757struct sony_pic_ioport {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001758 struct acpi_resource_io io1;
1759 struct acpi_resource_io io2;
malattia@linux.it33a04452007-04-09 19:26:03 +02001760 struct list_head list;
1761};
1762
1763struct sony_pic_irq {
1764 struct acpi_resource_irq irq;
1765 struct list_head list;
1766};
1767
Mattia Dongilide920432008-01-14 18:05:43 +09001768struct sonypi_eventtypes {
1769 u8 data;
1770 unsigned long mask;
1771 struct sonypi_event *events;
1772};
1773
malattia@linux.it33a04452007-04-09 19:26:03 +02001774struct sony_pic_dev {
Mattia Dongili31df7142009-09-16 00:05:29 +09001775 struct acpi_device *acpi_dev;
1776 struct sony_pic_irq *cur_irq;
1777 struct sony_pic_ioport *cur_ioport;
1778 struct list_head interrupts;
1779 struct list_head ioports;
1780 struct mutex lock;
1781 struct sonypi_eventtypes *event_types;
1782 int (*handle_irq)(const u8, const u8);
1783 int model;
1784 u16 evport_offset;
1785 u8 camera_power;
1786 u8 bluetooth_power;
1787 u8 wwan_power;
malattia@linux.it33a04452007-04-09 19:26:03 +02001788};
1789
1790static struct sony_pic_dev spic_dev = {
1791 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
1792 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
1793};
1794
Alan Jenkins5e6f9722009-09-16 00:05:32 +09001795static int spic_drv_registered;
1796
malattia@linux.it33a04452007-04-09 19:26:03 +02001797/* Event masks */
1798#define SONYPI_JOGGER_MASK 0x00000001
1799#define SONYPI_CAPTURE_MASK 0x00000002
1800#define SONYPI_FNKEY_MASK 0x00000004
1801#define SONYPI_BLUETOOTH_MASK 0x00000008
1802#define SONYPI_PKEY_MASK 0x00000010
1803#define SONYPI_BACK_MASK 0x00000020
1804#define SONYPI_HELP_MASK 0x00000040
1805#define SONYPI_LID_MASK 0x00000080
1806#define SONYPI_ZOOM_MASK 0x00000100
1807#define SONYPI_THUMBPHRASE_MASK 0x00000200
1808#define SONYPI_MEYE_MASK 0x00000400
1809#define SONYPI_MEMORYSTICK_MASK 0x00000800
1810#define SONYPI_BATTERY_MASK 0x00001000
1811#define SONYPI_WIRELESS_MASK 0x00002000
1812
1813struct sonypi_event {
1814 u8 data;
1815 u8 event;
1816};
1817
1818/* The set of possible button release events */
1819static struct sonypi_event sonypi_releaseev[] = {
1820 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
1821 { 0, 0 }
1822};
1823
1824/* The set of possible jogger events */
1825static struct sonypi_event sonypi_joggerev[] = {
1826 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
1827 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
1828 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
1829 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
1830 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
1831 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
1832 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
1833 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
1834 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
1835 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
1836 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
1837 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
1838 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
1839 { 0, 0 }
1840};
1841
1842/* The set of possible capture button events */
1843static struct sonypi_event sonypi_captureev[] = {
1844 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
1845 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09001846 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02001847 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
1848 { 0, 0 }
1849};
1850
1851/* The set of possible fnkeys events */
1852static struct sonypi_event sonypi_fnkeyev[] = {
1853 { 0x10, SONYPI_EVENT_FNKEY_ESC },
1854 { 0x11, SONYPI_EVENT_FNKEY_F1 },
1855 { 0x12, SONYPI_EVENT_FNKEY_F2 },
1856 { 0x13, SONYPI_EVENT_FNKEY_F3 },
1857 { 0x14, SONYPI_EVENT_FNKEY_F4 },
1858 { 0x15, SONYPI_EVENT_FNKEY_F5 },
1859 { 0x16, SONYPI_EVENT_FNKEY_F6 },
1860 { 0x17, SONYPI_EVENT_FNKEY_F7 },
1861 { 0x18, SONYPI_EVENT_FNKEY_F8 },
1862 { 0x19, SONYPI_EVENT_FNKEY_F9 },
1863 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
1864 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
1865 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
1866 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
1867 { 0x21, SONYPI_EVENT_FNKEY_1 },
1868 { 0x22, SONYPI_EVENT_FNKEY_2 },
1869 { 0x31, SONYPI_EVENT_FNKEY_D },
1870 { 0x32, SONYPI_EVENT_FNKEY_E },
1871 { 0x33, SONYPI_EVENT_FNKEY_F },
1872 { 0x34, SONYPI_EVENT_FNKEY_S },
1873 { 0x35, SONYPI_EVENT_FNKEY_B },
1874 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
1875 { 0, 0 }
1876};
1877
1878/* The set of possible program key events */
1879static struct sonypi_event sonypi_pkeyev[] = {
1880 { 0x01, SONYPI_EVENT_PKEY_P1 },
1881 { 0x02, SONYPI_EVENT_PKEY_P2 },
1882 { 0x04, SONYPI_EVENT_PKEY_P3 },
Harald Jenny1cae7102009-03-26 21:58:18 +09001883 { 0x20, SONYPI_EVENT_PKEY_P1 },
malattia@linux.it33a04452007-04-09 19:26:03 +02001884 { 0, 0 }
1885};
1886
1887/* The set of possible bluetooth events */
1888static struct sonypi_event sonypi_blueev[] = {
1889 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
1890 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
1891 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
1892 { 0, 0 }
1893};
1894
1895/* The set of possible wireless events */
1896static struct sonypi_event sonypi_wlessev[] = {
Mattia Dongili4eeb5022011-02-19 11:52:27 +09001897 { 0x59, SONYPI_EVENT_IGNORE },
1898 { 0x5a, SONYPI_EVENT_IGNORE },
malattia@linux.it33a04452007-04-09 19:26:03 +02001899 { 0, 0 }
1900};
1901
1902/* The set of possible back button events */
1903static struct sonypi_event sonypi_backev[] = {
1904 { 0x20, SONYPI_EVENT_BACK_PRESSED },
1905 { 0, 0 }
1906};
1907
1908/* The set of possible help button events */
1909static struct sonypi_event sonypi_helpev[] = {
1910 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
1911 { 0, 0 }
1912};
1913
1914
1915/* The set of possible lid events */
1916static struct sonypi_event sonypi_lidev[] = {
1917 { 0x51, SONYPI_EVENT_LID_CLOSED },
1918 { 0x50, SONYPI_EVENT_LID_OPENED },
1919 { 0, 0 }
1920};
1921
1922/* The set of possible zoom events */
1923static struct sonypi_event sonypi_zoomev[] = {
1924 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09001925 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
1926 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
Harald Jenny1cae7102009-03-26 21:58:18 +09001927 { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02001928 { 0, 0 }
1929};
1930
1931/* The set of possible thumbphrase events */
1932static struct sonypi_event sonypi_thumbphraseev[] = {
1933 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
1934 { 0, 0 }
1935};
1936
1937/* The set of possible motioneye camera events */
1938static struct sonypi_event sonypi_meyeev[] = {
1939 { 0x00, SONYPI_EVENT_MEYE_FACE },
1940 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
1941 { 0, 0 }
1942};
1943
1944/* The set of possible memorystick events */
1945static struct sonypi_event sonypi_memorystickev[] = {
1946 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
1947 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
1948 { 0, 0 }
1949};
1950
1951/* The set of possible battery events */
1952static struct sonypi_event sonypi_batteryev[] = {
1953 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
1954 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
1955 { 0, 0 }
1956};
1957
Harald Jenny1cae7102009-03-26 21:58:18 +09001958/* The set of possible volume events */
1959static struct sonypi_event sonypi_volumeev[] = {
1960 { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
1961 { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
1962 { 0, 0 }
1963};
1964
1965/* The set of possible brightness events */
1966static struct sonypi_event sonypi_brightnessev[] = {
1967 { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
1968 { 0, 0 }
1969};
1970
Mattia Dongilide920432008-01-14 18:05:43 +09001971static struct sonypi_eventtypes type1_events[] = {
1972 { 0, 0xffffffff, sonypi_releaseev },
1973 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
1974 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
1975 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
1976 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
1977 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1978 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1979 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
1980 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1981 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
1982 { 0 },
1983};
1984static struct sonypi_eventtypes type2_events[] = {
1985 { 0, 0xffffffff, sonypi_releaseev },
1986 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
1987 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
1988 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
1989 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1990 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1991 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
1992 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
1993 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
1994 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
1995 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
1996 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1997 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1998 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1999 { 0 },
2000};
2001static struct sonypi_eventtypes type3_events[] = {
2002 { 0, 0xffffffff, sonypi_releaseev },
2003 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2004 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
2005 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2006 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
2007 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002008 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
2009 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
2010 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
Harald Jenny1cae7102009-03-26 21:58:18 +09002011 { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
2012 { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002013 { 0 },
Mattia Dongilide920432008-01-14 18:05:43 +09002014};
2015
Mattia Dongili3eb87492008-01-14 18:05:45 +09002016/* low level spic calls */
malattia@linux.it33a04452007-04-09 19:26:03 +02002017#define ITERATIONS_LONG 10000
2018#define ITERATIONS_SHORT 10
2019#define wait_on_command(command, iterations) { \
2020 unsigned int n = iterations; \
2021 while (--n && (command)) \
2022 udelay(1); \
2023 if (!n) \
2024 dprintk("command failed at %s : %s (line %d)\n", \
Harvey Harrison6e574192008-04-29 00:59:20 -07002025 __FILE__, __func__, __LINE__); \
malattia@linux.it33a04452007-04-09 19:26:03 +02002026}
2027
2028static u8 sony_pic_call1(u8 dev)
2029{
2030 u8 v1, v2;
2031
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002032 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002033 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002034 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2035 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
2036 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002037 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02002038 return v2;
2039}
2040
2041static u8 sony_pic_call2(u8 dev, u8 fn)
2042{
2043 u8 v1;
2044
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002045 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002046 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002047 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2048 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002049 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002050 outb(fn, spic_dev.cur_ioport->io1.minimum);
2051 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002052 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02002053 return v1;
2054}
2055
malattia@linux.it49a11de2007-04-09 19:28:56 +02002056static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
2057{
2058 u8 v1;
2059
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002060 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2061 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2062 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2063 outb(fn, spic_dev.cur_ioport->io1.minimum);
2064 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2065 outb(v, spic_dev.cur_ioport->io1.minimum);
2066 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002067 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
2068 dev, fn, v, v1);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002069 return v1;
2070}
2071
Mattia Dongili3eb87492008-01-14 18:05:45 +09002072/*
2073 * minidrivers for SPIC models
2074 */
Mattia Dongilie93c8a62009-03-26 21:58:17 +09002075static int type3_handle_irq(const u8 data_mask, const u8 ev)
Mattia Dongili3eb87492008-01-14 18:05:45 +09002076{
2077 /*
2078 * 0x31 could mean we have to take some extra action and wait for
Mattia Dongilie93c8a62009-03-26 21:58:17 +09002079 * the next irq for some Type3 models, it will generate a new
Mattia Dongili3eb87492008-01-14 18:05:45 +09002080 * irq and we can read new data from the device:
2081 * - 0x5c and 0x5f requires 0xA0
2082 * - 0x61 requires 0xB3
2083 */
2084 if (data_mask == 0x31) {
2085 if (ev == 0x5c || ev == 0x5f)
2086 sony_pic_call1(0xA0);
2087 else if (ev == 0x61)
2088 sony_pic_call1(0xB3);
2089 return 0;
2090 }
2091 return 1;
2092}
2093
Mattia Dongili3eb87492008-01-14 18:05:45 +09002094static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
2095{
2096 struct pci_dev *pcidev;
2097
2098 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2099 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
2100 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002101 dev->model = SONYPI_DEVICE_TYPE1;
2102 dev->evport_offset = SONYPI_TYPE1_OFFSET;
2103 dev->event_types = type1_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002104 goto out;
2105 }
2106
2107 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2108 PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
2109 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002110 dev->model = SONYPI_DEVICE_TYPE2;
2111 dev->evport_offset = SONYPI_TYPE2_OFFSET;
2112 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002113 goto out;
2114 }
2115
2116 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2117 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
2118 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002119 dev->model = SONYPI_DEVICE_TYPE3;
2120 dev->handle_irq = type3_handle_irq;
2121 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2122 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002123 goto out;
2124 }
2125
2126 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2127 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
2128 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002129 dev->model = SONYPI_DEVICE_TYPE3;
2130 dev->handle_irq = type3_handle_irq;
2131 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2132 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002133 goto out;
2134 }
2135
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09002136 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2137 PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
2138 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002139 dev->model = SONYPI_DEVICE_TYPE3;
2140 dev->handle_irq = type3_handle_irq;
2141 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2142 dev->event_types = type3_events;
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09002143 goto out;
2144 }
2145
Mattia Dongili3eb87492008-01-14 18:05:45 +09002146 /* default */
Mattia Dongili31df7142009-09-16 00:05:29 +09002147 dev->model = SONYPI_DEVICE_TYPE2;
2148 dev->evport_offset = SONYPI_TYPE2_OFFSET;
2149 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002150
2151out:
2152 if (pcidev)
2153 pci_dev_put(pcidev);
2154
Mattia Dongilid6697932011-02-19 11:52:28 +09002155 pr_info(DRV_PFX "detected Type%d model\n",
Mattia Dongili31df7142009-09-16 00:05:29 +09002156 dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
2157 dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
Mattia Dongili3eb87492008-01-14 18:05:45 +09002158}
2159
malattia@linux.it49a11de2007-04-09 19:28:56 +02002160/* camera tests and poweron/poweroff */
2161#define SONYPI_CAMERA_PICTURE 5
malattia@linux.it49a11de2007-04-09 19:28:56 +02002162#define SONYPI_CAMERA_CONTROL 0x10
malattia@linux.ite3646322007-04-28 23:34:22 +09002163
2164#define SONYPI_CAMERA_BRIGHTNESS 0
2165#define SONYPI_CAMERA_CONTRAST 1
2166#define SONYPI_CAMERA_HUE 2
2167#define SONYPI_CAMERA_COLOR 3
2168#define SONYPI_CAMERA_SHARPNESS 4
2169
2170#define SONYPI_CAMERA_EXPOSURE_MASK 0xC
2171#define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
2172#define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
2173#define SONYPI_CAMERA_MUTE_MASK 0x40
2174
2175/* the rest don't need a loop until not 0xff */
2176#define SONYPI_CAMERA_AGC 6
2177#define SONYPI_CAMERA_AGC_MASK 0x30
2178#define SONYPI_CAMERA_SHUTTER_MASK 0x7
2179
2180#define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
2181#define SONYPI_CAMERA_CONTROL 0x10
2182
2183#define SONYPI_CAMERA_STATUS 7
2184#define SONYPI_CAMERA_STATUS_READY 0x2
2185#define SONYPI_CAMERA_STATUS_POSITION 0x4
2186
2187#define SONYPI_DIRECTION_BACKWARDS 0x4
2188
2189#define SONYPI_CAMERA_REVISION 8
2190#define SONYPI_CAMERA_ROMVERSION 9
malattia@linux.it49a11de2007-04-09 19:28:56 +02002191
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002192static int __sony_pic_camera_ready(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002193{
2194 u8 v;
2195
2196 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
2197 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
2198}
2199
malattia@linux.ite3646322007-04-28 23:34:22 +09002200static int __sony_pic_camera_off(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002201{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002202 if (!camera) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002203 pr_warn(DRV_PFX "camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002204 return -ENODEV;
2205 }
2206
malattia@linux.it49a11de2007-04-09 19:28:56 +02002207 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
2208 SONYPI_CAMERA_MUTE_MASK),
2209 ITERATIONS_SHORT);
2210
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002211 if (spic_dev.camera_power) {
2212 sony_pic_call2(0x91, 0);
2213 spic_dev.camera_power = 0;
2214 }
2215 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002216}
2217
malattia@linux.ite3646322007-04-28 23:34:22 +09002218static int __sony_pic_camera_on(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002219{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002220 int i, j, x;
2221
2222 if (!camera) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002223 pr_warn(DRV_PFX "camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002224 return -ENODEV;
2225 }
malattia@linux.it49a11de2007-04-09 19:28:56 +02002226
2227 if (spic_dev.camera_power)
malattia@linux.ite3646322007-04-28 23:34:22 +09002228 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002229
2230 for (j = 5; j > 0; j--) {
2231
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002232 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002233 msleep(10);
2234 sony_pic_call1(0x93);
2235
2236 for (i = 400; i > 0; i--) {
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002237 if (__sony_pic_camera_ready())
malattia@linux.it49a11de2007-04-09 19:28:56 +02002238 break;
2239 msleep(10);
2240 }
2241 if (i)
2242 break;
2243 }
2244
2245 if (j == 0) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002246 pr_warn(DRV_PFX "failed to power on camera\n");
malattia@linux.ite3646322007-04-28 23:34:22 +09002247 return -ENODEV;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002248 }
2249
2250 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
2251 0x5a),
2252 ITERATIONS_SHORT);
2253
2254 spic_dev.camera_power = 1;
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002255 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002256}
2257
malattia@linux.ite3646322007-04-28 23:34:22 +09002258/* External camera command (exported to the motion eye v4l driver) */
2259int sony_pic_camera_command(int command, u8 value)
2260{
2261 if (!camera)
2262 return -EIO;
2263
2264 mutex_lock(&spic_dev.lock);
2265
2266 switch (command) {
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002267 case SONY_PIC_COMMAND_SETCAMERA:
malattia@linux.ite3646322007-04-28 23:34:22 +09002268 if (value)
2269 __sony_pic_camera_on();
2270 else
2271 __sony_pic_camera_off();
2272 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002273 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09002274 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
2275 ITERATIONS_SHORT);
2276 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002277 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
malattia@linux.ite3646322007-04-28 23:34:22 +09002278 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
2279 ITERATIONS_SHORT);
2280 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002281 case SONY_PIC_COMMAND_SETCAMERAHUE:
malattia@linux.ite3646322007-04-28 23:34:22 +09002282 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
2283 ITERATIONS_SHORT);
2284 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002285 case SONY_PIC_COMMAND_SETCAMERACOLOR:
malattia@linux.ite3646322007-04-28 23:34:22 +09002286 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
2287 ITERATIONS_SHORT);
2288 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002289 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09002290 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
2291 ITERATIONS_SHORT);
2292 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002293 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
malattia@linux.ite3646322007-04-28 23:34:22 +09002294 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
2295 ITERATIONS_SHORT);
2296 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002297 case SONY_PIC_COMMAND_SETCAMERAAGC:
malattia@linux.ite3646322007-04-28 23:34:22 +09002298 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
2299 ITERATIONS_SHORT);
2300 break;
2301 default:
Mattia Dongilid6697932011-02-19 11:52:28 +09002302 pr_err(DRV_PFX "sony_pic_camera_command invalid: %d\n",
malattia@linux.ite3646322007-04-28 23:34:22 +09002303 command);
2304 break;
2305 }
2306 mutex_unlock(&spic_dev.lock);
2307 return 0;
2308}
2309EXPORT_SYMBOL(sony_pic_camera_command);
2310
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002311/* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002312static void __sony_pic_set_wwanpower(u8 state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002313{
2314 state = !!state;
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002315 if (spic_dev.wwan_power == state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002316 return;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002317 sony_pic_call2(0xB0, state);
Sergey Yanovich3ad1b762009-03-26 21:58:21 +09002318 sony_pic_call1(0x82);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002319 spic_dev.wwan_power = state;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002320}
2321
2322static ssize_t sony_pic_wwanpower_store(struct device *dev,
2323 struct device_attribute *attr,
2324 const char *buffer, size_t count)
2325{
2326 unsigned long value;
2327 if (count > 31)
2328 return -EINVAL;
2329
2330 value = simple_strtoul(buffer, NULL, 10);
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002331 mutex_lock(&spic_dev.lock);
2332 __sony_pic_set_wwanpower(value);
2333 mutex_unlock(&spic_dev.lock);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002334
2335 return count;
2336}
2337
2338static ssize_t sony_pic_wwanpower_show(struct device *dev,
2339 struct device_attribute *attr, char *buffer)
2340{
2341 ssize_t count;
2342 mutex_lock(&spic_dev.lock);
2343 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
2344 mutex_unlock(&spic_dev.lock);
2345 return count;
2346}
2347
malattia@linux.it49a11de2007-04-09 19:28:56 +02002348/* bluetooth subsystem power state */
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002349static void __sony_pic_set_bluetoothpower(u8 state)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002350{
2351 state = !!state;
2352 if (spic_dev.bluetooth_power == state)
2353 return;
2354 sony_pic_call2(0x96, state);
2355 sony_pic_call1(0x82);
2356 spic_dev.bluetooth_power = state;
2357}
2358
2359static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
2360 struct device_attribute *attr,
2361 const char *buffer, size_t count)
2362{
2363 unsigned long value;
2364 if (count > 31)
2365 return -EINVAL;
2366
2367 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002368 mutex_lock(&spic_dev.lock);
2369 __sony_pic_set_bluetoothpower(value);
2370 mutex_unlock(&spic_dev.lock);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002371
2372 return count;
2373}
2374
2375static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
2376 struct device_attribute *attr, char *buffer)
2377{
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002378 ssize_t count = 0;
2379 mutex_lock(&spic_dev.lock);
2380 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
2381 mutex_unlock(&spic_dev.lock);
2382 return count;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002383}
2384
2385/* fan speed */
2386/* FAN0 information (reverse engineered from ACPI tables) */
2387#define SONY_PIC_FAN0_STATUS 0x93
malattia@linux.it7b153f32007-04-09 19:31:25 +02002388static int sony_pic_set_fanspeed(unsigned long value)
2389{
2390 return ec_write(SONY_PIC_FAN0_STATUS, value);
2391}
2392
2393static int sony_pic_get_fanspeed(u8 *value)
2394{
2395 return ec_read(SONY_PIC_FAN0_STATUS, value);
2396}
2397
malattia@linux.it49a11de2007-04-09 19:28:56 +02002398static ssize_t sony_pic_fanspeed_store(struct device *dev,
2399 struct device_attribute *attr,
2400 const char *buffer, size_t count)
2401{
2402 unsigned long value;
2403 if (count > 31)
2404 return -EINVAL;
2405
2406 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002407 if (sony_pic_set_fanspeed(value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02002408 return -EIO;
2409
2410 return count;
2411}
2412
2413static ssize_t sony_pic_fanspeed_show(struct device *dev,
2414 struct device_attribute *attr, char *buffer)
2415{
2416 u8 value = 0;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002417 if (sony_pic_get_fanspeed(&value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02002418 return -EIO;
2419
2420 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
2421}
2422
2423#define SPIC_ATTR(_name, _mode) \
2424struct device_attribute spic_attr_##_name = __ATTR(_name, \
2425 _mode, sony_pic_## _name ##_show, \
2426 sony_pic_## _name ##_store)
2427
malattia@linux.it49a11de2007-04-09 19:28:56 +02002428static SPIC_ATTR(bluetoothpower, 0644);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002429static SPIC_ATTR(wwanpower, 0644);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002430static SPIC_ATTR(fanspeed, 0644);
2431
2432static struct attribute *spic_attributes[] = {
malattia@linux.it49a11de2007-04-09 19:28:56 +02002433 &spic_attr_bluetoothpower.attr,
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002434 &spic_attr_wwanpower.attr,
malattia@linux.it49a11de2007-04-09 19:28:56 +02002435 &spic_attr_fanspeed.attr,
2436 NULL
2437};
2438
2439static struct attribute_group spic_attribute_group = {
2440 .attrs = spic_attributes
2441};
2442
malattia@linux.it7b153f32007-04-09 19:31:25 +02002443/******** SONYPI compatibility **********/
Mattia Dongilia64e62a2007-05-01 11:19:53 +09002444#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +02002445
2446/* battery / brightness / temperature addresses */
2447#define SONYPI_BAT_FLAGS 0x81
2448#define SONYPI_LCD_LIGHT 0x96
2449#define SONYPI_BAT1_PCTRM 0xa0
2450#define SONYPI_BAT1_LEFT 0xa2
2451#define SONYPI_BAT1_MAXRT 0xa4
2452#define SONYPI_BAT2_PCTRM 0xa8
2453#define SONYPI_BAT2_LEFT 0xaa
2454#define SONYPI_BAT2_MAXRT 0xac
2455#define SONYPI_BAT1_MAXTK 0xb0
2456#define SONYPI_BAT1_FULL 0xb2
2457#define SONYPI_BAT2_MAXTK 0xb8
2458#define SONYPI_BAT2_FULL 0xba
2459#define SONYPI_TEMP_STATUS 0xC1
2460
2461struct sonypi_compat_s {
2462 struct fasync_struct *fifo_async;
Stefani Seibold45465482009-12-21 14:37:26 -08002463 struct kfifo fifo;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002464 spinlock_t fifo_lock;
2465 wait_queue_head_t fifo_proc_list;
2466 atomic_t open_count;
2467};
2468static struct sonypi_compat_s sonypi_compat = {
2469 .open_count = ATOMIC_INIT(0),
2470};
2471
2472static int sonypi_misc_fasync(int fd, struct file *filp, int on)
2473{
Jonathan Corbet60aa4922009-02-01 14:52:56 -07002474 return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002475}
2476
2477static int sonypi_misc_release(struct inode *inode, struct file *file)
2478{
malattia@linux.it7b153f32007-04-09 19:31:25 +02002479 atomic_dec(&sonypi_compat.open_count);
2480 return 0;
2481}
2482
2483static int sonypi_misc_open(struct inode *inode, struct file *file)
2484{
2485 /* Flush input queue on first open */
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002486 unsigned long flags;
2487
Stefani Seibold45465482009-12-21 14:37:26 -08002488 spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002489
malattia@linux.it7b153f32007-04-09 19:31:25 +02002490 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
Stefani Seibolde64c0262009-12-21 14:37:28 -08002491 kfifo_reset(&sonypi_compat.fifo);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002492
Stefani Seibold45465482009-12-21 14:37:26 -08002493 spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002494
malattia@linux.it7b153f32007-04-09 19:31:25 +02002495 return 0;
2496}
2497
2498static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
2499 size_t count, loff_t *pos)
2500{
2501 ssize_t ret;
2502 unsigned char c;
2503
Stefani Seibold45465482009-12-21 14:37:26 -08002504 if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
malattia@linux.it7b153f32007-04-09 19:31:25 +02002505 (file->f_flags & O_NONBLOCK))
2506 return -EAGAIN;
2507
2508 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
Stefani Seibold45465482009-12-21 14:37:26 -08002509 kfifo_len(&sonypi_compat.fifo) != 0);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002510 if (ret)
2511 return ret;
2512
2513 while (ret < count &&
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002514 (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002515 &sonypi_compat.fifo_lock) == sizeof(c))) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02002516 if (put_user(c, buf++))
2517 return -EFAULT;
2518 ret++;
2519 }
2520
2521 if (ret > 0) {
2522 struct inode *inode = file->f_path.dentry->d_inode;
2523 inode->i_atime = current_fs_time(inode->i_sb);
2524 }
2525
2526 return ret;
2527}
2528
2529static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
2530{
2531 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
Stefani Seibold45465482009-12-21 14:37:26 -08002532 if (kfifo_len(&sonypi_compat.fifo))
malattia@linux.it7b153f32007-04-09 19:31:25 +02002533 return POLLIN | POLLRDNORM;
2534 return 0;
2535}
2536
2537static int ec_read16(u8 addr, u16 *value)
2538{
2539 u8 val_lb, val_hb;
2540 if (ec_read(addr, &val_lb))
2541 return -1;
2542 if (ec_read(addr + 1, &val_hb))
2543 return -1;
2544 *value = val_lb | (val_hb << 8);
2545 return 0;
2546}
2547
Alan Cox2b24ef02009-03-26 21:58:19 +09002548static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
2549 unsigned long arg)
malattia@linux.it7b153f32007-04-09 19:31:25 +02002550{
2551 int ret = 0;
2552 void __user *argp = (void __user *)arg;
2553 u8 val8;
2554 u16 val16;
2555 int value;
2556
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002557 mutex_lock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002558 switch (cmd) {
2559 case SONYPI_IOCGBRT:
2560 if (sony_backlight_device == NULL) {
2561 ret = -EIO;
2562 break;
2563 }
2564 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
2565 ret = -EIO;
2566 break;
2567 }
2568 val8 = ((value & 0xff) - 1) << 5;
2569 if (copy_to_user(argp, &val8, sizeof(val8)))
2570 ret = -EFAULT;
2571 break;
2572 case SONYPI_IOCSBRT:
2573 if (sony_backlight_device == NULL) {
2574 ret = -EIO;
2575 break;
2576 }
2577 if (copy_from_user(&val8, argp, sizeof(val8))) {
2578 ret = -EFAULT;
2579 break;
2580 }
2581 if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
2582 (val8 >> 5) + 1, NULL)) {
2583 ret = -EIO;
2584 break;
2585 }
2586 /* sync the backlight device status */
2587 sony_backlight_device->props.brightness =
2588 sony_backlight_get_brightness(sony_backlight_device);
2589 break;
2590 case SONYPI_IOCGBAT1CAP:
2591 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
2592 ret = -EIO;
2593 break;
2594 }
2595 if (copy_to_user(argp, &val16, sizeof(val16)))
2596 ret = -EFAULT;
2597 break;
2598 case SONYPI_IOCGBAT1REM:
2599 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
2600 ret = -EIO;
2601 break;
2602 }
2603 if (copy_to_user(argp, &val16, sizeof(val16)))
2604 ret = -EFAULT;
2605 break;
2606 case SONYPI_IOCGBAT2CAP:
2607 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
2608 ret = -EIO;
2609 break;
2610 }
2611 if (copy_to_user(argp, &val16, sizeof(val16)))
2612 ret = -EFAULT;
2613 break;
2614 case SONYPI_IOCGBAT2REM:
2615 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
2616 ret = -EIO;
2617 break;
2618 }
2619 if (copy_to_user(argp, &val16, sizeof(val16)))
2620 ret = -EFAULT;
2621 break;
2622 case SONYPI_IOCGBATFLAGS:
2623 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
2624 ret = -EIO;
2625 break;
2626 }
2627 val8 &= 0x07;
2628 if (copy_to_user(argp, &val8, sizeof(val8)))
2629 ret = -EFAULT;
2630 break;
2631 case SONYPI_IOCGBLUE:
2632 val8 = spic_dev.bluetooth_power;
2633 if (copy_to_user(argp, &val8, sizeof(val8)))
2634 ret = -EFAULT;
2635 break;
2636 case SONYPI_IOCSBLUE:
2637 if (copy_from_user(&val8, argp, sizeof(val8))) {
2638 ret = -EFAULT;
2639 break;
2640 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002641 __sony_pic_set_bluetoothpower(val8);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002642 break;
2643 /* FAN Controls */
2644 case SONYPI_IOCGFAN:
2645 if (sony_pic_get_fanspeed(&val8)) {
2646 ret = -EIO;
2647 break;
2648 }
2649 if (copy_to_user(argp, &val8, sizeof(val8)))
2650 ret = -EFAULT;
2651 break;
2652 case SONYPI_IOCSFAN:
2653 if (copy_from_user(&val8, argp, sizeof(val8))) {
2654 ret = -EFAULT;
2655 break;
2656 }
2657 if (sony_pic_set_fanspeed(val8))
2658 ret = -EIO;
2659 break;
2660 /* GET Temperature (useful under APM) */
2661 case SONYPI_IOCGTEMP:
2662 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
2663 ret = -EIO;
2664 break;
2665 }
2666 if (copy_to_user(argp, &val8, sizeof(val8)))
2667 ret = -EFAULT;
2668 break;
2669 default:
2670 ret = -EINVAL;
2671 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002672 mutex_unlock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002673 return ret;
2674}
2675
2676static const struct file_operations sonypi_misc_fops = {
2677 .owner = THIS_MODULE,
2678 .read = sonypi_misc_read,
2679 .poll = sonypi_misc_poll,
2680 .open = sonypi_misc_open,
2681 .release = sonypi_misc_release,
2682 .fasync = sonypi_misc_fasync,
Alan Cox2b24ef02009-03-26 21:58:19 +09002683 .unlocked_ioctl = sonypi_misc_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002684 .llseek = noop_llseek,
malattia@linux.it7b153f32007-04-09 19:31:25 +02002685};
2686
2687static struct miscdevice sonypi_misc_device = {
2688 .minor = MISC_DYNAMIC_MINOR,
2689 .name = "sonypi",
2690 .fops = &sonypi_misc_fops,
2691};
2692
2693static void sonypi_compat_report_event(u8 event)
2694{
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002695 kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002696 sizeof(event), &sonypi_compat.fifo_lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002697 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
2698 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
2699}
2700
2701static int sonypi_compat_init(void)
2702{
2703 int error;
2704
2705 spin_lock_init(&sonypi_compat.fifo_lock);
Stefani Seibold45465482009-12-21 14:37:26 -08002706 error =
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002707 kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -08002708 if (error) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002709 pr_err(DRV_PFX "kfifo_alloc failed\n");
Stefani Seibold45465482009-12-21 14:37:26 -08002710 return error;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002711 }
2712
2713 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002714
2715 if (minor != -1)
2716 sonypi_misc_device.minor = minor;
2717 error = misc_register(&sonypi_misc_device);
2718 if (error) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002719 pr_err(DRV_PFX "misc_register failed\n");
malattia@linux.it7b153f32007-04-09 19:31:25 +02002720 goto err_free_kfifo;
2721 }
2722 if (minor == -1)
Mattia Dongilid6697932011-02-19 11:52:28 +09002723 pr_info(DRV_PFX "device allocated minor is %d\n",
malattia@linux.it7b153f32007-04-09 19:31:25 +02002724 sonypi_misc_device.minor);
2725
2726 return 0;
2727
2728err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -08002729 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002730 return error;
2731}
2732
2733static void sonypi_compat_exit(void)
2734{
2735 misc_deregister(&sonypi_misc_device);
Stefani Seibold45465482009-12-21 14:37:26 -08002736 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002737}
2738#else
2739static int sonypi_compat_init(void) { return 0; }
2740static void sonypi_compat_exit(void) { }
2741static void sonypi_compat_report_event(u8 event) { }
Mattia Dongilia64e62a2007-05-01 11:19:53 +09002742#endif /* CONFIG_SONYPI_COMPAT */
malattia@linux.it7b153f32007-04-09 19:31:25 +02002743
malattia@linux.it1549ee62007-04-09 10:19:08 +02002744/*
malattia@linux.it33a04452007-04-09 19:26:03 +02002745 * ACPI callbacks
malattia@linux.it1549ee62007-04-09 10:19:08 +02002746 */
malattia@linux.it33a04452007-04-09 19:26:03 +02002747static acpi_status
2748sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
2749{
2750 u32 i;
2751 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
2752
2753 switch (resource->type) {
2754 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002755 {
2756 /* start IO enumeration */
2757 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
2758 if (!ioport)
2759 return AE_ERROR;
2760
2761 list_add(&ioport->list, &dev->ioports);
2762 return AE_OK;
2763 }
2764
malattia@linux.it33a04452007-04-09 19:26:03 +02002765 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002766 /* end IO enumeration */
malattia@linux.it33a04452007-04-09 19:26:03 +02002767 return AE_OK;
2768
2769 case ACPI_RESOURCE_TYPE_IRQ:
2770 {
2771 struct acpi_resource_irq *p = &resource->data.irq;
2772 struct sony_pic_irq *interrupt = NULL;
2773 if (!p || !p->interrupt_count) {
2774 /*
2775 * IRQ descriptors may have no IRQ# bits set,
2776 * particularly those those w/ _STA disabled
2777 */
2778 dprintk("Blank IRQ resource\n");
2779 return AE_OK;
2780 }
2781 for (i = 0; i < p->interrupt_count; i++) {
2782 if (!p->interrupts[i]) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002783 pr_warn(DRV_PFX "Invalid IRQ %d\n",
malattia@linux.it33a04452007-04-09 19:26:03 +02002784 p->interrupts[i]);
2785 continue;
2786 }
2787 interrupt = kzalloc(sizeof(*interrupt),
2788 GFP_KERNEL);
2789 if (!interrupt)
2790 return AE_ERROR;
2791
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002792 list_add(&interrupt->list, &dev->interrupts);
malattia@linux.it33a04452007-04-09 19:26:03 +02002793 interrupt->irq.triggering = p->triggering;
2794 interrupt->irq.polarity = p->polarity;
2795 interrupt->irq.sharable = p->sharable;
2796 interrupt->irq.interrupt_count = 1;
2797 interrupt->irq.interrupts[0] = p->interrupts[i];
2798 }
2799 return AE_OK;
2800 }
2801 case ACPI_RESOURCE_TYPE_IO:
2802 {
2803 struct acpi_resource_io *io = &resource->data.io;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002804 struct sony_pic_ioport *ioport =
2805 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
malattia@linux.it33a04452007-04-09 19:26:03 +02002806 if (!io) {
2807 dprintk("Blank IO resource\n");
2808 return AE_OK;
2809 }
2810
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002811 if (!ioport->io1.minimum) {
2812 memcpy(&ioport->io1, io, sizeof(*io));
2813 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
2814 ioport->io1.address_length);
2815 }
2816 else if (!ioport->io2.minimum) {
2817 memcpy(&ioport->io2, io, sizeof(*io));
2818 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
2819 ioport->io2.address_length);
2820 }
2821 else {
Mattia Dongilid6697932011-02-19 11:52:28 +09002822 pr_err(DRV_PFX "Unknown SPIC Type, more than 2 IO Ports\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002823 return AE_ERROR;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002824 }
malattia@linux.it33a04452007-04-09 19:26:03 +02002825 return AE_OK;
2826 }
2827 default:
2828 dprintk("Resource %d isn't an IRQ nor an IO port\n",
2829 resource->type);
2830
2831 case ACPI_RESOURCE_TYPE_END_TAG:
2832 return AE_OK;
2833 }
2834 return AE_CTRL_TERMINATE;
2835}
2836
2837static int sony_pic_possible_resources(struct acpi_device *device)
2838{
2839 int result = 0;
2840 acpi_status status = AE_OK;
2841
2842 if (!device)
2843 return -EINVAL;
2844
2845 /* get device status */
2846 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
2847 dprintk("Evaluating _STA\n");
2848 result = acpi_bus_get_status(device);
2849 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002850 pr_warn(DRV_PFX "Unable to read status\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002851 goto end;
2852 }
2853
2854 if (!device->status.enabled)
2855 dprintk("Device disabled\n");
2856 else
2857 dprintk("Device enabled\n");
2858
2859 /*
2860 * Query and parse 'method'
2861 */
2862 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
2863 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
2864 sony_pic_read_possible_resource, &spic_dev);
2865 if (ACPI_FAILURE(status)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002866 pr_warn(DRV_PFX "Failure evaluating %s\n",
malattia@linux.it33a04452007-04-09 19:26:03 +02002867 METHOD_NAME__PRS);
2868 result = -ENODEV;
2869 }
2870end:
2871 return result;
2872}
2873
2874/*
2875 * Disable the spic device by calling its _DIS method
2876 */
2877static int sony_pic_disable(struct acpi_device *device)
2878{
Matthew Garrett6158d3a2008-10-29 14:01:03 -07002879 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
2880 NULL);
2881
2882 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
malattia@linux.it33a04452007-04-09 19:26:03 +02002883 return -ENXIO;
2884
2885 dprintk("Device disabled\n");
2886 return 0;
2887}
2888
2889
2890/*
2891 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
2892 *
2893 * Call _SRS to set current resources
2894 */
2895static int sony_pic_enable(struct acpi_device *device,
2896 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
2897{
2898 acpi_status status;
2899 int result = 0;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002900 /* Type 1 resource layout is:
2901 * IO
2902 * IO
2903 * IRQNoFlags
2904 * End
2905 *
2906 * Type 2 and 3 resource layout is:
2907 * IO
2908 * IRQNoFlags
2909 * End
2910 */
malattia@linux.it33a04452007-04-09 19:26:03 +02002911 struct {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002912 struct acpi_resource res1;
2913 struct acpi_resource res2;
2914 struct acpi_resource res3;
2915 struct acpi_resource res4;
malattia@linux.it33a04452007-04-09 19:26:03 +02002916 } *resource;
2917 struct acpi_buffer buffer = { 0, NULL };
2918
2919 if (!ioport || !irq)
2920 return -EINVAL;
2921
2922 /* init acpi_buffer */
2923 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
2924 if (!resource)
2925 return -ENOMEM;
2926
2927 buffer.length = sizeof(*resource) + 1;
2928 buffer.pointer = resource;
2929
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002930 /* setup Type 1 resources */
Mattia Dongili31df7142009-09-16 00:05:29 +09002931 if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002932
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002933 /* setup io resources */
2934 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2935 resource->res1.length = sizeof(struct acpi_resource);
2936 memcpy(&resource->res1.data.io, &ioport->io1,
2937 sizeof(struct acpi_resource_io));
malattia@linux.it33a04452007-04-09 19:26:03 +02002938
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002939 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
2940 resource->res2.length = sizeof(struct acpi_resource);
2941 memcpy(&resource->res2.data.io, &ioport->io2,
2942 sizeof(struct acpi_resource_io));
2943
2944 /* setup irq resource */
2945 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
2946 resource->res3.length = sizeof(struct acpi_resource);
2947 memcpy(&resource->res3.data.irq, &irq->irq,
2948 sizeof(struct acpi_resource_irq));
2949 /* we requested a shared irq */
2950 resource->res3.data.irq.sharable = ACPI_SHARED;
2951
2952 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
2953
2954 }
2955 /* setup Type 2/3 resources */
2956 else {
2957 /* setup io resource */
2958 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2959 resource->res1.length = sizeof(struct acpi_resource);
2960 memcpy(&resource->res1.data.io, &ioport->io1,
2961 sizeof(struct acpi_resource_io));
2962
2963 /* setup irq resource */
2964 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
2965 resource->res2.length = sizeof(struct acpi_resource);
2966 memcpy(&resource->res2.data.irq, &irq->irq,
2967 sizeof(struct acpi_resource_irq));
2968 /* we requested a shared irq */
2969 resource->res2.data.irq.sharable = ACPI_SHARED;
2970
2971 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
2972 }
malattia@linux.it33a04452007-04-09 19:26:03 +02002973
2974 /* Attempt to set the resource */
2975 dprintk("Evaluating _SRS\n");
2976 status = acpi_set_current_resources(device->handle, &buffer);
2977
2978 /* check for total failure */
2979 if (ACPI_FAILURE(status)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002980 pr_err(DRV_PFX "Error evaluating _SRS\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002981 result = -ENODEV;
2982 goto end;
2983 }
2984
2985 /* Necessary device initializations calls (from sonypi) */
2986 sony_pic_call1(0x82);
2987 sony_pic_call2(0x81, 0xff);
2988 sony_pic_call1(compat ? 0x92 : 0x82);
2989
2990end:
2991 kfree(resource);
2992 return result;
2993}
2994
2995/*****************
2996 *
2997 * ISR: some event is available
2998 *
2999 *****************/
3000static irqreturn_t sony_pic_irq(int irq, void *dev_id)
3001{
3002 int i, j;
malattia@linux.it33a04452007-04-09 19:26:03 +02003003 u8 ev = 0;
3004 u8 data_mask = 0;
3005 u8 device_event = 0;
3006
3007 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
3008
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003009 ev = inb_p(dev->cur_ioport->io1.minimum);
3010 if (dev->cur_ioport->io2.minimum)
3011 data_mask = inb_p(dev->cur_ioport->io2.minimum);
3012 else
Mattia Dongilide920432008-01-14 18:05:43 +09003013 data_mask = inb_p(dev->cur_ioport->io1.minimum +
Mattia Dongili31df7142009-09-16 00:05:29 +09003014 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003015
Mattia Dongili22a17782007-07-16 02:34:39 +09003016 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
Mattia Dongilide920432008-01-14 18:05:43 +09003017 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09003018 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003019
3020 if (ev == 0x00 || ev == 0xff)
3021 return IRQ_HANDLED;
3022
Mattia Dongili31df7142009-09-16 00:05:29 +09003023 for (i = 0; dev->event_types[i].mask; i++) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003024
Mattia Dongili31df7142009-09-16 00:05:29 +09003025 if ((data_mask & dev->event_types[i].data) !=
3026 dev->event_types[i].data)
malattia@linux.it33a04452007-04-09 19:26:03 +02003027 continue;
3028
Mattia Dongili31df7142009-09-16 00:05:29 +09003029 if (!(mask & dev->event_types[i].mask))
malattia@linux.it33a04452007-04-09 19:26:03 +02003030 continue;
3031
Mattia Dongili31df7142009-09-16 00:05:29 +09003032 for (j = 0; dev->event_types[i].events[j].event; j++) {
3033 if (ev == dev->event_types[i].events[j].data) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003034 device_event =
Mattia Dongili31df7142009-09-16 00:05:29 +09003035 dev->event_types[i].events[j].event;
Mattia Dongili4eeb5022011-02-19 11:52:27 +09003036 /* some events may require ignoring */
3037 if (!device_event)
3038 return IRQ_HANDLED;
malattia@linux.it33a04452007-04-09 19:26:03 +02003039 goto found;
3040 }
3041 }
3042 }
Mattia Dongili3eb87492008-01-14 18:05:45 +09003043 /* Still not able to decode the event try to pass
3044 * it over to the minidriver
3045 */
Mattia Dongili31df7142009-09-16 00:05:29 +09003046 if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
Mattia Dongili3eb87492008-01-14 18:05:45 +09003047 return IRQ_HANDLED;
3048
Mattia Dongilide920432008-01-14 18:05:43 +09003049 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
3050 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09003051 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003052 return IRQ_HANDLED;
3053
3054found:
malattia@linux.it1549ee62007-04-09 10:19:08 +02003055 sony_laptop_report_input_event(device_event);
Mattia Dongilide920432008-01-14 18:05:43 +09003056 acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003057 sonypi_compat_report_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02003058 return IRQ_HANDLED;
3059}
3060
3061/*****************
3062 *
3063 * ACPI driver
3064 *
3065 *****************/
3066static int sony_pic_remove(struct acpi_device *device, int type)
3067{
3068 struct sony_pic_ioport *io, *tmp_io;
3069 struct sony_pic_irq *irq, *tmp_irq;
3070
3071 if (sony_pic_disable(device)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003072 pr_err(DRV_PFX "Couldn't disable device.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003073 return -ENXIO;
3074 }
3075
3076 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003077 release_region(spic_dev.cur_ioport->io1.minimum,
3078 spic_dev.cur_ioport->io1.address_length);
3079 if (spic_dev.cur_ioport->io2.minimum)
3080 release_region(spic_dev.cur_ioport->io2.minimum,
3081 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02003082
Mattia Dongili015a9162007-08-12 16:20:27 +09003083 sonypi_compat_exit();
3084
malattia@linux.it1549ee62007-04-09 10:19:08 +02003085 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02003086
malattia@linux.it49a11de2007-04-09 19:28:56 +02003087 /* pf attrs */
3088 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
3089 sony_pf_remove();
3090
malattia@linux.it33a04452007-04-09 19:26:03 +02003091 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
3092 list_del(&io->list);
3093 kfree(io);
3094 }
3095 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
3096 list_del(&irq->list);
3097 kfree(irq);
3098 }
3099 spic_dev.cur_ioport = NULL;
3100 spic_dev.cur_irq = NULL;
3101
malattia@linux.itf6119b02007-04-09 19:31:06 +02003102 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003103 return 0;
3104}
3105
3106static int sony_pic_add(struct acpi_device *device)
3107{
3108 int result;
3109 struct sony_pic_ioport *io, *tmp_io;
3110 struct sony_pic_irq *irq, *tmp_irq;
3111
Mattia Dongilid6697932011-02-19 11:52:28 +09003112 pr_info(DRV_PFX "%s v%s.\n", SONY_PIC_DRIVER_NAME,
3113 SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.it33a04452007-04-09 19:26:03 +02003114
3115 spic_dev.acpi_dev = device;
3116 strcpy(acpi_device_class(device), "sony/hotkey");
Mattia Dongilide920432008-01-14 18:05:43 +09003117 sony_pic_detect_device_type(&spic_dev);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003118 mutex_init(&spic_dev.lock);
malattia@linux.it33a04452007-04-09 19:26:03 +02003119
3120 /* read _PRS resources */
3121 result = sony_pic_possible_resources(device);
3122 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003123 pr_err(DRV_PFX "Unable to read possible resources.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003124 goto err_free_resources;
3125 }
3126
3127 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05003128 result = sony_laptop_setup_input(device);
malattia@linux.it33a04452007-04-09 19:26:03 +02003129 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003130 pr_err(DRV_PFX "Unable to create input devices.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003131 goto err_free_resources;
3132 }
3133
Mattia Dongili015a9162007-08-12 16:20:27 +09003134 if (sonypi_compat_init())
3135 goto err_remove_input;
3136
malattia@linux.it33a04452007-04-09 19:26:03 +02003137 /* request io port */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003138 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
3139 if (request_region(io->io1.minimum, io->io1.address_length,
Lucas De Marchic8440332011-03-17 17:18:22 -03003140 "Sony Programmable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003141 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
3142 io->io1.minimum, io->io1.maximum,
3143 io->io1.address_length);
3144 /* Type 1 have 2 ioports */
3145 if (io->io2.minimum) {
3146 if (request_region(io->io2.minimum,
3147 io->io2.address_length,
Lucas De Marchic8440332011-03-17 17:18:22 -03003148 "Sony Programmable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003149 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
3150 io->io2.minimum, io->io2.maximum,
3151 io->io2.address_length);
3152 spic_dev.cur_ioport = io;
3153 break;
3154 }
3155 else {
3156 dprintk("Unable to get I/O port2: "
3157 "0x%.4x (0x%.4x) + 0x%.2x\n",
3158 io->io2.minimum, io->io2.maximum,
3159 io->io2.address_length);
3160 release_region(io->io1.minimum,
3161 io->io1.address_length);
3162 }
3163 }
3164 else {
3165 spic_dev.cur_ioport = io;
3166 break;
3167 }
malattia@linux.it33a04452007-04-09 19:26:03 +02003168 }
3169 }
3170 if (!spic_dev.cur_ioport) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003171 pr_err(DRV_PFX "Failed to request_region.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003172 result = -ENODEV;
Mattia Dongili015a9162007-08-12 16:20:27 +09003173 goto err_remove_compat;
malattia@linux.it33a04452007-04-09 19:26:03 +02003174 }
3175
3176 /* request IRQ */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003177 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003178 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
Mattia Dongilid1e0de92009-09-16 00:05:30 +09003179 IRQF_DISABLED, "sony-laptop", &spic_dev)) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003180 dprintk("IRQ: %d - triggering: %d - "
3181 "polarity: %d - shr: %d\n",
3182 irq->irq.interrupts[0],
3183 irq->irq.triggering,
3184 irq->irq.polarity,
3185 irq->irq.sharable);
3186 spic_dev.cur_irq = irq;
3187 break;
3188 }
3189 }
3190 if (!spic_dev.cur_irq) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003191 pr_err(DRV_PFX "Failed to request_irq.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003192 result = -ENODEV;
3193 goto err_release_region;
3194 }
3195
3196 /* set resource status _SRS */
3197 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
3198 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003199 pr_err(DRV_PFX "Couldn't enable device.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003200 goto err_free_irq;
3201 }
3202
malattia@linux.it49a11de2007-04-09 19:28:56 +02003203 spic_dev.bluetooth_power = -1;
3204 /* create device attributes */
3205 result = sony_pf_add();
3206 if (result)
3207 goto err_disable_device;
3208
3209 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
3210 if (result)
3211 goto err_remove_pf;
3212
malattia@linux.it33a04452007-04-09 19:26:03 +02003213 return 0;
3214
malattia@linux.it49a11de2007-04-09 19:28:56 +02003215err_remove_pf:
3216 sony_pf_remove();
3217
3218err_disable_device:
3219 sony_pic_disable(device);
3220
malattia@linux.it33a04452007-04-09 19:26:03 +02003221err_free_irq:
3222 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
3223
3224err_release_region:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003225 release_region(spic_dev.cur_ioport->io1.minimum,
3226 spic_dev.cur_ioport->io1.address_length);
3227 if (spic_dev.cur_ioport->io2.minimum)
3228 release_region(spic_dev.cur_ioport->io2.minimum,
3229 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02003230
Mattia Dongili015a9162007-08-12 16:20:27 +09003231err_remove_compat:
3232 sonypi_compat_exit();
3233
malattia@linux.it33a04452007-04-09 19:26:03 +02003234err_remove_input:
malattia@linux.it1549ee62007-04-09 10:19:08 +02003235 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02003236
3237err_free_resources:
3238 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
3239 list_del(&io->list);
3240 kfree(io);
3241 }
3242 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
3243 list_del(&irq->list);
3244 kfree(irq);
3245 }
3246 spic_dev.cur_ioport = NULL;
3247 spic_dev.cur_irq = NULL;
3248
3249 return result;
3250}
3251
3252static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
3253{
3254 if (sony_pic_disable(device))
3255 return -ENXIO;
3256 return 0;
3257}
3258
3259static int sony_pic_resume(struct acpi_device *device)
3260{
3261 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
3262 return 0;
3263}
3264
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003265static const struct acpi_device_id sony_pic_device_ids[] = {
3266 {SONY_PIC_HID, 0},
3267 {"", 0},
3268};
3269
malattia@linux.it33a04452007-04-09 19:26:03 +02003270static struct acpi_driver sony_pic_driver = {
3271 .name = SONY_PIC_DRIVER_NAME,
3272 .class = SONY_PIC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003273 .ids = sony_pic_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02003274 .owner = THIS_MODULE,
3275 .ops = {
3276 .add = sony_pic_add,
3277 .remove = sony_pic_remove,
3278 .suspend = sony_pic_suspend,
3279 .resume = sony_pic_resume,
3280 },
3281};
3282
3283static struct dmi_system_id __initdata sonypi_dmi_table[] = {
3284 {
3285 .ident = "Sony Vaio",
3286 .matches = {
3287 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
3288 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
3289 },
3290 },
3291 {
3292 .ident = "Sony Vaio",
3293 .matches = {
3294 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
3295 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
3296 },
3297 },
3298 { }
3299};
3300
malattia@linux.it59b19102007-04-09 10:19:04 +02003301static int __init sony_laptop_init(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01003302{
malattia@linux.it33a04452007-04-09 19:26:03 +02003303 int result;
3304
3305 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
3306 result = acpi_bus_register_driver(&sony_pic_driver);
3307 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003308 pr_err(DRV_PFX "Unable to register SPIC driver.");
malattia@linux.it33a04452007-04-09 19:26:03 +02003309 goto out;
3310 }
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003311 spic_drv_registered = 1;
malattia@linux.it33a04452007-04-09 19:26:03 +02003312 }
3313
3314 result = acpi_bus_register_driver(&sony_nc_driver);
3315 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003316 pr_err(DRV_PFX "Unable to register SNC driver.");
malattia@linux.it33a04452007-04-09 19:26:03 +02003317 goto out_unregister_pic;
3318 }
3319
3320 return 0;
3321
3322out_unregister_pic:
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003323 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02003324 acpi_bus_unregister_driver(&sony_pic_driver);
3325out:
3326 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +01003327}
3328
malattia@linux.it59b19102007-04-09 10:19:04 +02003329static void __exit sony_laptop_exit(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01003330{
malattia@linux.it59b19102007-04-09 10:19:04 +02003331 acpi_bus_unregister_driver(&sony_nc_driver);
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003332 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02003333 acpi_bus_unregister_driver(&sony_pic_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +01003334}
3335
malattia@linux.it59b19102007-04-09 10:19:04 +02003336module_init(sony_laptop_init);
3337module_exit(sony_laptop_exit);