blob: 33c3b8c066eebde2eb591b53fe06d5e249005255 [file] [log] [blame]
Stelian Pop7f09c432007-01-13 23:04:31 +01001/*
malattia@linux.it33a04452007-04-09 19:26:03 +02002 * ACPI Sony Notebook Control Driver (SNC and SPIC)
Stelian Pop7f09c432007-01-13 23:04:31 +01003 *
4 * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
Mattia Dongili37418342009-03-26 21:58:24 +09005 * Copyright (C) 2007-2009 Mattia Dongili <malattia@linux.it>
Stelian Pop7f09c432007-01-13 23:04:31 +01006 *
7 * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
8 * which are copyrighted by their respective authors.
9 *
malattia@linux.it33a04452007-04-09 19:26:03 +020010 * The SNY6001 driver part is based on the sonypi driver which includes
11 * material from:
12 *
13 * Copyright (C) 2001-2005 Stelian Pop <stelian@popies.net>
14 *
15 * Copyright (C) 2005 Narayanan R S <nars@kadamba.org>
16 *
Marcin Garskidb9551702007-10-19 23:22:11 +020017 * Copyright (C) 2001-2002 AlcĂ´ve <www.alcove.com>
malattia@linux.it33a04452007-04-09 19:26:03 +020018 *
19 * Copyright (C) 2001 Michael Ashley <m.ashley@unsw.edu.au>
20 *
21 * Copyright (C) 2001 Junichi Morita <jun1m@mars.dti.ne.jp>
22 *
23 * Copyright (C) 2000 Takaya Kinjo <t-kinjo@tc4.so-net.ne.jp>
24 *
25 * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
26 *
27 * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
28 *
Stelian Pop7f09c432007-01-13 23:04:31 +010029 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
33 *
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
38 *
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
42 *
43 */
44
45#include <linux/kernel.h>
46#include <linux/module.h>
47#include <linux/moduleparam.h>
48#include <linux/init.h>
49#include <linux/types.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010050#include <linux/backlight.h>
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010051#include <linux/platform_device.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010052#include <linux/err.h>
malattia@linux.it33a04452007-04-09 19:26:03 +020053#include <linux/dmi.h>
54#include <linux/pci.h>
55#include <linux/interrupt.h>
56#include <linux/delay.h>
57#include <linux/input.h>
58#include <linux/kfifo.h>
59#include <linux/workqueue.h>
60#include <linux/acpi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090061#include <linux/slab.h>
Stelian Pop7f09c432007-01-13 23:04:31 +010062#include <acpi/acpi_drivers.h>
63#include <acpi/acpi_bus.h>
64#include <asm/uaccess.h>
malattia@linux.it33a04452007-04-09 19:26:03 +020065#include <linux/sonypi.h>
malattia@linux.it1ce82c12007-04-28 23:34:36 +090066#include <linux/sony-laptop.h>
Matthew Garrett6cc056b2009-03-26 21:58:15 +090067#include <linux/rfkill.h>
Mattia Dongilia64e62a2007-05-01 11:19:53 +090068#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +020069#include <linux/poll.h>
70#include <linux/miscdevice.h>
71#endif
Stelian Pop7f09c432007-01-13 23:04:31 +010072
malattia@linux.it33a04452007-04-09 19:26:03 +020073#define DRV_PFX "sony-laptop: "
Mattia Dongilid6697932011-02-19 11:52:28 +090074#define dprintk(msg...) do { \
75 if (debug) \
76 pr_warn(DRV_PFX msg); \
malattia@linux.itb9a218b2007-04-09 10:19:06 +020077} while (0)
78
Mattia Dongili425ef5d2008-01-14 18:05:44 +090079#define SONY_LAPTOP_DRIVER_VERSION "0.6"
Alessandro Guido50f62af2007-01-13 23:04:34 +010080
malattia@linux.it33a04452007-04-09 19:26:03 +020081#define SONY_NC_CLASS "sony-nc"
82#define SONY_NC_HID "SNY5001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020083#define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
malattia@linux.it33a04452007-04-09 19:26:03 +020084
85#define SONY_PIC_CLASS "sony-pic"
86#define SONY_PIC_HID "SNY6001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020087#define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
Stelian Pop7f09c432007-01-13 23:04:31 +010088
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010089MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
malattia@linux.it33a04452007-04-09 19:26:03 +020090MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
Stelian Pop7f09c432007-01-13 23:04:31 +010091MODULE_LICENSE("GPL");
malattia@linux.it33a04452007-04-09 19:26:03 +020092MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
Stelian Pop7f09c432007-01-13 23:04:31 +010093
94static int debug;
95module_param(debug, int, 0);
96MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
Len Browna02d1c12007-02-07 15:34:02 -050097 "the development of this driver");
Stelian Pop7f09c432007-01-13 23:04:31 +010098
malattia@linux.it33a04452007-04-09 19:26:03 +020099static int no_spic; /* = 0 */
100module_param(no_spic, int, 0444);
101MODULE_PARM_DESC(no_spic,
102 "set this if you don't want to enable the SPIC device");
103
104static int compat; /* = 0 */
105module_param(compat, int, 0444);
106MODULE_PARM_DESC(compat,
malattia@linux.it7b153f32007-04-09 19:31:25 +0200107 "set this if you want to enable backward compatibility mode");
malattia@linux.it33a04452007-04-09 19:26:03 +0200108
109static unsigned long mask = 0xffffffff;
110module_param(mask, ulong, 0644);
111MODULE_PARM_DESC(mask,
112 "set this to the mask of event you want to enable (see doc)");
113
malattia@linux.it5f3d2892007-04-28 23:18:45 +0900114static int camera; /* = 0 */
115module_param(camera, int, 0444);
116MODULE_PARM_DESC(camera,
117 "set this to 1 to enable Motion Eye camera controls "
118 "(only use it if you have a C1VE or C1VN model)");
119
Mattia Dongilia64e62a2007-05-01 11:19:53 +0900120#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +0200121static int minor = -1;
122module_param(minor, int, 0);
123MODULE_PARM_DESC(minor,
124 "minor number of the misc device for the SPIC compatibility code, "
125 "default is -1 (automatic)");
126#endif
127
Mattia Dongilibf155712011-02-19 11:52:31 +0900128static int kbd_backlight; /* = 1 */
129module_param(kbd_backlight, int, 0444);
130MODULE_PARM_DESC(kbd_backlight,
131 "set this to 0 to disable keyboard backlight, "
132 "1 to enable it (default: 0)");
133
134static int kbd_backlight_timeout; /* = 0 */
135module_param(kbd_backlight_timeout, int, 0444);
136MODULE_PARM_DESC(kbd_backlight_timeout,
137 "set this to 0 to set the default 10 seconds timeout, "
138 "1 for 30 seconds, 2 for 60 seconds and 3 to disable timeout "
139 "(default: 0)");
140
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900141enum sony_nc_rfkill {
142 SONY_WIFI,
143 SONY_BLUETOOTH,
144 SONY_WWAN,
145 SONY_WIMAX,
Johannes Berg19d337d2009-06-02 13:01:37 +0200146 N_SONY_RFKILL,
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900147};
148
Mattia Dongilid5a664a2009-12-17 00:08:35 +0900149static int sony_rfkill_handle;
Johannes Berg19d337d2009-06-02 13:01:37 +0200150static struct rfkill *sony_rfkill_devices[N_SONY_RFKILL];
151static int sony_rfkill_address[N_SONY_RFKILL] = {0x300, 0x500, 0x700, 0x900};
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900152static void sony_nc_rfkill_update(void);
153
malattia@linux.it1549ee62007-04-09 10:19:08 +0200154/*********** Input Devices ***********/
155
156#define SONY_LAPTOP_BUF_SIZE 128
157struct sony_laptop_input_s {
158 atomic_t users;
159 struct input_dev *jog_dev;
160 struct input_dev *key_dev;
Stefani Seibold45465482009-12-21 14:37:26 -0800161 struct kfifo fifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200162 spinlock_t fifo_lock;
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800163 struct timer_list release_key_timer;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200164};
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900165
malattia@linux.it1549ee62007-04-09 10:19:08 +0200166static struct sony_laptop_input_s sony_laptop_input = {
167 .users = ATOMIC_INIT(0),
168};
169
170struct sony_laptop_keypress {
171 struct input_dev *dev;
172 int key;
173};
174
Mattia Dongilibc57f862007-07-20 02:01:57 +0900175/* Correspondance table between sonypi events
176 * and input layer indexes in the keymap
177 */
178static int sony_laptop_input_index[] = {
Mattia Dongili3eb87492008-01-14 18:05:45 +0900179 -1, /* 0 no event */
180 -1, /* 1 SONYPI_EVENT_JOGDIAL_DOWN */
181 -1, /* 2 SONYPI_EVENT_JOGDIAL_UP */
182 -1, /* 3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
183 -1, /* 4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */
184 -1, /* 5 SONYPI_EVENT_JOGDIAL_PRESSED */
185 -1, /* 6 SONYPI_EVENT_JOGDIAL_RELEASED */
186 0, /* 7 SONYPI_EVENT_CAPTURE_PRESSED */
187 1, /* 8 SONYPI_EVENT_CAPTURE_RELEASED */
188 2, /* 9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
189 3, /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
190 4, /* 11 SONYPI_EVENT_FNKEY_ESC */
191 5, /* 12 SONYPI_EVENT_FNKEY_F1 */
192 6, /* 13 SONYPI_EVENT_FNKEY_F2 */
193 7, /* 14 SONYPI_EVENT_FNKEY_F3 */
194 8, /* 15 SONYPI_EVENT_FNKEY_F4 */
195 9, /* 16 SONYPI_EVENT_FNKEY_F5 */
196 10, /* 17 SONYPI_EVENT_FNKEY_F6 */
197 11, /* 18 SONYPI_EVENT_FNKEY_F7 */
198 12, /* 19 SONYPI_EVENT_FNKEY_F8 */
199 13, /* 20 SONYPI_EVENT_FNKEY_F9 */
200 14, /* 21 SONYPI_EVENT_FNKEY_F10 */
201 15, /* 22 SONYPI_EVENT_FNKEY_F11 */
202 16, /* 23 SONYPI_EVENT_FNKEY_F12 */
203 17, /* 24 SONYPI_EVENT_FNKEY_1 */
204 18, /* 25 SONYPI_EVENT_FNKEY_2 */
205 19, /* 26 SONYPI_EVENT_FNKEY_D */
206 20, /* 27 SONYPI_EVENT_FNKEY_E */
207 21, /* 28 SONYPI_EVENT_FNKEY_F */
208 22, /* 29 SONYPI_EVENT_FNKEY_S */
209 23, /* 30 SONYPI_EVENT_FNKEY_B */
210 24, /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */
211 25, /* 32 SONYPI_EVENT_PKEY_P1 */
212 26, /* 33 SONYPI_EVENT_PKEY_P2 */
213 27, /* 34 SONYPI_EVENT_PKEY_P3 */
214 28, /* 35 SONYPI_EVENT_BACK_PRESSED */
215 -1, /* 36 SONYPI_EVENT_LID_CLOSED */
216 -1, /* 37 SONYPI_EVENT_LID_OPENED */
217 29, /* 38 SONYPI_EVENT_BLUETOOTH_ON */
218 30, /* 39 SONYPI_EVENT_BLUETOOTH_OFF */
219 31, /* 40 SONYPI_EVENT_HELP_PRESSED */
220 32, /* 41 SONYPI_EVENT_FNKEY_ONLY */
221 33, /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
222 34, /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */
223 35, /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
224 36, /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
225 37, /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
226 38, /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */
227 39, /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
228 40, /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
229 41, /* 50 SONYPI_EVENT_ZOOM_PRESSED */
230 42, /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */
231 43, /* 52 SONYPI_EVENT_MEYE_FACE */
232 44, /* 53 SONYPI_EVENT_MEYE_OPPOSITE */
233 45, /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */
234 46, /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */
235 -1, /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */
236 -1, /* 57 SONYPI_EVENT_BATTERY_INSERT */
237 -1, /* 58 SONYPI_EVENT_BATTERY_REMOVE */
238 -1, /* 59 SONYPI_EVENT_FNKEY_RELEASED */
239 47, /* 60 SONYPI_EVENT_WIRELESS_ON */
240 48, /* 61 SONYPI_EVENT_WIRELESS_OFF */
241 49, /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */
242 50, /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900243 51, /* 64 SONYPI_EVENT_CD_EJECT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900244 52, /* 65 SONYPI_EVENT_MODEKEY_PRESSED */
245 53, /* 66 SONYPI_EVENT_PKEY_P4 */
246 54, /* 67 SONYPI_EVENT_PKEY_P5 */
247 55, /* 68 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900248 56, /* 69 SONYPI_EVENT_VOLUME_INC_PRESSED */
249 57, /* 70 SONYPI_EVENT_VOLUME_DEC_PRESSED */
250 -1, /* 71 SONYPI_EVENT_BRIGHTNESS_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900251 58, /* 72 SONYPI_EVENT_MEDIA_PRESSED */
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900252 59, /* 72 SONYPI_EVENT_VENDOR_PRESSED */
Mattia Dongilibc57f862007-07-20 02:01:57 +0900253};
254
255static int sony_laptop_input_keycode_map[] = {
256 KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
257 KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
258 KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
259 KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
260 KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
261 KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
262 KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
263 KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
264 KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
265 KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
266 KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
267 KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
268 KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
269 KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
270 KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
271 KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
272 KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
273 KEY_FN_F1, /* 17 SONYPI_EVENT_FNKEY_1 */
274 KEY_FN_F2, /* 18 SONYPI_EVENT_FNKEY_2 */
275 KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
276 KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
277 KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
278 KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
279 KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
280 KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
281 KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
282 KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
283 KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
284 KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
285 KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
286 KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
287 KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
288 KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
289 KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
290 KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
291 KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
292 KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
293 KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
294 KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
295 KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
296 KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
297 KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
298 BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
299 KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
300 KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
301 KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
302 KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
303 KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
304 KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
Mattia Dongili3eb87492008-01-14 18:05:45 +0900305 KEY_ZOOMIN, /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900306 KEY_ZOOMOUT, /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900307 KEY_EJECTCD, /* 51 SONYPI_EVENT_CD_EJECT_PRESSED */
308 KEY_F13, /* 52 SONYPI_EVENT_MODEKEY_PRESSED */
309 KEY_PROG4, /* 53 SONYPI_EVENT_PKEY_P4 */
310 KEY_F14, /* 54 SONYPI_EVENT_PKEY_P5 */
311 KEY_F15, /* 55 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900312 KEY_VOLUMEUP, /* 56 SONYPI_EVENT_VOLUME_INC_PRESSED */
313 KEY_VOLUMEDOWN, /* 57 SONYPI_EVENT_VOLUME_DEC_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900314 KEY_MEDIA, /* 58 SONYPI_EVENT_MEDIA_PRESSED */
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900315 KEY_VENDOR, /* 59 SONYPI_EVENT_VENDOR_PRESSED */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200316};
317
318/* release buttons after a short delay if pressed */
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800319static void do_sony_laptop_release_key(unsigned long unused)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200320{
321 struct sony_laptop_keypress kp;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800322 unsigned long flags;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200323
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800324 spin_lock_irqsave(&sony_laptop_input.fifo_lock, flags);
325
326 if (kfifo_out(&sony_laptop_input.fifo,
327 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200328 input_report_key(kp.dev, kp.key, 0);
329 input_sync(kp.dev);
330 }
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800331
332 /* If there is something in the fifo schedule next release. */
333 if (kfifo_len(&sony_laptop_input.fifo) != 0)
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800334 mod_timer(&sony_laptop_input.release_key_timer,
335 jiffies + msecs_to_jiffies(10));
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800336
337 spin_unlock_irqrestore(&sony_laptop_input.fifo_lock, flags);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200338}
malattia@linux.it1549ee62007-04-09 10:19:08 +0200339
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200340/* forward event to the input subsystem */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200341static void sony_laptop_report_input_event(u8 event)
342{
343 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
344 struct input_dev *key_dev = sony_laptop_input.key_dev;
345 struct sony_laptop_keypress kp = { NULL };
malattia@linux.it1549ee62007-04-09 10:19:08 +0200346
Almer S. Tigelaara83021a2009-04-12 11:26:28 +0000347 if (event == SONYPI_EVENT_FNKEY_RELEASED ||
348 event == SONYPI_EVENT_ANYBUTTON_RELEASED) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200349 /* Nothing, not all VAIOs generate this event */
350 return;
351 }
352
353 /* report events */
354 switch (event) {
355 /* jog_dev events */
356 case SONYPI_EVENT_JOGDIAL_UP:
357 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
358 input_report_rel(jog_dev, REL_WHEEL, 1);
359 input_sync(jog_dev);
360 return;
361
362 case SONYPI_EVENT_JOGDIAL_DOWN:
363 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
364 input_report_rel(jog_dev, REL_WHEEL, -1);
365 input_sync(jog_dev);
366 return;
367
368 /* key_dev events */
369 case SONYPI_EVENT_JOGDIAL_PRESSED:
370 kp.key = BTN_MIDDLE;
371 kp.dev = jog_dev;
372 break;
373
374 default:
Adrian Bunkd399d132008-02-20 00:59:03 +0200375 if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
Mattia Dongilibc57f862007-07-20 02:01:57 +0900376 dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
377 break;
378 }
379 if (sony_laptop_input_index[event] != -1) {
380 kp.key = sony_laptop_input_keycode_map[sony_laptop_input_index[event]];
381 if (kp.key != KEY_UNKNOWN)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200382 kp.dev = key_dev;
Mattia Dongilibc57f862007-07-20 02:01:57 +0900383 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200384 break;
385 }
386
387 if (kp.dev) {
388 input_report_key(kp.dev, kp.key, 1);
Mattia Dongilibc57f862007-07-20 02:01:57 +0900389 /* we emit the scancode so we can always remap the key */
390 input_event(kp.dev, EV_MSC, MSC_SCAN, event);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200391 input_sync(kp.dev);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200392
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800393 /* schedule key release */
394 kfifo_in_locked(&sony_laptop_input.fifo,
395 (unsigned char *)&kp, sizeof(kp),
396 &sony_laptop_input.fifo_lock);
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800397 mod_timer(&sony_laptop_input.release_key_timer,
398 jiffies + msecs_to_jiffies(10));
malattia@linux.it1549ee62007-04-09 10:19:08 +0200399 } else
400 dprintk("unknown input event %.2x\n", event);
401}
402
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500403static int sony_laptop_setup_input(struct acpi_device *acpi_device)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200404{
405 struct input_dev *jog_dev;
406 struct input_dev *key_dev;
407 int i;
408 int error;
409
410 /* don't run again if already initialized */
411 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
412 return 0;
413
414 /* kfifo */
415 spin_lock_init(&sony_laptop_input.fifo_lock);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800416 error = kfifo_alloc(&sony_laptop_input.fifo,
417 SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -0800418 if (error) {
Mattia Dongilid6697932011-02-19 11:52:28 +0900419 pr_err(DRV_PFX "kfifo_alloc failed\n");
malattia@linux.it1549ee62007-04-09 10:19:08 +0200420 goto err_dec_users;
421 }
422
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800423 setup_timer(&sony_laptop_input.release_key_timer,
424 do_sony_laptop_release_key, 0);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200425
426 /* input keys */
427 key_dev = input_allocate_device();
428 if (!key_dev) {
429 error = -ENOMEM;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800430 goto err_free_kfifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200431 }
432
433 key_dev->name = "Sony Vaio Keys";
434 key_dev->id.bustype = BUS_ISA;
435 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500436 key_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200437
438 /* Initialize the Input Drivers: special keys */
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800439 input_set_capability(key_dev, EV_MSC, MSC_SCAN);
440
441 __set_bit(EV_KEY, key_dev->evbit);
Mattia Dongilibc57f862007-07-20 02:01:57 +0900442 key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
443 key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
444 key_dev->keycode = &sony_laptop_input_keycode_map;
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800445 for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++)
446 __set_bit(sony_laptop_input_keycode_map[i], key_dev->keybit);
447 __clear_bit(KEY_RESERVED, key_dev->keybit);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200448
449 error = input_register_device(key_dev);
450 if (error)
451 goto err_free_keydev;
452
453 sony_laptop_input.key_dev = key_dev;
454
455 /* jogdial */
456 jog_dev = input_allocate_device();
457 if (!jog_dev) {
458 error = -ENOMEM;
459 goto err_unregister_keydev;
460 }
461
462 jog_dev->name = "Sony Vaio Jogdial";
463 jog_dev->id.bustype = BUS_ISA;
464 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500465 key_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200466
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800467 input_set_capability(jog_dev, EV_KEY, BTN_MIDDLE);
468 input_set_capability(jog_dev, EV_REL, REL_WHEEL);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200469
470 error = input_register_device(jog_dev);
471 if (error)
472 goto err_free_jogdev;
473
474 sony_laptop_input.jog_dev = jog_dev;
475
476 return 0;
477
478err_free_jogdev:
479 input_free_device(jog_dev);
480
481err_unregister_keydev:
482 input_unregister_device(key_dev);
483 /* to avoid kref underflow below at input_free_device */
484 key_dev = NULL;
485
486err_free_keydev:
487 input_free_device(key_dev);
488
malattia@linux.it1549ee62007-04-09 10:19:08 +0200489err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -0800490 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200491
492err_dec_users:
493 atomic_dec(&sony_laptop_input.users);
494 return error;
495}
496
497static void sony_laptop_remove_input(void)
498{
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800499 struct sony_laptop_keypress kp = { NULL };
500
501 /* Cleanup only after the last user has gone */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200502 if (!atomic_dec_and_test(&sony_laptop_input.users))
503 return;
504
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800505 del_timer_sync(&sony_laptop_input.release_key_timer);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800506
507 /*
508 * Generate key-up events for remaining keys. Note that we don't
509 * need locking since nobody is adding new events to the kfifo.
510 */
511 while (kfifo_out(&sony_laptop_input.fifo,
512 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
513 input_report_key(kp.dev, kp.key, 0);
514 input_sync(kp.dev);
515 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200516
517 /* destroy input devs */
518 input_unregister_device(sony_laptop_input.key_dev);
519 sony_laptop_input.key_dev = NULL;
520
521 if (sony_laptop_input.jog_dev) {
522 input_unregister_device(sony_laptop_input.jog_dev);
523 sony_laptop_input.jog_dev = NULL;
524 }
525
Stefani Seibold45465482009-12-21 14:37:26 -0800526 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200527}
528
malattia@linux.it56b87562007-04-09 10:19:05 +0200529/*********** Platform Device ***********/
530
531static atomic_t sony_pf_users = ATOMIC_INIT(0);
532static struct platform_driver sony_pf_driver = {
533 .driver = {
534 .name = "sony-laptop",
535 .owner = THIS_MODULE,
536 }
537};
538static struct platform_device *sony_pf_device;
539
540static int sony_pf_add(void)
541{
542 int ret = 0;
543
544 /* don't run again if already initialized */
545 if (atomic_add_return(1, &sony_pf_users) > 1)
546 return 0;
547
548 ret = platform_driver_register(&sony_pf_driver);
549 if (ret)
550 goto out;
551
552 sony_pf_device = platform_device_alloc("sony-laptop", -1);
553 if (!sony_pf_device) {
554 ret = -ENOMEM;
555 goto out_platform_registered;
556 }
557
558 ret = platform_device_add(sony_pf_device);
559 if (ret)
560 goto out_platform_alloced;
561
562 return 0;
563
564 out_platform_alloced:
565 platform_device_put(sony_pf_device);
566 sony_pf_device = NULL;
567 out_platform_registered:
568 platform_driver_unregister(&sony_pf_driver);
569 out:
570 atomic_dec(&sony_pf_users);
571 return ret;
572}
573
574static void sony_pf_remove(void)
575{
576 /* deregister only after the last user has gone */
577 if (!atomic_dec_and_test(&sony_pf_users))
578 return;
579
Axel Lin08db2b32010-07-01 10:18:01 +0800580 platform_device_unregister(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +0200581 platform_driver_unregister(&sony_pf_driver);
582}
583
584/*********** SNC (SNY5001) Device ***********/
585
malattia@linux.it33a04452007-04-09 19:26:03 +0200586/* the device uses 1-based values, while the backlight subsystem uses
587 0-based values */
588#define SONY_MAX_BRIGHTNESS 8
589
590#define SNC_VALIDATE_IN 0
591#define SNC_VALIDATE_OUT 1
592
malattia@linux.it59b19102007-04-09 10:19:04 +0200593static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500594 char *);
malattia@linux.it59b19102007-04-09 10:19:04 +0200595static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500596 const char *, size_t);
Mattia Dongili156c2212007-02-12 22:01:07 +0100597static int boolean_validate(const int, const int);
598static int brightness_default_validate(const int, const int);
599
malattia@linux.it59b19102007-04-09 10:19:04 +0200600struct sony_nc_value {
Len Browna02d1c12007-02-07 15:34:02 -0500601 char *name; /* name of the entry */
602 char **acpiget; /* names of the ACPI get function */
603 char **acpiset; /* names of the ACPI set function */
Mattia Dongili156c2212007-02-12 22:01:07 +0100604 int (*validate)(const int, const int); /* input/output validation */
Len Browna02d1c12007-02-07 15:34:02 -0500605 int value; /* current setting */
606 int valid; /* Has ever been set */
607 int debug; /* active only in debug mode ? */
Lucas De Marchic8440332011-03-17 17:18:22 -0300608 struct device_attribute devattr; /* sysfs attribute */
Stelian Pop7f09c432007-01-13 23:04:31 +0100609};
610
malattia@linux.it59b19102007-04-09 10:19:04 +0200611#define SNC_HANDLE_NAMES(_name, _values...) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100612 static char *snc_##_name[] = { _values, NULL }
613
malattia@linux.it59b19102007-04-09 10:19:04 +0200614#define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100615 { \
616 .name = __stringify(_name), \
617 .acpiget = _getters, \
618 .acpiset = _setters, \
Mattia Dongili156c2212007-02-12 22:01:07 +0100619 .validate = _validate, \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100620 .debug = _debug, \
malattia@linux.it59b19102007-04-09 10:19:04 +0200621 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100622 }
623
malattia@linux.it59b19102007-04-09 10:19:04 +0200624#define SNC_HANDLE_NULL { .name = NULL }
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100625
malattia@linux.it59b19102007-04-09 10:19:04 +0200626SNC_HANDLE_NAMES(fnkey_get, "GHKE");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100627
malattia@linux.it59b19102007-04-09 10:19:04 +0200628SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
629SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100630
malattia@linux.it59b19102007-04-09 10:19:04 +0200631SNC_HANDLE_NAMES(cdpower_get, "GCDP");
632SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100633
malattia@linux.it59b19102007-04-09 10:19:04 +0200634SNC_HANDLE_NAMES(audiopower_get, "GAZP");
635SNC_HANDLE_NAMES(audiopower_set, "AZPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100636
malattia@linux.it59b19102007-04-09 10:19:04 +0200637SNC_HANDLE_NAMES(lanpower_get, "GLNP");
638SNC_HANDLE_NAMES(lanpower_set, "LNPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100639
Mattia Dongili044847e2007-07-16 02:34:33 +0900640SNC_HANDLE_NAMES(lidstate_get, "GLID");
641
642SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
643SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
644
645SNC_HANDLE_NAMES(gainbass_get, "GMGB");
646SNC_HANDLE_NAMES(gainbass_set, "CMGB");
647
malattia@linux.it59b19102007-04-09 10:19:04 +0200648SNC_HANDLE_NAMES(PID_get, "GPID");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100649
malattia@linux.it59b19102007-04-09 10:19:04 +0200650SNC_HANDLE_NAMES(CTR_get, "GCTR");
651SNC_HANDLE_NAMES(CTR_set, "SCTR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100652
malattia@linux.it59b19102007-04-09 10:19:04 +0200653SNC_HANDLE_NAMES(PCR_get, "GPCR");
654SNC_HANDLE_NAMES(PCR_set, "SPCR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100655
malattia@linux.it59b19102007-04-09 10:19:04 +0200656SNC_HANDLE_NAMES(CMI_get, "GCMI");
657SNC_HANDLE_NAMES(CMI_set, "SCMI");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100658
malattia@linux.it59b19102007-04-09 10:19:04 +0200659static struct sony_nc_value sony_nc_values[] = {
660 SNC_HANDLE(brightness_default, snc_brightness_def_get,
Mattia Dongili156c2212007-02-12 22:01:07 +0100661 snc_brightness_def_set, brightness_default_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200662 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
663 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
664 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100665 boolean_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200666 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100667 boolean_validate, 1),
Mattia Dongili044847e2007-07-16 02:34:33 +0900668 SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
669 boolean_validate, 0),
670 SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
671 boolean_validate, 0),
672 SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
673 boolean_validate, 0),
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100674 /* unknown methods */
malattia@linux.it59b19102007-04-09 10:19:04 +0200675 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
676 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
677 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
678 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
679 SNC_HANDLE_NULL
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100680};
681
malattia@linux.it59b19102007-04-09 10:19:04 +0200682static acpi_handle sony_nc_acpi_handle;
683static struct acpi_device *sony_nc_acpi_device = NULL;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100684
Mattia Dongilid78865c2007-02-07 20:01:56 +0100685/*
686 * acpi_evaluate_object wrappers
687 */
Stelian Pop7f09c432007-01-13 23:04:31 +0100688static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
689{
690 struct acpi_buffer output;
691 union acpi_object out_obj;
692 acpi_status status;
693
694 output.length = sizeof(out_obj);
695 output.pointer = &out_obj;
696
697 status = acpi_evaluate_object(handle, name, NULL, &output);
698 if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
699 *result = out_obj.integer.value;
700 return 0;
701 }
702
Mattia Dongilid6697932011-02-19 11:52:28 +0900703 pr_warn(DRV_PFX "acpi_callreadfunc failed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100704
705 return -1;
706}
707
708static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
709 int *result)
710{
711 struct acpi_object_list params;
712 union acpi_object in_obj;
713 struct acpi_buffer output;
714 union acpi_object out_obj;
715 acpi_status status;
716
717 params.count = 1;
718 params.pointer = &in_obj;
719 in_obj.type = ACPI_TYPE_INTEGER;
720 in_obj.integer.value = value;
721
722 output.length = sizeof(out_obj);
723 output.pointer = &out_obj;
724
725 status = acpi_evaluate_object(handle, name, &params, &output);
726 if (status == AE_OK) {
727 if (result != NULL) {
728 if (out_obj.type != ACPI_TYPE_INTEGER) {
Mattia Dongilid6697932011-02-19 11:52:28 +0900729 pr_warn(DRV_PFX "acpi_evaluate_object bad "
Stelian Pop7f09c432007-01-13 23:04:31 +0100730 "return type\n");
731 return -1;
732 }
733 *result = out_obj.integer.value;
734 }
735 return 0;
736 }
737
Mattia Dongilid6697932011-02-19 11:52:28 +0900738 pr_warn(DRV_PFX "acpi_evaluate_object failed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100739
740 return -1;
741}
742
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900743struct sony_nc_handles {
744 u16 cap[0x10];
745 struct device_attribute devattr;
746};
747
Dan Carpenterf11113b2011-02-26 15:54:27 +0300748static struct sony_nc_handles *handles;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900749
750static ssize_t sony_nc_handles_show(struct device *dev,
751 struct device_attribute *attr, char *buffer)
752{
753 ssize_t len = 0;
754 int i;
755
756 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
757 len += snprintf(buffer + len, PAGE_SIZE - len, "0x%.4x ",
758 handles->cap[i]);
759 }
760 len += snprintf(buffer + len, PAGE_SIZE - len, "\n");
761
762 return len;
763}
764
765static int sony_nc_handles_setup(struct platform_device *pd)
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900766{
767 int i;
768 int result;
769
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900770 handles = kzalloc(sizeof(*handles), GFP_KERNEL);
Dan Carpenter31f00752011-02-26 15:55:24 +0300771 if (!handles)
772 return -ENOMEM;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900773
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;
Mattia Dongilifef34862011-04-02 19:00:44 +0900811
812 /* not initialized yet, return early */
813 if (!handles)
814 return -1;
815
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900816 for (i = 0; i < 0x10; i++) {
817 if (handles->cap[i] == handle) {
818 dprintk("found handle 0x%.4x (offset: 0x%.2x)\n",
819 handle, i);
820 return i;
821 }
822 }
Mattia Dongili56e6e712011-02-19 11:52:25 +0900823 dprintk("handle 0x%.4x not found\n", handle);
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900824 return -1;
825}
826
827static int sony_call_snc_handle(int handle, int argument, int *result)
828{
Mattia Dongili56e6e712011-02-19 11:52:25 +0900829 int ret = 0;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900830 int offset = sony_find_snc_handle(handle);
831
832 if (offset < 0)
833 return -1;
834
Mattia Dongili56e6e712011-02-19 11:52:25 +0900835 ret = acpi_callsetfunc(sony_nc_acpi_handle, "SN07", offset | argument,
836 result);
837 dprintk("called SN07 with 0x%.4x (result: 0x%.4x)\n", offset | argument,
838 *result);
839 return ret;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900840}
841
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100842/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200843 * sony_nc_values input/output validate functions
Mattia Dongili156c2212007-02-12 22:01:07 +0100844 */
845
846/* brightness_default_validate:
847 *
848 * manipulate input output values to keep consistency with the
849 * backlight framework for which brightness values are 0-based.
850 */
851static int brightness_default_validate(const int direction, const int value)
852{
853 switch (direction) {
854 case SNC_VALIDATE_OUT:
855 return value - 1;
856 case SNC_VALIDATE_IN:
857 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
858 return value + 1;
859 }
860 return -EINVAL;
861}
862
863/* boolean_validate:
864 *
865 * on input validate boolean values 0/1, on output just pass the
866 * received value.
867 */
868static int boolean_validate(const int direction, const int value)
869{
870 if (direction == SNC_VALIDATE_IN) {
871 if (value != 0 && value != 1)
872 return -EINVAL;
873 }
874 return value;
875}
876
877/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200878 * Sysfs show/store common to all sony_nc_values
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100879 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200880static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
Len Browna02d1c12007-02-07 15:34:02 -0500881 char *buffer)
Stelian Pop7f09c432007-01-13 23:04:31 +0100882{
Stelian Pop7f09c432007-01-13 23:04:31 +0100883 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200884 struct sony_nc_value *item =
885 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100886
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100887 if (!*item->acpiget)
Stelian Pop7f09c432007-01-13 23:04:31 +0100888 return -EIO;
889
malattia@linux.it59b19102007-04-09 10:19:04 +0200890 if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100891 return -EIO;
892
Mattia Dongili156c2212007-02-12 22:01:07 +0100893 if (item->validate)
894 value = item->validate(SNC_VALIDATE_OUT, value);
895
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100896 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
Stelian Pop7f09c432007-01-13 23:04:31 +0100897}
898
malattia@linux.it59b19102007-04-09 10:19:04 +0200899static ssize_t sony_nc_sysfs_store(struct device *dev,
Len Browna02d1c12007-02-07 15:34:02 -0500900 struct device_attribute *attr,
901 const char *buffer, size_t count)
Stelian Pop7f09c432007-01-13 23:04:31 +0100902{
Stelian Pop7f09c432007-01-13 23:04:31 +0100903 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200904 struct sony_nc_value *item =
905 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100906
907 if (!item->acpiset)
908 return -EIO;
909
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100910 if (count > 31)
911 return -EINVAL;
912
913 value = simple_strtoul(buffer, NULL, 10);
Stelian Pop7f09c432007-01-13 23:04:31 +0100914
Mattia Dongili156c2212007-02-12 22:01:07 +0100915 if (item->validate)
916 value = item->validate(SNC_VALIDATE_IN, value);
917
918 if (value < 0)
919 return value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100920
malattia@linux.it59b19102007-04-09 10:19:04 +0200921 if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100922 return -EIO;
Andrew Morton3f4f4612007-01-13 23:04:32 +0100923 item->value = value;
924 item->valid = 1;
Stelian Pop7f09c432007-01-13 23:04:31 +0100925 return count;
926}
927
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100928
Mattia Dongilid78865c2007-02-07 20:01:56 +0100929/*
930 * Backlight device
931 */
932static int sony_backlight_update_status(struct backlight_device *bd)
Andrew Morton3f4f4612007-01-13 23:04:32 +0100933{
malattia@linux.it59b19102007-04-09 10:19:04 +0200934 return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
Richard Purdie321709c2007-02-10 15:04:08 +0000935 bd->props.brightness + 1, NULL);
Andrew Morton3f4f4612007-01-13 23:04:32 +0100936}
937
Mattia Dongilid78865c2007-02-07 20:01:56 +0100938static int sony_backlight_get_brightness(struct backlight_device *bd)
939{
940 int value;
941
malattia@linux.it59b19102007-04-09 10:19:04 +0200942 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
Mattia Dongilid78865c2007-02-07 20:01:56 +0100943 return 0;
944 /* brightness levels are 1-based, while backlight ones are 0-based */
945 return value - 1;
946}
947
Mattia Dongili7751ab82011-02-19 11:52:32 +0900948static int sony_nc_get_brightness_ng(struct backlight_device *bd)
949{
950 int result;
951 int *handle = (int *)bl_get_data(bd);
952
953 sony_call_snc_handle(*handle, 0x0200, &result);
954
955 return result & 0xff;
956}
957
958static int sony_nc_update_status_ng(struct backlight_device *bd)
959{
960 int value, result;
961 int *handle = (int *)bl_get_data(bd);
962
963 value = bd->props.brightness;
964 sony_call_snc_handle(*handle, 0x0100 | (value << 16), &result);
965
966 return sony_nc_get_brightness_ng(bd);
967}
968
Lionel Debrouxacc24722010-11-16 14:14:02 +0100969static const struct backlight_ops sony_backlight_ops = {
Mattia Dongili7751ab82011-02-19 11:52:32 +0900970 .options = BL_CORE_SUSPENDRESUME,
Len Browna02d1c12007-02-07 15:34:02 -0500971 .update_status = sony_backlight_update_status,
972 .get_brightness = sony_backlight_get_brightness,
Mattia Dongilid78865c2007-02-07 20:01:56 +0100973};
Mattia Dongili7751ab82011-02-19 11:52:32 +0900974static const struct backlight_ops sony_backlight_ng_ops = {
975 .options = BL_CORE_SUSPENDRESUME,
976 .update_status = sony_nc_update_status_ng,
977 .get_brightness = sony_nc_get_brightness_ng,
978};
979static int backlight_ng_handle;
980static struct backlight_device *sony_backlight_device;
Mattia Dongilid78865c2007-02-07 20:01:56 +0100981
982/*
Mattia Dongili6315fd12007-07-16 02:34:35 +0900983 * New SNC-only Vaios event mapping to driver known keys
984 */
985struct sony_nc_event {
986 u8 data;
987 u8 event;
988};
989
Matthew Garrett45c79422009-03-26 21:58:16 +0900990static struct sony_nc_event sony_100_events[] = {
Matthew Garrett9b578962009-03-26 21:58:14 +0900991 { 0x90, SONYPI_EVENT_PKEY_P1 },
992 { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthias Welwarsky6479efb2009-04-01 22:10:45 +0900993 { 0x91, SONYPI_EVENT_PKEY_P2 },
Matthew Garrett9b578962009-03-26 21:58:14 +0900994 { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +0900995 { 0x81, SONYPI_EVENT_FNKEY_F1 },
996 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
Anton Veretenenkof5acf5e2009-03-26 22:44:26 +0900997 { 0x82, SONYPI_EVENT_FNKEY_F2 },
998 { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
999 { 0x83, SONYPI_EVENT_FNKEY_F3 },
1000 { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
1001 { 0x84, SONYPI_EVENT_FNKEY_F4 },
1002 { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001003 { 0x85, SONYPI_EVENT_FNKEY_F5 },
1004 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
1005 { 0x86, SONYPI_EVENT_FNKEY_F6 },
1006 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
1007 { 0x87, SONYPI_EVENT_FNKEY_F7 },
1008 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +09001009 { 0x89, SONYPI_EVENT_FNKEY_F9 },
1010 { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001011 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
1012 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
1013 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
1014 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +09001015 { 0x9d, SONYPI_EVENT_ZOOM_PRESSED },
1016 { 0x1d, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +09001017 { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
1018 { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili4f924ba2009-12-17 00:08:33 +09001019 { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
1020 { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +09001021 { 0xa4, SONYPI_EVENT_CD_EJECT_PRESSED },
1022 { 0x24, SONYPI_EVENT_ANYBUTTON_RELEASED },
1023 { 0xa5, SONYPI_EVENT_VENDOR_PRESSED },
1024 { 0x25, SONYPI_EVENT_ANYBUTTON_RELEASED },
1025 { 0xa6, SONYPI_EVENT_HELP_PRESSED },
1026 { 0x26, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001027 { 0, 0 },
1028};
1029
Matthew Garrett45c79422009-03-26 21:58:16 +09001030static struct sony_nc_event sony_127_events[] = {
1031 { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
1032 { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
1033 { 0x82, SONYPI_EVENT_PKEY_P1 },
1034 { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
1035 { 0x83, SONYPI_EVENT_PKEY_P2 },
1036 { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
1037 { 0x84, SONYPI_EVENT_PKEY_P3 },
1038 { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
1039 { 0x85, SONYPI_EVENT_PKEY_P4 },
1040 { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
1041 { 0x86, SONYPI_EVENT_PKEY_P5 },
1042 { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett45c79422009-03-26 21:58:16 +09001043 { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
1044 { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
1045 { 0, 0 },
1046};
1047
Mattia Dongili6315fd12007-07-16 02:34:35 +09001048/*
Mattia Dongilid78865c2007-02-07 20:01:56 +01001049 * ACPI callbacks
1050 */
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001051static void sony_nc_notify(struct acpi_device *device, u32 event)
Stelian Pop7f09c432007-01-13 23:04:31 +01001052{
Mattia Dongili6315fd12007-07-16 02:34:35 +09001053 u32 ev = event;
Mattia Dongili6315fd12007-07-16 02:34:35 +09001054
Matthew Garrett9b578962009-03-26 21:58:14 +09001055 if (ev >= 0x90) {
1056 /* New-style event */
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001057 int result;
Matthew Garrett45c79422009-03-26 21:58:16 +09001058 int key_handle = 0;
Matthew Garrett9b578962009-03-26 21:58:14 +09001059 ev -= 0x90;
Mattia Dongili6315fd12007-07-16 02:34:35 +09001060
Matthew Garrett45c79422009-03-26 21:58:16 +09001061 if (sony_find_snc_handle(0x100) == ev)
1062 key_handle = 0x100;
1063 if (sony_find_snc_handle(0x127) == ev)
1064 key_handle = 0x127;
Matthew Garrett9b578962009-03-26 21:58:14 +09001065
Matthias Welwarsky6479efb2009-04-01 22:10:45 +09001066 if (key_handle) {
Matthew Garrett45c79422009-03-26 21:58:16 +09001067 struct sony_nc_event *key_event;
1068
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001069 if (sony_call_snc_handle(key_handle, 0x200, &result)) {
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001070 dprintk("sony_nc_notify, unable to decode"
Matthew Garrett45c79422009-03-26 21:58:16 +09001071 " event 0x%.2x 0x%.2x\n", key_handle,
1072 ev);
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001073 /* restore the original event */
1074 ev = event;
1075 } else {
Matthew Garrett9b578962009-03-26 21:58:14 +09001076 ev = result & 0xFF;
1077
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001078 if (key_handle == 0x100)
1079 key_event = sony_100_events;
1080 else
1081 key_event = sony_127_events;
Matthew Garrett45c79422009-03-26 21:58:16 +09001082
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001083 for (; key_event->data; key_event++) {
1084 if (key_event->data == ev) {
1085 ev = key_event->event;
1086 break;
1087 }
Matthew Garrett9b578962009-03-26 21:58:14 +09001088 }
Mattia Dongili6315fd12007-07-16 02:34:35 +09001089
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001090 if (!key_event->data)
Mattia Dongilid6697932011-02-19 11:52:28 +09001091 pr_info(DRV_PFX
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001092 "Unknown event: 0x%x 0x%x\n",
1093 key_handle,
1094 ev);
1095 else
1096 sony_laptop_report_input_event(ev);
Matthew Garrett45c79422009-03-26 21:58:16 +09001097 }
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001098 } else if (sony_find_snc_handle(sony_rfkill_handle) == ev) {
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001099 sony_nc_rfkill_update();
1100 return;
Matthew Garrett9b578962009-03-26 21:58:14 +09001101 }
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001102 } else
1103 sony_laptop_report_input_event(ev);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001104
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001105 dprintk("sony_nc_notify, event: 0x%.2x\n", ev);
Len Brown14e04fb2007-08-23 15:20:26 -04001106 acpi_bus_generate_proc_event(sony_nc_acpi_device, 1, ev);
Stelian Pop7f09c432007-01-13 23:04:31 +01001107}
1108
1109static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
1110 void *context, void **return_value)
1111{
Lin Ming30823732008-12-16 16:59:35 +08001112 struct acpi_device_info *info;
Stelian Pop7f09c432007-01-13 23:04:31 +01001113
Bob Moore15b8dd52009-06-29 13:39:29 +08001114 if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001115 pr_warn(DRV_PFX "method: name: %4.4s, args %X\n",
Lin Ming30823732008-12-16 16:59:35 +08001116 (char *)&info->name, info->param_count);
1117
Bob Moore15b8dd52009-06-29 13:39:29 +08001118 kfree(info);
Lin Ming30823732008-12-16 16:59:35 +08001119 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001120
1121 return AE_OK;
1122}
1123
Mattia Dongilid78865c2007-02-07 20:01:56 +01001124/*
1125 * ACPI device
1126 */
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001127static int sony_nc_function_setup(struct acpi_device *device)
1128{
1129 int result;
1130
1131 /* Enable all events */
1132 acpi_callsetfunc(sony_nc_acpi_handle, "SN02", 0xffff, &result);
1133
1134 /* Setup hotkeys */
1135 sony_call_snc_handle(0x0100, 0, &result);
1136 sony_call_snc_handle(0x0101, 0, &result);
1137 sony_call_snc_handle(0x0102, 0x100, &result);
Almer S. Tigelaar560e84a2009-04-12 11:26:27 +00001138 sony_call_snc_handle(0x0127, 0, &result);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001139
1140 return 0;
1141}
1142
malattia@linux.it59b19102007-04-09 10:19:04 +02001143static int sony_nc_resume(struct acpi_device *device)
Mattia Dongilid78865c2007-02-07 20:01:56 +01001144{
malattia@linux.it59b19102007-04-09 10:19:04 +02001145 struct sony_nc_value *item;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001146 acpi_handle handle;
Mattia Dongilid78865c2007-02-07 20:01:56 +01001147
malattia@linux.it59b19102007-04-09 10:19:04 +02001148 for (item = sony_nc_values; item->name; item++) {
Mattia Dongilid78865c2007-02-07 20:01:56 +01001149 int ret;
1150
1151 if (!item->valid)
1152 continue;
malattia@linux.it59b19102007-04-09 10:19:04 +02001153 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
Len Browna02d1c12007-02-07 15:34:02 -05001154 item->value, NULL);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001155 if (ret < 0) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001156 pr_err(DRV_PFX "%s: %d\n", __func__, ret);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001157 break;
1158 }
1159 }
Mattia Dongili6315fd12007-07-16 02:34:35 +09001160
Matthew Garrett82734bf2009-03-26 21:58:13 +09001161 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1162 &handle))) {
1163 if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
1164 dprintk("ECON Method failed\n");
1165 }
1166
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001167 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1168 &handle))) {
1169 dprintk("Doing SNC setup\n");
1170 sony_nc_function_setup(device);
1171 }
1172
Alan Jenkinsa0d97d62009-09-25 10:18:21 +01001173 /* re-read rfkill state */
1174 sony_nc_rfkill_update();
1175
Mattia Dongilid78865c2007-02-07 20:01:56 +01001176 return 0;
1177}
1178
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001179static void sony_nc_rfkill_cleanup(void)
1180{
1181 int i;
1182
Johannes Berg19d337d2009-06-02 13:01:37 +02001183 for (i = 0; i < N_SONY_RFKILL; i++) {
1184 if (sony_rfkill_devices[i]) {
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001185 rfkill_unregister(sony_rfkill_devices[i]);
Johannes Berg19d337d2009-06-02 13:01:37 +02001186 rfkill_destroy(sony_rfkill_devices[i]);
1187 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001188 }
1189}
1190
Johannes Berg19d337d2009-06-02 13:01:37 +02001191static int sony_nc_rfkill_set(void *data, bool blocked)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001192{
1193 int result;
1194 int argument = sony_rfkill_address[(long) data] + 0x100;
1195
Johannes Berg19d337d2009-06-02 13:01:37 +02001196 if (!blocked)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001197 argument |= 0xff0000;
1198
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001199 return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001200}
1201
Johannes Berg19d337d2009-06-02 13:01:37 +02001202static const struct rfkill_ops sony_rfkill_ops = {
1203 .set_block = sony_nc_rfkill_set,
1204};
1205
1206static int sony_nc_setup_rfkill(struct acpi_device *device,
1207 enum sony_nc_rfkill nc_type)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001208{
1209 int err = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001210 struct rfkill *rfk;
1211 enum rfkill_type type;
1212 const char *name;
Alan Jenkins50fab072009-09-24 20:15:24 +01001213 int result;
1214 bool hwblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001215
Johannes Berg19d337d2009-06-02 13:01:37 +02001216 switch (nc_type) {
1217 case SONY_WIFI:
1218 type = RFKILL_TYPE_WLAN;
1219 name = "sony-wifi";
1220 break;
1221 case SONY_BLUETOOTH:
1222 type = RFKILL_TYPE_BLUETOOTH;
1223 name = "sony-bluetooth";
1224 break;
1225 case SONY_WWAN:
1226 type = RFKILL_TYPE_WWAN;
1227 name = "sony-wwan";
1228 break;
1229 case SONY_WIMAX:
1230 type = RFKILL_TYPE_WIMAX;
1231 name = "sony-wimax";
1232 break;
1233 default:
1234 return -EINVAL;
Mattia Dongili53005a02009-04-12 11:26:31 +00001235 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001236
Johannes Berg19d337d2009-06-02 13:01:37 +02001237 rfk = rfkill_alloc(name, &device->dev, type,
1238 &sony_rfkill_ops, (void *)nc_type);
1239 if (!rfk)
1240 return -ENOMEM;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001241
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001242 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
Alan Jenkins50fab072009-09-24 20:15:24 +01001243 hwblock = !(result & 0x1);
1244 rfkill_set_hw_state(rfk, hwblock);
1245
Johannes Berg19d337d2009-06-02 13:01:37 +02001246 err = rfkill_register(rfk);
1247 if (err) {
1248 rfkill_destroy(rfk);
1249 return err;
Mattia Dongili53005a02009-04-12 11:26:31 +00001250 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001251 sony_rfkill_devices[nc_type] = rfk;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001252 return err;
1253}
1254
Randy Dunlapa46a7802011-01-08 19:56:44 -08001255static void sony_nc_rfkill_update(void)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001256{
Johannes Berg19d337d2009-06-02 13:01:37 +02001257 enum sony_nc_rfkill i;
1258 int result;
1259 bool hwblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001260
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001261 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001262 hwblock = !(result & 0x1);
1263
1264 for (i = 0; i < N_SONY_RFKILL; i++) {
1265 int argument = sony_rfkill_address[i];
1266
1267 if (!sony_rfkill_devices[i])
1268 continue;
1269
1270 if (hwblock) {
Johannes Berge1f8a192009-06-11 12:08:15 +02001271 if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
1272 /* we already know we're blocked */
1273 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001274 continue;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001275 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001276
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001277 sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001278 rfkill_set_states(sony_rfkill_devices[i],
1279 !(result & 0xf), false);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001280 }
1281}
1282
Mattia Dongili528809c2009-12-17 00:08:36 +09001283static void sony_nc_rfkill_setup(struct acpi_device *device)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001284{
Mattia Dongili528809c2009-12-17 00:08:36 +09001285 int offset;
1286 u8 dev_code, i;
1287 acpi_status status;
1288 struct acpi_object_list params;
1289 union acpi_object in_obj;
1290 union acpi_object *device_enum;
1291 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001292
Mattia Dongili528809c2009-12-17 00:08:36 +09001293 offset = sony_find_snc_handle(0x124);
1294 if (offset == -1) {
1295 offset = sony_find_snc_handle(0x135);
1296 if (offset == -1)
1297 return;
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001298 else
1299 sony_rfkill_handle = 0x135;
1300 } else
1301 sony_rfkill_handle = 0x124;
Mattia Dongili528809c2009-12-17 00:08:36 +09001302 dprintk("Found rkfill handle: 0x%.4x\n", sony_rfkill_handle);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001303
Mattia Dongili528809c2009-12-17 00:08:36 +09001304 /* need to read the whole buffer returned by the acpi call to SN06
1305 * here otherwise we may miss some features
1306 */
1307 params.count = 1;
1308 params.pointer = &in_obj;
1309 in_obj.type = ACPI_TYPE_INTEGER;
1310 in_obj.integer.value = offset;
1311 status = acpi_evaluate_object(sony_nc_acpi_handle, "SN06", &params,
1312 &buffer);
1313 if (ACPI_FAILURE(status)) {
1314 dprintk("Radio device enumeration failed\n");
1315 return;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001316 }
1317
Mattia Dongili528809c2009-12-17 00:08:36 +09001318 device_enum = (union acpi_object *) buffer.pointer;
Dan Carpenter200140b2011-02-27 17:13:25 +03001319 if (!device_enum) {
1320 pr_err(DRV_PFX "No SN06 return object.");
1321 goto out_no_enum;
1322 }
1323 if (device_enum->type != ACPI_TYPE_BUFFER) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001324 pr_err(DRV_PFX "Invalid SN06 return object 0x%.2x\n",
1325 device_enum->type);
Mattia Dongili528809c2009-12-17 00:08:36 +09001326 goto out_no_enum;
1327 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001328
Mattia Dongili528809c2009-12-17 00:08:36 +09001329 /* the buffer is filled with magic numbers describing the devices
1330 * available, 0xff terminates the enumeration
1331 */
Dmitry Torokhovc14973f2010-01-10 00:15:44 -08001332 for (i = 0; i < device_enum->buffer.length; i++) {
1333
1334 dev_code = *(device_enum->buffer.pointer + i);
1335 if (dev_code == 0xff)
1336 break;
1337
Mattia Dongili528809c2009-12-17 00:08:36 +09001338 dprintk("Radio devices, looking at 0x%.2x\n", dev_code);
1339
1340 if (dev_code == 0 && !sony_rfkill_devices[SONY_WIFI])
1341 sony_nc_setup_rfkill(device, SONY_WIFI);
1342
1343 if (dev_code == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
1344 sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
1345
1346 if ((0xf0 & dev_code) == 0x20 &&
1347 !sony_rfkill_devices[SONY_WWAN])
1348 sony_nc_setup_rfkill(device, SONY_WWAN);
1349
1350 if (dev_code == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
1351 sony_nc_setup_rfkill(device, SONY_WIMAX);
1352 }
1353
1354out_no_enum:
1355 kfree(buffer.pointer);
1356 return;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001357}
1358
Mattia Dongilibf155712011-02-19 11:52:31 +09001359/* Keyboard backlight feature */
1360#define KBDBL_HANDLER 0x137
1361#define KBDBL_PRESENT 0xB00
1362#define SET_MODE 0xC00
1363#define SET_TIMEOUT 0xE00
1364
1365struct kbd_backlight {
1366 int mode;
1367 int timeout;
1368 struct device_attribute mode_attr;
1369 struct device_attribute timeout_attr;
1370};
1371
Dan Carpenterf11113b2011-02-26 15:54:27 +03001372static struct kbd_backlight *kbdbl_handle;
Mattia Dongilibf155712011-02-19 11:52:31 +09001373
1374static ssize_t __sony_nc_kbd_backlight_mode_set(u8 value)
1375{
1376 int result;
1377
1378 if (value > 1)
1379 return -EINVAL;
1380
1381 if (sony_call_snc_handle(KBDBL_HANDLER,
1382 (value << 0x10) | SET_MODE, &result))
1383 return -EIO;
1384
1385 kbdbl_handle->mode = value;
1386
1387 return 0;
1388}
1389
1390static ssize_t sony_nc_kbd_backlight_mode_store(struct device *dev,
1391 struct device_attribute *attr,
1392 const char *buffer, size_t count)
1393{
1394 int ret = 0;
1395 unsigned long value;
1396
1397 if (count > 31)
1398 return -EINVAL;
1399
1400 if (strict_strtoul(buffer, 10, &value))
1401 return -EINVAL;
1402
1403 ret = __sony_nc_kbd_backlight_mode_set(value);
1404 if (ret < 0)
1405 return ret;
1406
1407 return count;
1408}
1409
1410static ssize_t sony_nc_kbd_backlight_mode_show(struct device *dev,
1411 struct device_attribute *attr, char *buffer)
1412{
1413 ssize_t count = 0;
1414 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_handle->mode);
1415 return count;
1416}
1417
1418static int __sony_nc_kbd_backlight_timeout_set(u8 value)
1419{
1420 int result;
1421
1422 if (value > 3)
1423 return -EINVAL;
1424
1425 if (sony_call_snc_handle(KBDBL_HANDLER,
1426 (value << 0x10) | SET_TIMEOUT, &result))
1427 return -EIO;
1428
1429 kbdbl_handle->timeout = value;
1430
1431 return 0;
1432}
1433
1434static ssize_t sony_nc_kbd_backlight_timeout_store(struct device *dev,
1435 struct device_attribute *attr,
1436 const char *buffer, size_t count)
1437{
1438 int ret = 0;
1439 unsigned long value;
1440
1441 if (count > 31)
1442 return -EINVAL;
1443
1444 if (strict_strtoul(buffer, 10, &value))
1445 return -EINVAL;
1446
1447 ret = __sony_nc_kbd_backlight_timeout_set(value);
1448 if (ret < 0)
1449 return ret;
1450
1451 return count;
1452}
1453
1454static ssize_t sony_nc_kbd_backlight_timeout_show(struct device *dev,
1455 struct device_attribute *attr, char *buffer)
1456{
1457 ssize_t count = 0;
1458 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_handle->timeout);
1459 return count;
1460}
1461
1462static int sony_nc_kbd_backlight_setup(struct platform_device *pd)
1463{
1464 int result;
1465
1466 if (sony_call_snc_handle(0x137, KBDBL_PRESENT, &result))
1467 return 0;
1468 if (!(result & 0x02))
1469 return 0;
1470
1471 kbdbl_handle = kzalloc(sizeof(*kbdbl_handle), GFP_KERNEL);
Dan Carpenter31f00752011-02-26 15:55:24 +03001472 if (!kbdbl_handle)
1473 return -ENOMEM;
Mattia Dongilibf155712011-02-19 11:52:31 +09001474
1475 sysfs_attr_init(&kbdbl_handle->mode_attr.attr);
1476 kbdbl_handle->mode_attr.attr.name = "kbd_backlight";
1477 kbdbl_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
1478 kbdbl_handle->mode_attr.show = sony_nc_kbd_backlight_mode_show;
1479 kbdbl_handle->mode_attr.store = sony_nc_kbd_backlight_mode_store;
1480
1481 sysfs_attr_init(&kbdbl_handle->timeout_attr.attr);
1482 kbdbl_handle->timeout_attr.attr.name = "kbd_backlight_timeout";
1483 kbdbl_handle->timeout_attr.attr.mode = S_IRUGO | S_IWUSR;
1484 kbdbl_handle->timeout_attr.show = sony_nc_kbd_backlight_timeout_show;
1485 kbdbl_handle->timeout_attr.store = sony_nc_kbd_backlight_timeout_store;
1486
1487 if (device_create_file(&pd->dev, &kbdbl_handle->mode_attr))
1488 goto outkzalloc;
1489
1490 if (device_create_file(&pd->dev, &kbdbl_handle->timeout_attr))
1491 goto outmode;
1492
1493 __sony_nc_kbd_backlight_mode_set(kbd_backlight);
1494 __sony_nc_kbd_backlight_timeout_set(kbd_backlight_timeout);
1495
1496 return 0;
1497
1498outmode:
1499 device_remove_file(&pd->dev, &kbdbl_handle->mode_attr);
1500outkzalloc:
1501 kfree(kbdbl_handle);
1502 kbdbl_handle = NULL;
1503 return -1;
1504}
1505
1506static int sony_nc_kbd_backlight_cleanup(struct platform_device *pd)
1507{
1508 if (kbdbl_handle) {
1509 device_remove_file(&pd->dev, &kbdbl_handle->mode_attr);
1510 device_remove_file(&pd->dev, &kbdbl_handle->timeout_attr);
1511 kfree(kbdbl_handle);
1512 }
1513 return 0;
1514}
1515
Mattia Dongili7751ab82011-02-19 11:52:32 +09001516static void sony_nc_backlight_setup(void)
1517{
1518 acpi_handle unused;
1519 int max_brightness = 0;
1520 const struct backlight_ops *ops = NULL;
1521 struct backlight_properties props;
1522
1523 if (sony_find_snc_handle(0x12f) != -1) {
1524 backlight_ng_handle = 0x12f;
1525 ops = &sony_backlight_ng_ops;
1526 max_brightness = 0xff;
1527
1528 } else if (sony_find_snc_handle(0x137) != -1) {
1529 backlight_ng_handle = 0x137;
1530 ops = &sony_backlight_ng_ops;
1531 max_brightness = 0xff;
1532
1533 } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
1534 &unused))) {
1535 ops = &sony_backlight_ops;
1536 max_brightness = SONY_MAX_BRIGHTNESS - 1;
1537
1538 } else
1539 return;
1540
1541 memset(&props, 0, sizeof(struct backlight_properties));
1542 props.type = BACKLIGHT_PLATFORM;
1543 props.max_brightness = max_brightness;
1544 sony_backlight_device = backlight_device_register("sony", NULL,
1545 &backlight_ng_handle,
1546 ops, &props);
1547
1548 if (IS_ERR(sony_backlight_device)) {
1549 pr_warning(DRV_PFX "unable to register backlight device\n");
1550 sony_backlight_device = NULL;
1551 } else
1552 sony_backlight_device->props.brightness =
1553 ops->get_brightness(sony_backlight_device);
1554}
1555
1556static void sony_nc_backlight_cleanup(void)
1557{
1558 if (sony_backlight_device)
1559 backlight_device_unregister(sony_backlight_device);
1560}
1561
malattia@linux.it59b19102007-04-09 10:19:04 +02001562static int sony_nc_add(struct acpi_device *device)
Stelian Pop7f09c432007-01-13 23:04:31 +01001563{
1564 acpi_status status;
Andrew Morton8607c672007-03-06 02:29:42 -08001565 int result = 0;
Alessandro Guido50f62af2007-01-13 23:04:34 +01001566 acpi_handle handle;
malattia@linux.it56b87562007-04-09 10:19:05 +02001567 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01001568
Mattia Dongilid6697932011-02-19 11:52:28 +09001569 pr_info(DRV_PFX "%s v%s.\n", SONY_NC_DRIVER_NAME,
1570 SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.itf6119b02007-04-09 19:31:06 +02001571
malattia@linux.it59b19102007-04-09 10:19:04 +02001572 sony_nc_acpi_device = device;
malattia@linux.it33a04452007-04-09 19:26:03 +02001573 strcpy(acpi_device_class(device), "sony/hotkey");
Stelian Popc5611622007-01-13 23:04:37 +01001574
malattia@linux.it59b19102007-04-09 10:19:04 +02001575 sony_nc_acpi_handle = device->handle;
Stelian Pop7f09c432007-01-13 23:04:31 +01001576
Mattia Dongilib25b7322007-07-16 02:34:36 +09001577 /* read device status */
1578 result = acpi_bus_get_status(device);
1579 /* bail IFF the above call was successful and the device is not present */
1580 if (!result && !device->status.present) {
1581 dprintk("Device not present\n");
1582 result = -ENODEV;
1583 goto outwalk;
1584 }
1585
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001586 result = sony_pf_add();
1587 if (result)
1588 goto outpresent;
1589
Stelian Pop7f09c432007-01-13 23:04:31 +01001590 if (debug) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001591 status = acpi_walk_namespace(ACPI_TYPE_METHOD,
1592 sony_nc_acpi_handle, 1, sony_walk_callback,
1593 NULL, NULL, NULL);
Stelian Pop7f09c432007-01-13 23:04:31 +01001594 if (ACPI_FAILURE(status)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001595 pr_warn(DRV_PFX "unable to walk acpi resources\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001596 result = -ENODEV;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001597 goto outpresent;
Stelian Pop7f09c432007-01-13 23:04:31 +01001598 }
Stelian Popc5611622007-01-13 23:04:37 +01001599 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001600
Matthew Garrett82734bf2009-03-26 21:58:13 +09001601 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1602 &handle))) {
1603 if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
1604 dprintk("ECON Method failed\n");
1605 }
1606
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001607 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1608 &handle))) {
1609 dprintk("Doing SNC setup\n");
Dan Carpenter7227ded2011-02-26 15:54:57 +03001610 result = sony_nc_handles_setup(sony_pf_device);
1611 if (result)
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001612 goto outpresent;
Dan Carpenter7227ded2011-02-26 15:54:57 +03001613 result = sony_nc_kbd_backlight_setup(sony_pf_device);
1614 if (result)
Mattia Dongilibf155712011-02-19 11:52:31 +09001615 goto outsnc;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001616 sony_nc_function_setup(device);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001617 sony_nc_rfkill_setup(device);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001618 }
1619
malattia@linux.it1549ee62007-04-09 10:19:08 +02001620 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05001621 result = sony_laptop_setup_input(device);
malattia@linux.it1549ee62007-04-09 10:19:08 +02001622 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001623 pr_err(DRV_PFX "Unable to create input devices.\n");
Mattia Dongilibf155712011-02-19 11:52:31 +09001624 goto outkbdbacklight;
malattia@linux.it1549ee62007-04-09 10:19:08 +02001625 }
1626
Alessandro Guido38cfc142008-11-12 23:03:28 +01001627 if (acpi_video_backlight_support()) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001628 pr_info(DRV_PFX "brightness ignored, must be "
Thomas Renninger540b8bb2008-08-01 17:38:02 +02001629 "controlled by ACPI video driver\n");
Mattia Dongili7751ab82011-02-19 11:52:32 +09001630 } else {
1631 sony_nc_backlight_setup();
Alessandro Guido50f62af2007-01-13 23:04:34 +01001632 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001633
malattia@linux.it56b87562007-04-09 10:19:05 +02001634 /* create sony_pf sysfs attributes related to the SNC device */
1635 for (item = sony_nc_values; item->name; ++item) {
1636
1637 if (!debug && item->debug)
1638 continue;
1639
1640 /* find the available acpiget as described in the DSDT */
1641 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
1642 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1643 *item->acpiget,
1644 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02001645 dprintk("Found %s getter: %s\n",
1646 item->name, *item->acpiget);
malattia@linux.it56b87562007-04-09 10:19:05 +02001647 item->devattr.attr.mode |= S_IRUGO;
1648 break;
1649 }
1650 }
1651
1652 /* find the available acpiset as described in the DSDT */
1653 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
1654 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1655 *item->acpiset,
1656 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02001657 dprintk("Found %s setter: %s\n",
1658 item->name, *item->acpiset);
malattia@linux.it56b87562007-04-09 10:19:05 +02001659 item->devattr.attr.mode |= S_IWUSR;
1660 break;
1661 }
1662 }
1663
1664 if (item->devattr.attr.mode != 0) {
1665 result =
1666 device_create_file(&sony_pf_device->dev,
1667 &item->devattr);
1668 if (result)
1669 goto out_sysfs;
1670 }
1671 }
1672
Stelian Pop7f09c432007-01-13 23:04:31 +01001673 return 0;
1674
malattia@linux.it56b87562007-04-09 10:19:05 +02001675 out_sysfs:
1676 for (item = sony_nc_values; item->name; ++item) {
1677 device_remove_file(&sony_pf_device->dev, &item->devattr);
1678 }
Mattia Dongili7751ab82011-02-19 11:52:32 +09001679 sony_nc_backlight_cleanup();
Mattia Dongili7df03b82007-01-13 23:04:41 +01001680
malattia@linux.it1549ee62007-04-09 10:19:08 +02001681 sony_laptop_remove_input();
1682
Mattia Dongilibf155712011-02-19 11:52:31 +09001683 outkbdbacklight:
1684 sony_nc_kbd_backlight_cleanup(sony_pf_device);
1685
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001686 outsnc:
1687 sony_nc_handles_cleanup(sony_pf_device);
1688
1689 outpresent:
1690 sony_pf_remove();
1691
Len Browna02d1c12007-02-07 15:34:02 -05001692 outwalk:
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001693 sony_nc_rfkill_cleanup();
Stelian Pop7f09c432007-01-13 23:04:31 +01001694 return result;
1695}
1696
malattia@linux.it59b19102007-04-09 10:19:04 +02001697static int sony_nc_remove(struct acpi_device *device, int type)
Stelian Pop7f09c432007-01-13 23:04:31 +01001698{
malattia@linux.it56b87562007-04-09 10:19:05 +02001699 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01001700
Mattia Dongili7751ab82011-02-19 11:52:32 +09001701 sony_nc_backlight_cleanup();
Alessandro Guido50f62af2007-01-13 23:04:34 +01001702
malattia@linux.it59b19102007-04-09 10:19:04 +02001703 sony_nc_acpi_device = NULL;
Stelian Popc5611622007-01-13 23:04:37 +01001704
malattia@linux.it56b87562007-04-09 10:19:05 +02001705 for (item = sony_nc_values; item->name; ++item) {
1706 device_remove_file(&sony_pf_device->dev, &item->devattr);
1707 }
1708
Mattia Dongilibf155712011-02-19 11:52:31 +09001709 sony_nc_kbd_backlight_cleanup(sony_pf_device);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001710 sony_nc_handles_cleanup(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +02001711 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +02001712 sony_laptop_remove_input();
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001713 sony_nc_rfkill_cleanup();
malattia@linux.itf6119b02007-04-09 19:31:06 +02001714 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001715
1716 return 0;
1717}
1718
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001719static const struct acpi_device_id sony_device_ids[] = {
1720 {SONY_NC_HID, 0},
1721 {SONY_PIC_HID, 0},
1722 {"", 0},
1723};
1724MODULE_DEVICE_TABLE(acpi, sony_device_ids);
1725
1726static const struct acpi_device_id sony_nc_device_ids[] = {
1727 {SONY_NC_HID, 0},
1728 {"", 0},
1729};
1730
malattia@linux.it59b19102007-04-09 10:19:04 +02001731static struct acpi_driver sony_nc_driver = {
1732 .name = SONY_NC_DRIVER_NAME,
1733 .class = SONY_NC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001734 .ids = sony_nc_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02001735 .owner = THIS_MODULE,
Len Browna02d1c12007-02-07 15:34:02 -05001736 .ops = {
malattia@linux.it59b19102007-04-09 10:19:04 +02001737 .add = sony_nc_add,
1738 .remove = sony_nc_remove,
1739 .resume = sony_nc_resume,
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001740 .notify = sony_nc_notify,
Len Browna02d1c12007-02-07 15:34:02 -05001741 },
Andrew Morton3f4f4612007-01-13 23:04:32 +01001742};
1743
malattia@linux.it33a04452007-04-09 19:26:03 +02001744/*********** SPIC (SNY6001) Device ***********/
1745
1746#define SONYPI_DEVICE_TYPE1 0x00000001
1747#define SONYPI_DEVICE_TYPE2 0x00000002
1748#define SONYPI_DEVICE_TYPE3 0x00000004
1749
Mattia Dongili22a17782007-07-16 02:34:39 +09001750#define SONYPI_TYPE1_OFFSET 0x04
1751#define SONYPI_TYPE2_OFFSET 0x12
1752#define SONYPI_TYPE3_OFFSET 0x12
malattia@linux.it33a04452007-04-09 19:26:03 +02001753
malattia@linux.it33a04452007-04-09 19:26:03 +02001754struct sony_pic_ioport {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001755 struct acpi_resource_io io1;
1756 struct acpi_resource_io io2;
malattia@linux.it33a04452007-04-09 19:26:03 +02001757 struct list_head list;
1758};
1759
1760struct sony_pic_irq {
1761 struct acpi_resource_irq irq;
1762 struct list_head list;
1763};
1764
Mattia Dongilide920432008-01-14 18:05:43 +09001765struct sonypi_eventtypes {
1766 u8 data;
1767 unsigned long mask;
1768 struct sonypi_event *events;
1769};
1770
malattia@linux.it33a04452007-04-09 19:26:03 +02001771struct sony_pic_dev {
Mattia Dongili31df7142009-09-16 00:05:29 +09001772 struct acpi_device *acpi_dev;
1773 struct sony_pic_irq *cur_irq;
1774 struct sony_pic_ioport *cur_ioport;
1775 struct list_head interrupts;
1776 struct list_head ioports;
1777 struct mutex lock;
1778 struct sonypi_eventtypes *event_types;
1779 int (*handle_irq)(const u8, const u8);
1780 int model;
1781 u16 evport_offset;
1782 u8 camera_power;
1783 u8 bluetooth_power;
1784 u8 wwan_power;
malattia@linux.it33a04452007-04-09 19:26:03 +02001785};
1786
1787static struct sony_pic_dev spic_dev = {
1788 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
1789 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
1790};
1791
Alan Jenkins5e6f9722009-09-16 00:05:32 +09001792static int spic_drv_registered;
1793
malattia@linux.it33a04452007-04-09 19:26:03 +02001794/* Event masks */
1795#define SONYPI_JOGGER_MASK 0x00000001
1796#define SONYPI_CAPTURE_MASK 0x00000002
1797#define SONYPI_FNKEY_MASK 0x00000004
1798#define SONYPI_BLUETOOTH_MASK 0x00000008
1799#define SONYPI_PKEY_MASK 0x00000010
1800#define SONYPI_BACK_MASK 0x00000020
1801#define SONYPI_HELP_MASK 0x00000040
1802#define SONYPI_LID_MASK 0x00000080
1803#define SONYPI_ZOOM_MASK 0x00000100
1804#define SONYPI_THUMBPHRASE_MASK 0x00000200
1805#define SONYPI_MEYE_MASK 0x00000400
1806#define SONYPI_MEMORYSTICK_MASK 0x00000800
1807#define SONYPI_BATTERY_MASK 0x00001000
1808#define SONYPI_WIRELESS_MASK 0x00002000
1809
1810struct sonypi_event {
1811 u8 data;
1812 u8 event;
1813};
1814
1815/* The set of possible button release events */
1816static struct sonypi_event sonypi_releaseev[] = {
1817 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
1818 { 0, 0 }
1819};
1820
1821/* The set of possible jogger events */
1822static struct sonypi_event sonypi_joggerev[] = {
1823 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
1824 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
1825 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
1826 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
1827 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
1828 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
1829 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
1830 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
1831 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
1832 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
1833 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
1834 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
1835 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
1836 { 0, 0 }
1837};
1838
1839/* The set of possible capture button events */
1840static struct sonypi_event sonypi_captureev[] = {
1841 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
1842 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09001843 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02001844 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
1845 { 0, 0 }
1846};
1847
1848/* The set of possible fnkeys events */
1849static struct sonypi_event sonypi_fnkeyev[] = {
1850 { 0x10, SONYPI_EVENT_FNKEY_ESC },
1851 { 0x11, SONYPI_EVENT_FNKEY_F1 },
1852 { 0x12, SONYPI_EVENT_FNKEY_F2 },
1853 { 0x13, SONYPI_EVENT_FNKEY_F3 },
1854 { 0x14, SONYPI_EVENT_FNKEY_F4 },
1855 { 0x15, SONYPI_EVENT_FNKEY_F5 },
1856 { 0x16, SONYPI_EVENT_FNKEY_F6 },
1857 { 0x17, SONYPI_EVENT_FNKEY_F7 },
1858 { 0x18, SONYPI_EVENT_FNKEY_F8 },
1859 { 0x19, SONYPI_EVENT_FNKEY_F9 },
1860 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
1861 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
1862 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
1863 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
1864 { 0x21, SONYPI_EVENT_FNKEY_1 },
1865 { 0x22, SONYPI_EVENT_FNKEY_2 },
1866 { 0x31, SONYPI_EVENT_FNKEY_D },
1867 { 0x32, SONYPI_EVENT_FNKEY_E },
1868 { 0x33, SONYPI_EVENT_FNKEY_F },
1869 { 0x34, SONYPI_EVENT_FNKEY_S },
1870 { 0x35, SONYPI_EVENT_FNKEY_B },
1871 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
1872 { 0, 0 }
1873};
1874
1875/* The set of possible program key events */
1876static struct sonypi_event sonypi_pkeyev[] = {
1877 { 0x01, SONYPI_EVENT_PKEY_P1 },
1878 { 0x02, SONYPI_EVENT_PKEY_P2 },
1879 { 0x04, SONYPI_EVENT_PKEY_P3 },
Harald Jenny1cae7102009-03-26 21:58:18 +09001880 { 0x20, SONYPI_EVENT_PKEY_P1 },
malattia@linux.it33a04452007-04-09 19:26:03 +02001881 { 0, 0 }
1882};
1883
1884/* The set of possible bluetooth events */
1885static struct sonypi_event sonypi_blueev[] = {
1886 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
1887 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
1888 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
1889 { 0, 0 }
1890};
1891
1892/* The set of possible wireless events */
1893static struct sonypi_event sonypi_wlessev[] = {
Mattia Dongili4eeb5022011-02-19 11:52:27 +09001894 { 0x59, SONYPI_EVENT_IGNORE },
1895 { 0x5a, SONYPI_EVENT_IGNORE },
malattia@linux.it33a04452007-04-09 19:26:03 +02001896 { 0, 0 }
1897};
1898
1899/* The set of possible back button events */
1900static struct sonypi_event sonypi_backev[] = {
1901 { 0x20, SONYPI_EVENT_BACK_PRESSED },
1902 { 0, 0 }
1903};
1904
1905/* The set of possible help button events */
1906static struct sonypi_event sonypi_helpev[] = {
1907 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
1908 { 0, 0 }
1909};
1910
1911
1912/* The set of possible lid events */
1913static struct sonypi_event sonypi_lidev[] = {
1914 { 0x51, SONYPI_EVENT_LID_CLOSED },
1915 { 0x50, SONYPI_EVENT_LID_OPENED },
1916 { 0, 0 }
1917};
1918
1919/* The set of possible zoom events */
1920static struct sonypi_event sonypi_zoomev[] = {
1921 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09001922 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
1923 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
Harald Jenny1cae7102009-03-26 21:58:18 +09001924 { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02001925 { 0, 0 }
1926};
1927
1928/* The set of possible thumbphrase events */
1929static struct sonypi_event sonypi_thumbphraseev[] = {
1930 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
1931 { 0, 0 }
1932};
1933
1934/* The set of possible motioneye camera events */
1935static struct sonypi_event sonypi_meyeev[] = {
1936 { 0x00, SONYPI_EVENT_MEYE_FACE },
1937 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
1938 { 0, 0 }
1939};
1940
1941/* The set of possible memorystick events */
1942static struct sonypi_event sonypi_memorystickev[] = {
1943 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
1944 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
1945 { 0, 0 }
1946};
1947
1948/* The set of possible battery events */
1949static struct sonypi_event sonypi_batteryev[] = {
1950 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
1951 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
1952 { 0, 0 }
1953};
1954
Harald Jenny1cae7102009-03-26 21:58:18 +09001955/* The set of possible volume events */
1956static struct sonypi_event sonypi_volumeev[] = {
1957 { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
1958 { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
1959 { 0, 0 }
1960};
1961
1962/* The set of possible brightness events */
1963static struct sonypi_event sonypi_brightnessev[] = {
1964 { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
1965 { 0, 0 }
1966};
1967
Mattia Dongilide920432008-01-14 18:05:43 +09001968static struct sonypi_eventtypes type1_events[] = {
1969 { 0, 0xffffffff, sonypi_releaseev },
1970 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
1971 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
1972 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
1973 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
1974 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1975 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1976 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
1977 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1978 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
1979 { 0 },
1980};
1981static struct sonypi_eventtypes type2_events[] = {
1982 { 0, 0xffffffff, sonypi_releaseev },
1983 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
1984 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
1985 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
1986 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1987 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1988 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
1989 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
1990 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
1991 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
1992 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
1993 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1994 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1995 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1996 { 0 },
1997};
1998static struct sonypi_eventtypes type3_events[] = {
1999 { 0, 0xffffffff, sonypi_releaseev },
2000 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2001 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
2002 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2003 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
2004 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002005 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
2006 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
2007 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
Harald Jenny1cae7102009-03-26 21:58:18 +09002008 { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
2009 { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002010 { 0 },
Mattia Dongilide920432008-01-14 18:05:43 +09002011};
2012
Mattia Dongili3eb87492008-01-14 18:05:45 +09002013/* low level spic calls */
malattia@linux.it33a04452007-04-09 19:26:03 +02002014#define ITERATIONS_LONG 10000
2015#define ITERATIONS_SHORT 10
2016#define wait_on_command(command, iterations) { \
2017 unsigned int n = iterations; \
2018 while (--n && (command)) \
2019 udelay(1); \
2020 if (!n) \
2021 dprintk("command failed at %s : %s (line %d)\n", \
Harvey Harrison6e574192008-04-29 00:59:20 -07002022 __FILE__, __func__, __LINE__); \
malattia@linux.it33a04452007-04-09 19:26:03 +02002023}
2024
2025static u8 sony_pic_call1(u8 dev)
2026{
2027 u8 v1, v2;
2028
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002029 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002030 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002031 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2032 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
2033 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002034 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02002035 return v2;
2036}
2037
2038static u8 sony_pic_call2(u8 dev, u8 fn)
2039{
2040 u8 v1;
2041
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002042 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002043 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002044 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2045 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002046 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002047 outb(fn, spic_dev.cur_ioport->io1.minimum);
2048 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002049 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02002050 return v1;
2051}
2052
malattia@linux.it49a11de2007-04-09 19:28:56 +02002053static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
2054{
2055 u8 v1;
2056
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002057 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2058 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2059 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2060 outb(fn, spic_dev.cur_ioport->io1.minimum);
2061 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2062 outb(v, spic_dev.cur_ioport->io1.minimum);
2063 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002064 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
2065 dev, fn, v, v1);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002066 return v1;
2067}
2068
Mattia Dongili3eb87492008-01-14 18:05:45 +09002069/*
2070 * minidrivers for SPIC models
2071 */
Mattia Dongilie93c8a62009-03-26 21:58:17 +09002072static int type3_handle_irq(const u8 data_mask, const u8 ev)
Mattia Dongili3eb87492008-01-14 18:05:45 +09002073{
2074 /*
2075 * 0x31 could mean we have to take some extra action and wait for
Mattia Dongilie93c8a62009-03-26 21:58:17 +09002076 * the next irq for some Type3 models, it will generate a new
Mattia Dongili3eb87492008-01-14 18:05:45 +09002077 * irq and we can read new data from the device:
2078 * - 0x5c and 0x5f requires 0xA0
2079 * - 0x61 requires 0xB3
2080 */
2081 if (data_mask == 0x31) {
2082 if (ev == 0x5c || ev == 0x5f)
2083 sony_pic_call1(0xA0);
2084 else if (ev == 0x61)
2085 sony_pic_call1(0xB3);
2086 return 0;
2087 }
2088 return 1;
2089}
2090
Mattia Dongili3eb87492008-01-14 18:05:45 +09002091static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
2092{
2093 struct pci_dev *pcidev;
2094
2095 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2096 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
2097 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002098 dev->model = SONYPI_DEVICE_TYPE1;
2099 dev->evport_offset = SONYPI_TYPE1_OFFSET;
2100 dev->event_types = type1_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002101 goto out;
2102 }
2103
2104 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2105 PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
2106 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002107 dev->model = SONYPI_DEVICE_TYPE2;
2108 dev->evport_offset = SONYPI_TYPE2_OFFSET;
2109 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002110 goto out;
2111 }
2112
2113 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2114 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
2115 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002116 dev->model = SONYPI_DEVICE_TYPE3;
2117 dev->handle_irq = type3_handle_irq;
2118 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2119 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002120 goto out;
2121 }
2122
2123 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2124 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
2125 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002126 dev->model = SONYPI_DEVICE_TYPE3;
2127 dev->handle_irq = type3_handle_irq;
2128 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2129 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002130 goto out;
2131 }
2132
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09002133 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2134 PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
2135 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002136 dev->model = SONYPI_DEVICE_TYPE3;
2137 dev->handle_irq = type3_handle_irq;
2138 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2139 dev->event_types = type3_events;
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09002140 goto out;
2141 }
2142
Mattia Dongili3eb87492008-01-14 18:05:45 +09002143 /* default */
Mattia Dongili31df7142009-09-16 00:05:29 +09002144 dev->model = SONYPI_DEVICE_TYPE2;
2145 dev->evport_offset = SONYPI_TYPE2_OFFSET;
2146 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002147
2148out:
2149 if (pcidev)
2150 pci_dev_put(pcidev);
2151
Mattia Dongilid6697932011-02-19 11:52:28 +09002152 pr_info(DRV_PFX "detected Type%d model\n",
Mattia Dongili31df7142009-09-16 00:05:29 +09002153 dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
2154 dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
Mattia Dongili3eb87492008-01-14 18:05:45 +09002155}
2156
malattia@linux.it49a11de2007-04-09 19:28:56 +02002157/* camera tests and poweron/poweroff */
2158#define SONYPI_CAMERA_PICTURE 5
malattia@linux.it49a11de2007-04-09 19:28:56 +02002159#define SONYPI_CAMERA_CONTROL 0x10
malattia@linux.ite3646322007-04-28 23:34:22 +09002160
2161#define SONYPI_CAMERA_BRIGHTNESS 0
2162#define SONYPI_CAMERA_CONTRAST 1
2163#define SONYPI_CAMERA_HUE 2
2164#define SONYPI_CAMERA_COLOR 3
2165#define SONYPI_CAMERA_SHARPNESS 4
2166
2167#define SONYPI_CAMERA_EXPOSURE_MASK 0xC
2168#define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
2169#define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
2170#define SONYPI_CAMERA_MUTE_MASK 0x40
2171
2172/* the rest don't need a loop until not 0xff */
2173#define SONYPI_CAMERA_AGC 6
2174#define SONYPI_CAMERA_AGC_MASK 0x30
2175#define SONYPI_CAMERA_SHUTTER_MASK 0x7
2176
2177#define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
2178#define SONYPI_CAMERA_CONTROL 0x10
2179
2180#define SONYPI_CAMERA_STATUS 7
2181#define SONYPI_CAMERA_STATUS_READY 0x2
2182#define SONYPI_CAMERA_STATUS_POSITION 0x4
2183
2184#define SONYPI_DIRECTION_BACKWARDS 0x4
2185
2186#define SONYPI_CAMERA_REVISION 8
2187#define SONYPI_CAMERA_ROMVERSION 9
malattia@linux.it49a11de2007-04-09 19:28:56 +02002188
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002189static int __sony_pic_camera_ready(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002190{
2191 u8 v;
2192
2193 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
2194 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
2195}
2196
malattia@linux.ite3646322007-04-28 23:34:22 +09002197static int __sony_pic_camera_off(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002198{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002199 if (!camera) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002200 pr_warn(DRV_PFX "camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002201 return -ENODEV;
2202 }
2203
malattia@linux.it49a11de2007-04-09 19:28:56 +02002204 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
2205 SONYPI_CAMERA_MUTE_MASK),
2206 ITERATIONS_SHORT);
2207
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002208 if (spic_dev.camera_power) {
2209 sony_pic_call2(0x91, 0);
2210 spic_dev.camera_power = 0;
2211 }
2212 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002213}
2214
malattia@linux.ite3646322007-04-28 23:34:22 +09002215static int __sony_pic_camera_on(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002216{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002217 int i, j, x;
2218
2219 if (!camera) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002220 pr_warn(DRV_PFX "camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002221 return -ENODEV;
2222 }
malattia@linux.it49a11de2007-04-09 19:28:56 +02002223
2224 if (spic_dev.camera_power)
malattia@linux.ite3646322007-04-28 23:34:22 +09002225 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002226
2227 for (j = 5; j > 0; j--) {
2228
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002229 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002230 msleep(10);
2231 sony_pic_call1(0x93);
2232
2233 for (i = 400; i > 0; i--) {
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002234 if (__sony_pic_camera_ready())
malattia@linux.it49a11de2007-04-09 19:28:56 +02002235 break;
2236 msleep(10);
2237 }
2238 if (i)
2239 break;
2240 }
2241
2242 if (j == 0) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002243 pr_warn(DRV_PFX "failed to power on camera\n");
malattia@linux.ite3646322007-04-28 23:34:22 +09002244 return -ENODEV;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002245 }
2246
2247 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
2248 0x5a),
2249 ITERATIONS_SHORT);
2250
2251 spic_dev.camera_power = 1;
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002252 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002253}
2254
malattia@linux.ite3646322007-04-28 23:34:22 +09002255/* External camera command (exported to the motion eye v4l driver) */
2256int sony_pic_camera_command(int command, u8 value)
2257{
2258 if (!camera)
2259 return -EIO;
2260
2261 mutex_lock(&spic_dev.lock);
2262
2263 switch (command) {
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002264 case SONY_PIC_COMMAND_SETCAMERA:
malattia@linux.ite3646322007-04-28 23:34:22 +09002265 if (value)
2266 __sony_pic_camera_on();
2267 else
2268 __sony_pic_camera_off();
2269 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002270 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09002271 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
2272 ITERATIONS_SHORT);
2273 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002274 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
malattia@linux.ite3646322007-04-28 23:34:22 +09002275 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
2276 ITERATIONS_SHORT);
2277 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002278 case SONY_PIC_COMMAND_SETCAMERAHUE:
malattia@linux.ite3646322007-04-28 23:34:22 +09002279 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
2280 ITERATIONS_SHORT);
2281 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002282 case SONY_PIC_COMMAND_SETCAMERACOLOR:
malattia@linux.ite3646322007-04-28 23:34:22 +09002283 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
2284 ITERATIONS_SHORT);
2285 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002286 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09002287 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
2288 ITERATIONS_SHORT);
2289 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002290 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
malattia@linux.ite3646322007-04-28 23:34:22 +09002291 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
2292 ITERATIONS_SHORT);
2293 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002294 case SONY_PIC_COMMAND_SETCAMERAAGC:
malattia@linux.ite3646322007-04-28 23:34:22 +09002295 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
2296 ITERATIONS_SHORT);
2297 break;
2298 default:
Mattia Dongilid6697932011-02-19 11:52:28 +09002299 pr_err(DRV_PFX "sony_pic_camera_command invalid: %d\n",
malattia@linux.ite3646322007-04-28 23:34:22 +09002300 command);
2301 break;
2302 }
2303 mutex_unlock(&spic_dev.lock);
2304 return 0;
2305}
2306EXPORT_SYMBOL(sony_pic_camera_command);
2307
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002308/* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002309static void __sony_pic_set_wwanpower(u8 state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002310{
2311 state = !!state;
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002312 if (spic_dev.wwan_power == state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002313 return;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002314 sony_pic_call2(0xB0, state);
Sergey Yanovich3ad1b762009-03-26 21:58:21 +09002315 sony_pic_call1(0x82);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002316 spic_dev.wwan_power = state;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002317}
2318
2319static ssize_t sony_pic_wwanpower_store(struct device *dev,
2320 struct device_attribute *attr,
2321 const char *buffer, size_t count)
2322{
2323 unsigned long value;
2324 if (count > 31)
2325 return -EINVAL;
2326
2327 value = simple_strtoul(buffer, NULL, 10);
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002328 mutex_lock(&spic_dev.lock);
2329 __sony_pic_set_wwanpower(value);
2330 mutex_unlock(&spic_dev.lock);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002331
2332 return count;
2333}
2334
2335static ssize_t sony_pic_wwanpower_show(struct device *dev,
2336 struct device_attribute *attr, char *buffer)
2337{
2338 ssize_t count;
2339 mutex_lock(&spic_dev.lock);
2340 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
2341 mutex_unlock(&spic_dev.lock);
2342 return count;
2343}
2344
malattia@linux.it49a11de2007-04-09 19:28:56 +02002345/* bluetooth subsystem power state */
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002346static void __sony_pic_set_bluetoothpower(u8 state)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002347{
2348 state = !!state;
2349 if (spic_dev.bluetooth_power == state)
2350 return;
2351 sony_pic_call2(0x96, state);
2352 sony_pic_call1(0x82);
2353 spic_dev.bluetooth_power = state;
2354}
2355
2356static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
2357 struct device_attribute *attr,
2358 const char *buffer, size_t count)
2359{
2360 unsigned long value;
2361 if (count > 31)
2362 return -EINVAL;
2363
2364 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002365 mutex_lock(&spic_dev.lock);
2366 __sony_pic_set_bluetoothpower(value);
2367 mutex_unlock(&spic_dev.lock);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002368
2369 return count;
2370}
2371
2372static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
2373 struct device_attribute *attr, char *buffer)
2374{
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002375 ssize_t count = 0;
2376 mutex_lock(&spic_dev.lock);
2377 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
2378 mutex_unlock(&spic_dev.lock);
2379 return count;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002380}
2381
2382/* fan speed */
2383/* FAN0 information (reverse engineered from ACPI tables) */
2384#define SONY_PIC_FAN0_STATUS 0x93
malattia@linux.it7b153f32007-04-09 19:31:25 +02002385static int sony_pic_set_fanspeed(unsigned long value)
2386{
2387 return ec_write(SONY_PIC_FAN0_STATUS, value);
2388}
2389
2390static int sony_pic_get_fanspeed(u8 *value)
2391{
2392 return ec_read(SONY_PIC_FAN0_STATUS, value);
2393}
2394
malattia@linux.it49a11de2007-04-09 19:28:56 +02002395static ssize_t sony_pic_fanspeed_store(struct device *dev,
2396 struct device_attribute *attr,
2397 const char *buffer, size_t count)
2398{
2399 unsigned long value;
2400 if (count > 31)
2401 return -EINVAL;
2402
2403 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002404 if (sony_pic_set_fanspeed(value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02002405 return -EIO;
2406
2407 return count;
2408}
2409
2410static ssize_t sony_pic_fanspeed_show(struct device *dev,
2411 struct device_attribute *attr, char *buffer)
2412{
2413 u8 value = 0;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002414 if (sony_pic_get_fanspeed(&value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02002415 return -EIO;
2416
2417 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
2418}
2419
2420#define SPIC_ATTR(_name, _mode) \
2421struct device_attribute spic_attr_##_name = __ATTR(_name, \
2422 _mode, sony_pic_## _name ##_show, \
2423 sony_pic_## _name ##_store)
2424
malattia@linux.it49a11de2007-04-09 19:28:56 +02002425static SPIC_ATTR(bluetoothpower, 0644);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002426static SPIC_ATTR(wwanpower, 0644);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002427static SPIC_ATTR(fanspeed, 0644);
2428
2429static struct attribute *spic_attributes[] = {
malattia@linux.it49a11de2007-04-09 19:28:56 +02002430 &spic_attr_bluetoothpower.attr,
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002431 &spic_attr_wwanpower.attr,
malattia@linux.it49a11de2007-04-09 19:28:56 +02002432 &spic_attr_fanspeed.attr,
2433 NULL
2434};
2435
2436static struct attribute_group spic_attribute_group = {
2437 .attrs = spic_attributes
2438};
2439
malattia@linux.it7b153f32007-04-09 19:31:25 +02002440/******** SONYPI compatibility **********/
Mattia Dongilia64e62a2007-05-01 11:19:53 +09002441#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +02002442
2443/* battery / brightness / temperature addresses */
2444#define SONYPI_BAT_FLAGS 0x81
2445#define SONYPI_LCD_LIGHT 0x96
2446#define SONYPI_BAT1_PCTRM 0xa0
2447#define SONYPI_BAT1_LEFT 0xa2
2448#define SONYPI_BAT1_MAXRT 0xa4
2449#define SONYPI_BAT2_PCTRM 0xa8
2450#define SONYPI_BAT2_LEFT 0xaa
2451#define SONYPI_BAT2_MAXRT 0xac
2452#define SONYPI_BAT1_MAXTK 0xb0
2453#define SONYPI_BAT1_FULL 0xb2
2454#define SONYPI_BAT2_MAXTK 0xb8
2455#define SONYPI_BAT2_FULL 0xba
2456#define SONYPI_TEMP_STATUS 0xC1
2457
2458struct sonypi_compat_s {
2459 struct fasync_struct *fifo_async;
Stefani Seibold45465482009-12-21 14:37:26 -08002460 struct kfifo fifo;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002461 spinlock_t fifo_lock;
2462 wait_queue_head_t fifo_proc_list;
2463 atomic_t open_count;
2464};
2465static struct sonypi_compat_s sonypi_compat = {
2466 .open_count = ATOMIC_INIT(0),
2467};
2468
2469static int sonypi_misc_fasync(int fd, struct file *filp, int on)
2470{
Jonathan Corbet60aa4922009-02-01 14:52:56 -07002471 return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002472}
2473
2474static int sonypi_misc_release(struct inode *inode, struct file *file)
2475{
malattia@linux.it7b153f32007-04-09 19:31:25 +02002476 atomic_dec(&sonypi_compat.open_count);
2477 return 0;
2478}
2479
2480static int sonypi_misc_open(struct inode *inode, struct file *file)
2481{
2482 /* Flush input queue on first open */
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002483 unsigned long flags;
2484
Stefani Seibold45465482009-12-21 14:37:26 -08002485 spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002486
malattia@linux.it7b153f32007-04-09 19:31:25 +02002487 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
Stefani Seibolde64c0262009-12-21 14:37:28 -08002488 kfifo_reset(&sonypi_compat.fifo);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002489
Stefani Seibold45465482009-12-21 14:37:26 -08002490 spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002491
malattia@linux.it7b153f32007-04-09 19:31:25 +02002492 return 0;
2493}
2494
2495static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
2496 size_t count, loff_t *pos)
2497{
2498 ssize_t ret;
2499 unsigned char c;
2500
Stefani Seibold45465482009-12-21 14:37:26 -08002501 if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
malattia@linux.it7b153f32007-04-09 19:31:25 +02002502 (file->f_flags & O_NONBLOCK))
2503 return -EAGAIN;
2504
2505 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
Stefani Seibold45465482009-12-21 14:37:26 -08002506 kfifo_len(&sonypi_compat.fifo) != 0);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002507 if (ret)
2508 return ret;
2509
2510 while (ret < count &&
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002511 (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002512 &sonypi_compat.fifo_lock) == sizeof(c))) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02002513 if (put_user(c, buf++))
2514 return -EFAULT;
2515 ret++;
2516 }
2517
2518 if (ret > 0) {
2519 struct inode *inode = file->f_path.dentry->d_inode;
2520 inode->i_atime = current_fs_time(inode->i_sb);
2521 }
2522
2523 return ret;
2524}
2525
2526static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
2527{
2528 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
Stefani Seibold45465482009-12-21 14:37:26 -08002529 if (kfifo_len(&sonypi_compat.fifo))
malattia@linux.it7b153f32007-04-09 19:31:25 +02002530 return POLLIN | POLLRDNORM;
2531 return 0;
2532}
2533
2534static int ec_read16(u8 addr, u16 *value)
2535{
2536 u8 val_lb, val_hb;
2537 if (ec_read(addr, &val_lb))
2538 return -1;
2539 if (ec_read(addr + 1, &val_hb))
2540 return -1;
2541 *value = val_lb | (val_hb << 8);
2542 return 0;
2543}
2544
Alan Cox2b24ef02009-03-26 21:58:19 +09002545static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
2546 unsigned long arg)
malattia@linux.it7b153f32007-04-09 19:31:25 +02002547{
2548 int ret = 0;
2549 void __user *argp = (void __user *)arg;
2550 u8 val8;
2551 u16 val16;
2552 int value;
2553
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002554 mutex_lock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002555 switch (cmd) {
2556 case SONYPI_IOCGBRT:
2557 if (sony_backlight_device == NULL) {
2558 ret = -EIO;
2559 break;
2560 }
2561 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
2562 ret = -EIO;
2563 break;
2564 }
2565 val8 = ((value & 0xff) - 1) << 5;
2566 if (copy_to_user(argp, &val8, sizeof(val8)))
2567 ret = -EFAULT;
2568 break;
2569 case SONYPI_IOCSBRT:
2570 if (sony_backlight_device == NULL) {
2571 ret = -EIO;
2572 break;
2573 }
2574 if (copy_from_user(&val8, argp, sizeof(val8))) {
2575 ret = -EFAULT;
2576 break;
2577 }
2578 if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
2579 (val8 >> 5) + 1, NULL)) {
2580 ret = -EIO;
2581 break;
2582 }
2583 /* sync the backlight device status */
2584 sony_backlight_device->props.brightness =
2585 sony_backlight_get_brightness(sony_backlight_device);
2586 break;
2587 case SONYPI_IOCGBAT1CAP:
2588 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
2589 ret = -EIO;
2590 break;
2591 }
2592 if (copy_to_user(argp, &val16, sizeof(val16)))
2593 ret = -EFAULT;
2594 break;
2595 case SONYPI_IOCGBAT1REM:
2596 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
2597 ret = -EIO;
2598 break;
2599 }
2600 if (copy_to_user(argp, &val16, sizeof(val16)))
2601 ret = -EFAULT;
2602 break;
2603 case SONYPI_IOCGBAT2CAP:
2604 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
2605 ret = -EIO;
2606 break;
2607 }
2608 if (copy_to_user(argp, &val16, sizeof(val16)))
2609 ret = -EFAULT;
2610 break;
2611 case SONYPI_IOCGBAT2REM:
2612 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
2613 ret = -EIO;
2614 break;
2615 }
2616 if (copy_to_user(argp, &val16, sizeof(val16)))
2617 ret = -EFAULT;
2618 break;
2619 case SONYPI_IOCGBATFLAGS:
2620 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
2621 ret = -EIO;
2622 break;
2623 }
2624 val8 &= 0x07;
2625 if (copy_to_user(argp, &val8, sizeof(val8)))
2626 ret = -EFAULT;
2627 break;
2628 case SONYPI_IOCGBLUE:
2629 val8 = spic_dev.bluetooth_power;
2630 if (copy_to_user(argp, &val8, sizeof(val8)))
2631 ret = -EFAULT;
2632 break;
2633 case SONYPI_IOCSBLUE:
2634 if (copy_from_user(&val8, argp, sizeof(val8))) {
2635 ret = -EFAULT;
2636 break;
2637 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002638 __sony_pic_set_bluetoothpower(val8);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002639 break;
2640 /* FAN Controls */
2641 case SONYPI_IOCGFAN:
2642 if (sony_pic_get_fanspeed(&val8)) {
2643 ret = -EIO;
2644 break;
2645 }
2646 if (copy_to_user(argp, &val8, sizeof(val8)))
2647 ret = -EFAULT;
2648 break;
2649 case SONYPI_IOCSFAN:
2650 if (copy_from_user(&val8, argp, sizeof(val8))) {
2651 ret = -EFAULT;
2652 break;
2653 }
2654 if (sony_pic_set_fanspeed(val8))
2655 ret = -EIO;
2656 break;
2657 /* GET Temperature (useful under APM) */
2658 case SONYPI_IOCGTEMP:
2659 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
2660 ret = -EIO;
2661 break;
2662 }
2663 if (copy_to_user(argp, &val8, sizeof(val8)))
2664 ret = -EFAULT;
2665 break;
2666 default:
2667 ret = -EINVAL;
2668 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002669 mutex_unlock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002670 return ret;
2671}
2672
2673static const struct file_operations sonypi_misc_fops = {
2674 .owner = THIS_MODULE,
2675 .read = sonypi_misc_read,
2676 .poll = sonypi_misc_poll,
2677 .open = sonypi_misc_open,
2678 .release = sonypi_misc_release,
2679 .fasync = sonypi_misc_fasync,
Alan Cox2b24ef02009-03-26 21:58:19 +09002680 .unlocked_ioctl = sonypi_misc_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002681 .llseek = noop_llseek,
malattia@linux.it7b153f32007-04-09 19:31:25 +02002682};
2683
2684static struct miscdevice sonypi_misc_device = {
2685 .minor = MISC_DYNAMIC_MINOR,
2686 .name = "sonypi",
2687 .fops = &sonypi_misc_fops,
2688};
2689
2690static void sonypi_compat_report_event(u8 event)
2691{
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002692 kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002693 sizeof(event), &sonypi_compat.fifo_lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002694 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
2695 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
2696}
2697
2698static int sonypi_compat_init(void)
2699{
2700 int error;
2701
2702 spin_lock_init(&sonypi_compat.fifo_lock);
Stefani Seibold45465482009-12-21 14:37:26 -08002703 error =
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002704 kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -08002705 if (error) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002706 pr_err(DRV_PFX "kfifo_alloc failed\n");
Stefani Seibold45465482009-12-21 14:37:26 -08002707 return error;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002708 }
2709
2710 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002711
2712 if (minor != -1)
2713 sonypi_misc_device.minor = minor;
2714 error = misc_register(&sonypi_misc_device);
2715 if (error) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002716 pr_err(DRV_PFX "misc_register failed\n");
malattia@linux.it7b153f32007-04-09 19:31:25 +02002717 goto err_free_kfifo;
2718 }
2719 if (minor == -1)
Mattia Dongilid6697932011-02-19 11:52:28 +09002720 pr_info(DRV_PFX "device allocated minor is %d\n",
malattia@linux.it7b153f32007-04-09 19:31:25 +02002721 sonypi_misc_device.minor);
2722
2723 return 0;
2724
2725err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -08002726 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002727 return error;
2728}
2729
2730static void sonypi_compat_exit(void)
2731{
2732 misc_deregister(&sonypi_misc_device);
Stefani Seibold45465482009-12-21 14:37:26 -08002733 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002734}
2735#else
2736static int sonypi_compat_init(void) { return 0; }
2737static void sonypi_compat_exit(void) { }
2738static void sonypi_compat_report_event(u8 event) { }
Mattia Dongilia64e62a2007-05-01 11:19:53 +09002739#endif /* CONFIG_SONYPI_COMPAT */
malattia@linux.it7b153f32007-04-09 19:31:25 +02002740
malattia@linux.it1549ee62007-04-09 10:19:08 +02002741/*
malattia@linux.it33a04452007-04-09 19:26:03 +02002742 * ACPI callbacks
malattia@linux.it1549ee62007-04-09 10:19:08 +02002743 */
malattia@linux.it33a04452007-04-09 19:26:03 +02002744static acpi_status
2745sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
2746{
2747 u32 i;
2748 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
2749
2750 switch (resource->type) {
2751 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002752 {
2753 /* start IO enumeration */
2754 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
2755 if (!ioport)
2756 return AE_ERROR;
2757
2758 list_add(&ioport->list, &dev->ioports);
2759 return AE_OK;
2760 }
2761
malattia@linux.it33a04452007-04-09 19:26:03 +02002762 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002763 /* end IO enumeration */
malattia@linux.it33a04452007-04-09 19:26:03 +02002764 return AE_OK;
2765
2766 case ACPI_RESOURCE_TYPE_IRQ:
2767 {
2768 struct acpi_resource_irq *p = &resource->data.irq;
2769 struct sony_pic_irq *interrupt = NULL;
2770 if (!p || !p->interrupt_count) {
2771 /*
2772 * IRQ descriptors may have no IRQ# bits set,
2773 * particularly those those w/ _STA disabled
2774 */
2775 dprintk("Blank IRQ resource\n");
2776 return AE_OK;
2777 }
2778 for (i = 0; i < p->interrupt_count; i++) {
2779 if (!p->interrupts[i]) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002780 pr_warn(DRV_PFX "Invalid IRQ %d\n",
malattia@linux.it33a04452007-04-09 19:26:03 +02002781 p->interrupts[i]);
2782 continue;
2783 }
2784 interrupt = kzalloc(sizeof(*interrupt),
2785 GFP_KERNEL);
2786 if (!interrupt)
2787 return AE_ERROR;
2788
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002789 list_add(&interrupt->list, &dev->interrupts);
malattia@linux.it33a04452007-04-09 19:26:03 +02002790 interrupt->irq.triggering = p->triggering;
2791 interrupt->irq.polarity = p->polarity;
2792 interrupt->irq.sharable = p->sharable;
2793 interrupt->irq.interrupt_count = 1;
2794 interrupt->irq.interrupts[0] = p->interrupts[i];
2795 }
2796 return AE_OK;
2797 }
2798 case ACPI_RESOURCE_TYPE_IO:
2799 {
2800 struct acpi_resource_io *io = &resource->data.io;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002801 struct sony_pic_ioport *ioport =
2802 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
malattia@linux.it33a04452007-04-09 19:26:03 +02002803 if (!io) {
2804 dprintk("Blank IO resource\n");
2805 return AE_OK;
2806 }
2807
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002808 if (!ioport->io1.minimum) {
2809 memcpy(&ioport->io1, io, sizeof(*io));
2810 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
2811 ioport->io1.address_length);
2812 }
2813 else if (!ioport->io2.minimum) {
2814 memcpy(&ioport->io2, io, sizeof(*io));
2815 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
2816 ioport->io2.address_length);
2817 }
2818 else {
Mattia Dongilid6697932011-02-19 11:52:28 +09002819 pr_err(DRV_PFX "Unknown SPIC Type, more than 2 IO Ports\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002820 return AE_ERROR;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002821 }
malattia@linux.it33a04452007-04-09 19:26:03 +02002822 return AE_OK;
2823 }
2824 default:
2825 dprintk("Resource %d isn't an IRQ nor an IO port\n",
2826 resource->type);
2827
2828 case ACPI_RESOURCE_TYPE_END_TAG:
2829 return AE_OK;
2830 }
2831 return AE_CTRL_TERMINATE;
2832}
2833
2834static int sony_pic_possible_resources(struct acpi_device *device)
2835{
2836 int result = 0;
2837 acpi_status status = AE_OK;
2838
2839 if (!device)
2840 return -EINVAL;
2841
2842 /* get device status */
2843 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
2844 dprintk("Evaluating _STA\n");
2845 result = acpi_bus_get_status(device);
2846 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002847 pr_warn(DRV_PFX "Unable to read status\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002848 goto end;
2849 }
2850
2851 if (!device->status.enabled)
2852 dprintk("Device disabled\n");
2853 else
2854 dprintk("Device enabled\n");
2855
2856 /*
2857 * Query and parse 'method'
2858 */
2859 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
2860 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
2861 sony_pic_read_possible_resource, &spic_dev);
2862 if (ACPI_FAILURE(status)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002863 pr_warn(DRV_PFX "Failure evaluating %s\n",
malattia@linux.it33a04452007-04-09 19:26:03 +02002864 METHOD_NAME__PRS);
2865 result = -ENODEV;
2866 }
2867end:
2868 return result;
2869}
2870
2871/*
2872 * Disable the spic device by calling its _DIS method
2873 */
2874static int sony_pic_disable(struct acpi_device *device)
2875{
Matthew Garrett6158d3a2008-10-29 14:01:03 -07002876 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
2877 NULL);
2878
2879 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
malattia@linux.it33a04452007-04-09 19:26:03 +02002880 return -ENXIO;
2881
2882 dprintk("Device disabled\n");
2883 return 0;
2884}
2885
2886
2887/*
2888 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
2889 *
2890 * Call _SRS to set current resources
2891 */
2892static int sony_pic_enable(struct acpi_device *device,
2893 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
2894{
2895 acpi_status status;
2896 int result = 0;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002897 /* Type 1 resource layout is:
2898 * IO
2899 * IO
2900 * IRQNoFlags
2901 * End
2902 *
2903 * Type 2 and 3 resource layout is:
2904 * IO
2905 * IRQNoFlags
2906 * End
2907 */
malattia@linux.it33a04452007-04-09 19:26:03 +02002908 struct {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002909 struct acpi_resource res1;
2910 struct acpi_resource res2;
2911 struct acpi_resource res3;
2912 struct acpi_resource res4;
malattia@linux.it33a04452007-04-09 19:26:03 +02002913 } *resource;
2914 struct acpi_buffer buffer = { 0, NULL };
2915
2916 if (!ioport || !irq)
2917 return -EINVAL;
2918
2919 /* init acpi_buffer */
2920 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
2921 if (!resource)
2922 return -ENOMEM;
2923
2924 buffer.length = sizeof(*resource) + 1;
2925 buffer.pointer = resource;
2926
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002927 /* setup Type 1 resources */
Mattia Dongili31df7142009-09-16 00:05:29 +09002928 if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002929
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002930 /* setup io resources */
2931 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2932 resource->res1.length = sizeof(struct acpi_resource);
2933 memcpy(&resource->res1.data.io, &ioport->io1,
2934 sizeof(struct acpi_resource_io));
malattia@linux.it33a04452007-04-09 19:26:03 +02002935
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002936 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
2937 resource->res2.length = sizeof(struct acpi_resource);
2938 memcpy(&resource->res2.data.io, &ioport->io2,
2939 sizeof(struct acpi_resource_io));
2940
2941 /* setup irq resource */
2942 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
2943 resource->res3.length = sizeof(struct acpi_resource);
2944 memcpy(&resource->res3.data.irq, &irq->irq,
2945 sizeof(struct acpi_resource_irq));
2946 /* we requested a shared irq */
2947 resource->res3.data.irq.sharable = ACPI_SHARED;
2948
2949 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
2950
2951 }
2952 /* setup Type 2/3 resources */
2953 else {
2954 /* setup io resource */
2955 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2956 resource->res1.length = sizeof(struct acpi_resource);
2957 memcpy(&resource->res1.data.io, &ioport->io1,
2958 sizeof(struct acpi_resource_io));
2959
2960 /* setup irq resource */
2961 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
2962 resource->res2.length = sizeof(struct acpi_resource);
2963 memcpy(&resource->res2.data.irq, &irq->irq,
2964 sizeof(struct acpi_resource_irq));
2965 /* we requested a shared irq */
2966 resource->res2.data.irq.sharable = ACPI_SHARED;
2967
2968 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
2969 }
malattia@linux.it33a04452007-04-09 19:26:03 +02002970
2971 /* Attempt to set the resource */
2972 dprintk("Evaluating _SRS\n");
2973 status = acpi_set_current_resources(device->handle, &buffer);
2974
2975 /* check for total failure */
2976 if (ACPI_FAILURE(status)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002977 pr_err(DRV_PFX "Error evaluating _SRS\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002978 result = -ENODEV;
2979 goto end;
2980 }
2981
2982 /* Necessary device initializations calls (from sonypi) */
2983 sony_pic_call1(0x82);
2984 sony_pic_call2(0x81, 0xff);
2985 sony_pic_call1(compat ? 0x92 : 0x82);
2986
2987end:
2988 kfree(resource);
2989 return result;
2990}
2991
2992/*****************
2993 *
2994 * ISR: some event is available
2995 *
2996 *****************/
2997static irqreturn_t sony_pic_irq(int irq, void *dev_id)
2998{
2999 int i, j;
malattia@linux.it33a04452007-04-09 19:26:03 +02003000 u8 ev = 0;
3001 u8 data_mask = 0;
3002 u8 device_event = 0;
3003
3004 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
3005
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003006 ev = inb_p(dev->cur_ioport->io1.minimum);
3007 if (dev->cur_ioport->io2.minimum)
3008 data_mask = inb_p(dev->cur_ioport->io2.minimum);
3009 else
Mattia Dongilide920432008-01-14 18:05:43 +09003010 data_mask = inb_p(dev->cur_ioport->io1.minimum +
Mattia Dongili31df7142009-09-16 00:05:29 +09003011 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003012
Mattia Dongili22a17782007-07-16 02:34:39 +09003013 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
Mattia Dongilide920432008-01-14 18:05:43 +09003014 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09003015 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003016
3017 if (ev == 0x00 || ev == 0xff)
3018 return IRQ_HANDLED;
3019
Mattia Dongili31df7142009-09-16 00:05:29 +09003020 for (i = 0; dev->event_types[i].mask; i++) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003021
Mattia Dongili31df7142009-09-16 00:05:29 +09003022 if ((data_mask & dev->event_types[i].data) !=
3023 dev->event_types[i].data)
malattia@linux.it33a04452007-04-09 19:26:03 +02003024 continue;
3025
Mattia Dongili31df7142009-09-16 00:05:29 +09003026 if (!(mask & dev->event_types[i].mask))
malattia@linux.it33a04452007-04-09 19:26:03 +02003027 continue;
3028
Mattia Dongili31df7142009-09-16 00:05:29 +09003029 for (j = 0; dev->event_types[i].events[j].event; j++) {
3030 if (ev == dev->event_types[i].events[j].data) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003031 device_event =
Mattia Dongili31df7142009-09-16 00:05:29 +09003032 dev->event_types[i].events[j].event;
Mattia Dongili4eeb5022011-02-19 11:52:27 +09003033 /* some events may require ignoring */
3034 if (!device_event)
3035 return IRQ_HANDLED;
malattia@linux.it33a04452007-04-09 19:26:03 +02003036 goto found;
3037 }
3038 }
3039 }
Mattia Dongili3eb87492008-01-14 18:05:45 +09003040 /* Still not able to decode the event try to pass
3041 * it over to the minidriver
3042 */
Mattia Dongili31df7142009-09-16 00:05:29 +09003043 if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
Mattia Dongili3eb87492008-01-14 18:05:45 +09003044 return IRQ_HANDLED;
3045
Mattia Dongilide920432008-01-14 18:05:43 +09003046 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
3047 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09003048 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003049 return IRQ_HANDLED;
3050
3051found:
malattia@linux.it1549ee62007-04-09 10:19:08 +02003052 sony_laptop_report_input_event(device_event);
Mattia Dongilide920432008-01-14 18:05:43 +09003053 acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003054 sonypi_compat_report_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02003055 return IRQ_HANDLED;
3056}
3057
3058/*****************
3059 *
3060 * ACPI driver
3061 *
3062 *****************/
3063static int sony_pic_remove(struct acpi_device *device, int type)
3064{
3065 struct sony_pic_ioport *io, *tmp_io;
3066 struct sony_pic_irq *irq, *tmp_irq;
3067
3068 if (sony_pic_disable(device)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003069 pr_err(DRV_PFX "Couldn't disable device.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003070 return -ENXIO;
3071 }
3072
3073 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003074 release_region(spic_dev.cur_ioport->io1.minimum,
3075 spic_dev.cur_ioport->io1.address_length);
3076 if (spic_dev.cur_ioport->io2.minimum)
3077 release_region(spic_dev.cur_ioport->io2.minimum,
3078 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02003079
Mattia Dongili015a9162007-08-12 16:20:27 +09003080 sonypi_compat_exit();
3081
malattia@linux.it1549ee62007-04-09 10:19:08 +02003082 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02003083
malattia@linux.it49a11de2007-04-09 19:28:56 +02003084 /* pf attrs */
3085 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
3086 sony_pf_remove();
3087
malattia@linux.it33a04452007-04-09 19:26:03 +02003088 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
3089 list_del(&io->list);
3090 kfree(io);
3091 }
3092 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
3093 list_del(&irq->list);
3094 kfree(irq);
3095 }
3096 spic_dev.cur_ioport = NULL;
3097 spic_dev.cur_irq = NULL;
3098
malattia@linux.itf6119b02007-04-09 19:31:06 +02003099 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003100 return 0;
3101}
3102
3103static int sony_pic_add(struct acpi_device *device)
3104{
3105 int result;
3106 struct sony_pic_ioport *io, *tmp_io;
3107 struct sony_pic_irq *irq, *tmp_irq;
3108
Mattia Dongilid6697932011-02-19 11:52:28 +09003109 pr_info(DRV_PFX "%s v%s.\n", SONY_PIC_DRIVER_NAME,
3110 SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.it33a04452007-04-09 19:26:03 +02003111
3112 spic_dev.acpi_dev = device;
3113 strcpy(acpi_device_class(device), "sony/hotkey");
Mattia Dongilide920432008-01-14 18:05:43 +09003114 sony_pic_detect_device_type(&spic_dev);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003115 mutex_init(&spic_dev.lock);
malattia@linux.it33a04452007-04-09 19:26:03 +02003116
3117 /* read _PRS resources */
3118 result = sony_pic_possible_resources(device);
3119 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003120 pr_err(DRV_PFX "Unable to read possible resources.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003121 goto err_free_resources;
3122 }
3123
3124 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05003125 result = sony_laptop_setup_input(device);
malattia@linux.it33a04452007-04-09 19:26:03 +02003126 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003127 pr_err(DRV_PFX "Unable to create input devices.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003128 goto err_free_resources;
3129 }
3130
Mattia Dongili015a9162007-08-12 16:20:27 +09003131 if (sonypi_compat_init())
3132 goto err_remove_input;
3133
malattia@linux.it33a04452007-04-09 19:26:03 +02003134 /* request io port */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003135 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
3136 if (request_region(io->io1.minimum, io->io1.address_length,
Lucas De Marchic8440332011-03-17 17:18:22 -03003137 "Sony Programmable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003138 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
3139 io->io1.minimum, io->io1.maximum,
3140 io->io1.address_length);
3141 /* Type 1 have 2 ioports */
3142 if (io->io2.minimum) {
3143 if (request_region(io->io2.minimum,
3144 io->io2.address_length,
Lucas De Marchic8440332011-03-17 17:18:22 -03003145 "Sony Programmable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003146 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
3147 io->io2.minimum, io->io2.maximum,
3148 io->io2.address_length);
3149 spic_dev.cur_ioport = io;
3150 break;
3151 }
3152 else {
3153 dprintk("Unable to get I/O port2: "
3154 "0x%.4x (0x%.4x) + 0x%.2x\n",
3155 io->io2.minimum, io->io2.maximum,
3156 io->io2.address_length);
3157 release_region(io->io1.minimum,
3158 io->io1.address_length);
3159 }
3160 }
3161 else {
3162 spic_dev.cur_ioport = io;
3163 break;
3164 }
malattia@linux.it33a04452007-04-09 19:26:03 +02003165 }
3166 }
3167 if (!spic_dev.cur_ioport) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003168 pr_err(DRV_PFX "Failed to request_region.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003169 result = -ENODEV;
Mattia Dongili015a9162007-08-12 16:20:27 +09003170 goto err_remove_compat;
malattia@linux.it33a04452007-04-09 19:26:03 +02003171 }
3172
3173 /* request IRQ */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003174 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003175 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
Mattia Dongilid1e0de92009-09-16 00:05:30 +09003176 IRQF_DISABLED, "sony-laptop", &spic_dev)) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003177 dprintk("IRQ: %d - triggering: %d - "
3178 "polarity: %d - shr: %d\n",
3179 irq->irq.interrupts[0],
3180 irq->irq.triggering,
3181 irq->irq.polarity,
3182 irq->irq.sharable);
3183 spic_dev.cur_irq = irq;
3184 break;
3185 }
3186 }
3187 if (!spic_dev.cur_irq) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003188 pr_err(DRV_PFX "Failed to request_irq.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003189 result = -ENODEV;
3190 goto err_release_region;
3191 }
3192
3193 /* set resource status _SRS */
3194 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
3195 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003196 pr_err(DRV_PFX "Couldn't enable device.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003197 goto err_free_irq;
3198 }
3199
malattia@linux.it49a11de2007-04-09 19:28:56 +02003200 spic_dev.bluetooth_power = -1;
3201 /* create device attributes */
3202 result = sony_pf_add();
3203 if (result)
3204 goto err_disable_device;
3205
3206 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
3207 if (result)
3208 goto err_remove_pf;
3209
malattia@linux.it33a04452007-04-09 19:26:03 +02003210 return 0;
3211
malattia@linux.it49a11de2007-04-09 19:28:56 +02003212err_remove_pf:
3213 sony_pf_remove();
3214
3215err_disable_device:
3216 sony_pic_disable(device);
3217
malattia@linux.it33a04452007-04-09 19:26:03 +02003218err_free_irq:
3219 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
3220
3221err_release_region:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003222 release_region(spic_dev.cur_ioport->io1.minimum,
3223 spic_dev.cur_ioport->io1.address_length);
3224 if (spic_dev.cur_ioport->io2.minimum)
3225 release_region(spic_dev.cur_ioport->io2.minimum,
3226 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02003227
Mattia Dongili015a9162007-08-12 16:20:27 +09003228err_remove_compat:
3229 sonypi_compat_exit();
3230
malattia@linux.it33a04452007-04-09 19:26:03 +02003231err_remove_input:
malattia@linux.it1549ee62007-04-09 10:19:08 +02003232 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02003233
3234err_free_resources:
3235 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
3236 list_del(&io->list);
3237 kfree(io);
3238 }
3239 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
3240 list_del(&irq->list);
3241 kfree(irq);
3242 }
3243 spic_dev.cur_ioport = NULL;
3244 spic_dev.cur_irq = NULL;
3245
3246 return result;
3247}
3248
3249static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
3250{
3251 if (sony_pic_disable(device))
3252 return -ENXIO;
3253 return 0;
3254}
3255
3256static int sony_pic_resume(struct acpi_device *device)
3257{
3258 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
3259 return 0;
3260}
3261
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003262static const struct acpi_device_id sony_pic_device_ids[] = {
3263 {SONY_PIC_HID, 0},
3264 {"", 0},
3265};
3266
malattia@linux.it33a04452007-04-09 19:26:03 +02003267static struct acpi_driver sony_pic_driver = {
3268 .name = SONY_PIC_DRIVER_NAME,
3269 .class = SONY_PIC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003270 .ids = sony_pic_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02003271 .owner = THIS_MODULE,
3272 .ops = {
3273 .add = sony_pic_add,
3274 .remove = sony_pic_remove,
3275 .suspend = sony_pic_suspend,
3276 .resume = sony_pic_resume,
3277 },
3278};
3279
3280static struct dmi_system_id __initdata sonypi_dmi_table[] = {
3281 {
3282 .ident = "Sony Vaio",
3283 .matches = {
3284 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
3285 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
3286 },
3287 },
3288 {
3289 .ident = "Sony Vaio",
3290 .matches = {
3291 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
3292 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
3293 },
3294 },
3295 { }
3296};
3297
malattia@linux.it59b19102007-04-09 10:19:04 +02003298static int __init sony_laptop_init(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01003299{
malattia@linux.it33a04452007-04-09 19:26:03 +02003300 int result;
3301
3302 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
3303 result = acpi_bus_register_driver(&sony_pic_driver);
3304 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003305 pr_err(DRV_PFX "Unable to register SPIC driver.");
malattia@linux.it33a04452007-04-09 19:26:03 +02003306 goto out;
3307 }
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003308 spic_drv_registered = 1;
malattia@linux.it33a04452007-04-09 19:26:03 +02003309 }
3310
3311 result = acpi_bus_register_driver(&sony_nc_driver);
3312 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003313 pr_err(DRV_PFX "Unable to register SNC driver.");
malattia@linux.it33a04452007-04-09 19:26:03 +02003314 goto out_unregister_pic;
3315 }
3316
3317 return 0;
3318
3319out_unregister_pic:
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003320 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02003321 acpi_bus_unregister_driver(&sony_pic_driver);
3322out:
3323 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +01003324}
3325
malattia@linux.it59b19102007-04-09 10:19:04 +02003326static void __exit sony_laptop_exit(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01003327{
malattia@linux.it59b19102007-04-09 10:19:04 +02003328 acpi_bus_unregister_driver(&sony_nc_driver);
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003329 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02003330 acpi_bus_unregister_driver(&sony_pic_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +01003331}
3332
malattia@linux.it59b19102007-04-09 10:19:04 +02003333module_init(sony_laptop_init);
3334module_exit(sony_laptop_exit);