blob: 571b211608d1a7224575d99fe3ca5e27cccd03a2 [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 Garskidb9551702007-10-19 23:22:11 +020017 * Copyright (C) 2001-2002 AlcĂ´ve <www.alcove.com>
malattia@linux.it33a04452007-04-09 19:26:03 +020018 *
19 * Copyright (C) 2001 Michael Ashley <m.ashley@unsw.edu.au>
20 *
21 * Copyright (C) 2001 Junichi Morita <jun1m@mars.dti.ne.jp>
22 *
23 * Copyright (C) 2000 Takaya Kinjo <t-kinjo@tc4.so-net.ne.jp>
24 *
25 * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
26 *
27 * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
28 *
Stelian Pop7f09c432007-01-13 23:04:31 +010029 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
33 *
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
38 *
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
42 *
43 */
44
45#include <linux/kernel.h>
46#include <linux/module.h>
47#include <linux/moduleparam.h>
48#include <linux/init.h>
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
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100692/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200693 * sony_nc_values input/output validate functions
Mattia Dongili156c2212007-02-12 22:01:07 +0100694 */
695
696/* brightness_default_validate:
697 *
698 * manipulate input output values to keep consistency with the
699 * backlight framework for which brightness values are 0-based.
700 */
701static int brightness_default_validate(const int direction, const int value)
702{
703 switch (direction) {
704 case SNC_VALIDATE_OUT:
705 return value - 1;
706 case SNC_VALIDATE_IN:
707 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
708 return value + 1;
709 }
710 return -EINVAL;
711}
712
713/* boolean_validate:
714 *
715 * on input validate boolean values 0/1, on output just pass the
716 * received value.
717 */
718static int boolean_validate(const int direction, const int value)
719{
720 if (direction == SNC_VALIDATE_IN) {
721 if (value != 0 && value != 1)
722 return -EINVAL;
723 }
724 return value;
725}
726
727/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200728 * Sysfs show/store common to all sony_nc_values
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100729 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200730static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
Len Browna02d1c12007-02-07 15:34:02 -0500731 char *buffer)
Stelian Pop7f09c432007-01-13 23:04:31 +0100732{
Stelian Pop7f09c432007-01-13 23:04:31 +0100733 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200734 struct sony_nc_value *item =
735 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100736
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100737 if (!*item->acpiget)
Stelian Pop7f09c432007-01-13 23:04:31 +0100738 return -EIO;
739
malattia@linux.it59b19102007-04-09 10:19:04 +0200740 if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100741 return -EIO;
742
Mattia Dongili156c2212007-02-12 22:01:07 +0100743 if (item->validate)
744 value = item->validate(SNC_VALIDATE_OUT, value);
745
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100746 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
Stelian Pop7f09c432007-01-13 23:04:31 +0100747}
748
malattia@linux.it59b19102007-04-09 10:19:04 +0200749static ssize_t sony_nc_sysfs_store(struct device *dev,
Len Browna02d1c12007-02-07 15:34:02 -0500750 struct device_attribute *attr,
751 const char *buffer, size_t count)
Stelian Pop7f09c432007-01-13 23:04:31 +0100752{
Stelian Pop7f09c432007-01-13 23:04:31 +0100753 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200754 struct sony_nc_value *item =
755 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100756
757 if (!item->acpiset)
758 return -EIO;
759
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100760 if (count > 31)
761 return -EINVAL;
762
763 value = simple_strtoul(buffer, NULL, 10);
Stelian Pop7f09c432007-01-13 23:04:31 +0100764
Mattia Dongili156c2212007-02-12 22:01:07 +0100765 if (item->validate)
766 value = item->validate(SNC_VALIDATE_IN, value);
767
768 if (value < 0)
769 return value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100770
malattia@linux.it59b19102007-04-09 10:19:04 +0200771 if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100772 return -EIO;
Andrew Morton3f4f4612007-01-13 23:04:32 +0100773 item->value = value;
774 item->valid = 1;
Stelian Pop7f09c432007-01-13 23:04:31 +0100775 return count;
776}
777
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100778
Mattia Dongilid78865c2007-02-07 20:01:56 +0100779/*
780 * Backlight device
781 */
782static int sony_backlight_update_status(struct backlight_device *bd)
Andrew Morton3f4f4612007-01-13 23:04:32 +0100783{
malattia@linux.it59b19102007-04-09 10:19:04 +0200784 return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
Richard Purdie321709c2007-02-10 15:04:08 +0000785 bd->props.brightness + 1, NULL);
Andrew Morton3f4f4612007-01-13 23:04:32 +0100786}
787
Mattia Dongilid78865c2007-02-07 20:01:56 +0100788static int sony_backlight_get_brightness(struct backlight_device *bd)
789{
790 int value;
791
malattia@linux.it59b19102007-04-09 10:19:04 +0200792 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
Mattia Dongilid78865c2007-02-07 20:01:56 +0100793 return 0;
794 /* brightness levels are 1-based, while backlight ones are 0-based */
795 return value - 1;
796}
797
798static struct backlight_device *sony_backlight_device;
Richard Purdie321709c2007-02-10 15:04:08 +0000799static struct backlight_ops sony_backlight_ops = {
Len Browna02d1c12007-02-07 15:34:02 -0500800 .update_status = sony_backlight_update_status,
801 .get_brightness = sony_backlight_get_brightness,
Mattia Dongilid78865c2007-02-07 20:01:56 +0100802};
803
804/*
Mattia Dongili6315fd12007-07-16 02:34:35 +0900805 * New SNC-only Vaios event mapping to driver known keys
806 */
807struct sony_nc_event {
808 u8 data;
809 u8 event;
810};
811
812static struct sony_nc_event *sony_nc_events;
813
814/* Vaio C* --maybe also FE*, N* and AR* ?-- special init sequence
815 * for Fn keys
816 */
Jeff Garzik18552562007-10-03 15:15:40 -0400817static int sony_nc_C_enable(const struct dmi_system_id *id)
Mattia Dongili6315fd12007-07-16 02:34:35 +0900818{
819 int result = 0;
820
821 printk(KERN_NOTICE DRV_PFX "detected %s\n", id->ident);
822
823 sony_nc_events = id->driver_data;
824
825 if (acpi_callsetfunc(sony_nc_acpi_handle, "SN02", 0x4, &result) < 0
826 || acpi_callsetfunc(sony_nc_acpi_handle, "SN07", 0x2, &result) < 0
827 || acpi_callsetfunc(sony_nc_acpi_handle, "SN02", 0x10, &result) < 0
828 || acpi_callsetfunc(sony_nc_acpi_handle, "SN07", 0x0, &result) < 0
829 || acpi_callsetfunc(sony_nc_acpi_handle, "SN03", 0x2, &result) < 0
830 || acpi_callsetfunc(sony_nc_acpi_handle, "SN07", 0x101, &result) < 0) {
831 printk(KERN_WARNING DRV_PFX "failed to initialize SNC, some "
832 "functionalities may be missing\n");
833 return 1;
834 }
835 return 0;
836}
837
838static struct sony_nc_event sony_C_events[] = {
839 { 0x81, SONYPI_EVENT_FNKEY_F1 },
840 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
841 { 0x85, SONYPI_EVENT_FNKEY_F5 },
842 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
843 { 0x86, SONYPI_EVENT_FNKEY_F6 },
844 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
845 { 0x87, SONYPI_EVENT_FNKEY_F7 },
846 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
847 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
848 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
849 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
850 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
851 { 0, 0 },
852};
853
854/* SNC-only model map */
Jeff Garzik18552562007-10-03 15:15:40 -0400855static const struct dmi_system_id sony_nc_ids[] = {
Mattia Dongili6315fd12007-07-16 02:34:35 +0900856 {
Mattia Dongili89892d12007-07-16 02:34:38 +0900857 .ident = "Sony Vaio FE Series",
858 .callback = sony_nc_C_enable,
859 .driver_data = sony_C_events,
860 .matches = {
861 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
862 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FE"),
863 },
864 },
865 {
Mattia Dongilif46d1602007-08-12 16:20:26 +0900866 .ident = "Sony Vaio FZ Series",
867 .callback = sony_nc_C_enable,
868 .driver_data = sony_C_events,
869 .matches = {
870 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
871 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FZ"),
872 },
873 },
874 {
Mattia Dongili6315fd12007-07-16 02:34:35 +0900875 .ident = "Sony Vaio C Series",
876 .callback = sony_nc_C_enable,
877 .driver_data = sony_C_events,
878 .matches = {
879 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
880 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-C"),
881 },
882 },
Mattia Dongili88877c22008-01-14 18:05:42 +0900883 {
884 .ident = "Sony Vaio N Series",
885 .callback = sony_nc_C_enable,
886 .driver_data = sony_C_events,
887 .matches = {
888 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
889 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-N"),
890 },
891 },
Mattia Dongili6315fd12007-07-16 02:34:35 +0900892 { }
893};
894
895/*
Mattia Dongilid78865c2007-02-07 20:01:56 +0100896 * ACPI callbacks
897 */
Stelian Pop7f09c432007-01-13 23:04:31 +0100898static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
899{
Mattia Dongili6315fd12007-07-16 02:34:35 +0900900 struct sony_nc_event *evmap;
901 u32 ev = event;
902 int result;
903
904 if (ev == 0x92) {
905 /* read the key pressed from EC.GECR
906 * A call to SN07 with 0x0202 will do it as well respecting
907 * the current protocol on different OSes
908 *
909 * Note: the path for GECR may be
910 * \_SB.PCI0.LPCB.EC (C, FE, AR, N and friends)
911 * \_SB.PCI0.PIB.EC0 (VGN-FR notifications are sent directly, no GECR)
912 *
913 * TODO: we may want to do the same for the older GHKE -need
914 * dmi list- so this snippet may become one more callback.
915 */
916 if (acpi_callsetfunc(handle, "SN07", 0x0202, &result) < 0)
917 dprintk("sony_acpi_notify, unable to decode event 0x%.2x\n", ev);
918 else
919 ev = result & 0xFF;
920 }
921
922 if (sony_nc_events)
923 for (evmap = sony_nc_events; evmap->event; evmap++) {
924 if (evmap->data == ev) {
925 ev = evmap->event;
926 break;
927 }
928 }
929
930 dprintk("sony_acpi_notify, event: 0x%.2x\n", ev);
931 sony_laptop_report_input_event(ev);
Len Brown14e04fb2007-08-23 15:20:26 -0400932 acpi_bus_generate_proc_event(sony_nc_acpi_device, 1, ev);
Stelian Pop7f09c432007-01-13 23:04:31 +0100933}
934
935static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
936 void *context, void **return_value)
937{
938 struct acpi_namespace_node *node;
939 union acpi_operand_object *operand;
940
Len Browna02d1c12007-02-07 15:34:02 -0500941 node = (struct acpi_namespace_node *)handle;
942 operand = (union acpi_operand_object *)node->object;
Stelian Pop7f09c432007-01-13 23:04:31 +0100943
malattia@linux.itf6119b02007-04-09 19:31:06 +0200944 printk(KERN_WARNING DRV_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
Stelian Pop7f09c432007-01-13 23:04:31 +0100945 (u32) operand->method.param_count);
946
947 return AE_OK;
948}
949
Mattia Dongilid78865c2007-02-07 20:01:56 +0100950/*
951 * ACPI device
952 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200953static int sony_nc_resume(struct acpi_device *device)
Mattia Dongilid78865c2007-02-07 20:01:56 +0100954{
malattia@linux.it59b19102007-04-09 10:19:04 +0200955 struct sony_nc_value *item;
Mattia Dongilid78865c2007-02-07 20:01:56 +0100956
malattia@linux.it59b19102007-04-09 10:19:04 +0200957 for (item = sony_nc_values; item->name; item++) {
Mattia Dongilid78865c2007-02-07 20:01:56 +0100958 int ret;
959
960 if (!item->valid)
961 continue;
malattia@linux.it59b19102007-04-09 10:19:04 +0200962 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
Len Browna02d1c12007-02-07 15:34:02 -0500963 item->value, NULL);
Mattia Dongilid78865c2007-02-07 20:01:56 +0100964 if (ret < 0) {
Harvey Harrison6e574192008-04-29 00:59:20 -0700965 printk("%s: %d\n", __func__, ret);
Mattia Dongilid78865c2007-02-07 20:01:56 +0100966 break;
967 }
968 }
Mattia Dongili6315fd12007-07-16 02:34:35 +0900969
Mattia Dongilie84a02b2007-08-04 00:22:30 +0900970 /* set the last requested brightness level */
971 if (sony_backlight_device &&
972 !sony_backlight_update_status(sony_backlight_device))
Adam Jackson0ef9cff2008-10-16 15:15:35 -0400973 printk(KERN_WARNING DRV_PFX "unable to restore brightness level\n");
Mattia Dongilie84a02b2007-08-04 00:22:30 +0900974
Mattia Dongili6315fd12007-07-16 02:34:35 +0900975 /* re-initialize models with specific requirements */
976 dmi_check_system(sony_nc_ids);
977
Mattia Dongilid78865c2007-02-07 20:01:56 +0100978 return 0;
979}
980
malattia@linux.it59b19102007-04-09 10:19:04 +0200981static int sony_nc_add(struct acpi_device *device)
Stelian Pop7f09c432007-01-13 23:04:31 +0100982{
983 acpi_status status;
Andrew Morton8607c672007-03-06 02:29:42 -0800984 int result = 0;
Alessandro Guido50f62af2007-01-13 23:04:34 +0100985 acpi_handle handle;
malattia@linux.it56b87562007-04-09 10:19:05 +0200986 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +0100987
malattia@linux.itf6119b02007-04-09 19:31:06 +0200988 printk(KERN_INFO DRV_PFX "%s v%s.\n",
989 SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
990
malattia@linux.it59b19102007-04-09 10:19:04 +0200991 sony_nc_acpi_device = device;
malattia@linux.it33a04452007-04-09 19:26:03 +0200992 strcpy(acpi_device_class(device), "sony/hotkey");
Stelian Popc5611622007-01-13 23:04:37 +0100993
malattia@linux.it59b19102007-04-09 10:19:04 +0200994 sony_nc_acpi_handle = device->handle;
Stelian Pop7f09c432007-01-13 23:04:31 +0100995
Mattia Dongilib25b7322007-07-16 02:34:36 +0900996 /* read device status */
997 result = acpi_bus_get_status(device);
998 /* bail IFF the above call was successful and the device is not present */
999 if (!result && !device->status.present) {
1000 dprintk("Device not present\n");
1001 result = -ENODEV;
1002 goto outwalk;
1003 }
1004
Stelian Pop7f09c432007-01-13 23:04:31 +01001005 if (debug) {
malattia@linux.it59b19102007-04-09 10:19:04 +02001006 status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_nc_acpi_handle,
Stelian Pop7f09c432007-01-13 23:04:31 +01001007 1, sony_walk_callback, NULL, NULL);
1008 if (ACPI_FAILURE(status)) {
malattia@linux.itf6119b02007-04-09 19:31:06 +02001009 printk(KERN_WARNING DRV_PFX "unable to walk acpi resources\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001010 result = -ENODEV;
1011 goto outwalk;
1012 }
Stelian Popc5611622007-01-13 23:04:37 +01001013 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001014
Mattia Dongilib25b7322007-07-16 02:34:36 +09001015 /* try to _INI the device if such method exists (ACPI spec 3.0-6.5.1
1016 * should be respected as we already checked for the device presence above */
1017 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, METHOD_NAME__INI, &handle))) {
1018 dprintk("Invoking _INI\n");
1019 if (ACPI_FAILURE(acpi_evaluate_object(sony_nc_acpi_handle, METHOD_NAME__INI,
1020 NULL, NULL)))
1021 dprintk("_INI Method failed\n");
1022 }
1023
malattia@linux.it1549ee62007-04-09 10:19:08 +02001024 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05001025 result = sony_laptop_setup_input(device);
malattia@linux.it1549ee62007-04-09 10:19:08 +02001026 if (result) {
1027 printk(KERN_ERR DRV_PFX
1028 "Unabe to create input devices.\n");
1029 goto outwalk;
1030 }
1031
malattia@linux.it59b19102007-04-09 10:19:04 +02001032 status = acpi_install_notify_handler(sony_nc_acpi_handle,
Stelian Popc5611622007-01-13 23:04:37 +01001033 ACPI_DEVICE_NOTIFY,
Len Browna02d1c12007-02-07 15:34:02 -05001034 sony_acpi_notify, NULL);
Stelian Popc5611622007-01-13 23:04:37 +01001035 if (ACPI_FAILURE(status)) {
Mattia Dongilib25b7322007-07-16 02:34:36 +09001036 printk(KERN_WARNING DRV_PFX "unable to install notify handler (%u)\n", status);
Stelian Popc5611622007-01-13 23:04:37 +01001037 result = -ENODEV;
malattia@linux.it1549ee62007-04-09 10:19:08 +02001038 goto outinput;
Stelian Pop7f09c432007-01-13 23:04:31 +01001039 }
1040
Alessandro Guido38cfc142008-11-12 23:03:28 +01001041 if (acpi_video_backlight_support()) {
Alessandro Guido3fedd902008-11-12 23:13:35 +01001042 printk(KERN_INFO DRV_PFX "brightness ignored, must be "
Thomas Renninger540b8bb2008-08-01 17:38:02 +02001043 "controlled by ACPI video driver\n");
1044 } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
1045 &handle))) {
Alessandro Guido50f62af2007-01-13 23:04:34 +01001046 sony_backlight_device = backlight_device_register("sony", NULL,
Len Browna02d1c12007-02-07 15:34:02 -05001047 NULL,
Richard Purdie321709c2007-02-10 15:04:08 +00001048 &sony_backlight_ops);
Mattia Dongili7df03b822007-01-13 23:04:41 +01001049
Len Browna02d1c12007-02-07 15:34:02 -05001050 if (IS_ERR(sony_backlight_device)) {
malattia@linux.itf6119b02007-04-09 19:31:06 +02001051 printk(KERN_WARNING DRV_PFX "unable to register backlight device\n");
Mattia Dongili7df03b822007-01-13 23:04:41 +01001052 sony_backlight_device = NULL;
Richard Purdie321709c2007-02-10 15:04:08 +00001053 } else {
1054 sony_backlight_device->props.brightness =
Len Browna02d1c12007-02-07 15:34:02 -05001055 sony_backlight_get_brightness
1056 (sony_backlight_device);
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05001057 sony_backlight_device->props.max_brightness =
Richard Purdie321709c2007-02-10 15:04:08 +00001058 SONY_MAX_BRIGHTNESS - 1;
1059 }
1060
Alessandro Guido50f62af2007-01-13 23:04:34 +01001061 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001062
Mattia Dongili6315fd12007-07-16 02:34:35 +09001063 /* initialize models with specific requirements */
1064 dmi_check_system(sony_nc_ids);
1065
malattia@linux.it49a11de2007-04-09 19:28:56 +02001066 result = sony_pf_add();
1067 if (result)
Mattia Dongilied3aa4b2007-02-07 20:01:54 +01001068 goto outbacklight;
Stelian Pop7f09c432007-01-13 23:04:31 +01001069
malattia@linux.it56b87562007-04-09 10:19:05 +02001070 /* create sony_pf sysfs attributes related to the SNC device */
1071 for (item = sony_nc_values; item->name; ++item) {
1072
1073 if (!debug && item->debug)
1074 continue;
1075
1076 /* find the available acpiget as described in the DSDT */
1077 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
1078 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1079 *item->acpiget,
1080 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02001081 dprintk("Found %s getter: %s\n",
1082 item->name, *item->acpiget);
malattia@linux.it56b87562007-04-09 10:19:05 +02001083 item->devattr.attr.mode |= S_IRUGO;
1084 break;
1085 }
1086 }
1087
1088 /* find the available acpiset as described in the DSDT */
1089 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
1090 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1091 *item->acpiset,
1092 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02001093 dprintk("Found %s setter: %s\n",
1094 item->name, *item->acpiset);
malattia@linux.it56b87562007-04-09 10:19:05 +02001095 item->devattr.attr.mode |= S_IWUSR;
1096 break;
1097 }
1098 }
1099
1100 if (item->devattr.attr.mode != 0) {
1101 result =
1102 device_create_file(&sony_pf_device->dev,
1103 &item->devattr);
1104 if (result)
1105 goto out_sysfs;
1106 }
1107 }
1108
Stelian Pop7f09c432007-01-13 23:04:31 +01001109 return 0;
1110
malattia@linux.it56b87562007-04-09 10:19:05 +02001111 out_sysfs:
1112 for (item = sony_nc_values; item->name; ++item) {
1113 device_remove_file(&sony_pf_device->dev, &item->devattr);
1114 }
1115 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +02001116
Len Browna02d1c12007-02-07 15:34:02 -05001117 outbacklight:
Mattia Dongili7df03b822007-01-13 23:04:41 +01001118 if (sony_backlight_device)
1119 backlight_device_unregister(sony_backlight_device);
1120
malattia@linux.it59b19102007-04-09 10:19:04 +02001121 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
Stelian Popc5611622007-01-13 23:04:37 +01001122 ACPI_DEVICE_NOTIFY,
1123 sony_acpi_notify);
1124 if (ACPI_FAILURE(status))
malattia@linux.itf6119b02007-04-09 19:31:06 +02001125 printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
malattia@linux.it1549ee62007-04-09 10:19:08 +02001126
1127 outinput:
1128 sony_laptop_remove_input();
1129
Len Browna02d1c12007-02-07 15:34:02 -05001130 outwalk:
Stelian Pop7f09c432007-01-13 23:04:31 +01001131 return result;
1132}
1133
malattia@linux.it59b19102007-04-09 10:19:04 +02001134static int sony_nc_remove(struct acpi_device *device, int type)
Stelian Pop7f09c432007-01-13 23:04:31 +01001135{
1136 acpi_status status;
malattia@linux.it56b87562007-04-09 10:19:05 +02001137 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01001138
Alessandro Guido50f62af2007-01-13 23:04:34 +01001139 if (sony_backlight_device)
1140 backlight_device_unregister(sony_backlight_device);
1141
malattia@linux.it59b19102007-04-09 10:19:04 +02001142 sony_nc_acpi_device = NULL;
Stelian Popc5611622007-01-13 23:04:37 +01001143
malattia@linux.it59b19102007-04-09 10:19:04 +02001144 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
Stelian Popc5611622007-01-13 23:04:37 +01001145 ACPI_DEVICE_NOTIFY,
1146 sony_acpi_notify);
1147 if (ACPI_FAILURE(status))
malattia@linux.itf6119b02007-04-09 19:31:06 +02001148 printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001149
malattia@linux.it56b87562007-04-09 10:19:05 +02001150 for (item = sony_nc_values; item->name; ++item) {
1151 device_remove_file(&sony_pf_device->dev, &item->devattr);
1152 }
1153
1154 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +02001155 sony_laptop_remove_input();
malattia@linux.itf6119b02007-04-09 19:31:06 +02001156 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001157
1158 return 0;
1159}
1160
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001161static const struct acpi_device_id sony_device_ids[] = {
1162 {SONY_NC_HID, 0},
1163 {SONY_PIC_HID, 0},
1164 {"", 0},
1165};
1166MODULE_DEVICE_TABLE(acpi, sony_device_ids);
1167
1168static const struct acpi_device_id sony_nc_device_ids[] = {
1169 {SONY_NC_HID, 0},
1170 {"", 0},
1171};
1172
malattia@linux.it59b19102007-04-09 10:19:04 +02001173static struct acpi_driver sony_nc_driver = {
1174 .name = SONY_NC_DRIVER_NAME,
1175 .class = SONY_NC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001176 .ids = sony_nc_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02001177 .owner = THIS_MODULE,
Len Browna02d1c12007-02-07 15:34:02 -05001178 .ops = {
malattia@linux.it59b19102007-04-09 10:19:04 +02001179 .add = sony_nc_add,
1180 .remove = sony_nc_remove,
1181 .resume = sony_nc_resume,
Len Browna02d1c12007-02-07 15:34:02 -05001182 },
Andrew Morton3f4f4612007-01-13 23:04:32 +01001183};
1184
malattia@linux.it33a04452007-04-09 19:26:03 +02001185/*********** SPIC (SNY6001) Device ***********/
1186
1187#define SONYPI_DEVICE_TYPE1 0x00000001
1188#define SONYPI_DEVICE_TYPE2 0x00000002
1189#define SONYPI_DEVICE_TYPE3 0x00000004
Mattia Dongili3eb87492008-01-14 18:05:45 +09001190#define SONYPI_DEVICE_TYPE4 0x00000008
malattia@linux.it33a04452007-04-09 19:26:03 +02001191
Mattia Dongili22a17782007-07-16 02:34:39 +09001192#define SONYPI_TYPE1_OFFSET 0x04
1193#define SONYPI_TYPE2_OFFSET 0x12
1194#define SONYPI_TYPE3_OFFSET 0x12
Mattia Dongili3eb87492008-01-14 18:05:45 +09001195#define SONYPI_TYPE4_OFFSET 0x12
malattia@linux.it33a04452007-04-09 19:26:03 +02001196
malattia@linux.it33a04452007-04-09 19:26:03 +02001197struct sony_pic_ioport {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001198 struct acpi_resource_io io1;
1199 struct acpi_resource_io io2;
malattia@linux.it33a04452007-04-09 19:26:03 +02001200 struct list_head list;
1201};
1202
1203struct sony_pic_irq {
1204 struct acpi_resource_irq irq;
1205 struct list_head list;
1206};
1207
Mattia Dongilide920432008-01-14 18:05:43 +09001208struct sonypi_eventtypes {
1209 u8 data;
1210 unsigned long mask;
1211 struct sonypi_event *events;
1212};
1213
1214struct device_ctrl {
1215 int model;
Mattia Dongili3eb87492008-01-14 18:05:45 +09001216 int (*handle_irq)(const u8, const u8);
Mattia Dongilide920432008-01-14 18:05:43 +09001217 u16 evport_offset;
1218 u8 has_camera;
1219 u8 has_bluetooth;
1220 u8 has_wwan;
1221 struct sonypi_eventtypes *event_types;
1222};
1223
malattia@linux.it33a04452007-04-09 19:26:03 +02001224struct sony_pic_dev {
Mattia Dongilide920432008-01-14 18:05:43 +09001225 struct device_ctrl *control;
malattia@linux.it33a04452007-04-09 19:26:03 +02001226 struct acpi_device *acpi_dev;
1227 struct sony_pic_irq *cur_irq;
1228 struct sony_pic_ioport *cur_ioport;
1229 struct list_head interrupts;
1230 struct list_head ioports;
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001231 struct mutex lock;
Mattia Dongilide920432008-01-14 18:05:43 +09001232 u8 camera_power;
1233 u8 bluetooth_power;
1234 u8 wwan_power;
malattia@linux.it33a04452007-04-09 19:26:03 +02001235};
1236
1237static struct sony_pic_dev spic_dev = {
1238 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
1239 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
1240};
1241
1242/* Event masks */
1243#define SONYPI_JOGGER_MASK 0x00000001
1244#define SONYPI_CAPTURE_MASK 0x00000002
1245#define SONYPI_FNKEY_MASK 0x00000004
1246#define SONYPI_BLUETOOTH_MASK 0x00000008
1247#define SONYPI_PKEY_MASK 0x00000010
1248#define SONYPI_BACK_MASK 0x00000020
1249#define SONYPI_HELP_MASK 0x00000040
1250#define SONYPI_LID_MASK 0x00000080
1251#define SONYPI_ZOOM_MASK 0x00000100
1252#define SONYPI_THUMBPHRASE_MASK 0x00000200
1253#define SONYPI_MEYE_MASK 0x00000400
1254#define SONYPI_MEMORYSTICK_MASK 0x00000800
1255#define SONYPI_BATTERY_MASK 0x00001000
1256#define SONYPI_WIRELESS_MASK 0x00002000
1257
1258struct sonypi_event {
1259 u8 data;
1260 u8 event;
1261};
1262
1263/* The set of possible button release events */
1264static struct sonypi_event sonypi_releaseev[] = {
1265 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
1266 { 0, 0 }
1267};
1268
1269/* The set of possible jogger events */
1270static struct sonypi_event sonypi_joggerev[] = {
1271 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
1272 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
1273 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
1274 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
1275 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
1276 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
1277 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
1278 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
1279 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
1280 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
1281 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
1282 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
1283 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
1284 { 0, 0 }
1285};
1286
1287/* The set of possible capture button events */
1288static struct sonypi_event sonypi_captureev[] = {
1289 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
1290 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09001291 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02001292 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
1293 { 0, 0 }
1294};
1295
1296/* The set of possible fnkeys events */
1297static struct sonypi_event sonypi_fnkeyev[] = {
1298 { 0x10, SONYPI_EVENT_FNKEY_ESC },
1299 { 0x11, SONYPI_EVENT_FNKEY_F1 },
1300 { 0x12, SONYPI_EVENT_FNKEY_F2 },
1301 { 0x13, SONYPI_EVENT_FNKEY_F3 },
1302 { 0x14, SONYPI_EVENT_FNKEY_F4 },
1303 { 0x15, SONYPI_EVENT_FNKEY_F5 },
1304 { 0x16, SONYPI_EVENT_FNKEY_F6 },
1305 { 0x17, SONYPI_EVENT_FNKEY_F7 },
1306 { 0x18, SONYPI_EVENT_FNKEY_F8 },
1307 { 0x19, SONYPI_EVENT_FNKEY_F9 },
1308 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
1309 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
1310 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
1311 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
1312 { 0x21, SONYPI_EVENT_FNKEY_1 },
1313 { 0x22, SONYPI_EVENT_FNKEY_2 },
1314 { 0x31, SONYPI_EVENT_FNKEY_D },
1315 { 0x32, SONYPI_EVENT_FNKEY_E },
1316 { 0x33, SONYPI_EVENT_FNKEY_F },
1317 { 0x34, SONYPI_EVENT_FNKEY_S },
1318 { 0x35, SONYPI_EVENT_FNKEY_B },
1319 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
1320 { 0, 0 }
1321};
1322
1323/* The set of possible program key events */
1324static struct sonypi_event sonypi_pkeyev[] = {
1325 { 0x01, SONYPI_EVENT_PKEY_P1 },
1326 { 0x02, SONYPI_EVENT_PKEY_P2 },
1327 { 0x04, SONYPI_EVENT_PKEY_P3 },
malattia@linux.it33a04452007-04-09 19:26:03 +02001328 { 0, 0 }
1329};
1330
1331/* The set of possible bluetooth events */
1332static struct sonypi_event sonypi_blueev[] = {
1333 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
1334 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
1335 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
1336 { 0, 0 }
1337};
1338
1339/* The set of possible wireless events */
1340static struct sonypi_event sonypi_wlessev[] = {
1341 { 0x59, SONYPI_EVENT_WIRELESS_ON },
1342 { 0x5a, SONYPI_EVENT_WIRELESS_OFF },
1343 { 0, 0 }
1344};
1345
1346/* The set of possible back button events */
1347static struct sonypi_event sonypi_backev[] = {
1348 { 0x20, SONYPI_EVENT_BACK_PRESSED },
1349 { 0, 0 }
1350};
1351
1352/* The set of possible help button events */
1353static struct sonypi_event sonypi_helpev[] = {
1354 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
1355 { 0, 0 }
1356};
1357
1358
1359/* The set of possible lid events */
1360static struct sonypi_event sonypi_lidev[] = {
1361 { 0x51, SONYPI_EVENT_LID_CLOSED },
1362 { 0x50, SONYPI_EVENT_LID_OPENED },
1363 { 0, 0 }
1364};
1365
1366/* The set of possible zoom events */
1367static struct sonypi_event sonypi_zoomev[] = {
1368 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09001369 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
1370 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02001371 { 0, 0 }
1372};
1373
1374/* The set of possible thumbphrase events */
1375static struct sonypi_event sonypi_thumbphraseev[] = {
1376 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
1377 { 0, 0 }
1378};
1379
1380/* The set of possible motioneye camera events */
1381static struct sonypi_event sonypi_meyeev[] = {
1382 { 0x00, SONYPI_EVENT_MEYE_FACE },
1383 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
1384 { 0, 0 }
1385};
1386
1387/* The set of possible memorystick events */
1388static struct sonypi_event sonypi_memorystickev[] = {
1389 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
1390 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
1391 { 0, 0 }
1392};
1393
1394/* The set of possible battery events */
1395static struct sonypi_event sonypi_batteryev[] = {
1396 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
1397 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
1398 { 0, 0 }
1399};
1400
Mattia Dongilide920432008-01-14 18:05:43 +09001401static struct sonypi_eventtypes type1_events[] = {
1402 { 0, 0xffffffff, sonypi_releaseev },
1403 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
1404 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
1405 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
1406 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
1407 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1408 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1409 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
1410 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1411 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
1412 { 0 },
1413};
1414static struct sonypi_eventtypes type2_events[] = {
1415 { 0, 0xffffffff, sonypi_releaseev },
1416 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
1417 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
1418 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
1419 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1420 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1421 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
1422 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
1423 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
1424 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
1425 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
1426 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1427 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1428 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1429 { 0 },
1430};
1431static struct sonypi_eventtypes type3_events[] = {
1432 { 0, 0xffffffff, sonypi_releaseev },
1433 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1434 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
1435 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1436 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1437 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1438 { 0 },
malattia@linux.it33a04452007-04-09 19:26:03 +02001439};
Mattia Dongili3eb87492008-01-14 18:05:45 +09001440static struct sonypi_eventtypes type4_events[] = {
1441 { 0, 0xffffffff, sonypi_releaseev },
1442 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1443 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
1444 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1445 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1446 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
1447 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
1448 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
1449 { 0 },
Mattia Dongilide920432008-01-14 18:05:43 +09001450};
1451
Mattia Dongili3eb87492008-01-14 18:05:45 +09001452/* low level spic calls */
malattia@linux.it33a04452007-04-09 19:26:03 +02001453#define ITERATIONS_LONG 10000
1454#define ITERATIONS_SHORT 10
1455#define wait_on_command(command, iterations) { \
1456 unsigned int n = iterations; \
1457 while (--n && (command)) \
1458 udelay(1); \
1459 if (!n) \
1460 dprintk("command failed at %s : %s (line %d)\n", \
Harvey Harrison6e574192008-04-29 00:59:20 -07001461 __FILE__, __func__, __LINE__); \
malattia@linux.it33a04452007-04-09 19:26:03 +02001462}
1463
1464static u8 sony_pic_call1(u8 dev)
1465{
1466 u8 v1, v2;
1467
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001468 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02001469 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001470 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1471 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
1472 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09001473 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02001474 return v2;
1475}
1476
1477static u8 sony_pic_call2(u8 dev, u8 fn)
1478{
1479 u8 v1;
1480
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001481 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02001482 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001483 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1484 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02001485 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001486 outb(fn, spic_dev.cur_ioport->io1.minimum);
1487 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09001488 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02001489 return v1;
1490}
1491
malattia@linux.it49a11de2007-04-09 19:28:56 +02001492static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
1493{
1494 u8 v1;
1495
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001496 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1497 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1498 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1499 outb(fn, spic_dev.cur_ioport->io1.minimum);
1500 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1501 outb(v, spic_dev.cur_ioport->io1.minimum);
1502 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09001503 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
1504 dev, fn, v, v1);
malattia@linux.it49a11de2007-04-09 19:28:56 +02001505 return v1;
1506}
1507
Mattia Dongili3eb87492008-01-14 18:05:45 +09001508/*
1509 * minidrivers for SPIC models
1510 */
1511static int type4_handle_irq(const u8 data_mask, const u8 ev)
1512{
1513 /*
1514 * 0x31 could mean we have to take some extra action and wait for
1515 * the next irq for some Type4 models, it will generate a new
1516 * irq and we can read new data from the device:
1517 * - 0x5c and 0x5f requires 0xA0
1518 * - 0x61 requires 0xB3
1519 */
1520 if (data_mask == 0x31) {
1521 if (ev == 0x5c || ev == 0x5f)
1522 sony_pic_call1(0xA0);
1523 else if (ev == 0x61)
1524 sony_pic_call1(0xB3);
1525 return 0;
1526 }
1527 return 1;
1528}
1529
1530static struct device_ctrl spic_types[] = {
1531 {
1532 .model = SONYPI_DEVICE_TYPE1,
1533 .handle_irq = NULL,
1534 .evport_offset = SONYPI_TYPE1_OFFSET,
1535 .event_types = type1_events,
1536 },
1537 {
1538 .model = SONYPI_DEVICE_TYPE2,
1539 .handle_irq = NULL,
1540 .evport_offset = SONYPI_TYPE2_OFFSET,
1541 .event_types = type2_events,
1542 },
1543 {
1544 .model = SONYPI_DEVICE_TYPE3,
1545 .handle_irq = NULL,
1546 .evport_offset = SONYPI_TYPE3_OFFSET,
1547 .event_types = type3_events,
1548 },
1549 {
1550 .model = SONYPI_DEVICE_TYPE4,
1551 .handle_irq = type4_handle_irq,
1552 .evport_offset = SONYPI_TYPE4_OFFSET,
1553 .event_types = type4_events,
1554 },
1555};
1556
1557static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
1558{
1559 struct pci_dev *pcidev;
1560
1561 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1562 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
1563 if (pcidev) {
1564 dev->control = &spic_types[0];
1565 goto out;
1566 }
1567
1568 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1569 PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
1570 if (pcidev) {
1571 dev->control = &spic_types[2];
1572 goto out;
1573 }
1574
1575 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1576 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
1577 if (pcidev) {
1578 dev->control = &spic_types[3];
1579 goto out;
1580 }
1581
1582 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1583 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
1584 if (pcidev) {
1585 dev->control = &spic_types[3];
1586 goto out;
1587 }
1588
1589 /* default */
1590 dev->control = &spic_types[1];
1591
1592out:
1593 if (pcidev)
1594 pci_dev_put(pcidev);
1595
1596 printk(KERN_INFO DRV_PFX "detected Type%d model\n",
1597 dev->control->model == SONYPI_DEVICE_TYPE1 ? 1 :
1598 dev->control->model == SONYPI_DEVICE_TYPE2 ? 2 :
1599 dev->control->model == SONYPI_DEVICE_TYPE3 ? 3 : 4);
1600}
1601
malattia@linux.it49a11de2007-04-09 19:28:56 +02001602/* camera tests and poweron/poweroff */
1603#define SONYPI_CAMERA_PICTURE 5
malattia@linux.it49a11de2007-04-09 19:28:56 +02001604#define SONYPI_CAMERA_CONTROL 0x10
malattia@linux.ite3646322007-04-28 23:34:22 +09001605
1606#define SONYPI_CAMERA_BRIGHTNESS 0
1607#define SONYPI_CAMERA_CONTRAST 1
1608#define SONYPI_CAMERA_HUE 2
1609#define SONYPI_CAMERA_COLOR 3
1610#define SONYPI_CAMERA_SHARPNESS 4
1611
1612#define SONYPI_CAMERA_EXPOSURE_MASK 0xC
1613#define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
1614#define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
1615#define SONYPI_CAMERA_MUTE_MASK 0x40
1616
1617/* the rest don't need a loop until not 0xff */
1618#define SONYPI_CAMERA_AGC 6
1619#define SONYPI_CAMERA_AGC_MASK 0x30
1620#define SONYPI_CAMERA_SHUTTER_MASK 0x7
1621
1622#define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
1623#define SONYPI_CAMERA_CONTROL 0x10
1624
1625#define SONYPI_CAMERA_STATUS 7
1626#define SONYPI_CAMERA_STATUS_READY 0x2
1627#define SONYPI_CAMERA_STATUS_POSITION 0x4
1628
1629#define SONYPI_DIRECTION_BACKWARDS 0x4
1630
1631#define SONYPI_CAMERA_REVISION 8
1632#define SONYPI_CAMERA_ROMVERSION 9
malattia@linux.it49a11de2007-04-09 19:28:56 +02001633
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001634static int __sony_pic_camera_ready(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001635{
1636 u8 v;
1637
1638 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
1639 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
1640}
1641
malattia@linux.ite3646322007-04-28 23:34:22 +09001642static int __sony_pic_camera_off(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001643{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001644 if (!camera) {
1645 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1646 return -ENODEV;
1647 }
1648
malattia@linux.it49a11de2007-04-09 19:28:56 +02001649 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
1650 SONYPI_CAMERA_MUTE_MASK),
1651 ITERATIONS_SHORT);
1652
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001653 if (spic_dev.camera_power) {
1654 sony_pic_call2(0x91, 0);
1655 spic_dev.camera_power = 0;
1656 }
1657 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001658}
1659
malattia@linux.ite3646322007-04-28 23:34:22 +09001660static int __sony_pic_camera_on(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001661{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001662 int i, j, x;
1663
1664 if (!camera) {
1665 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1666 return -ENODEV;
1667 }
malattia@linux.it49a11de2007-04-09 19:28:56 +02001668
1669 if (spic_dev.camera_power)
malattia@linux.ite3646322007-04-28 23:34:22 +09001670 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001671
1672 for (j = 5; j > 0; j--) {
1673
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001674 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001675 msleep(10);
1676 sony_pic_call1(0x93);
1677
1678 for (i = 400; i > 0; i--) {
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001679 if (__sony_pic_camera_ready())
malattia@linux.it49a11de2007-04-09 19:28:56 +02001680 break;
1681 msleep(10);
1682 }
1683 if (i)
1684 break;
1685 }
1686
1687 if (j == 0) {
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001688 printk(KERN_WARNING DRV_PFX "failed to power on camera\n");
malattia@linux.ite3646322007-04-28 23:34:22 +09001689 return -ENODEV;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001690 }
1691
1692 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
1693 0x5a),
1694 ITERATIONS_SHORT);
1695
1696 spic_dev.camera_power = 1;
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001697 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001698}
1699
malattia@linux.ite3646322007-04-28 23:34:22 +09001700/* External camera command (exported to the motion eye v4l driver) */
1701int sony_pic_camera_command(int command, u8 value)
1702{
1703 if (!camera)
1704 return -EIO;
1705
1706 mutex_lock(&spic_dev.lock);
1707
1708 switch (command) {
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001709 case SONY_PIC_COMMAND_SETCAMERA:
malattia@linux.ite3646322007-04-28 23:34:22 +09001710 if (value)
1711 __sony_pic_camera_on();
1712 else
1713 __sony_pic_camera_off();
1714 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001715 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09001716 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
1717 ITERATIONS_SHORT);
1718 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001719 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
malattia@linux.ite3646322007-04-28 23:34:22 +09001720 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
1721 ITERATIONS_SHORT);
1722 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001723 case SONY_PIC_COMMAND_SETCAMERAHUE:
malattia@linux.ite3646322007-04-28 23:34:22 +09001724 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
1725 ITERATIONS_SHORT);
1726 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001727 case SONY_PIC_COMMAND_SETCAMERACOLOR:
malattia@linux.ite3646322007-04-28 23:34:22 +09001728 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
1729 ITERATIONS_SHORT);
1730 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001731 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09001732 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
1733 ITERATIONS_SHORT);
1734 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001735 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
malattia@linux.ite3646322007-04-28 23:34:22 +09001736 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
1737 ITERATIONS_SHORT);
1738 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001739 case SONY_PIC_COMMAND_SETCAMERAAGC:
malattia@linux.ite3646322007-04-28 23:34:22 +09001740 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
1741 ITERATIONS_SHORT);
1742 break;
1743 default:
1744 printk(KERN_ERR DRV_PFX "sony_pic_camera_command invalid: %d\n",
1745 command);
1746 break;
1747 }
1748 mutex_unlock(&spic_dev.lock);
1749 return 0;
1750}
1751EXPORT_SYMBOL(sony_pic_camera_command);
1752
malattia@linux.it9476cdf2007-04-28 23:21:42 +09001753/* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
1754static void sony_pic_set_wwanpower(u8 state)
1755{
1756 state = !!state;
1757 mutex_lock(&spic_dev.lock);
1758 if (spic_dev.wwan_power == state) {
1759 mutex_unlock(&spic_dev.lock);
1760 return;
1761 }
1762 sony_pic_call2(0xB0, state);
1763 spic_dev.wwan_power = state;
1764 mutex_unlock(&spic_dev.lock);
1765}
1766
1767static ssize_t sony_pic_wwanpower_store(struct device *dev,
1768 struct device_attribute *attr,
1769 const char *buffer, size_t count)
1770{
1771 unsigned long value;
1772 if (count > 31)
1773 return -EINVAL;
1774
1775 value = simple_strtoul(buffer, NULL, 10);
1776 sony_pic_set_wwanpower(value);
1777
1778 return count;
1779}
1780
1781static ssize_t sony_pic_wwanpower_show(struct device *dev,
1782 struct device_attribute *attr, char *buffer)
1783{
1784 ssize_t count;
1785 mutex_lock(&spic_dev.lock);
1786 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
1787 mutex_unlock(&spic_dev.lock);
1788 return count;
1789}
1790
malattia@linux.it49a11de2007-04-09 19:28:56 +02001791/* bluetooth subsystem power state */
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001792static void __sony_pic_set_bluetoothpower(u8 state)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001793{
1794 state = !!state;
1795 if (spic_dev.bluetooth_power == state)
1796 return;
1797 sony_pic_call2(0x96, state);
1798 sony_pic_call1(0x82);
1799 spic_dev.bluetooth_power = state;
1800}
1801
1802static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
1803 struct device_attribute *attr,
1804 const char *buffer, size_t count)
1805{
1806 unsigned long value;
1807 if (count > 31)
1808 return -EINVAL;
1809
1810 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001811 mutex_lock(&spic_dev.lock);
1812 __sony_pic_set_bluetoothpower(value);
1813 mutex_unlock(&spic_dev.lock);
malattia@linux.it49a11de2007-04-09 19:28:56 +02001814
1815 return count;
1816}
1817
1818static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
1819 struct device_attribute *attr, char *buffer)
1820{
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001821 ssize_t count = 0;
1822 mutex_lock(&spic_dev.lock);
1823 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
1824 mutex_unlock(&spic_dev.lock);
1825 return count;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001826}
1827
1828/* fan speed */
1829/* FAN0 information (reverse engineered from ACPI tables) */
1830#define SONY_PIC_FAN0_STATUS 0x93
malattia@linux.it7b153f32007-04-09 19:31:25 +02001831static int sony_pic_set_fanspeed(unsigned long value)
1832{
1833 return ec_write(SONY_PIC_FAN0_STATUS, value);
1834}
1835
1836static int sony_pic_get_fanspeed(u8 *value)
1837{
1838 return ec_read(SONY_PIC_FAN0_STATUS, value);
1839}
1840
malattia@linux.it49a11de2007-04-09 19:28:56 +02001841static ssize_t sony_pic_fanspeed_store(struct device *dev,
1842 struct device_attribute *attr,
1843 const char *buffer, size_t count)
1844{
1845 unsigned long value;
1846 if (count > 31)
1847 return -EINVAL;
1848
1849 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it7b153f32007-04-09 19:31:25 +02001850 if (sony_pic_set_fanspeed(value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02001851 return -EIO;
1852
1853 return count;
1854}
1855
1856static ssize_t sony_pic_fanspeed_show(struct device *dev,
1857 struct device_attribute *attr, char *buffer)
1858{
1859 u8 value = 0;
malattia@linux.it7b153f32007-04-09 19:31:25 +02001860 if (sony_pic_get_fanspeed(&value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02001861 return -EIO;
1862
1863 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
1864}
1865
1866#define SPIC_ATTR(_name, _mode) \
1867struct device_attribute spic_attr_##_name = __ATTR(_name, \
1868 _mode, sony_pic_## _name ##_show, \
1869 sony_pic_## _name ##_store)
1870
malattia@linux.it49a11de2007-04-09 19:28:56 +02001871static SPIC_ATTR(bluetoothpower, 0644);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09001872static SPIC_ATTR(wwanpower, 0644);
malattia@linux.it49a11de2007-04-09 19:28:56 +02001873static SPIC_ATTR(fanspeed, 0644);
1874
1875static struct attribute *spic_attributes[] = {
malattia@linux.it49a11de2007-04-09 19:28:56 +02001876 &spic_attr_bluetoothpower.attr,
malattia@linux.it9476cdf2007-04-28 23:21:42 +09001877 &spic_attr_wwanpower.attr,
malattia@linux.it49a11de2007-04-09 19:28:56 +02001878 &spic_attr_fanspeed.attr,
1879 NULL
1880};
1881
1882static struct attribute_group spic_attribute_group = {
1883 .attrs = spic_attributes
1884};
1885
malattia@linux.it7b153f32007-04-09 19:31:25 +02001886/******** SONYPI compatibility **********/
Mattia Dongilia64e62a2007-05-01 11:19:53 +09001887#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +02001888
1889/* battery / brightness / temperature addresses */
1890#define SONYPI_BAT_FLAGS 0x81
1891#define SONYPI_LCD_LIGHT 0x96
1892#define SONYPI_BAT1_PCTRM 0xa0
1893#define SONYPI_BAT1_LEFT 0xa2
1894#define SONYPI_BAT1_MAXRT 0xa4
1895#define SONYPI_BAT2_PCTRM 0xa8
1896#define SONYPI_BAT2_LEFT 0xaa
1897#define SONYPI_BAT2_MAXRT 0xac
1898#define SONYPI_BAT1_MAXTK 0xb0
1899#define SONYPI_BAT1_FULL 0xb2
1900#define SONYPI_BAT2_MAXTK 0xb8
1901#define SONYPI_BAT2_FULL 0xba
1902#define SONYPI_TEMP_STATUS 0xC1
1903
1904struct sonypi_compat_s {
1905 struct fasync_struct *fifo_async;
1906 struct kfifo *fifo;
1907 spinlock_t fifo_lock;
1908 wait_queue_head_t fifo_proc_list;
1909 atomic_t open_count;
1910};
1911static struct sonypi_compat_s sonypi_compat = {
1912 .open_count = ATOMIC_INIT(0),
1913};
1914
1915static int sonypi_misc_fasync(int fd, struct file *filp, int on)
1916{
1917 int retval;
1918
1919 retval = fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
1920 if (retval < 0)
1921 return retval;
1922 return 0;
1923}
1924
1925static int sonypi_misc_release(struct inode *inode, struct file *file)
1926{
malattia@linux.it7b153f32007-04-09 19:31:25 +02001927 atomic_dec(&sonypi_compat.open_count);
1928 return 0;
1929}
1930
1931static int sonypi_misc_open(struct inode *inode, struct file *file)
1932{
1933 /* Flush input queue on first open */
Arnd Bergmann0410e682008-05-20 19:16:45 +02001934 lock_kernel();
malattia@linux.it7b153f32007-04-09 19:31:25 +02001935 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
1936 kfifo_reset(sonypi_compat.fifo);
Arnd Bergmann0410e682008-05-20 19:16:45 +02001937 unlock_kernel();
malattia@linux.it7b153f32007-04-09 19:31:25 +02001938 return 0;
1939}
1940
1941static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
1942 size_t count, loff_t *pos)
1943{
1944 ssize_t ret;
1945 unsigned char c;
1946
1947 if ((kfifo_len(sonypi_compat.fifo) == 0) &&
1948 (file->f_flags & O_NONBLOCK))
1949 return -EAGAIN;
1950
1951 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
1952 kfifo_len(sonypi_compat.fifo) != 0);
1953 if (ret)
1954 return ret;
1955
1956 while (ret < count &&
1957 (kfifo_get(sonypi_compat.fifo, &c, sizeof(c)) == sizeof(c))) {
1958 if (put_user(c, buf++))
1959 return -EFAULT;
1960 ret++;
1961 }
1962
1963 if (ret > 0) {
1964 struct inode *inode = file->f_path.dentry->d_inode;
1965 inode->i_atime = current_fs_time(inode->i_sb);
1966 }
1967
1968 return ret;
1969}
1970
1971static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
1972{
1973 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
1974 if (kfifo_len(sonypi_compat.fifo))
1975 return POLLIN | POLLRDNORM;
1976 return 0;
1977}
1978
1979static int ec_read16(u8 addr, u16 *value)
1980{
1981 u8 val_lb, val_hb;
1982 if (ec_read(addr, &val_lb))
1983 return -1;
1984 if (ec_read(addr + 1, &val_hb))
1985 return -1;
1986 *value = val_lb | (val_hb << 8);
1987 return 0;
1988}
1989
1990static int sonypi_misc_ioctl(struct inode *ip, struct file *fp,
1991 unsigned int cmd, unsigned long arg)
1992{
1993 int ret = 0;
1994 void __user *argp = (void __user *)arg;
1995 u8 val8;
1996 u16 val16;
1997 int value;
1998
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001999 mutex_lock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002000 switch (cmd) {
2001 case SONYPI_IOCGBRT:
2002 if (sony_backlight_device == NULL) {
2003 ret = -EIO;
2004 break;
2005 }
2006 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
2007 ret = -EIO;
2008 break;
2009 }
2010 val8 = ((value & 0xff) - 1) << 5;
2011 if (copy_to_user(argp, &val8, sizeof(val8)))
2012 ret = -EFAULT;
2013 break;
2014 case SONYPI_IOCSBRT:
2015 if (sony_backlight_device == NULL) {
2016 ret = -EIO;
2017 break;
2018 }
2019 if (copy_from_user(&val8, argp, sizeof(val8))) {
2020 ret = -EFAULT;
2021 break;
2022 }
2023 if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
2024 (val8 >> 5) + 1, NULL)) {
2025 ret = -EIO;
2026 break;
2027 }
2028 /* sync the backlight device status */
2029 sony_backlight_device->props.brightness =
2030 sony_backlight_get_brightness(sony_backlight_device);
2031 break;
2032 case SONYPI_IOCGBAT1CAP:
2033 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
2034 ret = -EIO;
2035 break;
2036 }
2037 if (copy_to_user(argp, &val16, sizeof(val16)))
2038 ret = -EFAULT;
2039 break;
2040 case SONYPI_IOCGBAT1REM:
2041 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
2042 ret = -EIO;
2043 break;
2044 }
2045 if (copy_to_user(argp, &val16, sizeof(val16)))
2046 ret = -EFAULT;
2047 break;
2048 case SONYPI_IOCGBAT2CAP:
2049 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
2050 ret = -EIO;
2051 break;
2052 }
2053 if (copy_to_user(argp, &val16, sizeof(val16)))
2054 ret = -EFAULT;
2055 break;
2056 case SONYPI_IOCGBAT2REM:
2057 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
2058 ret = -EIO;
2059 break;
2060 }
2061 if (copy_to_user(argp, &val16, sizeof(val16)))
2062 ret = -EFAULT;
2063 break;
2064 case SONYPI_IOCGBATFLAGS:
2065 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
2066 ret = -EIO;
2067 break;
2068 }
2069 val8 &= 0x07;
2070 if (copy_to_user(argp, &val8, sizeof(val8)))
2071 ret = -EFAULT;
2072 break;
2073 case SONYPI_IOCGBLUE:
2074 val8 = spic_dev.bluetooth_power;
2075 if (copy_to_user(argp, &val8, sizeof(val8)))
2076 ret = -EFAULT;
2077 break;
2078 case SONYPI_IOCSBLUE:
2079 if (copy_from_user(&val8, argp, sizeof(val8))) {
2080 ret = -EFAULT;
2081 break;
2082 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002083 __sony_pic_set_bluetoothpower(val8);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002084 break;
2085 /* FAN Controls */
2086 case SONYPI_IOCGFAN:
2087 if (sony_pic_get_fanspeed(&val8)) {
2088 ret = -EIO;
2089 break;
2090 }
2091 if (copy_to_user(argp, &val8, sizeof(val8)))
2092 ret = -EFAULT;
2093 break;
2094 case SONYPI_IOCSFAN:
2095 if (copy_from_user(&val8, argp, sizeof(val8))) {
2096 ret = -EFAULT;
2097 break;
2098 }
2099 if (sony_pic_set_fanspeed(val8))
2100 ret = -EIO;
2101 break;
2102 /* GET Temperature (useful under APM) */
2103 case SONYPI_IOCGTEMP:
2104 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
2105 ret = -EIO;
2106 break;
2107 }
2108 if (copy_to_user(argp, &val8, sizeof(val8)))
2109 ret = -EFAULT;
2110 break;
2111 default:
2112 ret = -EINVAL;
2113 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002114 mutex_unlock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002115 return ret;
2116}
2117
2118static const struct file_operations sonypi_misc_fops = {
2119 .owner = THIS_MODULE,
2120 .read = sonypi_misc_read,
2121 .poll = sonypi_misc_poll,
2122 .open = sonypi_misc_open,
2123 .release = sonypi_misc_release,
2124 .fasync = sonypi_misc_fasync,
2125 .ioctl = sonypi_misc_ioctl,
2126};
2127
2128static struct miscdevice sonypi_misc_device = {
2129 .minor = MISC_DYNAMIC_MINOR,
2130 .name = "sonypi",
2131 .fops = &sonypi_misc_fops,
2132};
2133
2134static void sonypi_compat_report_event(u8 event)
2135{
2136 kfifo_put(sonypi_compat.fifo, (unsigned char *)&event, sizeof(event));
2137 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
2138 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
2139}
2140
2141static int sonypi_compat_init(void)
2142{
2143 int error;
2144
2145 spin_lock_init(&sonypi_compat.fifo_lock);
2146 sonypi_compat.fifo = kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
2147 &sonypi_compat.fifo_lock);
2148 if (IS_ERR(sonypi_compat.fifo)) {
2149 printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
2150 return PTR_ERR(sonypi_compat.fifo);
2151 }
2152
2153 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002154
2155 if (minor != -1)
2156 sonypi_misc_device.minor = minor;
2157 error = misc_register(&sonypi_misc_device);
2158 if (error) {
2159 printk(KERN_ERR DRV_PFX "misc_register failed\n");
2160 goto err_free_kfifo;
2161 }
2162 if (minor == -1)
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002163 printk(KERN_INFO DRV_PFX "device allocated minor is %d\n",
malattia@linux.it7b153f32007-04-09 19:31:25 +02002164 sonypi_misc_device.minor);
2165
2166 return 0;
2167
2168err_free_kfifo:
2169 kfifo_free(sonypi_compat.fifo);
2170 return error;
2171}
2172
2173static void sonypi_compat_exit(void)
2174{
2175 misc_deregister(&sonypi_misc_device);
2176 kfifo_free(sonypi_compat.fifo);
2177}
2178#else
2179static int sonypi_compat_init(void) { return 0; }
2180static void sonypi_compat_exit(void) { }
2181static void sonypi_compat_report_event(u8 event) { }
Mattia Dongilia64e62a2007-05-01 11:19:53 +09002182#endif /* CONFIG_SONYPI_COMPAT */
malattia@linux.it7b153f32007-04-09 19:31:25 +02002183
malattia@linux.it1549ee62007-04-09 10:19:08 +02002184/*
malattia@linux.it33a04452007-04-09 19:26:03 +02002185 * ACPI callbacks
malattia@linux.it1549ee62007-04-09 10:19:08 +02002186 */
malattia@linux.it33a04452007-04-09 19:26:03 +02002187static acpi_status
2188sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
2189{
2190 u32 i;
2191 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
2192
2193 switch (resource->type) {
2194 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002195 {
2196 /* start IO enumeration */
2197 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
2198 if (!ioport)
2199 return AE_ERROR;
2200
2201 list_add(&ioport->list, &dev->ioports);
2202 return AE_OK;
2203 }
2204
malattia@linux.it33a04452007-04-09 19:26:03 +02002205 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002206 /* end IO enumeration */
malattia@linux.it33a04452007-04-09 19:26:03 +02002207 return AE_OK;
2208
2209 case ACPI_RESOURCE_TYPE_IRQ:
2210 {
2211 struct acpi_resource_irq *p = &resource->data.irq;
2212 struct sony_pic_irq *interrupt = NULL;
2213 if (!p || !p->interrupt_count) {
2214 /*
2215 * IRQ descriptors may have no IRQ# bits set,
2216 * particularly those those w/ _STA disabled
2217 */
2218 dprintk("Blank IRQ resource\n");
2219 return AE_OK;
2220 }
2221 for (i = 0; i < p->interrupt_count; i++) {
2222 if (!p->interrupts[i]) {
2223 printk(KERN_WARNING DRV_PFX
2224 "Invalid IRQ %d\n",
2225 p->interrupts[i]);
2226 continue;
2227 }
2228 interrupt = kzalloc(sizeof(*interrupt),
2229 GFP_KERNEL);
2230 if (!interrupt)
2231 return AE_ERROR;
2232
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002233 list_add(&interrupt->list, &dev->interrupts);
malattia@linux.it33a04452007-04-09 19:26:03 +02002234 interrupt->irq.triggering = p->triggering;
2235 interrupt->irq.polarity = p->polarity;
2236 interrupt->irq.sharable = p->sharable;
2237 interrupt->irq.interrupt_count = 1;
2238 interrupt->irq.interrupts[0] = p->interrupts[i];
2239 }
2240 return AE_OK;
2241 }
2242 case ACPI_RESOURCE_TYPE_IO:
2243 {
2244 struct acpi_resource_io *io = &resource->data.io;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002245 struct sony_pic_ioport *ioport =
2246 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
malattia@linux.it33a04452007-04-09 19:26:03 +02002247 if (!io) {
2248 dprintk("Blank IO resource\n");
2249 return AE_OK;
2250 }
2251
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002252 if (!ioport->io1.minimum) {
2253 memcpy(&ioport->io1, io, sizeof(*io));
2254 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
2255 ioport->io1.address_length);
2256 }
2257 else if (!ioport->io2.minimum) {
2258 memcpy(&ioport->io2, io, sizeof(*io));
2259 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
2260 ioport->io2.address_length);
2261 }
2262 else {
2263 printk(KERN_ERR DRV_PFX "Unknown SPIC Type, more than 2 IO Ports\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002264 return AE_ERROR;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002265 }
malattia@linux.it33a04452007-04-09 19:26:03 +02002266 return AE_OK;
2267 }
2268 default:
2269 dprintk("Resource %d isn't an IRQ nor an IO port\n",
2270 resource->type);
2271
2272 case ACPI_RESOURCE_TYPE_END_TAG:
2273 return AE_OK;
2274 }
2275 return AE_CTRL_TERMINATE;
2276}
2277
2278static int sony_pic_possible_resources(struct acpi_device *device)
2279{
2280 int result = 0;
2281 acpi_status status = AE_OK;
2282
2283 if (!device)
2284 return -EINVAL;
2285
2286 /* get device status */
2287 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
2288 dprintk("Evaluating _STA\n");
2289 result = acpi_bus_get_status(device);
2290 if (result) {
2291 printk(KERN_WARNING DRV_PFX "Unable to read status\n");
2292 goto end;
2293 }
2294
2295 if (!device->status.enabled)
2296 dprintk("Device disabled\n");
2297 else
2298 dprintk("Device enabled\n");
2299
2300 /*
2301 * Query and parse 'method'
2302 */
2303 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
2304 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
2305 sony_pic_read_possible_resource, &spic_dev);
2306 if (ACPI_FAILURE(status)) {
2307 printk(KERN_WARNING DRV_PFX
2308 "Failure evaluating %s\n",
2309 METHOD_NAME__PRS);
2310 result = -ENODEV;
2311 }
2312end:
2313 return result;
2314}
2315
2316/*
2317 * Disable the spic device by calling its _DIS method
2318 */
2319static int sony_pic_disable(struct acpi_device *device)
2320{
Matthew Garrett6158d3a2008-10-29 14:01:03 -07002321 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
2322 NULL);
2323
2324 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
malattia@linux.it33a04452007-04-09 19:26:03 +02002325 return -ENXIO;
2326
2327 dprintk("Device disabled\n");
2328 return 0;
2329}
2330
2331
2332/*
2333 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
2334 *
2335 * Call _SRS to set current resources
2336 */
2337static int sony_pic_enable(struct acpi_device *device,
2338 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
2339{
2340 acpi_status status;
2341 int result = 0;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002342 /* Type 1 resource layout is:
2343 * IO
2344 * IO
2345 * IRQNoFlags
2346 * End
2347 *
2348 * Type 2 and 3 resource layout is:
2349 * IO
2350 * IRQNoFlags
2351 * End
2352 */
malattia@linux.it33a04452007-04-09 19:26:03 +02002353 struct {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002354 struct acpi_resource res1;
2355 struct acpi_resource res2;
2356 struct acpi_resource res3;
2357 struct acpi_resource res4;
malattia@linux.it33a04452007-04-09 19:26:03 +02002358 } *resource;
2359 struct acpi_buffer buffer = { 0, NULL };
2360
2361 if (!ioport || !irq)
2362 return -EINVAL;
2363
2364 /* init acpi_buffer */
2365 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
2366 if (!resource)
2367 return -ENOMEM;
2368
2369 buffer.length = sizeof(*resource) + 1;
2370 buffer.pointer = resource;
2371
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002372 /* setup Type 1 resources */
Mattia Dongilide920432008-01-14 18:05:43 +09002373 if (spic_dev.control->model == SONYPI_DEVICE_TYPE1) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002374
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002375 /* setup io resources */
2376 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2377 resource->res1.length = sizeof(struct acpi_resource);
2378 memcpy(&resource->res1.data.io, &ioport->io1,
2379 sizeof(struct acpi_resource_io));
malattia@linux.it33a04452007-04-09 19:26:03 +02002380
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002381 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
2382 resource->res2.length = sizeof(struct acpi_resource);
2383 memcpy(&resource->res2.data.io, &ioport->io2,
2384 sizeof(struct acpi_resource_io));
2385
2386 /* setup irq resource */
2387 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
2388 resource->res3.length = sizeof(struct acpi_resource);
2389 memcpy(&resource->res3.data.irq, &irq->irq,
2390 sizeof(struct acpi_resource_irq));
2391 /* we requested a shared irq */
2392 resource->res3.data.irq.sharable = ACPI_SHARED;
2393
2394 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
2395
2396 }
2397 /* setup Type 2/3 resources */
2398 else {
2399 /* setup io resource */
2400 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2401 resource->res1.length = sizeof(struct acpi_resource);
2402 memcpy(&resource->res1.data.io, &ioport->io1,
2403 sizeof(struct acpi_resource_io));
2404
2405 /* setup irq resource */
2406 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
2407 resource->res2.length = sizeof(struct acpi_resource);
2408 memcpy(&resource->res2.data.irq, &irq->irq,
2409 sizeof(struct acpi_resource_irq));
2410 /* we requested a shared irq */
2411 resource->res2.data.irq.sharable = ACPI_SHARED;
2412
2413 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
2414 }
malattia@linux.it33a04452007-04-09 19:26:03 +02002415
2416 /* Attempt to set the resource */
2417 dprintk("Evaluating _SRS\n");
2418 status = acpi_set_current_resources(device->handle, &buffer);
2419
2420 /* check for total failure */
2421 if (ACPI_FAILURE(status)) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002422 printk(KERN_ERR DRV_PFX "Error evaluating _SRS\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002423 result = -ENODEV;
2424 goto end;
2425 }
2426
2427 /* Necessary device initializations calls (from sonypi) */
2428 sony_pic_call1(0x82);
2429 sony_pic_call2(0x81, 0xff);
2430 sony_pic_call1(compat ? 0x92 : 0x82);
2431
2432end:
2433 kfree(resource);
2434 return result;
2435}
2436
2437/*****************
2438 *
2439 * ISR: some event is available
2440 *
2441 *****************/
2442static irqreturn_t sony_pic_irq(int irq, void *dev_id)
2443{
2444 int i, j;
malattia@linux.it33a04452007-04-09 19:26:03 +02002445 u8 ev = 0;
2446 u8 data_mask = 0;
2447 u8 device_event = 0;
2448
2449 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
2450
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002451 ev = inb_p(dev->cur_ioport->io1.minimum);
2452 if (dev->cur_ioport->io2.minimum)
2453 data_mask = inb_p(dev->cur_ioport->io2.minimum);
2454 else
Mattia Dongilide920432008-01-14 18:05:43 +09002455 data_mask = inb_p(dev->cur_ioport->io1.minimum +
2456 dev->control->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02002457
Mattia Dongili22a17782007-07-16 02:34:39 +09002458 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
Mattia Dongilide920432008-01-14 18:05:43 +09002459 ev, data_mask, dev->cur_ioport->io1.minimum,
2460 dev->control->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02002461
2462 if (ev == 0x00 || ev == 0xff)
2463 return IRQ_HANDLED;
2464
Mattia Dongilide920432008-01-14 18:05:43 +09002465 for (i = 0; dev->control->event_types[i].mask; i++) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002466
Mattia Dongilide920432008-01-14 18:05:43 +09002467 if ((data_mask & dev->control->event_types[i].data) !=
2468 dev->control->event_types[i].data)
malattia@linux.it33a04452007-04-09 19:26:03 +02002469 continue;
2470
Mattia Dongilide920432008-01-14 18:05:43 +09002471 if (!(mask & dev->control->event_types[i].mask))
malattia@linux.it33a04452007-04-09 19:26:03 +02002472 continue;
2473
Mattia Dongilide920432008-01-14 18:05:43 +09002474 for (j = 0; dev->control->event_types[i].events[j].event; j++) {
2475 if (ev == dev->control->event_types[i].events[j].data) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002476 device_event =
Mattia Dongilide920432008-01-14 18:05:43 +09002477 dev->control->
2478 event_types[i].events[j].event;
malattia@linux.it33a04452007-04-09 19:26:03 +02002479 goto found;
2480 }
2481 }
2482 }
Mattia Dongili3eb87492008-01-14 18:05:45 +09002483 /* Still not able to decode the event try to pass
2484 * it over to the minidriver
2485 */
2486 if (dev->control->handle_irq &&
2487 dev->control->handle_irq(data_mask, ev) == 0)
2488 return IRQ_HANDLED;
2489
Mattia Dongilide920432008-01-14 18:05:43 +09002490 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
2491 ev, data_mask, dev->cur_ioport->io1.minimum,
2492 dev->control->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02002493 return IRQ_HANDLED;
2494
2495found:
malattia@linux.it1549ee62007-04-09 10:19:08 +02002496 sony_laptop_report_input_event(device_event);
Mattia Dongilide920432008-01-14 18:05:43 +09002497 acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002498 sonypi_compat_report_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02002499
2500 return IRQ_HANDLED;
2501}
2502
2503/*****************
2504 *
2505 * ACPI driver
2506 *
2507 *****************/
2508static int sony_pic_remove(struct acpi_device *device, int type)
2509{
2510 struct sony_pic_ioport *io, *tmp_io;
2511 struct sony_pic_irq *irq, *tmp_irq;
2512
2513 if (sony_pic_disable(device)) {
2514 printk(KERN_ERR DRV_PFX "Couldn't disable device.\n");
2515 return -ENXIO;
2516 }
2517
2518 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002519 release_region(spic_dev.cur_ioport->io1.minimum,
2520 spic_dev.cur_ioport->io1.address_length);
2521 if (spic_dev.cur_ioport->io2.minimum)
2522 release_region(spic_dev.cur_ioport->io2.minimum,
2523 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02002524
Mattia Dongili015a9162007-08-12 16:20:27 +09002525 sonypi_compat_exit();
2526
malattia@linux.it1549ee62007-04-09 10:19:08 +02002527 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02002528
malattia@linux.it49a11de2007-04-09 19:28:56 +02002529 /* pf attrs */
2530 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2531 sony_pf_remove();
2532
malattia@linux.it33a04452007-04-09 19:26:03 +02002533 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2534 list_del(&io->list);
2535 kfree(io);
2536 }
2537 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2538 list_del(&irq->list);
2539 kfree(irq);
2540 }
2541 spic_dev.cur_ioport = NULL;
2542 spic_dev.cur_irq = NULL;
2543
malattia@linux.itf6119b02007-04-09 19:31:06 +02002544 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002545 return 0;
2546}
2547
2548static int sony_pic_add(struct acpi_device *device)
2549{
2550 int result;
2551 struct sony_pic_ioport *io, *tmp_io;
2552 struct sony_pic_irq *irq, *tmp_irq;
2553
malattia@linux.itf6119b02007-04-09 19:31:06 +02002554 printk(KERN_INFO DRV_PFX "%s v%s.\n",
2555 SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.it33a04452007-04-09 19:26:03 +02002556
2557 spic_dev.acpi_dev = device;
2558 strcpy(acpi_device_class(device), "sony/hotkey");
Mattia Dongilide920432008-01-14 18:05:43 +09002559 sony_pic_detect_device_type(&spic_dev);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002560 mutex_init(&spic_dev.lock);
malattia@linux.it33a04452007-04-09 19:26:03 +02002561
2562 /* read _PRS resources */
2563 result = sony_pic_possible_resources(device);
2564 if (result) {
2565 printk(KERN_ERR DRV_PFX
2566 "Unabe to read possible resources.\n");
2567 goto err_free_resources;
2568 }
2569
2570 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05002571 result = sony_laptop_setup_input(device);
malattia@linux.it33a04452007-04-09 19:26:03 +02002572 if (result) {
2573 printk(KERN_ERR DRV_PFX
2574 "Unabe to create input devices.\n");
2575 goto err_free_resources;
2576 }
2577
Mattia Dongili015a9162007-08-12 16:20:27 +09002578 if (sonypi_compat_init())
2579 goto err_remove_input;
2580
malattia@linux.it33a04452007-04-09 19:26:03 +02002581 /* request io port */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002582 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
2583 if (request_region(io->io1.minimum, io->io1.address_length,
malattia@linux.it33a04452007-04-09 19:26:03 +02002584 "Sony Programable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002585 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
2586 io->io1.minimum, io->io1.maximum,
2587 io->io1.address_length);
2588 /* Type 1 have 2 ioports */
2589 if (io->io2.minimum) {
2590 if (request_region(io->io2.minimum,
2591 io->io2.address_length,
2592 "Sony Programable I/O Device")) {
2593 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
2594 io->io2.minimum, io->io2.maximum,
2595 io->io2.address_length);
2596 spic_dev.cur_ioport = io;
2597 break;
2598 }
2599 else {
2600 dprintk("Unable to get I/O port2: "
2601 "0x%.4x (0x%.4x) + 0x%.2x\n",
2602 io->io2.minimum, io->io2.maximum,
2603 io->io2.address_length);
2604 release_region(io->io1.minimum,
2605 io->io1.address_length);
2606 }
2607 }
2608 else {
2609 spic_dev.cur_ioport = io;
2610 break;
2611 }
malattia@linux.it33a04452007-04-09 19:26:03 +02002612 }
2613 }
2614 if (!spic_dev.cur_ioport) {
2615 printk(KERN_ERR DRV_PFX "Failed to request_region.\n");
2616 result = -ENODEV;
Mattia Dongili015a9162007-08-12 16:20:27 +09002617 goto err_remove_compat;
malattia@linux.it33a04452007-04-09 19:26:03 +02002618 }
2619
2620 /* request IRQ */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002621 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002622 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
2623 IRQF_SHARED, "sony-laptop", &spic_dev)) {
2624 dprintk("IRQ: %d - triggering: %d - "
2625 "polarity: %d - shr: %d\n",
2626 irq->irq.interrupts[0],
2627 irq->irq.triggering,
2628 irq->irq.polarity,
2629 irq->irq.sharable);
2630 spic_dev.cur_irq = irq;
2631 break;
2632 }
2633 }
2634 if (!spic_dev.cur_irq) {
2635 printk(KERN_ERR DRV_PFX "Failed to request_irq.\n");
2636 result = -ENODEV;
2637 goto err_release_region;
2638 }
2639
2640 /* set resource status _SRS */
2641 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2642 if (result) {
2643 printk(KERN_ERR DRV_PFX "Couldn't enable device.\n");
2644 goto err_free_irq;
2645 }
2646
malattia@linux.it49a11de2007-04-09 19:28:56 +02002647 spic_dev.bluetooth_power = -1;
2648 /* create device attributes */
2649 result = sony_pf_add();
2650 if (result)
2651 goto err_disable_device;
2652
2653 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2654 if (result)
2655 goto err_remove_pf;
2656
malattia@linux.it33a04452007-04-09 19:26:03 +02002657 return 0;
2658
malattia@linux.it49a11de2007-04-09 19:28:56 +02002659err_remove_pf:
2660 sony_pf_remove();
2661
2662err_disable_device:
2663 sony_pic_disable(device);
2664
malattia@linux.it33a04452007-04-09 19:26:03 +02002665err_free_irq:
2666 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2667
2668err_release_region:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002669 release_region(spic_dev.cur_ioport->io1.minimum,
2670 spic_dev.cur_ioport->io1.address_length);
2671 if (spic_dev.cur_ioport->io2.minimum)
2672 release_region(spic_dev.cur_ioport->io2.minimum,
2673 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02002674
Mattia Dongili015a9162007-08-12 16:20:27 +09002675err_remove_compat:
2676 sonypi_compat_exit();
2677
malattia@linux.it33a04452007-04-09 19:26:03 +02002678err_remove_input:
malattia@linux.it1549ee62007-04-09 10:19:08 +02002679 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02002680
2681err_free_resources:
2682 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2683 list_del(&io->list);
2684 kfree(io);
2685 }
2686 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2687 list_del(&irq->list);
2688 kfree(irq);
2689 }
2690 spic_dev.cur_ioport = NULL;
2691 spic_dev.cur_irq = NULL;
2692
2693 return result;
2694}
2695
2696static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
2697{
2698 if (sony_pic_disable(device))
2699 return -ENXIO;
2700 return 0;
2701}
2702
2703static int sony_pic_resume(struct acpi_device *device)
2704{
2705 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2706 return 0;
2707}
2708
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002709static const struct acpi_device_id sony_pic_device_ids[] = {
2710 {SONY_PIC_HID, 0},
2711 {"", 0},
2712};
2713
malattia@linux.it33a04452007-04-09 19:26:03 +02002714static struct acpi_driver sony_pic_driver = {
2715 .name = SONY_PIC_DRIVER_NAME,
2716 .class = SONY_PIC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002717 .ids = sony_pic_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02002718 .owner = THIS_MODULE,
2719 .ops = {
2720 .add = sony_pic_add,
2721 .remove = sony_pic_remove,
2722 .suspend = sony_pic_suspend,
2723 .resume = sony_pic_resume,
2724 },
2725};
2726
2727static struct dmi_system_id __initdata sonypi_dmi_table[] = {
2728 {
2729 .ident = "Sony Vaio",
2730 .matches = {
2731 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2732 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
2733 },
2734 },
2735 {
2736 .ident = "Sony Vaio",
2737 .matches = {
2738 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2739 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
2740 },
2741 },
2742 { }
2743};
2744
malattia@linux.it59b19102007-04-09 10:19:04 +02002745static int __init sony_laptop_init(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01002746{
malattia@linux.it33a04452007-04-09 19:26:03 +02002747 int result;
2748
2749 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
2750 result = acpi_bus_register_driver(&sony_pic_driver);
2751 if (result) {
2752 printk(KERN_ERR DRV_PFX
2753 "Unable to register SPIC driver.");
2754 goto out;
2755 }
2756 }
2757
2758 result = acpi_bus_register_driver(&sony_nc_driver);
2759 if (result) {
2760 printk(KERN_ERR DRV_PFX "Unable to register SNC driver.");
2761 goto out_unregister_pic;
2762 }
2763
2764 return 0;
2765
2766out_unregister_pic:
2767 if (!no_spic)
2768 acpi_bus_unregister_driver(&sony_pic_driver);
2769out:
2770 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +01002771}
2772
malattia@linux.it59b19102007-04-09 10:19:04 +02002773static void __exit sony_laptop_exit(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01002774{
malattia@linux.it59b19102007-04-09 10:19:04 +02002775 acpi_bus_unregister_driver(&sony_nc_driver);
malattia@linux.it33a04452007-04-09 19:26:03 +02002776 if (!no_spic)
2777 acpi_bus_unregister_driver(&sony_pic_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +01002778}
2779
malattia@linux.it59b19102007-04-09 10:19:04 +02002780module_init(sony_laptop_init);
2781module_exit(sony_laptop_exit);