blob: 04deed826180b4efd175aef6aed2d0ef2a6a08b7 [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 Dongilied3aa4b2007-02-07 20:01:54 +01005 * Copyright (C) 2007 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>
Arnd Bergmann0410e682008-05-20 19:16:45 +020049#include <linux/smp_lock.h>
Stelian Pop7f09c432007-01-13 23:04:31 +010050#include <linux/types.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010051#include <linux/backlight.h>
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010052#include <linux/platform_device.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010053#include <linux/err.h>
malattia@linux.it33a04452007-04-09 19:26:03 +020054#include <linux/dmi.h>
55#include <linux/pci.h>
56#include <linux/interrupt.h>
57#include <linux/delay.h>
58#include <linux/input.h>
59#include <linux/kfifo.h>
60#include <linux/workqueue.h>
61#include <linux/acpi.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>
Mattia Dongilia64e62a2007-05-01 11:19:53 +090067#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +020068#include <linux/poll.h>
69#include <linux/miscdevice.h>
70#endif
Stelian Pop7f09c432007-01-13 23:04:31 +010071
malattia@linux.it33a04452007-04-09 19:26:03 +020072#define DRV_PFX "sony-laptop: "
malattia@linux.itb9a218b2007-04-09 10:19:06 +020073#define dprintk(msg...) do { \
malattia@linux.itf6119b02007-04-09 19:31:06 +020074 if (debug) printk(KERN_WARNING DRV_PFX msg); \
malattia@linux.itb9a218b2007-04-09 10:19:06 +020075} while (0)
76
Mattia Dongili425ef5d2008-01-14 18:05:44 +090077#define SONY_LAPTOP_DRIVER_VERSION "0.6"
Alessandro Guido50f62af2007-01-13 23:04:34 +010078
malattia@linux.it33a04452007-04-09 19:26:03 +020079#define SONY_NC_CLASS "sony-nc"
80#define SONY_NC_HID "SNY5001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020081#define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
malattia@linux.it33a04452007-04-09 19:26:03 +020082
83#define SONY_PIC_CLASS "sony-pic"
84#define SONY_PIC_HID "SNY6001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020085#define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
Stelian Pop7f09c432007-01-13 23:04:31 +010086
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010087MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
malattia@linux.it33a04452007-04-09 19:26:03 +020088MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
Stelian Pop7f09c432007-01-13 23:04:31 +010089MODULE_LICENSE("GPL");
malattia@linux.it33a04452007-04-09 19:26:03 +020090MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
Stelian Pop7f09c432007-01-13 23:04:31 +010091
92static int debug;
93module_param(debug, int, 0);
94MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
Len Browna02d1c12007-02-07 15:34:02 -050095 "the development of this driver");
Stelian Pop7f09c432007-01-13 23:04:31 +010096
malattia@linux.it33a04452007-04-09 19:26:03 +020097static int no_spic; /* = 0 */
98module_param(no_spic, int, 0444);
99MODULE_PARM_DESC(no_spic,
100 "set this if you don't want to enable the SPIC device");
101
102static int compat; /* = 0 */
103module_param(compat, int, 0444);
104MODULE_PARM_DESC(compat,
malattia@linux.it7b153f32007-04-09 19:31:25 +0200105 "set this if you want to enable backward compatibility mode");
malattia@linux.it33a04452007-04-09 19:26:03 +0200106
107static unsigned long mask = 0xffffffff;
108module_param(mask, ulong, 0644);
109MODULE_PARM_DESC(mask,
110 "set this to the mask of event you want to enable (see doc)");
111
malattia@linux.it5f3d2892007-04-28 23:18:45 +0900112static int camera; /* = 0 */
113module_param(camera, int, 0444);
114MODULE_PARM_DESC(camera,
115 "set this to 1 to enable Motion Eye camera controls "
116 "(only use it if you have a C1VE or C1VN model)");
117
Mattia Dongilia64e62a2007-05-01 11:19:53 +0900118#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +0200119static int minor = -1;
120module_param(minor, int, 0);
121MODULE_PARM_DESC(minor,
122 "minor number of the misc device for the SPIC compatibility code, "
123 "default is -1 (automatic)");
124#endif
125
malattia@linux.it1549ee62007-04-09 10:19:08 +0200126/*********** Input Devices ***********/
127
128#define SONY_LAPTOP_BUF_SIZE 128
129struct sony_laptop_input_s {
130 atomic_t users;
131 struct input_dev *jog_dev;
132 struct input_dev *key_dev;
133 struct kfifo *fifo;
134 spinlock_t fifo_lock;
135 struct workqueue_struct *wq;
136};
137static struct sony_laptop_input_s sony_laptop_input = {
138 .users = ATOMIC_INIT(0),
139};
140
141struct sony_laptop_keypress {
142 struct input_dev *dev;
143 int key;
144};
145
Mattia Dongilibc57f862007-07-20 02:01:57 +0900146/* Correspondance table between sonypi events
147 * and input layer indexes in the keymap
148 */
149static int sony_laptop_input_index[] = {
Mattia Dongili3eb87492008-01-14 18:05:45 +0900150 -1, /* 0 no event */
151 -1, /* 1 SONYPI_EVENT_JOGDIAL_DOWN */
152 -1, /* 2 SONYPI_EVENT_JOGDIAL_UP */
153 -1, /* 3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
154 -1, /* 4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */
155 -1, /* 5 SONYPI_EVENT_JOGDIAL_PRESSED */
156 -1, /* 6 SONYPI_EVENT_JOGDIAL_RELEASED */
157 0, /* 7 SONYPI_EVENT_CAPTURE_PRESSED */
158 1, /* 8 SONYPI_EVENT_CAPTURE_RELEASED */
159 2, /* 9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
160 3, /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
161 4, /* 11 SONYPI_EVENT_FNKEY_ESC */
162 5, /* 12 SONYPI_EVENT_FNKEY_F1 */
163 6, /* 13 SONYPI_EVENT_FNKEY_F2 */
164 7, /* 14 SONYPI_EVENT_FNKEY_F3 */
165 8, /* 15 SONYPI_EVENT_FNKEY_F4 */
166 9, /* 16 SONYPI_EVENT_FNKEY_F5 */
167 10, /* 17 SONYPI_EVENT_FNKEY_F6 */
168 11, /* 18 SONYPI_EVENT_FNKEY_F7 */
169 12, /* 19 SONYPI_EVENT_FNKEY_F8 */
170 13, /* 20 SONYPI_EVENT_FNKEY_F9 */
171 14, /* 21 SONYPI_EVENT_FNKEY_F10 */
172 15, /* 22 SONYPI_EVENT_FNKEY_F11 */
173 16, /* 23 SONYPI_EVENT_FNKEY_F12 */
174 17, /* 24 SONYPI_EVENT_FNKEY_1 */
175 18, /* 25 SONYPI_EVENT_FNKEY_2 */
176 19, /* 26 SONYPI_EVENT_FNKEY_D */
177 20, /* 27 SONYPI_EVENT_FNKEY_E */
178 21, /* 28 SONYPI_EVENT_FNKEY_F */
179 22, /* 29 SONYPI_EVENT_FNKEY_S */
180 23, /* 30 SONYPI_EVENT_FNKEY_B */
181 24, /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */
182 25, /* 32 SONYPI_EVENT_PKEY_P1 */
183 26, /* 33 SONYPI_EVENT_PKEY_P2 */
184 27, /* 34 SONYPI_EVENT_PKEY_P3 */
185 28, /* 35 SONYPI_EVENT_BACK_PRESSED */
186 -1, /* 36 SONYPI_EVENT_LID_CLOSED */
187 -1, /* 37 SONYPI_EVENT_LID_OPENED */
188 29, /* 38 SONYPI_EVENT_BLUETOOTH_ON */
189 30, /* 39 SONYPI_EVENT_BLUETOOTH_OFF */
190 31, /* 40 SONYPI_EVENT_HELP_PRESSED */
191 32, /* 41 SONYPI_EVENT_FNKEY_ONLY */
192 33, /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
193 34, /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */
194 35, /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
195 36, /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
196 37, /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
197 38, /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */
198 39, /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
199 40, /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
200 41, /* 50 SONYPI_EVENT_ZOOM_PRESSED */
201 42, /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */
202 43, /* 52 SONYPI_EVENT_MEYE_FACE */
203 44, /* 53 SONYPI_EVENT_MEYE_OPPOSITE */
204 45, /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */
205 46, /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */
206 -1, /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */
207 -1, /* 57 SONYPI_EVENT_BATTERY_INSERT */
208 -1, /* 58 SONYPI_EVENT_BATTERY_REMOVE */
209 -1, /* 59 SONYPI_EVENT_FNKEY_RELEASED */
210 47, /* 60 SONYPI_EVENT_WIRELESS_ON */
211 48, /* 61 SONYPI_EVENT_WIRELESS_OFF */
212 49, /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */
213 50, /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Mattia Dongilibc57f862007-07-20 02:01:57 +0900214};
215
216static int sony_laptop_input_keycode_map[] = {
217 KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
218 KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
219 KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
220 KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
221 KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
222 KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
223 KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
224 KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
225 KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
226 KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
227 KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
228 KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
229 KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
230 KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
231 KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
232 KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
233 KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
234 KEY_FN_F1, /* 17 SONYPI_EVENT_FNKEY_1 */
235 KEY_FN_F2, /* 18 SONYPI_EVENT_FNKEY_2 */
236 KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
237 KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
238 KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
239 KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
240 KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
241 KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
242 KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
243 KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
244 KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
245 KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
246 KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
247 KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
248 KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
249 KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
250 KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
251 KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
252 KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
253 KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
254 KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
255 KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
256 KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
257 KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
258 KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
259 BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
260 KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
261 KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
262 KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
263 KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
264 KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
265 KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
Mattia Dongili3eb87492008-01-14 18:05:45 +0900266 KEY_ZOOMIN, /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
267 KEY_ZOOMOUT /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200268};
269
270/* release buttons after a short delay if pressed */
271static void do_sony_laptop_release_key(struct work_struct *work)
272{
273 struct sony_laptop_keypress kp;
274
275 while (kfifo_get(sony_laptop_input.fifo, (unsigned char *)&kp,
276 sizeof(kp)) == sizeof(kp)) {
277 msleep(10);
278 input_report_key(kp.dev, kp.key, 0);
279 input_sync(kp.dev);
280 }
281}
282static DECLARE_WORK(sony_laptop_release_key_work,
283 do_sony_laptop_release_key);
284
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200285/* forward event to the input subsystem */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200286static void sony_laptop_report_input_event(u8 event)
287{
288 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
289 struct input_dev *key_dev = sony_laptop_input.key_dev;
290 struct sony_laptop_keypress kp = { NULL };
malattia@linux.it1549ee62007-04-09 10:19:08 +0200291
292 if (event == SONYPI_EVENT_FNKEY_RELEASED) {
293 /* Nothing, not all VAIOs generate this event */
294 return;
295 }
296
297 /* report events */
298 switch (event) {
299 /* jog_dev events */
300 case SONYPI_EVENT_JOGDIAL_UP:
301 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
302 input_report_rel(jog_dev, REL_WHEEL, 1);
303 input_sync(jog_dev);
304 return;
305
306 case SONYPI_EVENT_JOGDIAL_DOWN:
307 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
308 input_report_rel(jog_dev, REL_WHEEL, -1);
309 input_sync(jog_dev);
310 return;
311
312 /* key_dev events */
313 case SONYPI_EVENT_JOGDIAL_PRESSED:
314 kp.key = BTN_MIDDLE;
315 kp.dev = jog_dev;
316 break;
317
318 default:
Adrian Bunkd399d132008-02-20 00:59:03 +0200319 if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
Mattia Dongilibc57f862007-07-20 02:01:57 +0900320 dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
321 break;
322 }
323 if (sony_laptop_input_index[event] != -1) {
324 kp.key = sony_laptop_input_keycode_map[sony_laptop_input_index[event]];
325 if (kp.key != KEY_UNKNOWN)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200326 kp.dev = key_dev;
Mattia Dongilibc57f862007-07-20 02:01:57 +0900327 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200328 break;
329 }
330
331 if (kp.dev) {
332 input_report_key(kp.dev, kp.key, 1);
Mattia Dongilibc57f862007-07-20 02:01:57 +0900333 /* we emit the scancode so we can always remap the key */
334 input_event(kp.dev, EV_MSC, MSC_SCAN, event);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200335 input_sync(kp.dev);
336 kfifo_put(sony_laptop_input.fifo,
337 (unsigned char *)&kp, sizeof(kp));
338
339 if (!work_pending(&sony_laptop_release_key_work))
340 queue_work(sony_laptop_input.wq,
341 &sony_laptop_release_key_work);
342 } else
343 dprintk("unknown input event %.2x\n", event);
344}
345
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500346static int sony_laptop_setup_input(struct acpi_device *acpi_device)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200347{
348 struct input_dev *jog_dev;
349 struct input_dev *key_dev;
350 int i;
351 int error;
352
353 /* don't run again if already initialized */
354 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
355 return 0;
356
357 /* kfifo */
358 spin_lock_init(&sony_laptop_input.fifo_lock);
359 sony_laptop_input.fifo =
360 kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
361 &sony_laptop_input.fifo_lock);
362 if (IS_ERR(sony_laptop_input.fifo)) {
363 printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
364 error = PTR_ERR(sony_laptop_input.fifo);
365 goto err_dec_users;
366 }
367
368 /* init workqueue */
369 sony_laptop_input.wq = create_singlethread_workqueue("sony-laptop");
370 if (!sony_laptop_input.wq) {
371 printk(KERN_ERR DRV_PFX
372 "Unabe to create workqueue.\n");
373 error = -ENXIO;
374 goto err_free_kfifo;
375 }
376
377 /* input keys */
378 key_dev = input_allocate_device();
379 if (!key_dev) {
380 error = -ENOMEM;
381 goto err_destroy_wq;
382 }
383
384 key_dev->name = "Sony Vaio Keys";
385 key_dev->id.bustype = BUS_ISA;
386 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500387 key_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200388
389 /* Initialize the Input Drivers: special keys */
Mattia Dongilibc57f862007-07-20 02:01:57 +0900390 set_bit(EV_KEY, key_dev->evbit);
391 set_bit(EV_MSC, key_dev->evbit);
392 set_bit(MSC_SCAN, key_dev->mscbit);
393 key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
394 key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
395 key_dev->keycode = &sony_laptop_input_keycode_map;
396 for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++) {
397 if (sony_laptop_input_keycode_map[i] != KEY_RESERVED) {
398 set_bit(sony_laptop_input_keycode_map[i],
399 key_dev->keybit);
400 }
401 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200402
403 error = input_register_device(key_dev);
404 if (error)
405 goto err_free_keydev;
406
407 sony_laptop_input.key_dev = key_dev;
408
409 /* jogdial */
410 jog_dev = input_allocate_device();
411 if (!jog_dev) {
412 error = -ENOMEM;
413 goto err_unregister_keydev;
414 }
415
416 jog_dev->name = "Sony Vaio Jogdial";
417 jog_dev->id.bustype = BUS_ISA;
418 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500419 key_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200420
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700421 jog_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
422 jog_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_MIDDLE);
423 jog_dev->relbit[0] = BIT_MASK(REL_WHEEL);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200424
425 error = input_register_device(jog_dev);
426 if (error)
427 goto err_free_jogdev;
428
429 sony_laptop_input.jog_dev = jog_dev;
430
431 return 0;
432
433err_free_jogdev:
434 input_free_device(jog_dev);
435
436err_unregister_keydev:
437 input_unregister_device(key_dev);
438 /* to avoid kref underflow below at input_free_device */
439 key_dev = NULL;
440
441err_free_keydev:
442 input_free_device(key_dev);
443
444err_destroy_wq:
445 destroy_workqueue(sony_laptop_input.wq);
446
447err_free_kfifo:
448 kfifo_free(sony_laptop_input.fifo);
449
450err_dec_users:
451 atomic_dec(&sony_laptop_input.users);
452 return error;
453}
454
455static void sony_laptop_remove_input(void)
456{
457 /* cleanup only after the last user has gone */
458 if (!atomic_dec_and_test(&sony_laptop_input.users))
459 return;
460
461 /* flush workqueue first */
462 flush_workqueue(sony_laptop_input.wq);
463
464 /* destroy input devs */
465 input_unregister_device(sony_laptop_input.key_dev);
466 sony_laptop_input.key_dev = NULL;
467
468 if (sony_laptop_input.jog_dev) {
469 input_unregister_device(sony_laptop_input.jog_dev);
470 sony_laptop_input.jog_dev = NULL;
471 }
472
473 destroy_workqueue(sony_laptop_input.wq);
474 kfifo_free(sony_laptop_input.fifo);
475}
476
malattia@linux.it56b87562007-04-09 10:19:05 +0200477/*********** Platform Device ***********/
478
479static atomic_t sony_pf_users = ATOMIC_INIT(0);
480static struct platform_driver sony_pf_driver = {
481 .driver = {
482 .name = "sony-laptop",
483 .owner = THIS_MODULE,
484 }
485};
486static struct platform_device *sony_pf_device;
487
488static int sony_pf_add(void)
489{
490 int ret = 0;
491
492 /* don't run again if already initialized */
493 if (atomic_add_return(1, &sony_pf_users) > 1)
494 return 0;
495
496 ret = platform_driver_register(&sony_pf_driver);
497 if (ret)
498 goto out;
499
500 sony_pf_device = platform_device_alloc("sony-laptop", -1);
501 if (!sony_pf_device) {
502 ret = -ENOMEM;
503 goto out_platform_registered;
504 }
505
506 ret = platform_device_add(sony_pf_device);
507 if (ret)
508 goto out_platform_alloced;
509
510 return 0;
511
512 out_platform_alloced:
513 platform_device_put(sony_pf_device);
514 sony_pf_device = NULL;
515 out_platform_registered:
516 platform_driver_unregister(&sony_pf_driver);
517 out:
518 atomic_dec(&sony_pf_users);
519 return ret;
520}
521
522static void sony_pf_remove(void)
523{
524 /* deregister only after the last user has gone */
525 if (!atomic_dec_and_test(&sony_pf_users))
526 return;
527
528 platform_device_del(sony_pf_device);
529 platform_device_put(sony_pf_device);
530 platform_driver_unregister(&sony_pf_driver);
531}
532
533/*********** SNC (SNY5001) Device ***********/
534
malattia@linux.it33a04452007-04-09 19:26:03 +0200535/* the device uses 1-based values, while the backlight subsystem uses
536 0-based values */
537#define SONY_MAX_BRIGHTNESS 8
538
539#define SNC_VALIDATE_IN 0
540#define SNC_VALIDATE_OUT 1
541
malattia@linux.it59b19102007-04-09 10:19:04 +0200542static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500543 char *);
malattia@linux.it59b19102007-04-09 10:19:04 +0200544static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500545 const char *, size_t);
Mattia Dongili156c2212007-02-12 22:01:07 +0100546static int boolean_validate(const int, const int);
547static int brightness_default_validate(const int, const int);
548
malattia@linux.it59b19102007-04-09 10:19:04 +0200549struct sony_nc_value {
Len Browna02d1c12007-02-07 15:34:02 -0500550 char *name; /* name of the entry */
551 char **acpiget; /* names of the ACPI get function */
552 char **acpiset; /* names of the ACPI set function */
Mattia Dongili156c2212007-02-12 22:01:07 +0100553 int (*validate)(const int, const int); /* input/output validation */
Len Browna02d1c12007-02-07 15:34:02 -0500554 int value; /* current setting */
555 int valid; /* Has ever been set */
556 int debug; /* active only in debug mode ? */
557 struct device_attribute devattr; /* sysfs atribute */
Stelian Pop7f09c432007-01-13 23:04:31 +0100558};
559
malattia@linux.it59b19102007-04-09 10:19:04 +0200560#define SNC_HANDLE_NAMES(_name, _values...) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100561 static char *snc_##_name[] = { _values, NULL }
562
malattia@linux.it59b19102007-04-09 10:19:04 +0200563#define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100564 { \
565 .name = __stringify(_name), \
566 .acpiget = _getters, \
567 .acpiset = _setters, \
Mattia Dongili156c2212007-02-12 22:01:07 +0100568 .validate = _validate, \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100569 .debug = _debug, \
malattia@linux.it59b19102007-04-09 10:19:04 +0200570 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100571 }
572
malattia@linux.it59b19102007-04-09 10:19:04 +0200573#define SNC_HANDLE_NULL { .name = NULL }
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100574
malattia@linux.it59b19102007-04-09 10:19:04 +0200575SNC_HANDLE_NAMES(fnkey_get, "GHKE");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100576
malattia@linux.it59b19102007-04-09 10:19:04 +0200577SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
578SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100579
malattia@linux.it59b19102007-04-09 10:19:04 +0200580SNC_HANDLE_NAMES(cdpower_get, "GCDP");
581SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100582
malattia@linux.it59b19102007-04-09 10:19:04 +0200583SNC_HANDLE_NAMES(audiopower_get, "GAZP");
584SNC_HANDLE_NAMES(audiopower_set, "AZPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100585
malattia@linux.it59b19102007-04-09 10:19:04 +0200586SNC_HANDLE_NAMES(lanpower_get, "GLNP");
587SNC_HANDLE_NAMES(lanpower_set, "LNPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100588
Mattia Dongili044847e2007-07-16 02:34:33 +0900589SNC_HANDLE_NAMES(lidstate_get, "GLID");
590
591SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
592SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
593
594SNC_HANDLE_NAMES(gainbass_get, "GMGB");
595SNC_HANDLE_NAMES(gainbass_set, "CMGB");
596
malattia@linux.it59b19102007-04-09 10:19:04 +0200597SNC_HANDLE_NAMES(PID_get, "GPID");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100598
malattia@linux.it59b19102007-04-09 10:19:04 +0200599SNC_HANDLE_NAMES(CTR_get, "GCTR");
600SNC_HANDLE_NAMES(CTR_set, "SCTR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100601
malattia@linux.it59b19102007-04-09 10:19:04 +0200602SNC_HANDLE_NAMES(PCR_get, "GPCR");
603SNC_HANDLE_NAMES(PCR_set, "SPCR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100604
malattia@linux.it59b19102007-04-09 10:19:04 +0200605SNC_HANDLE_NAMES(CMI_get, "GCMI");
606SNC_HANDLE_NAMES(CMI_set, "SCMI");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100607
malattia@linux.it59b19102007-04-09 10:19:04 +0200608static struct sony_nc_value sony_nc_values[] = {
609 SNC_HANDLE(brightness_default, snc_brightness_def_get,
Mattia Dongili156c2212007-02-12 22:01:07 +0100610 snc_brightness_def_set, brightness_default_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200611 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
612 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
613 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100614 boolean_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200615 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100616 boolean_validate, 1),
Mattia Dongili044847e2007-07-16 02:34:33 +0900617 SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
618 boolean_validate, 0),
619 SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
620 boolean_validate, 0),
621 SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
622 boolean_validate, 0),
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100623 /* unknown methods */
malattia@linux.it59b19102007-04-09 10:19:04 +0200624 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
625 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
626 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
627 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
628 SNC_HANDLE_NULL
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100629};
630
malattia@linux.it59b19102007-04-09 10:19:04 +0200631static acpi_handle sony_nc_acpi_handle;
632static struct acpi_device *sony_nc_acpi_device = NULL;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100633
Mattia Dongilid78865c2007-02-07 20:01:56 +0100634/*
635 * acpi_evaluate_object wrappers
636 */
Stelian Pop7f09c432007-01-13 23:04:31 +0100637static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
638{
639 struct acpi_buffer output;
640 union acpi_object out_obj;
641 acpi_status status;
642
643 output.length = sizeof(out_obj);
644 output.pointer = &out_obj;
645
646 status = acpi_evaluate_object(handle, name, NULL, &output);
647 if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
648 *result = out_obj.integer.value;
649 return 0;
650 }
651
malattia@linux.itf6119b02007-04-09 19:31:06 +0200652 printk(KERN_WARNING DRV_PFX "acpi_callreadfunc failed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100653
654 return -1;
655}
656
657static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
658 int *result)
659{
660 struct acpi_object_list params;
661 union acpi_object in_obj;
662 struct acpi_buffer output;
663 union acpi_object out_obj;
664 acpi_status status;
665
666 params.count = 1;
667 params.pointer = &in_obj;
668 in_obj.type = ACPI_TYPE_INTEGER;
669 in_obj.integer.value = value;
670
671 output.length = sizeof(out_obj);
672 output.pointer = &out_obj;
673
674 status = acpi_evaluate_object(handle, name, &params, &output);
675 if (status == AE_OK) {
676 if (result != NULL) {
677 if (out_obj.type != ACPI_TYPE_INTEGER) {
malattia@linux.itf6119b02007-04-09 19:31:06 +0200678 printk(KERN_WARNING DRV_PFX "acpi_evaluate_object bad "
Stelian Pop7f09c432007-01-13 23:04:31 +0100679 "return type\n");
680 return -1;
681 }
682 *result = out_obj.integer.value;
683 }
684 return 0;
685 }
686
malattia@linux.itf6119b02007-04-09 19:31:06 +0200687 printk(KERN_WARNING DRV_PFX "acpi_evaluate_object failed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100688
689 return -1;
690}
691
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900692static int sony_find_snc_handle(int handle)
693{
694 int i;
695 int result;
696
697 for (i = 0x20; i < 0x30; i++) {
698 acpi_callsetfunc(sony_nc_acpi_handle, "SN00", i, &result);
699 if (result == handle)
700 return i-0x20;
701 }
702
703 return -1;
704}
705
706static int sony_call_snc_handle(int handle, int argument, int *result)
707{
708 int offset = sony_find_snc_handle(handle);
709
710 if (offset < 0)
711 return -1;
712
713 return acpi_callsetfunc(sony_nc_acpi_handle, "SN07", offset | argument,
714 result);
715}
716
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100717/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200718 * sony_nc_values input/output validate functions
Mattia Dongili156c2212007-02-12 22:01:07 +0100719 */
720
721/* brightness_default_validate:
722 *
723 * manipulate input output values to keep consistency with the
724 * backlight framework for which brightness values are 0-based.
725 */
726static int brightness_default_validate(const int direction, const int value)
727{
728 switch (direction) {
729 case SNC_VALIDATE_OUT:
730 return value - 1;
731 case SNC_VALIDATE_IN:
732 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
733 return value + 1;
734 }
735 return -EINVAL;
736}
737
738/* boolean_validate:
739 *
740 * on input validate boolean values 0/1, on output just pass the
741 * received value.
742 */
743static int boolean_validate(const int direction, const int value)
744{
745 if (direction == SNC_VALIDATE_IN) {
746 if (value != 0 && value != 1)
747 return -EINVAL;
748 }
749 return value;
750}
751
752/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200753 * Sysfs show/store common to all sony_nc_values
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100754 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200755static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
Len Browna02d1c12007-02-07 15:34:02 -0500756 char *buffer)
Stelian Pop7f09c432007-01-13 23:04:31 +0100757{
Stelian Pop7f09c432007-01-13 23:04:31 +0100758 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200759 struct sony_nc_value *item =
760 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100761
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100762 if (!*item->acpiget)
Stelian Pop7f09c432007-01-13 23:04:31 +0100763 return -EIO;
764
malattia@linux.it59b19102007-04-09 10:19:04 +0200765 if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100766 return -EIO;
767
Mattia Dongili156c2212007-02-12 22:01:07 +0100768 if (item->validate)
769 value = item->validate(SNC_VALIDATE_OUT, value);
770
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100771 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
Stelian Pop7f09c432007-01-13 23:04:31 +0100772}
773
malattia@linux.it59b19102007-04-09 10:19:04 +0200774static ssize_t sony_nc_sysfs_store(struct device *dev,
Len Browna02d1c12007-02-07 15:34:02 -0500775 struct device_attribute *attr,
776 const char *buffer, size_t count)
Stelian Pop7f09c432007-01-13 23:04:31 +0100777{
Stelian Pop7f09c432007-01-13 23:04:31 +0100778 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200779 struct sony_nc_value *item =
780 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100781
782 if (!item->acpiset)
783 return -EIO;
784
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100785 if (count > 31)
786 return -EINVAL;
787
788 value = simple_strtoul(buffer, NULL, 10);
Stelian Pop7f09c432007-01-13 23:04:31 +0100789
Mattia Dongili156c2212007-02-12 22:01:07 +0100790 if (item->validate)
791 value = item->validate(SNC_VALIDATE_IN, value);
792
793 if (value < 0)
794 return value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100795
malattia@linux.it59b19102007-04-09 10:19:04 +0200796 if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100797 return -EIO;
Andrew Morton3f4f4612007-01-13 23:04:32 +0100798 item->value = value;
799 item->valid = 1;
Stelian Pop7f09c432007-01-13 23:04:31 +0100800 return count;
801}
802
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100803
Mattia Dongilid78865c2007-02-07 20:01:56 +0100804/*
805 * Backlight device
806 */
807static int sony_backlight_update_status(struct backlight_device *bd)
Andrew Morton3f4f4612007-01-13 23:04:32 +0100808{
malattia@linux.it59b19102007-04-09 10:19:04 +0200809 return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
Richard Purdie321709c2007-02-10 15:04:08 +0000810 bd->props.brightness + 1, NULL);
Andrew Morton3f4f4612007-01-13 23:04:32 +0100811}
812
Mattia Dongilid78865c2007-02-07 20:01:56 +0100813static int sony_backlight_get_brightness(struct backlight_device *bd)
814{
815 int value;
816
malattia@linux.it59b19102007-04-09 10:19:04 +0200817 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
Mattia Dongilid78865c2007-02-07 20:01:56 +0100818 return 0;
819 /* brightness levels are 1-based, while backlight ones are 0-based */
820 return value - 1;
821}
822
823static struct backlight_device *sony_backlight_device;
Richard Purdie321709c2007-02-10 15:04:08 +0000824static struct backlight_ops sony_backlight_ops = {
Len Browna02d1c12007-02-07 15:34:02 -0500825 .update_status = sony_backlight_update_status,
826 .get_brightness = sony_backlight_get_brightness,
Mattia Dongilid78865c2007-02-07 20:01:56 +0100827};
828
829/*
Mattia Dongili6315fd12007-07-16 02:34:35 +0900830 * New SNC-only Vaios event mapping to driver known keys
831 */
832struct sony_nc_event {
833 u8 data;
834 u8 event;
835};
836
Mattia Dongili6315fd12007-07-16 02:34:35 +0900837static struct sony_nc_event sony_C_events[] = {
838 { 0x81, SONYPI_EVENT_FNKEY_F1 },
839 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
840 { 0x85, SONYPI_EVENT_FNKEY_F5 },
841 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
842 { 0x86, SONYPI_EVENT_FNKEY_F6 },
843 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
844 { 0x87, SONYPI_EVENT_FNKEY_F7 },
845 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
846 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
847 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
848 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
849 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
850 { 0, 0 },
851};
852
Mattia Dongili6315fd12007-07-16 02:34:35 +0900853/*
Mattia Dongilid78865c2007-02-07 20:01:56 +0100854 * ACPI callbacks
855 */
Stelian Pop7f09c432007-01-13 23:04:31 +0100856static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
857{
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900858 int i;
Mattia Dongili6315fd12007-07-16 02:34:35 +0900859 u32 ev = event;
860 int result;
861
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900862 if (ev == 0x92 || ev == 0x90) {
863 int origev = ev;
Mattia Dongili6315fd12007-07-16 02:34:35 +0900864 /* read the key pressed from EC.GECR
865 * A call to SN07 with 0x0202 will do it as well respecting
866 * the current protocol on different OSes
867 *
868 * Note: the path for GECR may be
869 * \_SB.PCI0.LPCB.EC (C, FE, AR, N and friends)
870 * \_SB.PCI0.PIB.EC0 (VGN-FR notifications are sent directly, no GECR)
871 *
872 * TODO: we may want to do the same for the older GHKE -need
873 * dmi list- so this snippet may become one more callback.
874 */
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900875 if (sony_call_snc_handle(0x100, 0x200, &result))
Mattia Dongili6315fd12007-07-16 02:34:35 +0900876 dprintk("sony_acpi_notify, unable to decode event 0x%.2x\n", ev);
877 else
878 ev = result & 0xFF;
Mattia Dongili6315fd12007-07-16 02:34:35 +0900879
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900880 for (i = 0; sony_C_events[i].data; i++) {
881 if (sony_C_events[i].data == ev) {
882 ev = sony_C_events[i].event;
Mattia Dongili6315fd12007-07-16 02:34:35 +0900883 break;
884 }
885 }
886
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900887 if (!sony_C_events[i].data)
888 printk(KERN_INFO DRV_PFX "Unknown event: %x %x\n",
889 origev, ev);
890 }
891
Mattia Dongili6315fd12007-07-16 02:34:35 +0900892 dprintk("sony_acpi_notify, event: 0x%.2x\n", ev);
893 sony_laptop_report_input_event(ev);
Len Brown14e04fb2007-08-23 15:20:26 -0400894 acpi_bus_generate_proc_event(sony_nc_acpi_device, 1, ev);
Stelian Pop7f09c432007-01-13 23:04:31 +0100895}
896
897static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
898 void *context, void **return_value)
899{
Lin Ming30823732008-12-16 16:59:35 +0800900 struct acpi_device_info *info;
901 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Stelian Pop7f09c432007-01-13 23:04:31 +0100902
Lin Ming30823732008-12-16 16:59:35 +0800903 if (ACPI_SUCCESS(acpi_get_object_info(handle, &buffer))) {
904 info = buffer.pointer;
Stelian Pop7f09c432007-01-13 23:04:31 +0100905
Lin Ming30823732008-12-16 16:59:35 +0800906 printk(KERN_WARNING DRV_PFX "method: name: %4.4s, args %X\n",
907 (char *)&info->name, info->param_count);
908
909 kfree(buffer.pointer);
910 }
Stelian Pop7f09c432007-01-13 23:04:31 +0100911
912 return AE_OK;
913}
914
Mattia Dongilid78865c2007-02-07 20:01:56 +0100915/*
916 * ACPI device
917 */
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900918static int sony_nc_function_setup(struct acpi_device *device)
919{
920 int result;
921
922 /* Enable all events */
923 acpi_callsetfunc(sony_nc_acpi_handle, "SN02", 0xffff, &result);
924
925 /* Setup hotkeys */
926 sony_call_snc_handle(0x0100, 0, &result);
927 sony_call_snc_handle(0x0101, 0, &result);
928 sony_call_snc_handle(0x0102, 0x100, &result);
929
930 return 0;
931}
932
malattia@linux.it59b19102007-04-09 10:19:04 +0200933static int sony_nc_resume(struct acpi_device *device)
Mattia Dongilid78865c2007-02-07 20:01:56 +0100934{
malattia@linux.it59b19102007-04-09 10:19:04 +0200935 struct sony_nc_value *item;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900936 acpi_handle handle;
Mattia Dongilid78865c2007-02-07 20:01:56 +0100937
malattia@linux.it59b19102007-04-09 10:19:04 +0200938 for (item = sony_nc_values; item->name; item++) {
Mattia Dongilid78865c2007-02-07 20:01:56 +0100939 int ret;
940
941 if (!item->valid)
942 continue;
malattia@linux.it59b19102007-04-09 10:19:04 +0200943 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
Len Browna02d1c12007-02-07 15:34:02 -0500944 item->value, NULL);
Mattia Dongilid78865c2007-02-07 20:01:56 +0100945 if (ret < 0) {
Harvey Harrison6e574192008-04-29 00:59:20 -0700946 printk("%s: %d\n", __func__, ret);
Mattia Dongilid78865c2007-02-07 20:01:56 +0100947 break;
948 }
949 }
Mattia Dongili6315fd12007-07-16 02:34:35 +0900950
Matthew Garrett82734bf2009-03-26 21:58:13 +0900951 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
952 &handle))) {
953 if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
954 dprintk("ECON Method failed\n");
955 }
956
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900957 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
958 &handle))) {
959 dprintk("Doing SNC setup\n");
960 sony_nc_function_setup(device);
961 }
962
Mattia Dongilie84a02b2007-08-04 00:22:30 +0900963 /* set the last requested brightness level */
964 if (sony_backlight_device &&
965 !sony_backlight_update_status(sony_backlight_device))
Adam Jackson0ef9cff2008-10-16 15:15:35 -0400966 printk(KERN_WARNING DRV_PFX "unable to restore brightness level\n");
Mattia Dongilie84a02b2007-08-04 00:22:30 +0900967
Mattia Dongilid78865c2007-02-07 20:01:56 +0100968 return 0;
969}
970
malattia@linux.it59b19102007-04-09 10:19:04 +0200971static int sony_nc_add(struct acpi_device *device)
Stelian Pop7f09c432007-01-13 23:04:31 +0100972{
973 acpi_status status;
Andrew Morton8607c672007-03-06 02:29:42 -0800974 int result = 0;
Alessandro Guido50f62af2007-01-13 23:04:34 +0100975 acpi_handle handle;
malattia@linux.it56b87562007-04-09 10:19:05 +0200976 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +0100977
malattia@linux.itf6119b02007-04-09 19:31:06 +0200978 printk(KERN_INFO DRV_PFX "%s v%s.\n",
979 SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
980
malattia@linux.it59b19102007-04-09 10:19:04 +0200981 sony_nc_acpi_device = device;
malattia@linux.it33a04452007-04-09 19:26:03 +0200982 strcpy(acpi_device_class(device), "sony/hotkey");
Stelian Popc5611622007-01-13 23:04:37 +0100983
malattia@linux.it59b19102007-04-09 10:19:04 +0200984 sony_nc_acpi_handle = device->handle;
Stelian Pop7f09c432007-01-13 23:04:31 +0100985
Mattia Dongilib25b7322007-07-16 02:34:36 +0900986 /* read device status */
987 result = acpi_bus_get_status(device);
988 /* bail IFF the above call was successful and the device is not present */
989 if (!result && !device->status.present) {
990 dprintk("Device not present\n");
991 result = -ENODEV;
992 goto outwalk;
993 }
994
Stelian Pop7f09c432007-01-13 23:04:31 +0100995 if (debug) {
malattia@linux.it59b19102007-04-09 10:19:04 +0200996 status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_nc_acpi_handle,
Stelian Pop7f09c432007-01-13 23:04:31 +0100997 1, sony_walk_callback, NULL, NULL);
998 if (ACPI_FAILURE(status)) {
malattia@linux.itf6119b02007-04-09 19:31:06 +0200999 printk(KERN_WARNING DRV_PFX "unable to walk acpi resources\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001000 result = -ENODEV;
1001 goto outwalk;
1002 }
Stelian Popc5611622007-01-13 23:04:37 +01001003 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001004
Mattia Dongilib25b7322007-07-16 02:34:36 +09001005 /* try to _INI the device if such method exists (ACPI spec 3.0-6.5.1
1006 * should be respected as we already checked for the device presence above */
1007 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, METHOD_NAME__INI, &handle))) {
1008 dprintk("Invoking _INI\n");
1009 if (ACPI_FAILURE(acpi_evaluate_object(sony_nc_acpi_handle, METHOD_NAME__INI,
1010 NULL, NULL)))
1011 dprintk("_INI Method failed\n");
1012 }
1013
Matthew Garrett82734bf2009-03-26 21:58:13 +09001014 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1015 &handle))) {
1016 if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
1017 dprintk("ECON Method failed\n");
1018 }
1019
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001020 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1021 &handle))) {
1022 dprintk("Doing SNC setup\n");
1023 sony_nc_function_setup(device);
1024 }
1025
malattia@linux.it1549ee62007-04-09 10:19:08 +02001026 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05001027 result = sony_laptop_setup_input(device);
malattia@linux.it1549ee62007-04-09 10:19:08 +02001028 if (result) {
1029 printk(KERN_ERR DRV_PFX
1030 "Unabe to create input devices.\n");
1031 goto outwalk;
1032 }
1033
malattia@linux.it59b19102007-04-09 10:19:04 +02001034 status = acpi_install_notify_handler(sony_nc_acpi_handle,
Stelian Popc5611622007-01-13 23:04:37 +01001035 ACPI_DEVICE_NOTIFY,
Len Browna02d1c12007-02-07 15:34:02 -05001036 sony_acpi_notify, NULL);
Stelian Popc5611622007-01-13 23:04:37 +01001037 if (ACPI_FAILURE(status)) {
Mattia Dongilib25b7322007-07-16 02:34:36 +09001038 printk(KERN_WARNING DRV_PFX "unable to install notify handler (%u)\n", status);
Stelian Popc5611622007-01-13 23:04:37 +01001039 result = -ENODEV;
malattia@linux.it1549ee62007-04-09 10:19:08 +02001040 goto outinput;
Stelian Pop7f09c432007-01-13 23:04:31 +01001041 }
1042
Alessandro Guido38cfc142008-11-12 23:03:28 +01001043 if (acpi_video_backlight_support()) {
Alessandro Guido3fedd902008-11-12 23:13:35 +01001044 printk(KERN_INFO DRV_PFX "brightness ignored, must be "
Thomas Renninger540b8bb2008-08-01 17:38:02 +02001045 "controlled by ACPI video driver\n");
1046 } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
1047 &handle))) {
Alessandro Guido50f62af2007-01-13 23:04:34 +01001048 sony_backlight_device = backlight_device_register("sony", NULL,
Len Browna02d1c12007-02-07 15:34:02 -05001049 NULL,
Richard Purdie321709c2007-02-10 15:04:08 +00001050 &sony_backlight_ops);
Mattia Dongili7df03b82007-01-13 23:04:41 +01001051
Len Browna02d1c12007-02-07 15:34:02 -05001052 if (IS_ERR(sony_backlight_device)) {
malattia@linux.itf6119b02007-04-09 19:31:06 +02001053 printk(KERN_WARNING DRV_PFX "unable to register backlight device\n");
Mattia Dongili7df03b82007-01-13 23:04:41 +01001054 sony_backlight_device = NULL;
Richard Purdie321709c2007-02-10 15:04:08 +00001055 } else {
1056 sony_backlight_device->props.brightness =
Len Browna02d1c12007-02-07 15:34:02 -05001057 sony_backlight_get_brightness
1058 (sony_backlight_device);
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05001059 sony_backlight_device->props.max_brightness =
Richard Purdie321709c2007-02-10 15:04:08 +00001060 SONY_MAX_BRIGHTNESS - 1;
1061 }
1062
Alessandro Guido50f62af2007-01-13 23:04:34 +01001063 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001064
malattia@linux.it49a11de2007-04-09 19:28:56 +02001065 result = sony_pf_add();
1066 if (result)
Mattia Dongilied3aa4b2007-02-07 20:01:54 +01001067 goto outbacklight;
Stelian Pop7f09c432007-01-13 23:04:31 +01001068
malattia@linux.it56b87562007-04-09 10:19:05 +02001069 /* create sony_pf sysfs attributes related to the SNC device */
1070 for (item = sony_nc_values; item->name; ++item) {
1071
1072 if (!debug && item->debug)
1073 continue;
1074
1075 /* find the available acpiget as described in the DSDT */
1076 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
1077 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1078 *item->acpiget,
1079 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02001080 dprintk("Found %s getter: %s\n",
1081 item->name, *item->acpiget);
malattia@linux.it56b87562007-04-09 10:19:05 +02001082 item->devattr.attr.mode |= S_IRUGO;
1083 break;
1084 }
1085 }
1086
1087 /* find the available acpiset as described in the DSDT */
1088 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
1089 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1090 *item->acpiset,
1091 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02001092 dprintk("Found %s setter: %s\n",
1093 item->name, *item->acpiset);
malattia@linux.it56b87562007-04-09 10:19:05 +02001094 item->devattr.attr.mode |= S_IWUSR;
1095 break;
1096 }
1097 }
1098
1099 if (item->devattr.attr.mode != 0) {
1100 result =
1101 device_create_file(&sony_pf_device->dev,
1102 &item->devattr);
1103 if (result)
1104 goto out_sysfs;
1105 }
1106 }
1107
Stelian Pop7f09c432007-01-13 23:04:31 +01001108 return 0;
1109
malattia@linux.it56b87562007-04-09 10:19:05 +02001110 out_sysfs:
1111 for (item = sony_nc_values; item->name; ++item) {
1112 device_remove_file(&sony_pf_device->dev, &item->devattr);
1113 }
1114 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +02001115
Len Browna02d1c12007-02-07 15:34:02 -05001116 outbacklight:
Mattia Dongili7df03b82007-01-13 23:04:41 +01001117 if (sony_backlight_device)
1118 backlight_device_unregister(sony_backlight_device);
1119
malattia@linux.it59b19102007-04-09 10:19:04 +02001120 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
Stelian Popc5611622007-01-13 23:04:37 +01001121 ACPI_DEVICE_NOTIFY,
1122 sony_acpi_notify);
1123 if (ACPI_FAILURE(status))
malattia@linux.itf6119b02007-04-09 19:31:06 +02001124 printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
malattia@linux.it1549ee62007-04-09 10:19:08 +02001125
1126 outinput:
1127 sony_laptop_remove_input();
1128
Len Browna02d1c12007-02-07 15:34:02 -05001129 outwalk:
Stelian Pop7f09c432007-01-13 23:04:31 +01001130 return result;
1131}
1132
malattia@linux.it59b19102007-04-09 10:19:04 +02001133static int sony_nc_remove(struct acpi_device *device, int type)
Stelian Pop7f09c432007-01-13 23:04:31 +01001134{
1135 acpi_status status;
malattia@linux.it56b87562007-04-09 10:19:05 +02001136 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01001137
Alessandro Guido50f62af2007-01-13 23:04:34 +01001138 if (sony_backlight_device)
1139 backlight_device_unregister(sony_backlight_device);
1140
malattia@linux.it59b19102007-04-09 10:19:04 +02001141 sony_nc_acpi_device = NULL;
Stelian Popc5611622007-01-13 23:04:37 +01001142
malattia@linux.it59b19102007-04-09 10:19:04 +02001143 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
Stelian Popc5611622007-01-13 23:04:37 +01001144 ACPI_DEVICE_NOTIFY,
1145 sony_acpi_notify);
1146 if (ACPI_FAILURE(status))
malattia@linux.itf6119b02007-04-09 19:31:06 +02001147 printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001148
malattia@linux.it56b87562007-04-09 10:19:05 +02001149 for (item = sony_nc_values; item->name; ++item) {
1150 device_remove_file(&sony_pf_device->dev, &item->devattr);
1151 }
1152
1153 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +02001154 sony_laptop_remove_input();
malattia@linux.itf6119b02007-04-09 19:31:06 +02001155 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001156
1157 return 0;
1158}
1159
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001160static const struct acpi_device_id sony_device_ids[] = {
1161 {SONY_NC_HID, 0},
1162 {SONY_PIC_HID, 0},
1163 {"", 0},
1164};
1165MODULE_DEVICE_TABLE(acpi, sony_device_ids);
1166
1167static const struct acpi_device_id sony_nc_device_ids[] = {
1168 {SONY_NC_HID, 0},
1169 {"", 0},
1170};
1171
malattia@linux.it59b19102007-04-09 10:19:04 +02001172static struct acpi_driver sony_nc_driver = {
1173 .name = SONY_NC_DRIVER_NAME,
1174 .class = SONY_NC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001175 .ids = sony_nc_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02001176 .owner = THIS_MODULE,
Len Browna02d1c12007-02-07 15:34:02 -05001177 .ops = {
malattia@linux.it59b19102007-04-09 10:19:04 +02001178 .add = sony_nc_add,
1179 .remove = sony_nc_remove,
1180 .resume = sony_nc_resume,
Len Browna02d1c12007-02-07 15:34:02 -05001181 },
Andrew Morton3f4f4612007-01-13 23:04:32 +01001182};
1183
malattia@linux.it33a04452007-04-09 19:26:03 +02001184/*********** SPIC (SNY6001) Device ***********/
1185
1186#define SONYPI_DEVICE_TYPE1 0x00000001
1187#define SONYPI_DEVICE_TYPE2 0x00000002
1188#define SONYPI_DEVICE_TYPE3 0x00000004
Mattia Dongili3eb87492008-01-14 18:05:45 +09001189#define SONYPI_DEVICE_TYPE4 0x00000008
malattia@linux.it33a04452007-04-09 19:26:03 +02001190
Mattia Dongili22a17782007-07-16 02:34:39 +09001191#define SONYPI_TYPE1_OFFSET 0x04
1192#define SONYPI_TYPE2_OFFSET 0x12
1193#define SONYPI_TYPE3_OFFSET 0x12
Mattia Dongili3eb87492008-01-14 18:05:45 +09001194#define SONYPI_TYPE4_OFFSET 0x12
malattia@linux.it33a04452007-04-09 19:26:03 +02001195
malattia@linux.it33a04452007-04-09 19:26:03 +02001196struct sony_pic_ioport {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001197 struct acpi_resource_io io1;
1198 struct acpi_resource_io io2;
malattia@linux.it33a04452007-04-09 19:26:03 +02001199 struct list_head list;
1200};
1201
1202struct sony_pic_irq {
1203 struct acpi_resource_irq irq;
1204 struct list_head list;
1205};
1206
Mattia Dongilide920432008-01-14 18:05:43 +09001207struct sonypi_eventtypes {
1208 u8 data;
1209 unsigned long mask;
1210 struct sonypi_event *events;
1211};
1212
1213struct device_ctrl {
1214 int model;
Mattia Dongili3eb87492008-01-14 18:05:45 +09001215 int (*handle_irq)(const u8, const u8);
Mattia Dongilide920432008-01-14 18:05:43 +09001216 u16 evport_offset;
1217 u8 has_camera;
1218 u8 has_bluetooth;
1219 u8 has_wwan;
1220 struct sonypi_eventtypes *event_types;
1221};
1222
malattia@linux.it33a04452007-04-09 19:26:03 +02001223struct sony_pic_dev {
Mattia Dongilide920432008-01-14 18:05:43 +09001224 struct device_ctrl *control;
malattia@linux.it33a04452007-04-09 19:26:03 +02001225 struct acpi_device *acpi_dev;
1226 struct sony_pic_irq *cur_irq;
1227 struct sony_pic_ioport *cur_ioport;
1228 struct list_head interrupts;
1229 struct list_head ioports;
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001230 struct mutex lock;
Mattia Dongilide920432008-01-14 18:05:43 +09001231 u8 camera_power;
1232 u8 bluetooth_power;
1233 u8 wwan_power;
malattia@linux.it33a04452007-04-09 19:26:03 +02001234};
1235
1236static struct sony_pic_dev spic_dev = {
1237 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
1238 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
1239};
1240
1241/* Event masks */
1242#define SONYPI_JOGGER_MASK 0x00000001
1243#define SONYPI_CAPTURE_MASK 0x00000002
1244#define SONYPI_FNKEY_MASK 0x00000004
1245#define SONYPI_BLUETOOTH_MASK 0x00000008
1246#define SONYPI_PKEY_MASK 0x00000010
1247#define SONYPI_BACK_MASK 0x00000020
1248#define SONYPI_HELP_MASK 0x00000040
1249#define SONYPI_LID_MASK 0x00000080
1250#define SONYPI_ZOOM_MASK 0x00000100
1251#define SONYPI_THUMBPHRASE_MASK 0x00000200
1252#define SONYPI_MEYE_MASK 0x00000400
1253#define SONYPI_MEMORYSTICK_MASK 0x00000800
1254#define SONYPI_BATTERY_MASK 0x00001000
1255#define SONYPI_WIRELESS_MASK 0x00002000
1256
1257struct sonypi_event {
1258 u8 data;
1259 u8 event;
1260};
1261
1262/* The set of possible button release events */
1263static struct sonypi_event sonypi_releaseev[] = {
1264 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
1265 { 0, 0 }
1266};
1267
1268/* The set of possible jogger events */
1269static struct sonypi_event sonypi_joggerev[] = {
1270 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
1271 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
1272 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
1273 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
1274 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
1275 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
1276 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
1277 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
1278 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
1279 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
1280 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
1281 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
1282 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
1283 { 0, 0 }
1284};
1285
1286/* The set of possible capture button events */
1287static struct sonypi_event sonypi_captureev[] = {
1288 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
1289 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09001290 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02001291 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
1292 { 0, 0 }
1293};
1294
1295/* The set of possible fnkeys events */
1296static struct sonypi_event sonypi_fnkeyev[] = {
1297 { 0x10, SONYPI_EVENT_FNKEY_ESC },
1298 { 0x11, SONYPI_EVENT_FNKEY_F1 },
1299 { 0x12, SONYPI_EVENT_FNKEY_F2 },
1300 { 0x13, SONYPI_EVENT_FNKEY_F3 },
1301 { 0x14, SONYPI_EVENT_FNKEY_F4 },
1302 { 0x15, SONYPI_EVENT_FNKEY_F5 },
1303 { 0x16, SONYPI_EVENT_FNKEY_F6 },
1304 { 0x17, SONYPI_EVENT_FNKEY_F7 },
1305 { 0x18, SONYPI_EVENT_FNKEY_F8 },
1306 { 0x19, SONYPI_EVENT_FNKEY_F9 },
1307 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
1308 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
1309 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
1310 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
1311 { 0x21, SONYPI_EVENT_FNKEY_1 },
1312 { 0x22, SONYPI_EVENT_FNKEY_2 },
1313 { 0x31, SONYPI_EVENT_FNKEY_D },
1314 { 0x32, SONYPI_EVENT_FNKEY_E },
1315 { 0x33, SONYPI_EVENT_FNKEY_F },
1316 { 0x34, SONYPI_EVENT_FNKEY_S },
1317 { 0x35, SONYPI_EVENT_FNKEY_B },
1318 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
1319 { 0, 0 }
1320};
1321
1322/* The set of possible program key events */
1323static struct sonypi_event sonypi_pkeyev[] = {
1324 { 0x01, SONYPI_EVENT_PKEY_P1 },
1325 { 0x02, SONYPI_EVENT_PKEY_P2 },
1326 { 0x04, SONYPI_EVENT_PKEY_P3 },
malattia@linux.it33a04452007-04-09 19:26:03 +02001327 { 0, 0 }
1328};
1329
1330/* The set of possible bluetooth events */
1331static struct sonypi_event sonypi_blueev[] = {
1332 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
1333 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
1334 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
1335 { 0, 0 }
1336};
1337
1338/* The set of possible wireless events */
1339static struct sonypi_event sonypi_wlessev[] = {
1340 { 0x59, SONYPI_EVENT_WIRELESS_ON },
1341 { 0x5a, SONYPI_EVENT_WIRELESS_OFF },
1342 { 0, 0 }
1343};
1344
1345/* The set of possible back button events */
1346static struct sonypi_event sonypi_backev[] = {
1347 { 0x20, SONYPI_EVENT_BACK_PRESSED },
1348 { 0, 0 }
1349};
1350
1351/* The set of possible help button events */
1352static struct sonypi_event sonypi_helpev[] = {
1353 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
1354 { 0, 0 }
1355};
1356
1357
1358/* The set of possible lid events */
1359static struct sonypi_event sonypi_lidev[] = {
1360 { 0x51, SONYPI_EVENT_LID_CLOSED },
1361 { 0x50, SONYPI_EVENT_LID_OPENED },
1362 { 0, 0 }
1363};
1364
1365/* The set of possible zoom events */
1366static struct sonypi_event sonypi_zoomev[] = {
1367 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09001368 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
1369 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02001370 { 0, 0 }
1371};
1372
1373/* The set of possible thumbphrase events */
1374static struct sonypi_event sonypi_thumbphraseev[] = {
1375 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
1376 { 0, 0 }
1377};
1378
1379/* The set of possible motioneye camera events */
1380static struct sonypi_event sonypi_meyeev[] = {
1381 { 0x00, SONYPI_EVENT_MEYE_FACE },
1382 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
1383 { 0, 0 }
1384};
1385
1386/* The set of possible memorystick events */
1387static struct sonypi_event sonypi_memorystickev[] = {
1388 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
1389 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
1390 { 0, 0 }
1391};
1392
1393/* The set of possible battery events */
1394static struct sonypi_event sonypi_batteryev[] = {
1395 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
1396 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
1397 { 0, 0 }
1398};
1399
Mattia Dongilide920432008-01-14 18:05:43 +09001400static struct sonypi_eventtypes type1_events[] = {
1401 { 0, 0xffffffff, sonypi_releaseev },
1402 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
1403 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
1404 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
1405 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
1406 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1407 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1408 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
1409 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1410 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
1411 { 0 },
1412};
1413static struct sonypi_eventtypes type2_events[] = {
1414 { 0, 0xffffffff, sonypi_releaseev },
1415 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
1416 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
1417 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
1418 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1419 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1420 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
1421 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
1422 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
1423 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
1424 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
1425 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1426 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1427 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1428 { 0 },
1429};
1430static struct sonypi_eventtypes type3_events[] = {
1431 { 0, 0xffffffff, sonypi_releaseev },
1432 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1433 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
1434 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1435 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1436 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1437 { 0 },
malattia@linux.it33a04452007-04-09 19:26:03 +02001438};
Mattia Dongili3eb87492008-01-14 18:05:45 +09001439static struct sonypi_eventtypes type4_events[] = {
1440 { 0, 0xffffffff, sonypi_releaseev },
1441 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1442 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
1443 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1444 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1445 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
1446 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
1447 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
1448 { 0 },
Mattia Dongilide920432008-01-14 18:05:43 +09001449};
1450
Mattia Dongili3eb87492008-01-14 18:05:45 +09001451/* low level spic calls */
malattia@linux.it33a04452007-04-09 19:26:03 +02001452#define ITERATIONS_LONG 10000
1453#define ITERATIONS_SHORT 10
1454#define wait_on_command(command, iterations) { \
1455 unsigned int n = iterations; \
1456 while (--n && (command)) \
1457 udelay(1); \
1458 if (!n) \
1459 dprintk("command failed at %s : %s (line %d)\n", \
Harvey Harrison6e574192008-04-29 00:59:20 -07001460 __FILE__, __func__, __LINE__); \
malattia@linux.it33a04452007-04-09 19:26:03 +02001461}
1462
1463static u8 sony_pic_call1(u8 dev)
1464{
1465 u8 v1, v2;
1466
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001467 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02001468 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001469 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1470 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
1471 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09001472 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02001473 return v2;
1474}
1475
1476static u8 sony_pic_call2(u8 dev, u8 fn)
1477{
1478 u8 v1;
1479
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001480 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02001481 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001482 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1483 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02001484 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001485 outb(fn, spic_dev.cur_ioport->io1.minimum);
1486 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09001487 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02001488 return v1;
1489}
1490
malattia@linux.it49a11de2007-04-09 19:28:56 +02001491static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
1492{
1493 u8 v1;
1494
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001495 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1496 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1497 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1498 outb(fn, spic_dev.cur_ioport->io1.minimum);
1499 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1500 outb(v, spic_dev.cur_ioport->io1.minimum);
1501 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09001502 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
1503 dev, fn, v, v1);
malattia@linux.it49a11de2007-04-09 19:28:56 +02001504 return v1;
1505}
1506
Mattia Dongili3eb87492008-01-14 18:05:45 +09001507/*
1508 * minidrivers for SPIC models
1509 */
1510static int type4_handle_irq(const u8 data_mask, const u8 ev)
1511{
1512 /*
1513 * 0x31 could mean we have to take some extra action and wait for
1514 * the next irq for some Type4 models, it will generate a new
1515 * irq and we can read new data from the device:
1516 * - 0x5c and 0x5f requires 0xA0
1517 * - 0x61 requires 0xB3
1518 */
1519 if (data_mask == 0x31) {
1520 if (ev == 0x5c || ev == 0x5f)
1521 sony_pic_call1(0xA0);
1522 else if (ev == 0x61)
1523 sony_pic_call1(0xB3);
1524 return 0;
1525 }
1526 return 1;
1527}
1528
1529static struct device_ctrl spic_types[] = {
1530 {
1531 .model = SONYPI_DEVICE_TYPE1,
1532 .handle_irq = NULL,
1533 .evport_offset = SONYPI_TYPE1_OFFSET,
1534 .event_types = type1_events,
1535 },
1536 {
1537 .model = SONYPI_DEVICE_TYPE2,
1538 .handle_irq = NULL,
1539 .evport_offset = SONYPI_TYPE2_OFFSET,
1540 .event_types = type2_events,
1541 },
1542 {
1543 .model = SONYPI_DEVICE_TYPE3,
1544 .handle_irq = NULL,
1545 .evport_offset = SONYPI_TYPE3_OFFSET,
1546 .event_types = type3_events,
1547 },
1548 {
1549 .model = SONYPI_DEVICE_TYPE4,
1550 .handle_irq = type4_handle_irq,
1551 .evport_offset = SONYPI_TYPE4_OFFSET,
1552 .event_types = type4_events,
1553 },
1554};
1555
1556static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
1557{
1558 struct pci_dev *pcidev;
1559
1560 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1561 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
1562 if (pcidev) {
1563 dev->control = &spic_types[0];
1564 goto out;
1565 }
1566
1567 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1568 PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
1569 if (pcidev) {
1570 dev->control = &spic_types[2];
1571 goto out;
1572 }
1573
1574 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1575 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
1576 if (pcidev) {
1577 dev->control = &spic_types[3];
1578 goto out;
1579 }
1580
1581 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1582 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
1583 if (pcidev) {
1584 dev->control = &spic_types[3];
1585 goto out;
1586 }
1587
1588 /* default */
1589 dev->control = &spic_types[1];
1590
1591out:
1592 if (pcidev)
1593 pci_dev_put(pcidev);
1594
1595 printk(KERN_INFO DRV_PFX "detected Type%d model\n",
1596 dev->control->model == SONYPI_DEVICE_TYPE1 ? 1 :
1597 dev->control->model == SONYPI_DEVICE_TYPE2 ? 2 :
1598 dev->control->model == SONYPI_DEVICE_TYPE3 ? 3 : 4);
1599}
1600
malattia@linux.it49a11de2007-04-09 19:28:56 +02001601/* camera tests and poweron/poweroff */
1602#define SONYPI_CAMERA_PICTURE 5
malattia@linux.it49a11de2007-04-09 19:28:56 +02001603#define SONYPI_CAMERA_CONTROL 0x10
malattia@linux.ite3646322007-04-28 23:34:22 +09001604
1605#define SONYPI_CAMERA_BRIGHTNESS 0
1606#define SONYPI_CAMERA_CONTRAST 1
1607#define SONYPI_CAMERA_HUE 2
1608#define SONYPI_CAMERA_COLOR 3
1609#define SONYPI_CAMERA_SHARPNESS 4
1610
1611#define SONYPI_CAMERA_EXPOSURE_MASK 0xC
1612#define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
1613#define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
1614#define SONYPI_CAMERA_MUTE_MASK 0x40
1615
1616/* the rest don't need a loop until not 0xff */
1617#define SONYPI_CAMERA_AGC 6
1618#define SONYPI_CAMERA_AGC_MASK 0x30
1619#define SONYPI_CAMERA_SHUTTER_MASK 0x7
1620
1621#define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
1622#define SONYPI_CAMERA_CONTROL 0x10
1623
1624#define SONYPI_CAMERA_STATUS 7
1625#define SONYPI_CAMERA_STATUS_READY 0x2
1626#define SONYPI_CAMERA_STATUS_POSITION 0x4
1627
1628#define SONYPI_DIRECTION_BACKWARDS 0x4
1629
1630#define SONYPI_CAMERA_REVISION 8
1631#define SONYPI_CAMERA_ROMVERSION 9
malattia@linux.it49a11de2007-04-09 19:28:56 +02001632
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001633static int __sony_pic_camera_ready(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001634{
1635 u8 v;
1636
1637 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
1638 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
1639}
1640
malattia@linux.ite3646322007-04-28 23:34:22 +09001641static int __sony_pic_camera_off(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001642{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001643 if (!camera) {
1644 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1645 return -ENODEV;
1646 }
1647
malattia@linux.it49a11de2007-04-09 19:28:56 +02001648 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
1649 SONYPI_CAMERA_MUTE_MASK),
1650 ITERATIONS_SHORT);
1651
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001652 if (spic_dev.camera_power) {
1653 sony_pic_call2(0x91, 0);
1654 spic_dev.camera_power = 0;
1655 }
1656 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001657}
1658
malattia@linux.ite3646322007-04-28 23:34:22 +09001659static int __sony_pic_camera_on(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001660{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001661 int i, j, x;
1662
1663 if (!camera) {
1664 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1665 return -ENODEV;
1666 }
malattia@linux.it49a11de2007-04-09 19:28:56 +02001667
1668 if (spic_dev.camera_power)
malattia@linux.ite3646322007-04-28 23:34:22 +09001669 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001670
1671 for (j = 5; j > 0; j--) {
1672
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001673 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001674 msleep(10);
1675 sony_pic_call1(0x93);
1676
1677 for (i = 400; i > 0; i--) {
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001678 if (__sony_pic_camera_ready())
malattia@linux.it49a11de2007-04-09 19:28:56 +02001679 break;
1680 msleep(10);
1681 }
1682 if (i)
1683 break;
1684 }
1685
1686 if (j == 0) {
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001687 printk(KERN_WARNING DRV_PFX "failed to power on camera\n");
malattia@linux.ite3646322007-04-28 23:34:22 +09001688 return -ENODEV;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001689 }
1690
1691 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
1692 0x5a),
1693 ITERATIONS_SHORT);
1694
1695 spic_dev.camera_power = 1;
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001696 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001697}
1698
malattia@linux.ite3646322007-04-28 23:34:22 +09001699/* External camera command (exported to the motion eye v4l driver) */
1700int sony_pic_camera_command(int command, u8 value)
1701{
1702 if (!camera)
1703 return -EIO;
1704
1705 mutex_lock(&spic_dev.lock);
1706
1707 switch (command) {
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001708 case SONY_PIC_COMMAND_SETCAMERA:
malattia@linux.ite3646322007-04-28 23:34:22 +09001709 if (value)
1710 __sony_pic_camera_on();
1711 else
1712 __sony_pic_camera_off();
1713 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001714 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09001715 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
1716 ITERATIONS_SHORT);
1717 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001718 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
malattia@linux.ite3646322007-04-28 23:34:22 +09001719 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
1720 ITERATIONS_SHORT);
1721 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001722 case SONY_PIC_COMMAND_SETCAMERAHUE:
malattia@linux.ite3646322007-04-28 23:34:22 +09001723 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
1724 ITERATIONS_SHORT);
1725 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001726 case SONY_PIC_COMMAND_SETCAMERACOLOR:
malattia@linux.ite3646322007-04-28 23:34:22 +09001727 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
1728 ITERATIONS_SHORT);
1729 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001730 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09001731 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
1732 ITERATIONS_SHORT);
1733 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001734 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
malattia@linux.ite3646322007-04-28 23:34:22 +09001735 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
1736 ITERATIONS_SHORT);
1737 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001738 case SONY_PIC_COMMAND_SETCAMERAAGC:
malattia@linux.ite3646322007-04-28 23:34:22 +09001739 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
1740 ITERATIONS_SHORT);
1741 break;
1742 default:
1743 printk(KERN_ERR DRV_PFX "sony_pic_camera_command invalid: %d\n",
1744 command);
1745 break;
1746 }
1747 mutex_unlock(&spic_dev.lock);
1748 return 0;
1749}
1750EXPORT_SYMBOL(sony_pic_camera_command);
1751
malattia@linux.it9476cdf2007-04-28 23:21:42 +09001752/* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
1753static void sony_pic_set_wwanpower(u8 state)
1754{
1755 state = !!state;
1756 mutex_lock(&spic_dev.lock);
1757 if (spic_dev.wwan_power == state) {
1758 mutex_unlock(&spic_dev.lock);
1759 return;
1760 }
1761 sony_pic_call2(0xB0, state);
1762 spic_dev.wwan_power = state;
1763 mutex_unlock(&spic_dev.lock);
1764}
1765
1766static ssize_t sony_pic_wwanpower_store(struct device *dev,
1767 struct device_attribute *attr,
1768 const char *buffer, size_t count)
1769{
1770 unsigned long value;
1771 if (count > 31)
1772 return -EINVAL;
1773
1774 value = simple_strtoul(buffer, NULL, 10);
1775 sony_pic_set_wwanpower(value);
1776
1777 return count;
1778}
1779
1780static ssize_t sony_pic_wwanpower_show(struct device *dev,
1781 struct device_attribute *attr, char *buffer)
1782{
1783 ssize_t count;
1784 mutex_lock(&spic_dev.lock);
1785 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
1786 mutex_unlock(&spic_dev.lock);
1787 return count;
1788}
1789
malattia@linux.it49a11de2007-04-09 19:28:56 +02001790/* bluetooth subsystem power state */
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001791static void __sony_pic_set_bluetoothpower(u8 state)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001792{
1793 state = !!state;
1794 if (spic_dev.bluetooth_power == state)
1795 return;
1796 sony_pic_call2(0x96, state);
1797 sony_pic_call1(0x82);
1798 spic_dev.bluetooth_power = state;
1799}
1800
1801static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
1802 struct device_attribute *attr,
1803 const char *buffer, size_t count)
1804{
1805 unsigned long value;
1806 if (count > 31)
1807 return -EINVAL;
1808
1809 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001810 mutex_lock(&spic_dev.lock);
1811 __sony_pic_set_bluetoothpower(value);
1812 mutex_unlock(&spic_dev.lock);
malattia@linux.it49a11de2007-04-09 19:28:56 +02001813
1814 return count;
1815}
1816
1817static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
1818 struct device_attribute *attr, char *buffer)
1819{
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001820 ssize_t count = 0;
1821 mutex_lock(&spic_dev.lock);
1822 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
1823 mutex_unlock(&spic_dev.lock);
1824 return count;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001825}
1826
1827/* fan speed */
1828/* FAN0 information (reverse engineered from ACPI tables) */
1829#define SONY_PIC_FAN0_STATUS 0x93
malattia@linux.it7b153f32007-04-09 19:31:25 +02001830static int sony_pic_set_fanspeed(unsigned long value)
1831{
1832 return ec_write(SONY_PIC_FAN0_STATUS, value);
1833}
1834
1835static int sony_pic_get_fanspeed(u8 *value)
1836{
1837 return ec_read(SONY_PIC_FAN0_STATUS, value);
1838}
1839
malattia@linux.it49a11de2007-04-09 19:28:56 +02001840static ssize_t sony_pic_fanspeed_store(struct device *dev,
1841 struct device_attribute *attr,
1842 const char *buffer, size_t count)
1843{
1844 unsigned long value;
1845 if (count > 31)
1846 return -EINVAL;
1847
1848 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it7b153f32007-04-09 19:31:25 +02001849 if (sony_pic_set_fanspeed(value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02001850 return -EIO;
1851
1852 return count;
1853}
1854
1855static ssize_t sony_pic_fanspeed_show(struct device *dev,
1856 struct device_attribute *attr, char *buffer)
1857{
1858 u8 value = 0;
malattia@linux.it7b153f32007-04-09 19:31:25 +02001859 if (sony_pic_get_fanspeed(&value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02001860 return -EIO;
1861
1862 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
1863}
1864
1865#define SPIC_ATTR(_name, _mode) \
1866struct device_attribute spic_attr_##_name = __ATTR(_name, \
1867 _mode, sony_pic_## _name ##_show, \
1868 sony_pic_## _name ##_store)
1869
malattia@linux.it49a11de2007-04-09 19:28:56 +02001870static SPIC_ATTR(bluetoothpower, 0644);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09001871static SPIC_ATTR(wwanpower, 0644);
malattia@linux.it49a11de2007-04-09 19:28:56 +02001872static SPIC_ATTR(fanspeed, 0644);
1873
1874static struct attribute *spic_attributes[] = {
malattia@linux.it49a11de2007-04-09 19:28:56 +02001875 &spic_attr_bluetoothpower.attr,
malattia@linux.it9476cdf2007-04-28 23:21:42 +09001876 &spic_attr_wwanpower.attr,
malattia@linux.it49a11de2007-04-09 19:28:56 +02001877 &spic_attr_fanspeed.attr,
1878 NULL
1879};
1880
1881static struct attribute_group spic_attribute_group = {
1882 .attrs = spic_attributes
1883};
1884
malattia@linux.it7b153f32007-04-09 19:31:25 +02001885/******** SONYPI compatibility **********/
Mattia Dongilia64e62a2007-05-01 11:19:53 +09001886#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +02001887
1888/* battery / brightness / temperature addresses */
1889#define SONYPI_BAT_FLAGS 0x81
1890#define SONYPI_LCD_LIGHT 0x96
1891#define SONYPI_BAT1_PCTRM 0xa0
1892#define SONYPI_BAT1_LEFT 0xa2
1893#define SONYPI_BAT1_MAXRT 0xa4
1894#define SONYPI_BAT2_PCTRM 0xa8
1895#define SONYPI_BAT2_LEFT 0xaa
1896#define SONYPI_BAT2_MAXRT 0xac
1897#define SONYPI_BAT1_MAXTK 0xb0
1898#define SONYPI_BAT1_FULL 0xb2
1899#define SONYPI_BAT2_MAXTK 0xb8
1900#define SONYPI_BAT2_FULL 0xba
1901#define SONYPI_TEMP_STATUS 0xC1
1902
1903struct sonypi_compat_s {
1904 struct fasync_struct *fifo_async;
1905 struct kfifo *fifo;
1906 spinlock_t fifo_lock;
1907 wait_queue_head_t fifo_proc_list;
1908 atomic_t open_count;
1909};
1910static struct sonypi_compat_s sonypi_compat = {
1911 .open_count = ATOMIC_INIT(0),
1912};
1913
1914static int sonypi_misc_fasync(int fd, struct file *filp, int on)
1915{
1916 int retval;
1917
1918 retval = fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
1919 if (retval < 0)
1920 return retval;
1921 return 0;
1922}
1923
1924static int sonypi_misc_release(struct inode *inode, struct file *file)
1925{
malattia@linux.it7b153f32007-04-09 19:31:25 +02001926 atomic_dec(&sonypi_compat.open_count);
1927 return 0;
1928}
1929
1930static int sonypi_misc_open(struct inode *inode, struct file *file)
1931{
1932 /* Flush input queue on first open */
Arnd Bergmann0410e682008-05-20 19:16:45 +02001933 lock_kernel();
malattia@linux.it7b153f32007-04-09 19:31:25 +02001934 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
1935 kfifo_reset(sonypi_compat.fifo);
Arnd Bergmann0410e682008-05-20 19:16:45 +02001936 unlock_kernel();
malattia@linux.it7b153f32007-04-09 19:31:25 +02001937 return 0;
1938}
1939
1940static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
1941 size_t count, loff_t *pos)
1942{
1943 ssize_t ret;
1944 unsigned char c;
1945
1946 if ((kfifo_len(sonypi_compat.fifo) == 0) &&
1947 (file->f_flags & O_NONBLOCK))
1948 return -EAGAIN;
1949
1950 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
1951 kfifo_len(sonypi_compat.fifo) != 0);
1952 if (ret)
1953 return ret;
1954
1955 while (ret < count &&
1956 (kfifo_get(sonypi_compat.fifo, &c, sizeof(c)) == sizeof(c))) {
1957 if (put_user(c, buf++))
1958 return -EFAULT;
1959 ret++;
1960 }
1961
1962 if (ret > 0) {
1963 struct inode *inode = file->f_path.dentry->d_inode;
1964 inode->i_atime = current_fs_time(inode->i_sb);
1965 }
1966
1967 return ret;
1968}
1969
1970static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
1971{
1972 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
1973 if (kfifo_len(sonypi_compat.fifo))
1974 return POLLIN | POLLRDNORM;
1975 return 0;
1976}
1977
1978static int ec_read16(u8 addr, u16 *value)
1979{
1980 u8 val_lb, val_hb;
1981 if (ec_read(addr, &val_lb))
1982 return -1;
1983 if (ec_read(addr + 1, &val_hb))
1984 return -1;
1985 *value = val_lb | (val_hb << 8);
1986 return 0;
1987}
1988
1989static int sonypi_misc_ioctl(struct inode *ip, struct file *fp,
1990 unsigned int cmd, unsigned long arg)
1991{
1992 int ret = 0;
1993 void __user *argp = (void __user *)arg;
1994 u8 val8;
1995 u16 val16;
1996 int value;
1997
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001998 mutex_lock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02001999 switch (cmd) {
2000 case SONYPI_IOCGBRT:
2001 if (sony_backlight_device == NULL) {
2002 ret = -EIO;
2003 break;
2004 }
2005 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
2006 ret = -EIO;
2007 break;
2008 }
2009 val8 = ((value & 0xff) - 1) << 5;
2010 if (copy_to_user(argp, &val8, sizeof(val8)))
2011 ret = -EFAULT;
2012 break;
2013 case SONYPI_IOCSBRT:
2014 if (sony_backlight_device == NULL) {
2015 ret = -EIO;
2016 break;
2017 }
2018 if (copy_from_user(&val8, argp, sizeof(val8))) {
2019 ret = -EFAULT;
2020 break;
2021 }
2022 if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
2023 (val8 >> 5) + 1, NULL)) {
2024 ret = -EIO;
2025 break;
2026 }
2027 /* sync the backlight device status */
2028 sony_backlight_device->props.brightness =
2029 sony_backlight_get_brightness(sony_backlight_device);
2030 break;
2031 case SONYPI_IOCGBAT1CAP:
2032 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
2033 ret = -EIO;
2034 break;
2035 }
2036 if (copy_to_user(argp, &val16, sizeof(val16)))
2037 ret = -EFAULT;
2038 break;
2039 case SONYPI_IOCGBAT1REM:
2040 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
2041 ret = -EIO;
2042 break;
2043 }
2044 if (copy_to_user(argp, &val16, sizeof(val16)))
2045 ret = -EFAULT;
2046 break;
2047 case SONYPI_IOCGBAT2CAP:
2048 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
2049 ret = -EIO;
2050 break;
2051 }
2052 if (copy_to_user(argp, &val16, sizeof(val16)))
2053 ret = -EFAULT;
2054 break;
2055 case SONYPI_IOCGBAT2REM:
2056 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
2057 ret = -EIO;
2058 break;
2059 }
2060 if (copy_to_user(argp, &val16, sizeof(val16)))
2061 ret = -EFAULT;
2062 break;
2063 case SONYPI_IOCGBATFLAGS:
2064 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
2065 ret = -EIO;
2066 break;
2067 }
2068 val8 &= 0x07;
2069 if (copy_to_user(argp, &val8, sizeof(val8)))
2070 ret = -EFAULT;
2071 break;
2072 case SONYPI_IOCGBLUE:
2073 val8 = spic_dev.bluetooth_power;
2074 if (copy_to_user(argp, &val8, sizeof(val8)))
2075 ret = -EFAULT;
2076 break;
2077 case SONYPI_IOCSBLUE:
2078 if (copy_from_user(&val8, argp, sizeof(val8))) {
2079 ret = -EFAULT;
2080 break;
2081 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002082 __sony_pic_set_bluetoothpower(val8);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002083 break;
2084 /* FAN Controls */
2085 case SONYPI_IOCGFAN:
2086 if (sony_pic_get_fanspeed(&val8)) {
2087 ret = -EIO;
2088 break;
2089 }
2090 if (copy_to_user(argp, &val8, sizeof(val8)))
2091 ret = -EFAULT;
2092 break;
2093 case SONYPI_IOCSFAN:
2094 if (copy_from_user(&val8, argp, sizeof(val8))) {
2095 ret = -EFAULT;
2096 break;
2097 }
2098 if (sony_pic_set_fanspeed(val8))
2099 ret = -EIO;
2100 break;
2101 /* GET Temperature (useful under APM) */
2102 case SONYPI_IOCGTEMP:
2103 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
2104 ret = -EIO;
2105 break;
2106 }
2107 if (copy_to_user(argp, &val8, sizeof(val8)))
2108 ret = -EFAULT;
2109 break;
2110 default:
2111 ret = -EINVAL;
2112 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002113 mutex_unlock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002114 return ret;
2115}
2116
2117static const struct file_operations sonypi_misc_fops = {
2118 .owner = THIS_MODULE,
2119 .read = sonypi_misc_read,
2120 .poll = sonypi_misc_poll,
2121 .open = sonypi_misc_open,
2122 .release = sonypi_misc_release,
2123 .fasync = sonypi_misc_fasync,
2124 .ioctl = sonypi_misc_ioctl,
2125};
2126
2127static struct miscdevice sonypi_misc_device = {
2128 .minor = MISC_DYNAMIC_MINOR,
2129 .name = "sonypi",
2130 .fops = &sonypi_misc_fops,
2131};
2132
2133static void sonypi_compat_report_event(u8 event)
2134{
2135 kfifo_put(sonypi_compat.fifo, (unsigned char *)&event, sizeof(event));
2136 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
2137 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
2138}
2139
2140static int sonypi_compat_init(void)
2141{
2142 int error;
2143
2144 spin_lock_init(&sonypi_compat.fifo_lock);
2145 sonypi_compat.fifo = kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
2146 &sonypi_compat.fifo_lock);
2147 if (IS_ERR(sonypi_compat.fifo)) {
2148 printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
2149 return PTR_ERR(sonypi_compat.fifo);
2150 }
2151
2152 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002153
2154 if (minor != -1)
2155 sonypi_misc_device.minor = minor;
2156 error = misc_register(&sonypi_misc_device);
2157 if (error) {
2158 printk(KERN_ERR DRV_PFX "misc_register failed\n");
2159 goto err_free_kfifo;
2160 }
2161 if (minor == -1)
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002162 printk(KERN_INFO DRV_PFX "device allocated minor is %d\n",
malattia@linux.it7b153f32007-04-09 19:31:25 +02002163 sonypi_misc_device.minor);
2164
2165 return 0;
2166
2167err_free_kfifo:
2168 kfifo_free(sonypi_compat.fifo);
2169 return error;
2170}
2171
2172static void sonypi_compat_exit(void)
2173{
2174 misc_deregister(&sonypi_misc_device);
2175 kfifo_free(sonypi_compat.fifo);
2176}
2177#else
2178static int sonypi_compat_init(void) { return 0; }
2179static void sonypi_compat_exit(void) { }
2180static void sonypi_compat_report_event(u8 event) { }
Mattia Dongilia64e62a2007-05-01 11:19:53 +09002181#endif /* CONFIG_SONYPI_COMPAT */
malattia@linux.it7b153f32007-04-09 19:31:25 +02002182
malattia@linux.it1549ee62007-04-09 10:19:08 +02002183/*
malattia@linux.it33a04452007-04-09 19:26:03 +02002184 * ACPI callbacks
malattia@linux.it1549ee62007-04-09 10:19:08 +02002185 */
malattia@linux.it33a04452007-04-09 19:26:03 +02002186static acpi_status
2187sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
2188{
2189 u32 i;
2190 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
2191
2192 switch (resource->type) {
2193 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002194 {
2195 /* start IO enumeration */
2196 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
2197 if (!ioport)
2198 return AE_ERROR;
2199
2200 list_add(&ioport->list, &dev->ioports);
2201 return AE_OK;
2202 }
2203
malattia@linux.it33a04452007-04-09 19:26:03 +02002204 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002205 /* end IO enumeration */
malattia@linux.it33a04452007-04-09 19:26:03 +02002206 return AE_OK;
2207
2208 case ACPI_RESOURCE_TYPE_IRQ:
2209 {
2210 struct acpi_resource_irq *p = &resource->data.irq;
2211 struct sony_pic_irq *interrupt = NULL;
2212 if (!p || !p->interrupt_count) {
2213 /*
2214 * IRQ descriptors may have no IRQ# bits set,
2215 * particularly those those w/ _STA disabled
2216 */
2217 dprintk("Blank IRQ resource\n");
2218 return AE_OK;
2219 }
2220 for (i = 0; i < p->interrupt_count; i++) {
2221 if (!p->interrupts[i]) {
2222 printk(KERN_WARNING DRV_PFX
2223 "Invalid IRQ %d\n",
2224 p->interrupts[i]);
2225 continue;
2226 }
2227 interrupt = kzalloc(sizeof(*interrupt),
2228 GFP_KERNEL);
2229 if (!interrupt)
2230 return AE_ERROR;
2231
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002232 list_add(&interrupt->list, &dev->interrupts);
malattia@linux.it33a04452007-04-09 19:26:03 +02002233 interrupt->irq.triggering = p->triggering;
2234 interrupt->irq.polarity = p->polarity;
2235 interrupt->irq.sharable = p->sharable;
2236 interrupt->irq.interrupt_count = 1;
2237 interrupt->irq.interrupts[0] = p->interrupts[i];
2238 }
2239 return AE_OK;
2240 }
2241 case ACPI_RESOURCE_TYPE_IO:
2242 {
2243 struct acpi_resource_io *io = &resource->data.io;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002244 struct sony_pic_ioport *ioport =
2245 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
malattia@linux.it33a04452007-04-09 19:26:03 +02002246 if (!io) {
2247 dprintk("Blank IO resource\n");
2248 return AE_OK;
2249 }
2250
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002251 if (!ioport->io1.minimum) {
2252 memcpy(&ioport->io1, io, sizeof(*io));
2253 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
2254 ioport->io1.address_length);
2255 }
2256 else if (!ioport->io2.minimum) {
2257 memcpy(&ioport->io2, io, sizeof(*io));
2258 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
2259 ioport->io2.address_length);
2260 }
2261 else {
2262 printk(KERN_ERR DRV_PFX "Unknown SPIC Type, more than 2 IO Ports\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002263 return AE_ERROR;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002264 }
malattia@linux.it33a04452007-04-09 19:26:03 +02002265 return AE_OK;
2266 }
2267 default:
2268 dprintk("Resource %d isn't an IRQ nor an IO port\n",
2269 resource->type);
2270
2271 case ACPI_RESOURCE_TYPE_END_TAG:
2272 return AE_OK;
2273 }
2274 return AE_CTRL_TERMINATE;
2275}
2276
2277static int sony_pic_possible_resources(struct acpi_device *device)
2278{
2279 int result = 0;
2280 acpi_status status = AE_OK;
2281
2282 if (!device)
2283 return -EINVAL;
2284
2285 /* get device status */
2286 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
2287 dprintk("Evaluating _STA\n");
2288 result = acpi_bus_get_status(device);
2289 if (result) {
2290 printk(KERN_WARNING DRV_PFX "Unable to read status\n");
2291 goto end;
2292 }
2293
2294 if (!device->status.enabled)
2295 dprintk("Device disabled\n");
2296 else
2297 dprintk("Device enabled\n");
2298
2299 /*
2300 * Query and parse 'method'
2301 */
2302 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
2303 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
2304 sony_pic_read_possible_resource, &spic_dev);
2305 if (ACPI_FAILURE(status)) {
2306 printk(KERN_WARNING DRV_PFX
2307 "Failure evaluating %s\n",
2308 METHOD_NAME__PRS);
2309 result = -ENODEV;
2310 }
2311end:
2312 return result;
2313}
2314
2315/*
2316 * Disable the spic device by calling its _DIS method
2317 */
2318static int sony_pic_disable(struct acpi_device *device)
2319{
Matthew Garrett6158d3a2008-10-29 14:01:03 -07002320 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
2321 NULL);
2322
2323 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
malattia@linux.it33a04452007-04-09 19:26:03 +02002324 return -ENXIO;
2325
2326 dprintk("Device disabled\n");
2327 return 0;
2328}
2329
2330
2331/*
2332 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
2333 *
2334 * Call _SRS to set current resources
2335 */
2336static int sony_pic_enable(struct acpi_device *device,
2337 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
2338{
2339 acpi_status status;
2340 int result = 0;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002341 /* Type 1 resource layout is:
2342 * IO
2343 * IO
2344 * IRQNoFlags
2345 * End
2346 *
2347 * Type 2 and 3 resource layout is:
2348 * IO
2349 * IRQNoFlags
2350 * End
2351 */
malattia@linux.it33a04452007-04-09 19:26:03 +02002352 struct {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002353 struct acpi_resource res1;
2354 struct acpi_resource res2;
2355 struct acpi_resource res3;
2356 struct acpi_resource res4;
malattia@linux.it33a04452007-04-09 19:26:03 +02002357 } *resource;
2358 struct acpi_buffer buffer = { 0, NULL };
2359
2360 if (!ioport || !irq)
2361 return -EINVAL;
2362
2363 /* init acpi_buffer */
2364 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
2365 if (!resource)
2366 return -ENOMEM;
2367
2368 buffer.length = sizeof(*resource) + 1;
2369 buffer.pointer = resource;
2370
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002371 /* setup Type 1 resources */
Mattia Dongilide920432008-01-14 18:05:43 +09002372 if (spic_dev.control->model == SONYPI_DEVICE_TYPE1) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002373
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002374 /* setup io resources */
2375 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2376 resource->res1.length = sizeof(struct acpi_resource);
2377 memcpy(&resource->res1.data.io, &ioport->io1,
2378 sizeof(struct acpi_resource_io));
malattia@linux.it33a04452007-04-09 19:26:03 +02002379
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002380 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
2381 resource->res2.length = sizeof(struct acpi_resource);
2382 memcpy(&resource->res2.data.io, &ioport->io2,
2383 sizeof(struct acpi_resource_io));
2384
2385 /* setup irq resource */
2386 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
2387 resource->res3.length = sizeof(struct acpi_resource);
2388 memcpy(&resource->res3.data.irq, &irq->irq,
2389 sizeof(struct acpi_resource_irq));
2390 /* we requested a shared irq */
2391 resource->res3.data.irq.sharable = ACPI_SHARED;
2392
2393 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
2394
2395 }
2396 /* setup Type 2/3 resources */
2397 else {
2398 /* setup io resource */
2399 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2400 resource->res1.length = sizeof(struct acpi_resource);
2401 memcpy(&resource->res1.data.io, &ioport->io1,
2402 sizeof(struct acpi_resource_io));
2403
2404 /* setup irq resource */
2405 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
2406 resource->res2.length = sizeof(struct acpi_resource);
2407 memcpy(&resource->res2.data.irq, &irq->irq,
2408 sizeof(struct acpi_resource_irq));
2409 /* we requested a shared irq */
2410 resource->res2.data.irq.sharable = ACPI_SHARED;
2411
2412 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
2413 }
malattia@linux.it33a04452007-04-09 19:26:03 +02002414
2415 /* Attempt to set the resource */
2416 dprintk("Evaluating _SRS\n");
2417 status = acpi_set_current_resources(device->handle, &buffer);
2418
2419 /* check for total failure */
2420 if (ACPI_FAILURE(status)) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002421 printk(KERN_ERR DRV_PFX "Error evaluating _SRS\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002422 result = -ENODEV;
2423 goto end;
2424 }
2425
2426 /* Necessary device initializations calls (from sonypi) */
2427 sony_pic_call1(0x82);
2428 sony_pic_call2(0x81, 0xff);
2429 sony_pic_call1(compat ? 0x92 : 0x82);
2430
2431end:
2432 kfree(resource);
2433 return result;
2434}
2435
2436/*****************
2437 *
2438 * ISR: some event is available
2439 *
2440 *****************/
2441static irqreturn_t sony_pic_irq(int irq, void *dev_id)
2442{
2443 int i, j;
malattia@linux.it33a04452007-04-09 19:26:03 +02002444 u8 ev = 0;
2445 u8 data_mask = 0;
2446 u8 device_event = 0;
2447
2448 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
2449
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002450 ev = inb_p(dev->cur_ioport->io1.minimum);
2451 if (dev->cur_ioport->io2.minimum)
2452 data_mask = inb_p(dev->cur_ioport->io2.minimum);
2453 else
Mattia Dongilide920432008-01-14 18:05:43 +09002454 data_mask = inb_p(dev->cur_ioport->io1.minimum +
2455 dev->control->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02002456
Mattia Dongili22a17782007-07-16 02:34:39 +09002457 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
Mattia Dongilide920432008-01-14 18:05:43 +09002458 ev, data_mask, dev->cur_ioport->io1.minimum,
2459 dev->control->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02002460
2461 if (ev == 0x00 || ev == 0xff)
2462 return IRQ_HANDLED;
2463
Mattia Dongilide920432008-01-14 18:05:43 +09002464 for (i = 0; dev->control->event_types[i].mask; i++) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002465
Mattia Dongilide920432008-01-14 18:05:43 +09002466 if ((data_mask & dev->control->event_types[i].data) !=
2467 dev->control->event_types[i].data)
malattia@linux.it33a04452007-04-09 19:26:03 +02002468 continue;
2469
Mattia Dongilide920432008-01-14 18:05:43 +09002470 if (!(mask & dev->control->event_types[i].mask))
malattia@linux.it33a04452007-04-09 19:26:03 +02002471 continue;
2472
Mattia Dongilide920432008-01-14 18:05:43 +09002473 for (j = 0; dev->control->event_types[i].events[j].event; j++) {
2474 if (ev == dev->control->event_types[i].events[j].data) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002475 device_event =
Mattia Dongilide920432008-01-14 18:05:43 +09002476 dev->control->
2477 event_types[i].events[j].event;
malattia@linux.it33a04452007-04-09 19:26:03 +02002478 goto found;
2479 }
2480 }
2481 }
Mattia Dongili3eb87492008-01-14 18:05:45 +09002482 /* Still not able to decode the event try to pass
2483 * it over to the minidriver
2484 */
2485 if (dev->control->handle_irq &&
2486 dev->control->handle_irq(data_mask, ev) == 0)
2487 return IRQ_HANDLED;
2488
Mattia Dongilide920432008-01-14 18:05:43 +09002489 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
2490 ev, data_mask, dev->cur_ioport->io1.minimum,
2491 dev->control->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02002492 return IRQ_HANDLED;
2493
2494found:
malattia@linux.it1549ee62007-04-09 10:19:08 +02002495 sony_laptop_report_input_event(device_event);
Mattia Dongilide920432008-01-14 18:05:43 +09002496 acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002497 sonypi_compat_report_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02002498
2499 return IRQ_HANDLED;
2500}
2501
2502/*****************
2503 *
2504 * ACPI driver
2505 *
2506 *****************/
2507static int sony_pic_remove(struct acpi_device *device, int type)
2508{
2509 struct sony_pic_ioport *io, *tmp_io;
2510 struct sony_pic_irq *irq, *tmp_irq;
2511
2512 if (sony_pic_disable(device)) {
2513 printk(KERN_ERR DRV_PFX "Couldn't disable device.\n");
2514 return -ENXIO;
2515 }
2516
2517 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002518 release_region(spic_dev.cur_ioport->io1.minimum,
2519 spic_dev.cur_ioport->io1.address_length);
2520 if (spic_dev.cur_ioport->io2.minimum)
2521 release_region(spic_dev.cur_ioport->io2.minimum,
2522 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02002523
Mattia Dongili015a9162007-08-12 16:20:27 +09002524 sonypi_compat_exit();
2525
malattia@linux.it1549ee62007-04-09 10:19:08 +02002526 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02002527
malattia@linux.it49a11de2007-04-09 19:28:56 +02002528 /* pf attrs */
2529 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2530 sony_pf_remove();
2531
malattia@linux.it33a04452007-04-09 19:26:03 +02002532 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2533 list_del(&io->list);
2534 kfree(io);
2535 }
2536 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2537 list_del(&irq->list);
2538 kfree(irq);
2539 }
2540 spic_dev.cur_ioport = NULL;
2541 spic_dev.cur_irq = NULL;
2542
malattia@linux.itf6119b02007-04-09 19:31:06 +02002543 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002544 return 0;
2545}
2546
2547static int sony_pic_add(struct acpi_device *device)
2548{
2549 int result;
2550 struct sony_pic_ioport *io, *tmp_io;
2551 struct sony_pic_irq *irq, *tmp_irq;
2552
malattia@linux.itf6119b02007-04-09 19:31:06 +02002553 printk(KERN_INFO DRV_PFX "%s v%s.\n",
2554 SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.it33a04452007-04-09 19:26:03 +02002555
2556 spic_dev.acpi_dev = device;
2557 strcpy(acpi_device_class(device), "sony/hotkey");
Mattia Dongilide920432008-01-14 18:05:43 +09002558 sony_pic_detect_device_type(&spic_dev);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002559 mutex_init(&spic_dev.lock);
malattia@linux.it33a04452007-04-09 19:26:03 +02002560
2561 /* read _PRS resources */
2562 result = sony_pic_possible_resources(device);
2563 if (result) {
2564 printk(KERN_ERR DRV_PFX
2565 "Unabe to read possible resources.\n");
2566 goto err_free_resources;
2567 }
2568
2569 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05002570 result = sony_laptop_setup_input(device);
malattia@linux.it33a04452007-04-09 19:26:03 +02002571 if (result) {
2572 printk(KERN_ERR DRV_PFX
2573 "Unabe to create input devices.\n");
2574 goto err_free_resources;
2575 }
2576
Mattia Dongili015a9162007-08-12 16:20:27 +09002577 if (sonypi_compat_init())
2578 goto err_remove_input;
2579
malattia@linux.it33a04452007-04-09 19:26:03 +02002580 /* request io port */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002581 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
2582 if (request_region(io->io1.minimum, io->io1.address_length,
malattia@linux.it33a04452007-04-09 19:26:03 +02002583 "Sony Programable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002584 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
2585 io->io1.minimum, io->io1.maximum,
2586 io->io1.address_length);
2587 /* Type 1 have 2 ioports */
2588 if (io->io2.minimum) {
2589 if (request_region(io->io2.minimum,
2590 io->io2.address_length,
2591 "Sony Programable I/O Device")) {
2592 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
2593 io->io2.minimum, io->io2.maximum,
2594 io->io2.address_length);
2595 spic_dev.cur_ioport = io;
2596 break;
2597 }
2598 else {
2599 dprintk("Unable to get I/O port2: "
2600 "0x%.4x (0x%.4x) + 0x%.2x\n",
2601 io->io2.minimum, io->io2.maximum,
2602 io->io2.address_length);
2603 release_region(io->io1.minimum,
2604 io->io1.address_length);
2605 }
2606 }
2607 else {
2608 spic_dev.cur_ioport = io;
2609 break;
2610 }
malattia@linux.it33a04452007-04-09 19:26:03 +02002611 }
2612 }
2613 if (!spic_dev.cur_ioport) {
2614 printk(KERN_ERR DRV_PFX "Failed to request_region.\n");
2615 result = -ENODEV;
Mattia Dongili015a9162007-08-12 16:20:27 +09002616 goto err_remove_compat;
malattia@linux.it33a04452007-04-09 19:26:03 +02002617 }
2618
2619 /* request IRQ */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002620 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002621 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
2622 IRQF_SHARED, "sony-laptop", &spic_dev)) {
2623 dprintk("IRQ: %d - triggering: %d - "
2624 "polarity: %d - shr: %d\n",
2625 irq->irq.interrupts[0],
2626 irq->irq.triggering,
2627 irq->irq.polarity,
2628 irq->irq.sharable);
2629 spic_dev.cur_irq = irq;
2630 break;
2631 }
2632 }
2633 if (!spic_dev.cur_irq) {
2634 printk(KERN_ERR DRV_PFX "Failed to request_irq.\n");
2635 result = -ENODEV;
2636 goto err_release_region;
2637 }
2638
2639 /* set resource status _SRS */
2640 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2641 if (result) {
2642 printk(KERN_ERR DRV_PFX "Couldn't enable device.\n");
2643 goto err_free_irq;
2644 }
2645
malattia@linux.it49a11de2007-04-09 19:28:56 +02002646 spic_dev.bluetooth_power = -1;
2647 /* create device attributes */
2648 result = sony_pf_add();
2649 if (result)
2650 goto err_disable_device;
2651
2652 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2653 if (result)
2654 goto err_remove_pf;
2655
malattia@linux.it33a04452007-04-09 19:26:03 +02002656 return 0;
2657
malattia@linux.it49a11de2007-04-09 19:28:56 +02002658err_remove_pf:
2659 sony_pf_remove();
2660
2661err_disable_device:
2662 sony_pic_disable(device);
2663
malattia@linux.it33a04452007-04-09 19:26:03 +02002664err_free_irq:
2665 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2666
2667err_release_region:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002668 release_region(spic_dev.cur_ioport->io1.minimum,
2669 spic_dev.cur_ioport->io1.address_length);
2670 if (spic_dev.cur_ioport->io2.minimum)
2671 release_region(spic_dev.cur_ioport->io2.minimum,
2672 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02002673
Mattia Dongili015a9162007-08-12 16:20:27 +09002674err_remove_compat:
2675 sonypi_compat_exit();
2676
malattia@linux.it33a04452007-04-09 19:26:03 +02002677err_remove_input:
malattia@linux.it1549ee62007-04-09 10:19:08 +02002678 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02002679
2680err_free_resources:
2681 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2682 list_del(&io->list);
2683 kfree(io);
2684 }
2685 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2686 list_del(&irq->list);
2687 kfree(irq);
2688 }
2689 spic_dev.cur_ioport = NULL;
2690 spic_dev.cur_irq = NULL;
2691
2692 return result;
2693}
2694
2695static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
2696{
2697 if (sony_pic_disable(device))
2698 return -ENXIO;
2699 return 0;
2700}
2701
2702static int sony_pic_resume(struct acpi_device *device)
2703{
2704 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2705 return 0;
2706}
2707
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002708static const struct acpi_device_id sony_pic_device_ids[] = {
2709 {SONY_PIC_HID, 0},
2710 {"", 0},
2711};
2712
malattia@linux.it33a04452007-04-09 19:26:03 +02002713static struct acpi_driver sony_pic_driver = {
2714 .name = SONY_PIC_DRIVER_NAME,
2715 .class = SONY_PIC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002716 .ids = sony_pic_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02002717 .owner = THIS_MODULE,
2718 .ops = {
2719 .add = sony_pic_add,
2720 .remove = sony_pic_remove,
2721 .suspend = sony_pic_suspend,
2722 .resume = sony_pic_resume,
2723 },
2724};
2725
2726static struct dmi_system_id __initdata sonypi_dmi_table[] = {
2727 {
2728 .ident = "Sony Vaio",
2729 .matches = {
2730 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2731 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
2732 },
2733 },
2734 {
2735 .ident = "Sony Vaio",
2736 .matches = {
2737 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2738 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
2739 },
2740 },
2741 { }
2742};
2743
malattia@linux.it59b19102007-04-09 10:19:04 +02002744static int __init sony_laptop_init(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01002745{
malattia@linux.it33a04452007-04-09 19:26:03 +02002746 int result;
2747
2748 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
2749 result = acpi_bus_register_driver(&sony_pic_driver);
2750 if (result) {
2751 printk(KERN_ERR DRV_PFX
2752 "Unable to register SPIC driver.");
2753 goto out;
2754 }
2755 }
2756
2757 result = acpi_bus_register_driver(&sony_nc_driver);
2758 if (result) {
2759 printk(KERN_ERR DRV_PFX "Unable to register SNC driver.");
2760 goto out_unregister_pic;
2761 }
2762
2763 return 0;
2764
2765out_unregister_pic:
2766 if (!no_spic)
2767 acpi_bus_unregister_driver(&sony_pic_driver);
2768out:
2769 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +01002770}
2771
malattia@linux.it59b19102007-04-09 10:19:04 +02002772static void __exit sony_laptop_exit(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01002773{
malattia@linux.it59b19102007-04-09 10:19:04 +02002774 acpi_bus_unregister_driver(&sony_nc_driver);
malattia@linux.it33a04452007-04-09 19:26:03 +02002775 if (!no_spic)
2776 acpi_bus_unregister_driver(&sony_pic_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +01002777}
2778
malattia@linux.it59b19102007-04-09 10:19:04 +02002779module_init(sony_laptop_init);
2780module_exit(sony_laptop_exit);