blob: 3e8f3aaa0923b02fc45226630aee012cbc006272 [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 *
17 * Copyright (C) 2001-2002 AlcĂ´ve <www.alcove.com>
18 *
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>
Stelian Pop7f09c432007-01-13 23:04:31 +010065
malattia@linux.it33a04452007-04-09 19:26:03 +020066#define DRV_PFX "sony-laptop: "
67#define LOG_PFX KERN_WARNING DRV_PFX
malattia@linux.itb9a218b2007-04-09 10:19:06 +020068#define dprintk(msg...) do { \
malattia@linux.it33a04452007-04-09 19:26:03 +020069 if (debug) printk(LOG_PFX msg); \
malattia@linux.itb9a218b2007-04-09 10:19:06 +020070} while (0)
71
malattia@linux.it33a04452007-04-09 19:26:03 +020072#define SONY_LAPTOP_DRIVER_VERSION "0.5"
Alessandro Guido50f62af2007-01-13 23:04:34 +010073
malattia@linux.it33a04452007-04-09 19:26:03 +020074#define SONY_NC_CLASS "sony-nc"
75#define SONY_NC_HID "SNY5001"
76#define SONY_NC_DRIVER_NAME "Sony Notebook Control"
77
78#define SONY_PIC_CLASS "sony-pic"
79#define SONY_PIC_HID "SNY6001"
80#define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control"
Stelian Pop7f09c432007-01-13 23:04:31 +010081
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010082MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
malattia@linux.it33a04452007-04-09 19:26:03 +020083MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
Stelian Pop7f09c432007-01-13 23:04:31 +010084MODULE_LICENSE("GPL");
malattia@linux.it33a04452007-04-09 19:26:03 +020085MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
Stelian Pop7f09c432007-01-13 23:04:31 +010086
87static int debug;
88module_param(debug, int, 0);
89MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
Len Browna02d1c12007-02-07 15:34:02 -050090 "the development of this driver");
Stelian Pop7f09c432007-01-13 23:04:31 +010091
malattia@linux.it33a04452007-04-09 19:26:03 +020092static int no_spic; /* = 0 */
93module_param(no_spic, int, 0444);
94MODULE_PARM_DESC(no_spic,
95 "set this if you don't want to enable the SPIC device");
96
97static int compat; /* = 0 */
98module_param(compat, int, 0444);
99MODULE_PARM_DESC(compat,
malattia@linux.it1549ee62007-04-09 10:19:08 +0200100 "set this if you want to enable backward compatibility mode for SPIC");
malattia@linux.it33a04452007-04-09 19:26:03 +0200101
102static unsigned long mask = 0xffffffff;
103module_param(mask, ulong, 0644);
104MODULE_PARM_DESC(mask,
105 "set this to the mask of event you want to enable (see doc)");
106
malattia@linux.it1549ee62007-04-09 10:19:08 +0200107/*********** Input Devices ***********/
108
109#define SONY_LAPTOP_BUF_SIZE 128
110struct sony_laptop_input_s {
111 atomic_t users;
112 struct input_dev *jog_dev;
113 struct input_dev *key_dev;
114 struct kfifo *fifo;
115 spinlock_t fifo_lock;
116 struct workqueue_struct *wq;
117};
118static struct sony_laptop_input_s sony_laptop_input = {
119 .users = ATOMIC_INIT(0),
120};
121
122struct sony_laptop_keypress {
123 struct input_dev *dev;
124 int key;
125};
126
127/* Correspondance table between sonypi events and input layer events */
128static struct {
129 int sonypiev;
130 int inputev;
131} sony_laptop_inputkeys[] = {
132 { SONYPI_EVENT_CAPTURE_PRESSED, KEY_CAMERA },
133 { SONYPI_EVENT_FNKEY_ONLY, KEY_FN },
134 { SONYPI_EVENT_FNKEY_ESC, KEY_FN_ESC },
135 { SONYPI_EVENT_FNKEY_F1, KEY_FN_F1 },
136 { SONYPI_EVENT_FNKEY_F2, KEY_FN_F2 },
137 { SONYPI_EVENT_FNKEY_F3, KEY_FN_F3 },
138 { SONYPI_EVENT_FNKEY_F4, KEY_FN_F4 },
139 { SONYPI_EVENT_FNKEY_F5, KEY_FN_F5 },
140 { SONYPI_EVENT_FNKEY_F6, KEY_FN_F6 },
141 { SONYPI_EVENT_FNKEY_F7, KEY_FN_F7 },
142 { SONYPI_EVENT_FNKEY_F8, KEY_FN_F8 },
143 { SONYPI_EVENT_FNKEY_F9, KEY_FN_F9 },
144 { SONYPI_EVENT_FNKEY_F10, KEY_FN_F10 },
145 { SONYPI_EVENT_FNKEY_F11, KEY_FN_F11 },
146 { SONYPI_EVENT_FNKEY_F12, KEY_FN_F12 },
147 { SONYPI_EVENT_FNKEY_1, KEY_FN_1 },
148 { SONYPI_EVENT_FNKEY_2, KEY_FN_2 },
149 { SONYPI_EVENT_FNKEY_D, KEY_FN_D },
150 { SONYPI_EVENT_FNKEY_E, KEY_FN_E },
151 { SONYPI_EVENT_FNKEY_F, KEY_FN_F },
152 { SONYPI_EVENT_FNKEY_S, KEY_FN_S },
153 { SONYPI_EVENT_FNKEY_B, KEY_FN_B },
154 { SONYPI_EVENT_BLUETOOTH_PRESSED, KEY_BLUE },
155 { SONYPI_EVENT_BLUETOOTH_ON, KEY_BLUE },
156 { SONYPI_EVENT_PKEY_P1, KEY_PROG1 },
157 { SONYPI_EVENT_PKEY_P2, KEY_PROG2 },
158 { SONYPI_EVENT_PKEY_P3, KEY_PROG3 },
159 { SONYPI_EVENT_BACK_PRESSED, KEY_BACK },
160 { SONYPI_EVENT_HELP_PRESSED, KEY_HELP },
161 { SONYPI_EVENT_ZOOM_PRESSED, KEY_ZOOM },
162 { SONYPI_EVENT_THUMBPHRASE_PRESSED, BTN_THUMB },
163 { 0, 0 },
164};
165
166/* release buttons after a short delay if pressed */
167static void do_sony_laptop_release_key(struct work_struct *work)
168{
169 struct sony_laptop_keypress kp;
170
171 while (kfifo_get(sony_laptop_input.fifo, (unsigned char *)&kp,
172 sizeof(kp)) == sizeof(kp)) {
173 msleep(10);
174 input_report_key(kp.dev, kp.key, 0);
175 input_sync(kp.dev);
176 }
177}
178static DECLARE_WORK(sony_laptop_release_key_work,
179 do_sony_laptop_release_key);
180
181/* forward event to the input subsytem */
182static void sony_laptop_report_input_event(u8 event)
183{
184 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
185 struct input_dev *key_dev = sony_laptop_input.key_dev;
186 struct sony_laptop_keypress kp = { NULL };
187 int i;
188
189 if (event == SONYPI_EVENT_FNKEY_RELEASED) {
190 /* Nothing, not all VAIOs generate this event */
191 return;
192 }
193
194 /* report events */
195 switch (event) {
196 /* jog_dev events */
197 case SONYPI_EVENT_JOGDIAL_UP:
198 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
199 input_report_rel(jog_dev, REL_WHEEL, 1);
200 input_sync(jog_dev);
201 return;
202
203 case SONYPI_EVENT_JOGDIAL_DOWN:
204 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
205 input_report_rel(jog_dev, REL_WHEEL, -1);
206 input_sync(jog_dev);
207 return;
208
209 /* key_dev events */
210 case SONYPI_EVENT_JOGDIAL_PRESSED:
211 kp.key = BTN_MIDDLE;
212 kp.dev = jog_dev;
213 break;
214
215 default:
216 for (i = 0; sony_laptop_inputkeys[i].sonypiev; i++)
217 if (event == sony_laptop_inputkeys[i].sonypiev) {
218 kp.dev = key_dev;
219 kp.key = sony_laptop_inputkeys[i].inputev;
220 break;
221 }
222 break;
223 }
224
225 if (kp.dev) {
226 input_report_key(kp.dev, kp.key, 1);
227 input_sync(kp.dev);
228 kfifo_put(sony_laptop_input.fifo,
229 (unsigned char *)&kp, sizeof(kp));
230
231 if (!work_pending(&sony_laptop_release_key_work))
232 queue_work(sony_laptop_input.wq,
233 &sony_laptop_release_key_work);
234 } else
235 dprintk("unknown input event %.2x\n", event);
236}
237
238static int sony_laptop_setup_input(void)
239{
240 struct input_dev *jog_dev;
241 struct input_dev *key_dev;
242 int i;
243 int error;
244
245 /* don't run again if already initialized */
246 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
247 return 0;
248
249 /* kfifo */
250 spin_lock_init(&sony_laptop_input.fifo_lock);
251 sony_laptop_input.fifo =
252 kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
253 &sony_laptop_input.fifo_lock);
254 if (IS_ERR(sony_laptop_input.fifo)) {
255 printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
256 error = PTR_ERR(sony_laptop_input.fifo);
257 goto err_dec_users;
258 }
259
260 /* init workqueue */
261 sony_laptop_input.wq = create_singlethread_workqueue("sony-laptop");
262 if (!sony_laptop_input.wq) {
263 printk(KERN_ERR DRV_PFX
264 "Unabe to create workqueue.\n");
265 error = -ENXIO;
266 goto err_free_kfifo;
267 }
268
269 /* input keys */
270 key_dev = input_allocate_device();
271 if (!key_dev) {
272 error = -ENOMEM;
273 goto err_destroy_wq;
274 }
275
276 key_dev->name = "Sony Vaio Keys";
277 key_dev->id.bustype = BUS_ISA;
278 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
279
280 /* Initialize the Input Drivers: special keys */
281 key_dev->evbit[0] = BIT(EV_KEY);
282 for (i = 0; sony_laptop_inputkeys[i].sonypiev; i++)
283 if (sony_laptop_inputkeys[i].inputev)
284 set_bit(sony_laptop_inputkeys[i].inputev,
285 key_dev->keybit);
286
287 error = input_register_device(key_dev);
288 if (error)
289 goto err_free_keydev;
290
291 sony_laptop_input.key_dev = key_dev;
292
293 /* jogdial */
294 jog_dev = input_allocate_device();
295 if (!jog_dev) {
296 error = -ENOMEM;
297 goto err_unregister_keydev;
298 }
299
300 jog_dev->name = "Sony Vaio Jogdial";
301 jog_dev->id.bustype = BUS_ISA;
302 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
303
304 jog_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
305 jog_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_MIDDLE);
306 jog_dev->relbit[0] = BIT(REL_WHEEL);
307
308 error = input_register_device(jog_dev);
309 if (error)
310 goto err_free_jogdev;
311
312 sony_laptop_input.jog_dev = jog_dev;
313
314 return 0;
315
316err_free_jogdev:
317 input_free_device(jog_dev);
318
319err_unregister_keydev:
320 input_unregister_device(key_dev);
321 /* to avoid kref underflow below at input_free_device */
322 key_dev = NULL;
323
324err_free_keydev:
325 input_free_device(key_dev);
326
327err_destroy_wq:
328 destroy_workqueue(sony_laptop_input.wq);
329
330err_free_kfifo:
331 kfifo_free(sony_laptop_input.fifo);
332
333err_dec_users:
334 atomic_dec(&sony_laptop_input.users);
335 return error;
336}
337
338static void sony_laptop_remove_input(void)
339{
340 /* cleanup only after the last user has gone */
341 if (!atomic_dec_and_test(&sony_laptop_input.users))
342 return;
343
344 /* flush workqueue first */
345 flush_workqueue(sony_laptop_input.wq);
346
347 /* destroy input devs */
348 input_unregister_device(sony_laptop_input.key_dev);
349 sony_laptop_input.key_dev = NULL;
350
351 if (sony_laptop_input.jog_dev) {
352 input_unregister_device(sony_laptop_input.jog_dev);
353 sony_laptop_input.jog_dev = NULL;
354 }
355
356 destroy_workqueue(sony_laptop_input.wq);
357 kfifo_free(sony_laptop_input.fifo);
358}
359
malattia@linux.it56b87562007-04-09 10:19:05 +0200360/*********** Platform Device ***********/
361
362static atomic_t sony_pf_users = ATOMIC_INIT(0);
363static struct platform_driver sony_pf_driver = {
364 .driver = {
365 .name = "sony-laptop",
366 .owner = THIS_MODULE,
367 }
368};
369static struct platform_device *sony_pf_device;
370
371static int sony_pf_add(void)
372{
373 int ret = 0;
374
375 /* don't run again if already initialized */
376 if (atomic_add_return(1, &sony_pf_users) > 1)
377 return 0;
378
379 ret = platform_driver_register(&sony_pf_driver);
380 if (ret)
381 goto out;
382
383 sony_pf_device = platform_device_alloc("sony-laptop", -1);
384 if (!sony_pf_device) {
385 ret = -ENOMEM;
386 goto out_platform_registered;
387 }
388
389 ret = platform_device_add(sony_pf_device);
390 if (ret)
391 goto out_platform_alloced;
392
393 return 0;
394
395 out_platform_alloced:
396 platform_device_put(sony_pf_device);
397 sony_pf_device = NULL;
398 out_platform_registered:
399 platform_driver_unregister(&sony_pf_driver);
400 out:
401 atomic_dec(&sony_pf_users);
402 return ret;
403}
404
405static void sony_pf_remove(void)
406{
407 /* deregister only after the last user has gone */
408 if (!atomic_dec_and_test(&sony_pf_users))
409 return;
410
411 platform_device_del(sony_pf_device);
412 platform_device_put(sony_pf_device);
413 platform_driver_unregister(&sony_pf_driver);
414}
415
416/*********** SNC (SNY5001) Device ***********/
417
malattia@linux.it33a04452007-04-09 19:26:03 +0200418/* the device uses 1-based values, while the backlight subsystem uses
419 0-based values */
420#define SONY_MAX_BRIGHTNESS 8
421
422#define SNC_VALIDATE_IN 0
423#define SNC_VALIDATE_OUT 1
424
malattia@linux.it59b19102007-04-09 10:19:04 +0200425static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500426 char *);
malattia@linux.it59b19102007-04-09 10:19:04 +0200427static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500428 const char *, size_t);
Mattia Dongili156c2212007-02-12 22:01:07 +0100429static int boolean_validate(const int, const int);
430static int brightness_default_validate(const int, const int);
431
malattia@linux.it59b19102007-04-09 10:19:04 +0200432struct sony_nc_value {
Len Browna02d1c12007-02-07 15:34:02 -0500433 char *name; /* name of the entry */
434 char **acpiget; /* names of the ACPI get function */
435 char **acpiset; /* names of the ACPI set function */
Mattia Dongili156c2212007-02-12 22:01:07 +0100436 int (*validate)(const int, const int); /* input/output validation */
Len Browna02d1c12007-02-07 15:34:02 -0500437 int value; /* current setting */
438 int valid; /* Has ever been set */
439 int debug; /* active only in debug mode ? */
440 struct device_attribute devattr; /* sysfs atribute */
Stelian Pop7f09c432007-01-13 23:04:31 +0100441};
442
malattia@linux.it59b19102007-04-09 10:19:04 +0200443#define SNC_HANDLE_NAMES(_name, _values...) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100444 static char *snc_##_name[] = { _values, NULL }
445
malattia@linux.it59b19102007-04-09 10:19:04 +0200446#define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100447 { \
448 .name = __stringify(_name), \
449 .acpiget = _getters, \
450 .acpiset = _setters, \
Mattia Dongili156c2212007-02-12 22:01:07 +0100451 .validate = _validate, \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100452 .debug = _debug, \
malattia@linux.it59b19102007-04-09 10:19:04 +0200453 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100454 }
455
malattia@linux.it59b19102007-04-09 10:19:04 +0200456#define SNC_HANDLE_NULL { .name = NULL }
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100457
malattia@linux.it59b19102007-04-09 10:19:04 +0200458SNC_HANDLE_NAMES(fnkey_get, "GHKE");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100459
malattia@linux.it59b19102007-04-09 10:19:04 +0200460SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
461SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100462
malattia@linux.it59b19102007-04-09 10:19:04 +0200463SNC_HANDLE_NAMES(cdpower_get, "GCDP");
464SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100465
malattia@linux.it59b19102007-04-09 10:19:04 +0200466SNC_HANDLE_NAMES(audiopower_get, "GAZP");
467SNC_HANDLE_NAMES(audiopower_set, "AZPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100468
malattia@linux.it59b19102007-04-09 10:19:04 +0200469SNC_HANDLE_NAMES(lanpower_get, "GLNP");
470SNC_HANDLE_NAMES(lanpower_set, "LNPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100471
malattia@linux.it59b19102007-04-09 10:19:04 +0200472SNC_HANDLE_NAMES(PID_get, "GPID");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100473
malattia@linux.it59b19102007-04-09 10:19:04 +0200474SNC_HANDLE_NAMES(CTR_get, "GCTR");
475SNC_HANDLE_NAMES(CTR_set, "SCTR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100476
malattia@linux.it59b19102007-04-09 10:19:04 +0200477SNC_HANDLE_NAMES(PCR_get, "GPCR");
478SNC_HANDLE_NAMES(PCR_set, "SPCR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100479
malattia@linux.it59b19102007-04-09 10:19:04 +0200480SNC_HANDLE_NAMES(CMI_get, "GCMI");
481SNC_HANDLE_NAMES(CMI_set, "SCMI");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100482
malattia@linux.it59b19102007-04-09 10:19:04 +0200483static struct sony_nc_value sony_nc_values[] = {
484 SNC_HANDLE(brightness_default, snc_brightness_def_get,
Mattia Dongili156c2212007-02-12 22:01:07 +0100485 snc_brightness_def_set, brightness_default_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200486 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
487 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
488 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100489 boolean_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200490 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100491 boolean_validate, 1),
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100492 /* unknown methods */
malattia@linux.it59b19102007-04-09 10:19:04 +0200493 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
494 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
495 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
496 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
497 SNC_HANDLE_NULL
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100498};
499
malattia@linux.it59b19102007-04-09 10:19:04 +0200500static acpi_handle sony_nc_acpi_handle;
501static struct acpi_device *sony_nc_acpi_device = NULL;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100502
Mattia Dongilid78865c2007-02-07 20:01:56 +0100503/*
504 * acpi_evaluate_object wrappers
505 */
Stelian Pop7f09c432007-01-13 23:04:31 +0100506static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
507{
508 struct acpi_buffer output;
509 union acpi_object out_obj;
510 acpi_status status;
511
512 output.length = sizeof(out_obj);
513 output.pointer = &out_obj;
514
515 status = acpi_evaluate_object(handle, name, NULL, &output);
516 if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
517 *result = out_obj.integer.value;
518 return 0;
519 }
520
521 printk(LOG_PFX "acpi_callreadfunc failed\n");
522
523 return -1;
524}
525
526static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
527 int *result)
528{
529 struct acpi_object_list params;
530 union acpi_object in_obj;
531 struct acpi_buffer output;
532 union acpi_object out_obj;
533 acpi_status status;
534
535 params.count = 1;
536 params.pointer = &in_obj;
537 in_obj.type = ACPI_TYPE_INTEGER;
538 in_obj.integer.value = value;
539
540 output.length = sizeof(out_obj);
541 output.pointer = &out_obj;
542
543 status = acpi_evaluate_object(handle, name, &params, &output);
544 if (status == AE_OK) {
545 if (result != NULL) {
546 if (out_obj.type != ACPI_TYPE_INTEGER) {
547 printk(LOG_PFX "acpi_evaluate_object bad "
548 "return type\n");
549 return -1;
550 }
551 *result = out_obj.integer.value;
552 }
553 return 0;
554 }
555
556 printk(LOG_PFX "acpi_evaluate_object failed\n");
557
558 return -1;
559}
560
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100561/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200562 * sony_nc_values input/output validate functions
Mattia Dongili156c2212007-02-12 22:01:07 +0100563 */
564
565/* brightness_default_validate:
566 *
567 * manipulate input output values to keep consistency with the
568 * backlight framework for which brightness values are 0-based.
569 */
570static int brightness_default_validate(const int direction, const int value)
571{
572 switch (direction) {
573 case SNC_VALIDATE_OUT:
574 return value - 1;
575 case SNC_VALIDATE_IN:
576 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
577 return value + 1;
578 }
579 return -EINVAL;
580}
581
582/* boolean_validate:
583 *
584 * on input validate boolean values 0/1, on output just pass the
585 * received value.
586 */
587static int boolean_validate(const int direction, const int value)
588{
589 if (direction == SNC_VALIDATE_IN) {
590 if (value != 0 && value != 1)
591 return -EINVAL;
592 }
593 return value;
594}
595
596/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200597 * Sysfs show/store common to all sony_nc_values
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100598 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200599static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
Len Browna02d1c12007-02-07 15:34:02 -0500600 char *buffer)
Stelian Pop7f09c432007-01-13 23:04:31 +0100601{
Stelian Pop7f09c432007-01-13 23:04:31 +0100602 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200603 struct sony_nc_value *item =
604 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100605
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100606 if (!*item->acpiget)
Stelian Pop7f09c432007-01-13 23:04:31 +0100607 return -EIO;
608
malattia@linux.it59b19102007-04-09 10:19:04 +0200609 if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100610 return -EIO;
611
Mattia Dongili156c2212007-02-12 22:01:07 +0100612 if (item->validate)
613 value = item->validate(SNC_VALIDATE_OUT, value);
614
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100615 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
Stelian Pop7f09c432007-01-13 23:04:31 +0100616}
617
malattia@linux.it59b19102007-04-09 10:19:04 +0200618static ssize_t sony_nc_sysfs_store(struct device *dev,
Len Browna02d1c12007-02-07 15:34:02 -0500619 struct device_attribute *attr,
620 const char *buffer, size_t count)
Stelian Pop7f09c432007-01-13 23:04:31 +0100621{
Stelian Pop7f09c432007-01-13 23:04:31 +0100622 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200623 struct sony_nc_value *item =
624 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100625
626 if (!item->acpiset)
627 return -EIO;
628
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100629 if (count > 31)
630 return -EINVAL;
631
632 value = simple_strtoul(buffer, NULL, 10);
Stelian Pop7f09c432007-01-13 23:04:31 +0100633
Mattia Dongili156c2212007-02-12 22:01:07 +0100634 if (item->validate)
635 value = item->validate(SNC_VALIDATE_IN, value);
636
637 if (value < 0)
638 return value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100639
malattia@linux.it59b19102007-04-09 10:19:04 +0200640 if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100641 return -EIO;
Andrew Morton3f4f4612007-01-13 23:04:32 +0100642 item->value = value;
643 item->valid = 1;
Stelian Pop7f09c432007-01-13 23:04:31 +0100644 return count;
645}
646
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100647
Mattia Dongilid78865c2007-02-07 20:01:56 +0100648/*
649 * Backlight device
650 */
651static int sony_backlight_update_status(struct backlight_device *bd)
Andrew Morton3f4f4612007-01-13 23:04:32 +0100652{
malattia@linux.it59b19102007-04-09 10:19:04 +0200653 return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
Richard Purdie321709c2007-02-10 15:04:08 +0000654 bd->props.brightness + 1, NULL);
Andrew Morton3f4f4612007-01-13 23:04:32 +0100655}
656
Mattia Dongilid78865c2007-02-07 20:01:56 +0100657static int sony_backlight_get_brightness(struct backlight_device *bd)
658{
659 int value;
660
malattia@linux.it59b19102007-04-09 10:19:04 +0200661 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
Mattia Dongilid78865c2007-02-07 20:01:56 +0100662 return 0;
663 /* brightness levels are 1-based, while backlight ones are 0-based */
664 return value - 1;
665}
666
667static struct backlight_device *sony_backlight_device;
Richard Purdie321709c2007-02-10 15:04:08 +0000668static struct backlight_ops sony_backlight_ops = {
Len Browna02d1c12007-02-07 15:34:02 -0500669 .update_status = sony_backlight_update_status,
670 .get_brightness = sony_backlight_get_brightness,
Mattia Dongilid78865c2007-02-07 20:01:56 +0100671};
672
673/*
674 * ACPI callbacks
675 */
Stelian Pop7f09c432007-01-13 23:04:31 +0100676static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
677{
malattia@linux.itb9a218b2007-04-09 10:19:06 +0200678 dprintk("sony_acpi_notify, event: %d\n", event);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200679 sony_laptop_report_input_event(event);
malattia@linux.it59b19102007-04-09 10:19:04 +0200680 acpi_bus_generate_event(sony_nc_acpi_device, 1, event);
Stelian Pop7f09c432007-01-13 23:04:31 +0100681}
682
683static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
684 void *context, void **return_value)
685{
686 struct acpi_namespace_node *node;
687 union acpi_operand_object *operand;
688
Len Browna02d1c12007-02-07 15:34:02 -0500689 node = (struct acpi_namespace_node *)handle;
690 operand = (union acpi_operand_object *)node->object;
Stelian Pop7f09c432007-01-13 23:04:31 +0100691
692 printk(LOG_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
693 (u32) operand->method.param_count);
694
695 return AE_OK;
696}
697
Mattia Dongilid78865c2007-02-07 20:01:56 +0100698/*
699 * ACPI device
700 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200701static int sony_nc_resume(struct acpi_device *device)
Mattia Dongilid78865c2007-02-07 20:01:56 +0100702{
malattia@linux.it59b19102007-04-09 10:19:04 +0200703 struct sony_nc_value *item;
Mattia Dongilid78865c2007-02-07 20:01:56 +0100704
malattia@linux.it59b19102007-04-09 10:19:04 +0200705 for (item = sony_nc_values; item->name; item++) {
Mattia Dongilid78865c2007-02-07 20:01:56 +0100706 int ret;
707
708 if (!item->valid)
709 continue;
malattia@linux.it59b19102007-04-09 10:19:04 +0200710 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
Len Browna02d1c12007-02-07 15:34:02 -0500711 item->value, NULL);
Mattia Dongilid78865c2007-02-07 20:01:56 +0100712 if (ret < 0) {
713 printk("%s: %d\n", __FUNCTION__, ret);
714 break;
715 }
716 }
717 return 0;
718}
719
malattia@linux.it59b19102007-04-09 10:19:04 +0200720static int sony_nc_add(struct acpi_device *device)
Stelian Pop7f09c432007-01-13 23:04:31 +0100721{
722 acpi_status status;
Andrew Morton8607c672007-03-06 02:29:42 -0800723 int result = 0;
Alessandro Guido50f62af2007-01-13 23:04:34 +0100724 acpi_handle handle;
malattia@linux.it56b87562007-04-09 10:19:05 +0200725 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +0100726
malattia@linux.it59b19102007-04-09 10:19:04 +0200727 sony_nc_acpi_device = device;
malattia@linux.it33a04452007-04-09 19:26:03 +0200728 strcpy(acpi_device_class(device), "sony/hotkey");
Stelian Popc5611622007-01-13 23:04:37 +0100729
malattia@linux.it59b19102007-04-09 10:19:04 +0200730 sony_nc_acpi_handle = device->handle;
Stelian Pop7f09c432007-01-13 23:04:31 +0100731
Stelian Pop7f09c432007-01-13 23:04:31 +0100732 if (debug) {
malattia@linux.it59b19102007-04-09 10:19:04 +0200733 status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_nc_acpi_handle,
Stelian Pop7f09c432007-01-13 23:04:31 +0100734 1, sony_walk_callback, NULL, NULL);
735 if (ACPI_FAILURE(status)) {
736 printk(LOG_PFX "unable to walk acpi resources\n");
737 result = -ENODEV;
738 goto outwalk;
739 }
Stelian Popc5611622007-01-13 23:04:37 +0100740 }
Stelian Pop7f09c432007-01-13 23:04:31 +0100741
malattia@linux.it1549ee62007-04-09 10:19:08 +0200742 /* setup input devices and helper fifo */
743 result = sony_laptop_setup_input();
744 if (result) {
745 printk(KERN_ERR DRV_PFX
746 "Unabe to create input devices.\n");
747 goto outwalk;
748 }
749
malattia@linux.it59b19102007-04-09 10:19:04 +0200750 status = acpi_install_notify_handler(sony_nc_acpi_handle,
Stelian Popc5611622007-01-13 23:04:37 +0100751 ACPI_DEVICE_NOTIFY,
Len Browna02d1c12007-02-07 15:34:02 -0500752 sony_acpi_notify, NULL);
Stelian Popc5611622007-01-13 23:04:37 +0100753 if (ACPI_FAILURE(status)) {
754 printk(LOG_PFX "unable to install notify handler\n");
755 result = -ENODEV;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200756 goto outinput;
Stelian Pop7f09c432007-01-13 23:04:31 +0100757 }
758
malattia@linux.it59b19102007-04-09 10:19:04 +0200759 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT", &handle))) {
Alessandro Guido50f62af2007-01-13 23:04:34 +0100760 sony_backlight_device = backlight_device_register("sony", NULL,
Len Browna02d1c12007-02-07 15:34:02 -0500761 NULL,
Richard Purdie321709c2007-02-10 15:04:08 +0000762 &sony_backlight_ops);
Mattia Dongili7df03b82007-01-13 23:04:41 +0100763
Len Browna02d1c12007-02-07 15:34:02 -0500764 if (IS_ERR(sony_backlight_device)) {
Mattia Dongili91fbc1d2007-02-07 20:01:53 +0100765 printk(LOG_PFX "unable to register backlight device\n");
Mattia Dongili7df03b82007-01-13 23:04:41 +0100766 sony_backlight_device = NULL;
Richard Purdie321709c2007-02-10 15:04:08 +0000767 } else {
768 sony_backlight_device->props.brightness =
Len Browna02d1c12007-02-07 15:34:02 -0500769 sony_backlight_get_brightness
770 (sony_backlight_device);
Richard Purdie321709c2007-02-10 15:04:08 +0000771 sony_backlight_device->props.max_brightness =
772 SONY_MAX_BRIGHTNESS - 1;
773 }
774
Alessandro Guido50f62af2007-01-13 23:04:34 +0100775 }
Stelian Pop7f09c432007-01-13 23:04:31 +0100776
malattia@linux.it49a11de2007-04-09 19:28:56 +0200777 result = sony_pf_add();
778 if (result)
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100779 goto outbacklight;
Stelian Pop7f09c432007-01-13 23:04:31 +0100780
malattia@linux.it56b87562007-04-09 10:19:05 +0200781 /* create sony_pf sysfs attributes related to the SNC device */
782 for (item = sony_nc_values; item->name; ++item) {
783
784 if (!debug && item->debug)
785 continue;
786
787 /* find the available acpiget as described in the DSDT */
788 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
789 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
790 *item->acpiget,
791 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +0200792 dprintk("Found %s getter: %s\n",
793 item->name, *item->acpiget);
malattia@linux.it56b87562007-04-09 10:19:05 +0200794 item->devattr.attr.mode |= S_IRUGO;
795 break;
796 }
797 }
798
799 /* find the available acpiset as described in the DSDT */
800 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
801 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
802 *item->acpiset,
803 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +0200804 dprintk("Found %s setter: %s\n",
805 item->name, *item->acpiset);
malattia@linux.it56b87562007-04-09 10:19:05 +0200806 item->devattr.attr.mode |= S_IWUSR;
807 break;
808 }
809 }
810
811 if (item->devattr.attr.mode != 0) {
812 result =
813 device_create_file(&sony_pf_device->dev,
814 &item->devattr);
815 if (result)
816 goto out_sysfs;
817 }
818 }
819
malattia@linux.it59b19102007-04-09 10:19:04 +0200820 printk(KERN_INFO SONY_NC_DRIVER_NAME " successfully installed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100821
822 return 0;
823
malattia@linux.it56b87562007-04-09 10:19:05 +0200824 out_sysfs:
825 for (item = sony_nc_values; item->name; ++item) {
826 device_remove_file(&sony_pf_device->dev, &item->devattr);
827 }
828 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +0200829
Len Browna02d1c12007-02-07 15:34:02 -0500830 outbacklight:
Mattia Dongili7df03b82007-01-13 23:04:41 +0100831 if (sony_backlight_device)
832 backlight_device_unregister(sony_backlight_device);
833
malattia@linux.it59b19102007-04-09 10:19:04 +0200834 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
Stelian Popc5611622007-01-13 23:04:37 +0100835 ACPI_DEVICE_NOTIFY,
836 sony_acpi_notify);
837 if (ACPI_FAILURE(status))
838 printk(LOG_PFX "unable to remove notify handler\n");
malattia@linux.it1549ee62007-04-09 10:19:08 +0200839
840 outinput:
841 sony_laptop_remove_input();
842
Len Browna02d1c12007-02-07 15:34:02 -0500843 outwalk:
Stelian Pop7f09c432007-01-13 23:04:31 +0100844 return result;
845}
846
malattia@linux.it59b19102007-04-09 10:19:04 +0200847static int sony_nc_remove(struct acpi_device *device, int type)
Stelian Pop7f09c432007-01-13 23:04:31 +0100848{
849 acpi_status status;
malattia@linux.it56b87562007-04-09 10:19:05 +0200850 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +0100851
Alessandro Guido50f62af2007-01-13 23:04:34 +0100852 if (sony_backlight_device)
853 backlight_device_unregister(sony_backlight_device);
854
malattia@linux.it59b19102007-04-09 10:19:04 +0200855 sony_nc_acpi_device = NULL;
Stelian Popc5611622007-01-13 23:04:37 +0100856
malattia@linux.it59b19102007-04-09 10:19:04 +0200857 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
Stelian Popc5611622007-01-13 23:04:37 +0100858 ACPI_DEVICE_NOTIFY,
859 sony_acpi_notify);
860 if (ACPI_FAILURE(status))
861 printk(LOG_PFX "unable to remove notify handler\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100862
malattia@linux.it56b87562007-04-09 10:19:05 +0200863 for (item = sony_nc_values; item->name; ++item) {
864 device_remove_file(&sony_pf_device->dev, &item->devattr);
865 }
866
867 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +0200868 sony_laptop_remove_input();
Stelian Pop7f09c432007-01-13 23:04:31 +0100869
malattia@linux.it59b19102007-04-09 10:19:04 +0200870 printk(KERN_INFO SONY_NC_DRIVER_NAME " successfully removed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100871
872 return 0;
873}
874
malattia@linux.it59b19102007-04-09 10:19:04 +0200875static struct acpi_driver sony_nc_driver = {
876 .name = SONY_NC_DRIVER_NAME,
877 .class = SONY_NC_CLASS,
878 .ids = SONY_NC_HID,
malattia@linux.it33a04452007-04-09 19:26:03 +0200879 .owner = THIS_MODULE,
Len Browna02d1c12007-02-07 15:34:02 -0500880 .ops = {
malattia@linux.it59b19102007-04-09 10:19:04 +0200881 .add = sony_nc_add,
882 .remove = sony_nc_remove,
883 .resume = sony_nc_resume,
Len Browna02d1c12007-02-07 15:34:02 -0500884 },
Andrew Morton3f4f4612007-01-13 23:04:32 +0100885};
886
malattia@linux.it33a04452007-04-09 19:26:03 +0200887/*********** SPIC (SNY6001) Device ***********/
888
889#define SONYPI_DEVICE_TYPE1 0x00000001
890#define SONYPI_DEVICE_TYPE2 0x00000002
891#define SONYPI_DEVICE_TYPE3 0x00000004
892
malattia@linux.it33a04452007-04-09 19:26:03 +0200893#define SONY_PIC_EV_MASK 0xff
894
malattia@linux.it33a04452007-04-09 19:26:03 +0200895struct sony_pic_ioport {
896 struct acpi_resource_io io;
897 struct list_head list;
898};
899
900struct sony_pic_irq {
901 struct acpi_resource_irq irq;
902 struct list_head list;
903};
904
905struct sony_pic_dev {
906 int model;
malattia@linux.it49a11de2007-04-09 19:28:56 +0200907 u8 camera_power;
908 u8 bluetooth_power;
malattia@linux.it33a04452007-04-09 19:26:03 +0200909 struct acpi_device *acpi_dev;
910 struct sony_pic_irq *cur_irq;
911 struct sony_pic_ioport *cur_ioport;
912 struct list_head interrupts;
913 struct list_head ioports;
malattia@linux.it33a04452007-04-09 19:26:03 +0200914};
915
916static struct sony_pic_dev spic_dev = {
917 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
918 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
919};
920
921/* Event masks */
922#define SONYPI_JOGGER_MASK 0x00000001
923#define SONYPI_CAPTURE_MASK 0x00000002
924#define SONYPI_FNKEY_MASK 0x00000004
925#define SONYPI_BLUETOOTH_MASK 0x00000008
926#define SONYPI_PKEY_MASK 0x00000010
927#define SONYPI_BACK_MASK 0x00000020
928#define SONYPI_HELP_MASK 0x00000040
929#define SONYPI_LID_MASK 0x00000080
930#define SONYPI_ZOOM_MASK 0x00000100
931#define SONYPI_THUMBPHRASE_MASK 0x00000200
932#define SONYPI_MEYE_MASK 0x00000400
933#define SONYPI_MEMORYSTICK_MASK 0x00000800
934#define SONYPI_BATTERY_MASK 0x00001000
935#define SONYPI_WIRELESS_MASK 0x00002000
936
937struct sonypi_event {
938 u8 data;
939 u8 event;
940};
941
942/* The set of possible button release events */
943static struct sonypi_event sonypi_releaseev[] = {
944 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
945 { 0, 0 }
946};
947
948/* The set of possible jogger events */
949static struct sonypi_event sonypi_joggerev[] = {
950 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
951 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
952 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
953 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
954 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
955 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
956 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
957 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
958 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
959 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
960 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
961 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
962 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
963 { 0, 0 }
964};
965
966/* The set of possible capture button events */
967static struct sonypi_event sonypi_captureev[] = {
968 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
969 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
970 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
971 { 0, 0 }
972};
973
974/* The set of possible fnkeys events */
975static struct sonypi_event sonypi_fnkeyev[] = {
976 { 0x10, SONYPI_EVENT_FNKEY_ESC },
977 { 0x11, SONYPI_EVENT_FNKEY_F1 },
978 { 0x12, SONYPI_EVENT_FNKEY_F2 },
979 { 0x13, SONYPI_EVENT_FNKEY_F3 },
980 { 0x14, SONYPI_EVENT_FNKEY_F4 },
981 { 0x15, SONYPI_EVENT_FNKEY_F5 },
982 { 0x16, SONYPI_EVENT_FNKEY_F6 },
983 { 0x17, SONYPI_EVENT_FNKEY_F7 },
984 { 0x18, SONYPI_EVENT_FNKEY_F8 },
985 { 0x19, SONYPI_EVENT_FNKEY_F9 },
986 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
987 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
988 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
989 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
990 { 0x21, SONYPI_EVENT_FNKEY_1 },
991 { 0x22, SONYPI_EVENT_FNKEY_2 },
992 { 0x31, SONYPI_EVENT_FNKEY_D },
993 { 0x32, SONYPI_EVENT_FNKEY_E },
994 { 0x33, SONYPI_EVENT_FNKEY_F },
995 { 0x34, SONYPI_EVENT_FNKEY_S },
996 { 0x35, SONYPI_EVENT_FNKEY_B },
997 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
998 { 0, 0 }
999};
1000
1001/* The set of possible program key events */
1002static struct sonypi_event sonypi_pkeyev[] = {
1003 { 0x01, SONYPI_EVENT_PKEY_P1 },
1004 { 0x02, SONYPI_EVENT_PKEY_P2 },
1005 { 0x04, SONYPI_EVENT_PKEY_P3 },
1006 { 0x5c, SONYPI_EVENT_PKEY_P1 },
1007 { 0, 0 }
1008};
1009
1010/* The set of possible bluetooth events */
1011static struct sonypi_event sonypi_blueev[] = {
1012 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
1013 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
1014 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
1015 { 0, 0 }
1016};
1017
1018/* The set of possible wireless events */
1019static struct sonypi_event sonypi_wlessev[] = {
1020 { 0x59, SONYPI_EVENT_WIRELESS_ON },
1021 { 0x5a, SONYPI_EVENT_WIRELESS_OFF },
1022 { 0, 0 }
1023};
1024
1025/* The set of possible back button events */
1026static struct sonypi_event sonypi_backev[] = {
1027 { 0x20, SONYPI_EVENT_BACK_PRESSED },
1028 { 0, 0 }
1029};
1030
1031/* The set of possible help button events */
1032static struct sonypi_event sonypi_helpev[] = {
1033 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
1034 { 0, 0 }
1035};
1036
1037
1038/* The set of possible lid events */
1039static struct sonypi_event sonypi_lidev[] = {
1040 { 0x51, SONYPI_EVENT_LID_CLOSED },
1041 { 0x50, SONYPI_EVENT_LID_OPENED },
1042 { 0, 0 }
1043};
1044
1045/* The set of possible zoom events */
1046static struct sonypi_event sonypi_zoomev[] = {
1047 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
1048 { 0, 0 }
1049};
1050
1051/* The set of possible thumbphrase events */
1052static struct sonypi_event sonypi_thumbphraseev[] = {
1053 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
1054 { 0, 0 }
1055};
1056
1057/* The set of possible motioneye camera events */
1058static struct sonypi_event sonypi_meyeev[] = {
1059 { 0x00, SONYPI_EVENT_MEYE_FACE },
1060 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
1061 { 0, 0 }
1062};
1063
1064/* The set of possible memorystick events */
1065static struct sonypi_event sonypi_memorystickev[] = {
1066 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
1067 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
1068 { 0, 0 }
1069};
1070
1071/* The set of possible battery events */
1072static struct sonypi_event sonypi_batteryev[] = {
1073 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
1074 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
1075 { 0, 0 }
1076};
1077
1078static struct sonypi_eventtypes {
1079 int model;
1080 u8 data;
1081 unsigned long mask;
1082 struct sonypi_event * events;
1083} sony_pic_eventtypes[] = {
1084 { SONYPI_DEVICE_TYPE1, 0, 0xffffffff, sonypi_releaseev },
1085 { SONYPI_DEVICE_TYPE1, 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
1086 { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_LID_MASK, sonypi_lidev },
1087 { SONYPI_DEVICE_TYPE1, 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
1088 { SONYPI_DEVICE_TYPE1, 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
1089 { SONYPI_DEVICE_TYPE1, 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1090 { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1091 { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
1092 { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1093 { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
1094
1095 { SONYPI_DEVICE_TYPE2, 0, 0xffffffff, sonypi_releaseev },
1096 { SONYPI_DEVICE_TYPE2, 0x38, SONYPI_LID_MASK, sonypi_lidev },
1097 { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
1098 { SONYPI_DEVICE_TYPE2, 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
1099 { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1100 { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1101 { SONYPI_DEVICE_TYPE2, 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
1102 { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_BACK_MASK, sonypi_backev },
1103 { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_HELP_MASK, sonypi_helpev },
1104 { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
1105 { SONYPI_DEVICE_TYPE2, 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
1106 { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1107 { SONYPI_DEVICE_TYPE2, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1108 { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1109
1110 { SONYPI_DEVICE_TYPE3, 0, 0xffffffff, sonypi_releaseev },
1111 { SONYPI_DEVICE_TYPE3, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1112 { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
1113 { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1114 { SONYPI_DEVICE_TYPE3, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1115 { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1116 { 0 }
1117};
1118
1119static int sony_pic_detect_device_type(void)
1120{
1121 struct pci_dev *pcidev;
1122 int model = 0;
1123
1124 if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1125 PCI_DEVICE_ID_INTEL_82371AB_3, NULL)))
1126 model = SONYPI_DEVICE_TYPE1;
1127
1128 else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1129 PCI_DEVICE_ID_INTEL_ICH6_1, NULL)))
1130 model = SONYPI_DEVICE_TYPE3;
1131
1132 else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1133 PCI_DEVICE_ID_INTEL_ICH7_1, NULL)))
1134 model = SONYPI_DEVICE_TYPE3;
1135
1136 else
1137 model = SONYPI_DEVICE_TYPE2;
1138
1139 if (pcidev)
1140 pci_dev_put(pcidev);
1141
1142 printk(KERN_INFO DRV_PFX "detected Type%d model\n",
1143 model == SONYPI_DEVICE_TYPE1 ? 1 :
1144 model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
1145 return model;
1146}
1147
1148#define ITERATIONS_LONG 10000
1149#define ITERATIONS_SHORT 10
1150#define wait_on_command(command, iterations) { \
1151 unsigned int n = iterations; \
1152 while (--n && (command)) \
1153 udelay(1); \
1154 if (!n) \
1155 dprintk("command failed at %s : %s (line %d)\n", \
1156 __FILE__, __FUNCTION__, __LINE__); \
1157}
1158
1159static u8 sony_pic_call1(u8 dev)
1160{
1161 u8 v1, v2;
1162
1163 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1164 ITERATIONS_LONG);
1165 outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1166 v1 = inb_p(spic_dev.cur_ioport->io.minimum + 4);
1167 v2 = inb_p(spic_dev.cur_ioport->io.minimum);
1168 dprintk("sony_pic_call1: 0x%.4x\n", (v2 << 8) | v1);
1169 return v2;
1170}
1171
1172static u8 sony_pic_call2(u8 dev, u8 fn)
1173{
1174 u8 v1;
1175
1176 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1177 ITERATIONS_LONG);
1178 outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1179 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1180 ITERATIONS_LONG);
1181 outb(fn, spic_dev.cur_ioport->io.minimum);
1182 v1 = inb_p(spic_dev.cur_ioport->io.minimum);
1183 dprintk("sony_pic_call2: 0x%.4x\n", v1);
1184 return v1;
1185}
1186
malattia@linux.it49a11de2007-04-09 19:28:56 +02001187static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
1188{
1189 u8 v1;
1190
1191 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
1192 outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1193 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
1194 outb(fn, spic_dev.cur_ioport->io.minimum);
1195 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
1196 outb(v, spic_dev.cur_ioport->io.minimum);
1197 v1 = inb_p(spic_dev.cur_ioport->io.minimum);
1198 dprintk("sony_pic_call3: 0x%.4x\n", v1);
1199 return v1;
1200}
1201
1202/* camera tests and poweron/poweroff */
1203#define SONYPI_CAMERA_PICTURE 5
1204#define SONYPI_CAMERA_MUTE_MASK 0x40
1205#define SONYPI_CAMERA_CONTROL 0x10
1206#define SONYPI_CAMERA_STATUS 7
1207#define SONYPI_CAMERA_STATUS_READY 0x2
1208#define SONYPI_CAMERA_STATUS_POSITION 0x4
1209
1210static int sony_pic_camera_ready(void)
1211{
1212 u8 v;
1213
1214 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
1215 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
1216}
1217
1218static void sony_pic_camera_off(void)
1219{
1220 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
1221 SONYPI_CAMERA_MUTE_MASK),
1222 ITERATIONS_SHORT);
1223
1224 if (!spic_dev.camera_power)
1225 return;
1226
1227 sony_pic_call2(0x91, 0);
1228 spic_dev.camera_power = 0;
1229}
1230
1231static void sony_pic_camera_on(void)
1232{
1233 int i, j;
1234
1235 if (spic_dev.camera_power)
1236 return;
1237
1238 for (j = 5; j > 0; j--) {
1239
1240 while (sony_pic_call2(0x91, 0x1))
1241 msleep(10);
1242 sony_pic_call1(0x93);
1243
1244 for (i = 400; i > 0; i--) {
1245 if (sony_pic_camera_ready())
1246 break;
1247 msleep(10);
1248 }
1249 if (i)
1250 break;
1251 }
1252
1253 if (j == 0) {
1254 printk(KERN_WARNING "sonypi: failed to power on camera\n");
1255 return;
1256 }
1257
1258 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
1259 0x5a),
1260 ITERATIONS_SHORT);
1261
1262 spic_dev.camera_power = 1;
1263}
1264
1265static ssize_t sony_pic_camerapower_store(struct device *dev,
1266 struct device_attribute *attr,
1267 const char *buffer, size_t count)
1268{
1269 unsigned long value;
1270 if (count > 31)
1271 return -EINVAL;
1272
1273 value = simple_strtoul(buffer, NULL, 10);
1274 if (value)
1275 sony_pic_camera_on();
1276 else
1277 sony_pic_camera_off();
1278
1279 return count;
1280}
1281
1282static ssize_t sony_pic_camerapower_show(struct device *dev,
1283 struct device_attribute *attr, char *buffer)
1284{
1285 return snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.camera_power);
1286}
1287
1288/* bluetooth subsystem power state */
1289static void sony_pic_set_bluetoothpower(u8 state)
1290{
1291 state = !!state;
1292 if (spic_dev.bluetooth_power == state)
1293 return;
1294 sony_pic_call2(0x96, state);
1295 sony_pic_call1(0x82);
1296 spic_dev.bluetooth_power = state;
1297}
1298
1299static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
1300 struct device_attribute *attr,
1301 const char *buffer, size_t count)
1302{
1303 unsigned long value;
1304 if (count > 31)
1305 return -EINVAL;
1306
1307 value = simple_strtoul(buffer, NULL, 10);
1308 sony_pic_set_bluetoothpower(value);
1309
1310 return count;
1311}
1312
1313static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
1314 struct device_attribute *attr, char *buffer)
1315{
1316 return snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
1317}
1318
1319/* fan speed */
1320/* FAN0 information (reverse engineered from ACPI tables) */
1321#define SONY_PIC_FAN0_STATUS 0x93
1322static ssize_t sony_pic_fanspeed_store(struct device *dev,
1323 struct device_attribute *attr,
1324 const char *buffer, size_t count)
1325{
1326 unsigned long value;
1327 if (count > 31)
1328 return -EINVAL;
1329
1330 value = simple_strtoul(buffer, NULL, 10);
1331 if (ec_write(SONY_PIC_FAN0_STATUS, value))
1332 return -EIO;
1333
1334 return count;
1335}
1336
1337static ssize_t sony_pic_fanspeed_show(struct device *dev,
1338 struct device_attribute *attr, char *buffer)
1339{
1340 u8 value = 0;
1341 if (ec_read(SONY_PIC_FAN0_STATUS, &value))
1342 return -EIO;
1343
1344 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
1345}
1346
1347#define SPIC_ATTR(_name, _mode) \
1348struct device_attribute spic_attr_##_name = __ATTR(_name, \
1349 _mode, sony_pic_## _name ##_show, \
1350 sony_pic_## _name ##_store)
1351
1352static SPIC_ATTR(camerapower, 0644);
1353static SPIC_ATTR(bluetoothpower, 0644);
1354static SPIC_ATTR(fanspeed, 0644);
1355
1356static struct attribute *spic_attributes[] = {
1357 &spic_attr_camerapower.attr,
1358 &spic_attr_bluetoothpower.attr,
1359 &spic_attr_fanspeed.attr,
1360 NULL
1361};
1362
1363static struct attribute_group spic_attribute_group = {
1364 .attrs = spic_attributes
1365};
1366
malattia@linux.it1549ee62007-04-09 10:19:08 +02001367/*
malattia@linux.it33a04452007-04-09 19:26:03 +02001368 * ACPI callbacks
malattia@linux.it1549ee62007-04-09 10:19:08 +02001369 */
malattia@linux.it33a04452007-04-09 19:26:03 +02001370static acpi_status
1371sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
1372{
1373 u32 i;
1374 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
1375
1376 switch (resource->type) {
1377 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
1378 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
1379 return AE_OK;
1380
1381 case ACPI_RESOURCE_TYPE_IRQ:
1382 {
1383 struct acpi_resource_irq *p = &resource->data.irq;
1384 struct sony_pic_irq *interrupt = NULL;
1385 if (!p || !p->interrupt_count) {
1386 /*
1387 * IRQ descriptors may have no IRQ# bits set,
1388 * particularly those those w/ _STA disabled
1389 */
1390 dprintk("Blank IRQ resource\n");
1391 return AE_OK;
1392 }
1393 for (i = 0; i < p->interrupt_count; i++) {
1394 if (!p->interrupts[i]) {
1395 printk(KERN_WARNING DRV_PFX
1396 "Invalid IRQ %d\n",
1397 p->interrupts[i]);
1398 continue;
1399 }
1400 interrupt = kzalloc(sizeof(*interrupt),
1401 GFP_KERNEL);
1402 if (!interrupt)
1403 return AE_ERROR;
1404
1405 list_add(&interrupt->list, &dev->interrupts);
1406 interrupt->irq.triggering = p->triggering;
1407 interrupt->irq.polarity = p->polarity;
1408 interrupt->irq.sharable = p->sharable;
1409 interrupt->irq.interrupt_count = 1;
1410 interrupt->irq.interrupts[0] = p->interrupts[i];
1411 }
1412 return AE_OK;
1413 }
1414 case ACPI_RESOURCE_TYPE_IO:
1415 {
1416 struct acpi_resource_io *io = &resource->data.io;
1417 struct sony_pic_ioport *ioport = NULL;
1418 if (!io) {
1419 dprintk("Blank IO resource\n");
1420 return AE_OK;
1421 }
1422
1423 ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
1424 if (!ioport)
1425 return AE_ERROR;
1426
1427 list_add(&ioport->list, &dev->ioports);
1428 memcpy(&ioport->io, io, sizeof(*io));
1429 return AE_OK;
1430 }
1431 default:
1432 dprintk("Resource %d isn't an IRQ nor an IO port\n",
1433 resource->type);
1434
1435 case ACPI_RESOURCE_TYPE_END_TAG:
1436 return AE_OK;
1437 }
1438 return AE_CTRL_TERMINATE;
1439}
1440
1441static int sony_pic_possible_resources(struct acpi_device *device)
1442{
1443 int result = 0;
1444 acpi_status status = AE_OK;
1445
1446 if (!device)
1447 return -EINVAL;
1448
1449 /* get device status */
1450 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
1451 dprintk("Evaluating _STA\n");
1452 result = acpi_bus_get_status(device);
1453 if (result) {
1454 printk(KERN_WARNING DRV_PFX "Unable to read status\n");
1455 goto end;
1456 }
1457
1458 if (!device->status.enabled)
1459 dprintk("Device disabled\n");
1460 else
1461 dprintk("Device enabled\n");
1462
1463 /*
1464 * Query and parse 'method'
1465 */
1466 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
1467 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
1468 sony_pic_read_possible_resource, &spic_dev);
1469 if (ACPI_FAILURE(status)) {
1470 printk(KERN_WARNING DRV_PFX
1471 "Failure evaluating %s\n",
1472 METHOD_NAME__PRS);
1473 result = -ENODEV;
1474 }
1475end:
1476 return result;
1477}
1478
1479/*
1480 * Disable the spic device by calling its _DIS method
1481 */
1482static int sony_pic_disable(struct acpi_device *device)
1483{
1484 if (ACPI_FAILURE(acpi_evaluate_object(device->handle, "_DIS", 0, NULL)))
1485 return -ENXIO;
1486
1487 dprintk("Device disabled\n");
1488 return 0;
1489}
1490
1491
1492/*
1493 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
1494 *
1495 * Call _SRS to set current resources
1496 */
1497static int sony_pic_enable(struct acpi_device *device,
1498 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
1499{
1500 acpi_status status;
1501 int result = 0;
1502 struct {
1503 struct acpi_resource io_res;
1504 struct acpi_resource irq_res;
1505 struct acpi_resource end;
1506 } *resource;
1507 struct acpi_buffer buffer = { 0, NULL };
1508
1509 if (!ioport || !irq)
1510 return -EINVAL;
1511
1512 /* init acpi_buffer */
1513 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
1514 if (!resource)
1515 return -ENOMEM;
1516
1517 buffer.length = sizeof(*resource) + 1;
1518 buffer.pointer = resource;
1519
1520 /* setup io resource */
1521 resource->io_res.type = ACPI_RESOURCE_TYPE_IO;
1522 resource->io_res.length = sizeof(struct acpi_resource);
1523 memcpy(&resource->io_res.data.io, &ioport->io,
1524 sizeof(struct acpi_resource_io));
1525
1526 /* setup irq resource */
1527 resource->irq_res.type = ACPI_RESOURCE_TYPE_IRQ;
1528 resource->irq_res.length = sizeof(struct acpi_resource);
1529 memcpy(&resource->irq_res.data.irq, &irq->irq,
1530 sizeof(struct acpi_resource_irq));
1531 /* we requested a shared irq */
1532 resource->irq_res.data.irq.sharable = ACPI_SHARED;
1533
1534 resource->end.type = ACPI_RESOURCE_TYPE_END_TAG;
1535
1536 /* Attempt to set the resource */
1537 dprintk("Evaluating _SRS\n");
1538 status = acpi_set_current_resources(device->handle, &buffer);
1539
1540 /* check for total failure */
1541 if (ACPI_FAILURE(status)) {
1542 printk(KERN_ERR DRV_PFX "Error evaluating _SRS");
1543 result = -ENODEV;
1544 goto end;
1545 }
1546
1547 /* Necessary device initializations calls (from sonypi) */
1548 sony_pic_call1(0x82);
1549 sony_pic_call2(0x81, 0xff);
1550 sony_pic_call1(compat ? 0x92 : 0x82);
1551
1552end:
1553 kfree(resource);
1554 return result;
1555}
1556
1557/*****************
1558 *
1559 * ISR: some event is available
1560 *
1561 *****************/
1562static irqreturn_t sony_pic_irq(int irq, void *dev_id)
1563{
1564 int i, j;
1565 u32 port_val = 0;
1566 u8 ev = 0;
1567 u8 data_mask = 0;
1568 u8 device_event = 0;
1569
1570 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
1571
1572 acpi_os_read_port(dev->cur_ioport->io.minimum, &port_val,
1573 dev->cur_ioport->io.address_length);
1574 ev = port_val & SONY_PIC_EV_MASK;
1575 data_mask = 0xff & (port_val >> (dev->cur_ioport->io.address_length - 8));
1576
1577 dprintk("event (0x%.8x [%.2x] [%.2x]) at port 0x%.4x\n",
1578 port_val, ev, data_mask, dev->cur_ioport->io.minimum);
1579
1580 if (ev == 0x00 || ev == 0xff)
1581 return IRQ_HANDLED;
1582
1583 for (i = 0; sony_pic_eventtypes[i].model; i++) {
1584
1585 if (spic_dev.model != sony_pic_eventtypes[i].model)
1586 continue;
1587
1588 if ((data_mask & sony_pic_eventtypes[i].data) !=
1589 sony_pic_eventtypes[i].data)
1590 continue;
1591
1592 if (!(mask & sony_pic_eventtypes[i].mask))
1593 continue;
1594
1595 for (j = 0; sony_pic_eventtypes[i].events[j].event; j++) {
1596 if (ev == sony_pic_eventtypes[i].events[j].data) {
1597 device_event =
1598 sony_pic_eventtypes[i].events[j].event;
1599 goto found;
1600 }
1601 }
1602 }
1603 return IRQ_HANDLED;
1604
1605found:
malattia@linux.it1549ee62007-04-09 10:19:08 +02001606 sony_laptop_report_input_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02001607 acpi_bus_generate_event(spic_dev.acpi_dev, 1, device_event);
1608
1609 return IRQ_HANDLED;
1610}
1611
1612/*****************
1613 *
1614 * ACPI driver
1615 *
1616 *****************/
1617static int sony_pic_remove(struct acpi_device *device, int type)
1618{
1619 struct sony_pic_ioport *io, *tmp_io;
1620 struct sony_pic_irq *irq, *tmp_irq;
1621
1622 if (sony_pic_disable(device)) {
1623 printk(KERN_ERR DRV_PFX "Couldn't disable device.\n");
1624 return -ENXIO;
1625 }
1626
1627 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
1628 release_region(spic_dev.cur_ioport->io.minimum,
1629 spic_dev.cur_ioport->io.address_length);
1630
malattia@linux.it1549ee62007-04-09 10:19:08 +02001631 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02001632
malattia@linux.it49a11de2007-04-09 19:28:56 +02001633 /* pf attrs */
1634 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
1635 sony_pf_remove();
1636
malattia@linux.it33a04452007-04-09 19:26:03 +02001637 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
1638 list_del(&io->list);
1639 kfree(io);
1640 }
1641 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
1642 list_del(&irq->list);
1643 kfree(irq);
1644 }
1645 spic_dev.cur_ioport = NULL;
1646 spic_dev.cur_irq = NULL;
1647
1648 dprintk("removed.\n");
1649 return 0;
1650}
1651
1652static int sony_pic_add(struct acpi_device *device)
1653{
1654 int result;
1655 struct sony_pic_ioport *io, *tmp_io;
1656 struct sony_pic_irq *irq, *tmp_irq;
1657
1658 printk(KERN_INFO DRV_PFX
1659 "Sony Programmable I/O Controller Driver v%s.\n",
1660 SONY_LAPTOP_DRIVER_VERSION);
1661
1662 spic_dev.acpi_dev = device;
1663 strcpy(acpi_device_class(device), "sony/hotkey");
1664 spic_dev.model = sony_pic_detect_device_type();
1665
1666 /* read _PRS resources */
1667 result = sony_pic_possible_resources(device);
1668 if (result) {
1669 printk(KERN_ERR DRV_PFX
1670 "Unabe to read possible resources.\n");
1671 goto err_free_resources;
1672 }
1673
1674 /* setup input devices and helper fifo */
malattia@linux.it1549ee62007-04-09 10:19:08 +02001675 result = sony_laptop_setup_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02001676 if (result) {
1677 printk(KERN_ERR DRV_PFX
1678 "Unabe to create input devices.\n");
1679 goto err_free_resources;
1680 }
1681
1682 /* request io port */
1683 list_for_each_entry(io, &spic_dev.ioports, list) {
1684 if (request_region(io->io.minimum, io->io.address_length,
1685 "Sony Programable I/O Device")) {
1686 dprintk("I/O port: 0x%.4x (0x%.4x) + 0x%.2x\n",
1687 io->io.minimum, io->io.maximum,
1688 io->io.address_length);
1689 spic_dev.cur_ioport = io;
1690 break;
1691 }
1692 }
1693 if (!spic_dev.cur_ioport) {
1694 printk(KERN_ERR DRV_PFX "Failed to request_region.\n");
1695 result = -ENODEV;
1696 goto err_remove_input;
1697 }
1698
1699 /* request IRQ */
1700 list_for_each_entry(irq, &spic_dev.interrupts, list) {
1701 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
1702 IRQF_SHARED, "sony-laptop", &spic_dev)) {
1703 dprintk("IRQ: %d - triggering: %d - "
1704 "polarity: %d - shr: %d\n",
1705 irq->irq.interrupts[0],
1706 irq->irq.triggering,
1707 irq->irq.polarity,
1708 irq->irq.sharable);
1709 spic_dev.cur_irq = irq;
1710 break;
1711 }
1712 }
1713 if (!spic_dev.cur_irq) {
1714 printk(KERN_ERR DRV_PFX "Failed to request_irq.\n");
1715 result = -ENODEV;
1716 goto err_release_region;
1717 }
1718
1719 /* set resource status _SRS */
1720 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
1721 if (result) {
1722 printk(KERN_ERR DRV_PFX "Couldn't enable device.\n");
1723 goto err_free_irq;
1724 }
1725
malattia@linux.it49a11de2007-04-09 19:28:56 +02001726 spic_dev.bluetooth_power = -1;
1727 /* create device attributes */
1728 result = sony_pf_add();
1729 if (result)
1730 goto err_disable_device;
1731
1732 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
1733 if (result)
1734 goto err_remove_pf;
1735
malattia@linux.it33a04452007-04-09 19:26:03 +02001736 return 0;
1737
malattia@linux.it49a11de2007-04-09 19:28:56 +02001738err_remove_pf:
1739 sony_pf_remove();
1740
1741err_disable_device:
1742 sony_pic_disable(device);
1743
malattia@linux.it33a04452007-04-09 19:26:03 +02001744err_free_irq:
1745 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
1746
1747err_release_region:
1748 release_region(spic_dev.cur_ioport->io.minimum,
1749 spic_dev.cur_ioport->io.address_length);
1750
1751err_remove_input:
malattia@linux.it1549ee62007-04-09 10:19:08 +02001752 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02001753
1754err_free_resources:
1755 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
1756 list_del(&io->list);
1757 kfree(io);
1758 }
1759 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
1760 list_del(&irq->list);
1761 kfree(irq);
1762 }
1763 spic_dev.cur_ioport = NULL;
1764 spic_dev.cur_irq = NULL;
1765
1766 return result;
1767}
1768
1769static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
1770{
1771 if (sony_pic_disable(device))
1772 return -ENXIO;
1773 return 0;
1774}
1775
1776static int sony_pic_resume(struct acpi_device *device)
1777{
1778 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
1779 return 0;
1780}
1781
1782static struct acpi_driver sony_pic_driver = {
1783 .name = SONY_PIC_DRIVER_NAME,
1784 .class = SONY_PIC_CLASS,
1785 .ids = SONY_PIC_HID,
1786 .owner = THIS_MODULE,
1787 .ops = {
1788 .add = sony_pic_add,
1789 .remove = sony_pic_remove,
1790 .suspend = sony_pic_suspend,
1791 .resume = sony_pic_resume,
1792 },
1793};
1794
1795static struct dmi_system_id __initdata sonypi_dmi_table[] = {
1796 {
1797 .ident = "Sony Vaio",
1798 .matches = {
1799 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
1800 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
1801 },
1802 },
1803 {
1804 .ident = "Sony Vaio",
1805 .matches = {
1806 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
1807 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
1808 },
1809 },
1810 { }
1811};
1812
malattia@linux.it59b19102007-04-09 10:19:04 +02001813static int __init sony_laptop_init(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01001814{
malattia@linux.it33a04452007-04-09 19:26:03 +02001815 int result;
1816
1817 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
1818 result = acpi_bus_register_driver(&sony_pic_driver);
1819 if (result) {
1820 printk(KERN_ERR DRV_PFX
1821 "Unable to register SPIC driver.");
1822 goto out;
1823 }
1824 }
1825
1826 result = acpi_bus_register_driver(&sony_nc_driver);
1827 if (result) {
1828 printk(KERN_ERR DRV_PFX "Unable to register SNC driver.");
1829 goto out_unregister_pic;
1830 }
1831
1832 return 0;
1833
1834out_unregister_pic:
1835 if (!no_spic)
1836 acpi_bus_unregister_driver(&sony_pic_driver);
1837out:
1838 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +01001839}
1840
malattia@linux.it59b19102007-04-09 10:19:04 +02001841static void __exit sony_laptop_exit(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01001842{
malattia@linux.it59b19102007-04-09 10:19:04 +02001843 acpi_bus_unregister_driver(&sony_nc_driver);
malattia@linux.it33a04452007-04-09 19:26:03 +02001844 if (!no_spic)
1845 acpi_bus_unregister_driver(&sony_pic_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +01001846}
1847
malattia@linux.it59b19102007-04-09 10:19:04 +02001848module_init(sony_laptop_init);
1849module_exit(sony_laptop_exit);