blob: 7a32b355869bfa0600eb910fb357a09c057b8655 [file] [log] [blame]
Stelian Pop7f09c432007-01-13 23:04:31 +01001/*
malattia@linux.it33a04452007-04-09 19:26:03 +02002 * ACPI Sony Notebook Control Driver (SNC and SPIC)
Stelian Pop7f09c432007-01-13 23:04:31 +01003 *
4 * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
Mattia Dongilied3aa4b2007-02-07 20:01:54 +01005 * Copyright (C) 2007 Mattia Dongili <malattia@linux.it>
Stelian Pop7f09c432007-01-13 23:04:31 +01006 *
7 * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
8 * which are copyrighted by their respective authors.
9 *
malattia@linux.it33a04452007-04-09 19:26:03 +020010 * The SNY6001 driver part is based on the sonypi driver which includes
11 * material from:
12 *
13 * Copyright (C) 2001-2005 Stelian Pop <stelian@popies.net>
14 *
15 * Copyright (C) 2005 Narayanan R S <nars@kadamba.org>
16 *
Marcin Garskidb955172007-10-19 23:22:11 +020017 * Copyright (C) 2001-2002 AlcĂ´ve <www.alcove.com>
malattia@linux.it33a04452007-04-09 19:26:03 +020018 *
19 * Copyright (C) 2001 Michael Ashley <m.ashley@unsw.edu.au>
20 *
21 * Copyright (C) 2001 Junichi Morita <jun1m@mars.dti.ne.jp>
22 *
23 * Copyright (C) 2000 Takaya Kinjo <t-kinjo@tc4.so-net.ne.jp>
24 *
25 * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
26 *
27 * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
28 *
Stelian Pop7f09c432007-01-13 23:04:31 +010029 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
33 *
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
38 *
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
42 *
43 */
44
45#include <linux/kernel.h>
46#include <linux/module.h>
47#include <linux/moduleparam.h>
48#include <linux/init.h>
49#include <linux/types.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010050#include <linux/backlight.h>
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010051#include <linux/platform_device.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010052#include <linux/err.h>
malattia@linux.it33a04452007-04-09 19:26:03 +020053#include <linux/dmi.h>
54#include <linux/pci.h>
55#include <linux/interrupt.h>
56#include <linux/delay.h>
57#include <linux/input.h>
58#include <linux/kfifo.h>
59#include <linux/workqueue.h>
60#include <linux/acpi.h>
Stelian Pop7f09c432007-01-13 23:04:31 +010061#include <acpi/acpi_drivers.h>
62#include <acpi/acpi_bus.h>
63#include <asm/uaccess.h>
malattia@linux.it33a04452007-04-09 19:26:03 +020064#include <linux/sonypi.h>
malattia@linux.it1ce82c12007-04-28 23:34:36 +090065#include <linux/sony-laptop.h>
Mattia Dongilia64e62a2007-05-01 11:19:53 +090066#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +020067#include <linux/poll.h>
68#include <linux/miscdevice.h>
69#endif
Stelian Pop7f09c432007-01-13 23:04:31 +010070
malattia@linux.it33a04452007-04-09 19:26:03 +020071#define DRV_PFX "sony-laptop: "
malattia@linux.itb9a218b2007-04-09 10:19:06 +020072#define dprintk(msg...) do { \
malattia@linux.itf6119b02007-04-09 19:31:06 +020073 if (debug) printk(KERN_WARNING DRV_PFX msg); \
malattia@linux.itb9a218b2007-04-09 10:19:06 +020074} while (0)
75
malattia@linux.it33a04452007-04-09 19:26:03 +020076#define SONY_LAPTOP_DRIVER_VERSION "0.5"
Alessandro Guido50f62af2007-01-13 23:04:34 +010077
malattia@linux.it33a04452007-04-09 19:26:03 +020078#define SONY_NC_CLASS "sony-nc"
79#define SONY_NC_HID "SNY5001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020080#define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
malattia@linux.it33a04452007-04-09 19:26:03 +020081
82#define SONY_PIC_CLASS "sony-pic"
83#define SONY_PIC_HID "SNY6001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020084#define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
Stelian Pop7f09c432007-01-13 23:04:31 +010085
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010086MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
malattia@linux.it33a04452007-04-09 19:26:03 +020087MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
Stelian Pop7f09c432007-01-13 23:04:31 +010088MODULE_LICENSE("GPL");
malattia@linux.it33a04452007-04-09 19:26:03 +020089MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
Stelian Pop7f09c432007-01-13 23:04:31 +010090
91static int debug;
92module_param(debug, int, 0);
93MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
Len Browna02d1c12007-02-07 15:34:02 -050094 "the development of this driver");
Stelian Pop7f09c432007-01-13 23:04:31 +010095
malattia@linux.it33a04452007-04-09 19:26:03 +020096static int no_spic; /* = 0 */
97module_param(no_spic, int, 0444);
98MODULE_PARM_DESC(no_spic,
99 "set this if you don't want to enable the SPIC device");
100
101static int compat; /* = 0 */
102module_param(compat, int, 0444);
103MODULE_PARM_DESC(compat,
malattia@linux.it7b153f32007-04-09 19:31:25 +0200104 "set this if you want to enable backward compatibility mode");
malattia@linux.it33a04452007-04-09 19:26:03 +0200105
106static unsigned long mask = 0xffffffff;
107module_param(mask, ulong, 0644);
108MODULE_PARM_DESC(mask,
109 "set this to the mask of event you want to enable (see doc)");
110
malattia@linux.it5f3d2892007-04-28 23:18:45 +0900111static int camera; /* = 0 */
112module_param(camera, int, 0444);
113MODULE_PARM_DESC(camera,
114 "set this to 1 to enable Motion Eye camera controls "
115 "(only use it if you have a C1VE or C1VN model)");
116
Mattia Dongilia64e62a2007-05-01 11:19:53 +0900117#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +0200118static int minor = -1;
119module_param(minor, int, 0);
120MODULE_PARM_DESC(minor,
121 "minor number of the misc device for the SPIC compatibility code, "
122 "default is -1 (automatic)");
123#endif
124
malattia@linux.it1549ee62007-04-09 10:19:08 +0200125/*********** Input Devices ***********/
126
127#define SONY_LAPTOP_BUF_SIZE 128
128struct sony_laptop_input_s {
129 atomic_t users;
130 struct input_dev *jog_dev;
131 struct input_dev *key_dev;
132 struct kfifo *fifo;
133 spinlock_t fifo_lock;
134 struct workqueue_struct *wq;
135};
136static struct sony_laptop_input_s sony_laptop_input = {
137 .users = ATOMIC_INIT(0),
138};
139
140struct sony_laptop_keypress {
141 struct input_dev *dev;
142 int key;
143};
144
Mattia Dongilibc57f862007-07-20 02:01:57 +0900145/* Correspondance table between sonypi events
146 * and input layer indexes in the keymap
147 */
148static int sony_laptop_input_index[] = {
149 -1, /* no event */
150 -1, /* SONYPI_EVENT_JOGDIAL_DOWN */
151 -1, /* SONYPI_EVENT_JOGDIAL_UP */
152 -1, /* SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
153 -1, /* SONYPI_EVENT_JOGDIAL_UP_PRESSED */
154 -1, /* SONYPI_EVENT_JOGDIAL_PRESSED */
155 -1, /* SONYPI_EVENT_JOGDIAL_RELEASED */
156 0, /* SONYPI_EVENT_CAPTURE_PRESSED */
157 1, /* SONYPI_EVENT_CAPTURE_RELEASED */
158 2, /* SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
159 3, /* SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
160 4, /* SONYPI_EVENT_FNKEY_ESC */
161 5, /* SONYPI_EVENT_FNKEY_F1 */
162 6, /* SONYPI_EVENT_FNKEY_F2 */
163 7, /* SONYPI_EVENT_FNKEY_F3 */
164 8, /* SONYPI_EVENT_FNKEY_F4 */
165 9, /* SONYPI_EVENT_FNKEY_F5 */
166 10, /* SONYPI_EVENT_FNKEY_F6 */
167 11, /* SONYPI_EVENT_FNKEY_F7 */
168 12, /* SONYPI_EVENT_FNKEY_F8 */
169 13, /* SONYPI_EVENT_FNKEY_F9 */
170 14, /* SONYPI_EVENT_FNKEY_F10 */
171 15, /* SONYPI_EVENT_FNKEY_F11 */
172 16, /* SONYPI_EVENT_FNKEY_F12 */
173 17, /* SONYPI_EVENT_FNKEY_1 */
174 18, /* SONYPI_EVENT_FNKEY_2 */
175 19, /* SONYPI_EVENT_FNKEY_D */
176 20, /* SONYPI_EVENT_FNKEY_E */
177 21, /* SONYPI_EVENT_FNKEY_F */
178 22, /* SONYPI_EVENT_FNKEY_S */
179 23, /* SONYPI_EVENT_FNKEY_B */
180 24, /* SONYPI_EVENT_BLUETOOTH_PRESSED */
181 25, /* SONYPI_EVENT_PKEY_P1 */
182 26, /* SONYPI_EVENT_PKEY_P2 */
183 27, /* SONYPI_EVENT_PKEY_P3 */
184 28, /* SONYPI_EVENT_BACK_PRESSED */
185 -1, /* SONYPI_EVENT_LID_CLOSED */
186 -1, /* SONYPI_EVENT_LID_OPENED */
187 29, /* SONYPI_EVENT_BLUETOOTH_ON */
188 30, /* SONYPI_EVENT_BLUETOOTH_OFF */
189 31, /* SONYPI_EVENT_HELP_PRESSED */
190 32, /* SONYPI_EVENT_FNKEY_ONLY */
191 33, /* SONYPI_EVENT_JOGDIAL_FAST_DOWN */
192 34, /* SONYPI_EVENT_JOGDIAL_FAST_UP */
193 35, /* SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
194 36, /* SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
195 37, /* SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
196 38, /* SONYPI_EVENT_JOGDIAL_VFAST_UP */
197 39, /* SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
198 40, /* SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
199 41, /* SONYPI_EVENT_ZOOM_PRESSED */
200 42, /* SONYPI_EVENT_THUMBPHRASE_PRESSED */
201 43, /* SONYPI_EVENT_MEYE_FACE */
202 44, /* SONYPI_EVENT_MEYE_OPPOSITE */
203 45, /* SONYPI_EVENT_MEMORYSTICK_INSERT */
204 46, /* SONYPI_EVENT_MEMORYSTICK_EJECT */
205 -1, /* SONYPI_EVENT_ANYBUTTON_RELEASED */
206 -1, /* SONYPI_EVENT_BATTERY_INSERT */
207 -1, /* SONYPI_EVENT_BATTERY_REMOVE */
208 -1, /* SONYPI_EVENT_FNKEY_RELEASED */
209 47, /* SONYPI_EVENT_WIRELESS_ON */
210 48, /* SONYPI_EVENT_WIRELESS_OFF */
211};
212
213static int sony_laptop_input_keycode_map[] = {
214 KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
215 KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
216 KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
217 KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
218 KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
219 KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
220 KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
221 KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
222 KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
223 KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
224 KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
225 KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
226 KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
227 KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
228 KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
229 KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
230 KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
231 KEY_FN_F1, /* 17 SONYPI_EVENT_FNKEY_1 */
232 KEY_FN_F2, /* 18 SONYPI_EVENT_FNKEY_2 */
233 KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
234 KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
235 KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
236 KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
237 KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
238 KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
239 KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
240 KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
241 KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
242 KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
243 KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
244 KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
245 KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
246 KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
247 KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
248 KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
249 KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
250 KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
251 KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
252 KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
253 KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
254 KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
255 KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
256 BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
257 KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
258 KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
259 KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
260 KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
261 KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
262 KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200263};
264
265/* release buttons after a short delay if pressed */
266static void do_sony_laptop_release_key(struct work_struct *work)
267{
268 struct sony_laptop_keypress kp;
269
270 while (kfifo_get(sony_laptop_input.fifo, (unsigned char *)&kp,
271 sizeof(kp)) == sizeof(kp)) {
272 msleep(10);
273 input_report_key(kp.dev, kp.key, 0);
274 input_sync(kp.dev);
275 }
276}
277static DECLARE_WORK(sony_laptop_release_key_work,
278 do_sony_laptop_release_key);
279
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200280/* forward event to the input subsystem */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200281static void sony_laptop_report_input_event(u8 event)
282{
283 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
284 struct input_dev *key_dev = sony_laptop_input.key_dev;
285 struct sony_laptop_keypress kp = { NULL };
malattia@linux.it1549ee62007-04-09 10:19:08 +0200286
287 if (event == SONYPI_EVENT_FNKEY_RELEASED) {
288 /* Nothing, not all VAIOs generate this event */
289 return;
290 }
291
292 /* report events */
293 switch (event) {
294 /* jog_dev events */
295 case SONYPI_EVENT_JOGDIAL_UP:
296 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
297 input_report_rel(jog_dev, REL_WHEEL, 1);
298 input_sync(jog_dev);
299 return;
300
301 case SONYPI_EVENT_JOGDIAL_DOWN:
302 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
303 input_report_rel(jog_dev, REL_WHEEL, -1);
304 input_sync(jog_dev);
305 return;
306
307 /* key_dev events */
308 case SONYPI_EVENT_JOGDIAL_PRESSED:
309 kp.key = BTN_MIDDLE;
310 kp.dev = jog_dev;
311 break;
312
313 default:
Mattia Dongilibc57f862007-07-20 02:01:57 +0900314 if (event > ARRAY_SIZE (sony_laptop_input_keycode_map)) {
315 dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
316 break;
317 }
318 if (sony_laptop_input_index[event] != -1) {
319 kp.key = sony_laptop_input_keycode_map[sony_laptop_input_index[event]];
320 if (kp.key != KEY_UNKNOWN)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200321 kp.dev = key_dev;
Mattia Dongilibc57f862007-07-20 02:01:57 +0900322 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200323 break;
324 }
325
326 if (kp.dev) {
327 input_report_key(kp.dev, kp.key, 1);
Mattia Dongilibc57f862007-07-20 02:01:57 +0900328 /* we emit the scancode so we can always remap the key */
329 input_event(kp.dev, EV_MSC, MSC_SCAN, event);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200330 input_sync(kp.dev);
331 kfifo_put(sony_laptop_input.fifo,
332 (unsigned char *)&kp, sizeof(kp));
333
334 if (!work_pending(&sony_laptop_release_key_work))
335 queue_work(sony_laptop_input.wq,
336 &sony_laptop_release_key_work);
337 } else
338 dprintk("unknown input event %.2x\n", event);
339}
340
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500341static int sony_laptop_setup_input(struct acpi_device *acpi_device)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200342{
343 struct input_dev *jog_dev;
344 struct input_dev *key_dev;
345 int i;
346 int error;
347
348 /* don't run again if already initialized */
349 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
350 return 0;
351
352 /* kfifo */
353 spin_lock_init(&sony_laptop_input.fifo_lock);
354 sony_laptop_input.fifo =
355 kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
356 &sony_laptop_input.fifo_lock);
357 if (IS_ERR(sony_laptop_input.fifo)) {
358 printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
359 error = PTR_ERR(sony_laptop_input.fifo);
360 goto err_dec_users;
361 }
362
363 /* init workqueue */
364 sony_laptop_input.wq = create_singlethread_workqueue("sony-laptop");
365 if (!sony_laptop_input.wq) {
366 printk(KERN_ERR DRV_PFX
367 "Unabe to create workqueue.\n");
368 error = -ENXIO;
369 goto err_free_kfifo;
370 }
371
372 /* input keys */
373 key_dev = input_allocate_device();
374 if (!key_dev) {
375 error = -ENOMEM;
376 goto err_destroy_wq;
377 }
378
379 key_dev->name = "Sony Vaio Keys";
380 key_dev->id.bustype = BUS_ISA;
381 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500382 key_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200383
384 /* Initialize the Input Drivers: special keys */
Mattia Dongilibc57f862007-07-20 02:01:57 +0900385 set_bit(EV_KEY, key_dev->evbit);
386 set_bit(EV_MSC, key_dev->evbit);
387 set_bit(MSC_SCAN, key_dev->mscbit);
388 key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
389 key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
390 key_dev->keycode = &sony_laptop_input_keycode_map;
391 for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++) {
392 if (sony_laptop_input_keycode_map[i] != KEY_RESERVED) {
393 set_bit(sony_laptop_input_keycode_map[i],
394 key_dev->keybit);
395 }
396 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200397
398 error = input_register_device(key_dev);
399 if (error)
400 goto err_free_keydev;
401
402 sony_laptop_input.key_dev = key_dev;
403
404 /* jogdial */
405 jog_dev = input_allocate_device();
406 if (!jog_dev) {
407 error = -ENOMEM;
408 goto err_unregister_keydev;
409 }
410
411 jog_dev->name = "Sony Vaio Jogdial";
412 jog_dev->id.bustype = BUS_ISA;
413 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500414 key_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200415
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700416 jog_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
417 jog_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_MIDDLE);
418 jog_dev->relbit[0] = BIT_MASK(REL_WHEEL);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200419
420 error = input_register_device(jog_dev);
421 if (error)
422 goto err_free_jogdev;
423
424 sony_laptop_input.jog_dev = jog_dev;
425
426 return 0;
427
428err_free_jogdev:
429 input_free_device(jog_dev);
430
431err_unregister_keydev:
432 input_unregister_device(key_dev);
433 /* to avoid kref underflow below at input_free_device */
434 key_dev = NULL;
435
436err_free_keydev:
437 input_free_device(key_dev);
438
439err_destroy_wq:
440 destroy_workqueue(sony_laptop_input.wq);
441
442err_free_kfifo:
443 kfifo_free(sony_laptop_input.fifo);
444
445err_dec_users:
446 atomic_dec(&sony_laptop_input.users);
447 return error;
448}
449
450static void sony_laptop_remove_input(void)
451{
452 /* cleanup only after the last user has gone */
453 if (!atomic_dec_and_test(&sony_laptop_input.users))
454 return;
455
456 /* flush workqueue first */
457 flush_workqueue(sony_laptop_input.wq);
458
459 /* destroy input devs */
460 input_unregister_device(sony_laptop_input.key_dev);
461 sony_laptop_input.key_dev = NULL;
462
463 if (sony_laptop_input.jog_dev) {
464 input_unregister_device(sony_laptop_input.jog_dev);
465 sony_laptop_input.jog_dev = NULL;
466 }
467
468 destroy_workqueue(sony_laptop_input.wq);
469 kfifo_free(sony_laptop_input.fifo);
470}
471
malattia@linux.it56b87562007-04-09 10:19:05 +0200472/*********** Platform Device ***********/
473
474static atomic_t sony_pf_users = ATOMIC_INIT(0);
475static struct platform_driver sony_pf_driver = {
476 .driver = {
477 .name = "sony-laptop",
478 .owner = THIS_MODULE,
479 }
480};
481static struct platform_device *sony_pf_device;
482
483static int sony_pf_add(void)
484{
485 int ret = 0;
486
487 /* don't run again if already initialized */
488 if (atomic_add_return(1, &sony_pf_users) > 1)
489 return 0;
490
491 ret = platform_driver_register(&sony_pf_driver);
492 if (ret)
493 goto out;
494
495 sony_pf_device = platform_device_alloc("sony-laptop", -1);
496 if (!sony_pf_device) {
497 ret = -ENOMEM;
498 goto out_platform_registered;
499 }
500
501 ret = platform_device_add(sony_pf_device);
502 if (ret)
503 goto out_platform_alloced;
504
505 return 0;
506
507 out_platform_alloced:
508 platform_device_put(sony_pf_device);
509 sony_pf_device = NULL;
510 out_platform_registered:
511 platform_driver_unregister(&sony_pf_driver);
512 out:
513 atomic_dec(&sony_pf_users);
514 return ret;
515}
516
517static void sony_pf_remove(void)
518{
519 /* deregister only after the last user has gone */
520 if (!atomic_dec_and_test(&sony_pf_users))
521 return;
522
523 platform_device_del(sony_pf_device);
524 platform_device_put(sony_pf_device);
525 platform_driver_unregister(&sony_pf_driver);
526}
527
528/*********** SNC (SNY5001) Device ***********/
529
malattia@linux.it33a04452007-04-09 19:26:03 +0200530/* the device uses 1-based values, while the backlight subsystem uses
531 0-based values */
532#define SONY_MAX_BRIGHTNESS 8
533
534#define SNC_VALIDATE_IN 0
535#define SNC_VALIDATE_OUT 1
536
malattia@linux.it59b19102007-04-09 10:19:04 +0200537static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500538 char *);
malattia@linux.it59b19102007-04-09 10:19:04 +0200539static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500540 const char *, size_t);
Mattia Dongili156c2212007-02-12 22:01:07 +0100541static int boolean_validate(const int, const int);
542static int brightness_default_validate(const int, const int);
543
malattia@linux.it59b19102007-04-09 10:19:04 +0200544struct sony_nc_value {
Len Browna02d1c12007-02-07 15:34:02 -0500545 char *name; /* name of the entry */
546 char **acpiget; /* names of the ACPI get function */
547 char **acpiset; /* names of the ACPI set function */
Mattia Dongili156c2212007-02-12 22:01:07 +0100548 int (*validate)(const int, const int); /* input/output validation */
Len Browna02d1c12007-02-07 15:34:02 -0500549 int value; /* current setting */
550 int valid; /* Has ever been set */
551 int debug; /* active only in debug mode ? */
552 struct device_attribute devattr; /* sysfs atribute */
Stelian Pop7f09c432007-01-13 23:04:31 +0100553};
554
malattia@linux.it59b19102007-04-09 10:19:04 +0200555#define SNC_HANDLE_NAMES(_name, _values...) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100556 static char *snc_##_name[] = { _values, NULL }
557
malattia@linux.it59b19102007-04-09 10:19:04 +0200558#define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100559 { \
560 .name = __stringify(_name), \
561 .acpiget = _getters, \
562 .acpiset = _setters, \
Mattia Dongili156c2212007-02-12 22:01:07 +0100563 .validate = _validate, \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100564 .debug = _debug, \
malattia@linux.it59b19102007-04-09 10:19:04 +0200565 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100566 }
567
malattia@linux.it59b19102007-04-09 10:19:04 +0200568#define SNC_HANDLE_NULL { .name = NULL }
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100569
malattia@linux.it59b19102007-04-09 10:19:04 +0200570SNC_HANDLE_NAMES(fnkey_get, "GHKE");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100571
malattia@linux.it59b19102007-04-09 10:19:04 +0200572SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
573SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100574
malattia@linux.it59b19102007-04-09 10:19:04 +0200575SNC_HANDLE_NAMES(cdpower_get, "GCDP");
576SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100577
malattia@linux.it59b19102007-04-09 10:19:04 +0200578SNC_HANDLE_NAMES(audiopower_get, "GAZP");
579SNC_HANDLE_NAMES(audiopower_set, "AZPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100580
malattia@linux.it59b19102007-04-09 10:19:04 +0200581SNC_HANDLE_NAMES(lanpower_get, "GLNP");
582SNC_HANDLE_NAMES(lanpower_set, "LNPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100583
Mattia Dongili044847e2007-07-16 02:34:33 +0900584SNC_HANDLE_NAMES(lidstate_get, "GLID");
585
586SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
587SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
588
589SNC_HANDLE_NAMES(gainbass_get, "GMGB");
590SNC_HANDLE_NAMES(gainbass_set, "CMGB");
591
malattia@linux.it59b19102007-04-09 10:19:04 +0200592SNC_HANDLE_NAMES(PID_get, "GPID");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100593
malattia@linux.it59b19102007-04-09 10:19:04 +0200594SNC_HANDLE_NAMES(CTR_get, "GCTR");
595SNC_HANDLE_NAMES(CTR_set, "SCTR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100596
malattia@linux.it59b19102007-04-09 10:19:04 +0200597SNC_HANDLE_NAMES(PCR_get, "GPCR");
598SNC_HANDLE_NAMES(PCR_set, "SPCR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100599
malattia@linux.it59b19102007-04-09 10:19:04 +0200600SNC_HANDLE_NAMES(CMI_get, "GCMI");
601SNC_HANDLE_NAMES(CMI_set, "SCMI");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100602
malattia@linux.it59b19102007-04-09 10:19:04 +0200603static struct sony_nc_value sony_nc_values[] = {
604 SNC_HANDLE(brightness_default, snc_brightness_def_get,
Mattia Dongili156c2212007-02-12 22:01:07 +0100605 snc_brightness_def_set, brightness_default_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200606 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
607 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
608 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100609 boolean_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200610 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100611 boolean_validate, 1),
Mattia Dongili044847e2007-07-16 02:34:33 +0900612 SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
613 boolean_validate, 0),
614 SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
615 boolean_validate, 0),
616 SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
617 boolean_validate, 0),
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100618 /* unknown methods */
malattia@linux.it59b19102007-04-09 10:19:04 +0200619 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
620 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
621 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
622 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
623 SNC_HANDLE_NULL
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100624};
625
malattia@linux.it59b19102007-04-09 10:19:04 +0200626static acpi_handle sony_nc_acpi_handle;
627static struct acpi_device *sony_nc_acpi_device = NULL;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100628
Mattia Dongilid78865c2007-02-07 20:01:56 +0100629/*
630 * acpi_evaluate_object wrappers
631 */
Stelian Pop7f09c432007-01-13 23:04:31 +0100632static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
633{
634 struct acpi_buffer output;
635 union acpi_object out_obj;
636 acpi_status status;
637
638 output.length = sizeof(out_obj);
639 output.pointer = &out_obj;
640
641 status = acpi_evaluate_object(handle, name, NULL, &output);
642 if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
643 *result = out_obj.integer.value;
644 return 0;
645 }
646
malattia@linux.itf6119b02007-04-09 19:31:06 +0200647 printk(KERN_WARNING DRV_PFX "acpi_callreadfunc failed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100648
649 return -1;
650}
651
652static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
653 int *result)
654{
655 struct acpi_object_list params;
656 union acpi_object in_obj;
657 struct acpi_buffer output;
658 union acpi_object out_obj;
659 acpi_status status;
660
661 params.count = 1;
662 params.pointer = &in_obj;
663 in_obj.type = ACPI_TYPE_INTEGER;
664 in_obj.integer.value = value;
665
666 output.length = sizeof(out_obj);
667 output.pointer = &out_obj;
668
669 status = acpi_evaluate_object(handle, name, &params, &output);
670 if (status == AE_OK) {
671 if (result != NULL) {
672 if (out_obj.type != ACPI_TYPE_INTEGER) {
malattia@linux.itf6119b02007-04-09 19:31:06 +0200673 printk(KERN_WARNING DRV_PFX "acpi_evaluate_object bad "
Stelian Pop7f09c432007-01-13 23:04:31 +0100674 "return type\n");
675 return -1;
676 }
677 *result = out_obj.integer.value;
678 }
679 return 0;
680 }
681
malattia@linux.itf6119b02007-04-09 19:31:06 +0200682 printk(KERN_WARNING DRV_PFX "acpi_evaluate_object failed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100683
684 return -1;
685}
686
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100687/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200688 * sony_nc_values input/output validate functions
Mattia Dongili156c2212007-02-12 22:01:07 +0100689 */
690
691/* brightness_default_validate:
692 *
693 * manipulate input output values to keep consistency with the
694 * backlight framework for which brightness values are 0-based.
695 */
696static int brightness_default_validate(const int direction, const int value)
697{
698 switch (direction) {
699 case SNC_VALIDATE_OUT:
700 return value - 1;
701 case SNC_VALIDATE_IN:
702 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
703 return value + 1;
704 }
705 return -EINVAL;
706}
707
708/* boolean_validate:
709 *
710 * on input validate boolean values 0/1, on output just pass the
711 * received value.
712 */
713static int boolean_validate(const int direction, const int value)
714{
715 if (direction == SNC_VALIDATE_IN) {
716 if (value != 0 && value != 1)
717 return -EINVAL;
718 }
719 return value;
720}
721
722/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200723 * Sysfs show/store common to all sony_nc_values
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100724 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200725static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
Len Browna02d1c12007-02-07 15:34:02 -0500726 char *buffer)
Stelian Pop7f09c432007-01-13 23:04:31 +0100727{
Stelian Pop7f09c432007-01-13 23:04:31 +0100728 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200729 struct sony_nc_value *item =
730 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100731
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100732 if (!*item->acpiget)
Stelian Pop7f09c432007-01-13 23:04:31 +0100733 return -EIO;
734
malattia@linux.it59b19102007-04-09 10:19:04 +0200735 if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100736 return -EIO;
737
Mattia Dongili156c2212007-02-12 22:01:07 +0100738 if (item->validate)
739 value = item->validate(SNC_VALIDATE_OUT, value);
740
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100741 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
Stelian Pop7f09c432007-01-13 23:04:31 +0100742}
743
malattia@linux.it59b19102007-04-09 10:19:04 +0200744static ssize_t sony_nc_sysfs_store(struct device *dev,
Len Browna02d1c12007-02-07 15:34:02 -0500745 struct device_attribute *attr,
746 const char *buffer, size_t count)
Stelian Pop7f09c432007-01-13 23:04:31 +0100747{
Stelian Pop7f09c432007-01-13 23:04:31 +0100748 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200749 struct sony_nc_value *item =
750 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100751
752 if (!item->acpiset)
753 return -EIO;
754
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100755 if (count > 31)
756 return -EINVAL;
757
758 value = simple_strtoul(buffer, NULL, 10);
Stelian Pop7f09c432007-01-13 23:04:31 +0100759
Mattia Dongili156c2212007-02-12 22:01:07 +0100760 if (item->validate)
761 value = item->validate(SNC_VALIDATE_IN, value);
762
763 if (value < 0)
764 return value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100765
malattia@linux.it59b19102007-04-09 10:19:04 +0200766 if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100767 return -EIO;
Andrew Morton3f4f4612007-01-13 23:04:32 +0100768 item->value = value;
769 item->valid = 1;
Stelian Pop7f09c432007-01-13 23:04:31 +0100770 return count;
771}
772
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100773
Mattia Dongilid78865c2007-02-07 20:01:56 +0100774/*
775 * Backlight device
776 */
777static int sony_backlight_update_status(struct backlight_device *bd)
Andrew Morton3f4f4612007-01-13 23:04:32 +0100778{
malattia@linux.it59b19102007-04-09 10:19:04 +0200779 return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
Richard Purdie321709c2007-02-10 15:04:08 +0000780 bd->props.brightness + 1, NULL);
Andrew Morton3f4f4612007-01-13 23:04:32 +0100781}
782
Mattia Dongilid78865c2007-02-07 20:01:56 +0100783static int sony_backlight_get_brightness(struct backlight_device *bd)
784{
785 int value;
786
malattia@linux.it59b19102007-04-09 10:19:04 +0200787 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
Mattia Dongilid78865c2007-02-07 20:01:56 +0100788 return 0;
789 /* brightness levels are 1-based, while backlight ones are 0-based */
790 return value - 1;
791}
792
793static struct backlight_device *sony_backlight_device;
Richard Purdie321709c2007-02-10 15:04:08 +0000794static struct backlight_ops sony_backlight_ops = {
Len Browna02d1c12007-02-07 15:34:02 -0500795 .update_status = sony_backlight_update_status,
796 .get_brightness = sony_backlight_get_brightness,
Mattia Dongilid78865c2007-02-07 20:01:56 +0100797};
798
799/*
Mattia Dongili6315fd12007-07-16 02:34:35 +0900800 * New SNC-only Vaios event mapping to driver known keys
801 */
802struct sony_nc_event {
803 u8 data;
804 u8 event;
805};
806
807static struct sony_nc_event *sony_nc_events;
808
809/* Vaio C* --maybe also FE*, N* and AR* ?-- special init sequence
810 * for Fn keys
811 */
Jeff Garzik18552562007-10-03 15:15:40 -0400812static int sony_nc_C_enable(const struct dmi_system_id *id)
Mattia Dongili6315fd12007-07-16 02:34:35 +0900813{
814 int result = 0;
815
816 printk(KERN_NOTICE DRV_PFX "detected %s\n", id->ident);
817
818 sony_nc_events = id->driver_data;
819
820 if (acpi_callsetfunc(sony_nc_acpi_handle, "SN02", 0x4, &result) < 0
821 || acpi_callsetfunc(sony_nc_acpi_handle, "SN07", 0x2, &result) < 0
822 || acpi_callsetfunc(sony_nc_acpi_handle, "SN02", 0x10, &result) < 0
823 || acpi_callsetfunc(sony_nc_acpi_handle, "SN07", 0x0, &result) < 0
824 || acpi_callsetfunc(sony_nc_acpi_handle, "SN03", 0x2, &result) < 0
825 || acpi_callsetfunc(sony_nc_acpi_handle, "SN07", 0x101, &result) < 0) {
826 printk(KERN_WARNING DRV_PFX "failed to initialize SNC, some "
827 "functionalities may be missing\n");
828 return 1;
829 }
830 return 0;
831}
832
833static struct sony_nc_event sony_C_events[] = {
834 { 0x81, SONYPI_EVENT_FNKEY_F1 },
835 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
836 { 0x85, SONYPI_EVENT_FNKEY_F5 },
837 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
838 { 0x86, SONYPI_EVENT_FNKEY_F6 },
839 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
840 { 0x87, SONYPI_EVENT_FNKEY_F7 },
841 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
842 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
843 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
844 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
845 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
846 { 0, 0 },
847};
848
849/* SNC-only model map */
Jeff Garzik18552562007-10-03 15:15:40 -0400850static const struct dmi_system_id sony_nc_ids[] = {
Mattia Dongili6315fd12007-07-16 02:34:35 +0900851 {
Mattia Dongili89892d12007-07-16 02:34:38 +0900852 .ident = "Sony Vaio FE Series",
853 .callback = sony_nc_C_enable,
854 .driver_data = sony_C_events,
855 .matches = {
856 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
857 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FE"),
858 },
859 },
860 {
Mattia Dongilif46d1602007-08-12 16:20:26 +0900861 .ident = "Sony Vaio FZ Series",
862 .callback = sony_nc_C_enable,
863 .driver_data = sony_C_events,
864 .matches = {
865 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
866 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FZ"),
867 },
868 },
869 {
Mattia Dongili6315fd12007-07-16 02:34:35 +0900870 .ident = "Sony Vaio C Series",
871 .callback = sony_nc_C_enable,
872 .driver_data = sony_C_events,
873 .matches = {
874 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
875 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-C"),
876 },
877 },
Mattia Dongili88877c22008-01-14 18:05:42 +0900878 {
879 .ident = "Sony Vaio N Series",
880 .callback = sony_nc_C_enable,
881 .driver_data = sony_C_events,
882 .matches = {
883 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
884 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-N"),
885 },
886 },
Mattia Dongili6315fd12007-07-16 02:34:35 +0900887 { }
888};
889
890/*
Mattia Dongilid78865c2007-02-07 20:01:56 +0100891 * ACPI callbacks
892 */
Stelian Pop7f09c432007-01-13 23:04:31 +0100893static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
894{
Mattia Dongili6315fd12007-07-16 02:34:35 +0900895 struct sony_nc_event *evmap;
896 u32 ev = event;
897 int result;
898
899 if (ev == 0x92) {
900 /* read the key pressed from EC.GECR
901 * A call to SN07 with 0x0202 will do it as well respecting
902 * the current protocol on different OSes
903 *
904 * Note: the path for GECR may be
905 * \_SB.PCI0.LPCB.EC (C, FE, AR, N and friends)
906 * \_SB.PCI0.PIB.EC0 (VGN-FR notifications are sent directly, no GECR)
907 *
908 * TODO: we may want to do the same for the older GHKE -need
909 * dmi list- so this snippet may become one more callback.
910 */
911 if (acpi_callsetfunc(handle, "SN07", 0x0202, &result) < 0)
912 dprintk("sony_acpi_notify, unable to decode event 0x%.2x\n", ev);
913 else
914 ev = result & 0xFF;
915 }
916
917 if (sony_nc_events)
918 for (evmap = sony_nc_events; evmap->event; evmap++) {
919 if (evmap->data == ev) {
920 ev = evmap->event;
921 break;
922 }
923 }
924
925 dprintk("sony_acpi_notify, event: 0x%.2x\n", ev);
926 sony_laptop_report_input_event(ev);
Len Brown14e04fb2007-08-23 15:20:26 -0400927 acpi_bus_generate_proc_event(sony_nc_acpi_device, 1, ev);
Stelian Pop7f09c432007-01-13 23:04:31 +0100928}
929
930static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
931 void *context, void **return_value)
932{
933 struct acpi_namespace_node *node;
934 union acpi_operand_object *operand;
935
Len Browna02d1c12007-02-07 15:34:02 -0500936 node = (struct acpi_namespace_node *)handle;
937 operand = (union acpi_operand_object *)node->object;
Stelian Pop7f09c432007-01-13 23:04:31 +0100938
malattia@linux.itf6119b02007-04-09 19:31:06 +0200939 printk(KERN_WARNING DRV_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
Stelian Pop7f09c432007-01-13 23:04:31 +0100940 (u32) operand->method.param_count);
941
942 return AE_OK;
943}
944
Mattia Dongilid78865c2007-02-07 20:01:56 +0100945/*
946 * ACPI device
947 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200948static int sony_nc_resume(struct acpi_device *device)
Mattia Dongilid78865c2007-02-07 20:01:56 +0100949{
malattia@linux.it59b19102007-04-09 10:19:04 +0200950 struct sony_nc_value *item;
Mattia Dongilid78865c2007-02-07 20:01:56 +0100951
malattia@linux.it59b19102007-04-09 10:19:04 +0200952 for (item = sony_nc_values; item->name; item++) {
Mattia Dongilid78865c2007-02-07 20:01:56 +0100953 int ret;
954
955 if (!item->valid)
956 continue;
malattia@linux.it59b19102007-04-09 10:19:04 +0200957 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
Len Browna02d1c12007-02-07 15:34:02 -0500958 item->value, NULL);
Mattia Dongilid78865c2007-02-07 20:01:56 +0100959 if (ret < 0) {
960 printk("%s: %d\n", __FUNCTION__, ret);
961 break;
962 }
963 }
Mattia Dongili6315fd12007-07-16 02:34:35 +0900964
Mattia Dongilie84a02b2007-08-04 00:22:30 +0900965 /* set the last requested brightness level */
966 if (sony_backlight_device &&
967 !sony_backlight_update_status(sony_backlight_device))
968 printk(KERN_WARNING DRV_PFX "unable to restore brightness level");
969
Mattia Dongili6315fd12007-07-16 02:34:35 +0900970 /* re-initialize models with specific requirements */
971 dmi_check_system(sony_nc_ids);
972
Mattia Dongilid78865c2007-02-07 20:01:56 +0100973 return 0;
974}
975
malattia@linux.it59b19102007-04-09 10:19:04 +0200976static int sony_nc_add(struct acpi_device *device)
Stelian Pop7f09c432007-01-13 23:04:31 +0100977{
978 acpi_status status;
Andrew Morton8607c672007-03-06 02:29:42 -0800979 int result = 0;
Alessandro Guido50f62af2007-01-13 23:04:34 +0100980 acpi_handle handle;
malattia@linux.it56b87562007-04-09 10:19:05 +0200981 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +0100982
malattia@linux.itf6119b02007-04-09 19:31:06 +0200983 printk(KERN_INFO DRV_PFX "%s v%s.\n",
984 SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
985
malattia@linux.it59b19102007-04-09 10:19:04 +0200986 sony_nc_acpi_device = device;
malattia@linux.it33a04452007-04-09 19:26:03 +0200987 strcpy(acpi_device_class(device), "sony/hotkey");
Stelian Popc5611622007-01-13 23:04:37 +0100988
malattia@linux.it59b19102007-04-09 10:19:04 +0200989 sony_nc_acpi_handle = device->handle;
Stelian Pop7f09c432007-01-13 23:04:31 +0100990
Mattia Dongilib25b7322007-07-16 02:34:36 +0900991 /* read device status */
992 result = acpi_bus_get_status(device);
993 /* bail IFF the above call was successful and the device is not present */
994 if (!result && !device->status.present) {
995 dprintk("Device not present\n");
996 result = -ENODEV;
997 goto outwalk;
998 }
999
Stelian Pop7f09c432007-01-13 23:04:31 +01001000 if (debug) {
malattia@linux.it59b19102007-04-09 10:19:04 +02001001 status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_nc_acpi_handle,
Stelian Pop7f09c432007-01-13 23:04:31 +01001002 1, sony_walk_callback, NULL, NULL);
1003 if (ACPI_FAILURE(status)) {
malattia@linux.itf6119b02007-04-09 19:31:06 +02001004 printk(KERN_WARNING DRV_PFX "unable to walk acpi resources\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001005 result = -ENODEV;
1006 goto outwalk;
1007 }
Stelian Popc5611622007-01-13 23:04:37 +01001008 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001009
Mattia Dongilib25b7322007-07-16 02:34:36 +09001010 /* try to _INI the device if such method exists (ACPI spec 3.0-6.5.1
1011 * should be respected as we already checked for the device presence above */
1012 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, METHOD_NAME__INI, &handle))) {
1013 dprintk("Invoking _INI\n");
1014 if (ACPI_FAILURE(acpi_evaluate_object(sony_nc_acpi_handle, METHOD_NAME__INI,
1015 NULL, NULL)))
1016 dprintk("_INI Method failed\n");
1017 }
1018
malattia@linux.it1549ee62007-04-09 10:19:08 +02001019 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05001020 result = sony_laptop_setup_input(device);
malattia@linux.it1549ee62007-04-09 10:19:08 +02001021 if (result) {
1022 printk(KERN_ERR DRV_PFX
1023 "Unabe to create input devices.\n");
1024 goto outwalk;
1025 }
1026
malattia@linux.it59b19102007-04-09 10:19:04 +02001027 status = acpi_install_notify_handler(sony_nc_acpi_handle,
Stelian Popc5611622007-01-13 23:04:37 +01001028 ACPI_DEVICE_NOTIFY,
Len Browna02d1c12007-02-07 15:34:02 -05001029 sony_acpi_notify, NULL);
Stelian Popc5611622007-01-13 23:04:37 +01001030 if (ACPI_FAILURE(status)) {
Mattia Dongilib25b7322007-07-16 02:34:36 +09001031 printk(KERN_WARNING DRV_PFX "unable to install notify handler (%u)\n", status);
Stelian Popc5611622007-01-13 23:04:37 +01001032 result = -ENODEV;
malattia@linux.it1549ee62007-04-09 10:19:08 +02001033 goto outinput;
Stelian Pop7f09c432007-01-13 23:04:31 +01001034 }
1035
malattia@linux.it59b19102007-04-09 10:19:04 +02001036 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT", &handle))) {
Alessandro Guido50f62af2007-01-13 23:04:34 +01001037 sony_backlight_device = backlight_device_register("sony", NULL,
Len Browna02d1c12007-02-07 15:34:02 -05001038 NULL,
Richard Purdie321709c2007-02-10 15:04:08 +00001039 &sony_backlight_ops);
Mattia Dongili7df03b82007-01-13 23:04:41 +01001040
Len Browna02d1c12007-02-07 15:34:02 -05001041 if (IS_ERR(sony_backlight_device)) {
malattia@linux.itf6119b02007-04-09 19:31:06 +02001042 printk(KERN_WARNING DRV_PFX "unable to register backlight device\n");
Mattia Dongili7df03b82007-01-13 23:04:41 +01001043 sony_backlight_device = NULL;
Richard Purdie321709c2007-02-10 15:04:08 +00001044 } else {
1045 sony_backlight_device->props.brightness =
Len Browna02d1c12007-02-07 15:34:02 -05001046 sony_backlight_get_brightness
1047 (sony_backlight_device);
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05001048 sony_backlight_device->props.max_brightness =
Richard Purdie321709c2007-02-10 15:04:08 +00001049 SONY_MAX_BRIGHTNESS - 1;
1050 }
1051
Alessandro Guido50f62af2007-01-13 23:04:34 +01001052 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001053
Mattia Dongili6315fd12007-07-16 02:34:35 +09001054 /* initialize models with specific requirements */
1055 dmi_check_system(sony_nc_ids);
1056
malattia@linux.it49a11de2007-04-09 19:28:56 +02001057 result = sony_pf_add();
1058 if (result)
Mattia Dongilied3aa4b2007-02-07 20:01:54 +01001059 goto outbacklight;
Stelian Pop7f09c432007-01-13 23:04:31 +01001060
malattia@linux.it56b87562007-04-09 10:19:05 +02001061 /* create sony_pf sysfs attributes related to the SNC device */
1062 for (item = sony_nc_values; item->name; ++item) {
1063
1064 if (!debug && item->debug)
1065 continue;
1066
1067 /* find the available acpiget as described in the DSDT */
1068 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
1069 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1070 *item->acpiget,
1071 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02001072 dprintk("Found %s getter: %s\n",
1073 item->name, *item->acpiget);
malattia@linux.it56b87562007-04-09 10:19:05 +02001074 item->devattr.attr.mode |= S_IRUGO;
1075 break;
1076 }
1077 }
1078
1079 /* find the available acpiset as described in the DSDT */
1080 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
1081 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1082 *item->acpiset,
1083 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02001084 dprintk("Found %s setter: %s\n",
1085 item->name, *item->acpiset);
malattia@linux.it56b87562007-04-09 10:19:05 +02001086 item->devattr.attr.mode |= S_IWUSR;
1087 break;
1088 }
1089 }
1090
1091 if (item->devattr.attr.mode != 0) {
1092 result =
1093 device_create_file(&sony_pf_device->dev,
1094 &item->devattr);
1095 if (result)
1096 goto out_sysfs;
1097 }
1098 }
1099
Stelian Pop7f09c432007-01-13 23:04:31 +01001100 return 0;
1101
malattia@linux.it56b87562007-04-09 10:19:05 +02001102 out_sysfs:
1103 for (item = sony_nc_values; item->name; ++item) {
1104 device_remove_file(&sony_pf_device->dev, &item->devattr);
1105 }
1106 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +02001107
Len Browna02d1c12007-02-07 15:34:02 -05001108 outbacklight:
Mattia Dongili7df03b82007-01-13 23:04:41 +01001109 if (sony_backlight_device)
1110 backlight_device_unregister(sony_backlight_device);
1111
malattia@linux.it59b19102007-04-09 10:19:04 +02001112 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
Stelian Popc5611622007-01-13 23:04:37 +01001113 ACPI_DEVICE_NOTIFY,
1114 sony_acpi_notify);
1115 if (ACPI_FAILURE(status))
malattia@linux.itf6119b02007-04-09 19:31:06 +02001116 printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
malattia@linux.it1549ee62007-04-09 10:19:08 +02001117
1118 outinput:
1119 sony_laptop_remove_input();
1120
Len Browna02d1c12007-02-07 15:34:02 -05001121 outwalk:
Stelian Pop7f09c432007-01-13 23:04:31 +01001122 return result;
1123}
1124
malattia@linux.it59b19102007-04-09 10:19:04 +02001125static int sony_nc_remove(struct acpi_device *device, int type)
Stelian Pop7f09c432007-01-13 23:04:31 +01001126{
1127 acpi_status status;
malattia@linux.it56b87562007-04-09 10:19:05 +02001128 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01001129
Alessandro Guido50f62af2007-01-13 23:04:34 +01001130 if (sony_backlight_device)
1131 backlight_device_unregister(sony_backlight_device);
1132
malattia@linux.it59b19102007-04-09 10:19:04 +02001133 sony_nc_acpi_device = NULL;
Stelian Popc5611622007-01-13 23:04:37 +01001134
malattia@linux.it59b19102007-04-09 10:19:04 +02001135 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
Stelian Popc5611622007-01-13 23:04:37 +01001136 ACPI_DEVICE_NOTIFY,
1137 sony_acpi_notify);
1138 if (ACPI_FAILURE(status))
malattia@linux.itf6119b02007-04-09 19:31:06 +02001139 printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001140
malattia@linux.it56b87562007-04-09 10:19:05 +02001141 for (item = sony_nc_values; item->name; ++item) {
1142 device_remove_file(&sony_pf_device->dev, &item->devattr);
1143 }
1144
1145 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +02001146 sony_laptop_remove_input();
malattia@linux.itf6119b02007-04-09 19:31:06 +02001147 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001148
1149 return 0;
1150}
1151
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001152static const struct acpi_device_id sony_device_ids[] = {
1153 {SONY_NC_HID, 0},
1154 {SONY_PIC_HID, 0},
1155 {"", 0},
1156};
1157MODULE_DEVICE_TABLE(acpi, sony_device_ids);
1158
1159static const struct acpi_device_id sony_nc_device_ids[] = {
1160 {SONY_NC_HID, 0},
1161 {"", 0},
1162};
1163
malattia@linux.it59b19102007-04-09 10:19:04 +02001164static struct acpi_driver sony_nc_driver = {
1165 .name = SONY_NC_DRIVER_NAME,
1166 .class = SONY_NC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001167 .ids = sony_nc_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02001168 .owner = THIS_MODULE,
Len Browna02d1c12007-02-07 15:34:02 -05001169 .ops = {
malattia@linux.it59b19102007-04-09 10:19:04 +02001170 .add = sony_nc_add,
1171 .remove = sony_nc_remove,
1172 .resume = sony_nc_resume,
Len Browna02d1c12007-02-07 15:34:02 -05001173 },
Andrew Morton3f4f4612007-01-13 23:04:32 +01001174};
1175
malattia@linux.it33a04452007-04-09 19:26:03 +02001176/*********** SPIC (SNY6001) Device ***********/
1177
1178#define SONYPI_DEVICE_TYPE1 0x00000001
1179#define SONYPI_DEVICE_TYPE2 0x00000002
1180#define SONYPI_DEVICE_TYPE3 0x00000004
1181
Mattia Dongili22a17782007-07-16 02:34:39 +09001182#define SONYPI_TYPE1_OFFSET 0x04
1183#define SONYPI_TYPE2_OFFSET 0x12
1184#define SONYPI_TYPE3_OFFSET 0x12
malattia@linux.it33a04452007-04-09 19:26:03 +02001185
malattia@linux.it33a04452007-04-09 19:26:03 +02001186struct sony_pic_ioport {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001187 struct acpi_resource_io io1;
1188 struct acpi_resource_io io2;
malattia@linux.it33a04452007-04-09 19:26:03 +02001189 struct list_head list;
1190};
1191
1192struct sony_pic_irq {
1193 struct acpi_resource_irq irq;
1194 struct list_head list;
1195};
1196
1197struct sony_pic_dev {
1198 int model;
Mattia Dongili22a17782007-07-16 02:34:39 +09001199 u16 evport_offset;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001200 u8 camera_power;
1201 u8 bluetooth_power;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09001202 u8 wwan_power;
malattia@linux.it33a04452007-04-09 19:26:03 +02001203 struct acpi_device *acpi_dev;
1204 struct sony_pic_irq *cur_irq;
1205 struct sony_pic_ioport *cur_ioport;
1206 struct list_head interrupts;
1207 struct list_head ioports;
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001208 struct mutex lock;
malattia@linux.it33a04452007-04-09 19:26:03 +02001209};
1210
1211static struct sony_pic_dev spic_dev = {
1212 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
1213 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
1214};
1215
1216/* Event masks */
1217#define SONYPI_JOGGER_MASK 0x00000001
1218#define SONYPI_CAPTURE_MASK 0x00000002
1219#define SONYPI_FNKEY_MASK 0x00000004
1220#define SONYPI_BLUETOOTH_MASK 0x00000008
1221#define SONYPI_PKEY_MASK 0x00000010
1222#define SONYPI_BACK_MASK 0x00000020
1223#define SONYPI_HELP_MASK 0x00000040
1224#define SONYPI_LID_MASK 0x00000080
1225#define SONYPI_ZOOM_MASK 0x00000100
1226#define SONYPI_THUMBPHRASE_MASK 0x00000200
1227#define SONYPI_MEYE_MASK 0x00000400
1228#define SONYPI_MEMORYSTICK_MASK 0x00000800
1229#define SONYPI_BATTERY_MASK 0x00001000
1230#define SONYPI_WIRELESS_MASK 0x00002000
1231
1232struct sonypi_event {
1233 u8 data;
1234 u8 event;
1235};
1236
1237/* The set of possible button release events */
1238static struct sonypi_event sonypi_releaseev[] = {
1239 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
1240 { 0, 0 }
1241};
1242
1243/* The set of possible jogger events */
1244static struct sonypi_event sonypi_joggerev[] = {
1245 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
1246 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
1247 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
1248 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
1249 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
1250 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
1251 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
1252 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
1253 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
1254 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
1255 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
1256 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
1257 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
1258 { 0, 0 }
1259};
1260
1261/* The set of possible capture button events */
1262static struct sonypi_event sonypi_captureev[] = {
1263 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
1264 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
1265 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
1266 { 0, 0 }
1267};
1268
1269/* The set of possible fnkeys events */
1270static struct sonypi_event sonypi_fnkeyev[] = {
1271 { 0x10, SONYPI_EVENT_FNKEY_ESC },
1272 { 0x11, SONYPI_EVENT_FNKEY_F1 },
1273 { 0x12, SONYPI_EVENT_FNKEY_F2 },
1274 { 0x13, SONYPI_EVENT_FNKEY_F3 },
1275 { 0x14, SONYPI_EVENT_FNKEY_F4 },
1276 { 0x15, SONYPI_EVENT_FNKEY_F5 },
1277 { 0x16, SONYPI_EVENT_FNKEY_F6 },
1278 { 0x17, SONYPI_EVENT_FNKEY_F7 },
1279 { 0x18, SONYPI_EVENT_FNKEY_F8 },
1280 { 0x19, SONYPI_EVENT_FNKEY_F9 },
1281 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
1282 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
1283 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
1284 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
1285 { 0x21, SONYPI_EVENT_FNKEY_1 },
1286 { 0x22, SONYPI_EVENT_FNKEY_2 },
1287 { 0x31, SONYPI_EVENT_FNKEY_D },
1288 { 0x32, SONYPI_EVENT_FNKEY_E },
1289 { 0x33, SONYPI_EVENT_FNKEY_F },
1290 { 0x34, SONYPI_EVENT_FNKEY_S },
1291 { 0x35, SONYPI_EVENT_FNKEY_B },
1292 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
1293 { 0, 0 }
1294};
1295
1296/* The set of possible program key events */
1297static struct sonypi_event sonypi_pkeyev[] = {
1298 { 0x01, SONYPI_EVENT_PKEY_P1 },
1299 { 0x02, SONYPI_EVENT_PKEY_P2 },
1300 { 0x04, SONYPI_EVENT_PKEY_P3 },
1301 { 0x5c, SONYPI_EVENT_PKEY_P1 },
1302 { 0, 0 }
1303};
1304
1305/* The set of possible bluetooth events */
1306static struct sonypi_event sonypi_blueev[] = {
1307 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
1308 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
1309 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
1310 { 0, 0 }
1311};
1312
1313/* The set of possible wireless events */
1314static struct sonypi_event sonypi_wlessev[] = {
1315 { 0x59, SONYPI_EVENT_WIRELESS_ON },
1316 { 0x5a, SONYPI_EVENT_WIRELESS_OFF },
1317 { 0, 0 }
1318};
1319
1320/* The set of possible back button events */
1321static struct sonypi_event sonypi_backev[] = {
1322 { 0x20, SONYPI_EVENT_BACK_PRESSED },
1323 { 0, 0 }
1324};
1325
1326/* The set of possible help button events */
1327static struct sonypi_event sonypi_helpev[] = {
1328 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
1329 { 0, 0 }
1330};
1331
1332
1333/* The set of possible lid events */
1334static struct sonypi_event sonypi_lidev[] = {
1335 { 0x51, SONYPI_EVENT_LID_CLOSED },
1336 { 0x50, SONYPI_EVENT_LID_OPENED },
1337 { 0, 0 }
1338};
1339
1340/* The set of possible zoom events */
1341static struct sonypi_event sonypi_zoomev[] = {
1342 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
1343 { 0, 0 }
1344};
1345
1346/* The set of possible thumbphrase events */
1347static struct sonypi_event sonypi_thumbphraseev[] = {
1348 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
1349 { 0, 0 }
1350};
1351
1352/* The set of possible motioneye camera events */
1353static struct sonypi_event sonypi_meyeev[] = {
1354 { 0x00, SONYPI_EVENT_MEYE_FACE },
1355 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
1356 { 0, 0 }
1357};
1358
1359/* The set of possible memorystick events */
1360static struct sonypi_event sonypi_memorystickev[] = {
1361 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
1362 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
1363 { 0, 0 }
1364};
1365
1366/* The set of possible battery events */
1367static struct sonypi_event sonypi_batteryev[] = {
1368 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
1369 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
1370 { 0, 0 }
1371};
1372
1373static struct sonypi_eventtypes {
1374 int model;
1375 u8 data;
1376 unsigned long mask;
1377 struct sonypi_event * events;
1378} sony_pic_eventtypes[] = {
1379 { SONYPI_DEVICE_TYPE1, 0, 0xffffffff, sonypi_releaseev },
1380 { SONYPI_DEVICE_TYPE1, 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
1381 { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_LID_MASK, sonypi_lidev },
1382 { SONYPI_DEVICE_TYPE1, 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
1383 { SONYPI_DEVICE_TYPE1, 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
1384 { SONYPI_DEVICE_TYPE1, 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1385 { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1386 { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
1387 { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1388 { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
1389
1390 { SONYPI_DEVICE_TYPE2, 0, 0xffffffff, sonypi_releaseev },
1391 { SONYPI_DEVICE_TYPE2, 0x38, SONYPI_LID_MASK, sonypi_lidev },
1392 { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
1393 { SONYPI_DEVICE_TYPE2, 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
1394 { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1395 { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1396 { SONYPI_DEVICE_TYPE2, 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
1397 { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_BACK_MASK, sonypi_backev },
1398 { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_HELP_MASK, sonypi_helpev },
1399 { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
1400 { SONYPI_DEVICE_TYPE2, 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
1401 { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1402 { SONYPI_DEVICE_TYPE2, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1403 { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1404
1405 { SONYPI_DEVICE_TYPE3, 0, 0xffffffff, sonypi_releaseev },
1406 { SONYPI_DEVICE_TYPE3, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1407 { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
1408 { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1409 { SONYPI_DEVICE_TYPE3, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1410 { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1411 { 0 }
1412};
1413
1414static int sony_pic_detect_device_type(void)
1415{
1416 struct pci_dev *pcidev;
1417 int model = 0;
1418
1419 if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1420 PCI_DEVICE_ID_INTEL_82371AB_3, NULL)))
1421 model = SONYPI_DEVICE_TYPE1;
1422
1423 else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1424 PCI_DEVICE_ID_INTEL_ICH6_1, NULL)))
1425 model = SONYPI_DEVICE_TYPE3;
1426
1427 else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1428 PCI_DEVICE_ID_INTEL_ICH7_1, NULL)))
1429 model = SONYPI_DEVICE_TYPE3;
1430
1431 else
1432 model = SONYPI_DEVICE_TYPE2;
1433
1434 if (pcidev)
1435 pci_dev_put(pcidev);
1436
1437 printk(KERN_INFO DRV_PFX "detected Type%d model\n",
1438 model == SONYPI_DEVICE_TYPE1 ? 1 :
1439 model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
1440 return model;
1441}
1442
1443#define ITERATIONS_LONG 10000
1444#define ITERATIONS_SHORT 10
1445#define wait_on_command(command, iterations) { \
1446 unsigned int n = iterations; \
1447 while (--n && (command)) \
1448 udelay(1); \
1449 if (!n) \
1450 dprintk("command failed at %s : %s (line %d)\n", \
1451 __FILE__, __FUNCTION__, __LINE__); \
1452}
1453
1454static u8 sony_pic_call1(u8 dev)
1455{
1456 u8 v1, v2;
1457
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001458 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02001459 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001460 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1461 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
1462 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09001463 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02001464 return v2;
1465}
1466
1467static u8 sony_pic_call2(u8 dev, u8 fn)
1468{
1469 u8 v1;
1470
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001471 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02001472 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001473 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1474 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02001475 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001476 outb(fn, spic_dev.cur_ioport->io1.minimum);
1477 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09001478 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02001479 return v1;
1480}
1481
malattia@linux.it49a11de2007-04-09 19:28:56 +02001482static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
1483{
1484 u8 v1;
1485
Mattia Dongilifd1caae2007-08-12 16:20:28 +09001486 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1487 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1488 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1489 outb(fn, spic_dev.cur_ioport->io1.minimum);
1490 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1491 outb(v, spic_dev.cur_ioport->io1.minimum);
1492 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09001493 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
1494 dev, fn, v, v1);
malattia@linux.it49a11de2007-04-09 19:28:56 +02001495 return v1;
1496}
1497
1498/* camera tests and poweron/poweroff */
1499#define SONYPI_CAMERA_PICTURE 5
malattia@linux.it49a11de2007-04-09 19:28:56 +02001500#define SONYPI_CAMERA_CONTROL 0x10
malattia@linux.ite3646322007-04-28 23:34:22 +09001501
1502#define SONYPI_CAMERA_BRIGHTNESS 0
1503#define SONYPI_CAMERA_CONTRAST 1
1504#define SONYPI_CAMERA_HUE 2
1505#define SONYPI_CAMERA_COLOR 3
1506#define SONYPI_CAMERA_SHARPNESS 4
1507
1508#define SONYPI_CAMERA_EXPOSURE_MASK 0xC
1509#define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
1510#define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
1511#define SONYPI_CAMERA_MUTE_MASK 0x40
1512
1513/* the rest don't need a loop until not 0xff */
1514#define SONYPI_CAMERA_AGC 6
1515#define SONYPI_CAMERA_AGC_MASK 0x30
1516#define SONYPI_CAMERA_SHUTTER_MASK 0x7
1517
1518#define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
1519#define SONYPI_CAMERA_CONTROL 0x10
1520
1521#define SONYPI_CAMERA_STATUS 7
1522#define SONYPI_CAMERA_STATUS_READY 0x2
1523#define SONYPI_CAMERA_STATUS_POSITION 0x4
1524
1525#define SONYPI_DIRECTION_BACKWARDS 0x4
1526
1527#define SONYPI_CAMERA_REVISION 8
1528#define SONYPI_CAMERA_ROMVERSION 9
malattia@linux.it49a11de2007-04-09 19:28:56 +02001529
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001530static int __sony_pic_camera_ready(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001531{
1532 u8 v;
1533
1534 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
1535 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
1536}
1537
malattia@linux.ite3646322007-04-28 23:34:22 +09001538static int __sony_pic_camera_off(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001539{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001540 if (!camera) {
1541 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1542 return -ENODEV;
1543 }
1544
malattia@linux.it49a11de2007-04-09 19:28:56 +02001545 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
1546 SONYPI_CAMERA_MUTE_MASK),
1547 ITERATIONS_SHORT);
1548
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001549 if (spic_dev.camera_power) {
1550 sony_pic_call2(0x91, 0);
1551 spic_dev.camera_power = 0;
1552 }
1553 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001554}
1555
malattia@linux.ite3646322007-04-28 23:34:22 +09001556static int __sony_pic_camera_on(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001557{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001558 int i, j, x;
1559
1560 if (!camera) {
1561 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1562 return -ENODEV;
1563 }
malattia@linux.it49a11de2007-04-09 19:28:56 +02001564
1565 if (spic_dev.camera_power)
malattia@linux.ite3646322007-04-28 23:34:22 +09001566 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001567
1568 for (j = 5; j > 0; j--) {
1569
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001570 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001571 msleep(10);
1572 sony_pic_call1(0x93);
1573
1574 for (i = 400; i > 0; i--) {
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001575 if (__sony_pic_camera_ready())
malattia@linux.it49a11de2007-04-09 19:28:56 +02001576 break;
1577 msleep(10);
1578 }
1579 if (i)
1580 break;
1581 }
1582
1583 if (j == 0) {
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001584 printk(KERN_WARNING DRV_PFX "failed to power on camera\n");
malattia@linux.ite3646322007-04-28 23:34:22 +09001585 return -ENODEV;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001586 }
1587
1588 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
1589 0x5a),
1590 ITERATIONS_SHORT);
1591
1592 spic_dev.camera_power = 1;
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001593 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001594}
1595
malattia@linux.ite3646322007-04-28 23:34:22 +09001596/* External camera command (exported to the motion eye v4l driver) */
1597int sony_pic_camera_command(int command, u8 value)
1598{
1599 if (!camera)
1600 return -EIO;
1601
1602 mutex_lock(&spic_dev.lock);
1603
1604 switch (command) {
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001605 case SONY_PIC_COMMAND_SETCAMERA:
malattia@linux.ite3646322007-04-28 23:34:22 +09001606 if (value)
1607 __sony_pic_camera_on();
1608 else
1609 __sony_pic_camera_off();
1610 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001611 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09001612 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
1613 ITERATIONS_SHORT);
1614 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001615 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
malattia@linux.ite3646322007-04-28 23:34:22 +09001616 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
1617 ITERATIONS_SHORT);
1618 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001619 case SONY_PIC_COMMAND_SETCAMERAHUE:
malattia@linux.ite3646322007-04-28 23:34:22 +09001620 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
1621 ITERATIONS_SHORT);
1622 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001623 case SONY_PIC_COMMAND_SETCAMERACOLOR:
malattia@linux.ite3646322007-04-28 23:34:22 +09001624 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
1625 ITERATIONS_SHORT);
1626 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001627 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09001628 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
1629 ITERATIONS_SHORT);
1630 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001631 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
malattia@linux.ite3646322007-04-28 23:34:22 +09001632 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
1633 ITERATIONS_SHORT);
1634 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001635 case SONY_PIC_COMMAND_SETCAMERAAGC:
malattia@linux.ite3646322007-04-28 23:34:22 +09001636 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
1637 ITERATIONS_SHORT);
1638 break;
1639 default:
1640 printk(KERN_ERR DRV_PFX "sony_pic_camera_command invalid: %d\n",
1641 command);
1642 break;
1643 }
1644 mutex_unlock(&spic_dev.lock);
1645 return 0;
1646}
1647EXPORT_SYMBOL(sony_pic_camera_command);
1648
malattia@linux.it9476cdf2007-04-28 23:21:42 +09001649/* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
1650static void sony_pic_set_wwanpower(u8 state)
1651{
1652 state = !!state;
1653 mutex_lock(&spic_dev.lock);
1654 if (spic_dev.wwan_power == state) {
1655 mutex_unlock(&spic_dev.lock);
1656 return;
1657 }
1658 sony_pic_call2(0xB0, state);
1659 spic_dev.wwan_power = state;
1660 mutex_unlock(&spic_dev.lock);
1661}
1662
1663static ssize_t sony_pic_wwanpower_store(struct device *dev,
1664 struct device_attribute *attr,
1665 const char *buffer, size_t count)
1666{
1667 unsigned long value;
1668 if (count > 31)
1669 return -EINVAL;
1670
1671 value = simple_strtoul(buffer, NULL, 10);
1672 sony_pic_set_wwanpower(value);
1673
1674 return count;
1675}
1676
1677static ssize_t sony_pic_wwanpower_show(struct device *dev,
1678 struct device_attribute *attr, char *buffer)
1679{
1680 ssize_t count;
1681 mutex_lock(&spic_dev.lock);
1682 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
1683 mutex_unlock(&spic_dev.lock);
1684 return count;
1685}
1686
malattia@linux.it49a11de2007-04-09 19:28:56 +02001687/* bluetooth subsystem power state */
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001688static void __sony_pic_set_bluetoothpower(u8 state)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001689{
1690 state = !!state;
1691 if (spic_dev.bluetooth_power == state)
1692 return;
1693 sony_pic_call2(0x96, state);
1694 sony_pic_call1(0x82);
1695 spic_dev.bluetooth_power = state;
1696}
1697
1698static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
1699 struct device_attribute *attr,
1700 const char *buffer, size_t count)
1701{
1702 unsigned long value;
1703 if (count > 31)
1704 return -EINVAL;
1705
1706 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001707 mutex_lock(&spic_dev.lock);
1708 __sony_pic_set_bluetoothpower(value);
1709 mutex_unlock(&spic_dev.lock);
malattia@linux.it49a11de2007-04-09 19:28:56 +02001710
1711 return count;
1712}
1713
1714static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
1715 struct device_attribute *attr, char *buffer)
1716{
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001717 ssize_t count = 0;
1718 mutex_lock(&spic_dev.lock);
1719 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
1720 mutex_unlock(&spic_dev.lock);
1721 return count;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001722}
1723
1724/* fan speed */
1725/* FAN0 information (reverse engineered from ACPI tables) */
1726#define SONY_PIC_FAN0_STATUS 0x93
malattia@linux.it7b153f32007-04-09 19:31:25 +02001727static int sony_pic_set_fanspeed(unsigned long value)
1728{
1729 return ec_write(SONY_PIC_FAN0_STATUS, value);
1730}
1731
1732static int sony_pic_get_fanspeed(u8 *value)
1733{
1734 return ec_read(SONY_PIC_FAN0_STATUS, value);
1735}
1736
malattia@linux.it49a11de2007-04-09 19:28:56 +02001737static ssize_t sony_pic_fanspeed_store(struct device *dev,
1738 struct device_attribute *attr,
1739 const char *buffer, size_t count)
1740{
1741 unsigned long value;
1742 if (count > 31)
1743 return -EINVAL;
1744
1745 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it7b153f32007-04-09 19:31:25 +02001746 if (sony_pic_set_fanspeed(value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02001747 return -EIO;
1748
1749 return count;
1750}
1751
1752static ssize_t sony_pic_fanspeed_show(struct device *dev,
1753 struct device_attribute *attr, char *buffer)
1754{
1755 u8 value = 0;
malattia@linux.it7b153f32007-04-09 19:31:25 +02001756 if (sony_pic_get_fanspeed(&value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02001757 return -EIO;
1758
1759 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
1760}
1761
1762#define SPIC_ATTR(_name, _mode) \
1763struct device_attribute spic_attr_##_name = __ATTR(_name, \
1764 _mode, sony_pic_## _name ##_show, \
1765 sony_pic_## _name ##_store)
1766
malattia@linux.it49a11de2007-04-09 19:28:56 +02001767static SPIC_ATTR(bluetoothpower, 0644);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09001768static SPIC_ATTR(wwanpower, 0644);
malattia@linux.it49a11de2007-04-09 19:28:56 +02001769static SPIC_ATTR(fanspeed, 0644);
1770
1771static struct attribute *spic_attributes[] = {
malattia@linux.it49a11de2007-04-09 19:28:56 +02001772 &spic_attr_bluetoothpower.attr,
malattia@linux.it9476cdf2007-04-28 23:21:42 +09001773 &spic_attr_wwanpower.attr,
malattia@linux.it49a11de2007-04-09 19:28:56 +02001774 &spic_attr_fanspeed.attr,
1775 NULL
1776};
1777
1778static struct attribute_group spic_attribute_group = {
1779 .attrs = spic_attributes
1780};
1781
malattia@linux.it7b153f32007-04-09 19:31:25 +02001782/******** SONYPI compatibility **********/
Mattia Dongilia64e62a2007-05-01 11:19:53 +09001783#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +02001784
1785/* battery / brightness / temperature addresses */
1786#define SONYPI_BAT_FLAGS 0x81
1787#define SONYPI_LCD_LIGHT 0x96
1788#define SONYPI_BAT1_PCTRM 0xa0
1789#define SONYPI_BAT1_LEFT 0xa2
1790#define SONYPI_BAT1_MAXRT 0xa4
1791#define SONYPI_BAT2_PCTRM 0xa8
1792#define SONYPI_BAT2_LEFT 0xaa
1793#define SONYPI_BAT2_MAXRT 0xac
1794#define SONYPI_BAT1_MAXTK 0xb0
1795#define SONYPI_BAT1_FULL 0xb2
1796#define SONYPI_BAT2_MAXTK 0xb8
1797#define SONYPI_BAT2_FULL 0xba
1798#define SONYPI_TEMP_STATUS 0xC1
1799
1800struct sonypi_compat_s {
1801 struct fasync_struct *fifo_async;
1802 struct kfifo *fifo;
1803 spinlock_t fifo_lock;
1804 wait_queue_head_t fifo_proc_list;
1805 atomic_t open_count;
1806};
1807static struct sonypi_compat_s sonypi_compat = {
1808 .open_count = ATOMIC_INIT(0),
1809};
1810
1811static int sonypi_misc_fasync(int fd, struct file *filp, int on)
1812{
1813 int retval;
1814
1815 retval = fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
1816 if (retval < 0)
1817 return retval;
1818 return 0;
1819}
1820
1821static int sonypi_misc_release(struct inode *inode, struct file *file)
1822{
1823 sonypi_misc_fasync(-1, file, 0);
1824 atomic_dec(&sonypi_compat.open_count);
1825 return 0;
1826}
1827
1828static int sonypi_misc_open(struct inode *inode, struct file *file)
1829{
1830 /* Flush input queue on first open */
1831 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
1832 kfifo_reset(sonypi_compat.fifo);
1833 return 0;
1834}
1835
1836static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
1837 size_t count, loff_t *pos)
1838{
1839 ssize_t ret;
1840 unsigned char c;
1841
1842 if ((kfifo_len(sonypi_compat.fifo) == 0) &&
1843 (file->f_flags & O_NONBLOCK))
1844 return -EAGAIN;
1845
1846 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
1847 kfifo_len(sonypi_compat.fifo) != 0);
1848 if (ret)
1849 return ret;
1850
1851 while (ret < count &&
1852 (kfifo_get(sonypi_compat.fifo, &c, sizeof(c)) == sizeof(c))) {
1853 if (put_user(c, buf++))
1854 return -EFAULT;
1855 ret++;
1856 }
1857
1858 if (ret > 0) {
1859 struct inode *inode = file->f_path.dentry->d_inode;
1860 inode->i_atime = current_fs_time(inode->i_sb);
1861 }
1862
1863 return ret;
1864}
1865
1866static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
1867{
1868 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
1869 if (kfifo_len(sonypi_compat.fifo))
1870 return POLLIN | POLLRDNORM;
1871 return 0;
1872}
1873
1874static int ec_read16(u8 addr, u16 *value)
1875{
1876 u8 val_lb, val_hb;
1877 if (ec_read(addr, &val_lb))
1878 return -1;
1879 if (ec_read(addr + 1, &val_hb))
1880 return -1;
1881 *value = val_lb | (val_hb << 8);
1882 return 0;
1883}
1884
1885static int sonypi_misc_ioctl(struct inode *ip, struct file *fp,
1886 unsigned int cmd, unsigned long arg)
1887{
1888 int ret = 0;
1889 void __user *argp = (void __user *)arg;
1890 u8 val8;
1891 u16 val16;
1892 int value;
1893
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001894 mutex_lock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02001895 switch (cmd) {
1896 case SONYPI_IOCGBRT:
1897 if (sony_backlight_device == NULL) {
1898 ret = -EIO;
1899 break;
1900 }
1901 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
1902 ret = -EIO;
1903 break;
1904 }
1905 val8 = ((value & 0xff) - 1) << 5;
1906 if (copy_to_user(argp, &val8, sizeof(val8)))
1907 ret = -EFAULT;
1908 break;
1909 case SONYPI_IOCSBRT:
1910 if (sony_backlight_device == NULL) {
1911 ret = -EIO;
1912 break;
1913 }
1914 if (copy_from_user(&val8, argp, sizeof(val8))) {
1915 ret = -EFAULT;
1916 break;
1917 }
1918 if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
1919 (val8 >> 5) + 1, NULL)) {
1920 ret = -EIO;
1921 break;
1922 }
1923 /* sync the backlight device status */
1924 sony_backlight_device->props.brightness =
1925 sony_backlight_get_brightness(sony_backlight_device);
1926 break;
1927 case SONYPI_IOCGBAT1CAP:
1928 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
1929 ret = -EIO;
1930 break;
1931 }
1932 if (copy_to_user(argp, &val16, sizeof(val16)))
1933 ret = -EFAULT;
1934 break;
1935 case SONYPI_IOCGBAT1REM:
1936 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
1937 ret = -EIO;
1938 break;
1939 }
1940 if (copy_to_user(argp, &val16, sizeof(val16)))
1941 ret = -EFAULT;
1942 break;
1943 case SONYPI_IOCGBAT2CAP:
1944 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
1945 ret = -EIO;
1946 break;
1947 }
1948 if (copy_to_user(argp, &val16, sizeof(val16)))
1949 ret = -EFAULT;
1950 break;
1951 case SONYPI_IOCGBAT2REM:
1952 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
1953 ret = -EIO;
1954 break;
1955 }
1956 if (copy_to_user(argp, &val16, sizeof(val16)))
1957 ret = -EFAULT;
1958 break;
1959 case SONYPI_IOCGBATFLAGS:
1960 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
1961 ret = -EIO;
1962 break;
1963 }
1964 val8 &= 0x07;
1965 if (copy_to_user(argp, &val8, sizeof(val8)))
1966 ret = -EFAULT;
1967 break;
1968 case SONYPI_IOCGBLUE:
1969 val8 = spic_dev.bluetooth_power;
1970 if (copy_to_user(argp, &val8, sizeof(val8)))
1971 ret = -EFAULT;
1972 break;
1973 case SONYPI_IOCSBLUE:
1974 if (copy_from_user(&val8, argp, sizeof(val8))) {
1975 ret = -EFAULT;
1976 break;
1977 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001978 __sony_pic_set_bluetoothpower(val8);
malattia@linux.it7b153f32007-04-09 19:31:25 +02001979 break;
1980 /* FAN Controls */
1981 case SONYPI_IOCGFAN:
1982 if (sony_pic_get_fanspeed(&val8)) {
1983 ret = -EIO;
1984 break;
1985 }
1986 if (copy_to_user(argp, &val8, sizeof(val8)))
1987 ret = -EFAULT;
1988 break;
1989 case SONYPI_IOCSFAN:
1990 if (copy_from_user(&val8, argp, sizeof(val8))) {
1991 ret = -EFAULT;
1992 break;
1993 }
1994 if (sony_pic_set_fanspeed(val8))
1995 ret = -EIO;
1996 break;
1997 /* GET Temperature (useful under APM) */
1998 case SONYPI_IOCGTEMP:
1999 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
2000 ret = -EIO;
2001 break;
2002 }
2003 if (copy_to_user(argp, &val8, sizeof(val8)))
2004 ret = -EFAULT;
2005 break;
2006 default:
2007 ret = -EINVAL;
2008 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002009 mutex_unlock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002010 return ret;
2011}
2012
2013static const struct file_operations sonypi_misc_fops = {
2014 .owner = THIS_MODULE,
2015 .read = sonypi_misc_read,
2016 .poll = sonypi_misc_poll,
2017 .open = sonypi_misc_open,
2018 .release = sonypi_misc_release,
2019 .fasync = sonypi_misc_fasync,
2020 .ioctl = sonypi_misc_ioctl,
2021};
2022
2023static struct miscdevice sonypi_misc_device = {
2024 .minor = MISC_DYNAMIC_MINOR,
2025 .name = "sonypi",
2026 .fops = &sonypi_misc_fops,
2027};
2028
2029static void sonypi_compat_report_event(u8 event)
2030{
2031 kfifo_put(sonypi_compat.fifo, (unsigned char *)&event, sizeof(event));
2032 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
2033 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
2034}
2035
2036static int sonypi_compat_init(void)
2037{
2038 int error;
2039
2040 spin_lock_init(&sonypi_compat.fifo_lock);
2041 sonypi_compat.fifo = kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
2042 &sonypi_compat.fifo_lock);
2043 if (IS_ERR(sonypi_compat.fifo)) {
2044 printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
2045 return PTR_ERR(sonypi_compat.fifo);
2046 }
2047
2048 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002049
2050 if (minor != -1)
2051 sonypi_misc_device.minor = minor;
2052 error = misc_register(&sonypi_misc_device);
2053 if (error) {
2054 printk(KERN_ERR DRV_PFX "misc_register failed\n");
2055 goto err_free_kfifo;
2056 }
2057 if (minor == -1)
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002058 printk(KERN_INFO DRV_PFX "device allocated minor is %d\n",
malattia@linux.it7b153f32007-04-09 19:31:25 +02002059 sonypi_misc_device.minor);
2060
2061 return 0;
2062
2063err_free_kfifo:
2064 kfifo_free(sonypi_compat.fifo);
2065 return error;
2066}
2067
2068static void sonypi_compat_exit(void)
2069{
2070 misc_deregister(&sonypi_misc_device);
2071 kfifo_free(sonypi_compat.fifo);
2072}
2073#else
2074static int sonypi_compat_init(void) { return 0; }
2075static void sonypi_compat_exit(void) { }
2076static void sonypi_compat_report_event(u8 event) { }
Mattia Dongilia64e62a2007-05-01 11:19:53 +09002077#endif /* CONFIG_SONYPI_COMPAT */
malattia@linux.it7b153f32007-04-09 19:31:25 +02002078
malattia@linux.it1549ee62007-04-09 10:19:08 +02002079/*
malattia@linux.it33a04452007-04-09 19:26:03 +02002080 * ACPI callbacks
malattia@linux.it1549ee62007-04-09 10:19:08 +02002081 */
malattia@linux.it33a04452007-04-09 19:26:03 +02002082static acpi_status
2083sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
2084{
2085 u32 i;
2086 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
2087
2088 switch (resource->type) {
2089 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002090 {
2091 /* start IO enumeration */
2092 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
2093 if (!ioport)
2094 return AE_ERROR;
2095
2096 list_add(&ioport->list, &dev->ioports);
2097 return AE_OK;
2098 }
2099
malattia@linux.it33a04452007-04-09 19:26:03 +02002100 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002101 /* end IO enumeration */
malattia@linux.it33a04452007-04-09 19:26:03 +02002102 return AE_OK;
2103
2104 case ACPI_RESOURCE_TYPE_IRQ:
2105 {
2106 struct acpi_resource_irq *p = &resource->data.irq;
2107 struct sony_pic_irq *interrupt = NULL;
2108 if (!p || !p->interrupt_count) {
2109 /*
2110 * IRQ descriptors may have no IRQ# bits set,
2111 * particularly those those w/ _STA disabled
2112 */
2113 dprintk("Blank IRQ resource\n");
2114 return AE_OK;
2115 }
2116 for (i = 0; i < p->interrupt_count; i++) {
2117 if (!p->interrupts[i]) {
2118 printk(KERN_WARNING DRV_PFX
2119 "Invalid IRQ %d\n",
2120 p->interrupts[i]);
2121 continue;
2122 }
2123 interrupt = kzalloc(sizeof(*interrupt),
2124 GFP_KERNEL);
2125 if (!interrupt)
2126 return AE_ERROR;
2127
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002128 list_add(&interrupt->list, &dev->interrupts);
malattia@linux.it33a04452007-04-09 19:26:03 +02002129 interrupt->irq.triggering = p->triggering;
2130 interrupt->irq.polarity = p->polarity;
2131 interrupt->irq.sharable = p->sharable;
2132 interrupt->irq.interrupt_count = 1;
2133 interrupt->irq.interrupts[0] = p->interrupts[i];
2134 }
2135 return AE_OK;
2136 }
2137 case ACPI_RESOURCE_TYPE_IO:
2138 {
2139 struct acpi_resource_io *io = &resource->data.io;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002140 struct sony_pic_ioport *ioport =
2141 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
malattia@linux.it33a04452007-04-09 19:26:03 +02002142 if (!io) {
2143 dprintk("Blank IO resource\n");
2144 return AE_OK;
2145 }
2146
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002147 if (!ioport->io1.minimum) {
2148 memcpy(&ioport->io1, io, sizeof(*io));
2149 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
2150 ioport->io1.address_length);
2151 }
2152 else if (!ioport->io2.minimum) {
2153 memcpy(&ioport->io2, io, sizeof(*io));
2154 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
2155 ioport->io2.address_length);
2156 }
2157 else {
2158 printk(KERN_ERR DRV_PFX "Unknown SPIC Type, more than 2 IO Ports\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002159 return AE_ERROR;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002160 }
malattia@linux.it33a04452007-04-09 19:26:03 +02002161 return AE_OK;
2162 }
2163 default:
2164 dprintk("Resource %d isn't an IRQ nor an IO port\n",
2165 resource->type);
2166
2167 case ACPI_RESOURCE_TYPE_END_TAG:
2168 return AE_OK;
2169 }
2170 return AE_CTRL_TERMINATE;
2171}
2172
2173static int sony_pic_possible_resources(struct acpi_device *device)
2174{
2175 int result = 0;
2176 acpi_status status = AE_OK;
2177
2178 if (!device)
2179 return -EINVAL;
2180
2181 /* get device status */
2182 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
2183 dprintk("Evaluating _STA\n");
2184 result = acpi_bus_get_status(device);
2185 if (result) {
2186 printk(KERN_WARNING DRV_PFX "Unable to read status\n");
2187 goto end;
2188 }
2189
2190 if (!device->status.enabled)
2191 dprintk("Device disabled\n");
2192 else
2193 dprintk("Device enabled\n");
2194
2195 /*
2196 * Query and parse 'method'
2197 */
2198 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
2199 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
2200 sony_pic_read_possible_resource, &spic_dev);
2201 if (ACPI_FAILURE(status)) {
2202 printk(KERN_WARNING DRV_PFX
2203 "Failure evaluating %s\n",
2204 METHOD_NAME__PRS);
2205 result = -ENODEV;
2206 }
2207end:
2208 return result;
2209}
2210
2211/*
2212 * Disable the spic device by calling its _DIS method
2213 */
2214static int sony_pic_disable(struct acpi_device *device)
2215{
Randy Dunlapf3a740c2007-07-15 23:40:49 -07002216 if (ACPI_FAILURE(acpi_evaluate_object(device->handle,
2217 "_DIS", NULL, NULL)))
malattia@linux.it33a04452007-04-09 19:26:03 +02002218 return -ENXIO;
2219
2220 dprintk("Device disabled\n");
2221 return 0;
2222}
2223
2224
2225/*
2226 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
2227 *
2228 * Call _SRS to set current resources
2229 */
2230static int sony_pic_enable(struct acpi_device *device,
2231 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
2232{
2233 acpi_status status;
2234 int result = 0;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002235 /* Type 1 resource layout is:
2236 * IO
2237 * IO
2238 * IRQNoFlags
2239 * End
2240 *
2241 * Type 2 and 3 resource layout is:
2242 * IO
2243 * IRQNoFlags
2244 * End
2245 */
malattia@linux.it33a04452007-04-09 19:26:03 +02002246 struct {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002247 struct acpi_resource res1;
2248 struct acpi_resource res2;
2249 struct acpi_resource res3;
2250 struct acpi_resource res4;
malattia@linux.it33a04452007-04-09 19:26:03 +02002251 } *resource;
2252 struct acpi_buffer buffer = { 0, NULL };
2253
2254 if (!ioport || !irq)
2255 return -EINVAL;
2256
2257 /* init acpi_buffer */
2258 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
2259 if (!resource)
2260 return -ENOMEM;
2261
2262 buffer.length = sizeof(*resource) + 1;
2263 buffer.pointer = resource;
2264
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002265 /* setup Type 1 resources */
2266 if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002267
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002268 /* setup io resources */
2269 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2270 resource->res1.length = sizeof(struct acpi_resource);
2271 memcpy(&resource->res1.data.io, &ioport->io1,
2272 sizeof(struct acpi_resource_io));
malattia@linux.it33a04452007-04-09 19:26:03 +02002273
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002274 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
2275 resource->res2.length = sizeof(struct acpi_resource);
2276 memcpy(&resource->res2.data.io, &ioport->io2,
2277 sizeof(struct acpi_resource_io));
2278
2279 /* setup irq resource */
2280 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
2281 resource->res3.length = sizeof(struct acpi_resource);
2282 memcpy(&resource->res3.data.irq, &irq->irq,
2283 sizeof(struct acpi_resource_irq));
2284 /* we requested a shared irq */
2285 resource->res3.data.irq.sharable = ACPI_SHARED;
2286
2287 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
2288
2289 }
2290 /* setup Type 2/3 resources */
2291 else {
2292 /* setup io resource */
2293 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2294 resource->res1.length = sizeof(struct acpi_resource);
2295 memcpy(&resource->res1.data.io, &ioport->io1,
2296 sizeof(struct acpi_resource_io));
2297
2298 /* setup irq resource */
2299 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
2300 resource->res2.length = sizeof(struct acpi_resource);
2301 memcpy(&resource->res2.data.irq, &irq->irq,
2302 sizeof(struct acpi_resource_irq));
2303 /* we requested a shared irq */
2304 resource->res2.data.irq.sharable = ACPI_SHARED;
2305
2306 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
2307 }
malattia@linux.it33a04452007-04-09 19:26:03 +02002308
2309 /* Attempt to set the resource */
2310 dprintk("Evaluating _SRS\n");
2311 status = acpi_set_current_resources(device->handle, &buffer);
2312
2313 /* check for total failure */
2314 if (ACPI_FAILURE(status)) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002315 printk(KERN_ERR DRV_PFX "Error evaluating _SRS\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002316 result = -ENODEV;
2317 goto end;
2318 }
2319
2320 /* Necessary device initializations calls (from sonypi) */
2321 sony_pic_call1(0x82);
2322 sony_pic_call2(0x81, 0xff);
2323 sony_pic_call1(compat ? 0x92 : 0x82);
2324
2325end:
2326 kfree(resource);
2327 return result;
2328}
2329
2330/*****************
2331 *
2332 * ISR: some event is available
2333 *
2334 *****************/
2335static irqreturn_t sony_pic_irq(int irq, void *dev_id)
2336{
2337 int i, j;
malattia@linux.it33a04452007-04-09 19:26:03 +02002338 u8 ev = 0;
2339 u8 data_mask = 0;
2340 u8 device_event = 0;
2341
2342 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
2343
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002344 ev = inb_p(dev->cur_ioport->io1.minimum);
2345 if (dev->cur_ioport->io2.minimum)
2346 data_mask = inb_p(dev->cur_ioport->io2.minimum);
2347 else
2348 data_mask = inb_p(dev->cur_ioport->io1.minimum + dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02002349
Mattia Dongili22a17782007-07-16 02:34:39 +09002350 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002351 ev, data_mask, dev->cur_ioport->io1.minimum, dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02002352
2353 if (ev == 0x00 || ev == 0xff)
2354 return IRQ_HANDLED;
2355
2356 for (i = 0; sony_pic_eventtypes[i].model; i++) {
2357
2358 if (spic_dev.model != sony_pic_eventtypes[i].model)
2359 continue;
2360
2361 if ((data_mask & sony_pic_eventtypes[i].data) !=
2362 sony_pic_eventtypes[i].data)
2363 continue;
2364
2365 if (!(mask & sony_pic_eventtypes[i].mask))
2366 continue;
2367
2368 for (j = 0; sony_pic_eventtypes[i].events[j].event; j++) {
2369 if (ev == sony_pic_eventtypes[i].events[j].data) {
2370 device_event =
2371 sony_pic_eventtypes[i].events[j].event;
2372 goto found;
2373 }
2374 }
2375 }
2376 return IRQ_HANDLED;
2377
2378found:
malattia@linux.it1549ee62007-04-09 10:19:08 +02002379 sony_laptop_report_input_event(device_event);
Len Brown14e04fb2007-08-23 15:20:26 -04002380 acpi_bus_generate_proc_event(spic_dev.acpi_dev, 1, device_event);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002381 sonypi_compat_report_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02002382
2383 return IRQ_HANDLED;
2384}
2385
2386/*****************
2387 *
2388 * ACPI driver
2389 *
2390 *****************/
2391static int sony_pic_remove(struct acpi_device *device, int type)
2392{
2393 struct sony_pic_ioport *io, *tmp_io;
2394 struct sony_pic_irq *irq, *tmp_irq;
2395
2396 if (sony_pic_disable(device)) {
2397 printk(KERN_ERR DRV_PFX "Couldn't disable device.\n");
2398 return -ENXIO;
2399 }
2400
2401 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002402 release_region(spic_dev.cur_ioport->io1.minimum,
2403 spic_dev.cur_ioport->io1.address_length);
2404 if (spic_dev.cur_ioport->io2.minimum)
2405 release_region(spic_dev.cur_ioport->io2.minimum,
2406 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02002407
Mattia Dongili015a9162007-08-12 16:20:27 +09002408 sonypi_compat_exit();
2409
malattia@linux.it1549ee62007-04-09 10:19:08 +02002410 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02002411
malattia@linux.it49a11de2007-04-09 19:28:56 +02002412 /* pf attrs */
2413 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2414 sony_pf_remove();
2415
malattia@linux.it33a04452007-04-09 19:26:03 +02002416 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2417 list_del(&io->list);
2418 kfree(io);
2419 }
2420 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2421 list_del(&irq->list);
2422 kfree(irq);
2423 }
2424 spic_dev.cur_ioport = NULL;
2425 spic_dev.cur_irq = NULL;
2426
malattia@linux.itf6119b02007-04-09 19:31:06 +02002427 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002428 return 0;
2429}
2430
2431static int sony_pic_add(struct acpi_device *device)
2432{
2433 int result;
2434 struct sony_pic_ioport *io, *tmp_io;
2435 struct sony_pic_irq *irq, *tmp_irq;
2436
malattia@linux.itf6119b02007-04-09 19:31:06 +02002437 printk(KERN_INFO DRV_PFX "%s v%s.\n",
2438 SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.it33a04452007-04-09 19:26:03 +02002439
2440 spic_dev.acpi_dev = device;
2441 strcpy(acpi_device_class(device), "sony/hotkey");
2442 spic_dev.model = sony_pic_detect_device_type();
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002443 mutex_init(&spic_dev.lock);
malattia@linux.it33a04452007-04-09 19:26:03 +02002444
Mattia Dongili22a17782007-07-16 02:34:39 +09002445 /* model specific characteristics */
2446 switch(spic_dev.model) {
2447 case SONYPI_DEVICE_TYPE1:
2448 spic_dev.evport_offset = SONYPI_TYPE1_OFFSET;
2449 break;
2450 case SONYPI_DEVICE_TYPE3:
2451 spic_dev.evport_offset = SONYPI_TYPE3_OFFSET;
2452 break;
2453 case SONYPI_DEVICE_TYPE2:
2454 default:
2455 spic_dev.evport_offset = SONYPI_TYPE2_OFFSET;
2456 break;
2457 }
2458
malattia@linux.it33a04452007-04-09 19:26:03 +02002459 /* read _PRS resources */
2460 result = sony_pic_possible_resources(device);
2461 if (result) {
2462 printk(KERN_ERR DRV_PFX
2463 "Unabe to read possible resources.\n");
2464 goto err_free_resources;
2465 }
2466
2467 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05002468 result = sony_laptop_setup_input(device);
malattia@linux.it33a04452007-04-09 19:26:03 +02002469 if (result) {
2470 printk(KERN_ERR DRV_PFX
2471 "Unabe to create input devices.\n");
2472 goto err_free_resources;
2473 }
2474
Mattia Dongili015a9162007-08-12 16:20:27 +09002475 if (sonypi_compat_init())
2476 goto err_remove_input;
2477
malattia@linux.it33a04452007-04-09 19:26:03 +02002478 /* request io port */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002479 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
2480 if (request_region(io->io1.minimum, io->io1.address_length,
malattia@linux.it33a04452007-04-09 19:26:03 +02002481 "Sony Programable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002482 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
2483 io->io1.minimum, io->io1.maximum,
2484 io->io1.address_length);
2485 /* Type 1 have 2 ioports */
2486 if (io->io2.minimum) {
2487 if (request_region(io->io2.minimum,
2488 io->io2.address_length,
2489 "Sony Programable I/O Device")) {
2490 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
2491 io->io2.minimum, io->io2.maximum,
2492 io->io2.address_length);
2493 spic_dev.cur_ioport = io;
2494 break;
2495 }
2496 else {
2497 dprintk("Unable to get I/O port2: "
2498 "0x%.4x (0x%.4x) + 0x%.2x\n",
2499 io->io2.minimum, io->io2.maximum,
2500 io->io2.address_length);
2501 release_region(io->io1.minimum,
2502 io->io1.address_length);
2503 }
2504 }
2505 else {
2506 spic_dev.cur_ioport = io;
2507 break;
2508 }
malattia@linux.it33a04452007-04-09 19:26:03 +02002509 }
2510 }
2511 if (!spic_dev.cur_ioport) {
2512 printk(KERN_ERR DRV_PFX "Failed to request_region.\n");
2513 result = -ENODEV;
Mattia Dongili015a9162007-08-12 16:20:27 +09002514 goto err_remove_compat;
malattia@linux.it33a04452007-04-09 19:26:03 +02002515 }
2516
2517 /* request IRQ */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002518 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
malattia@linux.it33a04452007-04-09 19:26:03 +02002519 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
2520 IRQF_SHARED, "sony-laptop", &spic_dev)) {
2521 dprintk("IRQ: %d - triggering: %d - "
2522 "polarity: %d - shr: %d\n",
2523 irq->irq.interrupts[0],
2524 irq->irq.triggering,
2525 irq->irq.polarity,
2526 irq->irq.sharable);
2527 spic_dev.cur_irq = irq;
2528 break;
2529 }
2530 }
2531 if (!spic_dev.cur_irq) {
2532 printk(KERN_ERR DRV_PFX "Failed to request_irq.\n");
2533 result = -ENODEV;
2534 goto err_release_region;
2535 }
2536
2537 /* set resource status _SRS */
2538 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2539 if (result) {
2540 printk(KERN_ERR DRV_PFX "Couldn't enable device.\n");
2541 goto err_free_irq;
2542 }
2543
malattia@linux.it49a11de2007-04-09 19:28:56 +02002544 spic_dev.bluetooth_power = -1;
2545 /* create device attributes */
2546 result = sony_pf_add();
2547 if (result)
2548 goto err_disable_device;
2549
2550 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2551 if (result)
2552 goto err_remove_pf;
2553
malattia@linux.it33a04452007-04-09 19:26:03 +02002554 return 0;
2555
malattia@linux.it49a11de2007-04-09 19:28:56 +02002556err_remove_pf:
2557 sony_pf_remove();
2558
2559err_disable_device:
2560 sony_pic_disable(device);
2561
malattia@linux.it33a04452007-04-09 19:26:03 +02002562err_free_irq:
2563 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2564
2565err_release_region:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002566 release_region(spic_dev.cur_ioport->io1.minimum,
2567 spic_dev.cur_ioport->io1.address_length);
2568 if (spic_dev.cur_ioport->io2.minimum)
2569 release_region(spic_dev.cur_ioport->io2.minimum,
2570 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02002571
Mattia Dongili015a9162007-08-12 16:20:27 +09002572err_remove_compat:
2573 sonypi_compat_exit();
2574
malattia@linux.it33a04452007-04-09 19:26:03 +02002575err_remove_input:
malattia@linux.it1549ee62007-04-09 10:19:08 +02002576 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02002577
2578err_free_resources:
2579 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2580 list_del(&io->list);
2581 kfree(io);
2582 }
2583 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2584 list_del(&irq->list);
2585 kfree(irq);
2586 }
2587 spic_dev.cur_ioport = NULL;
2588 spic_dev.cur_irq = NULL;
2589
2590 return result;
2591}
2592
2593static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
2594{
2595 if (sony_pic_disable(device))
2596 return -ENXIO;
2597 return 0;
2598}
2599
2600static int sony_pic_resume(struct acpi_device *device)
2601{
2602 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2603 return 0;
2604}
2605
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002606static const struct acpi_device_id sony_pic_device_ids[] = {
2607 {SONY_PIC_HID, 0},
2608 {"", 0},
2609};
2610
malattia@linux.it33a04452007-04-09 19:26:03 +02002611static struct acpi_driver sony_pic_driver = {
2612 .name = SONY_PIC_DRIVER_NAME,
2613 .class = SONY_PIC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002614 .ids = sony_pic_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02002615 .owner = THIS_MODULE,
2616 .ops = {
2617 .add = sony_pic_add,
2618 .remove = sony_pic_remove,
2619 .suspend = sony_pic_suspend,
2620 .resume = sony_pic_resume,
2621 },
2622};
2623
2624static struct dmi_system_id __initdata sonypi_dmi_table[] = {
2625 {
2626 .ident = "Sony Vaio",
2627 .matches = {
2628 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2629 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
2630 },
2631 },
2632 {
2633 .ident = "Sony Vaio",
2634 .matches = {
2635 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2636 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
2637 },
2638 },
2639 { }
2640};
2641
malattia@linux.it59b19102007-04-09 10:19:04 +02002642static int __init sony_laptop_init(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01002643{
malattia@linux.it33a04452007-04-09 19:26:03 +02002644 int result;
2645
2646 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
2647 result = acpi_bus_register_driver(&sony_pic_driver);
2648 if (result) {
2649 printk(KERN_ERR DRV_PFX
2650 "Unable to register SPIC driver.");
2651 goto out;
2652 }
2653 }
2654
2655 result = acpi_bus_register_driver(&sony_nc_driver);
2656 if (result) {
2657 printk(KERN_ERR DRV_PFX "Unable to register SNC driver.");
2658 goto out_unregister_pic;
2659 }
2660
2661 return 0;
2662
2663out_unregister_pic:
2664 if (!no_spic)
2665 acpi_bus_unregister_driver(&sony_pic_driver);
2666out:
2667 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +01002668}
2669
malattia@linux.it59b19102007-04-09 10:19:04 +02002670static void __exit sony_laptop_exit(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01002671{
malattia@linux.it59b19102007-04-09 10:19:04 +02002672 acpi_bus_unregister_driver(&sony_nc_driver);
malattia@linux.it33a04452007-04-09 19:26:03 +02002673 if (!no_spic)
2674 acpi_bus_unregister_driver(&sony_pic_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +01002675}
2676
malattia@linux.it59b19102007-04-09 10:19:04 +02002677module_init(sony_laptop_init);
2678module_exit(sony_laptop_exit);