blob: e642f5f295046edf07f9d16aed5bc26f5dde8731 [file] [log] [blame]
Stelian Pop7f09c432007-01-13 23:04:31 +01001/*
malattia@linux.it33a04452007-04-09 19:26:03 +02002 * ACPI Sony Notebook Control Driver (SNC and SPIC)
Stelian Pop7f09c432007-01-13 23:04:31 +01003 *
4 * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
Mattia Dongili37418342009-03-26 21:58:24 +09005 * Copyright (C) 2007-2009 Mattia Dongili <malattia@linux.it>
Stelian Pop7f09c432007-01-13 23:04:31 +01006 *
7 * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
8 * which are copyrighted by their respective authors.
9 *
malattia@linux.it33a04452007-04-09 19:26:03 +020010 * The SNY6001 driver part is based on the sonypi driver which includes
11 * material from:
12 *
13 * Copyright (C) 2001-2005 Stelian Pop <stelian@popies.net>
14 *
15 * Copyright (C) 2005 Narayanan R S <nars@kadamba.org>
16 *
Marcin Garskidb955172007-10-19 23:22:11 +020017 * Copyright (C) 2001-2002 AlcĂ´ve <www.alcove.com>
malattia@linux.it33a04452007-04-09 19:26:03 +020018 *
19 * Copyright (C) 2001 Michael Ashley <m.ashley@unsw.edu.au>
20 *
21 * Copyright (C) 2001 Junichi Morita <jun1m@mars.dti.ne.jp>
22 *
23 * Copyright (C) 2000 Takaya Kinjo <t-kinjo@tc4.so-net.ne.jp>
24 *
25 * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
26 *
27 * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
28 *
Stelian Pop7f09c432007-01-13 23:04:31 +010029 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
33 *
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
38 *
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
42 *
43 */
44
45#include <linux/kernel.h>
46#include <linux/module.h>
47#include <linux/moduleparam.h>
48#include <linux/init.h>
49#include <linux/types.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010050#include <linux/backlight.h>
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010051#include <linux/platform_device.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010052#include <linux/err.h>
malattia@linux.it33a04452007-04-09 19:26:03 +020053#include <linux/dmi.h>
54#include <linux/pci.h>
55#include <linux/interrupt.h>
56#include <linux/delay.h>
57#include <linux/input.h>
58#include <linux/kfifo.h>
59#include <linux/workqueue.h>
60#include <linux/acpi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090061#include <linux/slab.h>
Stelian Pop7f09c432007-01-13 23:04:31 +010062#include <acpi/acpi_drivers.h>
63#include <acpi/acpi_bus.h>
64#include <asm/uaccess.h>
malattia@linux.it33a04452007-04-09 19:26:03 +020065#include <linux/sonypi.h>
malattia@linux.it1ce82c12007-04-28 23:34:36 +090066#include <linux/sony-laptop.h>
Matthew Garrett6cc056b2009-03-26 21:58:15 +090067#include <linux/rfkill.h>
Mattia Dongilia64e62a2007-05-01 11:19:53 +090068#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +020069#include <linux/poll.h>
70#include <linux/miscdevice.h>
71#endif
Stelian Pop7f09c432007-01-13 23:04:31 +010072
malattia@linux.it33a04452007-04-09 19:26:03 +020073#define DRV_PFX "sony-laptop: "
Mattia Dongilid6697932011-02-19 11:52:28 +090074#define dprintk(msg...) do { \
75 if (debug) \
76 pr_warn(DRV_PFX msg); \
malattia@linux.itb9a218b2007-04-09 10:19:06 +020077} while (0)
78
Mattia Dongili425ef5d2008-01-14 18:05:44 +090079#define SONY_LAPTOP_DRIVER_VERSION "0.6"
Alessandro Guido50f62af2007-01-13 23:04:34 +010080
malattia@linux.it33a04452007-04-09 19:26:03 +020081#define SONY_NC_CLASS "sony-nc"
82#define SONY_NC_HID "SNY5001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020083#define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
malattia@linux.it33a04452007-04-09 19:26:03 +020084
85#define SONY_PIC_CLASS "sony-pic"
86#define SONY_PIC_HID "SNY6001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020087#define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
Stelian Pop7f09c432007-01-13 23:04:31 +010088
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010089MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
malattia@linux.it33a04452007-04-09 19:26:03 +020090MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
Stelian Pop7f09c432007-01-13 23:04:31 +010091MODULE_LICENSE("GPL");
malattia@linux.it33a04452007-04-09 19:26:03 +020092MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
Stelian Pop7f09c432007-01-13 23:04:31 +010093
94static int debug;
95module_param(debug, int, 0);
96MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
Len Browna02d1c12007-02-07 15:34:02 -050097 "the development of this driver");
Stelian Pop7f09c432007-01-13 23:04:31 +010098
malattia@linux.it33a04452007-04-09 19:26:03 +020099static int no_spic; /* = 0 */
100module_param(no_spic, int, 0444);
101MODULE_PARM_DESC(no_spic,
102 "set this if you don't want to enable the SPIC device");
103
104static int compat; /* = 0 */
105module_param(compat, int, 0444);
106MODULE_PARM_DESC(compat,
malattia@linux.it7b153f32007-04-09 19:31:25 +0200107 "set this if you want to enable backward compatibility mode");
malattia@linux.it33a04452007-04-09 19:26:03 +0200108
109static unsigned long mask = 0xffffffff;
110module_param(mask, ulong, 0644);
111MODULE_PARM_DESC(mask,
112 "set this to the mask of event you want to enable (see doc)");
113
malattia@linux.it5f3d2892007-04-28 23:18:45 +0900114static int camera; /* = 0 */
115module_param(camera, int, 0444);
116MODULE_PARM_DESC(camera,
117 "set this to 1 to enable Motion Eye camera controls "
118 "(only use it if you have a C1VE or C1VN model)");
119
Mattia Dongilia64e62a2007-05-01 11:19:53 +0900120#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +0200121static int minor = -1;
122module_param(minor, int, 0);
123MODULE_PARM_DESC(minor,
124 "minor number of the misc device for the SPIC compatibility code, "
125 "default is -1 (automatic)");
126#endif
127
Mattia Dongilibf155712011-02-19 11:52:31 +0900128static int kbd_backlight; /* = 1 */
129module_param(kbd_backlight, int, 0444);
130MODULE_PARM_DESC(kbd_backlight,
131 "set this to 0 to disable keyboard backlight, "
132 "1 to enable it (default: 0)");
133
134static int kbd_backlight_timeout; /* = 0 */
135module_param(kbd_backlight_timeout, int, 0444);
136MODULE_PARM_DESC(kbd_backlight_timeout,
137 "set this to 0 to set the default 10 seconds timeout, "
138 "1 for 30 seconds, 2 for 60 seconds and 3 to disable timeout "
139 "(default: 0)");
140
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900141enum sony_nc_rfkill {
142 SONY_WIFI,
143 SONY_BLUETOOTH,
144 SONY_WWAN,
145 SONY_WIMAX,
Johannes Berg19d337d2009-06-02 13:01:37 +0200146 N_SONY_RFKILL,
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900147};
148
Mattia Dongilid5a664a2009-12-17 00:08:35 +0900149static int sony_rfkill_handle;
Johannes Berg19d337d2009-06-02 13:01:37 +0200150static struct rfkill *sony_rfkill_devices[N_SONY_RFKILL];
151static int sony_rfkill_address[N_SONY_RFKILL] = {0x300, 0x500, 0x700, 0x900};
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900152static void sony_nc_rfkill_update(void);
153
malattia@linux.it1549ee62007-04-09 10:19:08 +0200154/*********** Input Devices ***********/
155
156#define SONY_LAPTOP_BUF_SIZE 128
157struct sony_laptop_input_s {
158 atomic_t users;
159 struct input_dev *jog_dev;
160 struct input_dev *key_dev;
Stefani Seibold45465482009-12-21 14:37:26 -0800161 struct kfifo fifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200162 spinlock_t fifo_lock;
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800163 struct timer_list release_key_timer;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200164};
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900165
malattia@linux.it1549ee62007-04-09 10:19:08 +0200166static struct sony_laptop_input_s sony_laptop_input = {
167 .users = ATOMIC_INIT(0),
168};
169
170struct sony_laptop_keypress {
171 struct input_dev *dev;
172 int key;
173};
174
Mattia Dongilibc57f862007-07-20 02:01:57 +0900175/* Correspondance table between sonypi events
176 * and input layer indexes in the keymap
177 */
178static int sony_laptop_input_index[] = {
Mattia Dongili3eb87492008-01-14 18:05:45 +0900179 -1, /* 0 no event */
180 -1, /* 1 SONYPI_EVENT_JOGDIAL_DOWN */
181 -1, /* 2 SONYPI_EVENT_JOGDIAL_UP */
182 -1, /* 3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
183 -1, /* 4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */
184 -1, /* 5 SONYPI_EVENT_JOGDIAL_PRESSED */
185 -1, /* 6 SONYPI_EVENT_JOGDIAL_RELEASED */
186 0, /* 7 SONYPI_EVENT_CAPTURE_PRESSED */
187 1, /* 8 SONYPI_EVENT_CAPTURE_RELEASED */
188 2, /* 9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
189 3, /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
190 4, /* 11 SONYPI_EVENT_FNKEY_ESC */
191 5, /* 12 SONYPI_EVENT_FNKEY_F1 */
192 6, /* 13 SONYPI_EVENT_FNKEY_F2 */
193 7, /* 14 SONYPI_EVENT_FNKEY_F3 */
194 8, /* 15 SONYPI_EVENT_FNKEY_F4 */
195 9, /* 16 SONYPI_EVENT_FNKEY_F5 */
196 10, /* 17 SONYPI_EVENT_FNKEY_F6 */
197 11, /* 18 SONYPI_EVENT_FNKEY_F7 */
198 12, /* 19 SONYPI_EVENT_FNKEY_F8 */
199 13, /* 20 SONYPI_EVENT_FNKEY_F9 */
200 14, /* 21 SONYPI_EVENT_FNKEY_F10 */
201 15, /* 22 SONYPI_EVENT_FNKEY_F11 */
202 16, /* 23 SONYPI_EVENT_FNKEY_F12 */
203 17, /* 24 SONYPI_EVENT_FNKEY_1 */
204 18, /* 25 SONYPI_EVENT_FNKEY_2 */
205 19, /* 26 SONYPI_EVENT_FNKEY_D */
206 20, /* 27 SONYPI_EVENT_FNKEY_E */
207 21, /* 28 SONYPI_EVENT_FNKEY_F */
208 22, /* 29 SONYPI_EVENT_FNKEY_S */
209 23, /* 30 SONYPI_EVENT_FNKEY_B */
210 24, /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */
211 25, /* 32 SONYPI_EVENT_PKEY_P1 */
212 26, /* 33 SONYPI_EVENT_PKEY_P2 */
213 27, /* 34 SONYPI_EVENT_PKEY_P3 */
214 28, /* 35 SONYPI_EVENT_BACK_PRESSED */
215 -1, /* 36 SONYPI_EVENT_LID_CLOSED */
216 -1, /* 37 SONYPI_EVENT_LID_OPENED */
217 29, /* 38 SONYPI_EVENT_BLUETOOTH_ON */
218 30, /* 39 SONYPI_EVENT_BLUETOOTH_OFF */
219 31, /* 40 SONYPI_EVENT_HELP_PRESSED */
220 32, /* 41 SONYPI_EVENT_FNKEY_ONLY */
221 33, /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
222 34, /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */
223 35, /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
224 36, /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
225 37, /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
226 38, /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */
227 39, /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
228 40, /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
229 41, /* 50 SONYPI_EVENT_ZOOM_PRESSED */
230 42, /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */
231 43, /* 52 SONYPI_EVENT_MEYE_FACE */
232 44, /* 53 SONYPI_EVENT_MEYE_OPPOSITE */
233 45, /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */
234 46, /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */
235 -1, /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */
236 -1, /* 57 SONYPI_EVENT_BATTERY_INSERT */
237 -1, /* 58 SONYPI_EVENT_BATTERY_REMOVE */
238 -1, /* 59 SONYPI_EVENT_FNKEY_RELEASED */
239 47, /* 60 SONYPI_EVENT_WIRELESS_ON */
240 48, /* 61 SONYPI_EVENT_WIRELESS_OFF */
241 49, /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */
242 50, /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900243 51, /* 64 SONYPI_EVENT_CD_EJECT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900244 52, /* 65 SONYPI_EVENT_MODEKEY_PRESSED */
245 53, /* 66 SONYPI_EVENT_PKEY_P4 */
246 54, /* 67 SONYPI_EVENT_PKEY_P5 */
247 55, /* 68 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900248 56, /* 69 SONYPI_EVENT_VOLUME_INC_PRESSED */
249 57, /* 70 SONYPI_EVENT_VOLUME_DEC_PRESSED */
250 -1, /* 71 SONYPI_EVENT_BRIGHTNESS_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900251 58, /* 72 SONYPI_EVENT_MEDIA_PRESSED */
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900252 59, /* 72 SONYPI_EVENT_VENDOR_PRESSED */
Mattia Dongilibc57f862007-07-20 02:01:57 +0900253};
254
255static int sony_laptop_input_keycode_map[] = {
256 KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
257 KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
258 KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
259 KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
260 KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
261 KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
262 KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
263 KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
264 KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
265 KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
266 KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
267 KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
268 KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
269 KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
270 KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
271 KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
272 KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
273 KEY_FN_F1, /* 17 SONYPI_EVENT_FNKEY_1 */
274 KEY_FN_F2, /* 18 SONYPI_EVENT_FNKEY_2 */
275 KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
276 KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
277 KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
278 KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
279 KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
280 KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
281 KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
282 KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
283 KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
284 KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
285 KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
286 KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
287 KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
288 KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
289 KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
290 KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
291 KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
292 KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
293 KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
294 KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
295 KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
296 KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
297 KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
298 BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
299 KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
300 KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
301 KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
302 KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
303 KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
304 KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
Mattia Dongili3eb87492008-01-14 18:05:45 +0900305 KEY_ZOOMIN, /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900306 KEY_ZOOMOUT, /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900307 KEY_EJECTCD, /* 51 SONYPI_EVENT_CD_EJECT_PRESSED */
308 KEY_F13, /* 52 SONYPI_EVENT_MODEKEY_PRESSED */
309 KEY_PROG4, /* 53 SONYPI_EVENT_PKEY_P4 */
310 KEY_F14, /* 54 SONYPI_EVENT_PKEY_P5 */
311 KEY_F15, /* 55 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900312 KEY_VOLUMEUP, /* 56 SONYPI_EVENT_VOLUME_INC_PRESSED */
313 KEY_VOLUMEDOWN, /* 57 SONYPI_EVENT_VOLUME_DEC_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900314 KEY_MEDIA, /* 58 SONYPI_EVENT_MEDIA_PRESSED */
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900315 KEY_VENDOR, /* 59 SONYPI_EVENT_VENDOR_PRESSED */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200316};
317
318/* release buttons after a short delay if pressed */
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800319static void do_sony_laptop_release_key(unsigned long unused)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200320{
321 struct sony_laptop_keypress kp;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800322 unsigned long flags;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200323
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800324 spin_lock_irqsave(&sony_laptop_input.fifo_lock, flags);
325
326 if (kfifo_out(&sony_laptop_input.fifo,
327 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200328 input_report_key(kp.dev, kp.key, 0);
329 input_sync(kp.dev);
330 }
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800331
332 /* If there is something in the fifo schedule next release. */
333 if (kfifo_len(&sony_laptop_input.fifo) != 0)
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800334 mod_timer(&sony_laptop_input.release_key_timer,
335 jiffies + msecs_to_jiffies(10));
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800336
337 spin_unlock_irqrestore(&sony_laptop_input.fifo_lock, flags);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200338}
malattia@linux.it1549ee62007-04-09 10:19:08 +0200339
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200340/* forward event to the input subsystem */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200341static void sony_laptop_report_input_event(u8 event)
342{
343 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
344 struct input_dev *key_dev = sony_laptop_input.key_dev;
345 struct sony_laptop_keypress kp = { NULL };
malattia@linux.it1549ee62007-04-09 10:19:08 +0200346
Almer S. Tigelaara83021a2009-04-12 11:26:28 +0000347 if (event == SONYPI_EVENT_FNKEY_RELEASED ||
348 event == SONYPI_EVENT_ANYBUTTON_RELEASED) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200349 /* Nothing, not all VAIOs generate this event */
350 return;
351 }
352
353 /* report events */
354 switch (event) {
355 /* jog_dev events */
356 case SONYPI_EVENT_JOGDIAL_UP:
357 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
358 input_report_rel(jog_dev, REL_WHEEL, 1);
359 input_sync(jog_dev);
360 return;
361
362 case SONYPI_EVENT_JOGDIAL_DOWN:
363 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
364 input_report_rel(jog_dev, REL_WHEEL, -1);
365 input_sync(jog_dev);
366 return;
367
368 /* key_dev events */
369 case SONYPI_EVENT_JOGDIAL_PRESSED:
370 kp.key = BTN_MIDDLE;
371 kp.dev = jog_dev;
372 break;
373
374 default:
Adrian Bunkd399d132008-02-20 00:59:03 +0200375 if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
Mattia Dongilibc57f862007-07-20 02:01:57 +0900376 dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
377 break;
378 }
379 if (sony_laptop_input_index[event] != -1) {
380 kp.key = sony_laptop_input_keycode_map[sony_laptop_input_index[event]];
381 if (kp.key != KEY_UNKNOWN)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200382 kp.dev = key_dev;
Mattia Dongilibc57f862007-07-20 02:01:57 +0900383 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200384 break;
385 }
386
387 if (kp.dev) {
388 input_report_key(kp.dev, kp.key, 1);
Mattia Dongilibc57f862007-07-20 02:01:57 +0900389 /* we emit the scancode so we can always remap the key */
390 input_event(kp.dev, EV_MSC, MSC_SCAN, event);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200391 input_sync(kp.dev);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200392
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800393 /* schedule key release */
394 kfifo_in_locked(&sony_laptop_input.fifo,
395 (unsigned char *)&kp, sizeof(kp),
396 &sony_laptop_input.fifo_lock);
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800397 mod_timer(&sony_laptop_input.release_key_timer,
398 jiffies + msecs_to_jiffies(10));
malattia@linux.it1549ee62007-04-09 10:19:08 +0200399 } else
400 dprintk("unknown input event %.2x\n", event);
401}
402
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500403static int sony_laptop_setup_input(struct acpi_device *acpi_device)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200404{
405 struct input_dev *jog_dev;
406 struct input_dev *key_dev;
407 int i;
408 int error;
409
410 /* don't run again if already initialized */
411 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
412 return 0;
413
414 /* kfifo */
415 spin_lock_init(&sony_laptop_input.fifo_lock);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800416 error = kfifo_alloc(&sony_laptop_input.fifo,
417 SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -0800418 if (error) {
Mattia Dongilid6697932011-02-19 11:52:28 +0900419 pr_err(DRV_PFX "kfifo_alloc failed\n");
malattia@linux.it1549ee62007-04-09 10:19:08 +0200420 goto err_dec_users;
421 }
422
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800423 setup_timer(&sony_laptop_input.release_key_timer,
424 do_sony_laptop_release_key, 0);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200425
426 /* input keys */
427 key_dev = input_allocate_device();
428 if (!key_dev) {
429 error = -ENOMEM;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800430 goto err_free_kfifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200431 }
432
433 key_dev->name = "Sony Vaio Keys";
434 key_dev->id.bustype = BUS_ISA;
435 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500436 key_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200437
438 /* Initialize the Input Drivers: special keys */
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800439 input_set_capability(key_dev, EV_MSC, MSC_SCAN);
440
441 __set_bit(EV_KEY, key_dev->evbit);
Mattia Dongilibc57f862007-07-20 02:01:57 +0900442 key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
443 key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
444 key_dev->keycode = &sony_laptop_input_keycode_map;
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800445 for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++)
446 __set_bit(sony_laptop_input_keycode_map[i], key_dev->keybit);
447 __clear_bit(KEY_RESERVED, key_dev->keybit);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200448
449 error = input_register_device(key_dev);
450 if (error)
451 goto err_free_keydev;
452
453 sony_laptop_input.key_dev = key_dev;
454
455 /* jogdial */
456 jog_dev = input_allocate_device();
457 if (!jog_dev) {
458 error = -ENOMEM;
459 goto err_unregister_keydev;
460 }
461
462 jog_dev->name = "Sony Vaio Jogdial";
463 jog_dev->id.bustype = BUS_ISA;
464 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500465 key_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200466
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800467 input_set_capability(jog_dev, EV_KEY, BTN_MIDDLE);
468 input_set_capability(jog_dev, EV_REL, REL_WHEEL);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200469
470 error = input_register_device(jog_dev);
471 if (error)
472 goto err_free_jogdev;
473
474 sony_laptop_input.jog_dev = jog_dev;
475
476 return 0;
477
478err_free_jogdev:
479 input_free_device(jog_dev);
480
481err_unregister_keydev:
482 input_unregister_device(key_dev);
483 /* to avoid kref underflow below at input_free_device */
484 key_dev = NULL;
485
486err_free_keydev:
487 input_free_device(key_dev);
488
malattia@linux.it1549ee62007-04-09 10:19:08 +0200489err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -0800490 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200491
492err_dec_users:
493 atomic_dec(&sony_laptop_input.users);
494 return error;
495}
496
497static void sony_laptop_remove_input(void)
498{
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800499 struct sony_laptop_keypress kp = { NULL };
500
501 /* Cleanup only after the last user has gone */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200502 if (!atomic_dec_and_test(&sony_laptop_input.users))
503 return;
504
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800505 del_timer_sync(&sony_laptop_input.release_key_timer);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800506
507 /*
508 * Generate key-up events for remaining keys. Note that we don't
509 * need locking since nobody is adding new events to the kfifo.
510 */
511 while (kfifo_out(&sony_laptop_input.fifo,
512 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
513 input_report_key(kp.dev, kp.key, 0);
514 input_sync(kp.dev);
515 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200516
517 /* destroy input devs */
518 input_unregister_device(sony_laptop_input.key_dev);
519 sony_laptop_input.key_dev = NULL;
520
521 if (sony_laptop_input.jog_dev) {
522 input_unregister_device(sony_laptop_input.jog_dev);
523 sony_laptop_input.jog_dev = NULL;
524 }
525
Stefani Seibold45465482009-12-21 14:37:26 -0800526 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200527}
528
malattia@linux.it56b87562007-04-09 10:19:05 +0200529/*********** Platform Device ***********/
530
531static atomic_t sony_pf_users = ATOMIC_INIT(0);
532static struct platform_driver sony_pf_driver = {
533 .driver = {
534 .name = "sony-laptop",
535 .owner = THIS_MODULE,
536 }
537};
538static struct platform_device *sony_pf_device;
539
540static int sony_pf_add(void)
541{
542 int ret = 0;
543
544 /* don't run again if already initialized */
545 if (atomic_add_return(1, &sony_pf_users) > 1)
546 return 0;
547
548 ret = platform_driver_register(&sony_pf_driver);
549 if (ret)
550 goto out;
551
552 sony_pf_device = platform_device_alloc("sony-laptop", -1);
553 if (!sony_pf_device) {
554 ret = -ENOMEM;
555 goto out_platform_registered;
556 }
557
558 ret = platform_device_add(sony_pf_device);
559 if (ret)
560 goto out_platform_alloced;
561
562 return 0;
563
564 out_platform_alloced:
565 platform_device_put(sony_pf_device);
566 sony_pf_device = NULL;
567 out_platform_registered:
568 platform_driver_unregister(&sony_pf_driver);
569 out:
570 atomic_dec(&sony_pf_users);
571 return ret;
572}
573
574static void sony_pf_remove(void)
575{
576 /* deregister only after the last user has gone */
577 if (!atomic_dec_and_test(&sony_pf_users))
578 return;
579
Axel Lin08db2b32010-07-01 10:18:01 +0800580 platform_device_unregister(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +0200581 platform_driver_unregister(&sony_pf_driver);
582}
583
584/*********** SNC (SNY5001) Device ***********/
585
malattia@linux.it33a04452007-04-09 19:26:03 +0200586/* the device uses 1-based values, while the backlight subsystem uses
587 0-based values */
588#define SONY_MAX_BRIGHTNESS 8
589
590#define SNC_VALIDATE_IN 0
591#define SNC_VALIDATE_OUT 1
592
malattia@linux.it59b19102007-04-09 10:19:04 +0200593static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500594 char *);
malattia@linux.it59b19102007-04-09 10:19:04 +0200595static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500596 const char *, size_t);
Mattia Dongili156c2212007-02-12 22:01:07 +0100597static int boolean_validate(const int, const int);
598static int brightness_default_validate(const int, const int);
599
malattia@linux.it59b19102007-04-09 10:19:04 +0200600struct sony_nc_value {
Len Browna02d1c12007-02-07 15:34:02 -0500601 char *name; /* name of the entry */
602 char **acpiget; /* names of the ACPI get function */
603 char **acpiset; /* names of the ACPI set function */
Mattia Dongili156c2212007-02-12 22:01:07 +0100604 int (*validate)(const int, const int); /* input/output validation */
Len Browna02d1c12007-02-07 15:34:02 -0500605 int value; /* current setting */
606 int valid; /* Has ever been set */
607 int debug; /* active only in debug mode ? */
Lucas De Marchic8440332011-03-17 17:18:22 -0300608 struct device_attribute devattr; /* sysfs attribute */
Stelian Pop7f09c432007-01-13 23:04:31 +0100609};
610
malattia@linux.it59b19102007-04-09 10:19:04 +0200611#define SNC_HANDLE_NAMES(_name, _values...) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100612 static char *snc_##_name[] = { _values, NULL }
613
malattia@linux.it59b19102007-04-09 10:19:04 +0200614#define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100615 { \
616 .name = __stringify(_name), \
617 .acpiget = _getters, \
618 .acpiset = _setters, \
Mattia Dongili156c2212007-02-12 22:01:07 +0100619 .validate = _validate, \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100620 .debug = _debug, \
malattia@linux.it59b19102007-04-09 10:19:04 +0200621 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100622 }
623
malattia@linux.it59b19102007-04-09 10:19:04 +0200624#define SNC_HANDLE_NULL { .name = NULL }
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100625
malattia@linux.it59b19102007-04-09 10:19:04 +0200626SNC_HANDLE_NAMES(fnkey_get, "GHKE");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100627
malattia@linux.it59b19102007-04-09 10:19:04 +0200628SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
629SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100630
malattia@linux.it59b19102007-04-09 10:19:04 +0200631SNC_HANDLE_NAMES(cdpower_get, "GCDP");
632SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100633
malattia@linux.it59b19102007-04-09 10:19:04 +0200634SNC_HANDLE_NAMES(audiopower_get, "GAZP");
635SNC_HANDLE_NAMES(audiopower_set, "AZPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100636
malattia@linux.it59b19102007-04-09 10:19:04 +0200637SNC_HANDLE_NAMES(lanpower_get, "GLNP");
638SNC_HANDLE_NAMES(lanpower_set, "LNPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100639
Mattia Dongili044847e2007-07-16 02:34:33 +0900640SNC_HANDLE_NAMES(lidstate_get, "GLID");
641
642SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
643SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
644
645SNC_HANDLE_NAMES(gainbass_get, "GMGB");
646SNC_HANDLE_NAMES(gainbass_set, "CMGB");
647
malattia@linux.it59b19102007-04-09 10:19:04 +0200648SNC_HANDLE_NAMES(PID_get, "GPID");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100649
malattia@linux.it59b19102007-04-09 10:19:04 +0200650SNC_HANDLE_NAMES(CTR_get, "GCTR");
651SNC_HANDLE_NAMES(CTR_set, "SCTR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100652
malattia@linux.it59b19102007-04-09 10:19:04 +0200653SNC_HANDLE_NAMES(PCR_get, "GPCR");
654SNC_HANDLE_NAMES(PCR_set, "SPCR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100655
malattia@linux.it59b19102007-04-09 10:19:04 +0200656SNC_HANDLE_NAMES(CMI_get, "GCMI");
657SNC_HANDLE_NAMES(CMI_set, "SCMI");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100658
malattia@linux.it59b19102007-04-09 10:19:04 +0200659static struct sony_nc_value sony_nc_values[] = {
660 SNC_HANDLE(brightness_default, snc_brightness_def_get,
Mattia Dongili156c2212007-02-12 22:01:07 +0100661 snc_brightness_def_set, brightness_default_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200662 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
663 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
664 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100665 boolean_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200666 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100667 boolean_validate, 1),
Mattia Dongili044847e2007-07-16 02:34:33 +0900668 SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
669 boolean_validate, 0),
670 SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
671 boolean_validate, 0),
672 SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
673 boolean_validate, 0),
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100674 /* unknown methods */
malattia@linux.it59b19102007-04-09 10:19:04 +0200675 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
676 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
677 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
678 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
679 SNC_HANDLE_NULL
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100680};
681
malattia@linux.it59b19102007-04-09 10:19:04 +0200682static acpi_handle sony_nc_acpi_handle;
683static struct acpi_device *sony_nc_acpi_device = NULL;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100684
Mattia Dongilid78865c2007-02-07 20:01:56 +0100685/*
686 * acpi_evaluate_object wrappers
687 */
Stelian Pop7f09c432007-01-13 23:04:31 +0100688static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
689{
690 struct acpi_buffer output;
691 union acpi_object out_obj;
692 acpi_status status;
693
694 output.length = sizeof(out_obj);
695 output.pointer = &out_obj;
696
697 status = acpi_evaluate_object(handle, name, NULL, &output);
698 if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
699 *result = out_obj.integer.value;
700 return 0;
701 }
702
Mattia Dongilid6697932011-02-19 11:52:28 +0900703 pr_warn(DRV_PFX "acpi_callreadfunc failed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100704
705 return -1;
706}
707
708static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
709 int *result)
710{
711 struct acpi_object_list params;
712 union acpi_object in_obj;
713 struct acpi_buffer output;
714 union acpi_object out_obj;
715 acpi_status status;
716
717 params.count = 1;
718 params.pointer = &in_obj;
719 in_obj.type = ACPI_TYPE_INTEGER;
720 in_obj.integer.value = value;
721
722 output.length = sizeof(out_obj);
723 output.pointer = &out_obj;
724
725 status = acpi_evaluate_object(handle, name, &params, &output);
726 if (status == AE_OK) {
727 if (result != NULL) {
728 if (out_obj.type != ACPI_TYPE_INTEGER) {
Mattia Dongilid6697932011-02-19 11:52:28 +0900729 pr_warn(DRV_PFX "acpi_evaluate_object bad "
Stelian Pop7f09c432007-01-13 23:04:31 +0100730 "return type\n");
731 return -1;
732 }
733 *result = out_obj.integer.value;
734 }
735 return 0;
736 }
737
Mattia Dongilid6697932011-02-19 11:52:28 +0900738 pr_warn(DRV_PFX "acpi_evaluate_object failed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100739
740 return -1;
741}
742
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900743struct sony_nc_handles {
744 u16 cap[0x10];
745 struct device_attribute devattr;
746};
747
Dan Carpenterf11113b2011-02-26 15:54:27 +0300748static struct sony_nc_handles *handles;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900749
750static ssize_t sony_nc_handles_show(struct device *dev,
751 struct device_attribute *attr, char *buffer)
752{
753 ssize_t len = 0;
754 int i;
755
756 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
757 len += snprintf(buffer + len, PAGE_SIZE - len, "0x%.4x ",
758 handles->cap[i]);
759 }
760 len += snprintf(buffer + len, PAGE_SIZE - len, "\n");
761
762 return len;
763}
764
765static int sony_nc_handles_setup(struct platform_device *pd)
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900766{
767 int i;
768 int result;
769
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900770 handles = kzalloc(sizeof(*handles), GFP_KERNEL);
Dan Carpenter31f00752011-02-26 15:55:24 +0300771 if (!handles)
772 return -ENOMEM;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900773
774 sysfs_attr_init(&handles->devattr.attr);
775 handles->devattr.attr.name = "handles";
776 handles->devattr.attr.mode = S_IRUGO;
777 handles->devattr.show = sony_nc_handles_show;
778
779 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
780 if (!acpi_callsetfunc(sony_nc_acpi_handle,
781 "SN00", i + 0x20, &result)) {
782 dprintk("caching handle 0x%.4x (offset: 0x%.2x)\n",
783 result, i);
784 handles->cap[i] = result;
Mattia Dongili56e6e712011-02-19 11:52:25 +0900785 }
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900786 }
787
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900788 /* allow reading capabilities via sysfs */
789 if (device_create_file(&pd->dev, &handles->devattr)) {
790 kfree(handles);
791 handles = NULL;
792 return -1;
793 }
794
795 return 0;
796}
797
798static int sony_nc_handles_cleanup(struct platform_device *pd)
799{
800 if (handles) {
801 device_remove_file(&pd->dev, &handles->devattr);
802 kfree(handles);
803 handles = NULL;
804 }
805 return 0;
806}
807
808static int sony_find_snc_handle(int handle)
809{
810 int i;
811 for (i = 0; i < 0x10; i++) {
812 if (handles->cap[i] == handle) {
813 dprintk("found handle 0x%.4x (offset: 0x%.2x)\n",
814 handle, i);
815 return i;
816 }
817 }
Mattia Dongili56e6e712011-02-19 11:52:25 +0900818 dprintk("handle 0x%.4x not found\n", handle);
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900819 return -1;
820}
821
822static int sony_call_snc_handle(int handle, int argument, int *result)
823{
Mattia Dongili56e6e712011-02-19 11:52:25 +0900824 int ret = 0;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900825 int offset = sony_find_snc_handle(handle);
826
827 if (offset < 0)
828 return -1;
829
Mattia Dongili56e6e712011-02-19 11:52:25 +0900830 ret = acpi_callsetfunc(sony_nc_acpi_handle, "SN07", offset | argument,
831 result);
832 dprintk("called SN07 with 0x%.4x (result: 0x%.4x)\n", offset | argument,
833 *result);
834 return ret;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900835}
836
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100837/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200838 * sony_nc_values input/output validate functions
Mattia Dongili156c2212007-02-12 22:01:07 +0100839 */
840
841/* brightness_default_validate:
842 *
843 * manipulate input output values to keep consistency with the
844 * backlight framework for which brightness values are 0-based.
845 */
846static int brightness_default_validate(const int direction, const int value)
847{
848 switch (direction) {
849 case SNC_VALIDATE_OUT:
850 return value - 1;
851 case SNC_VALIDATE_IN:
852 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
853 return value + 1;
854 }
855 return -EINVAL;
856}
857
858/* boolean_validate:
859 *
860 * on input validate boolean values 0/1, on output just pass the
861 * received value.
862 */
863static int boolean_validate(const int direction, const int value)
864{
865 if (direction == SNC_VALIDATE_IN) {
866 if (value != 0 && value != 1)
867 return -EINVAL;
868 }
869 return value;
870}
871
872/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200873 * Sysfs show/store common to all sony_nc_values
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100874 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200875static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
Len Browna02d1c12007-02-07 15:34:02 -0500876 char *buffer)
Stelian Pop7f09c432007-01-13 23:04:31 +0100877{
Stelian Pop7f09c432007-01-13 23:04:31 +0100878 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200879 struct sony_nc_value *item =
880 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100881
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100882 if (!*item->acpiget)
Stelian Pop7f09c432007-01-13 23:04:31 +0100883 return -EIO;
884
malattia@linux.it59b19102007-04-09 10:19:04 +0200885 if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100886 return -EIO;
887
Mattia Dongili156c2212007-02-12 22:01:07 +0100888 if (item->validate)
889 value = item->validate(SNC_VALIDATE_OUT, value);
890
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100891 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
Stelian Pop7f09c432007-01-13 23:04:31 +0100892}
893
malattia@linux.it59b19102007-04-09 10:19:04 +0200894static ssize_t sony_nc_sysfs_store(struct device *dev,
Len Browna02d1c12007-02-07 15:34:02 -0500895 struct device_attribute *attr,
896 const char *buffer, size_t count)
Stelian Pop7f09c432007-01-13 23:04:31 +0100897{
Stelian Pop7f09c432007-01-13 23:04:31 +0100898 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200899 struct sony_nc_value *item =
900 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100901
902 if (!item->acpiset)
903 return -EIO;
904
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100905 if (count > 31)
906 return -EINVAL;
907
908 value = simple_strtoul(buffer, NULL, 10);
Stelian Pop7f09c432007-01-13 23:04:31 +0100909
Mattia Dongili156c2212007-02-12 22:01:07 +0100910 if (item->validate)
911 value = item->validate(SNC_VALIDATE_IN, value);
912
913 if (value < 0)
914 return value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100915
malattia@linux.it59b19102007-04-09 10:19:04 +0200916 if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100917 return -EIO;
Andrew Morton3f4f4612007-01-13 23:04:32 +0100918 item->value = value;
919 item->valid = 1;
Stelian Pop7f09c432007-01-13 23:04:31 +0100920 return count;
921}
922
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100923
Mattia Dongilid78865c2007-02-07 20:01:56 +0100924/*
925 * Backlight device
926 */
927static int sony_backlight_update_status(struct backlight_device *bd)
Andrew Morton3f4f4612007-01-13 23:04:32 +0100928{
malattia@linux.it59b19102007-04-09 10:19:04 +0200929 return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
Richard Purdie321709c2007-02-10 15:04:08 +0000930 bd->props.brightness + 1, NULL);
Andrew Morton3f4f4612007-01-13 23:04:32 +0100931}
932
Mattia Dongilid78865c2007-02-07 20:01:56 +0100933static int sony_backlight_get_brightness(struct backlight_device *bd)
934{
935 int value;
936
malattia@linux.it59b19102007-04-09 10:19:04 +0200937 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
Mattia Dongilid78865c2007-02-07 20:01:56 +0100938 return 0;
939 /* brightness levels are 1-based, while backlight ones are 0-based */
940 return value - 1;
941}
942
Mattia Dongili7751ab82011-02-19 11:52:32 +0900943static int sony_nc_get_brightness_ng(struct backlight_device *bd)
944{
945 int result;
946 int *handle = (int *)bl_get_data(bd);
947
948 sony_call_snc_handle(*handle, 0x0200, &result);
949
950 return result & 0xff;
951}
952
953static int sony_nc_update_status_ng(struct backlight_device *bd)
954{
955 int value, result;
956 int *handle = (int *)bl_get_data(bd);
957
958 value = bd->props.brightness;
959 sony_call_snc_handle(*handle, 0x0100 | (value << 16), &result);
960
961 return sony_nc_get_brightness_ng(bd);
962}
963
Lionel Debrouxacc24722010-11-16 14:14:02 +0100964static const struct backlight_ops sony_backlight_ops = {
Mattia Dongili7751ab82011-02-19 11:52:32 +0900965 .options = BL_CORE_SUSPENDRESUME,
Len Browna02d1c12007-02-07 15:34:02 -0500966 .update_status = sony_backlight_update_status,
967 .get_brightness = sony_backlight_get_brightness,
Mattia Dongilid78865c2007-02-07 20:01:56 +0100968};
Mattia Dongili7751ab82011-02-19 11:52:32 +0900969static const struct backlight_ops sony_backlight_ng_ops = {
970 .options = BL_CORE_SUSPENDRESUME,
971 .update_status = sony_nc_update_status_ng,
972 .get_brightness = sony_nc_get_brightness_ng,
973};
974static int backlight_ng_handle;
975static struct backlight_device *sony_backlight_device;
Mattia Dongilid78865c2007-02-07 20:01:56 +0100976
977/*
Mattia Dongili6315fd12007-07-16 02:34:35 +0900978 * New SNC-only Vaios event mapping to driver known keys
979 */
980struct sony_nc_event {
981 u8 data;
982 u8 event;
983};
984
Matthew Garrett45c79422009-03-26 21:58:16 +0900985static struct sony_nc_event sony_100_events[] = {
Matthew Garrett9b578962009-03-26 21:58:14 +0900986 { 0x90, SONYPI_EVENT_PKEY_P1 },
987 { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthias Welwarsky6479efb2009-04-01 22:10:45 +0900988 { 0x91, SONYPI_EVENT_PKEY_P2 },
Matthew Garrett9b578962009-03-26 21:58:14 +0900989 { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +0900990 { 0x81, SONYPI_EVENT_FNKEY_F1 },
991 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
Anton Veretenenkof5acf5e2009-03-26 22:44:26 +0900992 { 0x82, SONYPI_EVENT_FNKEY_F2 },
993 { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
994 { 0x83, SONYPI_EVENT_FNKEY_F3 },
995 { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
996 { 0x84, SONYPI_EVENT_FNKEY_F4 },
997 { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +0900998 { 0x85, SONYPI_EVENT_FNKEY_F5 },
999 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
1000 { 0x86, SONYPI_EVENT_FNKEY_F6 },
1001 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
1002 { 0x87, SONYPI_EVENT_FNKEY_F7 },
1003 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +09001004 { 0x89, SONYPI_EVENT_FNKEY_F9 },
1005 { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001006 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
1007 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
1008 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
1009 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +09001010 { 0x9d, SONYPI_EVENT_ZOOM_PRESSED },
1011 { 0x1d, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +09001012 { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
1013 { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili4f924ba2009-12-17 00:08:33 +09001014 { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
1015 { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +09001016 { 0xa4, SONYPI_EVENT_CD_EJECT_PRESSED },
1017 { 0x24, SONYPI_EVENT_ANYBUTTON_RELEASED },
1018 { 0xa5, SONYPI_EVENT_VENDOR_PRESSED },
1019 { 0x25, SONYPI_EVENT_ANYBUTTON_RELEASED },
1020 { 0xa6, SONYPI_EVENT_HELP_PRESSED },
1021 { 0x26, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001022 { 0, 0 },
1023};
1024
Matthew Garrett45c79422009-03-26 21:58:16 +09001025static struct sony_nc_event sony_127_events[] = {
1026 { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
1027 { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
1028 { 0x82, SONYPI_EVENT_PKEY_P1 },
1029 { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
1030 { 0x83, SONYPI_EVENT_PKEY_P2 },
1031 { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
1032 { 0x84, SONYPI_EVENT_PKEY_P3 },
1033 { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
1034 { 0x85, SONYPI_EVENT_PKEY_P4 },
1035 { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
1036 { 0x86, SONYPI_EVENT_PKEY_P5 },
1037 { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett45c79422009-03-26 21:58:16 +09001038 { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
1039 { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
1040 { 0, 0 },
1041};
1042
Mattia Dongili6315fd12007-07-16 02:34:35 +09001043/*
Mattia Dongilid78865c2007-02-07 20:01:56 +01001044 * ACPI callbacks
1045 */
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001046static void sony_nc_notify(struct acpi_device *device, u32 event)
Stelian Pop7f09c432007-01-13 23:04:31 +01001047{
Mattia Dongili6315fd12007-07-16 02:34:35 +09001048 u32 ev = event;
Mattia Dongili6315fd12007-07-16 02:34:35 +09001049
Matthew Garrett9b578962009-03-26 21:58:14 +09001050 if (ev >= 0x90) {
1051 /* New-style event */
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001052 int result;
Matthew Garrett45c79422009-03-26 21:58:16 +09001053 int key_handle = 0;
Matthew Garrett9b578962009-03-26 21:58:14 +09001054 ev -= 0x90;
Mattia Dongili6315fd12007-07-16 02:34:35 +09001055
Matthew Garrett45c79422009-03-26 21:58:16 +09001056 if (sony_find_snc_handle(0x100) == ev)
1057 key_handle = 0x100;
1058 if (sony_find_snc_handle(0x127) == ev)
1059 key_handle = 0x127;
Matthew Garrett9b578962009-03-26 21:58:14 +09001060
Matthias Welwarsky6479efb2009-04-01 22:10:45 +09001061 if (key_handle) {
Matthew Garrett45c79422009-03-26 21:58:16 +09001062 struct sony_nc_event *key_event;
1063
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001064 if (sony_call_snc_handle(key_handle, 0x200, &result)) {
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001065 dprintk("sony_nc_notify, unable to decode"
Matthew Garrett45c79422009-03-26 21:58:16 +09001066 " event 0x%.2x 0x%.2x\n", key_handle,
1067 ev);
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001068 /* restore the original event */
1069 ev = event;
1070 } else {
Matthew Garrett9b578962009-03-26 21:58:14 +09001071 ev = result & 0xFF;
1072
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001073 if (key_handle == 0x100)
1074 key_event = sony_100_events;
1075 else
1076 key_event = sony_127_events;
Matthew Garrett45c79422009-03-26 21:58:16 +09001077
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001078 for (; key_event->data; key_event++) {
1079 if (key_event->data == ev) {
1080 ev = key_event->event;
1081 break;
1082 }
Matthew Garrett9b578962009-03-26 21:58:14 +09001083 }
Mattia Dongili6315fd12007-07-16 02:34:35 +09001084
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001085 if (!key_event->data)
Mattia Dongilid6697932011-02-19 11:52:28 +09001086 pr_info(DRV_PFX
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001087 "Unknown event: 0x%x 0x%x\n",
1088 key_handle,
1089 ev);
1090 else
1091 sony_laptop_report_input_event(ev);
Matthew Garrett45c79422009-03-26 21:58:16 +09001092 }
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001093 } else if (sony_find_snc_handle(sony_rfkill_handle) == ev) {
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001094 sony_nc_rfkill_update();
1095 return;
Matthew Garrett9b578962009-03-26 21:58:14 +09001096 }
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001097 } else
1098 sony_laptop_report_input_event(ev);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001099
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001100 dprintk("sony_nc_notify, event: 0x%.2x\n", ev);
Len Brown14e04fb2007-08-23 15:20:26 -04001101 acpi_bus_generate_proc_event(sony_nc_acpi_device, 1, ev);
Stelian Pop7f09c432007-01-13 23:04:31 +01001102}
1103
1104static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
1105 void *context, void **return_value)
1106{
Lin Ming30823732008-12-16 16:59:35 +08001107 struct acpi_device_info *info;
Stelian Pop7f09c432007-01-13 23:04:31 +01001108
Bob Moore15b8dd52009-06-29 13:39:29 +08001109 if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001110 pr_warn(DRV_PFX "method: name: %4.4s, args %X\n",
Lin Ming30823732008-12-16 16:59:35 +08001111 (char *)&info->name, info->param_count);
1112
Bob Moore15b8dd52009-06-29 13:39:29 +08001113 kfree(info);
Lin Ming30823732008-12-16 16:59:35 +08001114 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001115
1116 return AE_OK;
1117}
1118
Mattia Dongilid78865c2007-02-07 20:01:56 +01001119/*
1120 * ACPI device
1121 */
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001122static int sony_nc_function_setup(struct acpi_device *device)
1123{
1124 int result;
1125
1126 /* Enable all events */
1127 acpi_callsetfunc(sony_nc_acpi_handle, "SN02", 0xffff, &result);
1128
1129 /* Setup hotkeys */
1130 sony_call_snc_handle(0x0100, 0, &result);
1131 sony_call_snc_handle(0x0101, 0, &result);
1132 sony_call_snc_handle(0x0102, 0x100, &result);
Almer S. Tigelaar560e84a2009-04-12 11:26:27 +00001133 sony_call_snc_handle(0x0127, 0, &result);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001134
1135 return 0;
1136}
1137
malattia@linux.it59b19102007-04-09 10:19:04 +02001138static int sony_nc_resume(struct acpi_device *device)
Mattia Dongilid78865c2007-02-07 20:01:56 +01001139{
malattia@linux.it59b19102007-04-09 10:19:04 +02001140 struct sony_nc_value *item;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001141 acpi_handle handle;
Mattia Dongilid78865c2007-02-07 20:01:56 +01001142
malattia@linux.it59b19102007-04-09 10:19:04 +02001143 for (item = sony_nc_values; item->name; item++) {
Mattia Dongilid78865c2007-02-07 20:01:56 +01001144 int ret;
1145
1146 if (!item->valid)
1147 continue;
malattia@linux.it59b19102007-04-09 10:19:04 +02001148 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
Len Browna02d1c12007-02-07 15:34:02 -05001149 item->value, NULL);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001150 if (ret < 0) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001151 pr_err(DRV_PFX "%s: %d\n", __func__, ret);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001152 break;
1153 }
1154 }
Mattia Dongili6315fd12007-07-16 02:34:35 +09001155
Matthew Garrett82734bf2009-03-26 21:58:13 +09001156 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1157 &handle))) {
1158 if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
1159 dprintk("ECON Method failed\n");
1160 }
1161
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001162 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1163 &handle))) {
1164 dprintk("Doing SNC setup\n");
1165 sony_nc_function_setup(device);
1166 }
1167
Alan Jenkinsa0d97d62009-09-25 10:18:21 +01001168 /* re-read rfkill state */
1169 sony_nc_rfkill_update();
1170
Mattia Dongilid78865c2007-02-07 20:01:56 +01001171 return 0;
1172}
1173
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001174static void sony_nc_rfkill_cleanup(void)
1175{
1176 int i;
1177
Johannes Berg19d337d2009-06-02 13:01:37 +02001178 for (i = 0; i < N_SONY_RFKILL; i++) {
1179 if (sony_rfkill_devices[i]) {
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001180 rfkill_unregister(sony_rfkill_devices[i]);
Johannes Berg19d337d2009-06-02 13:01:37 +02001181 rfkill_destroy(sony_rfkill_devices[i]);
1182 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001183 }
1184}
1185
Johannes Berg19d337d2009-06-02 13:01:37 +02001186static int sony_nc_rfkill_set(void *data, bool blocked)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001187{
1188 int result;
1189 int argument = sony_rfkill_address[(long) data] + 0x100;
1190
Johannes Berg19d337d2009-06-02 13:01:37 +02001191 if (!blocked)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001192 argument |= 0xff0000;
1193
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001194 return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001195}
1196
Johannes Berg19d337d2009-06-02 13:01:37 +02001197static const struct rfkill_ops sony_rfkill_ops = {
1198 .set_block = sony_nc_rfkill_set,
1199};
1200
1201static int sony_nc_setup_rfkill(struct acpi_device *device,
1202 enum sony_nc_rfkill nc_type)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001203{
1204 int err = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001205 struct rfkill *rfk;
1206 enum rfkill_type type;
1207 const char *name;
Alan Jenkins50fab072009-09-24 20:15:24 +01001208 int result;
1209 bool hwblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001210
Johannes Berg19d337d2009-06-02 13:01:37 +02001211 switch (nc_type) {
1212 case SONY_WIFI:
1213 type = RFKILL_TYPE_WLAN;
1214 name = "sony-wifi";
1215 break;
1216 case SONY_BLUETOOTH:
1217 type = RFKILL_TYPE_BLUETOOTH;
1218 name = "sony-bluetooth";
1219 break;
1220 case SONY_WWAN:
1221 type = RFKILL_TYPE_WWAN;
1222 name = "sony-wwan";
1223 break;
1224 case SONY_WIMAX:
1225 type = RFKILL_TYPE_WIMAX;
1226 name = "sony-wimax";
1227 break;
1228 default:
1229 return -EINVAL;
Mattia Dongili53005a02009-04-12 11:26:31 +00001230 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001231
Johannes Berg19d337d2009-06-02 13:01:37 +02001232 rfk = rfkill_alloc(name, &device->dev, type,
1233 &sony_rfkill_ops, (void *)nc_type);
1234 if (!rfk)
1235 return -ENOMEM;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001236
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001237 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
Alan Jenkins50fab072009-09-24 20:15:24 +01001238 hwblock = !(result & 0x1);
1239 rfkill_set_hw_state(rfk, hwblock);
1240
Johannes Berg19d337d2009-06-02 13:01:37 +02001241 err = rfkill_register(rfk);
1242 if (err) {
1243 rfkill_destroy(rfk);
1244 return err;
Mattia Dongili53005a02009-04-12 11:26:31 +00001245 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001246 sony_rfkill_devices[nc_type] = rfk;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001247 return err;
1248}
1249
Randy Dunlapa46a7802011-01-08 19:56:44 -08001250static void sony_nc_rfkill_update(void)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001251{
Johannes Berg19d337d2009-06-02 13:01:37 +02001252 enum sony_nc_rfkill i;
1253 int result;
1254 bool hwblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001255
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001256 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001257 hwblock = !(result & 0x1);
1258
1259 for (i = 0; i < N_SONY_RFKILL; i++) {
1260 int argument = sony_rfkill_address[i];
1261
1262 if (!sony_rfkill_devices[i])
1263 continue;
1264
1265 if (hwblock) {
Johannes Berge1f8a192009-06-11 12:08:15 +02001266 if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
1267 /* we already know we're blocked */
1268 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001269 continue;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001270 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001271
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001272 sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001273 rfkill_set_states(sony_rfkill_devices[i],
1274 !(result & 0xf), false);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001275 }
1276}
1277
Mattia Dongili528809c2009-12-17 00:08:36 +09001278static void sony_nc_rfkill_setup(struct acpi_device *device)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001279{
Mattia Dongili528809c2009-12-17 00:08:36 +09001280 int offset;
1281 u8 dev_code, i;
1282 acpi_status status;
1283 struct acpi_object_list params;
1284 union acpi_object in_obj;
1285 union acpi_object *device_enum;
1286 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001287
Mattia Dongili528809c2009-12-17 00:08:36 +09001288 offset = sony_find_snc_handle(0x124);
1289 if (offset == -1) {
1290 offset = sony_find_snc_handle(0x135);
1291 if (offset == -1)
1292 return;
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001293 else
1294 sony_rfkill_handle = 0x135;
1295 } else
1296 sony_rfkill_handle = 0x124;
Mattia Dongili528809c2009-12-17 00:08:36 +09001297 dprintk("Found rkfill handle: 0x%.4x\n", sony_rfkill_handle);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001298
Mattia Dongili528809c2009-12-17 00:08:36 +09001299 /* need to read the whole buffer returned by the acpi call to SN06
1300 * here otherwise we may miss some features
1301 */
1302 params.count = 1;
1303 params.pointer = &in_obj;
1304 in_obj.type = ACPI_TYPE_INTEGER;
1305 in_obj.integer.value = offset;
1306 status = acpi_evaluate_object(sony_nc_acpi_handle, "SN06", &params,
1307 &buffer);
1308 if (ACPI_FAILURE(status)) {
1309 dprintk("Radio device enumeration failed\n");
1310 return;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001311 }
1312
Mattia Dongili528809c2009-12-17 00:08:36 +09001313 device_enum = (union acpi_object *) buffer.pointer;
Dan Carpenter200140b2011-02-27 17:13:25 +03001314 if (!device_enum) {
1315 pr_err(DRV_PFX "No SN06 return object.");
1316 goto out_no_enum;
1317 }
1318 if (device_enum->type != ACPI_TYPE_BUFFER) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001319 pr_err(DRV_PFX "Invalid SN06 return object 0x%.2x\n",
1320 device_enum->type);
Mattia Dongili528809c2009-12-17 00:08:36 +09001321 goto out_no_enum;
1322 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001323
Mattia Dongili528809c2009-12-17 00:08:36 +09001324 /* the buffer is filled with magic numbers describing the devices
1325 * available, 0xff terminates the enumeration
1326 */
Dmitry Torokhovc14973f2010-01-10 00:15:44 -08001327 for (i = 0; i < device_enum->buffer.length; i++) {
1328
1329 dev_code = *(device_enum->buffer.pointer + i);
1330 if (dev_code == 0xff)
1331 break;
1332
Mattia Dongili528809c2009-12-17 00:08:36 +09001333 dprintk("Radio devices, looking at 0x%.2x\n", dev_code);
1334
1335 if (dev_code == 0 && !sony_rfkill_devices[SONY_WIFI])
1336 sony_nc_setup_rfkill(device, SONY_WIFI);
1337
1338 if (dev_code == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
1339 sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
1340
1341 if ((0xf0 & dev_code) == 0x20 &&
1342 !sony_rfkill_devices[SONY_WWAN])
1343 sony_nc_setup_rfkill(device, SONY_WWAN);
1344
1345 if (dev_code == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
1346 sony_nc_setup_rfkill(device, SONY_WIMAX);
1347 }
1348
1349out_no_enum:
1350 kfree(buffer.pointer);
1351 return;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001352}
1353
Mattia Dongilibf155712011-02-19 11:52:31 +09001354/* Keyboard backlight feature */
1355#define KBDBL_HANDLER 0x137
1356#define KBDBL_PRESENT 0xB00
1357#define SET_MODE 0xC00
1358#define SET_TIMEOUT 0xE00
1359
1360struct kbd_backlight {
1361 int mode;
1362 int timeout;
1363 struct device_attribute mode_attr;
1364 struct device_attribute timeout_attr;
1365};
1366
Dan Carpenterf11113b2011-02-26 15:54:27 +03001367static struct kbd_backlight *kbdbl_handle;
Mattia Dongilibf155712011-02-19 11:52:31 +09001368
1369static ssize_t __sony_nc_kbd_backlight_mode_set(u8 value)
1370{
1371 int result;
1372
1373 if (value > 1)
1374 return -EINVAL;
1375
1376 if (sony_call_snc_handle(KBDBL_HANDLER,
1377 (value << 0x10) | SET_MODE, &result))
1378 return -EIO;
1379
1380 kbdbl_handle->mode = value;
1381
1382 return 0;
1383}
1384
1385static ssize_t sony_nc_kbd_backlight_mode_store(struct device *dev,
1386 struct device_attribute *attr,
1387 const char *buffer, size_t count)
1388{
1389 int ret = 0;
1390 unsigned long value;
1391
1392 if (count > 31)
1393 return -EINVAL;
1394
1395 if (strict_strtoul(buffer, 10, &value))
1396 return -EINVAL;
1397
1398 ret = __sony_nc_kbd_backlight_mode_set(value);
1399 if (ret < 0)
1400 return ret;
1401
1402 return count;
1403}
1404
1405static ssize_t sony_nc_kbd_backlight_mode_show(struct device *dev,
1406 struct device_attribute *attr, char *buffer)
1407{
1408 ssize_t count = 0;
1409 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_handle->mode);
1410 return count;
1411}
1412
1413static int __sony_nc_kbd_backlight_timeout_set(u8 value)
1414{
1415 int result;
1416
1417 if (value > 3)
1418 return -EINVAL;
1419
1420 if (sony_call_snc_handle(KBDBL_HANDLER,
1421 (value << 0x10) | SET_TIMEOUT, &result))
1422 return -EIO;
1423
1424 kbdbl_handle->timeout = value;
1425
1426 return 0;
1427}
1428
1429static ssize_t sony_nc_kbd_backlight_timeout_store(struct device *dev,
1430 struct device_attribute *attr,
1431 const char *buffer, size_t count)
1432{
1433 int ret = 0;
1434 unsigned long value;
1435
1436 if (count > 31)
1437 return -EINVAL;
1438
1439 if (strict_strtoul(buffer, 10, &value))
1440 return -EINVAL;
1441
1442 ret = __sony_nc_kbd_backlight_timeout_set(value);
1443 if (ret < 0)
1444 return ret;
1445
1446 return count;
1447}
1448
1449static ssize_t sony_nc_kbd_backlight_timeout_show(struct device *dev,
1450 struct device_attribute *attr, char *buffer)
1451{
1452 ssize_t count = 0;
1453 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_handle->timeout);
1454 return count;
1455}
1456
1457static int sony_nc_kbd_backlight_setup(struct platform_device *pd)
1458{
1459 int result;
1460
1461 if (sony_call_snc_handle(0x137, KBDBL_PRESENT, &result))
1462 return 0;
1463 if (!(result & 0x02))
1464 return 0;
1465
1466 kbdbl_handle = kzalloc(sizeof(*kbdbl_handle), GFP_KERNEL);
Dan Carpenter31f00752011-02-26 15:55:24 +03001467 if (!kbdbl_handle)
1468 return -ENOMEM;
Mattia Dongilibf155712011-02-19 11:52:31 +09001469
1470 sysfs_attr_init(&kbdbl_handle->mode_attr.attr);
1471 kbdbl_handle->mode_attr.attr.name = "kbd_backlight";
1472 kbdbl_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
1473 kbdbl_handle->mode_attr.show = sony_nc_kbd_backlight_mode_show;
1474 kbdbl_handle->mode_attr.store = sony_nc_kbd_backlight_mode_store;
1475
1476 sysfs_attr_init(&kbdbl_handle->timeout_attr.attr);
1477 kbdbl_handle->timeout_attr.attr.name = "kbd_backlight_timeout";
1478 kbdbl_handle->timeout_attr.attr.mode = S_IRUGO | S_IWUSR;
1479 kbdbl_handle->timeout_attr.show = sony_nc_kbd_backlight_timeout_show;
1480 kbdbl_handle->timeout_attr.store = sony_nc_kbd_backlight_timeout_store;
1481
1482 if (device_create_file(&pd->dev, &kbdbl_handle->mode_attr))
1483 goto outkzalloc;
1484
1485 if (device_create_file(&pd->dev, &kbdbl_handle->timeout_attr))
1486 goto outmode;
1487
1488 __sony_nc_kbd_backlight_mode_set(kbd_backlight);
1489 __sony_nc_kbd_backlight_timeout_set(kbd_backlight_timeout);
1490
1491 return 0;
1492
1493outmode:
1494 device_remove_file(&pd->dev, &kbdbl_handle->mode_attr);
1495outkzalloc:
1496 kfree(kbdbl_handle);
1497 kbdbl_handle = NULL;
1498 return -1;
1499}
1500
1501static int sony_nc_kbd_backlight_cleanup(struct platform_device *pd)
1502{
1503 if (kbdbl_handle) {
1504 device_remove_file(&pd->dev, &kbdbl_handle->mode_attr);
1505 device_remove_file(&pd->dev, &kbdbl_handle->timeout_attr);
1506 kfree(kbdbl_handle);
1507 }
1508 return 0;
1509}
1510
Mattia Dongili7751ab82011-02-19 11:52:32 +09001511static void sony_nc_backlight_setup(void)
1512{
1513 acpi_handle unused;
1514 int max_brightness = 0;
1515 const struct backlight_ops *ops = NULL;
1516 struct backlight_properties props;
1517
1518 if (sony_find_snc_handle(0x12f) != -1) {
1519 backlight_ng_handle = 0x12f;
1520 ops = &sony_backlight_ng_ops;
1521 max_brightness = 0xff;
1522
1523 } else if (sony_find_snc_handle(0x137) != -1) {
1524 backlight_ng_handle = 0x137;
1525 ops = &sony_backlight_ng_ops;
1526 max_brightness = 0xff;
1527
1528 } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
1529 &unused))) {
1530 ops = &sony_backlight_ops;
1531 max_brightness = SONY_MAX_BRIGHTNESS - 1;
1532
1533 } else
1534 return;
1535
1536 memset(&props, 0, sizeof(struct backlight_properties));
1537 props.type = BACKLIGHT_PLATFORM;
1538 props.max_brightness = max_brightness;
1539 sony_backlight_device = backlight_device_register("sony", NULL,
1540 &backlight_ng_handle,
1541 ops, &props);
1542
1543 if (IS_ERR(sony_backlight_device)) {
1544 pr_warning(DRV_PFX "unable to register backlight device\n");
1545 sony_backlight_device = NULL;
1546 } else
1547 sony_backlight_device->props.brightness =
1548 ops->get_brightness(sony_backlight_device);
1549}
1550
1551static void sony_nc_backlight_cleanup(void)
1552{
1553 if (sony_backlight_device)
1554 backlight_device_unregister(sony_backlight_device);
1555}
1556
malattia@linux.it59b19102007-04-09 10:19:04 +02001557static int sony_nc_add(struct acpi_device *device)
Stelian Pop7f09c432007-01-13 23:04:31 +01001558{
1559 acpi_status status;
Andrew Morton8607c672007-03-06 02:29:42 -08001560 int result = 0;
Alessandro Guido50f62af2007-01-13 23:04:34 +01001561 acpi_handle handle;
malattia@linux.it56b87562007-04-09 10:19:05 +02001562 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01001563
Mattia Dongilid6697932011-02-19 11:52:28 +09001564 pr_info(DRV_PFX "%s v%s.\n", SONY_NC_DRIVER_NAME,
1565 SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.itf6119b02007-04-09 19:31:06 +02001566
malattia@linux.it59b19102007-04-09 10:19:04 +02001567 sony_nc_acpi_device = device;
malattia@linux.it33a04452007-04-09 19:26:03 +02001568 strcpy(acpi_device_class(device), "sony/hotkey");
Stelian Popc5611622007-01-13 23:04:37 +01001569
malattia@linux.it59b19102007-04-09 10:19:04 +02001570 sony_nc_acpi_handle = device->handle;
Stelian Pop7f09c432007-01-13 23:04:31 +01001571
Mattia Dongilib25b7322007-07-16 02:34:36 +09001572 /* read device status */
1573 result = acpi_bus_get_status(device);
1574 /* bail IFF the above call was successful and the device is not present */
1575 if (!result && !device->status.present) {
1576 dprintk("Device not present\n");
1577 result = -ENODEV;
1578 goto outwalk;
1579 }
1580
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001581 result = sony_pf_add();
1582 if (result)
1583 goto outpresent;
1584
Stelian Pop7f09c432007-01-13 23:04:31 +01001585 if (debug) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001586 status = acpi_walk_namespace(ACPI_TYPE_METHOD,
1587 sony_nc_acpi_handle, 1, sony_walk_callback,
1588 NULL, NULL, NULL);
Stelian Pop7f09c432007-01-13 23:04:31 +01001589 if (ACPI_FAILURE(status)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001590 pr_warn(DRV_PFX "unable to walk acpi resources\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001591 result = -ENODEV;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001592 goto outpresent;
Stelian Pop7f09c432007-01-13 23:04:31 +01001593 }
Stelian Popc5611622007-01-13 23:04:37 +01001594 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001595
Matthew Garrett82734bf2009-03-26 21:58:13 +09001596 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1597 &handle))) {
1598 if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
1599 dprintk("ECON Method failed\n");
1600 }
1601
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001602 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1603 &handle))) {
1604 dprintk("Doing SNC setup\n");
Dan Carpenter7227ded2011-02-26 15:54:57 +03001605 result = sony_nc_handles_setup(sony_pf_device);
1606 if (result)
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001607 goto outpresent;
Dan Carpenter7227ded2011-02-26 15:54:57 +03001608 result = sony_nc_kbd_backlight_setup(sony_pf_device);
1609 if (result)
Mattia Dongilibf155712011-02-19 11:52:31 +09001610 goto outsnc;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001611 sony_nc_function_setup(device);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001612 sony_nc_rfkill_setup(device);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001613 }
1614
malattia@linux.it1549ee62007-04-09 10:19:08 +02001615 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05001616 result = sony_laptop_setup_input(device);
malattia@linux.it1549ee62007-04-09 10:19:08 +02001617 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001618 pr_err(DRV_PFX "Unable to create input devices.\n");
Mattia Dongilibf155712011-02-19 11:52:31 +09001619 goto outkbdbacklight;
malattia@linux.it1549ee62007-04-09 10:19:08 +02001620 }
1621
Alessandro Guido38cfc142008-11-12 23:03:28 +01001622 if (acpi_video_backlight_support()) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001623 pr_info(DRV_PFX "brightness ignored, must be "
Thomas Renninger540b8bb2008-08-01 17:38:02 +02001624 "controlled by ACPI video driver\n");
Mattia Dongili7751ab82011-02-19 11:52:32 +09001625 } else {
1626 sony_nc_backlight_setup();
Alessandro Guido50f62af2007-01-13 23:04:34 +01001627 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001628
malattia@linux.it56b87562007-04-09 10:19:05 +02001629 /* create sony_pf sysfs attributes related to the SNC device */
1630 for (item = sony_nc_values; item->name; ++item) {
1631
1632 if (!debug && item->debug)
1633 continue;
1634
1635 /* find the available acpiget as described in the DSDT */
1636 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
1637 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1638 *item->acpiget,
1639 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02001640 dprintk("Found %s getter: %s\n",
1641 item->name, *item->acpiget);
malattia@linux.it56b87562007-04-09 10:19:05 +02001642 item->devattr.attr.mode |= S_IRUGO;
1643 break;
1644 }
1645 }
1646
1647 /* find the available acpiset as described in the DSDT */
1648 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
1649 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1650 *item->acpiset,
1651 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02001652 dprintk("Found %s setter: %s\n",
1653 item->name, *item->acpiset);
malattia@linux.it56b87562007-04-09 10:19:05 +02001654 item->devattr.attr.mode |= S_IWUSR;
1655 break;
1656 }
1657 }
1658
1659 if (item->devattr.attr.mode != 0) {
1660 result =
1661 device_create_file(&sony_pf_device->dev,
1662 &item->devattr);
1663 if (result)
1664 goto out_sysfs;
1665 }
1666 }
1667
Stelian Pop7f09c432007-01-13 23:04:31 +01001668 return 0;
1669
malattia@linux.it56b87562007-04-09 10:19:05 +02001670 out_sysfs:
1671 for (item = sony_nc_values; item->name; ++item) {
1672 device_remove_file(&sony_pf_device->dev, &item->devattr);
1673 }
Mattia Dongili7751ab82011-02-19 11:52:32 +09001674 sony_nc_backlight_cleanup();
Mattia Dongili7df03b82007-01-13 23:04:41 +01001675
malattia@linux.it1549ee62007-04-09 10:19:08 +02001676 sony_laptop_remove_input();
1677
Mattia Dongilibf155712011-02-19 11:52:31 +09001678 outkbdbacklight:
1679 sony_nc_kbd_backlight_cleanup(sony_pf_device);
1680
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001681 outsnc:
1682 sony_nc_handles_cleanup(sony_pf_device);
1683
1684 outpresent:
1685 sony_pf_remove();
1686
Len Browna02d1c12007-02-07 15:34:02 -05001687 outwalk:
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001688 sony_nc_rfkill_cleanup();
Stelian Pop7f09c432007-01-13 23:04:31 +01001689 return result;
1690}
1691
malattia@linux.it59b19102007-04-09 10:19:04 +02001692static int sony_nc_remove(struct acpi_device *device, int type)
Stelian Pop7f09c432007-01-13 23:04:31 +01001693{
malattia@linux.it56b87562007-04-09 10:19:05 +02001694 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01001695
Mattia Dongili7751ab82011-02-19 11:52:32 +09001696 sony_nc_backlight_cleanup();
Alessandro Guido50f62af2007-01-13 23:04:34 +01001697
malattia@linux.it59b19102007-04-09 10:19:04 +02001698 sony_nc_acpi_device = NULL;
Stelian Popc5611622007-01-13 23:04:37 +01001699
malattia@linux.it56b87562007-04-09 10:19:05 +02001700 for (item = sony_nc_values; item->name; ++item) {
1701 device_remove_file(&sony_pf_device->dev, &item->devattr);
1702 }
1703
Mattia Dongilibf155712011-02-19 11:52:31 +09001704 sony_nc_kbd_backlight_cleanup(sony_pf_device);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001705 sony_nc_handles_cleanup(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +02001706 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +02001707 sony_laptop_remove_input();
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001708 sony_nc_rfkill_cleanup();
malattia@linux.itf6119b02007-04-09 19:31:06 +02001709 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001710
1711 return 0;
1712}
1713
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001714static const struct acpi_device_id sony_device_ids[] = {
1715 {SONY_NC_HID, 0},
1716 {SONY_PIC_HID, 0},
1717 {"", 0},
1718};
1719MODULE_DEVICE_TABLE(acpi, sony_device_ids);
1720
1721static const struct acpi_device_id sony_nc_device_ids[] = {
1722 {SONY_NC_HID, 0},
1723 {"", 0},
1724};
1725
malattia@linux.it59b19102007-04-09 10:19:04 +02001726static struct acpi_driver sony_nc_driver = {
1727 .name = SONY_NC_DRIVER_NAME,
1728 .class = SONY_NC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001729 .ids = sony_nc_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02001730 .owner = THIS_MODULE,
Len Browna02d1c12007-02-07 15:34:02 -05001731 .ops = {
malattia@linux.it59b19102007-04-09 10:19:04 +02001732 .add = sony_nc_add,
1733 .remove = sony_nc_remove,
1734 .resume = sony_nc_resume,
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001735 .notify = sony_nc_notify,
Len Browna02d1c12007-02-07 15:34:02 -05001736 },
Andrew Morton3f4f4612007-01-13 23:04:32 +01001737};
1738
malattia@linux.it33a04452007-04-09 19:26:03 +02001739/*********** SPIC (SNY6001) Device ***********/
1740
1741#define SONYPI_DEVICE_TYPE1 0x00000001
1742#define SONYPI_DEVICE_TYPE2 0x00000002
1743#define SONYPI_DEVICE_TYPE3 0x00000004
1744
Mattia Dongili22a17782007-07-16 02:34:39 +09001745#define SONYPI_TYPE1_OFFSET 0x04
1746#define SONYPI_TYPE2_OFFSET 0x12
1747#define SONYPI_TYPE3_OFFSET 0x12
malattia@linux.it33a04452007-04-09 19:26:03 +02001748
malattia@linux.it33a04452007-04-09 19:26:03 +02001749struct sony_pic_ioport {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001750 struct acpi_resource_io io1;
1751 struct acpi_resource_io io2;
malattia@linux.it33a04452007-04-09 19:26:03 +02001752 struct list_head list;
1753};
1754
1755struct sony_pic_irq {
1756 struct acpi_resource_irq irq;
1757 struct list_head list;
1758};
1759
Mattia Dongilide920432008-01-14 18:05:43 +09001760struct sonypi_eventtypes {
1761 u8 data;
1762 unsigned long mask;
1763 struct sonypi_event *events;
1764};
1765
malattia@linux.it33a04452007-04-09 19:26:03 +02001766struct sony_pic_dev {
Mattia Dongili31df7142009-09-16 00:05:29 +09001767 struct acpi_device *acpi_dev;
1768 struct sony_pic_irq *cur_irq;
1769 struct sony_pic_ioport *cur_ioport;
1770 struct list_head interrupts;
1771 struct list_head ioports;
1772 struct mutex lock;
1773 struct sonypi_eventtypes *event_types;
1774 int (*handle_irq)(const u8, const u8);
1775 int model;
1776 u16 evport_offset;
1777 u8 camera_power;
1778 u8 bluetooth_power;
1779 u8 wwan_power;
malattia@linux.it33a04452007-04-09 19:26:03 +02001780};
1781
1782static struct sony_pic_dev spic_dev = {
1783 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
1784 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
1785};
1786
Alan Jenkins5e6f9722009-09-16 00:05:32 +09001787static int spic_drv_registered;
1788
malattia@linux.it33a04452007-04-09 19:26:03 +02001789/* Event masks */
1790#define SONYPI_JOGGER_MASK 0x00000001
1791#define SONYPI_CAPTURE_MASK 0x00000002
1792#define SONYPI_FNKEY_MASK 0x00000004
1793#define SONYPI_BLUETOOTH_MASK 0x00000008
1794#define SONYPI_PKEY_MASK 0x00000010
1795#define SONYPI_BACK_MASK 0x00000020
1796#define SONYPI_HELP_MASK 0x00000040
1797#define SONYPI_LID_MASK 0x00000080
1798#define SONYPI_ZOOM_MASK 0x00000100
1799#define SONYPI_THUMBPHRASE_MASK 0x00000200
1800#define SONYPI_MEYE_MASK 0x00000400
1801#define SONYPI_MEMORYSTICK_MASK 0x00000800
1802#define SONYPI_BATTERY_MASK 0x00001000
1803#define SONYPI_WIRELESS_MASK 0x00002000
1804
1805struct sonypi_event {
1806 u8 data;
1807 u8 event;
1808};
1809
1810/* The set of possible button release events */
1811static struct sonypi_event sonypi_releaseev[] = {
1812 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
1813 { 0, 0 }
1814};
1815
1816/* The set of possible jogger events */
1817static struct sonypi_event sonypi_joggerev[] = {
1818 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
1819 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
1820 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
1821 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
1822 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
1823 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
1824 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
1825 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
1826 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
1827 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
1828 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
1829 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
1830 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
1831 { 0, 0 }
1832};
1833
1834/* The set of possible capture button events */
1835static struct sonypi_event sonypi_captureev[] = {
1836 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
1837 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09001838 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02001839 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
1840 { 0, 0 }
1841};
1842
1843/* The set of possible fnkeys events */
1844static struct sonypi_event sonypi_fnkeyev[] = {
1845 { 0x10, SONYPI_EVENT_FNKEY_ESC },
1846 { 0x11, SONYPI_EVENT_FNKEY_F1 },
1847 { 0x12, SONYPI_EVENT_FNKEY_F2 },
1848 { 0x13, SONYPI_EVENT_FNKEY_F3 },
1849 { 0x14, SONYPI_EVENT_FNKEY_F4 },
1850 { 0x15, SONYPI_EVENT_FNKEY_F5 },
1851 { 0x16, SONYPI_EVENT_FNKEY_F6 },
1852 { 0x17, SONYPI_EVENT_FNKEY_F7 },
1853 { 0x18, SONYPI_EVENT_FNKEY_F8 },
1854 { 0x19, SONYPI_EVENT_FNKEY_F9 },
1855 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
1856 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
1857 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
1858 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
1859 { 0x21, SONYPI_EVENT_FNKEY_1 },
1860 { 0x22, SONYPI_EVENT_FNKEY_2 },
1861 { 0x31, SONYPI_EVENT_FNKEY_D },
1862 { 0x32, SONYPI_EVENT_FNKEY_E },
1863 { 0x33, SONYPI_EVENT_FNKEY_F },
1864 { 0x34, SONYPI_EVENT_FNKEY_S },
1865 { 0x35, SONYPI_EVENT_FNKEY_B },
1866 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
1867 { 0, 0 }
1868};
1869
1870/* The set of possible program key events */
1871static struct sonypi_event sonypi_pkeyev[] = {
1872 { 0x01, SONYPI_EVENT_PKEY_P1 },
1873 { 0x02, SONYPI_EVENT_PKEY_P2 },
1874 { 0x04, SONYPI_EVENT_PKEY_P3 },
Harald Jenny1cae7102009-03-26 21:58:18 +09001875 { 0x20, SONYPI_EVENT_PKEY_P1 },
malattia@linux.it33a04452007-04-09 19:26:03 +02001876 { 0, 0 }
1877};
1878
1879/* The set of possible bluetooth events */
1880static struct sonypi_event sonypi_blueev[] = {
1881 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
1882 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
1883 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
1884 { 0, 0 }
1885};
1886
1887/* The set of possible wireless events */
1888static struct sonypi_event sonypi_wlessev[] = {
Mattia Dongili4eeb5022011-02-19 11:52:27 +09001889 { 0x59, SONYPI_EVENT_IGNORE },
1890 { 0x5a, SONYPI_EVENT_IGNORE },
malattia@linux.it33a04452007-04-09 19:26:03 +02001891 { 0, 0 }
1892};
1893
1894/* The set of possible back button events */
1895static struct sonypi_event sonypi_backev[] = {
1896 { 0x20, SONYPI_EVENT_BACK_PRESSED },
1897 { 0, 0 }
1898};
1899
1900/* The set of possible help button events */
1901static struct sonypi_event sonypi_helpev[] = {
1902 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
1903 { 0, 0 }
1904};
1905
1906
1907/* The set of possible lid events */
1908static struct sonypi_event sonypi_lidev[] = {
1909 { 0x51, SONYPI_EVENT_LID_CLOSED },
1910 { 0x50, SONYPI_EVENT_LID_OPENED },
1911 { 0, 0 }
1912};
1913
1914/* The set of possible zoom events */
1915static struct sonypi_event sonypi_zoomev[] = {
1916 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09001917 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
1918 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
Harald Jenny1cae7102009-03-26 21:58:18 +09001919 { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02001920 { 0, 0 }
1921};
1922
1923/* The set of possible thumbphrase events */
1924static struct sonypi_event sonypi_thumbphraseev[] = {
1925 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
1926 { 0, 0 }
1927};
1928
1929/* The set of possible motioneye camera events */
1930static struct sonypi_event sonypi_meyeev[] = {
1931 { 0x00, SONYPI_EVENT_MEYE_FACE },
1932 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
1933 { 0, 0 }
1934};
1935
1936/* The set of possible memorystick events */
1937static struct sonypi_event sonypi_memorystickev[] = {
1938 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
1939 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
1940 { 0, 0 }
1941};
1942
1943/* The set of possible battery events */
1944static struct sonypi_event sonypi_batteryev[] = {
1945 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
1946 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
1947 { 0, 0 }
1948};
1949
Harald Jenny1cae7102009-03-26 21:58:18 +09001950/* The set of possible volume events */
1951static struct sonypi_event sonypi_volumeev[] = {
1952 { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
1953 { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
1954 { 0, 0 }
1955};
1956
1957/* The set of possible brightness events */
1958static struct sonypi_event sonypi_brightnessev[] = {
1959 { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
1960 { 0, 0 }
1961};
1962
Mattia Dongilide920432008-01-14 18:05:43 +09001963static struct sonypi_eventtypes type1_events[] = {
1964 { 0, 0xffffffff, sonypi_releaseev },
1965 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
1966 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
1967 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
1968 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
1969 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1970 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1971 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
1972 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1973 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
1974 { 0 },
1975};
1976static struct sonypi_eventtypes type2_events[] = {
1977 { 0, 0xffffffff, sonypi_releaseev },
1978 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
1979 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
1980 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
1981 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1982 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1983 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
1984 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
1985 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
1986 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
1987 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
1988 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1989 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1990 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1991 { 0 },
1992};
1993static struct sonypi_eventtypes type3_events[] = {
1994 { 0, 0xffffffff, sonypi_releaseev },
1995 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1996 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
1997 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1998 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1999 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002000 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
2001 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
2002 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
Harald Jenny1cae7102009-03-26 21:58:18 +09002003 { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
2004 { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002005 { 0 },
Mattia Dongilide920432008-01-14 18:05:43 +09002006};
2007
Mattia Dongili3eb87492008-01-14 18:05:45 +09002008/* low level spic calls */
malattia@linux.it33a04452007-04-09 19:26:03 +02002009#define ITERATIONS_LONG 10000
2010#define ITERATIONS_SHORT 10
2011#define wait_on_command(command, iterations) { \
2012 unsigned int n = iterations; \
2013 while (--n && (command)) \
2014 udelay(1); \
2015 if (!n) \
2016 dprintk("command failed at %s : %s (line %d)\n", \
Harvey Harrison6e574192008-04-29 00:59:20 -07002017 __FILE__, __func__, __LINE__); \
malattia@linux.it33a04452007-04-09 19:26:03 +02002018}
2019
2020static u8 sony_pic_call1(u8 dev)
2021{
2022 u8 v1, v2;
2023
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002024 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002025 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002026 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2027 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
2028 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002029 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02002030 return v2;
2031}
2032
2033static u8 sony_pic_call2(u8 dev, u8 fn)
2034{
2035 u8 v1;
2036
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002037 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002038 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002039 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2040 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002041 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002042 outb(fn, spic_dev.cur_ioport->io1.minimum);
2043 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002044 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02002045 return v1;
2046}
2047
malattia@linux.it49a11de2007-04-09 19:28:56 +02002048static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
2049{
2050 u8 v1;
2051
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002052 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2053 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2054 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2055 outb(fn, spic_dev.cur_ioport->io1.minimum);
2056 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2057 outb(v, spic_dev.cur_ioport->io1.minimum);
2058 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002059 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
2060 dev, fn, v, v1);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002061 return v1;
2062}
2063
Mattia Dongili3eb87492008-01-14 18:05:45 +09002064/*
2065 * minidrivers for SPIC models
2066 */
Mattia Dongilie93c8a62009-03-26 21:58:17 +09002067static int type3_handle_irq(const u8 data_mask, const u8 ev)
Mattia Dongili3eb87492008-01-14 18:05:45 +09002068{
2069 /*
2070 * 0x31 could mean we have to take some extra action and wait for
Mattia Dongilie93c8a62009-03-26 21:58:17 +09002071 * the next irq for some Type3 models, it will generate a new
Mattia Dongili3eb87492008-01-14 18:05:45 +09002072 * irq and we can read new data from the device:
2073 * - 0x5c and 0x5f requires 0xA0
2074 * - 0x61 requires 0xB3
2075 */
2076 if (data_mask == 0x31) {
2077 if (ev == 0x5c || ev == 0x5f)
2078 sony_pic_call1(0xA0);
2079 else if (ev == 0x61)
2080 sony_pic_call1(0xB3);
2081 return 0;
2082 }
2083 return 1;
2084}
2085
Mattia Dongili3eb87492008-01-14 18:05:45 +09002086static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
2087{
2088 struct pci_dev *pcidev;
2089
2090 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2091 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
2092 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002093 dev->model = SONYPI_DEVICE_TYPE1;
2094 dev->evport_offset = SONYPI_TYPE1_OFFSET;
2095 dev->event_types = type1_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002096 goto out;
2097 }
2098
2099 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2100 PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
2101 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002102 dev->model = SONYPI_DEVICE_TYPE2;
2103 dev->evport_offset = SONYPI_TYPE2_OFFSET;
2104 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002105 goto out;
2106 }
2107
2108 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2109 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
2110 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002111 dev->model = SONYPI_DEVICE_TYPE3;
2112 dev->handle_irq = type3_handle_irq;
2113 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2114 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002115 goto out;
2116 }
2117
2118 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2119 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
2120 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002121 dev->model = SONYPI_DEVICE_TYPE3;
2122 dev->handle_irq = type3_handle_irq;
2123 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2124 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002125 goto out;
2126 }
2127
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09002128 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2129 PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
2130 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002131 dev->model = SONYPI_DEVICE_TYPE3;
2132 dev->handle_irq = type3_handle_irq;
2133 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2134 dev->event_types = type3_events;
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09002135 goto out;
2136 }
2137
Mattia Dongili3eb87492008-01-14 18:05:45 +09002138 /* default */
Mattia Dongili31df7142009-09-16 00:05:29 +09002139 dev->model = SONYPI_DEVICE_TYPE2;
2140 dev->evport_offset = SONYPI_TYPE2_OFFSET;
2141 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002142
2143out:
2144 if (pcidev)
2145 pci_dev_put(pcidev);
2146
Mattia Dongilid6697932011-02-19 11:52:28 +09002147 pr_info(DRV_PFX "detected Type%d model\n",
Mattia Dongili31df7142009-09-16 00:05:29 +09002148 dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
2149 dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
Mattia Dongili3eb87492008-01-14 18:05:45 +09002150}
2151
malattia@linux.it49a11de2007-04-09 19:28:56 +02002152/* camera tests and poweron/poweroff */
2153#define SONYPI_CAMERA_PICTURE 5
malattia@linux.it49a11de2007-04-09 19:28:56 +02002154#define SONYPI_CAMERA_CONTROL 0x10
malattia@linux.ite3646322007-04-28 23:34:22 +09002155
2156#define SONYPI_CAMERA_BRIGHTNESS 0
2157#define SONYPI_CAMERA_CONTRAST 1
2158#define SONYPI_CAMERA_HUE 2
2159#define SONYPI_CAMERA_COLOR 3
2160#define SONYPI_CAMERA_SHARPNESS 4
2161
2162#define SONYPI_CAMERA_EXPOSURE_MASK 0xC
2163#define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
2164#define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
2165#define SONYPI_CAMERA_MUTE_MASK 0x40
2166
2167/* the rest don't need a loop until not 0xff */
2168#define SONYPI_CAMERA_AGC 6
2169#define SONYPI_CAMERA_AGC_MASK 0x30
2170#define SONYPI_CAMERA_SHUTTER_MASK 0x7
2171
2172#define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
2173#define SONYPI_CAMERA_CONTROL 0x10
2174
2175#define SONYPI_CAMERA_STATUS 7
2176#define SONYPI_CAMERA_STATUS_READY 0x2
2177#define SONYPI_CAMERA_STATUS_POSITION 0x4
2178
2179#define SONYPI_DIRECTION_BACKWARDS 0x4
2180
2181#define SONYPI_CAMERA_REVISION 8
2182#define SONYPI_CAMERA_ROMVERSION 9
malattia@linux.it49a11de2007-04-09 19:28:56 +02002183
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002184static int __sony_pic_camera_ready(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002185{
2186 u8 v;
2187
2188 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
2189 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
2190}
2191
malattia@linux.ite3646322007-04-28 23:34:22 +09002192static int __sony_pic_camera_off(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002193{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002194 if (!camera) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002195 pr_warn(DRV_PFX "camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002196 return -ENODEV;
2197 }
2198
malattia@linux.it49a11de2007-04-09 19:28:56 +02002199 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
2200 SONYPI_CAMERA_MUTE_MASK),
2201 ITERATIONS_SHORT);
2202
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002203 if (spic_dev.camera_power) {
2204 sony_pic_call2(0x91, 0);
2205 spic_dev.camera_power = 0;
2206 }
2207 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002208}
2209
malattia@linux.ite3646322007-04-28 23:34:22 +09002210static int __sony_pic_camera_on(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002211{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002212 int i, j, x;
2213
2214 if (!camera) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002215 pr_warn(DRV_PFX "camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002216 return -ENODEV;
2217 }
malattia@linux.it49a11de2007-04-09 19:28:56 +02002218
2219 if (spic_dev.camera_power)
malattia@linux.ite3646322007-04-28 23:34:22 +09002220 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002221
2222 for (j = 5; j > 0; j--) {
2223
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002224 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002225 msleep(10);
2226 sony_pic_call1(0x93);
2227
2228 for (i = 400; i > 0; i--) {
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002229 if (__sony_pic_camera_ready())
malattia@linux.it49a11de2007-04-09 19:28:56 +02002230 break;
2231 msleep(10);
2232 }
2233 if (i)
2234 break;
2235 }
2236
2237 if (j == 0) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002238 pr_warn(DRV_PFX "failed to power on camera\n");
malattia@linux.ite3646322007-04-28 23:34:22 +09002239 return -ENODEV;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002240 }
2241
2242 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
2243 0x5a),
2244 ITERATIONS_SHORT);
2245
2246 spic_dev.camera_power = 1;
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002247 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002248}
2249
malattia@linux.ite3646322007-04-28 23:34:22 +09002250/* External camera command (exported to the motion eye v4l driver) */
2251int sony_pic_camera_command(int command, u8 value)
2252{
2253 if (!camera)
2254 return -EIO;
2255
2256 mutex_lock(&spic_dev.lock);
2257
2258 switch (command) {
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002259 case SONY_PIC_COMMAND_SETCAMERA:
malattia@linux.ite3646322007-04-28 23:34:22 +09002260 if (value)
2261 __sony_pic_camera_on();
2262 else
2263 __sony_pic_camera_off();
2264 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002265 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09002266 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
2267 ITERATIONS_SHORT);
2268 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002269 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
malattia@linux.ite3646322007-04-28 23:34:22 +09002270 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
2271 ITERATIONS_SHORT);
2272 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002273 case SONY_PIC_COMMAND_SETCAMERAHUE:
malattia@linux.ite3646322007-04-28 23:34:22 +09002274 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
2275 ITERATIONS_SHORT);
2276 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002277 case SONY_PIC_COMMAND_SETCAMERACOLOR:
malattia@linux.ite3646322007-04-28 23:34:22 +09002278 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
2279 ITERATIONS_SHORT);
2280 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002281 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09002282 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
2283 ITERATIONS_SHORT);
2284 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002285 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
malattia@linux.ite3646322007-04-28 23:34:22 +09002286 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
2287 ITERATIONS_SHORT);
2288 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002289 case SONY_PIC_COMMAND_SETCAMERAAGC:
malattia@linux.ite3646322007-04-28 23:34:22 +09002290 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
2291 ITERATIONS_SHORT);
2292 break;
2293 default:
Mattia Dongilid6697932011-02-19 11:52:28 +09002294 pr_err(DRV_PFX "sony_pic_camera_command invalid: %d\n",
malattia@linux.ite3646322007-04-28 23:34:22 +09002295 command);
2296 break;
2297 }
2298 mutex_unlock(&spic_dev.lock);
2299 return 0;
2300}
2301EXPORT_SYMBOL(sony_pic_camera_command);
2302
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002303/* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002304static void __sony_pic_set_wwanpower(u8 state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002305{
2306 state = !!state;
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002307 if (spic_dev.wwan_power == state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002308 return;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002309 sony_pic_call2(0xB0, state);
Sergey Yanovich3ad1b762009-03-26 21:58:21 +09002310 sony_pic_call1(0x82);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002311 spic_dev.wwan_power = state;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002312}
2313
2314static ssize_t sony_pic_wwanpower_store(struct device *dev,
2315 struct device_attribute *attr,
2316 const char *buffer, size_t count)
2317{
2318 unsigned long value;
2319 if (count > 31)
2320 return -EINVAL;
2321
2322 value = simple_strtoul(buffer, NULL, 10);
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002323 mutex_lock(&spic_dev.lock);
2324 __sony_pic_set_wwanpower(value);
2325 mutex_unlock(&spic_dev.lock);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002326
2327 return count;
2328}
2329
2330static ssize_t sony_pic_wwanpower_show(struct device *dev,
2331 struct device_attribute *attr, char *buffer)
2332{
2333 ssize_t count;
2334 mutex_lock(&spic_dev.lock);
2335 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
2336 mutex_unlock(&spic_dev.lock);
2337 return count;
2338}
2339
malattia@linux.it49a11de2007-04-09 19:28:56 +02002340/* bluetooth subsystem power state */
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002341static void __sony_pic_set_bluetoothpower(u8 state)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002342{
2343 state = !!state;
2344 if (spic_dev.bluetooth_power == state)
2345 return;
2346 sony_pic_call2(0x96, state);
2347 sony_pic_call1(0x82);
2348 spic_dev.bluetooth_power = state;
2349}
2350
2351static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
2352 struct device_attribute *attr,
2353 const char *buffer, size_t count)
2354{
2355 unsigned long value;
2356 if (count > 31)
2357 return -EINVAL;
2358
2359 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002360 mutex_lock(&spic_dev.lock);
2361 __sony_pic_set_bluetoothpower(value);
2362 mutex_unlock(&spic_dev.lock);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002363
2364 return count;
2365}
2366
2367static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
2368 struct device_attribute *attr, char *buffer)
2369{
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002370 ssize_t count = 0;
2371 mutex_lock(&spic_dev.lock);
2372 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
2373 mutex_unlock(&spic_dev.lock);
2374 return count;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002375}
2376
2377/* fan speed */
2378/* FAN0 information (reverse engineered from ACPI tables) */
2379#define SONY_PIC_FAN0_STATUS 0x93
malattia@linux.it7b153f32007-04-09 19:31:25 +02002380static int sony_pic_set_fanspeed(unsigned long value)
2381{
2382 return ec_write(SONY_PIC_FAN0_STATUS, value);
2383}
2384
2385static int sony_pic_get_fanspeed(u8 *value)
2386{
2387 return ec_read(SONY_PIC_FAN0_STATUS, value);
2388}
2389
malattia@linux.it49a11de2007-04-09 19:28:56 +02002390static ssize_t sony_pic_fanspeed_store(struct device *dev,
2391 struct device_attribute *attr,
2392 const char *buffer, size_t count)
2393{
2394 unsigned long value;
2395 if (count > 31)
2396 return -EINVAL;
2397
2398 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002399 if (sony_pic_set_fanspeed(value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02002400 return -EIO;
2401
2402 return count;
2403}
2404
2405static ssize_t sony_pic_fanspeed_show(struct device *dev,
2406 struct device_attribute *attr, char *buffer)
2407{
2408 u8 value = 0;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002409 if (sony_pic_get_fanspeed(&value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02002410 return -EIO;
2411
2412 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
2413}
2414
2415#define SPIC_ATTR(_name, _mode) \
2416struct device_attribute spic_attr_##_name = __ATTR(_name, \
2417 _mode, sony_pic_## _name ##_show, \
2418 sony_pic_## _name ##_store)
2419
malattia@linux.it49a11de2007-04-09 19:28:56 +02002420static SPIC_ATTR(bluetoothpower, 0644);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002421static SPIC_ATTR(wwanpower, 0644);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002422static SPIC_ATTR(fanspeed, 0644);
2423
2424static struct attribute *spic_attributes[] = {
malattia@linux.it49a11de2007-04-09 19:28:56 +02002425 &spic_attr_bluetoothpower.attr,
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002426 &spic_attr_wwanpower.attr,
malattia@linux.it49a11de2007-04-09 19:28:56 +02002427 &spic_attr_fanspeed.attr,
2428 NULL
2429};
2430
2431static struct attribute_group spic_attribute_group = {
2432 .attrs = spic_attributes
2433};
2434
malattia@linux.it7b153f32007-04-09 19:31:25 +02002435/******** SONYPI compatibility **********/
Mattia Dongilia64e62a2007-05-01 11:19:53 +09002436#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +02002437
2438/* battery / brightness / temperature addresses */
2439#define SONYPI_BAT_FLAGS 0x81
2440#define SONYPI_LCD_LIGHT 0x96
2441#define SONYPI_BAT1_PCTRM 0xa0
2442#define SONYPI_BAT1_LEFT 0xa2
2443#define SONYPI_BAT1_MAXRT 0xa4
2444#define SONYPI_BAT2_PCTRM 0xa8
2445#define SONYPI_BAT2_LEFT 0xaa
2446#define SONYPI_BAT2_MAXRT 0xac
2447#define SONYPI_BAT1_MAXTK 0xb0
2448#define SONYPI_BAT1_FULL 0xb2
2449#define SONYPI_BAT2_MAXTK 0xb8
2450#define SONYPI_BAT2_FULL 0xba
2451#define SONYPI_TEMP_STATUS 0xC1
2452
2453struct sonypi_compat_s {
2454 struct fasync_struct *fifo_async;
Stefani Seibold45465482009-12-21 14:37:26 -08002455 struct kfifo fifo;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002456 spinlock_t fifo_lock;
2457 wait_queue_head_t fifo_proc_list;
2458 atomic_t open_count;
2459};
2460static struct sonypi_compat_s sonypi_compat = {
2461 .open_count = ATOMIC_INIT(0),
2462};
2463
2464static int sonypi_misc_fasync(int fd, struct file *filp, int on)
2465{
Jonathan Corbet60aa4922009-02-01 14:52:56 -07002466 return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002467}
2468
2469static int sonypi_misc_release(struct inode *inode, struct file *file)
2470{
malattia@linux.it7b153f32007-04-09 19:31:25 +02002471 atomic_dec(&sonypi_compat.open_count);
2472 return 0;
2473}
2474
2475static int sonypi_misc_open(struct inode *inode, struct file *file)
2476{
2477 /* Flush input queue on first open */
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002478 unsigned long flags;
2479
Stefani Seibold45465482009-12-21 14:37:26 -08002480 spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002481
malattia@linux.it7b153f32007-04-09 19:31:25 +02002482 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
Stefani Seibolde64c0262009-12-21 14:37:28 -08002483 kfifo_reset(&sonypi_compat.fifo);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002484
Stefani Seibold45465482009-12-21 14:37:26 -08002485 spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002486
malattia@linux.it7b153f32007-04-09 19:31:25 +02002487 return 0;
2488}
2489
2490static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
2491 size_t count, loff_t *pos)
2492{
2493 ssize_t ret;
2494 unsigned char c;
2495
Stefani Seibold45465482009-12-21 14:37:26 -08002496 if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
malattia@linux.it7b153f32007-04-09 19:31:25 +02002497 (file->f_flags & O_NONBLOCK))
2498 return -EAGAIN;
2499
2500 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
Stefani Seibold45465482009-12-21 14:37:26 -08002501 kfifo_len(&sonypi_compat.fifo) != 0);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002502 if (ret)
2503 return ret;
2504
2505 while (ret < count &&
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002506 (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002507 &sonypi_compat.fifo_lock) == sizeof(c))) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02002508 if (put_user(c, buf++))
2509 return -EFAULT;
2510 ret++;
2511 }
2512
2513 if (ret > 0) {
2514 struct inode *inode = file->f_path.dentry->d_inode;
2515 inode->i_atime = current_fs_time(inode->i_sb);
2516 }
2517
2518 return ret;
2519}
2520
2521static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
2522{
2523 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
Stefani Seibold45465482009-12-21 14:37:26 -08002524 if (kfifo_len(&sonypi_compat.fifo))
malattia@linux.it7b153f32007-04-09 19:31:25 +02002525 return POLLIN | POLLRDNORM;
2526 return 0;
2527}
2528
2529static int ec_read16(u8 addr, u16 *value)
2530{
2531 u8 val_lb, val_hb;
2532 if (ec_read(addr, &val_lb))
2533 return -1;
2534 if (ec_read(addr + 1, &val_hb))
2535 return -1;
2536 *value = val_lb | (val_hb << 8);
2537 return 0;
2538}
2539
Alan Cox2b24ef02009-03-26 21:58:19 +09002540static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
2541 unsigned long arg)
malattia@linux.it7b153f32007-04-09 19:31:25 +02002542{
2543 int ret = 0;
2544 void __user *argp = (void __user *)arg;
2545 u8 val8;
2546 u16 val16;
2547 int value;
2548
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002549 mutex_lock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002550 switch (cmd) {
2551 case SONYPI_IOCGBRT:
2552 if (sony_backlight_device == NULL) {
2553 ret = -EIO;
2554 break;
2555 }
2556 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
2557 ret = -EIO;
2558 break;
2559 }
2560 val8 = ((value & 0xff) - 1) << 5;
2561 if (copy_to_user(argp, &val8, sizeof(val8)))
2562 ret = -EFAULT;
2563 break;
2564 case SONYPI_IOCSBRT:
2565 if (sony_backlight_device == NULL) {
2566 ret = -EIO;
2567 break;
2568 }
2569 if (copy_from_user(&val8, argp, sizeof(val8))) {
2570 ret = -EFAULT;
2571 break;
2572 }
2573 if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
2574 (val8 >> 5) + 1, NULL)) {
2575 ret = -EIO;
2576 break;
2577 }
2578 /* sync the backlight device status */
2579 sony_backlight_device->props.brightness =
2580 sony_backlight_get_brightness(sony_backlight_device);
2581 break;
2582 case SONYPI_IOCGBAT1CAP:
2583 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
2584 ret = -EIO;
2585 break;
2586 }
2587 if (copy_to_user(argp, &val16, sizeof(val16)))
2588 ret = -EFAULT;
2589 break;
2590 case SONYPI_IOCGBAT1REM:
2591 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
2592 ret = -EIO;
2593 break;
2594 }
2595 if (copy_to_user(argp, &val16, sizeof(val16)))
2596 ret = -EFAULT;
2597 break;
2598 case SONYPI_IOCGBAT2CAP:
2599 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
2600 ret = -EIO;
2601 break;
2602 }
2603 if (copy_to_user(argp, &val16, sizeof(val16)))
2604 ret = -EFAULT;
2605 break;
2606 case SONYPI_IOCGBAT2REM:
2607 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
2608 ret = -EIO;
2609 break;
2610 }
2611 if (copy_to_user(argp, &val16, sizeof(val16)))
2612 ret = -EFAULT;
2613 break;
2614 case SONYPI_IOCGBATFLAGS:
2615 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
2616 ret = -EIO;
2617 break;
2618 }
2619 val8 &= 0x07;
2620 if (copy_to_user(argp, &val8, sizeof(val8)))
2621 ret = -EFAULT;
2622 break;
2623 case SONYPI_IOCGBLUE:
2624 val8 = spic_dev.bluetooth_power;
2625 if (copy_to_user(argp, &val8, sizeof(val8)))
2626 ret = -EFAULT;
2627 break;
2628 case SONYPI_IOCSBLUE:
2629 if (copy_from_user(&val8, argp, sizeof(val8))) {
2630 ret = -EFAULT;
2631 break;
2632 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002633 __sony_pic_set_bluetoothpower(val8);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002634 break;
2635 /* FAN Controls */
2636 case SONYPI_IOCGFAN:
2637 if (sony_pic_get_fanspeed(&val8)) {
2638 ret = -EIO;
2639 break;
2640 }
2641 if (copy_to_user(argp, &val8, sizeof(val8)))
2642 ret = -EFAULT;
2643 break;
2644 case SONYPI_IOCSFAN:
2645 if (copy_from_user(&val8, argp, sizeof(val8))) {
2646 ret = -EFAULT;
2647 break;
2648 }
2649 if (sony_pic_set_fanspeed(val8))
2650 ret = -EIO;
2651 break;
2652 /* GET Temperature (useful under APM) */
2653 case SONYPI_IOCGTEMP:
2654 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
2655 ret = -EIO;
2656 break;
2657 }
2658 if (copy_to_user(argp, &val8, sizeof(val8)))
2659 ret = -EFAULT;
2660 break;
2661 default:
2662 ret = -EINVAL;
2663 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002664 mutex_unlock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002665 return ret;
2666}
2667
2668static const struct file_operations sonypi_misc_fops = {
2669 .owner = THIS_MODULE,
2670 .read = sonypi_misc_read,
2671 .poll = sonypi_misc_poll,
2672 .open = sonypi_misc_open,
2673 .release = sonypi_misc_release,
2674 .fasync = sonypi_misc_fasync,
Alan Cox2b24ef02009-03-26 21:58:19 +09002675 .unlocked_ioctl = sonypi_misc_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002676 .llseek = noop_llseek,
malattia@linux.it7b153f32007-04-09 19:31:25 +02002677};
2678
2679static struct miscdevice sonypi_misc_device = {
2680 .minor = MISC_DYNAMIC_MINOR,
2681 .name = "sonypi",
2682 .fops = &sonypi_misc_fops,
2683};
2684
2685static void sonypi_compat_report_event(u8 event)
2686{
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002687 kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002688 sizeof(event), &sonypi_compat.fifo_lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002689 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
2690 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
2691}
2692
2693static int sonypi_compat_init(void)
2694{
2695 int error;
2696
2697 spin_lock_init(&sonypi_compat.fifo_lock);
Stefani Seibold45465482009-12-21 14:37:26 -08002698 error =
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002699 kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -08002700 if (error) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002701 pr_err(DRV_PFX "kfifo_alloc failed\n");
Stefani Seibold45465482009-12-21 14:37:26 -08002702 return error;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002703 }
2704
2705 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002706
2707 if (minor != -1)
2708 sonypi_misc_device.minor = minor;
2709 error = misc_register(&sonypi_misc_device);
2710 if (error) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002711 pr_err(DRV_PFX "misc_register failed\n");
malattia@linux.it7b153f32007-04-09 19:31:25 +02002712 goto err_free_kfifo;
2713 }
2714 if (minor == -1)
Mattia Dongilid6697932011-02-19 11:52:28 +09002715 pr_info(DRV_PFX "device allocated minor is %d\n",
malattia@linux.it7b153f32007-04-09 19:31:25 +02002716 sonypi_misc_device.minor);
2717
2718 return 0;
2719
2720err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -08002721 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002722 return error;
2723}
2724
2725static void sonypi_compat_exit(void)
2726{
2727 misc_deregister(&sonypi_misc_device);
Stefani Seibold45465482009-12-21 14:37:26 -08002728 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002729}
2730#else
2731static int sonypi_compat_init(void) { return 0; }
2732static void sonypi_compat_exit(void) { }
2733static void sonypi_compat_report_event(u8 event) { }
Mattia Dongilia64e62a2007-05-01 11:19:53 +09002734#endif /* CONFIG_SONYPI_COMPAT */
malattia@linux.it7b153f32007-04-09 19:31:25 +02002735
malattia@linux.it1549ee62007-04-09 10:19:08 +02002736/*
malattia@linux.it33a04452007-04-09 19:26:03 +02002737 * ACPI callbacks
malattia@linux.it1549ee62007-04-09 10:19:08 +02002738 */
malattia@linux.it33a04452007-04-09 19:26:03 +02002739static acpi_status
2740sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
2741{
2742 u32 i;
2743 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
2744
2745 switch (resource->type) {
2746 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002747 {
2748 /* start IO enumeration */
2749 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
2750 if (!ioport)
2751 return AE_ERROR;
2752
2753 list_add(&ioport->list, &dev->ioports);
2754 return AE_OK;
2755 }
2756
malattia@linux.it33a04452007-04-09 19:26:03 +02002757 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002758 /* end IO enumeration */
malattia@linux.it33a04452007-04-09 19:26:03 +02002759 return AE_OK;
2760
2761 case ACPI_RESOURCE_TYPE_IRQ:
2762 {
2763 struct acpi_resource_irq *p = &resource->data.irq;
2764 struct sony_pic_irq *interrupt = NULL;
2765 if (!p || !p->interrupt_count) {
2766 /*
2767 * IRQ descriptors may have no IRQ# bits set,
2768 * particularly those those w/ _STA disabled
2769 */
2770 dprintk("Blank IRQ resource\n");
2771 return AE_OK;
2772 }
2773 for (i = 0; i < p->interrupt_count; i++) {
2774 if (!p->interrupts[i]) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002775 pr_warn(DRV_PFX "Invalid IRQ %d\n",
malattia@linux.it33a04452007-04-09 19:26:03 +02002776 p->interrupts[i]);
2777 continue;
2778 }
2779 interrupt = kzalloc(sizeof(*interrupt),
2780 GFP_KERNEL);
2781 if (!interrupt)
2782 return AE_ERROR;
2783
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002784 list_add(&interrupt->list, &dev->interrupts);
malattia@linux.it33a04452007-04-09 19:26:03 +02002785 interrupt->irq.triggering = p->triggering;
2786 interrupt->irq.polarity = p->polarity;
2787 interrupt->irq.sharable = p->sharable;
2788 interrupt->irq.interrupt_count = 1;
2789 interrupt->irq.interrupts[0] = p->interrupts[i];
2790 }
2791 return AE_OK;
2792 }
2793 case ACPI_RESOURCE_TYPE_IO:
2794 {
2795 struct acpi_resource_io *io = &resource->data.io;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002796 struct sony_pic_ioport *ioport =
2797 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
malattia@linux.it33a04452007-04-09 19:26:03 +02002798 if (!io) {
2799 dprintk("Blank IO resource\n");
2800 return AE_OK;
2801 }
2802
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002803 if (!ioport->io1.minimum) {
2804 memcpy(&ioport->io1, io, sizeof(*io));
2805 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
2806 ioport->io1.address_length);
2807 }
2808 else if (!ioport->io2.minimum) {
2809 memcpy(&ioport->io2, io, sizeof(*io));
2810 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
2811 ioport->io2.address_length);
2812 }
2813 else {
Mattia Dongilid6697932011-02-19 11:52:28 +09002814 pr_err(DRV_PFX "Unknown SPIC Type, more than 2 IO Ports\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002815 return AE_ERROR;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002816 }
malattia@linux.it33a04452007-04-09 19:26:03 +02002817 return AE_OK;
2818 }
2819 default:
2820 dprintk("Resource %d isn't an IRQ nor an IO port\n",
2821 resource->type);
2822
2823 case ACPI_RESOURCE_TYPE_END_TAG:
2824 return AE_OK;
2825 }
2826 return AE_CTRL_TERMINATE;
2827}
2828
2829static int sony_pic_possible_resources(struct acpi_device *device)
2830{
2831 int result = 0;
2832 acpi_status status = AE_OK;
2833
2834 if (!device)
2835 return -EINVAL;
2836
2837 /* get device status */
2838 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
2839 dprintk("Evaluating _STA\n");
2840 result = acpi_bus_get_status(device);
2841 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002842 pr_warn(DRV_PFX "Unable to read status\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002843 goto end;
2844 }
2845
2846 if (!device->status.enabled)
2847 dprintk("Device disabled\n");
2848 else
2849 dprintk("Device enabled\n");
2850
2851 /*
2852 * Query and parse 'method'
2853 */
2854 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
2855 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
2856 sony_pic_read_possible_resource, &spic_dev);
2857 if (ACPI_FAILURE(status)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002858 pr_warn(DRV_PFX "Failure evaluating %s\n",
malattia@linux.it33a04452007-04-09 19:26:03 +02002859 METHOD_NAME__PRS);
2860 result = -ENODEV;
2861 }
2862end:
2863 return result;
2864}
2865
2866/*
2867 * Disable the spic device by calling its _DIS method
2868 */
2869static int sony_pic_disable(struct acpi_device *device)
2870{
Matthew Garrett6158d3a2008-10-29 14:01:03 -07002871 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
2872 NULL);
2873
2874 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
malattia@linux.it33a04452007-04-09 19:26:03 +02002875 return -ENXIO;
2876
2877 dprintk("Device disabled\n");
2878 return 0;
2879}
2880
2881
2882/*
2883 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
2884 *
2885 * Call _SRS to set current resources
2886 */
2887static int sony_pic_enable(struct acpi_device *device,
2888 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
2889{
2890 acpi_status status;
2891 int result = 0;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002892 /* Type 1 resource layout is:
2893 * IO
2894 * IO
2895 * IRQNoFlags
2896 * End
2897 *
2898 * Type 2 and 3 resource layout is:
2899 * IO
2900 * IRQNoFlags
2901 * End
2902 */
malattia@linux.it33a04452007-04-09 19:26:03 +02002903 struct {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002904 struct acpi_resource res1;
2905 struct acpi_resource res2;
2906 struct acpi_resource res3;
2907 struct acpi_resource res4;
malattia@linux.it33a04452007-04-09 19:26:03 +02002908 } *resource;
2909 struct acpi_buffer buffer = { 0, NULL };
2910
2911 if (!ioport || !irq)
2912 return -EINVAL;
2913
2914 /* init acpi_buffer */
2915 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
2916 if (!resource)
2917 return -ENOMEM;
2918
2919 buffer.length = sizeof(*resource) + 1;
2920 buffer.pointer = resource;
2921
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002922 /* setup Type 1 resources */
Mattia Dongili31df7142009-09-16 00:05:29 +09002923 if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002924
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002925 /* setup io resources */
2926 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2927 resource->res1.length = sizeof(struct acpi_resource);
2928 memcpy(&resource->res1.data.io, &ioport->io1,
2929 sizeof(struct acpi_resource_io));
malattia@linux.it33a04452007-04-09 19:26:03 +02002930
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002931 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
2932 resource->res2.length = sizeof(struct acpi_resource);
2933 memcpy(&resource->res2.data.io, &ioport->io2,
2934 sizeof(struct acpi_resource_io));
2935
2936 /* setup irq resource */
2937 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
2938 resource->res3.length = sizeof(struct acpi_resource);
2939 memcpy(&resource->res3.data.irq, &irq->irq,
2940 sizeof(struct acpi_resource_irq));
2941 /* we requested a shared irq */
2942 resource->res3.data.irq.sharable = ACPI_SHARED;
2943
2944 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
2945
2946 }
2947 /* setup Type 2/3 resources */
2948 else {
2949 /* setup io resource */
2950 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2951 resource->res1.length = sizeof(struct acpi_resource);
2952 memcpy(&resource->res1.data.io, &ioport->io1,
2953 sizeof(struct acpi_resource_io));
2954
2955 /* setup irq resource */
2956 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
2957 resource->res2.length = sizeof(struct acpi_resource);
2958 memcpy(&resource->res2.data.irq, &irq->irq,
2959 sizeof(struct acpi_resource_irq));
2960 /* we requested a shared irq */
2961 resource->res2.data.irq.sharable = ACPI_SHARED;
2962
2963 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
2964 }
malattia@linux.it33a04452007-04-09 19:26:03 +02002965
2966 /* Attempt to set the resource */
2967 dprintk("Evaluating _SRS\n");
2968 status = acpi_set_current_resources(device->handle, &buffer);
2969
2970 /* check for total failure */
2971 if (ACPI_FAILURE(status)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002972 pr_err(DRV_PFX "Error evaluating _SRS\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002973 result = -ENODEV;
2974 goto end;
2975 }
2976
2977 /* Necessary device initializations calls (from sonypi) */
2978 sony_pic_call1(0x82);
2979 sony_pic_call2(0x81, 0xff);
2980 sony_pic_call1(compat ? 0x92 : 0x82);
2981
2982end:
2983 kfree(resource);
2984 return result;
2985}
2986
2987/*****************
2988 *
2989 * ISR: some event is available
2990 *
2991 *****************/
2992static irqreturn_t sony_pic_irq(int irq, void *dev_id)
2993{
2994 int i, j;
malattia@linux.it33a04452007-04-09 19:26:03 +02002995 u8 ev = 0;
2996 u8 data_mask = 0;
2997 u8 device_event = 0;
2998
2999 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
3000
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003001 ev = inb_p(dev->cur_ioport->io1.minimum);
3002 if (dev->cur_ioport->io2.minimum)
3003 data_mask = inb_p(dev->cur_ioport->io2.minimum);
3004 else
Mattia Dongilide920432008-01-14 18:05:43 +09003005 data_mask = inb_p(dev->cur_ioport->io1.minimum +
Mattia Dongili31df7142009-09-16 00:05:29 +09003006 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003007
Mattia Dongili22a17782007-07-16 02:34:39 +09003008 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
Mattia Dongilide920432008-01-14 18:05:43 +09003009 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09003010 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003011
3012 if (ev == 0x00 || ev == 0xff)
3013 return IRQ_HANDLED;
3014
Mattia Dongili31df7142009-09-16 00:05:29 +09003015 for (i = 0; dev->event_types[i].mask; i++) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003016
Mattia Dongili31df7142009-09-16 00:05:29 +09003017 if ((data_mask & dev->event_types[i].data) !=
3018 dev->event_types[i].data)
malattia@linux.it33a04452007-04-09 19:26:03 +02003019 continue;
3020
Mattia Dongili31df7142009-09-16 00:05:29 +09003021 if (!(mask & dev->event_types[i].mask))
malattia@linux.it33a04452007-04-09 19:26:03 +02003022 continue;
3023
Mattia Dongili31df7142009-09-16 00:05:29 +09003024 for (j = 0; dev->event_types[i].events[j].event; j++) {
3025 if (ev == dev->event_types[i].events[j].data) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003026 device_event =
Mattia Dongili31df7142009-09-16 00:05:29 +09003027 dev->event_types[i].events[j].event;
Mattia Dongili4eeb5022011-02-19 11:52:27 +09003028 /* some events may require ignoring */
3029 if (!device_event)
3030 return IRQ_HANDLED;
malattia@linux.it33a04452007-04-09 19:26:03 +02003031 goto found;
3032 }
3033 }
3034 }
Mattia Dongili3eb87492008-01-14 18:05:45 +09003035 /* Still not able to decode the event try to pass
3036 * it over to the minidriver
3037 */
Mattia Dongili31df7142009-09-16 00:05:29 +09003038 if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
Mattia Dongili3eb87492008-01-14 18:05:45 +09003039 return IRQ_HANDLED;
3040
Mattia Dongilide920432008-01-14 18:05:43 +09003041 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
3042 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09003043 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003044 return IRQ_HANDLED;
3045
3046found:
malattia@linux.it1549ee62007-04-09 10:19:08 +02003047 sony_laptop_report_input_event(device_event);
Mattia Dongilide920432008-01-14 18:05:43 +09003048 acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003049 sonypi_compat_report_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02003050 return IRQ_HANDLED;
3051}
3052
3053/*****************
3054 *
3055 * ACPI driver
3056 *
3057 *****************/
3058static int sony_pic_remove(struct acpi_device *device, int type)
3059{
3060 struct sony_pic_ioport *io, *tmp_io;
3061 struct sony_pic_irq *irq, *tmp_irq;
3062
3063 if (sony_pic_disable(device)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003064 pr_err(DRV_PFX "Couldn't disable device.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003065 return -ENXIO;
3066 }
3067
3068 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003069 release_region(spic_dev.cur_ioport->io1.minimum,
3070 spic_dev.cur_ioport->io1.address_length);
3071 if (spic_dev.cur_ioport->io2.minimum)
3072 release_region(spic_dev.cur_ioport->io2.minimum,
3073 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02003074
Mattia Dongili015a9162007-08-12 16:20:27 +09003075 sonypi_compat_exit();
3076
malattia@linux.it1549ee62007-04-09 10:19:08 +02003077 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02003078
malattia@linux.it49a11de2007-04-09 19:28:56 +02003079 /* pf attrs */
3080 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
3081 sony_pf_remove();
3082
malattia@linux.it33a04452007-04-09 19:26:03 +02003083 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
3084 list_del(&io->list);
3085 kfree(io);
3086 }
3087 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
3088 list_del(&irq->list);
3089 kfree(irq);
3090 }
3091 spic_dev.cur_ioport = NULL;
3092 spic_dev.cur_irq = NULL;
3093
malattia@linux.itf6119b02007-04-09 19:31:06 +02003094 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003095 return 0;
3096}
3097
3098static int sony_pic_add(struct acpi_device *device)
3099{
3100 int result;
3101 struct sony_pic_ioport *io, *tmp_io;
3102 struct sony_pic_irq *irq, *tmp_irq;
3103
Mattia Dongilid6697932011-02-19 11:52:28 +09003104 pr_info(DRV_PFX "%s v%s.\n", SONY_PIC_DRIVER_NAME,
3105 SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.it33a04452007-04-09 19:26:03 +02003106
3107 spic_dev.acpi_dev = device;
3108 strcpy(acpi_device_class(device), "sony/hotkey");
Mattia Dongilide920432008-01-14 18:05:43 +09003109 sony_pic_detect_device_type(&spic_dev);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003110 mutex_init(&spic_dev.lock);
malattia@linux.it33a04452007-04-09 19:26:03 +02003111
3112 /* read _PRS resources */
3113 result = sony_pic_possible_resources(device);
3114 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003115 pr_err(DRV_PFX "Unable to read possible resources.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003116 goto err_free_resources;
3117 }
3118
3119 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05003120 result = sony_laptop_setup_input(device);
malattia@linux.it33a04452007-04-09 19:26:03 +02003121 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003122 pr_err(DRV_PFX "Unable to create input devices.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003123 goto err_free_resources;
3124 }
3125
Mattia Dongili015a9162007-08-12 16:20:27 +09003126 if (sonypi_compat_init())
3127 goto err_remove_input;
3128
malattia@linux.it33a04452007-04-09 19:26:03 +02003129 /* request io port */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003130 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
3131 if (request_region(io->io1.minimum, io->io1.address_length,
Lucas De Marchic8440332011-03-17 17:18:22 -03003132 "Sony Programmable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003133 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
3134 io->io1.minimum, io->io1.maximum,
3135 io->io1.address_length);
3136 /* Type 1 have 2 ioports */
3137 if (io->io2.minimum) {
3138 if (request_region(io->io2.minimum,
3139 io->io2.address_length,
Lucas De Marchic8440332011-03-17 17:18:22 -03003140 "Sony Programmable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003141 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
3142 io->io2.minimum, io->io2.maximum,
3143 io->io2.address_length);
3144 spic_dev.cur_ioport = io;
3145 break;
3146 }
3147 else {
3148 dprintk("Unable to get I/O port2: "
3149 "0x%.4x (0x%.4x) + 0x%.2x\n",
3150 io->io2.minimum, io->io2.maximum,
3151 io->io2.address_length);
3152 release_region(io->io1.minimum,
3153 io->io1.address_length);
3154 }
3155 }
3156 else {
3157 spic_dev.cur_ioport = io;
3158 break;
3159 }
malattia@linux.it33a04452007-04-09 19:26:03 +02003160 }
3161 }
3162 if (!spic_dev.cur_ioport) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003163 pr_err(DRV_PFX "Failed to request_region.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003164 result = -ENODEV;
Mattia Dongili015a9162007-08-12 16:20:27 +09003165 goto err_remove_compat;
malattia@linux.it33a04452007-04-09 19:26:03 +02003166 }
3167
3168 /* request IRQ */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003169 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003170 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
Mattia Dongilid1e0de92009-09-16 00:05:30 +09003171 IRQF_DISABLED, "sony-laptop", &spic_dev)) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003172 dprintk("IRQ: %d - triggering: %d - "
3173 "polarity: %d - shr: %d\n",
3174 irq->irq.interrupts[0],
3175 irq->irq.triggering,
3176 irq->irq.polarity,
3177 irq->irq.sharable);
3178 spic_dev.cur_irq = irq;
3179 break;
3180 }
3181 }
3182 if (!spic_dev.cur_irq) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003183 pr_err(DRV_PFX "Failed to request_irq.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003184 result = -ENODEV;
3185 goto err_release_region;
3186 }
3187
3188 /* set resource status _SRS */
3189 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
3190 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003191 pr_err(DRV_PFX "Couldn't enable device.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003192 goto err_free_irq;
3193 }
3194
malattia@linux.it49a11de2007-04-09 19:28:56 +02003195 spic_dev.bluetooth_power = -1;
3196 /* create device attributes */
3197 result = sony_pf_add();
3198 if (result)
3199 goto err_disable_device;
3200
3201 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
3202 if (result)
3203 goto err_remove_pf;
3204
malattia@linux.it33a04452007-04-09 19:26:03 +02003205 return 0;
3206
malattia@linux.it49a11de2007-04-09 19:28:56 +02003207err_remove_pf:
3208 sony_pf_remove();
3209
3210err_disable_device:
3211 sony_pic_disable(device);
3212
malattia@linux.it33a04452007-04-09 19:26:03 +02003213err_free_irq:
3214 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
3215
3216err_release_region:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003217 release_region(spic_dev.cur_ioport->io1.minimum,
3218 spic_dev.cur_ioport->io1.address_length);
3219 if (spic_dev.cur_ioport->io2.minimum)
3220 release_region(spic_dev.cur_ioport->io2.minimum,
3221 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02003222
Mattia Dongili015a9162007-08-12 16:20:27 +09003223err_remove_compat:
3224 sonypi_compat_exit();
3225
malattia@linux.it33a04452007-04-09 19:26:03 +02003226err_remove_input:
malattia@linux.it1549ee62007-04-09 10:19:08 +02003227 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02003228
3229err_free_resources:
3230 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
3231 list_del(&io->list);
3232 kfree(io);
3233 }
3234 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
3235 list_del(&irq->list);
3236 kfree(irq);
3237 }
3238 spic_dev.cur_ioport = NULL;
3239 spic_dev.cur_irq = NULL;
3240
3241 return result;
3242}
3243
3244static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
3245{
3246 if (sony_pic_disable(device))
3247 return -ENXIO;
3248 return 0;
3249}
3250
3251static int sony_pic_resume(struct acpi_device *device)
3252{
3253 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
3254 return 0;
3255}
3256
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003257static const struct acpi_device_id sony_pic_device_ids[] = {
3258 {SONY_PIC_HID, 0},
3259 {"", 0},
3260};
3261
malattia@linux.it33a04452007-04-09 19:26:03 +02003262static struct acpi_driver sony_pic_driver = {
3263 .name = SONY_PIC_DRIVER_NAME,
3264 .class = SONY_PIC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003265 .ids = sony_pic_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02003266 .owner = THIS_MODULE,
3267 .ops = {
3268 .add = sony_pic_add,
3269 .remove = sony_pic_remove,
3270 .suspend = sony_pic_suspend,
3271 .resume = sony_pic_resume,
3272 },
3273};
3274
3275static struct dmi_system_id __initdata sonypi_dmi_table[] = {
3276 {
3277 .ident = "Sony Vaio",
3278 .matches = {
3279 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
3280 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
3281 },
3282 },
3283 {
3284 .ident = "Sony Vaio",
3285 .matches = {
3286 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
3287 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
3288 },
3289 },
3290 { }
3291};
3292
malattia@linux.it59b19102007-04-09 10:19:04 +02003293static int __init sony_laptop_init(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01003294{
malattia@linux.it33a04452007-04-09 19:26:03 +02003295 int result;
3296
3297 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
3298 result = acpi_bus_register_driver(&sony_pic_driver);
3299 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003300 pr_err(DRV_PFX "Unable to register SPIC driver.");
malattia@linux.it33a04452007-04-09 19:26:03 +02003301 goto out;
3302 }
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003303 spic_drv_registered = 1;
malattia@linux.it33a04452007-04-09 19:26:03 +02003304 }
3305
3306 result = acpi_bus_register_driver(&sony_nc_driver);
3307 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003308 pr_err(DRV_PFX "Unable to register SNC driver.");
malattia@linux.it33a04452007-04-09 19:26:03 +02003309 goto out_unregister_pic;
3310 }
3311
3312 return 0;
3313
3314out_unregister_pic:
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003315 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02003316 acpi_bus_unregister_driver(&sony_pic_driver);
3317out:
3318 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +01003319}
3320
malattia@linux.it59b19102007-04-09 10:19:04 +02003321static void __exit sony_laptop_exit(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01003322{
malattia@linux.it59b19102007-04-09 10:19:04 +02003323 acpi_bus_unregister_driver(&sony_nc_driver);
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003324 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02003325 acpi_bus_unregister_driver(&sony_pic_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +01003326}
3327
malattia@linux.it59b19102007-04-09 10:19:04 +02003328module_init(sony_laptop_init);
3329module_exit(sony_laptop_exit);