blob: 326a4600fad19e3cc3d90435e6e7eb8b4634d4d8 [file] [log] [blame]
Stelian Pop7f09c432007-01-13 23:04:31 +01001/*
malattia@linux.it33a04452007-04-09 19:26:03 +02002 * ACPI Sony Notebook Control Driver (SNC and SPIC)
Stelian Pop7f09c432007-01-13 23:04:31 +01003 *
4 * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
Mattia Dongili37418342009-03-26 21:58:24 +09005 * Copyright (C) 2007-2009 Mattia Dongili <malattia@linux.it>
Stelian Pop7f09c432007-01-13 23:04:31 +01006 *
7 * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
8 * which are copyrighted by their respective authors.
9 *
malattia@linux.it33a04452007-04-09 19:26:03 +020010 * The SNY6001 driver part is based on the sonypi driver which includes
11 * material from:
12 *
13 * Copyright (C) 2001-2005 Stelian Pop <stelian@popies.net>
14 *
15 * Copyright (C) 2005 Narayanan R S <nars@kadamba.org>
16 *
Marcin Garskidb955172007-10-19 23:22:11 +020017 * Copyright (C) 2001-2002 AlcĂ´ve <www.alcove.com>
malattia@linux.it33a04452007-04-09 19:26:03 +020018 *
19 * Copyright (C) 2001 Michael Ashley <m.ashley@unsw.edu.au>
20 *
21 * Copyright (C) 2001 Junichi Morita <jun1m@mars.dti.ne.jp>
22 *
23 * Copyright (C) 2000 Takaya Kinjo <t-kinjo@tc4.so-net.ne.jp>
24 *
25 * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
26 *
27 * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
28 *
Stelian Pop7f09c432007-01-13 23:04:31 +010029 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
33 *
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
38 *
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
42 *
43 */
44
45#include <linux/kernel.h>
46#include <linux/module.h>
47#include <linux/moduleparam.h>
48#include <linux/init.h>
49#include <linux/types.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010050#include <linux/backlight.h>
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010051#include <linux/platform_device.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010052#include <linux/err.h>
malattia@linux.it33a04452007-04-09 19:26:03 +020053#include <linux/dmi.h>
54#include <linux/pci.h>
55#include <linux/interrupt.h>
56#include <linux/delay.h>
57#include <linux/input.h>
58#include <linux/kfifo.h>
59#include <linux/workqueue.h>
60#include <linux/acpi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090061#include <linux/slab.h>
Stelian Pop7f09c432007-01-13 23:04:31 +010062#include <acpi/acpi_drivers.h>
63#include <acpi/acpi_bus.h>
64#include <asm/uaccess.h>
malattia@linux.it33a04452007-04-09 19:26:03 +020065#include <linux/sonypi.h>
malattia@linux.it1ce82c12007-04-28 23:34:36 +090066#include <linux/sony-laptop.h>
Matthew Garrett6cc056b2009-03-26 21:58:15 +090067#include <linux/rfkill.h>
Mattia Dongilia64e62a2007-05-01 11:19:53 +090068#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +020069#include <linux/poll.h>
70#include <linux/miscdevice.h>
71#endif
Stelian Pop7f09c432007-01-13 23:04:31 +010072
malattia@linux.it33a04452007-04-09 19:26:03 +020073#define DRV_PFX "sony-laptop: "
Mattia Dongilid6697932011-02-19 11:52:28 +090074#define dprintk(msg...) do { \
75 if (debug) \
76 pr_warn(DRV_PFX msg); \
malattia@linux.itb9a218b2007-04-09 10:19:06 +020077} while (0)
78
Mattia Dongili425ef5d2008-01-14 18:05:44 +090079#define SONY_LAPTOP_DRIVER_VERSION "0.6"
Alessandro Guido50f62af2007-01-13 23:04:34 +010080
malattia@linux.it33a04452007-04-09 19:26:03 +020081#define SONY_NC_CLASS "sony-nc"
82#define SONY_NC_HID "SNY5001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020083#define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
malattia@linux.it33a04452007-04-09 19:26:03 +020084
85#define SONY_PIC_CLASS "sony-pic"
86#define SONY_PIC_HID "SNY6001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020087#define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
Stelian Pop7f09c432007-01-13 23:04:31 +010088
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010089MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
malattia@linux.it33a04452007-04-09 19:26:03 +020090MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
Stelian Pop7f09c432007-01-13 23:04:31 +010091MODULE_LICENSE("GPL");
malattia@linux.it33a04452007-04-09 19:26:03 +020092MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
Stelian Pop7f09c432007-01-13 23:04:31 +010093
94static int debug;
95module_param(debug, int, 0);
96MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
Len Browna02d1c12007-02-07 15:34:02 -050097 "the development of this driver");
Stelian Pop7f09c432007-01-13 23:04:31 +010098
malattia@linux.it33a04452007-04-09 19:26:03 +020099static int no_spic; /* = 0 */
100module_param(no_spic, int, 0444);
101MODULE_PARM_DESC(no_spic,
102 "set this if you don't want to enable the SPIC device");
103
104static int compat; /* = 0 */
105module_param(compat, int, 0444);
106MODULE_PARM_DESC(compat,
malattia@linux.it7b153f32007-04-09 19:31:25 +0200107 "set this if you want to enable backward compatibility mode");
malattia@linux.it33a04452007-04-09 19:26:03 +0200108
109static unsigned long mask = 0xffffffff;
110module_param(mask, ulong, 0644);
111MODULE_PARM_DESC(mask,
112 "set this to the mask of event you want to enable (see doc)");
113
malattia@linux.it5f3d2892007-04-28 23:18:45 +0900114static int camera; /* = 0 */
115module_param(camera, int, 0444);
116MODULE_PARM_DESC(camera,
117 "set this to 1 to enable Motion Eye camera controls "
118 "(only use it if you have a C1VE or C1VN model)");
119
Mattia Dongilia64e62a2007-05-01 11:19:53 +0900120#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +0200121static int minor = -1;
122module_param(minor, int, 0);
123MODULE_PARM_DESC(minor,
124 "minor number of the misc device for the SPIC compatibility code, "
125 "default is -1 (automatic)");
126#endif
127
Mattia Dongilibf155712011-02-19 11:52:31 +0900128static int kbd_backlight; /* = 1 */
129module_param(kbd_backlight, int, 0444);
130MODULE_PARM_DESC(kbd_backlight,
131 "set this to 0 to disable keyboard backlight, "
132 "1 to enable it (default: 0)");
133
134static int kbd_backlight_timeout; /* = 0 */
135module_param(kbd_backlight_timeout, int, 0444);
136MODULE_PARM_DESC(kbd_backlight_timeout,
137 "set this to 0 to set the default 10 seconds timeout, "
138 "1 for 30 seconds, 2 for 60 seconds and 3 to disable timeout "
139 "(default: 0)");
140
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900141enum sony_nc_rfkill {
142 SONY_WIFI,
143 SONY_BLUETOOTH,
144 SONY_WWAN,
145 SONY_WIMAX,
Johannes Berg19d337d2009-06-02 13:01:37 +0200146 N_SONY_RFKILL,
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900147};
148
Mattia Dongilid5a664a2009-12-17 00:08:35 +0900149static int sony_rfkill_handle;
Johannes Berg19d337d2009-06-02 13:01:37 +0200150static struct rfkill *sony_rfkill_devices[N_SONY_RFKILL];
151static int sony_rfkill_address[N_SONY_RFKILL] = {0x300, 0x500, 0x700, 0x900};
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900152static void sony_nc_rfkill_update(void);
153
malattia@linux.it1549ee62007-04-09 10:19:08 +0200154/*********** Input Devices ***********/
155
156#define SONY_LAPTOP_BUF_SIZE 128
157struct sony_laptop_input_s {
158 atomic_t users;
159 struct input_dev *jog_dev;
160 struct input_dev *key_dev;
Stefani Seibold45465482009-12-21 14:37:26 -0800161 struct kfifo fifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200162 spinlock_t fifo_lock;
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800163 struct timer_list release_key_timer;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200164};
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900165
malattia@linux.it1549ee62007-04-09 10:19:08 +0200166static struct sony_laptop_input_s sony_laptop_input = {
167 .users = ATOMIC_INIT(0),
168};
169
170struct sony_laptop_keypress {
171 struct input_dev *dev;
172 int key;
173};
174
Mattia Dongilibc57f862007-07-20 02:01:57 +0900175/* Correspondance table between sonypi events
176 * and input layer indexes in the keymap
177 */
178static int sony_laptop_input_index[] = {
Mattia Dongili3eb87492008-01-14 18:05:45 +0900179 -1, /* 0 no event */
180 -1, /* 1 SONYPI_EVENT_JOGDIAL_DOWN */
181 -1, /* 2 SONYPI_EVENT_JOGDIAL_UP */
182 -1, /* 3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
183 -1, /* 4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */
184 -1, /* 5 SONYPI_EVENT_JOGDIAL_PRESSED */
185 -1, /* 6 SONYPI_EVENT_JOGDIAL_RELEASED */
186 0, /* 7 SONYPI_EVENT_CAPTURE_PRESSED */
187 1, /* 8 SONYPI_EVENT_CAPTURE_RELEASED */
188 2, /* 9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
189 3, /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
190 4, /* 11 SONYPI_EVENT_FNKEY_ESC */
191 5, /* 12 SONYPI_EVENT_FNKEY_F1 */
192 6, /* 13 SONYPI_EVENT_FNKEY_F2 */
193 7, /* 14 SONYPI_EVENT_FNKEY_F3 */
194 8, /* 15 SONYPI_EVENT_FNKEY_F4 */
195 9, /* 16 SONYPI_EVENT_FNKEY_F5 */
196 10, /* 17 SONYPI_EVENT_FNKEY_F6 */
197 11, /* 18 SONYPI_EVENT_FNKEY_F7 */
198 12, /* 19 SONYPI_EVENT_FNKEY_F8 */
199 13, /* 20 SONYPI_EVENT_FNKEY_F9 */
200 14, /* 21 SONYPI_EVENT_FNKEY_F10 */
201 15, /* 22 SONYPI_EVENT_FNKEY_F11 */
202 16, /* 23 SONYPI_EVENT_FNKEY_F12 */
203 17, /* 24 SONYPI_EVENT_FNKEY_1 */
204 18, /* 25 SONYPI_EVENT_FNKEY_2 */
205 19, /* 26 SONYPI_EVENT_FNKEY_D */
206 20, /* 27 SONYPI_EVENT_FNKEY_E */
207 21, /* 28 SONYPI_EVENT_FNKEY_F */
208 22, /* 29 SONYPI_EVENT_FNKEY_S */
209 23, /* 30 SONYPI_EVENT_FNKEY_B */
210 24, /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */
211 25, /* 32 SONYPI_EVENT_PKEY_P1 */
212 26, /* 33 SONYPI_EVENT_PKEY_P2 */
213 27, /* 34 SONYPI_EVENT_PKEY_P3 */
214 28, /* 35 SONYPI_EVENT_BACK_PRESSED */
215 -1, /* 36 SONYPI_EVENT_LID_CLOSED */
216 -1, /* 37 SONYPI_EVENT_LID_OPENED */
217 29, /* 38 SONYPI_EVENT_BLUETOOTH_ON */
218 30, /* 39 SONYPI_EVENT_BLUETOOTH_OFF */
219 31, /* 40 SONYPI_EVENT_HELP_PRESSED */
220 32, /* 41 SONYPI_EVENT_FNKEY_ONLY */
221 33, /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
222 34, /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */
223 35, /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
224 36, /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
225 37, /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
226 38, /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */
227 39, /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
228 40, /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
229 41, /* 50 SONYPI_EVENT_ZOOM_PRESSED */
230 42, /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */
231 43, /* 52 SONYPI_EVENT_MEYE_FACE */
232 44, /* 53 SONYPI_EVENT_MEYE_OPPOSITE */
233 45, /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */
234 46, /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */
235 -1, /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */
236 -1, /* 57 SONYPI_EVENT_BATTERY_INSERT */
237 -1, /* 58 SONYPI_EVENT_BATTERY_REMOVE */
238 -1, /* 59 SONYPI_EVENT_FNKEY_RELEASED */
239 47, /* 60 SONYPI_EVENT_WIRELESS_ON */
240 48, /* 61 SONYPI_EVENT_WIRELESS_OFF */
241 49, /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */
242 50, /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900243 51, /* 64 SONYPI_EVENT_CD_EJECT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900244 52, /* 65 SONYPI_EVENT_MODEKEY_PRESSED */
245 53, /* 66 SONYPI_EVENT_PKEY_P4 */
246 54, /* 67 SONYPI_EVENT_PKEY_P5 */
247 55, /* 68 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900248 56, /* 69 SONYPI_EVENT_VOLUME_INC_PRESSED */
249 57, /* 70 SONYPI_EVENT_VOLUME_DEC_PRESSED */
250 -1, /* 71 SONYPI_EVENT_BRIGHTNESS_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900251 58, /* 72 SONYPI_EVENT_MEDIA_PRESSED */
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900252 59, /* 72 SONYPI_EVENT_VENDOR_PRESSED */
Mattia Dongilibc57f862007-07-20 02:01:57 +0900253};
254
255static int sony_laptop_input_keycode_map[] = {
256 KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
257 KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
258 KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
259 KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
260 KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
261 KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
262 KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
263 KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
264 KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
265 KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
266 KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
267 KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
268 KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
269 KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
270 KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
271 KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
272 KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
273 KEY_FN_F1, /* 17 SONYPI_EVENT_FNKEY_1 */
274 KEY_FN_F2, /* 18 SONYPI_EVENT_FNKEY_2 */
275 KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
276 KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
277 KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
278 KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
279 KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
280 KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
281 KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
282 KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
283 KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
284 KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
285 KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
286 KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
287 KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
288 KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
289 KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
290 KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
291 KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
292 KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
293 KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
294 KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
295 KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
296 KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
297 KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
298 BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
299 KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
300 KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
301 KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
302 KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
303 KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
304 KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
Mattia Dongili3eb87492008-01-14 18:05:45 +0900305 KEY_ZOOMIN, /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900306 KEY_ZOOMOUT, /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900307 KEY_EJECTCD, /* 51 SONYPI_EVENT_CD_EJECT_PRESSED */
308 KEY_F13, /* 52 SONYPI_EVENT_MODEKEY_PRESSED */
309 KEY_PROG4, /* 53 SONYPI_EVENT_PKEY_P4 */
310 KEY_F14, /* 54 SONYPI_EVENT_PKEY_P5 */
311 KEY_F15, /* 55 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900312 KEY_VOLUMEUP, /* 56 SONYPI_EVENT_VOLUME_INC_PRESSED */
313 KEY_VOLUMEDOWN, /* 57 SONYPI_EVENT_VOLUME_DEC_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900314 KEY_MEDIA, /* 58 SONYPI_EVENT_MEDIA_PRESSED */
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900315 KEY_VENDOR, /* 59 SONYPI_EVENT_VENDOR_PRESSED */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200316};
317
318/* release buttons after a short delay if pressed */
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800319static void do_sony_laptop_release_key(unsigned long unused)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200320{
321 struct sony_laptop_keypress kp;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800322 unsigned long flags;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200323
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800324 spin_lock_irqsave(&sony_laptop_input.fifo_lock, flags);
325
326 if (kfifo_out(&sony_laptop_input.fifo,
327 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200328 input_report_key(kp.dev, kp.key, 0);
329 input_sync(kp.dev);
330 }
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800331
332 /* If there is something in the fifo schedule next release. */
333 if (kfifo_len(&sony_laptop_input.fifo) != 0)
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800334 mod_timer(&sony_laptop_input.release_key_timer,
335 jiffies + msecs_to_jiffies(10));
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800336
337 spin_unlock_irqrestore(&sony_laptop_input.fifo_lock, flags);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200338}
malattia@linux.it1549ee62007-04-09 10:19:08 +0200339
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200340/* forward event to the input subsystem */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200341static void sony_laptop_report_input_event(u8 event)
342{
343 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
344 struct input_dev *key_dev = sony_laptop_input.key_dev;
345 struct sony_laptop_keypress kp = { NULL };
malattia@linux.it1549ee62007-04-09 10:19:08 +0200346
Almer S. Tigelaara83021a2009-04-12 11:26:28 +0000347 if (event == SONYPI_EVENT_FNKEY_RELEASED ||
348 event == SONYPI_EVENT_ANYBUTTON_RELEASED) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200349 /* Nothing, not all VAIOs generate this event */
350 return;
351 }
352
353 /* report events */
354 switch (event) {
355 /* jog_dev events */
356 case SONYPI_EVENT_JOGDIAL_UP:
357 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
358 input_report_rel(jog_dev, REL_WHEEL, 1);
359 input_sync(jog_dev);
360 return;
361
362 case SONYPI_EVENT_JOGDIAL_DOWN:
363 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
364 input_report_rel(jog_dev, REL_WHEEL, -1);
365 input_sync(jog_dev);
366 return;
367
368 /* key_dev events */
369 case SONYPI_EVENT_JOGDIAL_PRESSED:
370 kp.key = BTN_MIDDLE;
371 kp.dev = jog_dev;
372 break;
373
374 default:
Adrian Bunkd399d132008-02-20 00:59:03 +0200375 if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
Mattia Dongilibc57f862007-07-20 02:01:57 +0900376 dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
377 break;
378 }
379 if (sony_laptop_input_index[event] != -1) {
380 kp.key = sony_laptop_input_keycode_map[sony_laptop_input_index[event]];
381 if (kp.key != KEY_UNKNOWN)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200382 kp.dev = key_dev;
Mattia Dongilibc57f862007-07-20 02:01:57 +0900383 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200384 break;
385 }
386
387 if (kp.dev) {
388 input_report_key(kp.dev, kp.key, 1);
Mattia Dongilibc57f862007-07-20 02:01:57 +0900389 /* we emit the scancode so we can always remap the key */
390 input_event(kp.dev, EV_MSC, MSC_SCAN, event);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200391 input_sync(kp.dev);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200392
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800393 /* schedule key release */
394 kfifo_in_locked(&sony_laptop_input.fifo,
395 (unsigned char *)&kp, sizeof(kp),
396 &sony_laptop_input.fifo_lock);
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800397 mod_timer(&sony_laptop_input.release_key_timer,
398 jiffies + msecs_to_jiffies(10));
malattia@linux.it1549ee62007-04-09 10:19:08 +0200399 } else
400 dprintk("unknown input event %.2x\n", event);
401}
402
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500403static int sony_laptop_setup_input(struct acpi_device *acpi_device)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200404{
405 struct input_dev *jog_dev;
406 struct input_dev *key_dev;
407 int i;
408 int error;
409
410 /* don't run again if already initialized */
411 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
412 return 0;
413
414 /* kfifo */
415 spin_lock_init(&sony_laptop_input.fifo_lock);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800416 error = kfifo_alloc(&sony_laptop_input.fifo,
417 SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -0800418 if (error) {
Mattia Dongilid6697932011-02-19 11:52:28 +0900419 pr_err(DRV_PFX "kfifo_alloc failed\n");
malattia@linux.it1549ee62007-04-09 10:19:08 +0200420 goto err_dec_users;
421 }
422
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800423 setup_timer(&sony_laptop_input.release_key_timer,
424 do_sony_laptop_release_key, 0);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200425
426 /* input keys */
427 key_dev = input_allocate_device();
428 if (!key_dev) {
429 error = -ENOMEM;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800430 goto err_free_kfifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200431 }
432
433 key_dev->name = "Sony Vaio Keys";
434 key_dev->id.bustype = BUS_ISA;
435 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500436 key_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200437
438 /* Initialize the Input Drivers: special keys */
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800439 input_set_capability(key_dev, EV_MSC, MSC_SCAN);
440
441 __set_bit(EV_KEY, key_dev->evbit);
Mattia Dongilibc57f862007-07-20 02:01:57 +0900442 key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
443 key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
444 key_dev->keycode = &sony_laptop_input_keycode_map;
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800445 for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++)
446 __set_bit(sony_laptop_input_keycode_map[i], key_dev->keybit);
447 __clear_bit(KEY_RESERVED, key_dev->keybit);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200448
449 error = input_register_device(key_dev);
450 if (error)
451 goto err_free_keydev;
452
453 sony_laptop_input.key_dev = key_dev;
454
455 /* jogdial */
456 jog_dev = input_allocate_device();
457 if (!jog_dev) {
458 error = -ENOMEM;
459 goto err_unregister_keydev;
460 }
461
462 jog_dev->name = "Sony Vaio Jogdial";
463 jog_dev->id.bustype = BUS_ISA;
464 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500465 key_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200466
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800467 input_set_capability(jog_dev, EV_KEY, BTN_MIDDLE);
468 input_set_capability(jog_dev, EV_REL, REL_WHEEL);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200469
470 error = input_register_device(jog_dev);
471 if (error)
472 goto err_free_jogdev;
473
474 sony_laptop_input.jog_dev = jog_dev;
475
476 return 0;
477
478err_free_jogdev:
479 input_free_device(jog_dev);
480
481err_unregister_keydev:
482 input_unregister_device(key_dev);
483 /* to avoid kref underflow below at input_free_device */
484 key_dev = NULL;
485
486err_free_keydev:
487 input_free_device(key_dev);
488
malattia@linux.it1549ee62007-04-09 10:19:08 +0200489err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -0800490 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200491
492err_dec_users:
493 atomic_dec(&sony_laptop_input.users);
494 return error;
495}
496
497static void sony_laptop_remove_input(void)
498{
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800499 struct sony_laptop_keypress kp = { NULL };
500
501 /* Cleanup only after the last user has gone */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200502 if (!atomic_dec_and_test(&sony_laptop_input.users))
503 return;
504
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800505 del_timer_sync(&sony_laptop_input.release_key_timer);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800506
507 /*
508 * Generate key-up events for remaining keys. Note that we don't
509 * need locking since nobody is adding new events to the kfifo.
510 */
511 while (kfifo_out(&sony_laptop_input.fifo,
512 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
513 input_report_key(kp.dev, kp.key, 0);
514 input_sync(kp.dev);
515 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200516
517 /* destroy input devs */
518 input_unregister_device(sony_laptop_input.key_dev);
519 sony_laptop_input.key_dev = NULL;
520
521 if (sony_laptop_input.jog_dev) {
522 input_unregister_device(sony_laptop_input.jog_dev);
523 sony_laptop_input.jog_dev = NULL;
524 }
525
Stefani Seibold45465482009-12-21 14:37:26 -0800526 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200527}
528
malattia@linux.it56b87562007-04-09 10:19:05 +0200529/*********** Platform Device ***********/
530
531static atomic_t sony_pf_users = ATOMIC_INIT(0);
532static struct platform_driver sony_pf_driver = {
533 .driver = {
534 .name = "sony-laptop",
535 .owner = THIS_MODULE,
536 }
537};
538static struct platform_device *sony_pf_device;
539
540static int sony_pf_add(void)
541{
542 int ret = 0;
543
544 /* don't run again if already initialized */
545 if (atomic_add_return(1, &sony_pf_users) > 1)
546 return 0;
547
548 ret = platform_driver_register(&sony_pf_driver);
549 if (ret)
550 goto out;
551
552 sony_pf_device = platform_device_alloc("sony-laptop", -1);
553 if (!sony_pf_device) {
554 ret = -ENOMEM;
555 goto out_platform_registered;
556 }
557
558 ret = platform_device_add(sony_pf_device);
559 if (ret)
560 goto out_platform_alloced;
561
562 return 0;
563
564 out_platform_alloced:
565 platform_device_put(sony_pf_device);
566 sony_pf_device = NULL;
567 out_platform_registered:
568 platform_driver_unregister(&sony_pf_driver);
569 out:
570 atomic_dec(&sony_pf_users);
571 return ret;
572}
573
574static void sony_pf_remove(void)
575{
576 /* deregister only after the last user has gone */
577 if (!atomic_dec_and_test(&sony_pf_users))
578 return;
579
Axel Lin08db2b32010-07-01 10:18:01 +0800580 platform_device_unregister(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +0200581 platform_driver_unregister(&sony_pf_driver);
582}
583
584/*********** SNC (SNY5001) Device ***********/
585
malattia@linux.it33a04452007-04-09 19:26:03 +0200586/* the device uses 1-based values, while the backlight subsystem uses
587 0-based values */
588#define SONY_MAX_BRIGHTNESS 8
589
590#define SNC_VALIDATE_IN 0
591#define SNC_VALIDATE_OUT 1
592
malattia@linux.it59b19102007-04-09 10:19:04 +0200593static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500594 char *);
malattia@linux.it59b19102007-04-09 10:19:04 +0200595static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500596 const char *, size_t);
Mattia Dongili156c2212007-02-12 22:01:07 +0100597static int boolean_validate(const int, const int);
598static int brightness_default_validate(const int, const int);
599
malattia@linux.it59b19102007-04-09 10:19:04 +0200600struct sony_nc_value {
Len Browna02d1c12007-02-07 15:34:02 -0500601 char *name; /* name of the entry */
602 char **acpiget; /* names of the ACPI get function */
603 char **acpiset; /* names of the ACPI set function */
Mattia Dongili156c2212007-02-12 22:01:07 +0100604 int (*validate)(const int, const int); /* input/output validation */
Len Browna02d1c12007-02-07 15:34:02 -0500605 int value; /* current setting */
606 int valid; /* Has ever been set */
607 int debug; /* active only in debug mode ? */
608 struct device_attribute devattr; /* sysfs atribute */
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
748struct sony_nc_handles *handles;
749
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);
771
772 sysfs_attr_init(&handles->devattr.attr);
773 handles->devattr.attr.name = "handles";
774 handles->devattr.attr.mode = S_IRUGO;
775 handles->devattr.show = sony_nc_handles_show;
776
777 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
778 if (!acpi_callsetfunc(sony_nc_acpi_handle,
779 "SN00", i + 0x20, &result)) {
780 dprintk("caching handle 0x%.4x (offset: 0x%.2x)\n",
781 result, i);
782 handles->cap[i] = result;
Mattia Dongili56e6e712011-02-19 11:52:25 +0900783 }
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900784 }
785
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900786 /* allow reading capabilities via sysfs */
787 if (device_create_file(&pd->dev, &handles->devattr)) {
788 kfree(handles);
789 handles = NULL;
790 return -1;
791 }
792
793 return 0;
794}
795
796static int sony_nc_handles_cleanup(struct platform_device *pd)
797{
798 if (handles) {
799 device_remove_file(&pd->dev, &handles->devattr);
800 kfree(handles);
801 handles = NULL;
802 }
803 return 0;
804}
805
806static int sony_find_snc_handle(int handle)
807{
808 int i;
809 for (i = 0; i < 0x10; i++) {
810 if (handles->cap[i] == handle) {
811 dprintk("found handle 0x%.4x (offset: 0x%.2x)\n",
812 handle, i);
813 return i;
814 }
815 }
Mattia Dongili56e6e712011-02-19 11:52:25 +0900816 dprintk("handle 0x%.4x not found\n", handle);
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900817 return -1;
818}
819
820static int sony_call_snc_handle(int handle, int argument, int *result)
821{
Mattia Dongili56e6e712011-02-19 11:52:25 +0900822 int ret = 0;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900823 int offset = sony_find_snc_handle(handle);
824
825 if (offset < 0)
826 return -1;
827
Mattia Dongili56e6e712011-02-19 11:52:25 +0900828 ret = acpi_callsetfunc(sony_nc_acpi_handle, "SN07", offset | argument,
829 result);
830 dprintk("called SN07 with 0x%.4x (result: 0x%.4x)\n", offset | argument,
831 *result);
832 return ret;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900833}
834
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100835/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200836 * sony_nc_values input/output validate functions
Mattia Dongili156c2212007-02-12 22:01:07 +0100837 */
838
839/* brightness_default_validate:
840 *
841 * manipulate input output values to keep consistency with the
842 * backlight framework for which brightness values are 0-based.
843 */
844static int brightness_default_validate(const int direction, const int value)
845{
846 switch (direction) {
847 case SNC_VALIDATE_OUT:
848 return value - 1;
849 case SNC_VALIDATE_IN:
850 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
851 return value + 1;
852 }
853 return -EINVAL;
854}
855
856/* boolean_validate:
857 *
858 * on input validate boolean values 0/1, on output just pass the
859 * received value.
860 */
861static int boolean_validate(const int direction, const int value)
862{
863 if (direction == SNC_VALIDATE_IN) {
864 if (value != 0 && value != 1)
865 return -EINVAL;
866 }
867 return value;
868}
869
870/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200871 * Sysfs show/store common to all sony_nc_values
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100872 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200873static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
Len Browna02d1c12007-02-07 15:34:02 -0500874 char *buffer)
Stelian Pop7f09c432007-01-13 23:04:31 +0100875{
Stelian Pop7f09c432007-01-13 23:04:31 +0100876 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200877 struct sony_nc_value *item =
878 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100879
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100880 if (!*item->acpiget)
Stelian Pop7f09c432007-01-13 23:04:31 +0100881 return -EIO;
882
malattia@linux.it59b19102007-04-09 10:19:04 +0200883 if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100884 return -EIO;
885
Mattia Dongili156c2212007-02-12 22:01:07 +0100886 if (item->validate)
887 value = item->validate(SNC_VALIDATE_OUT, value);
888
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100889 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
Stelian Pop7f09c432007-01-13 23:04:31 +0100890}
891
malattia@linux.it59b19102007-04-09 10:19:04 +0200892static ssize_t sony_nc_sysfs_store(struct device *dev,
Len Browna02d1c12007-02-07 15:34:02 -0500893 struct device_attribute *attr,
894 const char *buffer, size_t count)
Stelian Pop7f09c432007-01-13 23:04:31 +0100895{
Stelian Pop7f09c432007-01-13 23:04:31 +0100896 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200897 struct sony_nc_value *item =
898 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100899
900 if (!item->acpiset)
901 return -EIO;
902
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100903 if (count > 31)
904 return -EINVAL;
905
906 value = simple_strtoul(buffer, NULL, 10);
Stelian Pop7f09c432007-01-13 23:04:31 +0100907
Mattia Dongili156c2212007-02-12 22:01:07 +0100908 if (item->validate)
909 value = item->validate(SNC_VALIDATE_IN, value);
910
911 if (value < 0)
912 return value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100913
malattia@linux.it59b19102007-04-09 10:19:04 +0200914 if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100915 return -EIO;
Andrew Morton3f4f4612007-01-13 23:04:32 +0100916 item->value = value;
917 item->valid = 1;
Stelian Pop7f09c432007-01-13 23:04:31 +0100918 return count;
919}
920
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100921
Mattia Dongilid78865c2007-02-07 20:01:56 +0100922/*
923 * Backlight device
924 */
925static int sony_backlight_update_status(struct backlight_device *bd)
Andrew Morton3f4f4612007-01-13 23:04:32 +0100926{
malattia@linux.it59b19102007-04-09 10:19:04 +0200927 return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
Richard Purdie321709c2007-02-10 15:04:08 +0000928 bd->props.brightness + 1, NULL);
Andrew Morton3f4f4612007-01-13 23:04:32 +0100929}
930
Mattia Dongilid78865c2007-02-07 20:01:56 +0100931static int sony_backlight_get_brightness(struct backlight_device *bd)
932{
933 int value;
934
malattia@linux.it59b19102007-04-09 10:19:04 +0200935 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
Mattia Dongilid78865c2007-02-07 20:01:56 +0100936 return 0;
937 /* brightness levels are 1-based, while backlight ones are 0-based */
938 return value - 1;
939}
940
941static struct backlight_device *sony_backlight_device;
Lionel Debrouxacc24722010-11-16 14:14:02 +0100942static const struct backlight_ops sony_backlight_ops = {
Len Browna02d1c12007-02-07 15:34:02 -0500943 .update_status = sony_backlight_update_status,
944 .get_brightness = sony_backlight_get_brightness,
Mattia Dongilid78865c2007-02-07 20:01:56 +0100945};
946
947/*
Mattia Dongili6315fd12007-07-16 02:34:35 +0900948 * New SNC-only Vaios event mapping to driver known keys
949 */
950struct sony_nc_event {
951 u8 data;
952 u8 event;
953};
954
Matthew Garrett45c79422009-03-26 21:58:16 +0900955static struct sony_nc_event sony_100_events[] = {
Matthew Garrett9b578962009-03-26 21:58:14 +0900956 { 0x90, SONYPI_EVENT_PKEY_P1 },
957 { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthias Welwarsky6479efb2009-04-01 22:10:45 +0900958 { 0x91, SONYPI_EVENT_PKEY_P2 },
Matthew Garrett9b578962009-03-26 21:58:14 +0900959 { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +0900960 { 0x81, SONYPI_EVENT_FNKEY_F1 },
961 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
Anton Veretenenkof5acf5e2009-03-26 22:44:26 +0900962 { 0x82, SONYPI_EVENT_FNKEY_F2 },
963 { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
964 { 0x83, SONYPI_EVENT_FNKEY_F3 },
965 { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
966 { 0x84, SONYPI_EVENT_FNKEY_F4 },
967 { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +0900968 { 0x85, SONYPI_EVENT_FNKEY_F5 },
969 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
970 { 0x86, SONYPI_EVENT_FNKEY_F6 },
971 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
972 { 0x87, SONYPI_EVENT_FNKEY_F7 },
973 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +0900974 { 0x89, SONYPI_EVENT_FNKEY_F9 },
975 { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +0900976 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
977 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
978 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
979 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900980 { 0x9d, SONYPI_EVENT_ZOOM_PRESSED },
981 { 0x1d, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +0900982 { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
983 { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900984 { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
985 { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900986 { 0xa4, SONYPI_EVENT_CD_EJECT_PRESSED },
987 { 0x24, SONYPI_EVENT_ANYBUTTON_RELEASED },
988 { 0xa5, SONYPI_EVENT_VENDOR_PRESSED },
989 { 0x25, SONYPI_EVENT_ANYBUTTON_RELEASED },
990 { 0xa6, SONYPI_EVENT_HELP_PRESSED },
991 { 0x26, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +0900992 { 0, 0 },
993};
994
Matthew Garrett45c79422009-03-26 21:58:16 +0900995static struct sony_nc_event sony_127_events[] = {
996 { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
997 { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
998 { 0x82, SONYPI_EVENT_PKEY_P1 },
999 { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
1000 { 0x83, SONYPI_EVENT_PKEY_P2 },
1001 { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
1002 { 0x84, SONYPI_EVENT_PKEY_P3 },
1003 { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
1004 { 0x85, SONYPI_EVENT_PKEY_P4 },
1005 { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
1006 { 0x86, SONYPI_EVENT_PKEY_P5 },
1007 { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett45c79422009-03-26 21:58:16 +09001008 { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
1009 { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
1010 { 0, 0 },
1011};
1012
Mattia Dongili6315fd12007-07-16 02:34:35 +09001013/*
Mattia Dongilid78865c2007-02-07 20:01:56 +01001014 * ACPI callbacks
1015 */
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001016static void sony_nc_notify(struct acpi_device *device, u32 event)
Stelian Pop7f09c432007-01-13 23:04:31 +01001017{
Mattia Dongili6315fd12007-07-16 02:34:35 +09001018 u32 ev = event;
Mattia Dongili6315fd12007-07-16 02:34:35 +09001019
Matthew Garrett9b578962009-03-26 21:58:14 +09001020 if (ev >= 0x90) {
1021 /* New-style event */
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001022 int result;
Matthew Garrett45c79422009-03-26 21:58:16 +09001023 int key_handle = 0;
Matthew Garrett9b578962009-03-26 21:58:14 +09001024 ev -= 0x90;
Mattia Dongili6315fd12007-07-16 02:34:35 +09001025
Matthew Garrett45c79422009-03-26 21:58:16 +09001026 if (sony_find_snc_handle(0x100) == ev)
1027 key_handle = 0x100;
1028 if (sony_find_snc_handle(0x127) == ev)
1029 key_handle = 0x127;
Matthew Garrett9b578962009-03-26 21:58:14 +09001030
Matthias Welwarsky6479efb2009-04-01 22:10:45 +09001031 if (key_handle) {
Matthew Garrett45c79422009-03-26 21:58:16 +09001032 struct sony_nc_event *key_event;
1033
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001034 if (sony_call_snc_handle(key_handle, 0x200, &result)) {
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001035 dprintk("sony_nc_notify, unable to decode"
Matthew Garrett45c79422009-03-26 21:58:16 +09001036 " event 0x%.2x 0x%.2x\n", key_handle,
1037 ev);
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001038 /* restore the original event */
1039 ev = event;
1040 } else {
Matthew Garrett9b578962009-03-26 21:58:14 +09001041 ev = result & 0xFF;
1042
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001043 if (key_handle == 0x100)
1044 key_event = sony_100_events;
1045 else
1046 key_event = sony_127_events;
Matthew Garrett45c79422009-03-26 21:58:16 +09001047
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001048 for (; key_event->data; key_event++) {
1049 if (key_event->data == ev) {
1050 ev = key_event->event;
1051 break;
1052 }
Matthew Garrett9b578962009-03-26 21:58:14 +09001053 }
Mattia Dongili6315fd12007-07-16 02:34:35 +09001054
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001055 if (!key_event->data)
Mattia Dongilid6697932011-02-19 11:52:28 +09001056 pr_info(DRV_PFX
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001057 "Unknown event: 0x%x 0x%x\n",
1058 key_handle,
1059 ev);
1060 else
1061 sony_laptop_report_input_event(ev);
Matthew Garrett45c79422009-03-26 21:58:16 +09001062 }
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001063 } else if (sony_find_snc_handle(sony_rfkill_handle) == ev) {
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001064 sony_nc_rfkill_update();
1065 return;
Matthew Garrett9b578962009-03-26 21:58:14 +09001066 }
Matthias Welwarsky16dd55f2009-04-01 22:10:47 +09001067 } else
1068 sony_laptop_report_input_event(ev);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001069
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001070 dprintk("sony_nc_notify, event: 0x%.2x\n", ev);
Len Brown14e04fb2007-08-23 15:20:26 -04001071 acpi_bus_generate_proc_event(sony_nc_acpi_device, 1, ev);
Stelian Pop7f09c432007-01-13 23:04:31 +01001072}
1073
1074static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
1075 void *context, void **return_value)
1076{
Lin Ming30823732008-12-16 16:59:35 +08001077 struct acpi_device_info *info;
Stelian Pop7f09c432007-01-13 23:04:31 +01001078
Bob Moore15b8dd52009-06-29 13:39:29 +08001079 if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001080 pr_warn(DRV_PFX "method: name: %4.4s, args %X\n",
Lin Ming30823732008-12-16 16:59:35 +08001081 (char *)&info->name, info->param_count);
1082
Bob Moore15b8dd52009-06-29 13:39:29 +08001083 kfree(info);
Lin Ming30823732008-12-16 16:59:35 +08001084 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001085
1086 return AE_OK;
1087}
1088
Mattia Dongilid78865c2007-02-07 20:01:56 +01001089/*
1090 * ACPI device
1091 */
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001092static int sony_nc_function_setup(struct acpi_device *device)
1093{
1094 int result;
1095
1096 /* Enable all events */
1097 acpi_callsetfunc(sony_nc_acpi_handle, "SN02", 0xffff, &result);
1098
1099 /* Setup hotkeys */
1100 sony_call_snc_handle(0x0100, 0, &result);
1101 sony_call_snc_handle(0x0101, 0, &result);
1102 sony_call_snc_handle(0x0102, 0x100, &result);
Almer S. Tigelaar560e84a2009-04-12 11:26:27 +00001103 sony_call_snc_handle(0x0127, 0, &result);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001104
1105 return 0;
1106}
1107
malattia@linux.it59b19102007-04-09 10:19:04 +02001108static int sony_nc_resume(struct acpi_device *device)
Mattia Dongilid78865c2007-02-07 20:01:56 +01001109{
malattia@linux.it59b19102007-04-09 10:19:04 +02001110 struct sony_nc_value *item;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001111 acpi_handle handle;
Mattia Dongilid78865c2007-02-07 20:01:56 +01001112
malattia@linux.it59b19102007-04-09 10:19:04 +02001113 for (item = sony_nc_values; item->name; item++) {
Mattia Dongilid78865c2007-02-07 20:01:56 +01001114 int ret;
1115
1116 if (!item->valid)
1117 continue;
malattia@linux.it59b19102007-04-09 10:19:04 +02001118 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
Len Browna02d1c12007-02-07 15:34:02 -05001119 item->value, NULL);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001120 if (ret < 0) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001121 pr_err(DRV_PFX "%s: %d\n", __func__, ret);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001122 break;
1123 }
1124 }
Mattia Dongili6315fd12007-07-16 02:34:35 +09001125
Matthew Garrett82734bf2009-03-26 21:58:13 +09001126 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1127 &handle))) {
1128 if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
1129 dprintk("ECON Method failed\n");
1130 }
1131
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001132 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1133 &handle))) {
1134 dprintk("Doing SNC setup\n");
1135 sony_nc_function_setup(device);
1136 }
1137
Mattia Dongilie84a02b2007-08-04 00:22:30 +09001138 /* set the last requested brightness level */
1139 if (sony_backlight_device &&
Mattia Dongilic35d4b32009-04-12 11:26:30 +00001140 sony_backlight_update_status(sony_backlight_device) < 0)
Mattia Dongilid6697932011-02-19 11:52:28 +09001141 pr_warn(DRV_PFX "unable to restore brightness level\n");
Mattia Dongilie84a02b2007-08-04 00:22:30 +09001142
Alan Jenkinsa0d97d62009-09-25 10:18:21 +01001143 /* re-read rfkill state */
1144 sony_nc_rfkill_update();
1145
Mattia Dongilid78865c2007-02-07 20:01:56 +01001146 return 0;
1147}
1148
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001149static void sony_nc_rfkill_cleanup(void)
1150{
1151 int i;
1152
Johannes Berg19d337d2009-06-02 13:01:37 +02001153 for (i = 0; i < N_SONY_RFKILL; i++) {
1154 if (sony_rfkill_devices[i]) {
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001155 rfkill_unregister(sony_rfkill_devices[i]);
Johannes Berg19d337d2009-06-02 13:01:37 +02001156 rfkill_destroy(sony_rfkill_devices[i]);
1157 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001158 }
1159}
1160
Johannes Berg19d337d2009-06-02 13:01:37 +02001161static int sony_nc_rfkill_set(void *data, bool blocked)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001162{
1163 int result;
1164 int argument = sony_rfkill_address[(long) data] + 0x100;
1165
Johannes Berg19d337d2009-06-02 13:01:37 +02001166 if (!blocked)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001167 argument |= 0xff0000;
1168
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001169 return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001170}
1171
Johannes Berg19d337d2009-06-02 13:01:37 +02001172static const struct rfkill_ops sony_rfkill_ops = {
1173 .set_block = sony_nc_rfkill_set,
1174};
1175
1176static int sony_nc_setup_rfkill(struct acpi_device *device,
1177 enum sony_nc_rfkill nc_type)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001178{
1179 int err = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001180 struct rfkill *rfk;
1181 enum rfkill_type type;
1182 const char *name;
Alan Jenkins50fab072009-09-24 20:15:24 +01001183 int result;
1184 bool hwblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001185
Johannes Berg19d337d2009-06-02 13:01:37 +02001186 switch (nc_type) {
1187 case SONY_WIFI:
1188 type = RFKILL_TYPE_WLAN;
1189 name = "sony-wifi";
1190 break;
1191 case SONY_BLUETOOTH:
1192 type = RFKILL_TYPE_BLUETOOTH;
1193 name = "sony-bluetooth";
1194 break;
1195 case SONY_WWAN:
1196 type = RFKILL_TYPE_WWAN;
1197 name = "sony-wwan";
1198 break;
1199 case SONY_WIMAX:
1200 type = RFKILL_TYPE_WIMAX;
1201 name = "sony-wimax";
1202 break;
1203 default:
1204 return -EINVAL;
Mattia Dongili53005a02009-04-12 11:26:31 +00001205 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001206
Johannes Berg19d337d2009-06-02 13:01:37 +02001207 rfk = rfkill_alloc(name, &device->dev, type,
1208 &sony_rfkill_ops, (void *)nc_type);
1209 if (!rfk)
1210 return -ENOMEM;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001211
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001212 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
Alan Jenkins50fab072009-09-24 20:15:24 +01001213 hwblock = !(result & 0x1);
1214 rfkill_set_hw_state(rfk, hwblock);
1215
Johannes Berg19d337d2009-06-02 13:01:37 +02001216 err = rfkill_register(rfk);
1217 if (err) {
1218 rfkill_destroy(rfk);
1219 return err;
Mattia Dongili53005a02009-04-12 11:26:31 +00001220 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001221 sony_rfkill_devices[nc_type] = rfk;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001222 return err;
1223}
1224
Randy Dunlapa46a7802011-01-08 19:56:44 -08001225static void sony_nc_rfkill_update(void)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001226{
Johannes Berg19d337d2009-06-02 13:01:37 +02001227 enum sony_nc_rfkill i;
1228 int result;
1229 bool hwblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001230
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001231 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001232 hwblock = !(result & 0x1);
1233
1234 for (i = 0; i < N_SONY_RFKILL; i++) {
1235 int argument = sony_rfkill_address[i];
1236
1237 if (!sony_rfkill_devices[i])
1238 continue;
1239
1240 if (hwblock) {
Johannes Berge1f8a192009-06-11 12:08:15 +02001241 if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
1242 /* we already know we're blocked */
1243 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001244 continue;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001245 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001246
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001247 sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001248 rfkill_set_states(sony_rfkill_devices[i],
1249 !(result & 0xf), false);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001250 }
1251}
1252
Mattia Dongili528809c2009-12-17 00:08:36 +09001253static void sony_nc_rfkill_setup(struct acpi_device *device)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001254{
Mattia Dongili528809c2009-12-17 00:08:36 +09001255 int offset;
1256 u8 dev_code, i;
1257 acpi_status status;
1258 struct acpi_object_list params;
1259 union acpi_object in_obj;
1260 union acpi_object *device_enum;
1261 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001262
Mattia Dongili528809c2009-12-17 00:08:36 +09001263 offset = sony_find_snc_handle(0x124);
1264 if (offset == -1) {
1265 offset = sony_find_snc_handle(0x135);
1266 if (offset == -1)
1267 return;
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001268 else
1269 sony_rfkill_handle = 0x135;
1270 } else
1271 sony_rfkill_handle = 0x124;
Mattia Dongili528809c2009-12-17 00:08:36 +09001272 dprintk("Found rkfill handle: 0x%.4x\n", sony_rfkill_handle);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001273
Mattia Dongili528809c2009-12-17 00:08:36 +09001274 /* need to read the whole buffer returned by the acpi call to SN06
1275 * here otherwise we may miss some features
1276 */
1277 params.count = 1;
1278 params.pointer = &in_obj;
1279 in_obj.type = ACPI_TYPE_INTEGER;
1280 in_obj.integer.value = offset;
1281 status = acpi_evaluate_object(sony_nc_acpi_handle, "SN06", &params,
1282 &buffer);
1283 if (ACPI_FAILURE(status)) {
1284 dprintk("Radio device enumeration failed\n");
1285 return;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001286 }
1287
Mattia Dongili528809c2009-12-17 00:08:36 +09001288 device_enum = (union acpi_object *) buffer.pointer;
Mattia Dongilid6697932011-02-19 11:52:28 +09001289 if (!device_enum || device_enum->type != ACPI_TYPE_BUFFER) {
1290 pr_err(DRV_PFX "Invalid SN06 return object 0x%.2x\n",
1291 device_enum->type);
Mattia Dongili528809c2009-12-17 00:08:36 +09001292 goto out_no_enum;
1293 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001294
Mattia Dongili528809c2009-12-17 00:08:36 +09001295 /* the buffer is filled with magic numbers describing the devices
1296 * available, 0xff terminates the enumeration
1297 */
Dmitry Torokhovc14973f2010-01-10 00:15:44 -08001298 for (i = 0; i < device_enum->buffer.length; i++) {
1299
1300 dev_code = *(device_enum->buffer.pointer + i);
1301 if (dev_code == 0xff)
1302 break;
1303
Mattia Dongili528809c2009-12-17 00:08:36 +09001304 dprintk("Radio devices, looking at 0x%.2x\n", dev_code);
1305
1306 if (dev_code == 0 && !sony_rfkill_devices[SONY_WIFI])
1307 sony_nc_setup_rfkill(device, SONY_WIFI);
1308
1309 if (dev_code == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
1310 sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
1311
1312 if ((0xf0 & dev_code) == 0x20 &&
1313 !sony_rfkill_devices[SONY_WWAN])
1314 sony_nc_setup_rfkill(device, SONY_WWAN);
1315
1316 if (dev_code == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
1317 sony_nc_setup_rfkill(device, SONY_WIMAX);
1318 }
1319
1320out_no_enum:
1321 kfree(buffer.pointer);
1322 return;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001323}
1324
Mattia Dongilibf155712011-02-19 11:52:31 +09001325/* Keyboard backlight feature */
1326#define KBDBL_HANDLER 0x137
1327#define KBDBL_PRESENT 0xB00
1328#define SET_MODE 0xC00
1329#define SET_TIMEOUT 0xE00
1330
1331struct kbd_backlight {
1332 int mode;
1333 int timeout;
1334 struct device_attribute mode_attr;
1335 struct device_attribute timeout_attr;
1336};
1337
1338struct kbd_backlight *kbdbl_handle;
1339
1340static ssize_t __sony_nc_kbd_backlight_mode_set(u8 value)
1341{
1342 int result;
1343
1344 if (value > 1)
1345 return -EINVAL;
1346
1347 if (sony_call_snc_handle(KBDBL_HANDLER,
1348 (value << 0x10) | SET_MODE, &result))
1349 return -EIO;
1350
1351 kbdbl_handle->mode = value;
1352
1353 return 0;
1354}
1355
1356static ssize_t sony_nc_kbd_backlight_mode_store(struct device *dev,
1357 struct device_attribute *attr,
1358 const char *buffer, size_t count)
1359{
1360 int ret = 0;
1361 unsigned long value;
1362
1363 if (count > 31)
1364 return -EINVAL;
1365
1366 if (strict_strtoul(buffer, 10, &value))
1367 return -EINVAL;
1368
1369 ret = __sony_nc_kbd_backlight_mode_set(value);
1370 if (ret < 0)
1371 return ret;
1372
1373 return count;
1374}
1375
1376static ssize_t sony_nc_kbd_backlight_mode_show(struct device *dev,
1377 struct device_attribute *attr, char *buffer)
1378{
1379 ssize_t count = 0;
1380 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_handle->mode);
1381 return count;
1382}
1383
1384static int __sony_nc_kbd_backlight_timeout_set(u8 value)
1385{
1386 int result;
1387
1388 if (value > 3)
1389 return -EINVAL;
1390
1391 if (sony_call_snc_handle(KBDBL_HANDLER,
1392 (value << 0x10) | SET_TIMEOUT, &result))
1393 return -EIO;
1394
1395 kbdbl_handle->timeout = value;
1396
1397 return 0;
1398}
1399
1400static ssize_t sony_nc_kbd_backlight_timeout_store(struct device *dev,
1401 struct device_attribute *attr,
1402 const char *buffer, size_t count)
1403{
1404 int ret = 0;
1405 unsigned long value;
1406
1407 if (count > 31)
1408 return -EINVAL;
1409
1410 if (strict_strtoul(buffer, 10, &value))
1411 return -EINVAL;
1412
1413 ret = __sony_nc_kbd_backlight_timeout_set(value);
1414 if (ret < 0)
1415 return ret;
1416
1417 return count;
1418}
1419
1420static ssize_t sony_nc_kbd_backlight_timeout_show(struct device *dev,
1421 struct device_attribute *attr, char *buffer)
1422{
1423 ssize_t count = 0;
1424 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_handle->timeout);
1425 return count;
1426}
1427
1428static int sony_nc_kbd_backlight_setup(struct platform_device *pd)
1429{
1430 int result;
1431
1432 if (sony_call_snc_handle(0x137, KBDBL_PRESENT, &result))
1433 return 0;
1434 if (!(result & 0x02))
1435 return 0;
1436
1437 kbdbl_handle = kzalloc(sizeof(*kbdbl_handle), GFP_KERNEL);
1438
1439 sysfs_attr_init(&kbdbl_handle->mode_attr.attr);
1440 kbdbl_handle->mode_attr.attr.name = "kbd_backlight";
1441 kbdbl_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
1442 kbdbl_handle->mode_attr.show = sony_nc_kbd_backlight_mode_show;
1443 kbdbl_handle->mode_attr.store = sony_nc_kbd_backlight_mode_store;
1444
1445 sysfs_attr_init(&kbdbl_handle->timeout_attr.attr);
1446 kbdbl_handle->timeout_attr.attr.name = "kbd_backlight_timeout";
1447 kbdbl_handle->timeout_attr.attr.mode = S_IRUGO | S_IWUSR;
1448 kbdbl_handle->timeout_attr.show = sony_nc_kbd_backlight_timeout_show;
1449 kbdbl_handle->timeout_attr.store = sony_nc_kbd_backlight_timeout_store;
1450
1451 if (device_create_file(&pd->dev, &kbdbl_handle->mode_attr))
1452 goto outkzalloc;
1453
1454 if (device_create_file(&pd->dev, &kbdbl_handle->timeout_attr))
1455 goto outmode;
1456
1457 __sony_nc_kbd_backlight_mode_set(kbd_backlight);
1458 __sony_nc_kbd_backlight_timeout_set(kbd_backlight_timeout);
1459
1460 return 0;
1461
1462outmode:
1463 device_remove_file(&pd->dev, &kbdbl_handle->mode_attr);
1464outkzalloc:
1465 kfree(kbdbl_handle);
1466 kbdbl_handle = NULL;
1467 return -1;
1468}
1469
1470static int sony_nc_kbd_backlight_cleanup(struct platform_device *pd)
1471{
1472 if (kbdbl_handle) {
1473 device_remove_file(&pd->dev, &kbdbl_handle->mode_attr);
1474 device_remove_file(&pd->dev, &kbdbl_handle->timeout_attr);
1475 kfree(kbdbl_handle);
1476 }
1477 return 0;
1478}
1479
malattia@linux.it59b19102007-04-09 10:19:04 +02001480static int sony_nc_add(struct acpi_device *device)
Stelian Pop7f09c432007-01-13 23:04:31 +01001481{
1482 acpi_status status;
Andrew Morton8607c672007-03-06 02:29:42 -08001483 int result = 0;
Alessandro Guido50f62af2007-01-13 23:04:34 +01001484 acpi_handle handle;
malattia@linux.it56b87562007-04-09 10:19:05 +02001485 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01001486
Mattia Dongilid6697932011-02-19 11:52:28 +09001487 pr_info(DRV_PFX "%s v%s.\n", SONY_NC_DRIVER_NAME,
1488 SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.itf6119b02007-04-09 19:31:06 +02001489
malattia@linux.it59b19102007-04-09 10:19:04 +02001490 sony_nc_acpi_device = device;
malattia@linux.it33a04452007-04-09 19:26:03 +02001491 strcpy(acpi_device_class(device), "sony/hotkey");
Stelian Popc5611622007-01-13 23:04:37 +01001492
malattia@linux.it59b19102007-04-09 10:19:04 +02001493 sony_nc_acpi_handle = device->handle;
Stelian Pop7f09c432007-01-13 23:04:31 +01001494
Mattia Dongilib25b7322007-07-16 02:34:36 +09001495 /* read device status */
1496 result = acpi_bus_get_status(device);
1497 /* bail IFF the above call was successful and the device is not present */
1498 if (!result && !device->status.present) {
1499 dprintk("Device not present\n");
1500 result = -ENODEV;
1501 goto outwalk;
1502 }
1503
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001504 result = sony_pf_add();
1505 if (result)
1506 goto outpresent;
1507
Stelian Pop7f09c432007-01-13 23:04:31 +01001508 if (debug) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001509 status = acpi_walk_namespace(ACPI_TYPE_METHOD,
1510 sony_nc_acpi_handle, 1, sony_walk_callback,
1511 NULL, NULL, NULL);
Stelian Pop7f09c432007-01-13 23:04:31 +01001512 if (ACPI_FAILURE(status)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001513 pr_warn(DRV_PFX "unable to walk acpi resources\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001514 result = -ENODEV;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001515 goto outpresent;
Stelian Pop7f09c432007-01-13 23:04:31 +01001516 }
Stelian Popc5611622007-01-13 23:04:37 +01001517 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001518
Matthew Garrett82734bf2009-03-26 21:58:13 +09001519 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1520 &handle))) {
1521 if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
1522 dprintk("ECON Method failed\n");
1523 }
1524
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001525 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1526 &handle))) {
1527 dprintk("Doing SNC setup\n");
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001528 if (sony_nc_handles_setup(sony_pf_device))
1529 goto outpresent;
Mattia Dongilibf155712011-02-19 11:52:31 +09001530 if (sony_nc_kbd_backlight_setup(sony_pf_device))
1531 goto outsnc;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001532 sony_nc_function_setup(device);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001533 sony_nc_rfkill_setup(device);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001534 }
1535
malattia@linux.it1549ee62007-04-09 10:19:08 +02001536 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05001537 result = sony_laptop_setup_input(device);
malattia@linux.it1549ee62007-04-09 10:19:08 +02001538 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001539 pr_err(DRV_PFX "Unable to create input devices.\n");
Mattia Dongilibf155712011-02-19 11:52:31 +09001540 goto outkbdbacklight;
malattia@linux.it1549ee62007-04-09 10:19:08 +02001541 }
1542
Alessandro Guido38cfc142008-11-12 23:03:28 +01001543 if (acpi_video_backlight_support()) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001544 pr_info(DRV_PFX "brightness ignored, must be "
Thomas Renninger540b8bb2008-08-01 17:38:02 +02001545 "controlled by ACPI video driver\n");
1546 } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
1547 &handle))) {
Matthew Garrettbb7ca742011-03-22 16:30:21 -07001548 struct backlight_properties props;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05001549 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -07001550 props.type = BACKLIGHT_PLATFORM;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05001551 props.max_brightness = SONY_MAX_BRIGHTNESS - 1;
Alessandro Guido50f62af2007-01-13 23:04:34 +01001552 sony_backlight_device = backlight_device_register("sony", NULL,
Len Browna02d1c12007-02-07 15:34:02 -05001553 NULL,
Matthew Garretta19a6ee2010-02-17 16:39:44 -05001554 &sony_backlight_ops,
1555 &props);
Mattia Dongili7df03b82007-01-13 23:04:41 +01001556
Len Browna02d1c12007-02-07 15:34:02 -05001557 if (IS_ERR(sony_backlight_device)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09001558 pr_warning(DRV_PFX "unable to register backlight device\n");
Mattia Dongili7df03b82007-01-13 23:04:41 +01001559 sony_backlight_device = NULL;
Richard Purdie321709c2007-02-10 15:04:08 +00001560 } else {
1561 sony_backlight_device->props.brightness =
Len Browna02d1c12007-02-07 15:34:02 -05001562 sony_backlight_get_brightness
1563 (sony_backlight_device);
Richard Purdie321709c2007-02-10 15:04:08 +00001564 }
1565
Alessandro Guido50f62af2007-01-13 23:04:34 +01001566 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001567
malattia@linux.it56b87562007-04-09 10:19:05 +02001568 /* create sony_pf sysfs attributes related to the SNC device */
1569 for (item = sony_nc_values; item->name; ++item) {
1570
1571 if (!debug && item->debug)
1572 continue;
1573
1574 /* find the available acpiget as described in the DSDT */
1575 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
1576 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1577 *item->acpiget,
1578 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02001579 dprintk("Found %s getter: %s\n",
1580 item->name, *item->acpiget);
malattia@linux.it56b87562007-04-09 10:19:05 +02001581 item->devattr.attr.mode |= S_IRUGO;
1582 break;
1583 }
1584 }
1585
1586 /* find the available acpiset as described in the DSDT */
1587 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
1588 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1589 *item->acpiset,
1590 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02001591 dprintk("Found %s setter: %s\n",
1592 item->name, *item->acpiset);
malattia@linux.it56b87562007-04-09 10:19:05 +02001593 item->devattr.attr.mode |= S_IWUSR;
1594 break;
1595 }
1596 }
1597
1598 if (item->devattr.attr.mode != 0) {
1599 result =
1600 device_create_file(&sony_pf_device->dev,
1601 &item->devattr);
1602 if (result)
1603 goto out_sysfs;
1604 }
1605 }
1606
Stelian Pop7f09c432007-01-13 23:04:31 +01001607 return 0;
1608
malattia@linux.it56b87562007-04-09 10:19:05 +02001609 out_sysfs:
1610 for (item = sony_nc_values; item->name; ++item) {
1611 device_remove_file(&sony_pf_device->dev, &item->devattr);
1612 }
Mattia Dongili7df03b82007-01-13 23:04:41 +01001613 if (sony_backlight_device)
1614 backlight_device_unregister(sony_backlight_device);
1615
malattia@linux.it1549ee62007-04-09 10:19:08 +02001616 sony_laptop_remove_input();
1617
Mattia Dongilibf155712011-02-19 11:52:31 +09001618 outkbdbacklight:
1619 sony_nc_kbd_backlight_cleanup(sony_pf_device);
1620
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001621 outsnc:
1622 sony_nc_handles_cleanup(sony_pf_device);
1623
1624 outpresent:
1625 sony_pf_remove();
1626
Len Browna02d1c12007-02-07 15:34:02 -05001627 outwalk:
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001628 sony_nc_rfkill_cleanup();
Stelian Pop7f09c432007-01-13 23:04:31 +01001629 return result;
1630}
1631
malattia@linux.it59b19102007-04-09 10:19:04 +02001632static int sony_nc_remove(struct acpi_device *device, int type)
Stelian Pop7f09c432007-01-13 23:04:31 +01001633{
malattia@linux.it56b87562007-04-09 10:19:05 +02001634 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01001635
Alessandro Guido50f62af2007-01-13 23:04:34 +01001636 if (sony_backlight_device)
1637 backlight_device_unregister(sony_backlight_device);
1638
malattia@linux.it59b19102007-04-09 10:19:04 +02001639 sony_nc_acpi_device = NULL;
Stelian Popc5611622007-01-13 23:04:37 +01001640
malattia@linux.it56b87562007-04-09 10:19:05 +02001641 for (item = sony_nc_values; item->name; ++item) {
1642 device_remove_file(&sony_pf_device->dev, &item->devattr);
1643 }
1644
Mattia Dongilibf155712011-02-19 11:52:31 +09001645 sony_nc_kbd_backlight_cleanup(sony_pf_device);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09001646 sony_nc_handles_cleanup(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +02001647 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +02001648 sony_laptop_remove_input();
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001649 sony_nc_rfkill_cleanup();
malattia@linux.itf6119b02007-04-09 19:31:06 +02001650 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001651
1652 return 0;
1653}
1654
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001655static const struct acpi_device_id sony_device_ids[] = {
1656 {SONY_NC_HID, 0},
1657 {SONY_PIC_HID, 0},
1658 {"", 0},
1659};
1660MODULE_DEVICE_TABLE(acpi, sony_device_ids);
1661
1662static const struct acpi_device_id sony_nc_device_ids[] = {
1663 {SONY_NC_HID, 0},
1664 {"", 0},
1665};
1666
malattia@linux.it59b19102007-04-09 10:19:04 +02001667static struct acpi_driver sony_nc_driver = {
1668 .name = SONY_NC_DRIVER_NAME,
1669 .class = SONY_NC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001670 .ids = sony_nc_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02001671 .owner = THIS_MODULE,
Len Browna02d1c12007-02-07 15:34:02 -05001672 .ops = {
malattia@linux.it59b19102007-04-09 10:19:04 +02001673 .add = sony_nc_add,
1674 .remove = sony_nc_remove,
1675 .resume = sony_nc_resume,
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001676 .notify = sony_nc_notify,
Len Browna02d1c12007-02-07 15:34:02 -05001677 },
Andrew Morton3f4f4612007-01-13 23:04:32 +01001678};
1679
malattia@linux.it33a04452007-04-09 19:26:03 +02001680/*********** SPIC (SNY6001) Device ***********/
1681
1682#define SONYPI_DEVICE_TYPE1 0x00000001
1683#define SONYPI_DEVICE_TYPE2 0x00000002
1684#define SONYPI_DEVICE_TYPE3 0x00000004
1685
Mattia Dongili22a17782007-07-16 02:34:39 +09001686#define SONYPI_TYPE1_OFFSET 0x04
1687#define SONYPI_TYPE2_OFFSET 0x12
1688#define SONYPI_TYPE3_OFFSET 0x12
malattia@linux.it33a04452007-04-09 19:26:03 +02001689
malattia@linux.it33a04452007-04-09 19:26:03 +02001690struct sony_pic_ioport {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001691 struct acpi_resource_io io1;
1692 struct acpi_resource_io io2;
malattia@linux.it33a04452007-04-09 19:26:03 +02001693 struct list_head list;
1694};
1695
1696struct sony_pic_irq {
1697 struct acpi_resource_irq irq;
1698 struct list_head list;
1699};
1700
Mattia Dongilide920432008-01-14 18:05:43 +09001701struct sonypi_eventtypes {
1702 u8 data;
1703 unsigned long mask;
1704 struct sonypi_event *events;
1705};
1706
malattia@linux.it33a04452007-04-09 19:26:03 +02001707struct sony_pic_dev {
Mattia Dongili31df7142009-09-16 00:05:29 +09001708 struct acpi_device *acpi_dev;
1709 struct sony_pic_irq *cur_irq;
1710 struct sony_pic_ioport *cur_ioport;
1711 struct list_head interrupts;
1712 struct list_head ioports;
1713 struct mutex lock;
1714 struct sonypi_eventtypes *event_types;
1715 int (*handle_irq)(const u8, const u8);
1716 int model;
1717 u16 evport_offset;
1718 u8 camera_power;
1719 u8 bluetooth_power;
1720 u8 wwan_power;
malattia@linux.it33a04452007-04-09 19:26:03 +02001721};
1722
1723static struct sony_pic_dev spic_dev = {
1724 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
1725 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
1726};
1727
Alan Jenkins5e6f9722009-09-16 00:05:32 +09001728static int spic_drv_registered;
1729
malattia@linux.it33a04452007-04-09 19:26:03 +02001730/* Event masks */
1731#define SONYPI_JOGGER_MASK 0x00000001
1732#define SONYPI_CAPTURE_MASK 0x00000002
1733#define SONYPI_FNKEY_MASK 0x00000004
1734#define SONYPI_BLUETOOTH_MASK 0x00000008
1735#define SONYPI_PKEY_MASK 0x00000010
1736#define SONYPI_BACK_MASK 0x00000020
1737#define SONYPI_HELP_MASK 0x00000040
1738#define SONYPI_LID_MASK 0x00000080
1739#define SONYPI_ZOOM_MASK 0x00000100
1740#define SONYPI_THUMBPHRASE_MASK 0x00000200
1741#define SONYPI_MEYE_MASK 0x00000400
1742#define SONYPI_MEMORYSTICK_MASK 0x00000800
1743#define SONYPI_BATTERY_MASK 0x00001000
1744#define SONYPI_WIRELESS_MASK 0x00002000
1745
1746struct sonypi_event {
1747 u8 data;
1748 u8 event;
1749};
1750
1751/* The set of possible button release events */
1752static struct sonypi_event sonypi_releaseev[] = {
1753 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
1754 { 0, 0 }
1755};
1756
1757/* The set of possible jogger events */
1758static struct sonypi_event sonypi_joggerev[] = {
1759 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
1760 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
1761 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
1762 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
1763 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
1764 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
1765 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
1766 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
1767 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
1768 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
1769 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
1770 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
1771 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
1772 { 0, 0 }
1773};
1774
1775/* The set of possible capture button events */
1776static struct sonypi_event sonypi_captureev[] = {
1777 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
1778 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09001779 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02001780 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
1781 { 0, 0 }
1782};
1783
1784/* The set of possible fnkeys events */
1785static struct sonypi_event sonypi_fnkeyev[] = {
1786 { 0x10, SONYPI_EVENT_FNKEY_ESC },
1787 { 0x11, SONYPI_EVENT_FNKEY_F1 },
1788 { 0x12, SONYPI_EVENT_FNKEY_F2 },
1789 { 0x13, SONYPI_EVENT_FNKEY_F3 },
1790 { 0x14, SONYPI_EVENT_FNKEY_F4 },
1791 { 0x15, SONYPI_EVENT_FNKEY_F5 },
1792 { 0x16, SONYPI_EVENT_FNKEY_F6 },
1793 { 0x17, SONYPI_EVENT_FNKEY_F7 },
1794 { 0x18, SONYPI_EVENT_FNKEY_F8 },
1795 { 0x19, SONYPI_EVENT_FNKEY_F9 },
1796 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
1797 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
1798 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
1799 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
1800 { 0x21, SONYPI_EVENT_FNKEY_1 },
1801 { 0x22, SONYPI_EVENT_FNKEY_2 },
1802 { 0x31, SONYPI_EVENT_FNKEY_D },
1803 { 0x32, SONYPI_EVENT_FNKEY_E },
1804 { 0x33, SONYPI_EVENT_FNKEY_F },
1805 { 0x34, SONYPI_EVENT_FNKEY_S },
1806 { 0x35, SONYPI_EVENT_FNKEY_B },
1807 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
1808 { 0, 0 }
1809};
1810
1811/* The set of possible program key events */
1812static struct sonypi_event sonypi_pkeyev[] = {
1813 { 0x01, SONYPI_EVENT_PKEY_P1 },
1814 { 0x02, SONYPI_EVENT_PKEY_P2 },
1815 { 0x04, SONYPI_EVENT_PKEY_P3 },
Harald Jenny1cae7102009-03-26 21:58:18 +09001816 { 0x20, SONYPI_EVENT_PKEY_P1 },
malattia@linux.it33a04452007-04-09 19:26:03 +02001817 { 0, 0 }
1818};
1819
1820/* The set of possible bluetooth events */
1821static struct sonypi_event sonypi_blueev[] = {
1822 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
1823 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
1824 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
1825 { 0, 0 }
1826};
1827
1828/* The set of possible wireless events */
1829static struct sonypi_event sonypi_wlessev[] = {
Mattia Dongili4eeb5022011-02-19 11:52:27 +09001830 { 0x59, SONYPI_EVENT_IGNORE },
1831 { 0x5a, SONYPI_EVENT_IGNORE },
malattia@linux.it33a04452007-04-09 19:26:03 +02001832 { 0, 0 }
1833};
1834
1835/* The set of possible back button events */
1836static struct sonypi_event sonypi_backev[] = {
1837 { 0x20, SONYPI_EVENT_BACK_PRESSED },
1838 { 0, 0 }
1839};
1840
1841/* The set of possible help button events */
1842static struct sonypi_event sonypi_helpev[] = {
1843 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
1844 { 0, 0 }
1845};
1846
1847
1848/* The set of possible lid events */
1849static struct sonypi_event sonypi_lidev[] = {
1850 { 0x51, SONYPI_EVENT_LID_CLOSED },
1851 { 0x50, SONYPI_EVENT_LID_OPENED },
1852 { 0, 0 }
1853};
1854
1855/* The set of possible zoom events */
1856static struct sonypi_event sonypi_zoomev[] = {
1857 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09001858 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
1859 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
Harald Jenny1cae7102009-03-26 21:58:18 +09001860 { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02001861 { 0, 0 }
1862};
1863
1864/* The set of possible thumbphrase events */
1865static struct sonypi_event sonypi_thumbphraseev[] = {
1866 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
1867 { 0, 0 }
1868};
1869
1870/* The set of possible motioneye camera events */
1871static struct sonypi_event sonypi_meyeev[] = {
1872 { 0x00, SONYPI_EVENT_MEYE_FACE },
1873 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
1874 { 0, 0 }
1875};
1876
1877/* The set of possible memorystick events */
1878static struct sonypi_event sonypi_memorystickev[] = {
1879 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
1880 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
1881 { 0, 0 }
1882};
1883
1884/* The set of possible battery events */
1885static struct sonypi_event sonypi_batteryev[] = {
1886 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
1887 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
1888 { 0, 0 }
1889};
1890
Harald Jenny1cae7102009-03-26 21:58:18 +09001891/* The set of possible volume events */
1892static struct sonypi_event sonypi_volumeev[] = {
1893 { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
1894 { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
1895 { 0, 0 }
1896};
1897
1898/* The set of possible brightness events */
1899static struct sonypi_event sonypi_brightnessev[] = {
1900 { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
1901 { 0, 0 }
1902};
1903
Mattia Dongilide920432008-01-14 18:05:43 +09001904static struct sonypi_eventtypes type1_events[] = {
1905 { 0, 0xffffffff, sonypi_releaseev },
1906 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
1907 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
1908 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
1909 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
1910 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1911 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1912 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
1913 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1914 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
1915 { 0 },
1916};
1917static struct sonypi_eventtypes type2_events[] = {
1918 { 0, 0xffffffff, sonypi_releaseev },
1919 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
1920 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
1921 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
1922 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1923 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1924 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
1925 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
1926 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
1927 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
1928 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
1929 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1930 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1931 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1932 { 0 },
1933};
1934static struct sonypi_eventtypes type3_events[] = {
1935 { 0, 0xffffffff, sonypi_releaseev },
1936 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1937 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
1938 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1939 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1940 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09001941 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
1942 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
1943 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
Harald Jenny1cae7102009-03-26 21:58:18 +09001944 { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
1945 { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09001946 { 0 },
Mattia Dongilide920432008-01-14 18:05:43 +09001947};
1948
Mattia Dongili3eb87492008-01-14 18:05:45 +09001949/* low level spic calls */
malattia@linux.it33a04452007-04-09 19:26:03 +02001950#define ITERATIONS_LONG 10000
1951#define ITERATIONS_SHORT 10
1952#define wait_on_command(command, iterations) { \
1953 unsigned int n = iterations; \
1954 while (--n && (command)) \
1955 udelay(1); \
1956 if (!n) \
1957 dprintk("command failed at %s : %s (line %d)\n", \
Harvey Harrison6e574192008-04-29 00:59:20 -07001958 __FILE__, __func__, __LINE__); \
malattia@linux.it33a04452007-04-09 19:26:03 +02001959}
1960
1961static u8 sony_pic_call1(u8 dev)
1962{
1963 u8 v1, v2;
1964
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001965 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02001966 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001967 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1968 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
1969 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09001970 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02001971 return v2;
1972}
1973
1974static u8 sony_pic_call2(u8 dev, u8 fn)
1975{
1976 u8 v1;
1977
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001978 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02001979 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001980 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1981 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02001982 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001983 outb(fn, spic_dev.cur_ioport->io1.minimum);
1984 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09001985 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02001986 return v1;
1987}
1988
malattia@linux.it49a11de2007-04-09 19:28:56 +02001989static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
1990{
1991 u8 v1;
1992
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001993 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1994 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1995 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1996 outb(fn, spic_dev.cur_ioport->io1.minimum);
1997 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1998 outb(v, spic_dev.cur_ioport->io1.minimum);
1999 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002000 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
2001 dev, fn, v, v1);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002002 return v1;
2003}
2004
Mattia Dongili3eb87492008-01-14 18:05:45 +09002005/*
2006 * minidrivers for SPIC models
2007 */
Mattia Dongilie93c8a62009-03-26 21:58:17 +09002008static int type3_handle_irq(const u8 data_mask, const u8 ev)
Mattia Dongili3eb87492008-01-14 18:05:45 +09002009{
2010 /*
2011 * 0x31 could mean we have to take some extra action and wait for
Mattia Dongilie93c8a62009-03-26 21:58:17 +09002012 * the next irq for some Type3 models, it will generate a new
Mattia Dongili3eb87492008-01-14 18:05:45 +09002013 * irq and we can read new data from the device:
2014 * - 0x5c and 0x5f requires 0xA0
2015 * - 0x61 requires 0xB3
2016 */
2017 if (data_mask == 0x31) {
2018 if (ev == 0x5c || ev == 0x5f)
2019 sony_pic_call1(0xA0);
2020 else if (ev == 0x61)
2021 sony_pic_call1(0xB3);
2022 return 0;
2023 }
2024 return 1;
2025}
2026
Mattia Dongili3eb87492008-01-14 18:05:45 +09002027static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
2028{
2029 struct pci_dev *pcidev;
2030
2031 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2032 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
2033 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002034 dev->model = SONYPI_DEVICE_TYPE1;
2035 dev->evport_offset = SONYPI_TYPE1_OFFSET;
2036 dev->event_types = type1_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002037 goto out;
2038 }
2039
2040 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2041 PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
2042 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002043 dev->model = SONYPI_DEVICE_TYPE2;
2044 dev->evport_offset = SONYPI_TYPE2_OFFSET;
2045 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002046 goto out;
2047 }
2048
2049 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2050 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
2051 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002052 dev->model = SONYPI_DEVICE_TYPE3;
2053 dev->handle_irq = type3_handle_irq;
2054 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2055 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002056 goto out;
2057 }
2058
2059 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2060 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
2061 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002062 dev->model = SONYPI_DEVICE_TYPE3;
2063 dev->handle_irq = type3_handle_irq;
2064 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2065 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002066 goto out;
2067 }
2068
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09002069 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2070 PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
2071 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002072 dev->model = SONYPI_DEVICE_TYPE3;
2073 dev->handle_irq = type3_handle_irq;
2074 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2075 dev->event_types = type3_events;
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09002076 goto out;
2077 }
2078
Mattia Dongili3eb87492008-01-14 18:05:45 +09002079 /* default */
Mattia Dongili31df7142009-09-16 00:05:29 +09002080 dev->model = SONYPI_DEVICE_TYPE2;
2081 dev->evport_offset = SONYPI_TYPE2_OFFSET;
2082 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002083
2084out:
2085 if (pcidev)
2086 pci_dev_put(pcidev);
2087
Mattia Dongilid6697932011-02-19 11:52:28 +09002088 pr_info(DRV_PFX "detected Type%d model\n",
Mattia Dongili31df7142009-09-16 00:05:29 +09002089 dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
2090 dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
Mattia Dongili3eb87492008-01-14 18:05:45 +09002091}
2092
malattia@linux.it49a11de2007-04-09 19:28:56 +02002093/* camera tests and poweron/poweroff */
2094#define SONYPI_CAMERA_PICTURE 5
malattia@linux.it49a11de2007-04-09 19:28:56 +02002095#define SONYPI_CAMERA_CONTROL 0x10
malattia@linux.ite3646322007-04-28 23:34:22 +09002096
2097#define SONYPI_CAMERA_BRIGHTNESS 0
2098#define SONYPI_CAMERA_CONTRAST 1
2099#define SONYPI_CAMERA_HUE 2
2100#define SONYPI_CAMERA_COLOR 3
2101#define SONYPI_CAMERA_SHARPNESS 4
2102
2103#define SONYPI_CAMERA_EXPOSURE_MASK 0xC
2104#define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
2105#define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
2106#define SONYPI_CAMERA_MUTE_MASK 0x40
2107
2108/* the rest don't need a loop until not 0xff */
2109#define SONYPI_CAMERA_AGC 6
2110#define SONYPI_CAMERA_AGC_MASK 0x30
2111#define SONYPI_CAMERA_SHUTTER_MASK 0x7
2112
2113#define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
2114#define SONYPI_CAMERA_CONTROL 0x10
2115
2116#define SONYPI_CAMERA_STATUS 7
2117#define SONYPI_CAMERA_STATUS_READY 0x2
2118#define SONYPI_CAMERA_STATUS_POSITION 0x4
2119
2120#define SONYPI_DIRECTION_BACKWARDS 0x4
2121
2122#define SONYPI_CAMERA_REVISION 8
2123#define SONYPI_CAMERA_ROMVERSION 9
malattia@linux.it49a11de2007-04-09 19:28:56 +02002124
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002125static int __sony_pic_camera_ready(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002126{
2127 u8 v;
2128
2129 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
2130 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
2131}
2132
malattia@linux.ite3646322007-04-28 23:34:22 +09002133static int __sony_pic_camera_off(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002134{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002135 if (!camera) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002136 pr_warn(DRV_PFX "camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002137 return -ENODEV;
2138 }
2139
malattia@linux.it49a11de2007-04-09 19:28:56 +02002140 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
2141 SONYPI_CAMERA_MUTE_MASK),
2142 ITERATIONS_SHORT);
2143
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002144 if (spic_dev.camera_power) {
2145 sony_pic_call2(0x91, 0);
2146 spic_dev.camera_power = 0;
2147 }
2148 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002149}
2150
malattia@linux.ite3646322007-04-28 23:34:22 +09002151static int __sony_pic_camera_on(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002152{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002153 int i, j, x;
2154
2155 if (!camera) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002156 pr_warn(DRV_PFX "camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002157 return -ENODEV;
2158 }
malattia@linux.it49a11de2007-04-09 19:28:56 +02002159
2160 if (spic_dev.camera_power)
malattia@linux.ite3646322007-04-28 23:34:22 +09002161 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002162
2163 for (j = 5; j > 0; j--) {
2164
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002165 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002166 msleep(10);
2167 sony_pic_call1(0x93);
2168
2169 for (i = 400; i > 0; i--) {
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002170 if (__sony_pic_camera_ready())
malattia@linux.it49a11de2007-04-09 19:28:56 +02002171 break;
2172 msleep(10);
2173 }
2174 if (i)
2175 break;
2176 }
2177
2178 if (j == 0) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002179 pr_warn(DRV_PFX "failed to power on camera\n");
malattia@linux.ite3646322007-04-28 23:34:22 +09002180 return -ENODEV;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002181 }
2182
2183 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
2184 0x5a),
2185 ITERATIONS_SHORT);
2186
2187 spic_dev.camera_power = 1;
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002188 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002189}
2190
malattia@linux.ite3646322007-04-28 23:34:22 +09002191/* External camera command (exported to the motion eye v4l driver) */
2192int sony_pic_camera_command(int command, u8 value)
2193{
2194 if (!camera)
2195 return -EIO;
2196
2197 mutex_lock(&spic_dev.lock);
2198
2199 switch (command) {
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002200 case SONY_PIC_COMMAND_SETCAMERA:
malattia@linux.ite3646322007-04-28 23:34:22 +09002201 if (value)
2202 __sony_pic_camera_on();
2203 else
2204 __sony_pic_camera_off();
2205 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002206 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09002207 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
2208 ITERATIONS_SHORT);
2209 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002210 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
malattia@linux.ite3646322007-04-28 23:34:22 +09002211 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
2212 ITERATIONS_SHORT);
2213 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002214 case SONY_PIC_COMMAND_SETCAMERAHUE:
malattia@linux.ite3646322007-04-28 23:34:22 +09002215 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
2216 ITERATIONS_SHORT);
2217 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002218 case SONY_PIC_COMMAND_SETCAMERACOLOR:
malattia@linux.ite3646322007-04-28 23:34:22 +09002219 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
2220 ITERATIONS_SHORT);
2221 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002222 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09002223 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
2224 ITERATIONS_SHORT);
2225 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002226 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
malattia@linux.ite3646322007-04-28 23:34:22 +09002227 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
2228 ITERATIONS_SHORT);
2229 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09002230 case SONY_PIC_COMMAND_SETCAMERAAGC:
malattia@linux.ite3646322007-04-28 23:34:22 +09002231 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
2232 ITERATIONS_SHORT);
2233 break;
2234 default:
Mattia Dongilid6697932011-02-19 11:52:28 +09002235 pr_err(DRV_PFX "sony_pic_camera_command invalid: %d\n",
malattia@linux.ite3646322007-04-28 23:34:22 +09002236 command);
2237 break;
2238 }
2239 mutex_unlock(&spic_dev.lock);
2240 return 0;
2241}
2242EXPORT_SYMBOL(sony_pic_camera_command);
2243
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002244/* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002245static void __sony_pic_set_wwanpower(u8 state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002246{
2247 state = !!state;
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002248 if (spic_dev.wwan_power == state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002249 return;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002250 sony_pic_call2(0xB0, state);
Sergey Yanovich3ad1b762009-03-26 21:58:21 +09002251 sony_pic_call1(0x82);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002252 spic_dev.wwan_power = state;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002253}
2254
2255static ssize_t sony_pic_wwanpower_store(struct device *dev,
2256 struct device_attribute *attr,
2257 const char *buffer, size_t count)
2258{
2259 unsigned long value;
2260 if (count > 31)
2261 return -EINVAL;
2262
2263 value = simple_strtoul(buffer, NULL, 10);
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09002264 mutex_lock(&spic_dev.lock);
2265 __sony_pic_set_wwanpower(value);
2266 mutex_unlock(&spic_dev.lock);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002267
2268 return count;
2269}
2270
2271static ssize_t sony_pic_wwanpower_show(struct device *dev,
2272 struct device_attribute *attr, char *buffer)
2273{
2274 ssize_t count;
2275 mutex_lock(&spic_dev.lock);
2276 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
2277 mutex_unlock(&spic_dev.lock);
2278 return count;
2279}
2280
malattia@linux.it49a11de2007-04-09 19:28:56 +02002281/* bluetooth subsystem power state */
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002282static void __sony_pic_set_bluetoothpower(u8 state)
malattia@linux.it49a11de2007-04-09 19:28:56 +02002283{
2284 state = !!state;
2285 if (spic_dev.bluetooth_power == state)
2286 return;
2287 sony_pic_call2(0x96, state);
2288 sony_pic_call1(0x82);
2289 spic_dev.bluetooth_power = state;
2290}
2291
2292static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
2293 struct device_attribute *attr,
2294 const char *buffer, size_t count)
2295{
2296 unsigned long value;
2297 if (count > 31)
2298 return -EINVAL;
2299
2300 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002301 mutex_lock(&spic_dev.lock);
2302 __sony_pic_set_bluetoothpower(value);
2303 mutex_unlock(&spic_dev.lock);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002304
2305 return count;
2306}
2307
2308static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
2309 struct device_attribute *attr, char *buffer)
2310{
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002311 ssize_t count = 0;
2312 mutex_lock(&spic_dev.lock);
2313 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
2314 mutex_unlock(&spic_dev.lock);
2315 return count;
malattia@linux.it49a11de2007-04-09 19:28:56 +02002316}
2317
2318/* fan speed */
2319/* FAN0 information (reverse engineered from ACPI tables) */
2320#define SONY_PIC_FAN0_STATUS 0x93
malattia@linux.it7b153f32007-04-09 19:31:25 +02002321static int sony_pic_set_fanspeed(unsigned long value)
2322{
2323 return ec_write(SONY_PIC_FAN0_STATUS, value);
2324}
2325
2326static int sony_pic_get_fanspeed(u8 *value)
2327{
2328 return ec_read(SONY_PIC_FAN0_STATUS, value);
2329}
2330
malattia@linux.it49a11de2007-04-09 19:28:56 +02002331static ssize_t sony_pic_fanspeed_store(struct device *dev,
2332 struct device_attribute *attr,
2333 const char *buffer, size_t count)
2334{
2335 unsigned long value;
2336 if (count > 31)
2337 return -EINVAL;
2338
2339 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002340 if (sony_pic_set_fanspeed(value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02002341 return -EIO;
2342
2343 return count;
2344}
2345
2346static ssize_t sony_pic_fanspeed_show(struct device *dev,
2347 struct device_attribute *attr, char *buffer)
2348{
2349 u8 value = 0;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002350 if (sony_pic_get_fanspeed(&value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02002351 return -EIO;
2352
2353 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
2354}
2355
2356#define SPIC_ATTR(_name, _mode) \
2357struct device_attribute spic_attr_##_name = __ATTR(_name, \
2358 _mode, sony_pic_## _name ##_show, \
2359 sony_pic_## _name ##_store)
2360
malattia@linux.it49a11de2007-04-09 19:28:56 +02002361static SPIC_ATTR(bluetoothpower, 0644);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002362static SPIC_ATTR(wwanpower, 0644);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002363static SPIC_ATTR(fanspeed, 0644);
2364
2365static struct attribute *spic_attributes[] = {
malattia@linux.it49a11de2007-04-09 19:28:56 +02002366 &spic_attr_bluetoothpower.attr,
malattia@linux.it9476cdf2007-04-28 23:21:42 +09002367 &spic_attr_wwanpower.attr,
malattia@linux.it49a11de2007-04-09 19:28:56 +02002368 &spic_attr_fanspeed.attr,
2369 NULL
2370};
2371
2372static struct attribute_group spic_attribute_group = {
2373 .attrs = spic_attributes
2374};
2375
malattia@linux.it7b153f32007-04-09 19:31:25 +02002376/******** SONYPI compatibility **********/
Mattia Dongilia64e62a2007-05-01 11:19:53 +09002377#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +02002378
2379/* battery / brightness / temperature addresses */
2380#define SONYPI_BAT_FLAGS 0x81
2381#define SONYPI_LCD_LIGHT 0x96
2382#define SONYPI_BAT1_PCTRM 0xa0
2383#define SONYPI_BAT1_LEFT 0xa2
2384#define SONYPI_BAT1_MAXRT 0xa4
2385#define SONYPI_BAT2_PCTRM 0xa8
2386#define SONYPI_BAT2_LEFT 0xaa
2387#define SONYPI_BAT2_MAXRT 0xac
2388#define SONYPI_BAT1_MAXTK 0xb0
2389#define SONYPI_BAT1_FULL 0xb2
2390#define SONYPI_BAT2_MAXTK 0xb8
2391#define SONYPI_BAT2_FULL 0xba
2392#define SONYPI_TEMP_STATUS 0xC1
2393
2394struct sonypi_compat_s {
2395 struct fasync_struct *fifo_async;
Stefani Seibold45465482009-12-21 14:37:26 -08002396 struct kfifo fifo;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002397 spinlock_t fifo_lock;
2398 wait_queue_head_t fifo_proc_list;
2399 atomic_t open_count;
2400};
2401static struct sonypi_compat_s sonypi_compat = {
2402 .open_count = ATOMIC_INIT(0),
2403};
2404
2405static int sonypi_misc_fasync(int fd, struct file *filp, int on)
2406{
Jonathan Corbet60aa4922009-02-01 14:52:56 -07002407 return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002408}
2409
2410static int sonypi_misc_release(struct inode *inode, struct file *file)
2411{
malattia@linux.it7b153f32007-04-09 19:31:25 +02002412 atomic_dec(&sonypi_compat.open_count);
2413 return 0;
2414}
2415
2416static int sonypi_misc_open(struct inode *inode, struct file *file)
2417{
2418 /* Flush input queue on first open */
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002419 unsigned long flags;
2420
Stefani Seibold45465482009-12-21 14:37:26 -08002421 spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002422
malattia@linux.it7b153f32007-04-09 19:31:25 +02002423 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
Stefani Seibolde64c0262009-12-21 14:37:28 -08002424 kfifo_reset(&sonypi_compat.fifo);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002425
Stefani Seibold45465482009-12-21 14:37:26 -08002426 spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09002427
malattia@linux.it7b153f32007-04-09 19:31:25 +02002428 return 0;
2429}
2430
2431static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
2432 size_t count, loff_t *pos)
2433{
2434 ssize_t ret;
2435 unsigned char c;
2436
Stefani Seibold45465482009-12-21 14:37:26 -08002437 if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
malattia@linux.it7b153f32007-04-09 19:31:25 +02002438 (file->f_flags & O_NONBLOCK))
2439 return -EAGAIN;
2440
2441 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
Stefani Seibold45465482009-12-21 14:37:26 -08002442 kfifo_len(&sonypi_compat.fifo) != 0);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002443 if (ret)
2444 return ret;
2445
2446 while (ret < count &&
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002447 (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002448 &sonypi_compat.fifo_lock) == sizeof(c))) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02002449 if (put_user(c, buf++))
2450 return -EFAULT;
2451 ret++;
2452 }
2453
2454 if (ret > 0) {
2455 struct inode *inode = file->f_path.dentry->d_inode;
2456 inode->i_atime = current_fs_time(inode->i_sb);
2457 }
2458
2459 return ret;
2460}
2461
2462static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
2463{
2464 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
Stefani Seibold45465482009-12-21 14:37:26 -08002465 if (kfifo_len(&sonypi_compat.fifo))
malattia@linux.it7b153f32007-04-09 19:31:25 +02002466 return POLLIN | POLLRDNORM;
2467 return 0;
2468}
2469
2470static int ec_read16(u8 addr, u16 *value)
2471{
2472 u8 val_lb, val_hb;
2473 if (ec_read(addr, &val_lb))
2474 return -1;
2475 if (ec_read(addr + 1, &val_hb))
2476 return -1;
2477 *value = val_lb | (val_hb << 8);
2478 return 0;
2479}
2480
Alan Cox2b24ef02009-03-26 21:58:19 +09002481static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
2482 unsigned long arg)
malattia@linux.it7b153f32007-04-09 19:31:25 +02002483{
2484 int ret = 0;
2485 void __user *argp = (void __user *)arg;
2486 u8 val8;
2487 u16 val16;
2488 int value;
2489
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002490 mutex_lock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002491 switch (cmd) {
2492 case SONYPI_IOCGBRT:
2493 if (sony_backlight_device == NULL) {
2494 ret = -EIO;
2495 break;
2496 }
2497 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
2498 ret = -EIO;
2499 break;
2500 }
2501 val8 = ((value & 0xff) - 1) << 5;
2502 if (copy_to_user(argp, &val8, sizeof(val8)))
2503 ret = -EFAULT;
2504 break;
2505 case SONYPI_IOCSBRT:
2506 if (sony_backlight_device == NULL) {
2507 ret = -EIO;
2508 break;
2509 }
2510 if (copy_from_user(&val8, argp, sizeof(val8))) {
2511 ret = -EFAULT;
2512 break;
2513 }
2514 if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
2515 (val8 >> 5) + 1, NULL)) {
2516 ret = -EIO;
2517 break;
2518 }
2519 /* sync the backlight device status */
2520 sony_backlight_device->props.brightness =
2521 sony_backlight_get_brightness(sony_backlight_device);
2522 break;
2523 case SONYPI_IOCGBAT1CAP:
2524 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
2525 ret = -EIO;
2526 break;
2527 }
2528 if (copy_to_user(argp, &val16, sizeof(val16)))
2529 ret = -EFAULT;
2530 break;
2531 case SONYPI_IOCGBAT1REM:
2532 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
2533 ret = -EIO;
2534 break;
2535 }
2536 if (copy_to_user(argp, &val16, sizeof(val16)))
2537 ret = -EFAULT;
2538 break;
2539 case SONYPI_IOCGBAT2CAP:
2540 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
2541 ret = -EIO;
2542 break;
2543 }
2544 if (copy_to_user(argp, &val16, sizeof(val16)))
2545 ret = -EFAULT;
2546 break;
2547 case SONYPI_IOCGBAT2REM:
2548 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
2549 ret = -EIO;
2550 break;
2551 }
2552 if (copy_to_user(argp, &val16, sizeof(val16)))
2553 ret = -EFAULT;
2554 break;
2555 case SONYPI_IOCGBATFLAGS:
2556 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
2557 ret = -EIO;
2558 break;
2559 }
2560 val8 &= 0x07;
2561 if (copy_to_user(argp, &val8, sizeof(val8)))
2562 ret = -EFAULT;
2563 break;
2564 case SONYPI_IOCGBLUE:
2565 val8 = spic_dev.bluetooth_power;
2566 if (copy_to_user(argp, &val8, sizeof(val8)))
2567 ret = -EFAULT;
2568 break;
2569 case SONYPI_IOCSBLUE:
2570 if (copy_from_user(&val8, argp, sizeof(val8))) {
2571 ret = -EFAULT;
2572 break;
2573 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002574 __sony_pic_set_bluetoothpower(val8);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002575 break;
2576 /* FAN Controls */
2577 case SONYPI_IOCGFAN:
2578 if (sony_pic_get_fanspeed(&val8)) {
2579 ret = -EIO;
2580 break;
2581 }
2582 if (copy_to_user(argp, &val8, sizeof(val8)))
2583 ret = -EFAULT;
2584 break;
2585 case SONYPI_IOCSFAN:
2586 if (copy_from_user(&val8, argp, sizeof(val8))) {
2587 ret = -EFAULT;
2588 break;
2589 }
2590 if (sony_pic_set_fanspeed(val8))
2591 ret = -EIO;
2592 break;
2593 /* GET Temperature (useful under APM) */
2594 case SONYPI_IOCGTEMP:
2595 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
2596 ret = -EIO;
2597 break;
2598 }
2599 if (copy_to_user(argp, &val8, sizeof(val8)))
2600 ret = -EFAULT;
2601 break;
2602 default:
2603 ret = -EINVAL;
2604 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002605 mutex_unlock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002606 return ret;
2607}
2608
2609static const struct file_operations sonypi_misc_fops = {
2610 .owner = THIS_MODULE,
2611 .read = sonypi_misc_read,
2612 .poll = sonypi_misc_poll,
2613 .open = sonypi_misc_open,
2614 .release = sonypi_misc_release,
2615 .fasync = sonypi_misc_fasync,
Alan Cox2b24ef02009-03-26 21:58:19 +09002616 .unlocked_ioctl = sonypi_misc_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002617 .llseek = noop_llseek,
malattia@linux.it7b153f32007-04-09 19:31:25 +02002618};
2619
2620static struct miscdevice sonypi_misc_device = {
2621 .minor = MISC_DYNAMIC_MINOR,
2622 .name = "sonypi",
2623 .fops = &sonypi_misc_fops,
2624};
2625
2626static void sonypi_compat_report_event(u8 event)
2627{
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002628 kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002629 sizeof(event), &sonypi_compat.fifo_lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002630 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
2631 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
2632}
2633
2634static int sonypi_compat_init(void)
2635{
2636 int error;
2637
2638 spin_lock_init(&sonypi_compat.fifo_lock);
Stefani Seibold45465482009-12-21 14:37:26 -08002639 error =
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002640 kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -08002641 if (error) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002642 pr_err(DRV_PFX "kfifo_alloc failed\n");
Stefani Seibold45465482009-12-21 14:37:26 -08002643 return error;
malattia@linux.it7b153f32007-04-09 19:31:25 +02002644 }
2645
2646 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002647
2648 if (minor != -1)
2649 sonypi_misc_device.minor = minor;
2650 error = misc_register(&sonypi_misc_device);
2651 if (error) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002652 pr_err(DRV_PFX "misc_register failed\n");
malattia@linux.it7b153f32007-04-09 19:31:25 +02002653 goto err_free_kfifo;
2654 }
2655 if (minor == -1)
Mattia Dongilid6697932011-02-19 11:52:28 +09002656 pr_info(DRV_PFX "device allocated minor is %d\n",
malattia@linux.it7b153f32007-04-09 19:31:25 +02002657 sonypi_misc_device.minor);
2658
2659 return 0;
2660
2661err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -08002662 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002663 return error;
2664}
2665
2666static void sonypi_compat_exit(void)
2667{
2668 misc_deregister(&sonypi_misc_device);
Stefani Seibold45465482009-12-21 14:37:26 -08002669 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002670}
2671#else
2672static int sonypi_compat_init(void) { return 0; }
2673static void sonypi_compat_exit(void) { }
2674static void sonypi_compat_report_event(u8 event) { }
Mattia Dongilia64e62a2007-05-01 11:19:53 +09002675#endif /* CONFIG_SONYPI_COMPAT */
malattia@linux.it7b153f32007-04-09 19:31:25 +02002676
malattia@linux.it1549ee62007-04-09 10:19:08 +02002677/*
malattia@linux.it33a04452007-04-09 19:26:03 +02002678 * ACPI callbacks
malattia@linux.it1549ee62007-04-09 10:19:08 +02002679 */
malattia@linux.it33a04452007-04-09 19:26:03 +02002680static acpi_status
2681sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
2682{
2683 u32 i;
2684 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
2685
2686 switch (resource->type) {
2687 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002688 {
2689 /* start IO enumeration */
2690 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
2691 if (!ioport)
2692 return AE_ERROR;
2693
2694 list_add(&ioport->list, &dev->ioports);
2695 return AE_OK;
2696 }
2697
malattia@linux.it33a04452007-04-09 19:26:03 +02002698 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002699 /* end IO enumeration */
malattia@linux.it33a04452007-04-09 19:26:03 +02002700 return AE_OK;
2701
2702 case ACPI_RESOURCE_TYPE_IRQ:
2703 {
2704 struct acpi_resource_irq *p = &resource->data.irq;
2705 struct sony_pic_irq *interrupt = NULL;
2706 if (!p || !p->interrupt_count) {
2707 /*
2708 * IRQ descriptors may have no IRQ# bits set,
2709 * particularly those those w/ _STA disabled
2710 */
2711 dprintk("Blank IRQ resource\n");
2712 return AE_OK;
2713 }
2714 for (i = 0; i < p->interrupt_count; i++) {
2715 if (!p->interrupts[i]) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002716 pr_warn(DRV_PFX "Invalid IRQ %d\n",
malattia@linux.it33a04452007-04-09 19:26:03 +02002717 p->interrupts[i]);
2718 continue;
2719 }
2720 interrupt = kzalloc(sizeof(*interrupt),
2721 GFP_KERNEL);
2722 if (!interrupt)
2723 return AE_ERROR;
2724
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002725 list_add(&interrupt->list, &dev->interrupts);
malattia@linux.it33a04452007-04-09 19:26:03 +02002726 interrupt->irq.triggering = p->triggering;
2727 interrupt->irq.polarity = p->polarity;
2728 interrupt->irq.sharable = p->sharable;
2729 interrupt->irq.interrupt_count = 1;
2730 interrupt->irq.interrupts[0] = p->interrupts[i];
2731 }
2732 return AE_OK;
2733 }
2734 case ACPI_RESOURCE_TYPE_IO:
2735 {
2736 struct acpi_resource_io *io = &resource->data.io;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002737 struct sony_pic_ioport *ioport =
2738 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
malattia@linux.it33a04452007-04-09 19:26:03 +02002739 if (!io) {
2740 dprintk("Blank IO resource\n");
2741 return AE_OK;
2742 }
2743
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002744 if (!ioport->io1.minimum) {
2745 memcpy(&ioport->io1, io, sizeof(*io));
2746 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
2747 ioport->io1.address_length);
2748 }
2749 else if (!ioport->io2.minimum) {
2750 memcpy(&ioport->io2, io, sizeof(*io));
2751 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
2752 ioport->io2.address_length);
2753 }
2754 else {
Mattia Dongilid6697932011-02-19 11:52:28 +09002755 pr_err(DRV_PFX "Unknown SPIC Type, more than 2 IO Ports\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002756 return AE_ERROR;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002757 }
malattia@linux.it33a04452007-04-09 19:26:03 +02002758 return AE_OK;
2759 }
2760 default:
2761 dprintk("Resource %d isn't an IRQ nor an IO port\n",
2762 resource->type);
2763
2764 case ACPI_RESOURCE_TYPE_END_TAG:
2765 return AE_OK;
2766 }
2767 return AE_CTRL_TERMINATE;
2768}
2769
2770static int sony_pic_possible_resources(struct acpi_device *device)
2771{
2772 int result = 0;
2773 acpi_status status = AE_OK;
2774
2775 if (!device)
2776 return -EINVAL;
2777
2778 /* get device status */
2779 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
2780 dprintk("Evaluating _STA\n");
2781 result = acpi_bus_get_status(device);
2782 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002783 pr_warn(DRV_PFX "Unable to read status\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002784 goto end;
2785 }
2786
2787 if (!device->status.enabled)
2788 dprintk("Device disabled\n");
2789 else
2790 dprintk("Device enabled\n");
2791
2792 /*
2793 * Query and parse 'method'
2794 */
2795 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
2796 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
2797 sony_pic_read_possible_resource, &spic_dev);
2798 if (ACPI_FAILURE(status)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002799 pr_warn(DRV_PFX "Failure evaluating %s\n",
malattia@linux.it33a04452007-04-09 19:26:03 +02002800 METHOD_NAME__PRS);
2801 result = -ENODEV;
2802 }
2803end:
2804 return result;
2805}
2806
2807/*
2808 * Disable the spic device by calling its _DIS method
2809 */
2810static int sony_pic_disable(struct acpi_device *device)
2811{
Matthew Garrett6158d3a2008-10-29 14:01:03 -07002812 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
2813 NULL);
2814
2815 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
malattia@linux.it33a04452007-04-09 19:26:03 +02002816 return -ENXIO;
2817
2818 dprintk("Device disabled\n");
2819 return 0;
2820}
2821
2822
2823/*
2824 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
2825 *
2826 * Call _SRS to set current resources
2827 */
2828static int sony_pic_enable(struct acpi_device *device,
2829 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
2830{
2831 acpi_status status;
2832 int result = 0;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002833 /* Type 1 resource layout is:
2834 * IO
2835 * IO
2836 * IRQNoFlags
2837 * End
2838 *
2839 * Type 2 and 3 resource layout is:
2840 * IO
2841 * IRQNoFlags
2842 * End
2843 */
malattia@linux.it33a04452007-04-09 19:26:03 +02002844 struct {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002845 struct acpi_resource res1;
2846 struct acpi_resource res2;
2847 struct acpi_resource res3;
2848 struct acpi_resource res4;
malattia@linux.it33a04452007-04-09 19:26:03 +02002849 } *resource;
2850 struct acpi_buffer buffer = { 0, NULL };
2851
2852 if (!ioport || !irq)
2853 return -EINVAL;
2854
2855 /* init acpi_buffer */
2856 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
2857 if (!resource)
2858 return -ENOMEM;
2859
2860 buffer.length = sizeof(*resource) + 1;
2861 buffer.pointer = resource;
2862
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002863 /* setup Type 1 resources */
Mattia Dongili31df7142009-09-16 00:05:29 +09002864 if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002865
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002866 /* setup io resources */
2867 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2868 resource->res1.length = sizeof(struct acpi_resource);
2869 memcpy(&resource->res1.data.io, &ioport->io1,
2870 sizeof(struct acpi_resource_io));
malattia@linux.it33a04452007-04-09 19:26:03 +02002871
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002872 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
2873 resource->res2.length = sizeof(struct acpi_resource);
2874 memcpy(&resource->res2.data.io, &ioport->io2,
2875 sizeof(struct acpi_resource_io));
2876
2877 /* setup irq resource */
2878 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
2879 resource->res3.length = sizeof(struct acpi_resource);
2880 memcpy(&resource->res3.data.irq, &irq->irq,
2881 sizeof(struct acpi_resource_irq));
2882 /* we requested a shared irq */
2883 resource->res3.data.irq.sharable = ACPI_SHARED;
2884
2885 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
2886
2887 }
2888 /* setup Type 2/3 resources */
2889 else {
2890 /* setup io resource */
2891 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2892 resource->res1.length = sizeof(struct acpi_resource);
2893 memcpy(&resource->res1.data.io, &ioport->io1,
2894 sizeof(struct acpi_resource_io));
2895
2896 /* setup irq resource */
2897 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
2898 resource->res2.length = sizeof(struct acpi_resource);
2899 memcpy(&resource->res2.data.irq, &irq->irq,
2900 sizeof(struct acpi_resource_irq));
2901 /* we requested a shared irq */
2902 resource->res2.data.irq.sharable = ACPI_SHARED;
2903
2904 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
2905 }
malattia@linux.it33a04452007-04-09 19:26:03 +02002906
2907 /* Attempt to set the resource */
2908 dprintk("Evaluating _SRS\n");
2909 status = acpi_set_current_resources(device->handle, &buffer);
2910
2911 /* check for total failure */
2912 if (ACPI_FAILURE(status)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002913 pr_err(DRV_PFX "Error evaluating _SRS\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002914 result = -ENODEV;
2915 goto end;
2916 }
2917
2918 /* Necessary device initializations calls (from sonypi) */
2919 sony_pic_call1(0x82);
2920 sony_pic_call2(0x81, 0xff);
2921 sony_pic_call1(compat ? 0x92 : 0x82);
2922
2923end:
2924 kfree(resource);
2925 return result;
2926}
2927
2928/*****************
2929 *
2930 * ISR: some event is available
2931 *
2932 *****************/
2933static irqreturn_t sony_pic_irq(int irq, void *dev_id)
2934{
2935 int i, j;
malattia@linux.it33a04452007-04-09 19:26:03 +02002936 u8 ev = 0;
2937 u8 data_mask = 0;
2938 u8 device_event = 0;
2939
2940 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
2941
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002942 ev = inb_p(dev->cur_ioport->io1.minimum);
2943 if (dev->cur_ioport->io2.minimum)
2944 data_mask = inb_p(dev->cur_ioport->io2.minimum);
2945 else
Mattia Dongilide920432008-01-14 18:05:43 +09002946 data_mask = inb_p(dev->cur_ioport->io1.minimum +
Mattia Dongili31df7142009-09-16 00:05:29 +09002947 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02002948
Mattia Dongili22a17782007-07-16 02:34:39 +09002949 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
Mattia Dongilide920432008-01-14 18:05:43 +09002950 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09002951 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02002952
2953 if (ev == 0x00 || ev == 0xff)
2954 return IRQ_HANDLED;
2955
Mattia Dongili31df7142009-09-16 00:05:29 +09002956 for (i = 0; dev->event_types[i].mask; i++) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002957
Mattia Dongili31df7142009-09-16 00:05:29 +09002958 if ((data_mask & dev->event_types[i].data) !=
2959 dev->event_types[i].data)
malattia@linux.it33a04452007-04-09 19:26:03 +02002960 continue;
2961
Mattia Dongili31df7142009-09-16 00:05:29 +09002962 if (!(mask & dev->event_types[i].mask))
malattia@linux.it33a04452007-04-09 19:26:03 +02002963 continue;
2964
Mattia Dongili31df7142009-09-16 00:05:29 +09002965 for (j = 0; dev->event_types[i].events[j].event; j++) {
2966 if (ev == dev->event_types[i].events[j].data) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002967 device_event =
Mattia Dongili31df7142009-09-16 00:05:29 +09002968 dev->event_types[i].events[j].event;
Mattia Dongili4eeb5022011-02-19 11:52:27 +09002969 /* some events may require ignoring */
2970 if (!device_event)
2971 return IRQ_HANDLED;
malattia@linux.it33a04452007-04-09 19:26:03 +02002972 goto found;
2973 }
2974 }
2975 }
Mattia Dongili3eb87492008-01-14 18:05:45 +09002976 /* Still not able to decode the event try to pass
2977 * it over to the minidriver
2978 */
Mattia Dongili31df7142009-09-16 00:05:29 +09002979 if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
Mattia Dongili3eb87492008-01-14 18:05:45 +09002980 return IRQ_HANDLED;
2981
Mattia Dongilide920432008-01-14 18:05:43 +09002982 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
2983 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09002984 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02002985 return IRQ_HANDLED;
2986
2987found:
malattia@linux.it1549ee62007-04-09 10:19:08 +02002988 sony_laptop_report_input_event(device_event);
Mattia Dongilide920432008-01-14 18:05:43 +09002989 acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002990 sonypi_compat_report_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02002991 return IRQ_HANDLED;
2992}
2993
2994/*****************
2995 *
2996 * ACPI driver
2997 *
2998 *****************/
2999static int sony_pic_remove(struct acpi_device *device, int type)
3000{
3001 struct sony_pic_ioport *io, *tmp_io;
3002 struct sony_pic_irq *irq, *tmp_irq;
3003
3004 if (sony_pic_disable(device)) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003005 pr_err(DRV_PFX "Couldn't disable device.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003006 return -ENXIO;
3007 }
3008
3009 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003010 release_region(spic_dev.cur_ioport->io1.minimum,
3011 spic_dev.cur_ioport->io1.address_length);
3012 if (spic_dev.cur_ioport->io2.minimum)
3013 release_region(spic_dev.cur_ioport->io2.minimum,
3014 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02003015
Mattia Dongili015a9162007-08-12 16:20:27 +09003016 sonypi_compat_exit();
3017
malattia@linux.it1549ee62007-04-09 10:19:08 +02003018 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02003019
malattia@linux.it49a11de2007-04-09 19:28:56 +02003020 /* pf attrs */
3021 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
3022 sony_pf_remove();
3023
malattia@linux.it33a04452007-04-09 19:26:03 +02003024 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
3025 list_del(&io->list);
3026 kfree(io);
3027 }
3028 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
3029 list_del(&irq->list);
3030 kfree(irq);
3031 }
3032 spic_dev.cur_ioport = NULL;
3033 spic_dev.cur_irq = NULL;
3034
malattia@linux.itf6119b02007-04-09 19:31:06 +02003035 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003036 return 0;
3037}
3038
3039static int sony_pic_add(struct acpi_device *device)
3040{
3041 int result;
3042 struct sony_pic_ioport *io, *tmp_io;
3043 struct sony_pic_irq *irq, *tmp_irq;
3044
Mattia Dongilid6697932011-02-19 11:52:28 +09003045 pr_info(DRV_PFX "%s v%s.\n", SONY_PIC_DRIVER_NAME,
3046 SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.it33a04452007-04-09 19:26:03 +02003047
3048 spic_dev.acpi_dev = device;
3049 strcpy(acpi_device_class(device), "sony/hotkey");
Mattia Dongilide920432008-01-14 18:05:43 +09003050 sony_pic_detect_device_type(&spic_dev);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003051 mutex_init(&spic_dev.lock);
malattia@linux.it33a04452007-04-09 19:26:03 +02003052
3053 /* read _PRS resources */
3054 result = sony_pic_possible_resources(device);
3055 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003056 pr_err(DRV_PFX "Unable to read possible resources.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003057 goto err_free_resources;
3058 }
3059
3060 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05003061 result = sony_laptop_setup_input(device);
malattia@linux.it33a04452007-04-09 19:26:03 +02003062 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003063 pr_err(DRV_PFX "Unable to create input devices.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003064 goto err_free_resources;
3065 }
3066
Mattia Dongili015a9162007-08-12 16:20:27 +09003067 if (sonypi_compat_init())
3068 goto err_remove_input;
3069
malattia@linux.it33a04452007-04-09 19:26:03 +02003070 /* request io port */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003071 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
3072 if (request_region(io->io1.minimum, io->io1.address_length,
malattia@linux.it33a04452007-04-09 19:26:03 +02003073 "Sony Programable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003074 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
3075 io->io1.minimum, io->io1.maximum,
3076 io->io1.address_length);
3077 /* Type 1 have 2 ioports */
3078 if (io->io2.minimum) {
3079 if (request_region(io->io2.minimum,
3080 io->io2.address_length,
3081 "Sony Programable I/O Device")) {
3082 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
3083 io->io2.minimum, io->io2.maximum,
3084 io->io2.address_length);
3085 spic_dev.cur_ioport = io;
3086 break;
3087 }
3088 else {
3089 dprintk("Unable to get I/O port2: "
3090 "0x%.4x (0x%.4x) + 0x%.2x\n",
3091 io->io2.minimum, io->io2.maximum,
3092 io->io2.address_length);
3093 release_region(io->io1.minimum,
3094 io->io1.address_length);
3095 }
3096 }
3097 else {
3098 spic_dev.cur_ioport = io;
3099 break;
3100 }
malattia@linux.it33a04452007-04-09 19:26:03 +02003101 }
3102 }
3103 if (!spic_dev.cur_ioport) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003104 pr_err(DRV_PFX "Failed to request_region.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003105 result = -ENODEV;
Mattia Dongili015a9162007-08-12 16:20:27 +09003106 goto err_remove_compat;
malattia@linux.it33a04452007-04-09 19:26:03 +02003107 }
3108
3109 /* request IRQ */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003110 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003111 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
Mattia Dongilid1e0de92009-09-16 00:05:30 +09003112 IRQF_DISABLED, "sony-laptop", &spic_dev)) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003113 dprintk("IRQ: %d - triggering: %d - "
3114 "polarity: %d - shr: %d\n",
3115 irq->irq.interrupts[0],
3116 irq->irq.triggering,
3117 irq->irq.polarity,
3118 irq->irq.sharable);
3119 spic_dev.cur_irq = irq;
3120 break;
3121 }
3122 }
3123 if (!spic_dev.cur_irq) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003124 pr_err(DRV_PFX "Failed to request_irq.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003125 result = -ENODEV;
3126 goto err_release_region;
3127 }
3128
3129 /* set resource status _SRS */
3130 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
3131 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003132 pr_err(DRV_PFX "Couldn't enable device.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003133 goto err_free_irq;
3134 }
3135
malattia@linux.it49a11de2007-04-09 19:28:56 +02003136 spic_dev.bluetooth_power = -1;
3137 /* create device attributes */
3138 result = sony_pf_add();
3139 if (result)
3140 goto err_disable_device;
3141
3142 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
3143 if (result)
3144 goto err_remove_pf;
3145
malattia@linux.it33a04452007-04-09 19:26:03 +02003146 return 0;
3147
malattia@linux.it49a11de2007-04-09 19:28:56 +02003148err_remove_pf:
3149 sony_pf_remove();
3150
3151err_disable_device:
3152 sony_pic_disable(device);
3153
malattia@linux.it33a04452007-04-09 19:26:03 +02003154err_free_irq:
3155 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
3156
3157err_release_region:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003158 release_region(spic_dev.cur_ioport->io1.minimum,
3159 spic_dev.cur_ioport->io1.address_length);
3160 if (spic_dev.cur_ioport->io2.minimum)
3161 release_region(spic_dev.cur_ioport->io2.minimum,
3162 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02003163
Mattia Dongili015a9162007-08-12 16:20:27 +09003164err_remove_compat:
3165 sonypi_compat_exit();
3166
malattia@linux.it33a04452007-04-09 19:26:03 +02003167err_remove_input:
malattia@linux.it1549ee62007-04-09 10:19:08 +02003168 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02003169
3170err_free_resources:
3171 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
3172 list_del(&io->list);
3173 kfree(io);
3174 }
3175 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
3176 list_del(&irq->list);
3177 kfree(irq);
3178 }
3179 spic_dev.cur_ioport = NULL;
3180 spic_dev.cur_irq = NULL;
3181
3182 return result;
3183}
3184
3185static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
3186{
3187 if (sony_pic_disable(device))
3188 return -ENXIO;
3189 return 0;
3190}
3191
3192static int sony_pic_resume(struct acpi_device *device)
3193{
3194 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
3195 return 0;
3196}
3197
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003198static const struct acpi_device_id sony_pic_device_ids[] = {
3199 {SONY_PIC_HID, 0},
3200 {"", 0},
3201};
3202
malattia@linux.it33a04452007-04-09 19:26:03 +02003203static struct acpi_driver sony_pic_driver = {
3204 .name = SONY_PIC_DRIVER_NAME,
3205 .class = SONY_PIC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003206 .ids = sony_pic_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02003207 .owner = THIS_MODULE,
3208 .ops = {
3209 .add = sony_pic_add,
3210 .remove = sony_pic_remove,
3211 .suspend = sony_pic_suspend,
3212 .resume = sony_pic_resume,
3213 },
3214};
3215
3216static struct dmi_system_id __initdata sonypi_dmi_table[] = {
3217 {
3218 .ident = "Sony Vaio",
3219 .matches = {
3220 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
3221 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
3222 },
3223 },
3224 {
3225 .ident = "Sony Vaio",
3226 .matches = {
3227 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
3228 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
3229 },
3230 },
3231 { }
3232};
3233
malattia@linux.it59b19102007-04-09 10:19:04 +02003234static int __init sony_laptop_init(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01003235{
malattia@linux.it33a04452007-04-09 19:26:03 +02003236 int result;
3237
3238 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
3239 result = acpi_bus_register_driver(&sony_pic_driver);
3240 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003241 pr_err(DRV_PFX "Unable to register SPIC driver.");
malattia@linux.it33a04452007-04-09 19:26:03 +02003242 goto out;
3243 }
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003244 spic_drv_registered = 1;
malattia@linux.it33a04452007-04-09 19:26:03 +02003245 }
3246
3247 result = acpi_bus_register_driver(&sony_nc_driver);
3248 if (result) {
Mattia Dongilid6697932011-02-19 11:52:28 +09003249 pr_err(DRV_PFX "Unable to register SNC driver.");
malattia@linux.it33a04452007-04-09 19:26:03 +02003250 goto out_unregister_pic;
3251 }
3252
3253 return 0;
3254
3255out_unregister_pic:
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003256 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02003257 acpi_bus_unregister_driver(&sony_pic_driver);
3258out:
3259 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +01003260}
3261
malattia@linux.it59b19102007-04-09 10:19:04 +02003262static void __exit sony_laptop_exit(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01003263{
malattia@linux.it59b19102007-04-09 10:19:04 +02003264 acpi_bus_unregister_driver(&sony_nc_driver);
Alan Jenkins5e6f9722009-09-16 00:05:32 +09003265 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02003266 acpi_bus_unregister_driver(&sony_pic_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +01003267}
3268
malattia@linux.it59b19102007-04-09 10:19:04 +02003269module_init(sony_laptop_init);
3270module_exit(sony_laptop_exit);