blob: 0ac186e9abdc89c4079cc91ec41ce011cac7f67f [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
Joe Perches50f581a2011-03-29 15:21:48 -070045#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
46
Stelian Pop7f09c432007-01-13 23:04:31 +010047#include <linux/kernel.h>
48#include <linux/module.h>
49#include <linux/moduleparam.h>
50#include <linux/init.h>
51#include <linux/types.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010052#include <linux/backlight.h>
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010053#include <linux/platform_device.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010054#include <linux/err.h>
malattia@linux.it33a04452007-04-09 19:26:03 +020055#include <linux/dmi.h>
56#include <linux/pci.h>
57#include <linux/interrupt.h>
58#include <linux/delay.h>
59#include <linux/input.h>
60#include <linux/kfifo.h>
61#include <linux/workqueue.h>
62#include <linux/acpi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090063#include <linux/slab.h>
Stelian Pop7f09c432007-01-13 23:04:31 +010064#include <acpi/acpi_drivers.h>
65#include <acpi/acpi_bus.h>
66#include <asm/uaccess.h>
malattia@linux.it33a04452007-04-09 19:26:03 +020067#include <linux/sonypi.h>
malattia@linux.it1ce82c12007-04-28 23:34:36 +090068#include <linux/sony-laptop.h>
Matthew Garrett6cc056b2009-03-26 21:58:15 +090069#include <linux/rfkill.h>
Mattia Dongilia64e62a2007-05-01 11:19:53 +090070#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +020071#include <linux/poll.h>
72#include <linux/miscdevice.h>
73#endif
Stelian Pop7f09c432007-01-13 23:04:31 +010074
Joe Perches50f581a2011-03-29 15:21:48 -070075#define dprintk(fmt, ...) \
76do { \
77 if (debug) \
78 pr_warn(fmt, ##__VA_ARGS__); \
malattia@linux.itb9a218b2007-04-09 10:19:06 +020079} while (0)
80
Mattia Dongili425ef5d2008-01-14 18:05:44 +090081#define SONY_LAPTOP_DRIVER_VERSION "0.6"
Alessandro Guido50f62af2007-01-13 23:04:34 +010082
malattia@linux.it33a04452007-04-09 19:26:03 +020083#define SONY_NC_CLASS "sony-nc"
84#define SONY_NC_HID "SNY5001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020085#define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
malattia@linux.it33a04452007-04-09 19:26:03 +020086
87#define SONY_PIC_CLASS "sony-pic"
88#define SONY_PIC_HID "SNY6001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020089#define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
Stelian Pop7f09c432007-01-13 23:04:31 +010090
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010091MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
malattia@linux.it33a04452007-04-09 19:26:03 +020092MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
Stelian Pop7f09c432007-01-13 23:04:31 +010093MODULE_LICENSE("GPL");
malattia@linux.it33a04452007-04-09 19:26:03 +020094MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
Stelian Pop7f09c432007-01-13 23:04:31 +010095
96static int debug;
97module_param(debug, int, 0);
98MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
Len Browna02d1c12007-02-07 15:34:02 -050099 "the development of this driver");
Stelian Pop7f09c432007-01-13 23:04:31 +0100100
malattia@linux.it33a04452007-04-09 19:26:03 +0200101static int no_spic; /* = 0 */
102module_param(no_spic, int, 0444);
103MODULE_PARM_DESC(no_spic,
104 "set this if you don't want to enable the SPIC device");
105
106static int compat; /* = 0 */
107module_param(compat, int, 0444);
108MODULE_PARM_DESC(compat,
malattia@linux.it7b153f32007-04-09 19:31:25 +0200109 "set this if you want to enable backward compatibility mode");
malattia@linux.it33a04452007-04-09 19:26:03 +0200110
111static unsigned long mask = 0xffffffff;
112module_param(mask, ulong, 0644);
113MODULE_PARM_DESC(mask,
114 "set this to the mask of event you want to enable (see doc)");
115
malattia@linux.it5f3d2892007-04-28 23:18:45 +0900116static int camera; /* = 0 */
117module_param(camera, int, 0444);
118MODULE_PARM_DESC(camera,
119 "set this to 1 to enable Motion Eye camera controls "
120 "(only use it if you have a C1VE or C1VN model)");
121
Mattia Dongilia64e62a2007-05-01 11:19:53 +0900122#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +0200123static int minor = -1;
124module_param(minor, int, 0);
125MODULE_PARM_DESC(minor,
126 "minor number of the misc device for the SPIC compatibility code, "
127 "default is -1 (automatic)");
128#endif
129
Josh Boyer6fe6ae52011-11-02 14:32:00 -0400130static int kbd_backlight = 1;
Mattia Dongilibf155712011-02-19 11:52:31 +0900131module_param(kbd_backlight, int, 0444);
132MODULE_PARM_DESC(kbd_backlight,
133 "set this to 0 to disable keyboard backlight, "
134 "1 to enable it (default: 0)");
135
136static int kbd_backlight_timeout; /* = 0 */
137module_param(kbd_backlight_timeout, int, 0444);
138MODULE_PARM_DESC(kbd_backlight_timeout,
139 "set this to 0 to set the default 10 seconds timeout, "
140 "1 for 30 seconds, 2 for 60 seconds and 3 to disable timeout "
141 "(default: 0)");
142
Marco Chiapperodf410d52011-04-05 23:38:34 +0900143static void sony_nc_kbd_backlight_resume(void);
Marco Chiappero5fe801a2012-05-19 22:35:48 +0900144static void sony_nc_kbd_backlight_setup(struct platform_device *pd);
145static int sony_nc_kbd_backlight_cleanup(struct platform_device *pd);
Marco Chiapperodf410d52011-04-05 23:38:34 +0900146
Marco Chiappero967145a2012-05-19 22:35:50 +0900147static int sony_nc_battery_care_setup(struct platform_device *pd,
148 unsigned int handle);
149static void sony_nc_battery_care_cleanup(struct platform_device *pd);
150
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900151enum sony_nc_rfkill {
152 SONY_WIFI,
153 SONY_BLUETOOTH,
154 SONY_WWAN,
155 SONY_WIMAX,
Johannes Berg19d337d2009-06-02 13:01:37 +0200156 N_SONY_RFKILL,
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900157};
158
Mattia Dongilid5a664a2009-12-17 00:08:35 +0900159static int sony_rfkill_handle;
Johannes Berg19d337d2009-06-02 13:01:37 +0200160static struct rfkill *sony_rfkill_devices[N_SONY_RFKILL];
161static int sony_rfkill_address[N_SONY_RFKILL] = {0x300, 0x500, 0x700, 0x900};
Marco Chiappero5fe801a2012-05-19 22:35:48 +0900162static void sony_nc_rfkill_setup(struct acpi_device *device);
163static void sony_nc_rfkill_cleanup(void);
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900164static void sony_nc_rfkill_update(void);
165
malattia@linux.it1549ee62007-04-09 10:19:08 +0200166/*********** Input Devices ***********/
167
168#define SONY_LAPTOP_BUF_SIZE 128
169struct sony_laptop_input_s {
170 atomic_t users;
171 struct input_dev *jog_dev;
172 struct input_dev *key_dev;
Stefani Seibold45465482009-12-21 14:37:26 -0800173 struct kfifo fifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200174 spinlock_t fifo_lock;
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800175 struct timer_list release_key_timer;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200176};
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900177
malattia@linux.it1549ee62007-04-09 10:19:08 +0200178static struct sony_laptop_input_s sony_laptop_input = {
179 .users = ATOMIC_INIT(0),
180};
181
182struct sony_laptop_keypress {
183 struct input_dev *dev;
184 int key;
185};
186
Mattia Dongilibc57f862007-07-20 02:01:57 +0900187/* Correspondance table between sonypi events
188 * and input layer indexes in the keymap
189 */
190static int sony_laptop_input_index[] = {
Mattia Dongili3eb87492008-01-14 18:05:45 +0900191 -1, /* 0 no event */
192 -1, /* 1 SONYPI_EVENT_JOGDIAL_DOWN */
193 -1, /* 2 SONYPI_EVENT_JOGDIAL_UP */
194 -1, /* 3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
195 -1, /* 4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */
196 -1, /* 5 SONYPI_EVENT_JOGDIAL_PRESSED */
197 -1, /* 6 SONYPI_EVENT_JOGDIAL_RELEASED */
198 0, /* 7 SONYPI_EVENT_CAPTURE_PRESSED */
199 1, /* 8 SONYPI_EVENT_CAPTURE_RELEASED */
200 2, /* 9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
201 3, /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
202 4, /* 11 SONYPI_EVENT_FNKEY_ESC */
203 5, /* 12 SONYPI_EVENT_FNKEY_F1 */
204 6, /* 13 SONYPI_EVENT_FNKEY_F2 */
205 7, /* 14 SONYPI_EVENT_FNKEY_F3 */
206 8, /* 15 SONYPI_EVENT_FNKEY_F4 */
207 9, /* 16 SONYPI_EVENT_FNKEY_F5 */
208 10, /* 17 SONYPI_EVENT_FNKEY_F6 */
209 11, /* 18 SONYPI_EVENT_FNKEY_F7 */
210 12, /* 19 SONYPI_EVENT_FNKEY_F8 */
211 13, /* 20 SONYPI_EVENT_FNKEY_F9 */
212 14, /* 21 SONYPI_EVENT_FNKEY_F10 */
213 15, /* 22 SONYPI_EVENT_FNKEY_F11 */
214 16, /* 23 SONYPI_EVENT_FNKEY_F12 */
215 17, /* 24 SONYPI_EVENT_FNKEY_1 */
216 18, /* 25 SONYPI_EVENT_FNKEY_2 */
217 19, /* 26 SONYPI_EVENT_FNKEY_D */
218 20, /* 27 SONYPI_EVENT_FNKEY_E */
219 21, /* 28 SONYPI_EVENT_FNKEY_F */
220 22, /* 29 SONYPI_EVENT_FNKEY_S */
221 23, /* 30 SONYPI_EVENT_FNKEY_B */
222 24, /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */
223 25, /* 32 SONYPI_EVENT_PKEY_P1 */
224 26, /* 33 SONYPI_EVENT_PKEY_P2 */
225 27, /* 34 SONYPI_EVENT_PKEY_P3 */
226 28, /* 35 SONYPI_EVENT_BACK_PRESSED */
227 -1, /* 36 SONYPI_EVENT_LID_CLOSED */
228 -1, /* 37 SONYPI_EVENT_LID_OPENED */
229 29, /* 38 SONYPI_EVENT_BLUETOOTH_ON */
230 30, /* 39 SONYPI_EVENT_BLUETOOTH_OFF */
231 31, /* 40 SONYPI_EVENT_HELP_PRESSED */
232 32, /* 41 SONYPI_EVENT_FNKEY_ONLY */
233 33, /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
234 34, /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */
235 35, /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
236 36, /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
237 37, /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
238 38, /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */
239 39, /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
240 40, /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
241 41, /* 50 SONYPI_EVENT_ZOOM_PRESSED */
242 42, /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */
243 43, /* 52 SONYPI_EVENT_MEYE_FACE */
244 44, /* 53 SONYPI_EVENT_MEYE_OPPOSITE */
245 45, /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */
246 46, /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */
247 -1, /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */
248 -1, /* 57 SONYPI_EVENT_BATTERY_INSERT */
249 -1, /* 58 SONYPI_EVENT_BATTERY_REMOVE */
250 -1, /* 59 SONYPI_EVENT_FNKEY_RELEASED */
251 47, /* 60 SONYPI_EVENT_WIRELESS_ON */
252 48, /* 61 SONYPI_EVENT_WIRELESS_OFF */
253 49, /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */
254 50, /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900255 51, /* 64 SONYPI_EVENT_CD_EJECT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900256 52, /* 65 SONYPI_EVENT_MODEKEY_PRESSED */
257 53, /* 66 SONYPI_EVENT_PKEY_P4 */
258 54, /* 67 SONYPI_EVENT_PKEY_P5 */
259 55, /* 68 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900260 56, /* 69 SONYPI_EVENT_VOLUME_INC_PRESSED */
261 57, /* 70 SONYPI_EVENT_VOLUME_DEC_PRESSED */
262 -1, /* 71 SONYPI_EVENT_BRIGHTNESS_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900263 58, /* 72 SONYPI_EVENT_MEDIA_PRESSED */
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900264 59, /* 72 SONYPI_EVENT_VENDOR_PRESSED */
Mattia Dongilibc57f862007-07-20 02:01:57 +0900265};
266
267static int sony_laptop_input_keycode_map[] = {
268 KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
269 KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
270 KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
271 KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
272 KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
273 KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
274 KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
275 KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
276 KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
277 KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
278 KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
279 KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
280 KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
281 KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
282 KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
283 KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
284 KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
285 KEY_FN_F1, /* 17 SONYPI_EVENT_FNKEY_1 */
286 KEY_FN_F2, /* 18 SONYPI_EVENT_FNKEY_2 */
287 KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
288 KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
289 KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
290 KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
291 KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
292 KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
293 KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
294 KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
295 KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
296 KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
297 KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
298 KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
299 KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
300 KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
301 KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
302 KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
303 KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
304 KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
305 KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
306 KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
307 KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
308 KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
309 KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
310 BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
311 KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
312 KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
313 KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
314 KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
315 KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
316 KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
Mattia Dongili3eb87492008-01-14 18:05:45 +0900317 KEY_ZOOMIN, /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900318 KEY_ZOOMOUT, /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900319 KEY_EJECTCD, /* 51 SONYPI_EVENT_CD_EJECT_PRESSED */
320 KEY_F13, /* 52 SONYPI_EVENT_MODEKEY_PRESSED */
321 KEY_PROG4, /* 53 SONYPI_EVENT_PKEY_P4 */
322 KEY_F14, /* 54 SONYPI_EVENT_PKEY_P5 */
323 KEY_F15, /* 55 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900324 KEY_VOLUMEUP, /* 56 SONYPI_EVENT_VOLUME_INC_PRESSED */
325 KEY_VOLUMEDOWN, /* 57 SONYPI_EVENT_VOLUME_DEC_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900326 KEY_MEDIA, /* 58 SONYPI_EVENT_MEDIA_PRESSED */
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900327 KEY_VENDOR, /* 59 SONYPI_EVENT_VENDOR_PRESSED */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200328};
329
330/* release buttons after a short delay if pressed */
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800331static void do_sony_laptop_release_key(unsigned long unused)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200332{
333 struct sony_laptop_keypress kp;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800334 unsigned long flags;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200335
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800336 spin_lock_irqsave(&sony_laptop_input.fifo_lock, flags);
337
338 if (kfifo_out(&sony_laptop_input.fifo,
339 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200340 input_report_key(kp.dev, kp.key, 0);
341 input_sync(kp.dev);
342 }
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800343
344 /* If there is something in the fifo schedule next release. */
345 if (kfifo_len(&sony_laptop_input.fifo) != 0)
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800346 mod_timer(&sony_laptop_input.release_key_timer,
347 jiffies + msecs_to_jiffies(10));
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800348
349 spin_unlock_irqrestore(&sony_laptop_input.fifo_lock, flags);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200350}
malattia@linux.it1549ee62007-04-09 10:19:08 +0200351
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200352/* forward event to the input subsystem */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200353static void sony_laptop_report_input_event(u8 event)
354{
355 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
356 struct input_dev *key_dev = sony_laptop_input.key_dev;
357 struct sony_laptop_keypress kp = { NULL };
John Hughes747a5622011-11-16 19:51:57 +0100358 int scancode = -1;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200359
Almer S. Tigelaara83021a2009-04-12 11:26:28 +0000360 if (event == SONYPI_EVENT_FNKEY_RELEASED ||
361 event == SONYPI_EVENT_ANYBUTTON_RELEASED) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200362 /* Nothing, not all VAIOs generate this event */
363 return;
364 }
365
366 /* report events */
367 switch (event) {
368 /* jog_dev events */
369 case SONYPI_EVENT_JOGDIAL_UP:
370 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
371 input_report_rel(jog_dev, REL_WHEEL, 1);
372 input_sync(jog_dev);
373 return;
374
375 case SONYPI_EVENT_JOGDIAL_DOWN:
376 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
377 input_report_rel(jog_dev, REL_WHEEL, -1);
378 input_sync(jog_dev);
379 return;
380
381 /* key_dev events */
382 case SONYPI_EVENT_JOGDIAL_PRESSED:
383 kp.key = BTN_MIDDLE;
384 kp.dev = jog_dev;
385 break;
386
387 default:
Adrian Bunkd399d132008-02-20 00:59:03 +0200388 if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
Mattia Dongilibc57f862007-07-20 02:01:57 +0900389 dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
390 break;
391 }
John Hughes747a5622011-11-16 19:51:57 +0100392 if ((scancode = sony_laptop_input_index[event]) != -1) {
393 kp.key = sony_laptop_input_keycode_map[scancode];
Mattia Dongilibc57f862007-07-20 02:01:57 +0900394 if (kp.key != KEY_UNKNOWN)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200395 kp.dev = key_dev;
Mattia Dongilibc57f862007-07-20 02:01:57 +0900396 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200397 break;
398 }
399
400 if (kp.dev) {
John Hughes747a5622011-11-16 19:51:57 +0100401 /* if we have a scancode we emit it so we can always
402 remap the key */
403 if (scancode != -1)
404 input_event(kp.dev, EV_MSC, MSC_SCAN, scancode);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200405 input_report_key(kp.dev, kp.key, 1);
406 input_sync(kp.dev);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200407
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800408 /* schedule key release */
409 kfifo_in_locked(&sony_laptop_input.fifo,
410 (unsigned char *)&kp, sizeof(kp),
411 &sony_laptop_input.fifo_lock);
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800412 mod_timer(&sony_laptop_input.release_key_timer,
413 jiffies + msecs_to_jiffies(10));
malattia@linux.it1549ee62007-04-09 10:19:08 +0200414 } else
415 dprintk("unknown input event %.2x\n", event);
416}
417
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500418static int sony_laptop_setup_input(struct acpi_device *acpi_device)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200419{
420 struct input_dev *jog_dev;
421 struct input_dev *key_dev;
422 int i;
423 int error;
424
425 /* don't run again if already initialized */
426 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
427 return 0;
428
429 /* kfifo */
430 spin_lock_init(&sony_laptop_input.fifo_lock);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800431 error = kfifo_alloc(&sony_laptop_input.fifo,
432 SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -0800433 if (error) {
Joe Perches50f581a2011-03-29 15:21:48 -0700434 pr_err("kfifo_alloc failed\n");
malattia@linux.it1549ee62007-04-09 10:19:08 +0200435 goto err_dec_users;
436 }
437
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800438 setup_timer(&sony_laptop_input.release_key_timer,
439 do_sony_laptop_release_key, 0);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200440
441 /* input keys */
442 key_dev = input_allocate_device();
443 if (!key_dev) {
444 error = -ENOMEM;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800445 goto err_free_kfifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200446 }
447
448 key_dev->name = "Sony Vaio Keys";
449 key_dev->id.bustype = BUS_ISA;
450 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500451 key_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200452
453 /* Initialize the Input Drivers: special keys */
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800454 input_set_capability(key_dev, EV_MSC, MSC_SCAN);
455
456 __set_bit(EV_KEY, key_dev->evbit);
Mattia Dongilibc57f862007-07-20 02:01:57 +0900457 key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
458 key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
459 key_dev->keycode = &sony_laptop_input_keycode_map;
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800460 for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++)
461 __set_bit(sony_laptop_input_keycode_map[i], key_dev->keybit);
462 __clear_bit(KEY_RESERVED, key_dev->keybit);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200463
464 error = input_register_device(key_dev);
465 if (error)
466 goto err_free_keydev;
467
468 sony_laptop_input.key_dev = key_dev;
469
470 /* jogdial */
471 jog_dev = input_allocate_device();
472 if (!jog_dev) {
473 error = -ENOMEM;
474 goto err_unregister_keydev;
475 }
476
477 jog_dev->name = "Sony Vaio Jogdial";
478 jog_dev->id.bustype = BUS_ISA;
479 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
John Hughes747a5622011-11-16 19:51:57 +0100480 jog_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200481
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800482 input_set_capability(jog_dev, EV_KEY, BTN_MIDDLE);
483 input_set_capability(jog_dev, EV_REL, REL_WHEEL);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200484
485 error = input_register_device(jog_dev);
486 if (error)
487 goto err_free_jogdev;
488
489 sony_laptop_input.jog_dev = jog_dev;
490
491 return 0;
492
493err_free_jogdev:
494 input_free_device(jog_dev);
495
496err_unregister_keydev:
497 input_unregister_device(key_dev);
498 /* to avoid kref underflow below at input_free_device */
499 key_dev = NULL;
500
501err_free_keydev:
502 input_free_device(key_dev);
503
malattia@linux.it1549ee62007-04-09 10:19:08 +0200504err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -0800505 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200506
507err_dec_users:
508 atomic_dec(&sony_laptop_input.users);
509 return error;
510}
511
512static void sony_laptop_remove_input(void)
513{
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800514 struct sony_laptop_keypress kp = { NULL };
515
516 /* Cleanup only after the last user has gone */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200517 if (!atomic_dec_and_test(&sony_laptop_input.users))
518 return;
519
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800520 del_timer_sync(&sony_laptop_input.release_key_timer);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800521
522 /*
523 * Generate key-up events for remaining keys. Note that we don't
524 * need locking since nobody is adding new events to the kfifo.
525 */
526 while (kfifo_out(&sony_laptop_input.fifo,
527 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
528 input_report_key(kp.dev, kp.key, 0);
529 input_sync(kp.dev);
530 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200531
532 /* destroy input devs */
533 input_unregister_device(sony_laptop_input.key_dev);
534 sony_laptop_input.key_dev = NULL;
535
536 if (sony_laptop_input.jog_dev) {
537 input_unregister_device(sony_laptop_input.jog_dev);
538 sony_laptop_input.jog_dev = NULL;
539 }
540
Stefani Seibold45465482009-12-21 14:37:26 -0800541 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200542}
543
malattia@linux.it56b87562007-04-09 10:19:05 +0200544/*********** Platform Device ***********/
545
546static atomic_t sony_pf_users = ATOMIC_INIT(0);
547static struct platform_driver sony_pf_driver = {
548 .driver = {
549 .name = "sony-laptop",
550 .owner = THIS_MODULE,
551 }
552};
553static struct platform_device *sony_pf_device;
554
555static int sony_pf_add(void)
556{
557 int ret = 0;
558
559 /* don't run again if already initialized */
560 if (atomic_add_return(1, &sony_pf_users) > 1)
561 return 0;
562
563 ret = platform_driver_register(&sony_pf_driver);
564 if (ret)
565 goto out;
566
567 sony_pf_device = platform_device_alloc("sony-laptop", -1);
568 if (!sony_pf_device) {
569 ret = -ENOMEM;
570 goto out_platform_registered;
571 }
572
573 ret = platform_device_add(sony_pf_device);
574 if (ret)
575 goto out_platform_alloced;
576
577 return 0;
578
579 out_platform_alloced:
580 platform_device_put(sony_pf_device);
581 sony_pf_device = NULL;
582 out_platform_registered:
583 platform_driver_unregister(&sony_pf_driver);
584 out:
585 atomic_dec(&sony_pf_users);
586 return ret;
587}
588
589static void sony_pf_remove(void)
590{
591 /* deregister only after the last user has gone */
592 if (!atomic_dec_and_test(&sony_pf_users))
593 return;
594
Axel Lin08db2b32010-07-01 10:18:01 +0800595 platform_device_unregister(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +0200596 platform_driver_unregister(&sony_pf_driver);
597}
598
599/*********** SNC (SNY5001) Device ***********/
600
malattia@linux.it33a04452007-04-09 19:26:03 +0200601/* the device uses 1-based values, while the backlight subsystem uses
602 0-based values */
603#define SONY_MAX_BRIGHTNESS 8
604
605#define SNC_VALIDATE_IN 0
606#define SNC_VALIDATE_OUT 1
607
malattia@linux.it59b19102007-04-09 10:19:04 +0200608static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500609 char *);
malattia@linux.it59b19102007-04-09 10:19:04 +0200610static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500611 const char *, size_t);
Mattia Dongili156c2212007-02-12 22:01:07 +0100612static int boolean_validate(const int, const int);
613static int brightness_default_validate(const int, const int);
614
malattia@linux.it59b19102007-04-09 10:19:04 +0200615struct sony_nc_value {
Len Browna02d1c12007-02-07 15:34:02 -0500616 char *name; /* name of the entry */
617 char **acpiget; /* names of the ACPI get function */
618 char **acpiset; /* names of the ACPI set function */
Mattia Dongili156c2212007-02-12 22:01:07 +0100619 int (*validate)(const int, const int); /* input/output validation */
Len Browna02d1c12007-02-07 15:34:02 -0500620 int value; /* current setting */
621 int valid; /* Has ever been set */
622 int debug; /* active only in debug mode ? */
Lucas De Marchic8440332011-03-17 17:18:22 -0300623 struct device_attribute devattr; /* sysfs attribute */
Stelian Pop7f09c432007-01-13 23:04:31 +0100624};
625
malattia@linux.it59b19102007-04-09 10:19:04 +0200626#define SNC_HANDLE_NAMES(_name, _values...) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100627 static char *snc_##_name[] = { _values, NULL }
628
malattia@linux.it59b19102007-04-09 10:19:04 +0200629#define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100630 { \
631 .name = __stringify(_name), \
632 .acpiget = _getters, \
633 .acpiset = _setters, \
Mattia Dongili156c2212007-02-12 22:01:07 +0100634 .validate = _validate, \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100635 .debug = _debug, \
malattia@linux.it59b19102007-04-09 10:19:04 +0200636 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100637 }
638
malattia@linux.it59b19102007-04-09 10:19:04 +0200639#define SNC_HANDLE_NULL { .name = NULL }
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100640
malattia@linux.it59b19102007-04-09 10:19:04 +0200641SNC_HANDLE_NAMES(fnkey_get, "GHKE");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100642
malattia@linux.it59b19102007-04-09 10:19:04 +0200643SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
644SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100645
malattia@linux.it59b19102007-04-09 10:19:04 +0200646SNC_HANDLE_NAMES(cdpower_get, "GCDP");
647SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100648
malattia@linux.it59b19102007-04-09 10:19:04 +0200649SNC_HANDLE_NAMES(audiopower_get, "GAZP");
650SNC_HANDLE_NAMES(audiopower_set, "AZPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100651
malattia@linux.it59b19102007-04-09 10:19:04 +0200652SNC_HANDLE_NAMES(lanpower_get, "GLNP");
653SNC_HANDLE_NAMES(lanpower_set, "LNPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100654
Mattia Dongili044847e2007-07-16 02:34:33 +0900655SNC_HANDLE_NAMES(lidstate_get, "GLID");
656
657SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
658SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
659
660SNC_HANDLE_NAMES(gainbass_get, "GMGB");
661SNC_HANDLE_NAMES(gainbass_set, "CMGB");
662
malattia@linux.it59b19102007-04-09 10:19:04 +0200663SNC_HANDLE_NAMES(PID_get, "GPID");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100664
malattia@linux.it59b19102007-04-09 10:19:04 +0200665SNC_HANDLE_NAMES(CTR_get, "GCTR");
666SNC_HANDLE_NAMES(CTR_set, "SCTR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100667
malattia@linux.it59b19102007-04-09 10:19:04 +0200668SNC_HANDLE_NAMES(PCR_get, "GPCR");
669SNC_HANDLE_NAMES(PCR_set, "SPCR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100670
malattia@linux.it59b19102007-04-09 10:19:04 +0200671SNC_HANDLE_NAMES(CMI_get, "GCMI");
672SNC_HANDLE_NAMES(CMI_set, "SCMI");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100673
malattia@linux.it59b19102007-04-09 10:19:04 +0200674static struct sony_nc_value sony_nc_values[] = {
675 SNC_HANDLE(brightness_default, snc_brightness_def_get,
Mattia Dongili156c2212007-02-12 22:01:07 +0100676 snc_brightness_def_set, brightness_default_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200677 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
678 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
679 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100680 boolean_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200681 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100682 boolean_validate, 1),
Mattia Dongili044847e2007-07-16 02:34:33 +0900683 SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
684 boolean_validate, 0),
685 SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
686 boolean_validate, 0),
687 SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
688 boolean_validate, 0),
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100689 /* unknown methods */
malattia@linux.it59b19102007-04-09 10:19:04 +0200690 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
691 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
692 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
693 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
694 SNC_HANDLE_NULL
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100695};
696
malattia@linux.it59b19102007-04-09 10:19:04 +0200697static acpi_handle sony_nc_acpi_handle;
698static struct acpi_device *sony_nc_acpi_device = NULL;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100699
Mattia Dongilid78865c2007-02-07 20:01:56 +0100700/*
701 * acpi_evaluate_object wrappers
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900702 * all useful calls into SNC methods take one or zero parameters and return
703 * integers or arrays.
Mattia Dongilid78865c2007-02-07 20:01:56 +0100704 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900705static union acpi_object *__call_snc_method(acpi_handle handle, char *method,
706 u64 *value)
Stelian Pop7f09c432007-01-13 23:04:31 +0100707{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900708 union acpi_object *result = NULL;
709 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
Stelian Pop7f09c432007-01-13 23:04:31 +0100710 acpi_status status;
711
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900712 if (value) {
713 struct acpi_object_list params;
714 union acpi_object in;
715 in.type = ACPI_TYPE_INTEGER;
716 in.integer.value = *value;
717 params.count = 1;
718 params.pointer = &in;
719 status = acpi_evaluate_object(handle, method, &params, &output);
Mattia Dongiliae188712012-05-19 22:35:49 +0900720 dprintk("__call_snc_method: [%s:0x%.8x%.8x]\n", method,
721 (unsigned int)(*value >> 32),
722 (unsigned int)*value & 0xffffffff);
723 } else {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900724 status = acpi_evaluate_object(handle, method, NULL, &output);
Mattia Dongiliae188712012-05-19 22:35:49 +0900725 dprintk("__call_snc_method: [%s]\n", method);
726 }
Stelian Pop7f09c432007-01-13 23:04:31 +0100727
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900728 if (ACPI_FAILURE(status)) {
729 pr_err("Failed to evaluate [%s]\n", method);
730 return NULL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100731 }
732
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900733 result = (union acpi_object *) output.pointer;
734 if (!result)
735 dprintk("No return object [%s]\n", method);
Stelian Pop7f09c432007-01-13 23:04:31 +0100736
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900737 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +0100738}
739
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900740static int sony_nc_int_call(acpi_handle handle, char *name, int *value,
741 int *result)
Stelian Pop7f09c432007-01-13 23:04:31 +0100742{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900743 union acpi_object *object = NULL;
744 if (value) {
745 u64 v = *value;
746 object = __call_snc_method(handle, name, &v);
747 } else
748 object = __call_snc_method(handle, name, NULL);
Stelian Pop7f09c432007-01-13 23:04:31 +0100749
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900750 if (!object)
751 return -EINVAL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100752
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900753 if (object->type != ACPI_TYPE_INTEGER) {
754 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
755 ACPI_TYPE_INTEGER, object->type);
756 kfree(object);
757 return -EINVAL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100758 }
759
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900760 if (result)
761 *result = object->integer.value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100762
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900763 kfree(object);
764 return 0;
765}
766
767#define MIN(a, b) (a > b ? b : a)
768static int sony_nc_buffer_call(acpi_handle handle, char *name, u64 *value,
769 void *buffer, size_t buflen)
770{
771 size_t len = len;
772 union acpi_object *object = __call_snc_method(handle, name, value);
773
774 if (!object)
775 return -EINVAL;
776
777 if (object->type == ACPI_TYPE_BUFFER)
778 len = MIN(buflen, object->buffer.length);
779
780 else if (object->type == ACPI_TYPE_INTEGER)
781 len = MIN(buflen, sizeof(object->integer.value));
782
783 else {
784 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
785 ACPI_TYPE_BUFFER, object->type);
786 kfree(object);
787 return -EINVAL;
788 }
789
790 memcpy(buffer, object->buffer.pointer, len);
791 kfree(object);
792 return 0;
Stelian Pop7f09c432007-01-13 23:04:31 +0100793}
794
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900795struct sony_nc_handles {
796 u16 cap[0x10];
797 struct device_attribute devattr;
798};
799
Dan Carpenterf11113b2011-02-26 15:54:27 +0300800static struct sony_nc_handles *handles;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900801
802static ssize_t sony_nc_handles_show(struct device *dev,
803 struct device_attribute *attr, char *buffer)
804{
805 ssize_t len = 0;
806 int i;
807
808 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
809 len += snprintf(buffer + len, PAGE_SIZE - len, "0x%.4x ",
810 handles->cap[i]);
811 }
812 len += snprintf(buffer + len, PAGE_SIZE - len, "\n");
813
814 return len;
815}
816
817static int sony_nc_handles_setup(struct platform_device *pd)
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900818{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900819 int i, r, result, arg;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900820
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900821 handles = kzalloc(sizeof(*handles), GFP_KERNEL);
Dan Carpenter31f00752011-02-26 15:55:24 +0300822 if (!handles)
823 return -ENOMEM;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900824
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900825 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900826 arg = i + 0x20;
827 r = sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg,
828 &result);
829 if (!r) {
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900830 dprintk("caching handle 0x%.4x (offset: 0x%.2x)\n",
831 result, i);
832 handles->cap[i] = result;
Mattia Dongili56e6e712011-02-19 11:52:25 +0900833 }
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900834 }
835
Mattia Dongili855b8bc2011-04-05 23:38:35 +0900836 if (debug) {
837 sysfs_attr_init(&handles->devattr.attr);
838 handles->devattr.attr.name = "handles";
839 handles->devattr.attr.mode = S_IRUGO;
840 handles->devattr.show = sony_nc_handles_show;
841
842 /* allow reading capabilities via sysfs */
843 if (device_create_file(&pd->dev, &handles->devattr)) {
844 kfree(handles);
845 handles = NULL;
846 return -1;
847 }
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900848 }
849
850 return 0;
851}
852
853static int sony_nc_handles_cleanup(struct platform_device *pd)
854{
855 if (handles) {
Mattia Dongili855b8bc2011-04-05 23:38:35 +0900856 if (debug)
857 device_remove_file(&pd->dev, &handles->devattr);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900858 kfree(handles);
859 handles = NULL;
860 }
861 return 0;
862}
863
864static int sony_find_snc_handle(int handle)
865{
866 int i;
Mattia Dongilifef34862011-04-02 19:00:44 +0900867
868 /* not initialized yet, return early */
869 if (!handles)
870 return -1;
871
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900872 for (i = 0; i < 0x10; i++) {
873 if (handles->cap[i] == handle) {
874 dprintk("found handle 0x%.4x (offset: 0x%.2x)\n",
875 handle, i);
876 return i;
877 }
878 }
Mattia Dongili56e6e712011-02-19 11:52:25 +0900879 dprintk("handle 0x%.4x not found\n", handle);
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900880 return -1;
881}
882
883static int sony_call_snc_handle(int handle, int argument, int *result)
884{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900885 int arg, ret = 0;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900886 int offset = sony_find_snc_handle(handle);
887
888 if (offset < 0)
889 return -1;
890
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900891 arg = offset | argument;
892 ret = sony_nc_int_call(sony_nc_acpi_handle, "SN07", &arg, result);
893 dprintk("called SN07 with 0x%.4x (result: 0x%.4x)\n", arg, *result);
Mattia Dongili56e6e712011-02-19 11:52:25 +0900894 return ret;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900895}
896
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100897/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200898 * sony_nc_values input/output validate functions
Mattia Dongili156c2212007-02-12 22:01:07 +0100899 */
900
901/* brightness_default_validate:
902 *
903 * manipulate input output values to keep consistency with the
904 * backlight framework for which brightness values are 0-based.
905 */
906static int brightness_default_validate(const int direction, const int value)
907{
908 switch (direction) {
909 case SNC_VALIDATE_OUT:
910 return value - 1;
911 case SNC_VALIDATE_IN:
912 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
913 return value + 1;
914 }
915 return -EINVAL;
916}
917
918/* boolean_validate:
919 *
920 * on input validate boolean values 0/1, on output just pass the
921 * received value.
922 */
923static int boolean_validate(const int direction, const int value)
924{
925 if (direction == SNC_VALIDATE_IN) {
926 if (value != 0 && value != 1)
927 return -EINVAL;
928 }
929 return value;
930}
931
932/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200933 * Sysfs show/store common to all sony_nc_values
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100934 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200935static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
Len Browna02d1c12007-02-07 15:34:02 -0500936 char *buffer)
Stelian Pop7f09c432007-01-13 23:04:31 +0100937{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900938 int value, ret = 0;
malattia@linux.it59b19102007-04-09 10:19:04 +0200939 struct sony_nc_value *item =
940 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100941
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100942 if (!*item->acpiget)
Stelian Pop7f09c432007-01-13 23:04:31 +0100943 return -EIO;
944
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900945 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiget, NULL,
946 &value);
947 if (ret < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100948 return -EIO;
949
Mattia Dongili156c2212007-02-12 22:01:07 +0100950 if (item->validate)
951 value = item->validate(SNC_VALIDATE_OUT, value);
952
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100953 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
Stelian Pop7f09c432007-01-13 23:04:31 +0100954}
955
malattia@linux.it59b19102007-04-09 10:19:04 +0200956static ssize_t sony_nc_sysfs_store(struct device *dev,
Len Browna02d1c12007-02-07 15:34:02 -0500957 struct device_attribute *attr,
958 const char *buffer, size_t count)
Stelian Pop7f09c432007-01-13 23:04:31 +0100959{
Mattia Dongili9e123372012-05-19 22:35:47 +0900960 unsigned long value = 0;
961 int ret = 0;
malattia@linux.it59b19102007-04-09 10:19:04 +0200962 struct sony_nc_value *item =
963 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100964
965 if (!item->acpiset)
966 return -EIO;
967
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100968 if (count > 31)
969 return -EINVAL;
970
Mattia Dongili9e123372012-05-19 22:35:47 +0900971 if (kstrtoul(buffer, 10, &value))
972 return -EINVAL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100973
Mattia Dongili156c2212007-02-12 22:01:07 +0100974 if (item->validate)
975 value = item->validate(SNC_VALIDATE_IN, value);
976
977 if (value < 0)
978 return value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100979
Mattia Dongili9e123372012-05-19 22:35:47 +0900980 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
981 (int *)&value, NULL);
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900982 if (ret < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100983 return -EIO;
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900984
Andrew Morton3f4f4612007-01-13 23:04:32 +0100985 item->value = value;
986 item->valid = 1;
Stelian Pop7f09c432007-01-13 23:04:31 +0100987 return count;
988}
989
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100990
Mattia Dongilid78865c2007-02-07 20:01:56 +0100991/*
992 * Backlight device
993 */
Mattia Dongili62d2f232011-05-09 10:20:29 -0400994struct sony_backlight_props {
995 struct backlight_device *dev;
996 int handle;
997 u8 offset;
998 u8 maxlvl;
999};
1000struct sony_backlight_props sony_bl_props;
1001
Mattia Dongilid78865c2007-02-07 20:01:56 +01001002static int sony_backlight_update_status(struct backlight_device *bd)
Andrew Morton3f4f4612007-01-13 23:04:32 +01001003{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001004 int arg = bd->props.brightness + 1;
1005 return sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &arg, NULL);
Andrew Morton3f4f4612007-01-13 23:04:32 +01001006}
1007
Mattia Dongilid78865c2007-02-07 20:01:56 +01001008static int sony_backlight_get_brightness(struct backlight_device *bd)
1009{
1010 int value;
1011
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001012 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL, &value))
Mattia Dongilid78865c2007-02-07 20:01:56 +01001013 return 0;
1014 /* brightness levels are 1-based, while backlight ones are 0-based */
1015 return value - 1;
1016}
1017
Mattia Dongili7751ab82011-02-19 11:52:32 +09001018static int sony_nc_get_brightness_ng(struct backlight_device *bd)
1019{
1020 int result;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001021 struct sony_backlight_props *sdev =
1022 (struct sony_backlight_props *)bl_get_data(bd);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001023
Mattia Dongili62d2f232011-05-09 10:20:29 -04001024 sony_call_snc_handle(sdev->handle, 0x0200, &result);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001025
Mattia Dongili62d2f232011-05-09 10:20:29 -04001026 return (result & 0xff) - sdev->offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001027}
1028
1029static int sony_nc_update_status_ng(struct backlight_device *bd)
1030{
1031 int value, result;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001032 struct sony_backlight_props *sdev =
1033 (struct sony_backlight_props *)bl_get_data(bd);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001034
Mattia Dongili62d2f232011-05-09 10:20:29 -04001035 value = bd->props.brightness + sdev->offset;
1036 if (sony_call_snc_handle(sdev->handle, 0x0100 | (value << 16), &result))
Mattia Dongili6192fa72011-04-05 23:38:36 +09001037 return -EIO;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001038
Mattia Dongili6192fa72011-04-05 23:38:36 +09001039 return value;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001040}
1041
Lionel Debrouxacc24722010-11-16 14:14:02 +01001042static const struct backlight_ops sony_backlight_ops = {
Mattia Dongili7751ab82011-02-19 11:52:32 +09001043 .options = BL_CORE_SUSPENDRESUME,
Len Browna02d1c12007-02-07 15:34:02 -05001044 .update_status = sony_backlight_update_status,
1045 .get_brightness = sony_backlight_get_brightness,
Mattia Dongilid78865c2007-02-07 20:01:56 +01001046};
Mattia Dongili7751ab82011-02-19 11:52:32 +09001047static const struct backlight_ops sony_backlight_ng_ops = {
1048 .options = BL_CORE_SUSPENDRESUME,
1049 .update_status = sony_nc_update_status_ng,
1050 .get_brightness = sony_nc_get_brightness_ng,
1051};
Mattia Dongilid78865c2007-02-07 20:01:56 +01001052
1053/*
Mattia Dongili6315fd12007-07-16 02:34:35 +09001054 * New SNC-only Vaios event mapping to driver known keys
1055 */
1056struct sony_nc_event {
1057 u8 data;
1058 u8 event;
1059};
1060
Matthew Garrett45c79422009-03-26 21:58:16 +09001061static struct sony_nc_event sony_100_events[] = {
Matthew Garrett9b578962009-03-26 21:58:14 +09001062 { 0x90, SONYPI_EVENT_PKEY_P1 },
1063 { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthias Welwarsky6479efb2009-04-01 22:10:45 +09001064 { 0x91, SONYPI_EVENT_PKEY_P2 },
Matthew Garrett9b578962009-03-26 21:58:14 +09001065 { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001066 { 0x81, SONYPI_EVENT_FNKEY_F1 },
1067 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
Anton Veretenenkof5acf5e2009-03-26 22:44:26 +09001068 { 0x82, SONYPI_EVENT_FNKEY_F2 },
1069 { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
1070 { 0x83, SONYPI_EVENT_FNKEY_F3 },
1071 { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
1072 { 0x84, SONYPI_EVENT_FNKEY_F4 },
1073 { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001074 { 0x85, SONYPI_EVENT_FNKEY_F5 },
1075 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
1076 { 0x86, SONYPI_EVENT_FNKEY_F6 },
1077 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
1078 { 0x87, SONYPI_EVENT_FNKEY_F7 },
1079 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +09001080 { 0x89, SONYPI_EVENT_FNKEY_F9 },
1081 { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001082 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
1083 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
1084 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
1085 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +09001086 { 0x9d, SONYPI_EVENT_ZOOM_PRESSED },
1087 { 0x1d, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +09001088 { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
1089 { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili4f924ba2009-12-17 00:08:33 +09001090 { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
1091 { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +09001092 { 0xa4, SONYPI_EVENT_CD_EJECT_PRESSED },
1093 { 0x24, SONYPI_EVENT_ANYBUTTON_RELEASED },
1094 { 0xa5, SONYPI_EVENT_VENDOR_PRESSED },
1095 { 0x25, SONYPI_EVENT_ANYBUTTON_RELEASED },
1096 { 0xa6, SONYPI_EVENT_HELP_PRESSED },
1097 { 0x26, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001098 { 0, 0 },
1099};
1100
Matthew Garrett45c79422009-03-26 21:58:16 +09001101static struct sony_nc_event sony_127_events[] = {
1102 { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
1103 { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
1104 { 0x82, SONYPI_EVENT_PKEY_P1 },
1105 { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
1106 { 0x83, SONYPI_EVENT_PKEY_P2 },
1107 { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
1108 { 0x84, SONYPI_EVENT_PKEY_P3 },
1109 { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
1110 { 0x85, SONYPI_EVENT_PKEY_P4 },
1111 { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
1112 { 0x86, SONYPI_EVENT_PKEY_P5 },
1113 { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett45c79422009-03-26 21:58:16 +09001114 { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
1115 { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
1116 { 0, 0 },
1117};
1118
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001119static int sony_nc_hotkeys_decode(u32 event, unsigned int handle)
1120{
1121 int ret = -EINVAL;
1122 unsigned int result = 0;
1123 struct sony_nc_event *key_event;
1124
1125 if (sony_call_snc_handle(handle, 0x200, &result)) {
1126 dprintk("Unable to decode event 0x%.2x 0x%.2x\n", handle,
1127 event);
1128 return -EINVAL;
1129 }
1130
1131 result &= 0xFF;
1132
1133 if (handle == 0x0100)
1134 key_event = sony_100_events;
1135 else
1136 key_event = sony_127_events;
1137
1138 for (; key_event->data; key_event++) {
1139 if (key_event->data == result) {
1140 ret = key_event->event;
1141 break;
1142 }
1143 }
1144
1145 if (!key_event->data)
1146 pr_info("Unknown hotkey 0x%.2x/0x%.2x (handle 0x%.2x)\n",
1147 event, result, handle);
1148
1149 return ret;
1150}
1151
Mattia Dongili6315fd12007-07-16 02:34:35 +09001152/*
Mattia Dongilid78865c2007-02-07 20:01:56 +01001153 * ACPI callbacks
1154 */
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001155static void sony_nc_notify(struct acpi_device *device, u32 event)
Stelian Pop7f09c432007-01-13 23:04:31 +01001156{
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001157 u32 real_ev = event;
1158 u8 ev_type = 0;
1159 dprintk("sony_nc_notify, event: 0x%.2x\n", event);
Mattia Dongili6315fd12007-07-16 02:34:35 +09001160
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001161 if (event >= 0x90) {
1162 unsigned int result = 0;
1163 unsigned int arg = 0;
1164 unsigned int handle = 0;
1165 unsigned int offset = event - 0x90;
Mattia Dongili6315fd12007-07-16 02:34:35 +09001166
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001167 if (offset >= ARRAY_SIZE(handles->cap)) {
1168 pr_err("Event 0x%x outside of capabilities list\n",
1169 event);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001170 return;
Matthew Garrett9b578962009-03-26 21:58:14 +09001171 }
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001172 handle = handles->cap[offset];
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001173
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001174 /* list of handles known for generating events */
1175 switch (handle) {
1176 /* hotkey event */
1177 case 0x0100:
1178 case 0x0127:
1179 ev_type = 1;
1180 real_ev = sony_nc_hotkeys_decode(event, handle);
1181
1182 if (real_ev > 0)
1183 sony_laptop_report_input_event(real_ev);
1184 else
1185 /* restore the original event for reporting */
1186 real_ev = event;
1187
1188 break;
1189
1190 /* wlan switch */
1191 case 0x0124:
1192 case 0x0135:
1193 /* events on this handle are reported when the
1194 * switch changes position or for battery
1195 * events. We'll notify both of them but only
1196 * update the rfkill device status when the
1197 * switch is moved.
1198 */
1199 ev_type = 2;
1200 sony_call_snc_handle(handle, 0x0100, &result);
1201 real_ev = result & 0x03;
1202
1203 /* hw switch event */
1204 if (real_ev == 1)
1205 sony_nc_rfkill_update();
1206
1207 break;
1208
1209 default:
1210 dprintk("Unknown event 0x%x for handle 0x%x\n",
1211 event, handle);
1212 break;
1213 }
1214
1215 /* clear the event (and the event reason when present) */
1216 arg = 1 << offset;
1217 sony_nc_int_call(sony_nc_acpi_handle, "SN05", &arg, &result);
1218
1219 } else {
1220 /* old style event */
1221 ev_type = 1;
1222 sony_laptop_report_input_event(real_ev);
1223 }
1224
1225 acpi_bus_generate_proc_event(sony_nc_acpi_device, ev_type, real_ev);
1226
1227 acpi_bus_generate_netlink_event(sony_nc_acpi_device->pnp.device_class,
1228 dev_name(&sony_nc_acpi_device->dev), ev_type, real_ev);
Stelian Pop7f09c432007-01-13 23:04:31 +01001229}
1230
1231static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
1232 void *context, void **return_value)
1233{
Lin Ming30823732008-12-16 16:59:35 +08001234 struct acpi_device_info *info;
Stelian Pop7f09c432007-01-13 23:04:31 +01001235
Bob Moore15b8dd52009-06-29 13:39:29 +08001236 if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
Joe Perches50f581a2011-03-29 15:21:48 -07001237 pr_warn("method: name: %4.4s, args %X\n",
Lin Ming30823732008-12-16 16:59:35 +08001238 (char *)&info->name, info->param_count);
1239
Bob Moore15b8dd52009-06-29 13:39:29 +08001240 kfree(info);
Lin Ming30823732008-12-16 16:59:35 +08001241 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001242
1243 return AE_OK;
1244}
1245
Mattia Dongilid78865c2007-02-07 20:01:56 +01001246/*
1247 * ACPI device
1248 */
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001249static void sony_nc_function_setup(struct acpi_device *device,
1250 struct platform_device *pf_device)
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001251{
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001252 unsigned int i, result, bitmask, arg;
1253
1254 if (!handles)
1255 return;
1256
1257 /* setup found handles here */
1258 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1259 unsigned int handle = handles->cap[i];
1260
1261 if (!handle)
1262 continue;
1263
1264 dprintk("setting up handle 0x%.4x\n", handle);
1265
1266 switch (handle) {
1267 case 0x0100:
1268 case 0x0101:
1269 case 0x0127:
1270 /* setup hotkeys */
1271 sony_call_snc_handle(handle, 0, &result);
1272 break;
1273 case 0x0102:
1274 /* setup hotkeys */
1275 sony_call_snc_handle(handle, 0x100, &result);
1276 break;
Marco Chiappero967145a2012-05-19 22:35:50 +09001277 case 0x0115:
1278 case 0x0136:
1279 case 0x013f:
1280 result = sony_nc_battery_care_setup(pf_device, handle);
1281 if (result)
1282 pr_err("couldn't set up battery care function (%d)\n",
1283 result);
1284 break;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001285 case 0x0124:
1286 case 0x0135:
1287 sony_nc_rfkill_setup(device);
1288 break;
1289 case 0x0137:
1290 sony_nc_kbd_backlight_setup(pf_device);
1291 break;
1292 default:
1293 continue;
1294 }
1295 }
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001296
1297 /* Enable all events */
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001298 arg = 0x10;
1299 if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1300 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1301 &result);
1302}
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001303
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001304static void sony_nc_function_cleanup(struct platform_device *pd)
1305{
1306 unsigned int i, result, bitmask, handle;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001307
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001308 /* get enabled events and disable them */
1309 sony_nc_int_call(sony_nc_acpi_handle, "SN01", NULL, &bitmask);
1310 sony_nc_int_call(sony_nc_acpi_handle, "SN03", &bitmask, &result);
1311
1312 /* cleanup handles here */
1313 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1314
1315 handle = handles->cap[i];
1316
1317 if (!handle)
1318 continue;
1319
1320 switch (handle) {
Marco Chiappero967145a2012-05-19 22:35:50 +09001321 case 0x0115:
1322 case 0x0136:
1323 case 0x013f:
1324 sony_nc_battery_care_cleanup(pd);
1325 break;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001326 case 0x0124:
1327 case 0x0135:
1328 sony_nc_rfkill_cleanup();
1329 break;
1330 case 0x0137:
1331 sony_nc_kbd_backlight_cleanup(pd);
1332 break;
1333 default:
1334 continue;
1335 }
1336 }
1337
1338 /* finally cleanup the handles list */
1339 sony_nc_handles_cleanup(pd);
1340}
1341
1342static void sony_nc_function_resume(void)
1343{
1344 unsigned int i, result, bitmask, arg;
1345
1346 dprintk("Resuming SNC device\n");
1347
1348 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1349 unsigned int handle = handles->cap[i];
1350
1351 if (!handle)
1352 continue;
1353
1354 switch (handle) {
1355 case 0x0100:
1356 case 0x0101:
1357 case 0x0127:
1358 /* re-enable hotkeys */
1359 sony_call_snc_handle(handle, 0, &result);
1360 break;
1361 case 0x0102:
1362 /* re-enable hotkeys */
1363 sony_call_snc_handle(handle, 0x100, &result);
1364 break;
1365 case 0x0124:
1366 case 0x0135:
1367 sony_nc_rfkill_update();
1368 break;
1369 case 0x0137:
1370 sony_nc_kbd_backlight_resume();
1371 break;
1372 default:
1373 continue;
1374 }
1375 }
1376
1377 /* Enable all events */
1378 arg = 0x10;
1379 if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1380 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1381 &result);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001382}
1383
malattia@linux.it59b19102007-04-09 10:19:04 +02001384static int sony_nc_resume(struct acpi_device *device)
Mattia Dongilid78865c2007-02-07 20:01:56 +01001385{
malattia@linux.it59b19102007-04-09 10:19:04 +02001386 struct sony_nc_value *item;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001387 acpi_handle handle;
Mattia Dongilid78865c2007-02-07 20:01:56 +01001388
malattia@linux.it59b19102007-04-09 10:19:04 +02001389 for (item = sony_nc_values; item->name; item++) {
Mattia Dongilid78865c2007-02-07 20:01:56 +01001390 int ret;
1391
1392 if (!item->valid)
1393 continue;
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001394 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
1395 &item->value, NULL);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001396 if (ret < 0) {
Joe Perches50f581a2011-03-29 15:21:48 -07001397 pr_err("%s: %d\n", __func__, ret);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001398 break;
1399 }
1400 }
Mattia Dongili6315fd12007-07-16 02:34:35 +09001401
Matthew Garrett82734bf2009-03-26 21:58:13 +09001402 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1403 &handle))) {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001404 int arg = 1;
1405 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
Matthew Garrett82734bf2009-03-26 21:58:13 +09001406 dprintk("ECON Method failed\n");
1407 }
1408
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001409 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001410 &handle)))
1411 sony_nc_function_resume();
Marco Chiapperodf410d52011-04-05 23:38:34 +09001412
Mattia Dongilid78865c2007-02-07 20:01:56 +01001413 return 0;
1414}
1415
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001416static void sony_nc_rfkill_cleanup(void)
1417{
1418 int i;
1419
Johannes Berg19d337d2009-06-02 13:01:37 +02001420 for (i = 0; i < N_SONY_RFKILL; i++) {
1421 if (sony_rfkill_devices[i]) {
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001422 rfkill_unregister(sony_rfkill_devices[i]);
Johannes Berg19d337d2009-06-02 13:01:37 +02001423 rfkill_destroy(sony_rfkill_devices[i]);
1424 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001425 }
1426}
1427
Johannes Berg19d337d2009-06-02 13:01:37 +02001428static int sony_nc_rfkill_set(void *data, bool blocked)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001429{
1430 int result;
1431 int argument = sony_rfkill_address[(long) data] + 0x100;
1432
Johannes Berg19d337d2009-06-02 13:01:37 +02001433 if (!blocked)
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001434 argument |= 0x030000;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001435
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001436 return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001437}
1438
Johannes Berg19d337d2009-06-02 13:01:37 +02001439static const struct rfkill_ops sony_rfkill_ops = {
1440 .set_block = sony_nc_rfkill_set,
1441};
1442
1443static int sony_nc_setup_rfkill(struct acpi_device *device,
1444 enum sony_nc_rfkill nc_type)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001445{
1446 int err = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001447 struct rfkill *rfk;
1448 enum rfkill_type type;
1449 const char *name;
Alan Jenkins50fab072009-09-24 20:15:24 +01001450 int result;
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001451 bool hwblock, swblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001452
Johannes Berg19d337d2009-06-02 13:01:37 +02001453 switch (nc_type) {
1454 case SONY_WIFI:
1455 type = RFKILL_TYPE_WLAN;
1456 name = "sony-wifi";
1457 break;
1458 case SONY_BLUETOOTH:
1459 type = RFKILL_TYPE_BLUETOOTH;
1460 name = "sony-bluetooth";
1461 break;
1462 case SONY_WWAN:
1463 type = RFKILL_TYPE_WWAN;
1464 name = "sony-wwan";
1465 break;
1466 case SONY_WIMAX:
1467 type = RFKILL_TYPE_WIMAX;
1468 name = "sony-wimax";
1469 break;
1470 default:
1471 return -EINVAL;
Mattia Dongili53005a02009-04-12 11:26:31 +00001472 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001473
Johannes Berg19d337d2009-06-02 13:01:37 +02001474 rfk = rfkill_alloc(name, &device->dev, type,
1475 &sony_rfkill_ops, (void *)nc_type);
1476 if (!rfk)
1477 return -ENOMEM;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001478
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001479 if (sony_call_snc_handle(sony_rfkill_handle, 0x200, &result) < 0) {
1480 rfkill_destroy(rfk);
1481 return -1;
1482 }
Alan Jenkins50fab072009-09-24 20:15:24 +01001483 hwblock = !(result & 0x1);
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001484
1485 if (sony_call_snc_handle(sony_rfkill_handle,
1486 sony_rfkill_address[nc_type],
1487 &result) < 0) {
1488 rfkill_destroy(rfk);
1489 return -1;
1490 }
1491 swblock = !(result & 0x2);
1492
1493 rfkill_init_sw_state(rfk, swblock);
Alan Jenkins50fab072009-09-24 20:15:24 +01001494 rfkill_set_hw_state(rfk, hwblock);
1495
Johannes Berg19d337d2009-06-02 13:01:37 +02001496 err = rfkill_register(rfk);
1497 if (err) {
1498 rfkill_destroy(rfk);
1499 return err;
Mattia Dongili53005a02009-04-12 11:26:31 +00001500 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001501 sony_rfkill_devices[nc_type] = rfk;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001502 return err;
1503}
1504
Randy Dunlapa46a7802011-01-08 19:56:44 -08001505static void sony_nc_rfkill_update(void)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001506{
Johannes Berg19d337d2009-06-02 13:01:37 +02001507 enum sony_nc_rfkill i;
1508 int result;
1509 bool hwblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001510
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001511 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001512 hwblock = !(result & 0x1);
1513
1514 for (i = 0; i < N_SONY_RFKILL; i++) {
1515 int argument = sony_rfkill_address[i];
1516
1517 if (!sony_rfkill_devices[i])
1518 continue;
1519
1520 if (hwblock) {
Johannes Berge1f8a192009-06-11 12:08:15 +02001521 if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
1522 /* we already know we're blocked */
1523 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001524 continue;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001525 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001526
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001527 sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001528 rfkill_set_states(sony_rfkill_devices[i],
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001529 !(result & 0x2), false);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001530 }
1531}
1532
Mattia Dongili528809c2009-12-17 00:08:36 +09001533static void sony_nc_rfkill_setup(struct acpi_device *device)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001534{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001535 u64 offset;
1536 int i;
1537 unsigned char buffer[32] = { 0 };
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001538
Mattia Dongili528809c2009-12-17 00:08:36 +09001539 offset = sony_find_snc_handle(0x124);
1540 if (offset == -1) {
1541 offset = sony_find_snc_handle(0x135);
1542 if (offset == -1)
1543 return;
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001544 else
1545 sony_rfkill_handle = 0x135;
1546 } else
1547 sony_rfkill_handle = 0x124;
Mattia Dongili528809c2009-12-17 00:08:36 +09001548 dprintk("Found rkfill handle: 0x%.4x\n", sony_rfkill_handle);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001549
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001550 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
1551 32);
1552 if (i < 0)
Mattia Dongili528809c2009-12-17 00:08:36 +09001553 return;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001554
Mattia Dongili528809c2009-12-17 00:08:36 +09001555 /* the buffer is filled with magic numbers describing the devices
1556 * available, 0xff terminates the enumeration
1557 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001558 for (i = 0; i < ARRAY_SIZE(buffer); i++) {
Dmitry Torokhovc14973f2010-01-10 00:15:44 -08001559
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001560 if (buffer[i] == 0xff)
Dmitry Torokhovc14973f2010-01-10 00:15:44 -08001561 break;
1562
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001563 dprintk("Radio devices, looking at 0x%.2x\n", buffer[i]);
Mattia Dongili528809c2009-12-17 00:08:36 +09001564
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001565 if (buffer[i] == 0 && !sony_rfkill_devices[SONY_WIFI])
Mattia Dongili528809c2009-12-17 00:08:36 +09001566 sony_nc_setup_rfkill(device, SONY_WIFI);
1567
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001568 if (buffer[i] == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
Mattia Dongili528809c2009-12-17 00:08:36 +09001569 sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
1570
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001571 if ((0xf0 & buffer[i]) == 0x20 &&
Mattia Dongili528809c2009-12-17 00:08:36 +09001572 !sony_rfkill_devices[SONY_WWAN])
1573 sony_nc_setup_rfkill(device, SONY_WWAN);
1574
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001575 if (buffer[i] == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
Mattia Dongili528809c2009-12-17 00:08:36 +09001576 sony_nc_setup_rfkill(device, SONY_WIMAX);
1577 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001578}
1579
Mattia Dongilibf155712011-02-19 11:52:31 +09001580/* Keyboard backlight feature */
1581#define KBDBL_HANDLER 0x137
1582#define KBDBL_PRESENT 0xB00
1583#define SET_MODE 0xC00
Marco Chiapperodf410d52011-04-05 23:38:34 +09001584#define SET_STATE 0xD00
Mattia Dongilibf155712011-02-19 11:52:31 +09001585#define SET_TIMEOUT 0xE00
1586
1587struct kbd_backlight {
1588 int mode;
1589 int timeout;
1590 struct device_attribute mode_attr;
1591 struct device_attribute timeout_attr;
1592};
1593
Dan Carpenterf11113b2011-02-26 15:54:27 +03001594static struct kbd_backlight *kbdbl_handle;
Mattia Dongilibf155712011-02-19 11:52:31 +09001595
1596static ssize_t __sony_nc_kbd_backlight_mode_set(u8 value)
1597{
1598 int result;
1599
1600 if (value > 1)
1601 return -EINVAL;
1602
1603 if (sony_call_snc_handle(KBDBL_HANDLER,
1604 (value << 0x10) | SET_MODE, &result))
1605 return -EIO;
1606
Marco Chiapperodf410d52011-04-05 23:38:34 +09001607 /* Try to turn the light on/off immediately */
1608 sony_call_snc_handle(KBDBL_HANDLER, (value << 0x10) | SET_STATE,
1609 &result);
1610
Mattia Dongilibf155712011-02-19 11:52:31 +09001611 kbdbl_handle->mode = value;
1612
1613 return 0;
1614}
1615
1616static ssize_t sony_nc_kbd_backlight_mode_store(struct device *dev,
1617 struct device_attribute *attr,
1618 const char *buffer, size_t count)
1619{
1620 int ret = 0;
1621 unsigned long value;
1622
1623 if (count > 31)
1624 return -EINVAL;
1625
Mattia Dongili9e123372012-05-19 22:35:47 +09001626 if (kstrtoul(buffer, 10, &value))
Mattia Dongilibf155712011-02-19 11:52:31 +09001627 return -EINVAL;
1628
1629 ret = __sony_nc_kbd_backlight_mode_set(value);
1630 if (ret < 0)
1631 return ret;
1632
1633 return count;
1634}
1635
1636static ssize_t sony_nc_kbd_backlight_mode_show(struct device *dev,
1637 struct device_attribute *attr, char *buffer)
1638{
1639 ssize_t count = 0;
1640 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_handle->mode);
1641 return count;
1642}
1643
1644static int __sony_nc_kbd_backlight_timeout_set(u8 value)
1645{
1646 int result;
1647
1648 if (value > 3)
1649 return -EINVAL;
1650
1651 if (sony_call_snc_handle(KBDBL_HANDLER,
1652 (value << 0x10) | SET_TIMEOUT, &result))
1653 return -EIO;
1654
1655 kbdbl_handle->timeout = value;
1656
1657 return 0;
1658}
1659
1660static ssize_t sony_nc_kbd_backlight_timeout_store(struct device *dev,
1661 struct device_attribute *attr,
1662 const char *buffer, size_t count)
1663{
1664 int ret = 0;
1665 unsigned long value;
1666
1667 if (count > 31)
1668 return -EINVAL;
1669
Mattia Dongili9e123372012-05-19 22:35:47 +09001670 if (kstrtoul(buffer, 10, &value))
Mattia Dongilibf155712011-02-19 11:52:31 +09001671 return -EINVAL;
1672
1673 ret = __sony_nc_kbd_backlight_timeout_set(value);
1674 if (ret < 0)
1675 return ret;
1676
1677 return count;
1678}
1679
1680static ssize_t sony_nc_kbd_backlight_timeout_show(struct device *dev,
1681 struct device_attribute *attr, char *buffer)
1682{
1683 ssize_t count = 0;
1684 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_handle->timeout);
1685 return count;
1686}
1687
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001688static void sony_nc_kbd_backlight_setup(struct platform_device *pd)
Mattia Dongilibf155712011-02-19 11:52:31 +09001689{
1690 int result;
1691
Marco Chiapperodf410d52011-04-05 23:38:34 +09001692 if (sony_call_snc_handle(KBDBL_HANDLER, KBDBL_PRESENT, &result))
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001693 return;
Mattia Dongilibf155712011-02-19 11:52:31 +09001694 if (!(result & 0x02))
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001695 return;
Mattia Dongilibf155712011-02-19 11:52:31 +09001696
1697 kbdbl_handle = kzalloc(sizeof(*kbdbl_handle), GFP_KERNEL);
Dan Carpenter31f00752011-02-26 15:55:24 +03001698 if (!kbdbl_handle)
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001699 return;
Mattia Dongilibf155712011-02-19 11:52:31 +09001700
1701 sysfs_attr_init(&kbdbl_handle->mode_attr.attr);
1702 kbdbl_handle->mode_attr.attr.name = "kbd_backlight";
1703 kbdbl_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
1704 kbdbl_handle->mode_attr.show = sony_nc_kbd_backlight_mode_show;
1705 kbdbl_handle->mode_attr.store = sony_nc_kbd_backlight_mode_store;
1706
1707 sysfs_attr_init(&kbdbl_handle->timeout_attr.attr);
1708 kbdbl_handle->timeout_attr.attr.name = "kbd_backlight_timeout";
1709 kbdbl_handle->timeout_attr.attr.mode = S_IRUGO | S_IWUSR;
1710 kbdbl_handle->timeout_attr.show = sony_nc_kbd_backlight_timeout_show;
1711 kbdbl_handle->timeout_attr.store = sony_nc_kbd_backlight_timeout_store;
1712
1713 if (device_create_file(&pd->dev, &kbdbl_handle->mode_attr))
1714 goto outkzalloc;
1715
1716 if (device_create_file(&pd->dev, &kbdbl_handle->timeout_attr))
1717 goto outmode;
1718
1719 __sony_nc_kbd_backlight_mode_set(kbd_backlight);
1720 __sony_nc_kbd_backlight_timeout_set(kbd_backlight_timeout);
1721
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001722 return;
Mattia Dongilibf155712011-02-19 11:52:31 +09001723
1724outmode:
1725 device_remove_file(&pd->dev, &kbdbl_handle->mode_attr);
1726outkzalloc:
1727 kfree(kbdbl_handle);
1728 kbdbl_handle = NULL;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001729 return;
Mattia Dongilibf155712011-02-19 11:52:31 +09001730}
1731
1732static int sony_nc_kbd_backlight_cleanup(struct platform_device *pd)
1733{
1734 if (kbdbl_handle) {
Marco Chiapperodf410d52011-04-05 23:38:34 +09001735 int result;
1736
Mattia Dongilibf155712011-02-19 11:52:31 +09001737 device_remove_file(&pd->dev, &kbdbl_handle->mode_attr);
1738 device_remove_file(&pd->dev, &kbdbl_handle->timeout_attr);
Marco Chiapperodf410d52011-04-05 23:38:34 +09001739
1740 /* restore the default hw behaviour */
1741 sony_call_snc_handle(KBDBL_HANDLER, 0x1000 | SET_MODE, &result);
1742 sony_call_snc_handle(KBDBL_HANDLER, SET_TIMEOUT, &result);
1743
Mattia Dongilibf155712011-02-19 11:52:31 +09001744 kfree(kbdbl_handle);
1745 }
1746 return 0;
1747}
1748
Marco Chiapperodf410d52011-04-05 23:38:34 +09001749static void sony_nc_kbd_backlight_resume(void)
1750{
1751 int ignore = 0;
1752
1753 if (!kbdbl_handle)
1754 return;
1755
1756 if (kbdbl_handle->mode == 0)
1757 sony_call_snc_handle(KBDBL_HANDLER, SET_MODE, &ignore);
1758
1759 if (kbdbl_handle->timeout != 0)
1760 sony_call_snc_handle(KBDBL_HANDLER,
1761 (kbdbl_handle->timeout << 0x10) | SET_TIMEOUT,
1762 &ignore);
1763}
1764
Marco Chiappero967145a2012-05-19 22:35:50 +09001765struct battery_care_control {
1766 struct device_attribute attrs[2];
1767 unsigned int handle;
1768};
1769static struct battery_care_control *bcare_ctl;
1770
1771static ssize_t sony_nc_battery_care_limit_store(struct device *dev,
1772 struct device_attribute *attr,
1773 const char *buffer, size_t count)
1774{
1775 unsigned int result, cmd;
1776 unsigned long value;
1777
1778 if (count > 31)
1779 return -EINVAL;
1780
1781 if (kstrtoul(buffer, 10, &value))
1782 return -EINVAL;
1783
1784 /* limit values (2 bits):
1785 * 00 - none
1786 * 01 - 80%
1787 * 10 - 50%
1788 * 11 - 100%
1789 *
1790 * bit 0: 0 disable BCL, 1 enable BCL
1791 * bit 1: 1 tell to store the battery limit (see bits 6,7) too
1792 * bits 2,3: reserved
1793 * bits 4,5: store the limit into the EC
1794 * bits 6,7: store the limit into the battery
1795 */
1796
1797 /*
1798 * handle 0x0115 should allow storing on battery too;
1799 * handle 0x0136 same as 0x0115 + health status;
1800 * handle 0x013f, same as 0x0136 but no storing on the battery
1801 *
1802 * Store only inside the EC for now, regardless the handle number
1803 */
1804 if (value == 0)
1805 /* disable limits */
1806 cmd = 0x0;
1807
1808 else if (value <= 50)
1809 cmd = 0x21;
1810
1811 else if (value <= 80)
1812 cmd = 0x11;
1813
1814 else if (value <= 100)
1815 cmd = 0x31;
1816
1817 else
1818 return -EINVAL;
1819
1820 if (sony_call_snc_handle(bcare_ctl->handle, (cmd << 0x10) | 0x0100,
1821 &result))
1822 return -EIO;
1823
1824 return count;
1825}
1826
1827static ssize_t sony_nc_battery_care_limit_show(struct device *dev,
1828 struct device_attribute *attr, char *buffer)
1829{
1830 unsigned int result, status;
1831
1832 if (sony_call_snc_handle(bcare_ctl->handle, 0x0000, &result))
1833 return -EIO;
1834
1835 status = (result & 0x01) ? ((result & 0x30) >> 0x04) : 0;
1836 switch (status) {
1837 case 1:
1838 status = 80;
1839 break;
1840 case 2:
1841 status = 50;
1842 break;
1843 case 3:
1844 status = 100;
1845 break;
1846 default:
1847 status = 0;
1848 break;
1849 }
1850
1851 return snprintf(buffer, PAGE_SIZE, "%d\n", status);
1852}
1853
1854static ssize_t sony_nc_battery_care_health_show(struct device *dev,
1855 struct device_attribute *attr, char *buffer)
1856{
1857 ssize_t count = 0;
1858 unsigned int health;
1859
1860 if (sony_call_snc_handle(bcare_ctl->handle, 0x0200, &health))
1861 return -EIO;
1862
1863 count = snprintf(buffer, PAGE_SIZE, "%d\n", health & 0xff);
1864
1865 return count;
1866}
1867
1868static int sony_nc_battery_care_setup(struct platform_device *pd,
1869 unsigned int handle)
1870{
1871 int ret = 0;
1872
1873 bcare_ctl = kzalloc(sizeof(struct battery_care_control), GFP_KERNEL);
1874 if (!bcare_ctl)
1875 return -ENOMEM;
1876
1877 bcare_ctl->handle = handle;
1878
1879 sysfs_attr_init(&bcare_ctl->attrs[0].attr);
1880 bcare_ctl->attrs[0].attr.name = "battery_care_limiter";
1881 bcare_ctl->attrs[0].attr.mode = S_IRUGO | S_IWUSR;
1882 bcare_ctl->attrs[0].show = sony_nc_battery_care_limit_show;
1883 bcare_ctl->attrs[0].store = sony_nc_battery_care_limit_store;
1884
1885 ret = device_create_file(&pd->dev, &bcare_ctl->attrs[0]);
1886 if (ret)
1887 goto outkzalloc;
1888
1889 /* 0x0115 is for models with no health reporting capability */
1890 if (handle == 0x0115)
1891 return 0;
1892
1893 sysfs_attr_init(&bcare_ctl->attrs[1].attr);
1894 bcare_ctl->attrs[1].attr.name = "battery_care_health";
1895 bcare_ctl->attrs[1].attr.mode = S_IRUGO;
1896 bcare_ctl->attrs[1].show = sony_nc_battery_care_health_show;
1897
1898 ret = device_create_file(&pd->dev, &bcare_ctl->attrs[1]);
1899 if (ret)
1900 goto outlimiter;
1901
1902 return 0;
1903
1904outlimiter:
1905 device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
1906
1907outkzalloc:
1908 kfree(bcare_ctl);
1909 bcare_ctl = NULL;
1910
1911 return ret;
1912}
1913
1914static void sony_nc_battery_care_cleanup(struct platform_device *pd)
1915{
1916 if (bcare_ctl) {
1917 device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
1918 if (bcare_ctl->handle != 0x0115)
1919 device_remove_file(&pd->dev, &bcare_ctl->attrs[1]);
1920
1921 kfree(bcare_ctl);
1922 bcare_ctl = NULL;
1923 }
1924}
1925
Mattia Dongili62d2f232011-05-09 10:20:29 -04001926static void sony_nc_backlight_ng_read_limits(int handle,
1927 struct sony_backlight_props *props)
1928{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001929 u64 offset;
1930 int i;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001931 u8 min = 0xff, max = 0x00;
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001932 unsigned char buffer[32] = { 0 };
Mattia Dongili62d2f232011-05-09 10:20:29 -04001933
1934 props->handle = handle;
1935 props->offset = 0;
1936 props->maxlvl = 0xff;
1937
1938 offset = sony_find_snc_handle(handle);
1939 if (offset < 0)
1940 return;
1941
1942 /* try to read the boundaries from ACPI tables, if we fail the above
1943 * defaults should be reasonable
1944 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001945 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
1946 32);
1947 if (i < 0)
Mattia Dongili62d2f232011-05-09 10:20:29 -04001948 return;
1949
Mattia Dongili62d2f232011-05-09 10:20:29 -04001950 /* the buffer lists brightness levels available, brightness levels are
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001951 * from position 0 to 8 in the array, other values are used by ALS
1952 * control.
Mattia Dongili62d2f232011-05-09 10:20:29 -04001953 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001954 for (i = 0; i < 9 && i < ARRAY_SIZE(buffer); i++) {
Mattia Dongili62d2f232011-05-09 10:20:29 -04001955
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001956 dprintk("Brightness level: %d\n", buffer[i]);
Mattia Dongili62d2f232011-05-09 10:20:29 -04001957
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001958 if (!buffer[i])
Mattia Dongili62d2f232011-05-09 10:20:29 -04001959 break;
1960
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001961 if (buffer[i] > max)
1962 max = buffer[i];
1963 if (buffer[i] < min)
1964 min = buffer[i];
Mattia Dongili62d2f232011-05-09 10:20:29 -04001965 }
1966 props->offset = min;
1967 props->maxlvl = max;
1968 dprintk("Brightness levels: min=%d max=%d\n", props->offset,
1969 props->maxlvl);
Mattia Dongili62d2f232011-05-09 10:20:29 -04001970}
1971
Mattia Dongili7751ab82011-02-19 11:52:32 +09001972static void sony_nc_backlight_setup(void)
1973{
1974 acpi_handle unused;
1975 int max_brightness = 0;
1976 const struct backlight_ops *ops = NULL;
1977 struct backlight_properties props;
1978
1979 if (sony_find_snc_handle(0x12f) != -1) {
Mattia Dongili7751ab82011-02-19 11:52:32 +09001980 ops = &sony_backlight_ng_ops;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001981 sony_nc_backlight_ng_read_limits(0x12f, &sony_bl_props);
1982 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001983
1984 } else if (sony_find_snc_handle(0x137) != -1) {
Mattia Dongili7751ab82011-02-19 11:52:32 +09001985 ops = &sony_backlight_ng_ops;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001986 sony_nc_backlight_ng_read_limits(0x137, &sony_bl_props);
1987 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001988
1989 } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
1990 &unused))) {
1991 ops = &sony_backlight_ops;
1992 max_brightness = SONY_MAX_BRIGHTNESS - 1;
1993
1994 } else
1995 return;
1996
1997 memset(&props, 0, sizeof(struct backlight_properties));
1998 props.type = BACKLIGHT_PLATFORM;
1999 props.max_brightness = max_brightness;
Mattia Dongili62d2f232011-05-09 10:20:29 -04002000 sony_bl_props.dev = backlight_device_register("sony", NULL,
2001 &sony_bl_props,
2002 ops, &props);
Mattia Dongili7751ab82011-02-19 11:52:32 +09002003
Mattia Dongili62d2f232011-05-09 10:20:29 -04002004 if (IS_ERR(sony_bl_props.dev)) {
Joe Perches50f581a2011-03-29 15:21:48 -07002005 pr_warn("unable to register backlight device\n");
Mattia Dongili62d2f232011-05-09 10:20:29 -04002006 sony_bl_props.dev = NULL;
Mattia Dongili7751ab82011-02-19 11:52:32 +09002007 } else
Mattia Dongili62d2f232011-05-09 10:20:29 -04002008 sony_bl_props.dev->props.brightness =
2009 ops->get_brightness(sony_bl_props.dev);
Mattia Dongili7751ab82011-02-19 11:52:32 +09002010}
2011
2012static void sony_nc_backlight_cleanup(void)
2013{
Mattia Dongili62d2f232011-05-09 10:20:29 -04002014 if (sony_bl_props.dev)
2015 backlight_device_unregister(sony_bl_props.dev);
Mattia Dongili7751ab82011-02-19 11:52:32 +09002016}
2017
malattia@linux.it59b19102007-04-09 10:19:04 +02002018static int sony_nc_add(struct acpi_device *device)
Stelian Pop7f09c432007-01-13 23:04:31 +01002019{
2020 acpi_status status;
Andrew Morton8607c672007-03-06 02:29:42 -08002021 int result = 0;
Alessandro Guido50f62af2007-01-13 23:04:34 +01002022 acpi_handle handle;
malattia@linux.it56b87562007-04-09 10:19:05 +02002023 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01002024
Joe Perches50f581a2011-03-29 15:21:48 -07002025 pr_info("%s v%s\n", SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.itf6119b02007-04-09 19:31:06 +02002026
malattia@linux.it59b19102007-04-09 10:19:04 +02002027 sony_nc_acpi_device = device;
malattia@linux.it33a04452007-04-09 19:26:03 +02002028 strcpy(acpi_device_class(device), "sony/hotkey");
Stelian Popc5611622007-01-13 23:04:37 +01002029
malattia@linux.it59b19102007-04-09 10:19:04 +02002030 sony_nc_acpi_handle = device->handle;
Stelian Pop7f09c432007-01-13 23:04:31 +01002031
Mattia Dongilib25b7322007-07-16 02:34:36 +09002032 /* read device status */
2033 result = acpi_bus_get_status(device);
2034 /* bail IFF the above call was successful and the device is not present */
2035 if (!result && !device->status.present) {
2036 dprintk("Device not present\n");
2037 result = -ENODEV;
2038 goto outwalk;
2039 }
2040
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002041 result = sony_pf_add();
2042 if (result)
2043 goto outpresent;
2044
Stelian Pop7f09c432007-01-13 23:04:31 +01002045 if (debug) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002046 status = acpi_walk_namespace(ACPI_TYPE_METHOD,
2047 sony_nc_acpi_handle, 1, sony_walk_callback,
2048 NULL, NULL, NULL);
Stelian Pop7f09c432007-01-13 23:04:31 +01002049 if (ACPI_FAILURE(status)) {
Joe Perches50f581a2011-03-29 15:21:48 -07002050 pr_warn("unable to walk acpi resources\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01002051 result = -ENODEV;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002052 goto outpresent;
Stelian Pop7f09c432007-01-13 23:04:31 +01002053 }
Stelian Popc5611622007-01-13 23:04:37 +01002054 }
Stelian Pop7f09c432007-01-13 23:04:31 +01002055
Matthew Garrett82734bf2009-03-26 21:58:13 +09002056 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
2057 &handle))) {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002058 int arg = 1;
2059 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
Matthew Garrett82734bf2009-03-26 21:58:13 +09002060 dprintk("ECON Method failed\n");
2061 }
2062
Matthew Garrettbadf26f2009-03-26 21:58:12 +09002063 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
2064 &handle))) {
2065 dprintk("Doing SNC setup\n");
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002066 /* retrieve the available handles */
Dan Carpenter7227ded2011-02-26 15:54:57 +03002067 result = sony_nc_handles_setup(sony_pf_device);
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002068 if (!result)
2069 sony_nc_function_setup(device, sony_pf_device);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09002070 }
2071
malattia@linux.it1549ee62007-04-09 10:19:08 +02002072 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05002073 result = sony_laptop_setup_input(device);
malattia@linux.it1549ee62007-04-09 10:19:08 +02002074 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07002075 pr_err("Unable to create input devices\n");
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002076 goto outsnc;
malattia@linux.it1549ee62007-04-09 10:19:08 +02002077 }
2078
Alessandro Guido38cfc142008-11-12 23:03:28 +01002079 if (acpi_video_backlight_support()) {
Joe Perches50f581a2011-03-29 15:21:48 -07002080 pr_info("brightness ignored, must be controlled by ACPI video driver\n");
Mattia Dongili7751ab82011-02-19 11:52:32 +09002081 } else {
2082 sony_nc_backlight_setup();
Alessandro Guido50f62af2007-01-13 23:04:34 +01002083 }
Stelian Pop7f09c432007-01-13 23:04:31 +01002084
malattia@linux.it56b87562007-04-09 10:19:05 +02002085 /* create sony_pf sysfs attributes related to the SNC device */
2086 for (item = sony_nc_values; item->name; ++item) {
2087
2088 if (!debug && item->debug)
2089 continue;
2090
2091 /* find the available acpiget as described in the DSDT */
2092 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
2093 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
2094 *item->acpiget,
2095 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02002096 dprintk("Found %s getter: %s\n",
2097 item->name, *item->acpiget);
malattia@linux.it56b87562007-04-09 10:19:05 +02002098 item->devattr.attr.mode |= S_IRUGO;
2099 break;
2100 }
2101 }
2102
2103 /* find the available acpiset as described in the DSDT */
2104 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
2105 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
2106 *item->acpiset,
2107 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02002108 dprintk("Found %s setter: %s\n",
2109 item->name, *item->acpiset);
malattia@linux.it56b87562007-04-09 10:19:05 +02002110 item->devattr.attr.mode |= S_IWUSR;
2111 break;
2112 }
2113 }
2114
2115 if (item->devattr.attr.mode != 0) {
2116 result =
2117 device_create_file(&sony_pf_device->dev,
2118 &item->devattr);
2119 if (result)
2120 goto out_sysfs;
2121 }
2122 }
2123
Stelian Pop7f09c432007-01-13 23:04:31 +01002124 return 0;
2125
malattia@linux.it56b87562007-04-09 10:19:05 +02002126 out_sysfs:
2127 for (item = sony_nc_values; item->name; ++item) {
2128 device_remove_file(&sony_pf_device->dev, &item->devattr);
2129 }
Mattia Dongili7751ab82011-02-19 11:52:32 +09002130 sony_nc_backlight_cleanup();
Mattia Dongili7df03b82007-01-13 23:04:41 +01002131
malattia@linux.it1549ee62007-04-09 10:19:08 +02002132 sony_laptop_remove_input();
2133
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002134 outsnc:
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002135 sony_nc_function_cleanup(sony_pf_device);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002136 sony_nc_handles_cleanup(sony_pf_device);
2137
2138 outpresent:
2139 sony_pf_remove();
2140
Len Browna02d1c12007-02-07 15:34:02 -05002141 outwalk:
Matthew Garrett6cc056b2009-03-26 21:58:15 +09002142 sony_nc_rfkill_cleanup();
Stelian Pop7f09c432007-01-13 23:04:31 +01002143 return result;
2144}
2145
malattia@linux.it59b19102007-04-09 10:19:04 +02002146static int sony_nc_remove(struct acpi_device *device, int type)
Stelian Pop7f09c432007-01-13 23:04:31 +01002147{
malattia@linux.it56b87562007-04-09 10:19:05 +02002148 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01002149
Mattia Dongili7751ab82011-02-19 11:52:32 +09002150 sony_nc_backlight_cleanup();
Alessandro Guido50f62af2007-01-13 23:04:34 +01002151
malattia@linux.it59b19102007-04-09 10:19:04 +02002152 sony_nc_acpi_device = NULL;
Stelian Popc5611622007-01-13 23:04:37 +01002153
malattia@linux.it56b87562007-04-09 10:19:05 +02002154 for (item = sony_nc_values; item->name; ++item) {
2155 device_remove_file(&sony_pf_device->dev, &item->devattr);
2156 }
2157
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002158 sony_nc_function_cleanup(sony_pf_device);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002159 sony_nc_handles_cleanup(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +02002160 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +02002161 sony_laptop_remove_input();
malattia@linux.itf6119b02007-04-09 19:31:06 +02002162 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01002163
2164 return 0;
2165}
2166
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002167static const struct acpi_device_id sony_device_ids[] = {
2168 {SONY_NC_HID, 0},
2169 {SONY_PIC_HID, 0},
2170 {"", 0},
2171};
2172MODULE_DEVICE_TABLE(acpi, sony_device_ids);
2173
2174static const struct acpi_device_id sony_nc_device_ids[] = {
2175 {SONY_NC_HID, 0},
2176 {"", 0},
2177};
2178
malattia@linux.it59b19102007-04-09 10:19:04 +02002179static struct acpi_driver sony_nc_driver = {
2180 .name = SONY_NC_DRIVER_NAME,
2181 .class = SONY_NC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002182 .ids = sony_nc_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02002183 .owner = THIS_MODULE,
Len Browna02d1c12007-02-07 15:34:02 -05002184 .ops = {
malattia@linux.it59b19102007-04-09 10:19:04 +02002185 .add = sony_nc_add,
2186 .remove = sony_nc_remove,
2187 .resume = sony_nc_resume,
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00002188 .notify = sony_nc_notify,
Len Browna02d1c12007-02-07 15:34:02 -05002189 },
Andrew Morton3f4f4612007-01-13 23:04:32 +01002190};
2191
malattia@linux.it33a04452007-04-09 19:26:03 +02002192/*********** SPIC (SNY6001) Device ***********/
2193
2194#define SONYPI_DEVICE_TYPE1 0x00000001
2195#define SONYPI_DEVICE_TYPE2 0x00000002
2196#define SONYPI_DEVICE_TYPE3 0x00000004
2197
Mattia Dongili22a17782007-07-16 02:34:39 +09002198#define SONYPI_TYPE1_OFFSET 0x04
2199#define SONYPI_TYPE2_OFFSET 0x12
2200#define SONYPI_TYPE3_OFFSET 0x12
malattia@linux.it33a04452007-04-09 19:26:03 +02002201
malattia@linux.it33a04452007-04-09 19:26:03 +02002202struct sony_pic_ioport {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002203 struct acpi_resource_io io1;
2204 struct acpi_resource_io io2;
malattia@linux.it33a04452007-04-09 19:26:03 +02002205 struct list_head list;
2206};
2207
2208struct sony_pic_irq {
2209 struct acpi_resource_irq irq;
2210 struct list_head list;
2211};
2212
Mattia Dongilide920432008-01-14 18:05:43 +09002213struct sonypi_eventtypes {
2214 u8 data;
2215 unsigned long mask;
2216 struct sonypi_event *events;
2217};
2218
malattia@linux.it33a04452007-04-09 19:26:03 +02002219struct sony_pic_dev {
Mattia Dongili31df7142009-09-16 00:05:29 +09002220 struct acpi_device *acpi_dev;
2221 struct sony_pic_irq *cur_irq;
2222 struct sony_pic_ioport *cur_ioport;
2223 struct list_head interrupts;
2224 struct list_head ioports;
2225 struct mutex lock;
2226 struct sonypi_eventtypes *event_types;
2227 int (*handle_irq)(const u8, const u8);
2228 int model;
2229 u16 evport_offset;
2230 u8 camera_power;
2231 u8 bluetooth_power;
2232 u8 wwan_power;
malattia@linux.it33a04452007-04-09 19:26:03 +02002233};
2234
2235static struct sony_pic_dev spic_dev = {
2236 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
2237 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
2238};
2239
Alan Jenkins5e6f9722009-09-16 00:05:32 +09002240static int spic_drv_registered;
2241
malattia@linux.it33a04452007-04-09 19:26:03 +02002242/* Event masks */
2243#define SONYPI_JOGGER_MASK 0x00000001
2244#define SONYPI_CAPTURE_MASK 0x00000002
2245#define SONYPI_FNKEY_MASK 0x00000004
2246#define SONYPI_BLUETOOTH_MASK 0x00000008
2247#define SONYPI_PKEY_MASK 0x00000010
2248#define SONYPI_BACK_MASK 0x00000020
2249#define SONYPI_HELP_MASK 0x00000040
2250#define SONYPI_LID_MASK 0x00000080
2251#define SONYPI_ZOOM_MASK 0x00000100
2252#define SONYPI_THUMBPHRASE_MASK 0x00000200
2253#define SONYPI_MEYE_MASK 0x00000400
2254#define SONYPI_MEMORYSTICK_MASK 0x00000800
2255#define SONYPI_BATTERY_MASK 0x00001000
2256#define SONYPI_WIRELESS_MASK 0x00002000
2257
2258struct sonypi_event {
2259 u8 data;
2260 u8 event;
2261};
2262
2263/* The set of possible button release events */
2264static struct sonypi_event sonypi_releaseev[] = {
2265 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
2266 { 0, 0 }
2267};
2268
2269/* The set of possible jogger events */
2270static struct sonypi_event sonypi_joggerev[] = {
2271 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
2272 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
2273 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
2274 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
2275 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
2276 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
2277 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
2278 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
2279 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
2280 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
2281 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
2282 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
2283 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
2284 { 0, 0 }
2285};
2286
2287/* The set of possible capture button events */
2288static struct sonypi_event sonypi_captureev[] = {
2289 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
2290 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002291 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02002292 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
2293 { 0, 0 }
2294};
2295
2296/* The set of possible fnkeys events */
2297static struct sonypi_event sonypi_fnkeyev[] = {
2298 { 0x10, SONYPI_EVENT_FNKEY_ESC },
2299 { 0x11, SONYPI_EVENT_FNKEY_F1 },
2300 { 0x12, SONYPI_EVENT_FNKEY_F2 },
2301 { 0x13, SONYPI_EVENT_FNKEY_F3 },
2302 { 0x14, SONYPI_EVENT_FNKEY_F4 },
2303 { 0x15, SONYPI_EVENT_FNKEY_F5 },
2304 { 0x16, SONYPI_EVENT_FNKEY_F6 },
2305 { 0x17, SONYPI_EVENT_FNKEY_F7 },
2306 { 0x18, SONYPI_EVENT_FNKEY_F8 },
2307 { 0x19, SONYPI_EVENT_FNKEY_F9 },
2308 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
2309 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
2310 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
2311 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
2312 { 0x21, SONYPI_EVENT_FNKEY_1 },
2313 { 0x22, SONYPI_EVENT_FNKEY_2 },
2314 { 0x31, SONYPI_EVENT_FNKEY_D },
2315 { 0x32, SONYPI_EVENT_FNKEY_E },
2316 { 0x33, SONYPI_EVENT_FNKEY_F },
2317 { 0x34, SONYPI_EVENT_FNKEY_S },
2318 { 0x35, SONYPI_EVENT_FNKEY_B },
2319 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
2320 { 0, 0 }
2321};
2322
2323/* The set of possible program key events */
2324static struct sonypi_event sonypi_pkeyev[] = {
2325 { 0x01, SONYPI_EVENT_PKEY_P1 },
2326 { 0x02, SONYPI_EVENT_PKEY_P2 },
2327 { 0x04, SONYPI_EVENT_PKEY_P3 },
Harald Jenny1cae7102009-03-26 21:58:18 +09002328 { 0x20, SONYPI_EVENT_PKEY_P1 },
malattia@linux.it33a04452007-04-09 19:26:03 +02002329 { 0, 0 }
2330};
2331
2332/* The set of possible bluetooth events */
2333static struct sonypi_event sonypi_blueev[] = {
2334 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
2335 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
2336 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
2337 { 0, 0 }
2338};
2339
2340/* The set of possible wireless events */
2341static struct sonypi_event sonypi_wlessev[] = {
Mattia Dongili4eeb5022011-02-19 11:52:27 +09002342 { 0x59, SONYPI_EVENT_IGNORE },
2343 { 0x5a, SONYPI_EVENT_IGNORE },
malattia@linux.it33a04452007-04-09 19:26:03 +02002344 { 0, 0 }
2345};
2346
2347/* The set of possible back button events */
2348static struct sonypi_event sonypi_backev[] = {
2349 { 0x20, SONYPI_EVENT_BACK_PRESSED },
2350 { 0, 0 }
2351};
2352
2353/* The set of possible help button events */
2354static struct sonypi_event sonypi_helpev[] = {
2355 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
2356 { 0, 0 }
2357};
2358
2359
2360/* The set of possible lid events */
2361static struct sonypi_event sonypi_lidev[] = {
2362 { 0x51, SONYPI_EVENT_LID_CLOSED },
2363 { 0x50, SONYPI_EVENT_LID_OPENED },
2364 { 0, 0 }
2365};
2366
2367/* The set of possible zoom events */
2368static struct sonypi_event sonypi_zoomev[] = {
2369 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002370 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
2371 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
Harald Jenny1cae7102009-03-26 21:58:18 +09002372 { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02002373 { 0, 0 }
2374};
2375
2376/* The set of possible thumbphrase events */
2377static struct sonypi_event sonypi_thumbphraseev[] = {
2378 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
2379 { 0, 0 }
2380};
2381
2382/* The set of possible motioneye camera events */
2383static struct sonypi_event sonypi_meyeev[] = {
2384 { 0x00, SONYPI_EVENT_MEYE_FACE },
2385 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
2386 { 0, 0 }
2387};
2388
2389/* The set of possible memorystick events */
2390static struct sonypi_event sonypi_memorystickev[] = {
2391 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
2392 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
2393 { 0, 0 }
2394};
2395
2396/* The set of possible battery events */
2397static struct sonypi_event sonypi_batteryev[] = {
2398 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
2399 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
2400 { 0, 0 }
2401};
2402
Harald Jenny1cae7102009-03-26 21:58:18 +09002403/* The set of possible volume events */
2404static struct sonypi_event sonypi_volumeev[] = {
2405 { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
2406 { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
2407 { 0, 0 }
2408};
2409
2410/* The set of possible brightness events */
2411static struct sonypi_event sonypi_brightnessev[] = {
2412 { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
2413 { 0, 0 }
2414};
2415
Mattia Dongilide920432008-01-14 18:05:43 +09002416static struct sonypi_eventtypes type1_events[] = {
2417 { 0, 0xffffffff, sonypi_releaseev },
2418 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
2419 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
2420 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
2421 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
2422 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2423 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
2424 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
2425 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2426 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
2427 { 0 },
2428};
2429static struct sonypi_eventtypes type2_events[] = {
2430 { 0, 0xffffffff, sonypi_releaseev },
2431 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
2432 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
2433 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
2434 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2435 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
2436 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
2437 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
2438 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
2439 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
2440 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
2441 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2442 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
2443 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
2444 { 0 },
2445};
2446static struct sonypi_eventtypes type3_events[] = {
2447 { 0, 0xffffffff, sonypi_releaseev },
2448 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2449 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
2450 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2451 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
2452 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002453 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
2454 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
2455 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
Harald Jenny1cae7102009-03-26 21:58:18 +09002456 { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
2457 { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002458 { 0 },
Mattia Dongilide920432008-01-14 18:05:43 +09002459};
2460
Mattia Dongili3eb87492008-01-14 18:05:45 +09002461/* low level spic calls */
malattia@linux.it33a04452007-04-09 19:26:03 +02002462#define ITERATIONS_LONG 10000
2463#define ITERATIONS_SHORT 10
2464#define wait_on_command(command, iterations) { \
2465 unsigned int n = iterations; \
2466 while (--n && (command)) \
2467 udelay(1); \
2468 if (!n) \
2469 dprintk("command failed at %s : %s (line %d)\n", \
Harvey Harrison6e574192008-04-29 00:59:20 -07002470 __FILE__, __func__, __LINE__); \
malattia@linux.it33a04452007-04-09 19:26:03 +02002471}
2472
2473static u8 sony_pic_call1(u8 dev)
2474{
2475 u8 v1, v2;
2476
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002477 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002478 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002479 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2480 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
2481 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002482 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02002483 return v2;
2484}
2485
2486static u8 sony_pic_call2(u8 dev, u8 fn)
2487{
2488 u8 v1;
2489
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002490 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002491 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002492 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2493 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002494 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002495 outb(fn, spic_dev.cur_ioport->io1.minimum);
2496 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002497 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02002498 return v1;
2499}
2500
malattia@linux.it49a11de2007-04-09 19:28:56 +02002501static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
2502{
2503 u8 v1;
2504
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002505 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2506 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2507 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2508 outb(fn, spic_dev.cur_ioport->io1.minimum);
2509 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2510 outb(v, spic_dev.cur_ioport->io1.minimum);
2511 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002512 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
2513 dev, fn, v, v1);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002514 return v1;
2515}
2516
Mattia Dongili3eb87492008-01-14 18:05:45 +09002517/*
2518 * minidrivers for SPIC models
2519 */
Mattia Dongilie93c8a62009-03-26 21:58:17 +09002520static int type3_handle_irq(const u8 data_mask, const u8 ev)
Mattia Dongili3eb87492008-01-14 18:05:45 +09002521{
2522 /*
2523 * 0x31 could mean we have to take some extra action and wait for
Mattia Dongilie93c8a62009-03-26 21:58:17 +09002524 * the next irq for some Type3 models, it will generate a new
Mattia Dongili3eb87492008-01-14 18:05:45 +09002525 * irq and we can read new data from the device:
2526 * - 0x5c and 0x5f requires 0xA0
2527 * - 0x61 requires 0xB3
2528 */
2529 if (data_mask == 0x31) {
2530 if (ev == 0x5c || ev == 0x5f)
2531 sony_pic_call1(0xA0);
2532 else if (ev == 0x61)
2533 sony_pic_call1(0xB3);
2534 return 0;
2535 }
2536 return 1;
2537}
2538
Mattia Dongili3eb87492008-01-14 18:05:45 +09002539static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
2540{
2541 struct pci_dev *pcidev;
2542
2543 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2544 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
2545 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002546 dev->model = SONYPI_DEVICE_TYPE1;
2547 dev->evport_offset = SONYPI_TYPE1_OFFSET;
2548 dev->event_types = type1_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002549 goto out;
2550 }
2551
2552 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2553 PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
2554 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002555 dev->model = SONYPI_DEVICE_TYPE2;
2556 dev->evport_offset = SONYPI_TYPE2_OFFSET;
2557 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002558 goto out;
2559 }
2560
2561 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2562 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
2563 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002564 dev->model = SONYPI_DEVICE_TYPE3;
2565 dev->handle_irq = type3_handle_irq;
2566 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2567 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002568 goto out;
2569 }
2570
2571 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2572 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
2573 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002574 dev->model = SONYPI_DEVICE_TYPE3;
2575 dev->handle_irq = type3_handle_irq;
2576 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2577 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002578 goto out;
2579 }
2580
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09002581 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2582 PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
2583 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002584 dev->model = SONYPI_DEVICE_TYPE3;
2585 dev->handle_irq = type3_handle_irq;
2586 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2587 dev->event_types = type3_events;
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09002588 goto out;
2589 }
2590
Mattia Dongili3eb87492008-01-14 18:05:45 +09002591 /* default */
Mattia Dongili31df7142009-09-16 00:05:29 +09002592 dev->model = SONYPI_DEVICE_TYPE2;
2593 dev->evport_offset = SONYPI_TYPE2_OFFSET;
2594 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002595
2596out:
2597 if (pcidev)
2598 pci_dev_put(pcidev);
2599
Joe Perches50f581a2011-03-29 15:21:48 -07002600 pr_info("detected Type%d model\n",
2601 dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
2602 dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
Mattia Dongili3eb87492008-01-14 18:05:45 +09002603}
2604
malattia@linux.it49a11de2007-04-09 19:28:56 +02002605/* camera tests and poweron/poweroff */
2606#define SONYPI_CAMERA_PICTURE 5
malattia@linux.it49a11de2007-04-09 19:28:56 +02002607#define SONYPI_CAMERA_CONTROL 0x10
malattia@linux.ite3646322007-04-28 23:34:22 +09002608
2609#define SONYPI_CAMERA_BRIGHTNESS 0
2610#define SONYPI_CAMERA_CONTRAST 1
2611#define SONYPI_CAMERA_HUE 2
2612#define SONYPI_CAMERA_COLOR 3
2613#define SONYPI_CAMERA_SHARPNESS 4
2614
2615#define SONYPI_CAMERA_EXPOSURE_MASK 0xC
2616#define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
2617#define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
2618#define SONYPI_CAMERA_MUTE_MASK 0x40
2619
2620/* the rest don't need a loop until not 0xff */
2621#define SONYPI_CAMERA_AGC 6
2622#define SONYPI_CAMERA_AGC_MASK 0x30
2623#define SONYPI_CAMERA_SHUTTER_MASK 0x7
2624
2625#define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
2626#define SONYPI_CAMERA_CONTROL 0x10
2627
2628#define SONYPI_CAMERA_STATUS 7
2629#define SONYPI_CAMERA_STATUS_READY 0x2
2630#define SONYPI_CAMERA_STATUS_POSITION 0x4
2631
2632#define SONYPI_DIRECTION_BACKWARDS 0x4
2633
2634#define SONYPI_CAMERA_REVISION 8
2635#define SONYPI_CAMERA_ROMVERSION 9
malattia@linux.it49a11de2007-04-09 19:28:56 +02002636
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002637static int __sony_pic_camera_ready(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002638{
2639 u8 v;
2640
2641 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
2642 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
2643}
2644
malattia@linux.ite3646322007-04-28 23:34:22 +09002645static int __sony_pic_camera_off(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002646{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002647 if (!camera) {
Joe Perches50f581a2011-03-29 15:21:48 -07002648 pr_warn("camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002649 return -ENODEV;
2650 }
2651
malattia@linux.it49a11de2007-04-09 19:28:56 +02002652 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
2653 SONYPI_CAMERA_MUTE_MASK),
2654 ITERATIONS_SHORT);
2655
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002656 if (spic_dev.camera_power) {
2657 sony_pic_call2(0x91, 0);
2658 spic_dev.camera_power = 0;
2659 }
2660 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002661}
2662
malattia@linux.ite3646322007-04-28 23:34:22 +09002663static int __sony_pic_camera_on(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002664{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002665 int i, j, x;
2666
2667 if (!camera) {
Joe Perches50f581a2011-03-29 15:21:48 -07002668 pr_warn("camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002669 return -ENODEV;
2670 }
malattia@linux.it49a11de2007-04-09 19:28:56 +02002671
2672 if (spic_dev.camera_power)
malattia@linux.ite3646322007-04-28 23:34:22 +09002673 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002674
2675 for (j = 5; j > 0; j--) {
2676
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002677 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002678 msleep(10);
2679 sony_pic_call1(0x93);
2680
2681 for (i = 400; i > 0; i--) {
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002682 if (__sony_pic_camera_ready())
malattia@linux.it49a11de2007-04-09 19:28:56 +02002683 break;
2684 msleep(10);
2685 }
2686 if (i)
2687 break;
2688 }
2689
2690 if (j == 0) {
Joe Perches50f581a2011-03-29 15:21:48 -07002691 pr_warn("failed to power on camera\n");
malattia@linux.ite3646322007-04-28 23:34:22 +09002692 return -ENODEV;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002693 }
2694
2695 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
2696 0x5a),
2697 ITERATIONS_SHORT);
2698
2699 spic_dev.camera_power = 1;
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002700 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002701}
2702
malattia@linux.ite3646322007-04-28 23:34:22 +09002703/* External camera command (exported to the motion eye v4l driver) */
2704int sony_pic_camera_command(int command, u8 value)
2705{
2706 if (!camera)
2707 return -EIO;
2708
2709 mutex_lock(&spic_dev.lock);
2710
2711 switch (command) {
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002712 case SONY_PIC_COMMAND_SETCAMERA:
malattia@linux.ite3646322007-04-28 23:34:22 +09002713 if (value)
2714 __sony_pic_camera_on();
2715 else
2716 __sony_pic_camera_off();
2717 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002718 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09002719 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
2720 ITERATIONS_SHORT);
2721 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002722 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
malattia@linux.ite3646322007-04-28 23:34:22 +09002723 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
2724 ITERATIONS_SHORT);
2725 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002726 case SONY_PIC_COMMAND_SETCAMERAHUE:
malattia@linux.ite3646322007-04-28 23:34:22 +09002727 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
2728 ITERATIONS_SHORT);
2729 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002730 case SONY_PIC_COMMAND_SETCAMERACOLOR:
malattia@linux.ite3646322007-04-28 23:34:22 +09002731 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
2732 ITERATIONS_SHORT);
2733 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002734 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09002735 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
2736 ITERATIONS_SHORT);
2737 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002738 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
malattia@linux.ite3646322007-04-28 23:34:22 +09002739 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
2740 ITERATIONS_SHORT);
2741 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002742 case SONY_PIC_COMMAND_SETCAMERAAGC:
malattia@linux.ite3646322007-04-28 23:34:22 +09002743 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
2744 ITERATIONS_SHORT);
2745 break;
2746 default:
Joe Perches50f581a2011-03-29 15:21:48 -07002747 pr_err("sony_pic_camera_command invalid: %d\n", command);
malattia@linux.ite3646322007-04-28 23:34:22 +09002748 break;
2749 }
2750 mutex_unlock(&spic_dev.lock);
2751 return 0;
2752}
2753EXPORT_SYMBOL(sony_pic_camera_command);
2754
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002755/* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002756static void __sony_pic_set_wwanpower(u8 state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002757{
2758 state = !!state;
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002759 if (spic_dev.wwan_power == state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002760 return;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002761 sony_pic_call2(0xB0, state);
Sergey Yanovich3ad1b762009-03-26 21:58:21 +09002762 sony_pic_call1(0x82);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002763 spic_dev.wwan_power = state;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002764}
2765
2766static ssize_t sony_pic_wwanpower_store(struct device *dev,
2767 struct device_attribute *attr,
2768 const char *buffer, size_t count)
2769{
2770 unsigned long value;
2771 if (count > 31)
2772 return -EINVAL;
2773
Mattia Dongili9e123372012-05-19 22:35:47 +09002774 if (kstrtoul(buffer, 10, &value))
2775 return -EINVAL;
2776
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002777 mutex_lock(&spic_dev.lock);
2778 __sony_pic_set_wwanpower(value);
2779 mutex_unlock(&spic_dev.lock);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002780
2781 return count;
2782}
2783
2784static ssize_t sony_pic_wwanpower_show(struct device *dev,
2785 struct device_attribute *attr, char *buffer)
2786{
2787 ssize_t count;
2788 mutex_lock(&spic_dev.lock);
2789 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
2790 mutex_unlock(&spic_dev.lock);
2791 return count;
2792}
2793
malattia@linux.it49a11de2007-04-09 19:28:56 +02002794/* bluetooth subsystem power state */
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002795static void __sony_pic_set_bluetoothpower(u8 state)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002796{
2797 state = !!state;
2798 if (spic_dev.bluetooth_power == state)
2799 return;
2800 sony_pic_call2(0x96, state);
2801 sony_pic_call1(0x82);
2802 spic_dev.bluetooth_power = state;
2803}
2804
2805static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
2806 struct device_attribute *attr,
2807 const char *buffer, size_t count)
2808{
2809 unsigned long value;
2810 if (count > 31)
2811 return -EINVAL;
2812
Mattia Dongili9e123372012-05-19 22:35:47 +09002813 if (kstrtoul(buffer, 10, &value))
2814 return -EINVAL;
2815
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002816 mutex_lock(&spic_dev.lock);
2817 __sony_pic_set_bluetoothpower(value);
2818 mutex_unlock(&spic_dev.lock);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002819
2820 return count;
2821}
2822
2823static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
2824 struct device_attribute *attr, char *buffer)
2825{
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002826 ssize_t count = 0;
2827 mutex_lock(&spic_dev.lock);
2828 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
2829 mutex_unlock(&spic_dev.lock);
2830 return count;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002831}
2832
2833/* fan speed */
2834/* FAN0 information (reverse engineered from ACPI tables) */
2835#define SONY_PIC_FAN0_STATUS 0x93
malattia@linux.it7b153f32007-04-09 19:31:25 +02002836static int sony_pic_set_fanspeed(unsigned long value)
2837{
2838 return ec_write(SONY_PIC_FAN0_STATUS, value);
2839}
2840
2841static int sony_pic_get_fanspeed(u8 *value)
2842{
2843 return ec_read(SONY_PIC_FAN0_STATUS, value);
2844}
2845
malattia@linux.it49a11de2007-04-09 19:28:56 +02002846static ssize_t sony_pic_fanspeed_store(struct device *dev,
2847 struct device_attribute *attr,
2848 const char *buffer, size_t count)
2849{
2850 unsigned long value;
2851 if (count > 31)
2852 return -EINVAL;
2853
Mattia Dongili9e123372012-05-19 22:35:47 +09002854 if (kstrtoul(buffer, 10, &value))
2855 return -EINVAL;
2856
malattia@linux.it7b153f32007-04-09 19:31:25 +02002857 if (sony_pic_set_fanspeed(value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02002858 return -EIO;
2859
2860 return count;
2861}
2862
2863static ssize_t sony_pic_fanspeed_show(struct device *dev,
2864 struct device_attribute *attr, char *buffer)
2865{
2866 u8 value = 0;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002867 if (sony_pic_get_fanspeed(&value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02002868 return -EIO;
2869
2870 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
2871}
2872
2873#define SPIC_ATTR(_name, _mode) \
2874struct device_attribute spic_attr_##_name = __ATTR(_name, \
2875 _mode, sony_pic_## _name ##_show, \
2876 sony_pic_## _name ##_store)
2877
malattia@linux.it49a11de2007-04-09 19:28:56 +02002878static SPIC_ATTR(bluetoothpower, 0644);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002879static SPIC_ATTR(wwanpower, 0644);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002880static SPIC_ATTR(fanspeed, 0644);
2881
2882static struct attribute *spic_attributes[] = {
malattia@linux.it49a11de2007-04-09 19:28:56 +02002883 &spic_attr_bluetoothpower.attr,
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002884 &spic_attr_wwanpower.attr,
malattia@linux.it49a11de2007-04-09 19:28:56 +02002885 &spic_attr_fanspeed.attr,
2886 NULL
2887};
2888
2889static struct attribute_group spic_attribute_group = {
2890 .attrs = spic_attributes
2891};
2892
malattia@linux.it7b153f32007-04-09 19:31:25 +02002893/******** SONYPI compatibility **********/
Mattia Dongilia64e62a2007-05-01 11:19:53 +09002894#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +02002895
2896/* battery / brightness / temperature addresses */
2897#define SONYPI_BAT_FLAGS 0x81
2898#define SONYPI_LCD_LIGHT 0x96
2899#define SONYPI_BAT1_PCTRM 0xa0
2900#define SONYPI_BAT1_LEFT 0xa2
2901#define SONYPI_BAT1_MAXRT 0xa4
2902#define SONYPI_BAT2_PCTRM 0xa8
2903#define SONYPI_BAT2_LEFT 0xaa
2904#define SONYPI_BAT2_MAXRT 0xac
2905#define SONYPI_BAT1_MAXTK 0xb0
2906#define SONYPI_BAT1_FULL 0xb2
2907#define SONYPI_BAT2_MAXTK 0xb8
2908#define SONYPI_BAT2_FULL 0xba
2909#define SONYPI_TEMP_STATUS 0xC1
2910
2911struct sonypi_compat_s {
2912 struct fasync_struct *fifo_async;
Stefani Seibold45465482009-12-21 14:37:26 -08002913 struct kfifo fifo;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002914 spinlock_t fifo_lock;
2915 wait_queue_head_t fifo_proc_list;
2916 atomic_t open_count;
2917};
2918static struct sonypi_compat_s sonypi_compat = {
2919 .open_count = ATOMIC_INIT(0),
2920};
2921
2922static int sonypi_misc_fasync(int fd, struct file *filp, int on)
2923{
Jonathan Corbet60aa4922009-02-01 14:52:56 -07002924 return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002925}
2926
2927static int sonypi_misc_release(struct inode *inode, struct file *file)
2928{
malattia@linux.it7b153f32007-04-09 19:31:25 +02002929 atomic_dec(&sonypi_compat.open_count);
2930 return 0;
2931}
2932
2933static int sonypi_misc_open(struct inode *inode, struct file *file)
2934{
2935 /* Flush input queue on first open */
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002936 unsigned long flags;
2937
Stefani Seibold45465482009-12-21 14:37:26 -08002938 spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002939
malattia@linux.it7b153f32007-04-09 19:31:25 +02002940 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
Stefani Seibolde64c0262009-12-21 14:37:28 -08002941 kfifo_reset(&sonypi_compat.fifo);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002942
Stefani Seibold45465482009-12-21 14:37:26 -08002943 spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002944
malattia@linux.it7b153f32007-04-09 19:31:25 +02002945 return 0;
2946}
2947
2948static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
2949 size_t count, loff_t *pos)
2950{
2951 ssize_t ret;
2952 unsigned char c;
2953
Stefani Seibold45465482009-12-21 14:37:26 -08002954 if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
malattia@linux.it7b153f32007-04-09 19:31:25 +02002955 (file->f_flags & O_NONBLOCK))
2956 return -EAGAIN;
2957
2958 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
Stefani Seibold45465482009-12-21 14:37:26 -08002959 kfifo_len(&sonypi_compat.fifo) != 0);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002960 if (ret)
2961 return ret;
2962
2963 while (ret < count &&
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002964 (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002965 &sonypi_compat.fifo_lock) == sizeof(c))) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02002966 if (put_user(c, buf++))
2967 return -EFAULT;
2968 ret++;
2969 }
2970
2971 if (ret > 0) {
2972 struct inode *inode = file->f_path.dentry->d_inode;
2973 inode->i_atime = current_fs_time(inode->i_sb);
2974 }
2975
2976 return ret;
2977}
2978
2979static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
2980{
2981 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
Stefani Seibold45465482009-12-21 14:37:26 -08002982 if (kfifo_len(&sonypi_compat.fifo))
malattia@linux.it7b153f32007-04-09 19:31:25 +02002983 return POLLIN | POLLRDNORM;
2984 return 0;
2985}
2986
2987static int ec_read16(u8 addr, u16 *value)
2988{
2989 u8 val_lb, val_hb;
2990 if (ec_read(addr, &val_lb))
2991 return -1;
2992 if (ec_read(addr + 1, &val_hb))
2993 return -1;
2994 *value = val_lb | (val_hb << 8);
2995 return 0;
2996}
2997
Alan Cox2b24ef02009-03-26 21:58:19 +09002998static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
2999 unsigned long arg)
malattia@linux.it7b153f32007-04-09 19:31:25 +02003000{
3001 int ret = 0;
3002 void __user *argp = (void __user *)arg;
3003 u8 val8;
3004 u16 val16;
3005 int value;
3006
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003007 mutex_lock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003008 switch (cmd) {
3009 case SONYPI_IOCGBRT:
Mattia Dongili62d2f232011-05-09 10:20:29 -04003010 if (sony_bl_props.dev == NULL) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003011 ret = -EIO;
3012 break;
3013 }
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09003014 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL,
3015 &value)) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003016 ret = -EIO;
3017 break;
3018 }
3019 val8 = ((value & 0xff) - 1) << 5;
3020 if (copy_to_user(argp, &val8, sizeof(val8)))
3021 ret = -EFAULT;
3022 break;
3023 case SONYPI_IOCSBRT:
Mattia Dongili62d2f232011-05-09 10:20:29 -04003024 if (sony_bl_props.dev == NULL) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003025 ret = -EIO;
3026 break;
3027 }
3028 if (copy_from_user(&val8, argp, sizeof(val8))) {
3029 ret = -EFAULT;
3030 break;
3031 }
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09003032 value = (val8 >> 5) + 1;
3033 if (sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &value,
3034 NULL)) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003035 ret = -EIO;
3036 break;
3037 }
3038 /* sync the backlight device status */
Mattia Dongili62d2f232011-05-09 10:20:29 -04003039 sony_bl_props.dev->props.brightness =
3040 sony_backlight_get_brightness(sony_bl_props.dev);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003041 break;
3042 case SONYPI_IOCGBAT1CAP:
3043 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
3044 ret = -EIO;
3045 break;
3046 }
3047 if (copy_to_user(argp, &val16, sizeof(val16)))
3048 ret = -EFAULT;
3049 break;
3050 case SONYPI_IOCGBAT1REM:
3051 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
3052 ret = -EIO;
3053 break;
3054 }
3055 if (copy_to_user(argp, &val16, sizeof(val16)))
3056 ret = -EFAULT;
3057 break;
3058 case SONYPI_IOCGBAT2CAP:
3059 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
3060 ret = -EIO;
3061 break;
3062 }
3063 if (copy_to_user(argp, &val16, sizeof(val16)))
3064 ret = -EFAULT;
3065 break;
3066 case SONYPI_IOCGBAT2REM:
3067 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
3068 ret = -EIO;
3069 break;
3070 }
3071 if (copy_to_user(argp, &val16, sizeof(val16)))
3072 ret = -EFAULT;
3073 break;
3074 case SONYPI_IOCGBATFLAGS:
3075 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
3076 ret = -EIO;
3077 break;
3078 }
3079 val8 &= 0x07;
3080 if (copy_to_user(argp, &val8, sizeof(val8)))
3081 ret = -EFAULT;
3082 break;
3083 case SONYPI_IOCGBLUE:
3084 val8 = spic_dev.bluetooth_power;
3085 if (copy_to_user(argp, &val8, sizeof(val8)))
3086 ret = -EFAULT;
3087 break;
3088 case SONYPI_IOCSBLUE:
3089 if (copy_from_user(&val8, argp, sizeof(val8))) {
3090 ret = -EFAULT;
3091 break;
3092 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003093 __sony_pic_set_bluetoothpower(val8);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003094 break;
3095 /* FAN Controls */
3096 case SONYPI_IOCGFAN:
3097 if (sony_pic_get_fanspeed(&val8)) {
3098 ret = -EIO;
3099 break;
3100 }
3101 if (copy_to_user(argp, &val8, sizeof(val8)))
3102 ret = -EFAULT;
3103 break;
3104 case SONYPI_IOCSFAN:
3105 if (copy_from_user(&val8, argp, sizeof(val8))) {
3106 ret = -EFAULT;
3107 break;
3108 }
3109 if (sony_pic_set_fanspeed(val8))
3110 ret = -EIO;
3111 break;
3112 /* GET Temperature (useful under APM) */
3113 case SONYPI_IOCGTEMP:
3114 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
3115 ret = -EIO;
3116 break;
3117 }
3118 if (copy_to_user(argp, &val8, sizeof(val8)))
3119 ret = -EFAULT;
3120 break;
3121 default:
3122 ret = -EINVAL;
3123 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003124 mutex_unlock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003125 return ret;
3126}
3127
3128static const struct file_operations sonypi_misc_fops = {
3129 .owner = THIS_MODULE,
3130 .read = sonypi_misc_read,
3131 .poll = sonypi_misc_poll,
3132 .open = sonypi_misc_open,
3133 .release = sonypi_misc_release,
3134 .fasync = sonypi_misc_fasync,
Alan Cox2b24ef02009-03-26 21:58:19 +09003135 .unlocked_ioctl = sonypi_misc_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003136 .llseek = noop_llseek,
malattia@linux.it7b153f32007-04-09 19:31:25 +02003137};
3138
3139static struct miscdevice sonypi_misc_device = {
3140 .minor = MISC_DYNAMIC_MINOR,
3141 .name = "sonypi",
3142 .fops = &sonypi_misc_fops,
3143};
3144
3145static void sonypi_compat_report_event(u8 event)
3146{
Stefani Seibold7acd72e2009-12-21 14:37:28 -08003147 kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08003148 sizeof(event), &sonypi_compat.fifo_lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003149 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
3150 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
3151}
3152
3153static int sonypi_compat_init(void)
3154{
3155 int error;
3156
3157 spin_lock_init(&sonypi_compat.fifo_lock);
Stefani Seibold45465482009-12-21 14:37:26 -08003158 error =
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08003159 kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -08003160 if (error) {
Joe Perches50f581a2011-03-29 15:21:48 -07003161 pr_err("kfifo_alloc failed\n");
Stefani Seibold45465482009-12-21 14:37:26 -08003162 return error;
malattia@linux.it7b153f32007-04-09 19:31:25 +02003163 }
3164
3165 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003166
3167 if (minor != -1)
3168 sonypi_misc_device.minor = minor;
3169 error = misc_register(&sonypi_misc_device);
3170 if (error) {
Joe Perches50f581a2011-03-29 15:21:48 -07003171 pr_err("misc_register failed\n");
malattia@linux.it7b153f32007-04-09 19:31:25 +02003172 goto err_free_kfifo;
3173 }
3174 if (minor == -1)
Joe Perches50f581a2011-03-29 15:21:48 -07003175 pr_info("device allocated minor is %d\n",
3176 sonypi_misc_device.minor);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003177
3178 return 0;
3179
3180err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -08003181 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003182 return error;
3183}
3184
3185static void sonypi_compat_exit(void)
3186{
3187 misc_deregister(&sonypi_misc_device);
Stefani Seibold45465482009-12-21 14:37:26 -08003188 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003189}
3190#else
3191static int sonypi_compat_init(void) { return 0; }
3192static void sonypi_compat_exit(void) { }
3193static void sonypi_compat_report_event(u8 event) { }
Mattia Dongilia64e62a2007-05-01 11:19:53 +09003194#endif /* CONFIG_SONYPI_COMPAT */
malattia@linux.it7b153f32007-04-09 19:31:25 +02003195
malattia@linux.it1549ee62007-04-09 10:19:08 +02003196/*
malattia@linux.it33a04452007-04-09 19:26:03 +02003197 * ACPI callbacks
malattia@linux.it1549ee62007-04-09 10:19:08 +02003198 */
malattia@linux.it33a04452007-04-09 19:26:03 +02003199static acpi_status
3200sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
3201{
3202 u32 i;
3203 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
3204
3205 switch (resource->type) {
3206 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003207 {
3208 /* start IO enumeration */
3209 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
3210 if (!ioport)
3211 return AE_ERROR;
3212
3213 list_add(&ioport->list, &dev->ioports);
3214 return AE_OK;
3215 }
3216
malattia@linux.it33a04452007-04-09 19:26:03 +02003217 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003218 /* end IO enumeration */
malattia@linux.it33a04452007-04-09 19:26:03 +02003219 return AE_OK;
3220
3221 case ACPI_RESOURCE_TYPE_IRQ:
3222 {
3223 struct acpi_resource_irq *p = &resource->data.irq;
3224 struct sony_pic_irq *interrupt = NULL;
3225 if (!p || !p->interrupt_count) {
3226 /*
3227 * IRQ descriptors may have no IRQ# bits set,
3228 * particularly those those w/ _STA disabled
3229 */
3230 dprintk("Blank IRQ resource\n");
3231 return AE_OK;
3232 }
3233 for (i = 0; i < p->interrupt_count; i++) {
3234 if (!p->interrupts[i]) {
Joe Perches50f581a2011-03-29 15:21:48 -07003235 pr_warn("Invalid IRQ %d\n",
3236 p->interrupts[i]);
malattia@linux.it33a04452007-04-09 19:26:03 +02003237 continue;
3238 }
3239 interrupt = kzalloc(sizeof(*interrupt),
3240 GFP_KERNEL);
3241 if (!interrupt)
3242 return AE_ERROR;
3243
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003244 list_add(&interrupt->list, &dev->interrupts);
malattia@linux.it33a04452007-04-09 19:26:03 +02003245 interrupt->irq.triggering = p->triggering;
3246 interrupt->irq.polarity = p->polarity;
3247 interrupt->irq.sharable = p->sharable;
3248 interrupt->irq.interrupt_count = 1;
3249 interrupt->irq.interrupts[0] = p->interrupts[i];
3250 }
3251 return AE_OK;
3252 }
3253 case ACPI_RESOURCE_TYPE_IO:
3254 {
3255 struct acpi_resource_io *io = &resource->data.io;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003256 struct sony_pic_ioport *ioport =
3257 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
malattia@linux.it33a04452007-04-09 19:26:03 +02003258 if (!io) {
3259 dprintk("Blank IO resource\n");
3260 return AE_OK;
3261 }
3262
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003263 if (!ioport->io1.minimum) {
3264 memcpy(&ioport->io1, io, sizeof(*io));
3265 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
3266 ioport->io1.address_length);
3267 }
3268 else if (!ioport->io2.minimum) {
3269 memcpy(&ioport->io2, io, sizeof(*io));
3270 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
3271 ioport->io2.address_length);
3272 }
3273 else {
Joe Perches50f581a2011-03-29 15:21:48 -07003274 pr_err("Unknown SPIC Type, more than 2 IO Ports\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003275 return AE_ERROR;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003276 }
malattia@linux.it33a04452007-04-09 19:26:03 +02003277 return AE_OK;
3278 }
3279 default:
3280 dprintk("Resource %d isn't an IRQ nor an IO port\n",
Joe Perches50f581a2011-03-29 15:21:48 -07003281 resource->type);
malattia@linux.it33a04452007-04-09 19:26:03 +02003282
3283 case ACPI_RESOURCE_TYPE_END_TAG:
3284 return AE_OK;
3285 }
3286 return AE_CTRL_TERMINATE;
3287}
3288
3289static int sony_pic_possible_resources(struct acpi_device *device)
3290{
3291 int result = 0;
3292 acpi_status status = AE_OK;
3293
3294 if (!device)
3295 return -EINVAL;
3296
3297 /* get device status */
3298 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
3299 dprintk("Evaluating _STA\n");
3300 result = acpi_bus_get_status(device);
3301 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07003302 pr_warn("Unable to read status\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003303 goto end;
3304 }
3305
3306 if (!device->status.enabled)
3307 dprintk("Device disabled\n");
3308 else
3309 dprintk("Device enabled\n");
3310
3311 /*
3312 * Query and parse 'method'
3313 */
3314 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
3315 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
3316 sony_pic_read_possible_resource, &spic_dev);
3317 if (ACPI_FAILURE(status)) {
Joe Perches50f581a2011-03-29 15:21:48 -07003318 pr_warn("Failure evaluating %s\n", METHOD_NAME__PRS);
malattia@linux.it33a04452007-04-09 19:26:03 +02003319 result = -ENODEV;
3320 }
3321end:
3322 return result;
3323}
3324
3325/*
3326 * Disable the spic device by calling its _DIS method
3327 */
3328static int sony_pic_disable(struct acpi_device *device)
3329{
Matthew Garrett6158d3a2008-10-29 14:01:03 -07003330 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
3331 NULL);
3332
3333 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
malattia@linux.it33a04452007-04-09 19:26:03 +02003334 return -ENXIO;
3335
3336 dprintk("Device disabled\n");
3337 return 0;
3338}
3339
3340
3341/*
3342 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
3343 *
3344 * Call _SRS to set current resources
3345 */
3346static int sony_pic_enable(struct acpi_device *device,
3347 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
3348{
3349 acpi_status status;
3350 int result = 0;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003351 /* Type 1 resource layout is:
3352 * IO
3353 * IO
3354 * IRQNoFlags
3355 * End
3356 *
3357 * Type 2 and 3 resource layout is:
3358 * IO
3359 * IRQNoFlags
3360 * End
3361 */
malattia@linux.it33a04452007-04-09 19:26:03 +02003362 struct {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003363 struct acpi_resource res1;
3364 struct acpi_resource res2;
3365 struct acpi_resource res3;
3366 struct acpi_resource res4;
malattia@linux.it33a04452007-04-09 19:26:03 +02003367 } *resource;
3368 struct acpi_buffer buffer = { 0, NULL };
3369
3370 if (!ioport || !irq)
3371 return -EINVAL;
3372
3373 /* init acpi_buffer */
3374 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
3375 if (!resource)
3376 return -ENOMEM;
3377
3378 buffer.length = sizeof(*resource) + 1;
3379 buffer.pointer = resource;
3380
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003381 /* setup Type 1 resources */
Mattia Dongili31df7142009-09-16 00:05:29 +09003382 if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003383
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003384 /* setup io resources */
3385 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
3386 resource->res1.length = sizeof(struct acpi_resource);
3387 memcpy(&resource->res1.data.io, &ioport->io1,
3388 sizeof(struct acpi_resource_io));
malattia@linux.it33a04452007-04-09 19:26:03 +02003389
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003390 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
3391 resource->res2.length = sizeof(struct acpi_resource);
3392 memcpy(&resource->res2.data.io, &ioport->io2,
3393 sizeof(struct acpi_resource_io));
3394
3395 /* setup irq resource */
3396 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
3397 resource->res3.length = sizeof(struct acpi_resource);
3398 memcpy(&resource->res3.data.irq, &irq->irq,
3399 sizeof(struct acpi_resource_irq));
3400 /* we requested a shared irq */
3401 resource->res3.data.irq.sharable = ACPI_SHARED;
3402
3403 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
3404
3405 }
3406 /* setup Type 2/3 resources */
3407 else {
3408 /* setup io resource */
3409 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
3410 resource->res1.length = sizeof(struct acpi_resource);
3411 memcpy(&resource->res1.data.io, &ioport->io1,
3412 sizeof(struct acpi_resource_io));
3413
3414 /* setup irq resource */
3415 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
3416 resource->res2.length = sizeof(struct acpi_resource);
3417 memcpy(&resource->res2.data.irq, &irq->irq,
3418 sizeof(struct acpi_resource_irq));
3419 /* we requested a shared irq */
3420 resource->res2.data.irq.sharable = ACPI_SHARED;
3421
3422 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
3423 }
malattia@linux.it33a04452007-04-09 19:26:03 +02003424
3425 /* Attempt to set the resource */
3426 dprintk("Evaluating _SRS\n");
3427 status = acpi_set_current_resources(device->handle, &buffer);
3428
3429 /* check for total failure */
3430 if (ACPI_FAILURE(status)) {
Joe Perches50f581a2011-03-29 15:21:48 -07003431 pr_err("Error evaluating _SRS\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003432 result = -ENODEV;
3433 goto end;
3434 }
3435
3436 /* Necessary device initializations calls (from sonypi) */
3437 sony_pic_call1(0x82);
3438 sony_pic_call2(0x81, 0xff);
3439 sony_pic_call1(compat ? 0x92 : 0x82);
3440
3441end:
3442 kfree(resource);
3443 return result;
3444}
3445
3446/*****************
3447 *
3448 * ISR: some event is available
3449 *
3450 *****************/
3451static irqreturn_t sony_pic_irq(int irq, void *dev_id)
3452{
3453 int i, j;
malattia@linux.it33a04452007-04-09 19:26:03 +02003454 u8 ev = 0;
3455 u8 data_mask = 0;
3456 u8 device_event = 0;
3457
3458 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
3459
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003460 ev = inb_p(dev->cur_ioport->io1.minimum);
3461 if (dev->cur_ioport->io2.minimum)
3462 data_mask = inb_p(dev->cur_ioport->io2.minimum);
3463 else
Mattia Dongilide920432008-01-14 18:05:43 +09003464 data_mask = inb_p(dev->cur_ioport->io1.minimum +
Mattia Dongili31df7142009-09-16 00:05:29 +09003465 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003466
Mattia Dongili22a17782007-07-16 02:34:39 +09003467 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
Mattia Dongilide920432008-01-14 18:05:43 +09003468 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09003469 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003470
3471 if (ev == 0x00 || ev == 0xff)
3472 return IRQ_HANDLED;
3473
Mattia Dongili31df7142009-09-16 00:05:29 +09003474 for (i = 0; dev->event_types[i].mask; i++) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003475
Mattia Dongili31df7142009-09-16 00:05:29 +09003476 if ((data_mask & dev->event_types[i].data) !=
3477 dev->event_types[i].data)
malattia@linux.it33a04452007-04-09 19:26:03 +02003478 continue;
3479
Mattia Dongili31df7142009-09-16 00:05:29 +09003480 if (!(mask & dev->event_types[i].mask))
malattia@linux.it33a04452007-04-09 19:26:03 +02003481 continue;
3482
Mattia Dongili31df7142009-09-16 00:05:29 +09003483 for (j = 0; dev->event_types[i].events[j].event; j++) {
3484 if (ev == dev->event_types[i].events[j].data) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003485 device_event =
Mattia Dongili31df7142009-09-16 00:05:29 +09003486 dev->event_types[i].events[j].event;
Mattia Dongili4eeb5022011-02-19 11:52:27 +09003487 /* some events may require ignoring */
3488 if (!device_event)
3489 return IRQ_HANDLED;
malattia@linux.it33a04452007-04-09 19:26:03 +02003490 goto found;
3491 }
3492 }
3493 }
Mattia Dongili3eb87492008-01-14 18:05:45 +09003494 /* Still not able to decode the event try to pass
3495 * it over to the minidriver
3496 */
Mattia Dongili31df7142009-09-16 00:05:29 +09003497 if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
Mattia Dongili3eb87492008-01-14 18:05:45 +09003498 return IRQ_HANDLED;
3499
Mattia Dongilide920432008-01-14 18:05:43 +09003500 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
3501 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09003502 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003503 return IRQ_HANDLED;
3504
3505found:
malattia@linux.it1549ee62007-04-09 10:19:08 +02003506 sony_laptop_report_input_event(device_event);
Mattia Dongilide920432008-01-14 18:05:43 +09003507 acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003508 sonypi_compat_report_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02003509 return IRQ_HANDLED;
3510}
3511
3512/*****************
3513 *
3514 * ACPI driver
3515 *
3516 *****************/
3517static int sony_pic_remove(struct acpi_device *device, int type)
3518{
3519 struct sony_pic_ioport *io, *tmp_io;
3520 struct sony_pic_irq *irq, *tmp_irq;
3521
3522 if (sony_pic_disable(device)) {
Joe Perches50f581a2011-03-29 15:21:48 -07003523 pr_err("Couldn't disable device\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003524 return -ENXIO;
3525 }
3526
3527 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003528 release_region(spic_dev.cur_ioport->io1.minimum,
3529 spic_dev.cur_ioport->io1.address_length);
3530 if (spic_dev.cur_ioport->io2.minimum)
3531 release_region(spic_dev.cur_ioport->io2.minimum,
3532 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02003533
Mattia Dongili015a9162007-08-12 16:20:27 +09003534 sonypi_compat_exit();
3535
malattia@linux.it1549ee62007-04-09 10:19:08 +02003536 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02003537
malattia@linux.it49a11de2007-04-09 19:28:56 +02003538 /* pf attrs */
3539 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
3540 sony_pf_remove();
3541
malattia@linux.it33a04452007-04-09 19:26:03 +02003542 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
3543 list_del(&io->list);
3544 kfree(io);
3545 }
3546 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
3547 list_del(&irq->list);
3548 kfree(irq);
3549 }
3550 spic_dev.cur_ioport = NULL;
3551 spic_dev.cur_irq = NULL;
3552
malattia@linux.itf6119b02007-04-09 19:31:06 +02003553 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003554 return 0;
3555}
3556
3557static int sony_pic_add(struct acpi_device *device)
3558{
3559 int result;
3560 struct sony_pic_ioport *io, *tmp_io;
3561 struct sony_pic_irq *irq, *tmp_irq;
3562
Joe Perches50f581a2011-03-29 15:21:48 -07003563 pr_info("%s v%s\n", SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.it33a04452007-04-09 19:26:03 +02003564
3565 spic_dev.acpi_dev = device;
3566 strcpy(acpi_device_class(device), "sony/hotkey");
Mattia Dongilide920432008-01-14 18:05:43 +09003567 sony_pic_detect_device_type(&spic_dev);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003568 mutex_init(&spic_dev.lock);
malattia@linux.it33a04452007-04-09 19:26:03 +02003569
3570 /* read _PRS resources */
3571 result = sony_pic_possible_resources(device);
3572 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07003573 pr_err("Unable to read possible resources\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003574 goto err_free_resources;
3575 }
3576
3577 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05003578 result = sony_laptop_setup_input(device);
malattia@linux.it33a04452007-04-09 19:26:03 +02003579 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07003580 pr_err("Unable to create input devices\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003581 goto err_free_resources;
3582 }
3583
Mattia Dongili015a9162007-08-12 16:20:27 +09003584 if (sonypi_compat_init())
3585 goto err_remove_input;
3586
malattia@linux.it33a04452007-04-09 19:26:03 +02003587 /* request io port */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003588 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
3589 if (request_region(io->io1.minimum, io->io1.address_length,
Lucas De Marchic8440332011-03-17 17:18:22 -03003590 "Sony Programmable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003591 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
3592 io->io1.minimum, io->io1.maximum,
3593 io->io1.address_length);
3594 /* Type 1 have 2 ioports */
3595 if (io->io2.minimum) {
3596 if (request_region(io->io2.minimum,
3597 io->io2.address_length,
Lucas De Marchic8440332011-03-17 17:18:22 -03003598 "Sony Programmable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003599 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
3600 io->io2.minimum, io->io2.maximum,
3601 io->io2.address_length);
3602 spic_dev.cur_ioport = io;
3603 break;
3604 }
3605 else {
3606 dprintk("Unable to get I/O port2: "
3607 "0x%.4x (0x%.4x) + 0x%.2x\n",
3608 io->io2.minimum, io->io2.maximum,
3609 io->io2.address_length);
3610 release_region(io->io1.minimum,
3611 io->io1.address_length);
3612 }
3613 }
3614 else {
3615 spic_dev.cur_ioport = io;
3616 break;
3617 }
malattia@linux.it33a04452007-04-09 19:26:03 +02003618 }
3619 }
3620 if (!spic_dev.cur_ioport) {
Joe Perches50f581a2011-03-29 15:21:48 -07003621 pr_err("Failed to request_region\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003622 result = -ENODEV;
Mattia Dongili015a9162007-08-12 16:20:27 +09003623 goto err_remove_compat;
malattia@linux.it33a04452007-04-09 19:26:03 +02003624 }
3625
3626 /* request IRQ */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003627 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003628 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
Yong Zhang6a0d89c2011-10-22 17:56:50 +08003629 0, "sony-laptop", &spic_dev)) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003630 dprintk("IRQ: %d - triggering: %d - "
3631 "polarity: %d - shr: %d\n",
3632 irq->irq.interrupts[0],
3633 irq->irq.triggering,
3634 irq->irq.polarity,
3635 irq->irq.sharable);
3636 spic_dev.cur_irq = irq;
3637 break;
3638 }
3639 }
3640 if (!spic_dev.cur_irq) {
Joe Perches50f581a2011-03-29 15:21:48 -07003641 pr_err("Failed to request_irq\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003642 result = -ENODEV;
3643 goto err_release_region;
3644 }
3645
3646 /* set resource status _SRS */
3647 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
3648 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07003649 pr_err("Couldn't enable device\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003650 goto err_free_irq;
3651 }
3652
malattia@linux.it49a11de2007-04-09 19:28:56 +02003653 spic_dev.bluetooth_power = -1;
3654 /* create device attributes */
3655 result = sony_pf_add();
3656 if (result)
3657 goto err_disable_device;
3658
3659 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
3660 if (result)
3661 goto err_remove_pf;
3662
malattia@linux.it33a04452007-04-09 19:26:03 +02003663 return 0;
3664
malattia@linux.it49a11de2007-04-09 19:28:56 +02003665err_remove_pf:
3666 sony_pf_remove();
3667
3668err_disable_device:
3669 sony_pic_disable(device);
3670
malattia@linux.it33a04452007-04-09 19:26:03 +02003671err_free_irq:
3672 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
3673
3674err_release_region:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003675 release_region(spic_dev.cur_ioport->io1.minimum,
3676 spic_dev.cur_ioport->io1.address_length);
3677 if (spic_dev.cur_ioport->io2.minimum)
3678 release_region(spic_dev.cur_ioport->io2.minimum,
3679 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02003680
Mattia Dongili015a9162007-08-12 16:20:27 +09003681err_remove_compat:
3682 sonypi_compat_exit();
3683
malattia@linux.it33a04452007-04-09 19:26:03 +02003684err_remove_input:
malattia@linux.it1549ee62007-04-09 10:19:08 +02003685 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02003686
3687err_free_resources:
3688 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
3689 list_del(&io->list);
3690 kfree(io);
3691 }
3692 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
3693 list_del(&irq->list);
3694 kfree(irq);
3695 }
3696 spic_dev.cur_ioport = NULL;
3697 spic_dev.cur_irq = NULL;
3698
3699 return result;
3700}
3701
3702static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
3703{
3704 if (sony_pic_disable(device))
3705 return -ENXIO;
3706 return 0;
3707}
3708
3709static int sony_pic_resume(struct acpi_device *device)
3710{
3711 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
3712 return 0;
3713}
3714
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003715static const struct acpi_device_id sony_pic_device_ids[] = {
3716 {SONY_PIC_HID, 0},
3717 {"", 0},
3718};
3719
malattia@linux.it33a04452007-04-09 19:26:03 +02003720static struct acpi_driver sony_pic_driver = {
3721 .name = SONY_PIC_DRIVER_NAME,
3722 .class = SONY_PIC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003723 .ids = sony_pic_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02003724 .owner = THIS_MODULE,
3725 .ops = {
3726 .add = sony_pic_add,
3727 .remove = sony_pic_remove,
3728 .suspend = sony_pic_suspend,
3729 .resume = sony_pic_resume,
3730 },
3731};
3732
3733static struct dmi_system_id __initdata sonypi_dmi_table[] = {
3734 {
3735 .ident = "Sony Vaio",
3736 .matches = {
3737 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
3738 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
3739 },
3740 },
3741 {
3742 .ident = "Sony Vaio",
3743 .matches = {
3744 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
3745 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
3746 },
3747 },
3748 { }
3749};
3750
malattia@linux.it59b19102007-04-09 10:19:04 +02003751static int __init sony_laptop_init(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01003752{
malattia@linux.it33a04452007-04-09 19:26:03 +02003753 int result;
3754
3755 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
3756 result = acpi_bus_register_driver(&sony_pic_driver);
3757 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07003758 pr_err("Unable to register SPIC driver\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003759 goto out;
3760 }
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003761 spic_drv_registered = 1;
malattia@linux.it33a04452007-04-09 19:26:03 +02003762 }
3763
3764 result = acpi_bus_register_driver(&sony_nc_driver);
3765 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07003766 pr_err("Unable to register SNC driver\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003767 goto out_unregister_pic;
3768 }
3769
3770 return 0;
3771
3772out_unregister_pic:
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003773 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02003774 acpi_bus_unregister_driver(&sony_pic_driver);
3775out:
3776 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +01003777}
3778
malattia@linux.it59b19102007-04-09 10:19:04 +02003779static void __exit sony_laptop_exit(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01003780{
malattia@linux.it59b19102007-04-09 10:19:04 +02003781 acpi_bus_unregister_driver(&sony_nc_driver);
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003782 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02003783 acpi_bus_unregister_driver(&sony_pic_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +01003784}
3785
malattia@linux.it59b19102007-04-09 10:19:04 +02003786module_init(sony_laptop_init);
3787module_exit(sony_laptop_exit);