blob: 9042184d543a14c1534ef630e0c7d981dfef72d8 [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: "
malattia@linux.itb9a218b2007-04-09 10:19:06 +020067#define dprintk(msg...) do { \
malattia@linux.itf6119b02007-04-09 19:31:06 +020068 if (debug) printk(KERN_WARNING DRV_PFX msg); \
malattia@linux.itb9a218b2007-04-09 10:19:06 +020069} while (0)
70
malattia@linux.it33a04452007-04-09 19:26:03 +020071#define SONY_LAPTOP_DRIVER_VERSION "0.5"
Alessandro Guido50f62af2007-01-13 23:04:34 +010072
malattia@linux.it33a04452007-04-09 19:26:03 +020073#define SONY_NC_CLASS "sony-nc"
74#define SONY_NC_HID "SNY5001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020075#define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
malattia@linux.it33a04452007-04-09 19:26:03 +020076
77#define SONY_PIC_CLASS "sony-pic"
78#define SONY_PIC_HID "SNY6001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020079#define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
Stelian Pop7f09c432007-01-13 23:04:31 +010080
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010081MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
malattia@linux.it33a04452007-04-09 19:26:03 +020082MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
Stelian Pop7f09c432007-01-13 23:04:31 +010083MODULE_LICENSE("GPL");
malattia@linux.it33a04452007-04-09 19:26:03 +020084MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
Stelian Pop7f09c432007-01-13 23:04:31 +010085
86static int debug;
87module_param(debug, int, 0);
88MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
Len Browna02d1c12007-02-07 15:34:02 -050089 "the development of this driver");
Stelian Pop7f09c432007-01-13 23:04:31 +010090
malattia@linux.it33a04452007-04-09 19:26:03 +020091static int no_spic; /* = 0 */
92module_param(no_spic, int, 0444);
93MODULE_PARM_DESC(no_spic,
94 "set this if you don't want to enable the SPIC device");
95
96static int compat; /* = 0 */
97module_param(compat, int, 0444);
98MODULE_PARM_DESC(compat,
malattia@linux.it1549ee62007-04-09 10:19:08 +020099 "set this if you want to enable backward compatibility mode for SPIC");
malattia@linux.it33a04452007-04-09 19:26:03 +0200100
101static unsigned long mask = 0xffffffff;
102module_param(mask, ulong, 0644);
103MODULE_PARM_DESC(mask,
104 "set this to the mask of event you want to enable (see doc)");
105
malattia@linux.it1549ee62007-04-09 10:19:08 +0200106/*********** Input Devices ***********/
107
108#define SONY_LAPTOP_BUF_SIZE 128
109struct sony_laptop_input_s {
110 atomic_t users;
111 struct input_dev *jog_dev;
112 struct input_dev *key_dev;
113 struct kfifo *fifo;
114 spinlock_t fifo_lock;
115 struct workqueue_struct *wq;
116};
117static struct sony_laptop_input_s sony_laptop_input = {
118 .users = ATOMIC_INIT(0),
119};
120
121struct sony_laptop_keypress {
122 struct input_dev *dev;
123 int key;
124};
125
126/* Correspondance table between sonypi events and input layer events */
127static struct {
128 int sonypiev;
129 int inputev;
130} sony_laptop_inputkeys[] = {
131 { SONYPI_EVENT_CAPTURE_PRESSED, KEY_CAMERA },
132 { SONYPI_EVENT_FNKEY_ONLY, KEY_FN },
133 { SONYPI_EVENT_FNKEY_ESC, KEY_FN_ESC },
134 { SONYPI_EVENT_FNKEY_F1, KEY_FN_F1 },
135 { SONYPI_EVENT_FNKEY_F2, KEY_FN_F2 },
136 { SONYPI_EVENT_FNKEY_F3, KEY_FN_F3 },
137 { SONYPI_EVENT_FNKEY_F4, KEY_FN_F4 },
138 { SONYPI_EVENT_FNKEY_F5, KEY_FN_F5 },
139 { SONYPI_EVENT_FNKEY_F6, KEY_FN_F6 },
140 { SONYPI_EVENT_FNKEY_F7, KEY_FN_F7 },
141 { SONYPI_EVENT_FNKEY_F8, KEY_FN_F8 },
142 { SONYPI_EVENT_FNKEY_F9, KEY_FN_F9 },
143 { SONYPI_EVENT_FNKEY_F10, KEY_FN_F10 },
144 { SONYPI_EVENT_FNKEY_F11, KEY_FN_F11 },
145 { SONYPI_EVENT_FNKEY_F12, KEY_FN_F12 },
146 { SONYPI_EVENT_FNKEY_1, KEY_FN_1 },
147 { SONYPI_EVENT_FNKEY_2, KEY_FN_2 },
148 { SONYPI_EVENT_FNKEY_D, KEY_FN_D },
149 { SONYPI_EVENT_FNKEY_E, KEY_FN_E },
150 { SONYPI_EVENT_FNKEY_F, KEY_FN_F },
151 { SONYPI_EVENT_FNKEY_S, KEY_FN_S },
152 { SONYPI_EVENT_FNKEY_B, KEY_FN_B },
153 { SONYPI_EVENT_BLUETOOTH_PRESSED, KEY_BLUE },
154 { SONYPI_EVENT_BLUETOOTH_ON, KEY_BLUE },
155 { SONYPI_EVENT_PKEY_P1, KEY_PROG1 },
156 { SONYPI_EVENT_PKEY_P2, KEY_PROG2 },
157 { SONYPI_EVENT_PKEY_P3, KEY_PROG3 },
158 { SONYPI_EVENT_BACK_PRESSED, KEY_BACK },
159 { SONYPI_EVENT_HELP_PRESSED, KEY_HELP },
160 { SONYPI_EVENT_ZOOM_PRESSED, KEY_ZOOM },
161 { SONYPI_EVENT_THUMBPHRASE_PRESSED, BTN_THUMB },
162 { 0, 0 },
163};
164
165/* release buttons after a short delay if pressed */
166static void do_sony_laptop_release_key(struct work_struct *work)
167{
168 struct sony_laptop_keypress kp;
169
170 while (kfifo_get(sony_laptop_input.fifo, (unsigned char *)&kp,
171 sizeof(kp)) == sizeof(kp)) {
172 msleep(10);
173 input_report_key(kp.dev, kp.key, 0);
174 input_sync(kp.dev);
175 }
176}
177static DECLARE_WORK(sony_laptop_release_key_work,
178 do_sony_laptop_release_key);
179
180/* forward event to the input subsytem */
181static void sony_laptop_report_input_event(u8 event)
182{
183 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
184 struct input_dev *key_dev = sony_laptop_input.key_dev;
185 struct sony_laptop_keypress kp = { NULL };
186 int i;
187
188 if (event == SONYPI_EVENT_FNKEY_RELEASED) {
189 /* Nothing, not all VAIOs generate this event */
190 return;
191 }
192
193 /* report events */
194 switch (event) {
195 /* jog_dev events */
196 case SONYPI_EVENT_JOGDIAL_UP:
197 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
198 input_report_rel(jog_dev, REL_WHEEL, 1);
199 input_sync(jog_dev);
200 return;
201
202 case SONYPI_EVENT_JOGDIAL_DOWN:
203 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
204 input_report_rel(jog_dev, REL_WHEEL, -1);
205 input_sync(jog_dev);
206 return;
207
208 /* key_dev events */
209 case SONYPI_EVENT_JOGDIAL_PRESSED:
210 kp.key = BTN_MIDDLE;
211 kp.dev = jog_dev;
212 break;
213
214 default:
215 for (i = 0; sony_laptop_inputkeys[i].sonypiev; i++)
216 if (event == sony_laptop_inputkeys[i].sonypiev) {
217 kp.dev = key_dev;
218 kp.key = sony_laptop_inputkeys[i].inputev;
219 break;
220 }
221 break;
222 }
223
224 if (kp.dev) {
225 input_report_key(kp.dev, kp.key, 1);
226 input_sync(kp.dev);
227 kfifo_put(sony_laptop_input.fifo,
228 (unsigned char *)&kp, sizeof(kp));
229
230 if (!work_pending(&sony_laptop_release_key_work))
231 queue_work(sony_laptop_input.wq,
232 &sony_laptop_release_key_work);
233 } else
234 dprintk("unknown input event %.2x\n", event);
235}
236
237static int sony_laptop_setup_input(void)
238{
239 struct input_dev *jog_dev;
240 struct input_dev *key_dev;
241 int i;
242 int error;
243
244 /* don't run again if already initialized */
245 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
246 return 0;
247
248 /* kfifo */
249 spin_lock_init(&sony_laptop_input.fifo_lock);
250 sony_laptop_input.fifo =
251 kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
252 &sony_laptop_input.fifo_lock);
253 if (IS_ERR(sony_laptop_input.fifo)) {
254 printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
255 error = PTR_ERR(sony_laptop_input.fifo);
256 goto err_dec_users;
257 }
258
259 /* init workqueue */
260 sony_laptop_input.wq = create_singlethread_workqueue("sony-laptop");
261 if (!sony_laptop_input.wq) {
262 printk(KERN_ERR DRV_PFX
263 "Unabe to create workqueue.\n");
264 error = -ENXIO;
265 goto err_free_kfifo;
266 }
267
268 /* input keys */
269 key_dev = input_allocate_device();
270 if (!key_dev) {
271 error = -ENOMEM;
272 goto err_destroy_wq;
273 }
274
275 key_dev->name = "Sony Vaio Keys";
276 key_dev->id.bustype = BUS_ISA;
277 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
278
279 /* Initialize the Input Drivers: special keys */
280 key_dev->evbit[0] = BIT(EV_KEY);
281 for (i = 0; sony_laptop_inputkeys[i].sonypiev; i++)
282 if (sony_laptop_inputkeys[i].inputev)
283 set_bit(sony_laptop_inputkeys[i].inputev,
284 key_dev->keybit);
285
286 error = input_register_device(key_dev);
287 if (error)
288 goto err_free_keydev;
289
290 sony_laptop_input.key_dev = key_dev;
291
292 /* jogdial */
293 jog_dev = input_allocate_device();
294 if (!jog_dev) {
295 error = -ENOMEM;
296 goto err_unregister_keydev;
297 }
298
299 jog_dev->name = "Sony Vaio Jogdial";
300 jog_dev->id.bustype = BUS_ISA;
301 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
302
303 jog_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
304 jog_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_MIDDLE);
305 jog_dev->relbit[0] = BIT(REL_WHEEL);
306
307 error = input_register_device(jog_dev);
308 if (error)
309 goto err_free_jogdev;
310
311 sony_laptop_input.jog_dev = jog_dev;
312
313 return 0;
314
315err_free_jogdev:
316 input_free_device(jog_dev);
317
318err_unregister_keydev:
319 input_unregister_device(key_dev);
320 /* to avoid kref underflow below at input_free_device */
321 key_dev = NULL;
322
323err_free_keydev:
324 input_free_device(key_dev);
325
326err_destroy_wq:
327 destroy_workqueue(sony_laptop_input.wq);
328
329err_free_kfifo:
330 kfifo_free(sony_laptop_input.fifo);
331
332err_dec_users:
333 atomic_dec(&sony_laptop_input.users);
334 return error;
335}
336
337static void sony_laptop_remove_input(void)
338{
339 /* cleanup only after the last user has gone */
340 if (!atomic_dec_and_test(&sony_laptop_input.users))
341 return;
342
343 /* flush workqueue first */
344 flush_workqueue(sony_laptop_input.wq);
345
346 /* destroy input devs */
347 input_unregister_device(sony_laptop_input.key_dev);
348 sony_laptop_input.key_dev = NULL;
349
350 if (sony_laptop_input.jog_dev) {
351 input_unregister_device(sony_laptop_input.jog_dev);
352 sony_laptop_input.jog_dev = NULL;
353 }
354
355 destroy_workqueue(sony_laptop_input.wq);
356 kfifo_free(sony_laptop_input.fifo);
357}
358
malattia@linux.it56b87562007-04-09 10:19:05 +0200359/*********** Platform Device ***********/
360
361static atomic_t sony_pf_users = ATOMIC_INIT(0);
362static struct platform_driver sony_pf_driver = {
363 .driver = {
364 .name = "sony-laptop",
365 .owner = THIS_MODULE,
366 }
367};
368static struct platform_device *sony_pf_device;
369
370static int sony_pf_add(void)
371{
372 int ret = 0;
373
374 /* don't run again if already initialized */
375 if (atomic_add_return(1, &sony_pf_users) > 1)
376 return 0;
377
378 ret = platform_driver_register(&sony_pf_driver);
379 if (ret)
380 goto out;
381
382 sony_pf_device = platform_device_alloc("sony-laptop", -1);
383 if (!sony_pf_device) {
384 ret = -ENOMEM;
385 goto out_platform_registered;
386 }
387
388 ret = platform_device_add(sony_pf_device);
389 if (ret)
390 goto out_platform_alloced;
391
392 return 0;
393
394 out_platform_alloced:
395 platform_device_put(sony_pf_device);
396 sony_pf_device = NULL;
397 out_platform_registered:
398 platform_driver_unregister(&sony_pf_driver);
399 out:
400 atomic_dec(&sony_pf_users);
401 return ret;
402}
403
404static void sony_pf_remove(void)
405{
406 /* deregister only after the last user has gone */
407 if (!atomic_dec_and_test(&sony_pf_users))
408 return;
409
410 platform_device_del(sony_pf_device);
411 platform_device_put(sony_pf_device);
412 platform_driver_unregister(&sony_pf_driver);
413}
414
415/*********** SNC (SNY5001) Device ***********/
416
malattia@linux.it33a04452007-04-09 19:26:03 +0200417/* the device uses 1-based values, while the backlight subsystem uses
418 0-based values */
419#define SONY_MAX_BRIGHTNESS 8
420
421#define SNC_VALIDATE_IN 0
422#define SNC_VALIDATE_OUT 1
423
malattia@linux.it59b19102007-04-09 10:19:04 +0200424static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500425 char *);
malattia@linux.it59b19102007-04-09 10:19:04 +0200426static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500427 const char *, size_t);
Mattia Dongili156c2212007-02-12 22:01:07 +0100428static int boolean_validate(const int, const int);
429static int brightness_default_validate(const int, const int);
430
malattia@linux.it59b19102007-04-09 10:19:04 +0200431struct sony_nc_value {
Len Browna02d1c12007-02-07 15:34:02 -0500432 char *name; /* name of the entry */
433 char **acpiget; /* names of the ACPI get function */
434 char **acpiset; /* names of the ACPI set function */
Mattia Dongili156c2212007-02-12 22:01:07 +0100435 int (*validate)(const int, const int); /* input/output validation */
Len Browna02d1c12007-02-07 15:34:02 -0500436 int value; /* current setting */
437 int valid; /* Has ever been set */
438 int debug; /* active only in debug mode ? */
439 struct device_attribute devattr; /* sysfs atribute */
Stelian Pop7f09c432007-01-13 23:04:31 +0100440};
441
malattia@linux.it59b19102007-04-09 10:19:04 +0200442#define SNC_HANDLE_NAMES(_name, _values...) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100443 static char *snc_##_name[] = { _values, NULL }
444
malattia@linux.it59b19102007-04-09 10:19:04 +0200445#define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100446 { \
447 .name = __stringify(_name), \
448 .acpiget = _getters, \
449 .acpiset = _setters, \
Mattia Dongili156c2212007-02-12 22:01:07 +0100450 .validate = _validate, \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100451 .debug = _debug, \
malattia@linux.it59b19102007-04-09 10:19:04 +0200452 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100453 }
454
malattia@linux.it59b19102007-04-09 10:19:04 +0200455#define SNC_HANDLE_NULL { .name = NULL }
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100456
malattia@linux.it59b19102007-04-09 10:19:04 +0200457SNC_HANDLE_NAMES(fnkey_get, "GHKE");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100458
malattia@linux.it59b19102007-04-09 10:19:04 +0200459SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
460SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100461
malattia@linux.it59b19102007-04-09 10:19:04 +0200462SNC_HANDLE_NAMES(cdpower_get, "GCDP");
463SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100464
malattia@linux.it59b19102007-04-09 10:19:04 +0200465SNC_HANDLE_NAMES(audiopower_get, "GAZP");
466SNC_HANDLE_NAMES(audiopower_set, "AZPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100467
malattia@linux.it59b19102007-04-09 10:19:04 +0200468SNC_HANDLE_NAMES(lanpower_get, "GLNP");
469SNC_HANDLE_NAMES(lanpower_set, "LNPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100470
malattia@linux.it59b19102007-04-09 10:19:04 +0200471SNC_HANDLE_NAMES(PID_get, "GPID");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100472
malattia@linux.it59b19102007-04-09 10:19:04 +0200473SNC_HANDLE_NAMES(CTR_get, "GCTR");
474SNC_HANDLE_NAMES(CTR_set, "SCTR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100475
malattia@linux.it59b19102007-04-09 10:19:04 +0200476SNC_HANDLE_NAMES(PCR_get, "GPCR");
477SNC_HANDLE_NAMES(PCR_set, "SPCR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100478
malattia@linux.it59b19102007-04-09 10:19:04 +0200479SNC_HANDLE_NAMES(CMI_get, "GCMI");
480SNC_HANDLE_NAMES(CMI_set, "SCMI");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100481
malattia@linux.it59b19102007-04-09 10:19:04 +0200482static struct sony_nc_value sony_nc_values[] = {
483 SNC_HANDLE(brightness_default, snc_brightness_def_get,
Mattia Dongili156c2212007-02-12 22:01:07 +0100484 snc_brightness_def_set, brightness_default_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200485 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
486 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
487 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100488 boolean_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200489 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100490 boolean_validate, 1),
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100491 /* unknown methods */
malattia@linux.it59b19102007-04-09 10:19:04 +0200492 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
493 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
494 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
495 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
496 SNC_HANDLE_NULL
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100497};
498
malattia@linux.it59b19102007-04-09 10:19:04 +0200499static acpi_handle sony_nc_acpi_handle;
500static struct acpi_device *sony_nc_acpi_device = NULL;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100501
Mattia Dongilid78865c2007-02-07 20:01:56 +0100502/*
503 * acpi_evaluate_object wrappers
504 */
Stelian Pop7f09c432007-01-13 23:04:31 +0100505static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
506{
507 struct acpi_buffer output;
508 union acpi_object out_obj;
509 acpi_status status;
510
511 output.length = sizeof(out_obj);
512 output.pointer = &out_obj;
513
514 status = acpi_evaluate_object(handle, name, NULL, &output);
515 if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
516 *result = out_obj.integer.value;
517 return 0;
518 }
519
malattia@linux.itf6119b02007-04-09 19:31:06 +0200520 printk(KERN_WARNING DRV_PFX "acpi_callreadfunc failed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100521
522 return -1;
523}
524
525static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
526 int *result)
527{
528 struct acpi_object_list params;
529 union acpi_object in_obj;
530 struct acpi_buffer output;
531 union acpi_object out_obj;
532 acpi_status status;
533
534 params.count = 1;
535 params.pointer = &in_obj;
536 in_obj.type = ACPI_TYPE_INTEGER;
537 in_obj.integer.value = value;
538
539 output.length = sizeof(out_obj);
540 output.pointer = &out_obj;
541
542 status = acpi_evaluate_object(handle, name, &params, &output);
543 if (status == AE_OK) {
544 if (result != NULL) {
545 if (out_obj.type != ACPI_TYPE_INTEGER) {
malattia@linux.itf6119b02007-04-09 19:31:06 +0200546 printk(KERN_WARNING DRV_PFX "acpi_evaluate_object bad "
Stelian Pop7f09c432007-01-13 23:04:31 +0100547 "return type\n");
548 return -1;
549 }
550 *result = out_obj.integer.value;
551 }
552 return 0;
553 }
554
malattia@linux.itf6119b02007-04-09 19:31:06 +0200555 printk(KERN_WARNING DRV_PFX "acpi_evaluate_object failed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100556
557 return -1;
558}
559
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100560/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200561 * sony_nc_values input/output validate functions
Mattia Dongili156c2212007-02-12 22:01:07 +0100562 */
563
564/* brightness_default_validate:
565 *
566 * manipulate input output values to keep consistency with the
567 * backlight framework for which brightness values are 0-based.
568 */
569static int brightness_default_validate(const int direction, const int value)
570{
571 switch (direction) {
572 case SNC_VALIDATE_OUT:
573 return value - 1;
574 case SNC_VALIDATE_IN:
575 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
576 return value + 1;
577 }
578 return -EINVAL;
579}
580
581/* boolean_validate:
582 *
583 * on input validate boolean values 0/1, on output just pass the
584 * received value.
585 */
586static int boolean_validate(const int direction, const int value)
587{
588 if (direction == SNC_VALIDATE_IN) {
589 if (value != 0 && value != 1)
590 return -EINVAL;
591 }
592 return value;
593}
594
595/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200596 * Sysfs show/store common to all sony_nc_values
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100597 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200598static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
Len Browna02d1c12007-02-07 15:34:02 -0500599 char *buffer)
Stelian Pop7f09c432007-01-13 23:04:31 +0100600{
Stelian Pop7f09c432007-01-13 23:04:31 +0100601 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200602 struct sony_nc_value *item =
603 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100604
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100605 if (!*item->acpiget)
Stelian Pop7f09c432007-01-13 23:04:31 +0100606 return -EIO;
607
malattia@linux.it59b19102007-04-09 10:19:04 +0200608 if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100609 return -EIO;
610
Mattia Dongili156c2212007-02-12 22:01:07 +0100611 if (item->validate)
612 value = item->validate(SNC_VALIDATE_OUT, value);
613
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100614 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
Stelian Pop7f09c432007-01-13 23:04:31 +0100615}
616
malattia@linux.it59b19102007-04-09 10:19:04 +0200617static ssize_t sony_nc_sysfs_store(struct device *dev,
Len Browna02d1c12007-02-07 15:34:02 -0500618 struct device_attribute *attr,
619 const char *buffer, size_t count)
Stelian Pop7f09c432007-01-13 23:04:31 +0100620{
Stelian Pop7f09c432007-01-13 23:04:31 +0100621 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200622 struct sony_nc_value *item =
623 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100624
625 if (!item->acpiset)
626 return -EIO;
627
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100628 if (count > 31)
629 return -EINVAL;
630
631 value = simple_strtoul(buffer, NULL, 10);
Stelian Pop7f09c432007-01-13 23:04:31 +0100632
Mattia Dongili156c2212007-02-12 22:01:07 +0100633 if (item->validate)
634 value = item->validate(SNC_VALIDATE_IN, value);
635
636 if (value < 0)
637 return value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100638
malattia@linux.it59b19102007-04-09 10:19:04 +0200639 if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100640 return -EIO;
Andrew Morton3f4f4612007-01-13 23:04:32 +0100641 item->value = value;
642 item->valid = 1;
Stelian Pop7f09c432007-01-13 23:04:31 +0100643 return count;
644}
645
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100646
Mattia Dongilid78865c2007-02-07 20:01:56 +0100647/*
648 * Backlight device
649 */
650static int sony_backlight_update_status(struct backlight_device *bd)
Andrew Morton3f4f4612007-01-13 23:04:32 +0100651{
malattia@linux.it59b19102007-04-09 10:19:04 +0200652 return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
Richard Purdie321709c2007-02-10 15:04:08 +0000653 bd->props.brightness + 1, NULL);
Andrew Morton3f4f4612007-01-13 23:04:32 +0100654}
655
Mattia Dongilid78865c2007-02-07 20:01:56 +0100656static int sony_backlight_get_brightness(struct backlight_device *bd)
657{
658 int value;
659
malattia@linux.it59b19102007-04-09 10:19:04 +0200660 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
Mattia Dongilid78865c2007-02-07 20:01:56 +0100661 return 0;
662 /* brightness levels are 1-based, while backlight ones are 0-based */
663 return value - 1;
664}
665
666static struct backlight_device *sony_backlight_device;
Richard Purdie321709c2007-02-10 15:04:08 +0000667static struct backlight_ops sony_backlight_ops = {
Len Browna02d1c12007-02-07 15:34:02 -0500668 .update_status = sony_backlight_update_status,
669 .get_brightness = sony_backlight_get_brightness,
Mattia Dongilid78865c2007-02-07 20:01:56 +0100670};
671
672/*
673 * ACPI callbacks
674 */
Stelian Pop7f09c432007-01-13 23:04:31 +0100675static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
676{
malattia@linux.itb9a218b2007-04-09 10:19:06 +0200677 dprintk("sony_acpi_notify, event: %d\n", event);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200678 sony_laptop_report_input_event(event);
malattia@linux.it59b19102007-04-09 10:19:04 +0200679 acpi_bus_generate_event(sony_nc_acpi_device, 1, event);
Stelian Pop7f09c432007-01-13 23:04:31 +0100680}
681
682static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
683 void *context, void **return_value)
684{
685 struct acpi_namespace_node *node;
686 union acpi_operand_object *operand;
687
Len Browna02d1c12007-02-07 15:34:02 -0500688 node = (struct acpi_namespace_node *)handle;
689 operand = (union acpi_operand_object *)node->object;
Stelian Pop7f09c432007-01-13 23:04:31 +0100690
malattia@linux.itf6119b02007-04-09 19:31:06 +0200691 printk(KERN_WARNING DRV_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
Stelian Pop7f09c432007-01-13 23:04:31 +0100692 (u32) operand->method.param_count);
693
694 return AE_OK;
695}
696
Mattia Dongilid78865c2007-02-07 20:01:56 +0100697/*
698 * ACPI device
699 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200700static int sony_nc_resume(struct acpi_device *device)
Mattia Dongilid78865c2007-02-07 20:01:56 +0100701{
malattia@linux.it59b19102007-04-09 10:19:04 +0200702 struct sony_nc_value *item;
Mattia Dongilid78865c2007-02-07 20:01:56 +0100703
malattia@linux.it59b19102007-04-09 10:19:04 +0200704 for (item = sony_nc_values; item->name; item++) {
Mattia Dongilid78865c2007-02-07 20:01:56 +0100705 int ret;
706
707 if (!item->valid)
708 continue;
malattia@linux.it59b19102007-04-09 10:19:04 +0200709 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
Len Browna02d1c12007-02-07 15:34:02 -0500710 item->value, NULL);
Mattia Dongilid78865c2007-02-07 20:01:56 +0100711 if (ret < 0) {
712 printk("%s: %d\n", __FUNCTION__, ret);
713 break;
714 }
715 }
716 return 0;
717}
718
malattia@linux.it59b19102007-04-09 10:19:04 +0200719static int sony_nc_add(struct acpi_device *device)
Stelian Pop7f09c432007-01-13 23:04:31 +0100720{
721 acpi_status status;
Andrew Morton8607c672007-03-06 02:29:42 -0800722 int result = 0;
Alessandro Guido50f62af2007-01-13 23:04:34 +0100723 acpi_handle handle;
malattia@linux.it56b87562007-04-09 10:19:05 +0200724 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +0100725
malattia@linux.itf6119b02007-04-09 19:31:06 +0200726 printk(KERN_INFO DRV_PFX "%s v%s.\n",
727 SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
728
malattia@linux.it59b19102007-04-09 10:19:04 +0200729 sony_nc_acpi_device = device;
malattia@linux.it33a04452007-04-09 19:26:03 +0200730 strcpy(acpi_device_class(device), "sony/hotkey");
Stelian Popc5611622007-01-13 23:04:37 +0100731
malattia@linux.it59b19102007-04-09 10:19:04 +0200732 sony_nc_acpi_handle = device->handle;
Stelian Pop7f09c432007-01-13 23:04:31 +0100733
Stelian Pop7f09c432007-01-13 23:04:31 +0100734 if (debug) {
malattia@linux.it59b19102007-04-09 10:19:04 +0200735 status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_nc_acpi_handle,
Stelian Pop7f09c432007-01-13 23:04:31 +0100736 1, sony_walk_callback, NULL, NULL);
737 if (ACPI_FAILURE(status)) {
malattia@linux.itf6119b02007-04-09 19:31:06 +0200738 printk(KERN_WARNING DRV_PFX "unable to walk acpi resources\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100739 result = -ENODEV;
740 goto outwalk;
741 }
Stelian Popc5611622007-01-13 23:04:37 +0100742 }
Stelian Pop7f09c432007-01-13 23:04:31 +0100743
malattia@linux.it1549ee62007-04-09 10:19:08 +0200744 /* setup input devices and helper fifo */
745 result = sony_laptop_setup_input();
746 if (result) {
747 printk(KERN_ERR DRV_PFX
748 "Unabe to create input devices.\n");
749 goto outwalk;
750 }
751
malattia@linux.it59b19102007-04-09 10:19:04 +0200752 status = acpi_install_notify_handler(sony_nc_acpi_handle,
Stelian Popc5611622007-01-13 23:04:37 +0100753 ACPI_DEVICE_NOTIFY,
Len Browna02d1c12007-02-07 15:34:02 -0500754 sony_acpi_notify, NULL);
Stelian Popc5611622007-01-13 23:04:37 +0100755 if (ACPI_FAILURE(status)) {
malattia@linux.itf6119b02007-04-09 19:31:06 +0200756 printk(KERN_WARNING DRV_PFX "unable to install notify handler\n");
Stelian Popc5611622007-01-13 23:04:37 +0100757 result = -ENODEV;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200758 goto outinput;
Stelian Pop7f09c432007-01-13 23:04:31 +0100759 }
760
malattia@linux.it59b19102007-04-09 10:19:04 +0200761 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT", &handle))) {
Alessandro Guido50f62af2007-01-13 23:04:34 +0100762 sony_backlight_device = backlight_device_register("sony", NULL,
Len Browna02d1c12007-02-07 15:34:02 -0500763 NULL,
Richard Purdie321709c2007-02-10 15:04:08 +0000764 &sony_backlight_ops);
Mattia Dongili7df03b82007-01-13 23:04:41 +0100765
Len Browna02d1c12007-02-07 15:34:02 -0500766 if (IS_ERR(sony_backlight_device)) {
malattia@linux.itf6119b02007-04-09 19:31:06 +0200767 printk(KERN_WARNING DRV_PFX "unable to register backlight device\n");
Mattia Dongili7df03b82007-01-13 23:04:41 +0100768 sony_backlight_device = NULL;
Richard Purdie321709c2007-02-10 15:04:08 +0000769 } else {
770 sony_backlight_device->props.brightness =
Len Browna02d1c12007-02-07 15:34:02 -0500771 sony_backlight_get_brightness
772 (sony_backlight_device);
Richard Purdie321709c2007-02-10 15:04:08 +0000773 sony_backlight_device->props.max_brightness =
774 SONY_MAX_BRIGHTNESS - 1;
775 }
776
Alessandro Guido50f62af2007-01-13 23:04:34 +0100777 }
Stelian Pop7f09c432007-01-13 23:04:31 +0100778
malattia@linux.it49a11de2007-04-09 19:28:56 +0200779 result = sony_pf_add();
780 if (result)
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100781 goto outbacklight;
Stelian Pop7f09c432007-01-13 23:04:31 +0100782
malattia@linux.it56b87562007-04-09 10:19:05 +0200783 /* create sony_pf sysfs attributes related to the SNC device */
784 for (item = sony_nc_values; item->name; ++item) {
785
786 if (!debug && item->debug)
787 continue;
788
789 /* find the available acpiget as described in the DSDT */
790 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
791 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
792 *item->acpiget,
793 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +0200794 dprintk("Found %s getter: %s\n",
795 item->name, *item->acpiget);
malattia@linux.it56b87562007-04-09 10:19:05 +0200796 item->devattr.attr.mode |= S_IRUGO;
797 break;
798 }
799 }
800
801 /* find the available acpiset as described in the DSDT */
802 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
803 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
804 *item->acpiset,
805 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +0200806 dprintk("Found %s setter: %s\n",
807 item->name, *item->acpiset);
malattia@linux.it56b87562007-04-09 10:19:05 +0200808 item->devattr.attr.mode |= S_IWUSR;
809 break;
810 }
811 }
812
813 if (item->devattr.attr.mode != 0) {
814 result =
815 device_create_file(&sony_pf_device->dev,
816 &item->devattr);
817 if (result)
818 goto out_sysfs;
819 }
820 }
821
Stelian Pop7f09c432007-01-13 23:04:31 +0100822 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))
malattia@linux.itf6119b02007-04-09 19:31:06 +0200838 printk(KERN_WARNING DRV_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))
malattia@linux.itf6119b02007-04-09 19:31:06 +0200861 printk(KERN_WARNING DRV_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();
malattia@linux.itf6119b02007-04-09 19:31:06 +0200869 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100870
871 return 0;
872}
873
malattia@linux.it59b19102007-04-09 10:19:04 +0200874static struct acpi_driver sony_nc_driver = {
875 .name = SONY_NC_DRIVER_NAME,
876 .class = SONY_NC_CLASS,
877 .ids = SONY_NC_HID,
malattia@linux.it33a04452007-04-09 19:26:03 +0200878 .owner = THIS_MODULE,
Len Browna02d1c12007-02-07 15:34:02 -0500879 .ops = {
malattia@linux.it59b19102007-04-09 10:19:04 +0200880 .add = sony_nc_add,
881 .remove = sony_nc_remove,
882 .resume = sony_nc_resume,
Len Browna02d1c12007-02-07 15:34:02 -0500883 },
Andrew Morton3f4f4612007-01-13 23:04:32 +0100884};
885
malattia@linux.it33a04452007-04-09 19:26:03 +0200886/*********** SPIC (SNY6001) Device ***********/
887
888#define SONYPI_DEVICE_TYPE1 0x00000001
889#define SONYPI_DEVICE_TYPE2 0x00000002
890#define SONYPI_DEVICE_TYPE3 0x00000004
891
malattia@linux.it33a04452007-04-09 19:26:03 +0200892#define SONY_PIC_EV_MASK 0xff
893
malattia@linux.it33a04452007-04-09 19:26:03 +0200894struct sony_pic_ioport {
895 struct acpi_resource_io io;
896 struct list_head list;
897};
898
899struct sony_pic_irq {
900 struct acpi_resource_irq irq;
901 struct list_head list;
902};
903
904struct sony_pic_dev {
905 int model;
malattia@linux.it49a11de2007-04-09 19:28:56 +0200906 u8 camera_power;
907 u8 bluetooth_power;
malattia@linux.it33a04452007-04-09 19:26:03 +0200908 struct acpi_device *acpi_dev;
909 struct sony_pic_irq *cur_irq;
910 struct sony_pic_ioport *cur_ioport;
911 struct list_head interrupts;
912 struct list_head ioports;
malattia@linux.it33a04452007-04-09 19:26:03 +0200913};
914
915static struct sony_pic_dev spic_dev = {
916 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
917 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
918};
919
920/* Event masks */
921#define SONYPI_JOGGER_MASK 0x00000001
922#define SONYPI_CAPTURE_MASK 0x00000002
923#define SONYPI_FNKEY_MASK 0x00000004
924#define SONYPI_BLUETOOTH_MASK 0x00000008
925#define SONYPI_PKEY_MASK 0x00000010
926#define SONYPI_BACK_MASK 0x00000020
927#define SONYPI_HELP_MASK 0x00000040
928#define SONYPI_LID_MASK 0x00000080
929#define SONYPI_ZOOM_MASK 0x00000100
930#define SONYPI_THUMBPHRASE_MASK 0x00000200
931#define SONYPI_MEYE_MASK 0x00000400
932#define SONYPI_MEMORYSTICK_MASK 0x00000800
933#define SONYPI_BATTERY_MASK 0x00001000
934#define SONYPI_WIRELESS_MASK 0x00002000
935
936struct sonypi_event {
937 u8 data;
938 u8 event;
939};
940
941/* The set of possible button release events */
942static struct sonypi_event sonypi_releaseev[] = {
943 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
944 { 0, 0 }
945};
946
947/* The set of possible jogger events */
948static struct sonypi_event sonypi_joggerev[] = {
949 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
950 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
951 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
952 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
953 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
954 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
955 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
956 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
957 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
958 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
959 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
960 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
961 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
962 { 0, 0 }
963};
964
965/* The set of possible capture button events */
966static struct sonypi_event sonypi_captureev[] = {
967 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
968 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
969 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
970 { 0, 0 }
971};
972
973/* The set of possible fnkeys events */
974static struct sonypi_event sonypi_fnkeyev[] = {
975 { 0x10, SONYPI_EVENT_FNKEY_ESC },
976 { 0x11, SONYPI_EVENT_FNKEY_F1 },
977 { 0x12, SONYPI_EVENT_FNKEY_F2 },
978 { 0x13, SONYPI_EVENT_FNKEY_F3 },
979 { 0x14, SONYPI_EVENT_FNKEY_F4 },
980 { 0x15, SONYPI_EVENT_FNKEY_F5 },
981 { 0x16, SONYPI_EVENT_FNKEY_F6 },
982 { 0x17, SONYPI_EVENT_FNKEY_F7 },
983 { 0x18, SONYPI_EVENT_FNKEY_F8 },
984 { 0x19, SONYPI_EVENT_FNKEY_F9 },
985 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
986 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
987 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
988 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
989 { 0x21, SONYPI_EVENT_FNKEY_1 },
990 { 0x22, SONYPI_EVENT_FNKEY_2 },
991 { 0x31, SONYPI_EVENT_FNKEY_D },
992 { 0x32, SONYPI_EVENT_FNKEY_E },
993 { 0x33, SONYPI_EVENT_FNKEY_F },
994 { 0x34, SONYPI_EVENT_FNKEY_S },
995 { 0x35, SONYPI_EVENT_FNKEY_B },
996 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
997 { 0, 0 }
998};
999
1000/* The set of possible program key events */
1001static struct sonypi_event sonypi_pkeyev[] = {
1002 { 0x01, SONYPI_EVENT_PKEY_P1 },
1003 { 0x02, SONYPI_EVENT_PKEY_P2 },
1004 { 0x04, SONYPI_EVENT_PKEY_P3 },
1005 { 0x5c, SONYPI_EVENT_PKEY_P1 },
1006 { 0, 0 }
1007};
1008
1009/* The set of possible bluetooth events */
1010static struct sonypi_event sonypi_blueev[] = {
1011 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
1012 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
1013 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
1014 { 0, 0 }
1015};
1016
1017/* The set of possible wireless events */
1018static struct sonypi_event sonypi_wlessev[] = {
1019 { 0x59, SONYPI_EVENT_WIRELESS_ON },
1020 { 0x5a, SONYPI_EVENT_WIRELESS_OFF },
1021 { 0, 0 }
1022};
1023
1024/* The set of possible back button events */
1025static struct sonypi_event sonypi_backev[] = {
1026 { 0x20, SONYPI_EVENT_BACK_PRESSED },
1027 { 0, 0 }
1028};
1029
1030/* The set of possible help button events */
1031static struct sonypi_event sonypi_helpev[] = {
1032 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
1033 { 0, 0 }
1034};
1035
1036
1037/* The set of possible lid events */
1038static struct sonypi_event sonypi_lidev[] = {
1039 { 0x51, SONYPI_EVENT_LID_CLOSED },
1040 { 0x50, SONYPI_EVENT_LID_OPENED },
1041 { 0, 0 }
1042};
1043
1044/* The set of possible zoom events */
1045static struct sonypi_event sonypi_zoomev[] = {
1046 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
1047 { 0, 0 }
1048};
1049
1050/* The set of possible thumbphrase events */
1051static struct sonypi_event sonypi_thumbphraseev[] = {
1052 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
1053 { 0, 0 }
1054};
1055
1056/* The set of possible motioneye camera events */
1057static struct sonypi_event sonypi_meyeev[] = {
1058 { 0x00, SONYPI_EVENT_MEYE_FACE },
1059 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
1060 { 0, 0 }
1061};
1062
1063/* The set of possible memorystick events */
1064static struct sonypi_event sonypi_memorystickev[] = {
1065 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
1066 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
1067 { 0, 0 }
1068};
1069
1070/* The set of possible battery events */
1071static struct sonypi_event sonypi_batteryev[] = {
1072 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
1073 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
1074 { 0, 0 }
1075};
1076
1077static struct sonypi_eventtypes {
1078 int model;
1079 u8 data;
1080 unsigned long mask;
1081 struct sonypi_event * events;
1082} sony_pic_eventtypes[] = {
1083 { SONYPI_DEVICE_TYPE1, 0, 0xffffffff, sonypi_releaseev },
1084 { SONYPI_DEVICE_TYPE1, 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
1085 { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_LID_MASK, sonypi_lidev },
1086 { SONYPI_DEVICE_TYPE1, 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
1087 { SONYPI_DEVICE_TYPE1, 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
1088 { SONYPI_DEVICE_TYPE1, 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1089 { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1090 { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
1091 { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1092 { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
1093
1094 { SONYPI_DEVICE_TYPE2, 0, 0xffffffff, sonypi_releaseev },
1095 { SONYPI_DEVICE_TYPE2, 0x38, SONYPI_LID_MASK, sonypi_lidev },
1096 { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
1097 { SONYPI_DEVICE_TYPE2, 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
1098 { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1099 { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1100 { SONYPI_DEVICE_TYPE2, 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
1101 { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_BACK_MASK, sonypi_backev },
1102 { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_HELP_MASK, sonypi_helpev },
1103 { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
1104 { SONYPI_DEVICE_TYPE2, 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
1105 { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1106 { SONYPI_DEVICE_TYPE2, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1107 { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1108
1109 { SONYPI_DEVICE_TYPE3, 0, 0xffffffff, sonypi_releaseev },
1110 { SONYPI_DEVICE_TYPE3, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1111 { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
1112 { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1113 { SONYPI_DEVICE_TYPE3, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1114 { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1115 { 0 }
1116};
1117
1118static int sony_pic_detect_device_type(void)
1119{
1120 struct pci_dev *pcidev;
1121 int model = 0;
1122
1123 if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1124 PCI_DEVICE_ID_INTEL_82371AB_3, NULL)))
1125 model = SONYPI_DEVICE_TYPE1;
1126
1127 else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1128 PCI_DEVICE_ID_INTEL_ICH6_1, NULL)))
1129 model = SONYPI_DEVICE_TYPE3;
1130
1131 else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1132 PCI_DEVICE_ID_INTEL_ICH7_1, NULL)))
1133 model = SONYPI_DEVICE_TYPE3;
1134
1135 else
1136 model = SONYPI_DEVICE_TYPE2;
1137
1138 if (pcidev)
1139 pci_dev_put(pcidev);
1140
1141 printk(KERN_INFO DRV_PFX "detected Type%d model\n",
1142 model == SONYPI_DEVICE_TYPE1 ? 1 :
1143 model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
1144 return model;
1145}
1146
1147#define ITERATIONS_LONG 10000
1148#define ITERATIONS_SHORT 10
1149#define wait_on_command(command, iterations) { \
1150 unsigned int n = iterations; \
1151 while (--n && (command)) \
1152 udelay(1); \
1153 if (!n) \
1154 dprintk("command failed at %s : %s (line %d)\n", \
1155 __FILE__, __FUNCTION__, __LINE__); \
1156}
1157
1158static u8 sony_pic_call1(u8 dev)
1159{
1160 u8 v1, v2;
1161
1162 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1163 ITERATIONS_LONG);
1164 outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1165 v1 = inb_p(spic_dev.cur_ioport->io.minimum + 4);
1166 v2 = inb_p(spic_dev.cur_ioport->io.minimum);
1167 dprintk("sony_pic_call1: 0x%.4x\n", (v2 << 8) | v1);
1168 return v2;
1169}
1170
1171static u8 sony_pic_call2(u8 dev, u8 fn)
1172{
1173 u8 v1;
1174
1175 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1176 ITERATIONS_LONG);
1177 outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1178 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1179 ITERATIONS_LONG);
1180 outb(fn, spic_dev.cur_ioport->io.minimum);
1181 v1 = inb_p(spic_dev.cur_ioport->io.minimum);
1182 dprintk("sony_pic_call2: 0x%.4x\n", v1);
1183 return v1;
1184}
1185
malattia@linux.it49a11de2007-04-09 19:28:56 +02001186static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
1187{
1188 u8 v1;
1189
1190 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
1191 outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1192 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
1193 outb(fn, spic_dev.cur_ioport->io.minimum);
1194 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
1195 outb(v, spic_dev.cur_ioport->io.minimum);
1196 v1 = inb_p(spic_dev.cur_ioport->io.minimum);
1197 dprintk("sony_pic_call3: 0x%.4x\n", v1);
1198 return v1;
1199}
1200
1201/* camera tests and poweron/poweroff */
1202#define SONYPI_CAMERA_PICTURE 5
1203#define SONYPI_CAMERA_MUTE_MASK 0x40
1204#define SONYPI_CAMERA_CONTROL 0x10
1205#define SONYPI_CAMERA_STATUS 7
1206#define SONYPI_CAMERA_STATUS_READY 0x2
1207#define SONYPI_CAMERA_STATUS_POSITION 0x4
1208
1209static int sony_pic_camera_ready(void)
1210{
1211 u8 v;
1212
1213 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
1214 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
1215}
1216
1217static void sony_pic_camera_off(void)
1218{
1219 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
1220 SONYPI_CAMERA_MUTE_MASK),
1221 ITERATIONS_SHORT);
1222
1223 if (!spic_dev.camera_power)
1224 return;
1225
1226 sony_pic_call2(0x91, 0);
1227 spic_dev.camera_power = 0;
1228}
1229
1230static void sony_pic_camera_on(void)
1231{
1232 int i, j;
1233
1234 if (spic_dev.camera_power)
1235 return;
1236
1237 for (j = 5; j > 0; j--) {
1238
1239 while (sony_pic_call2(0x91, 0x1))
1240 msleep(10);
1241 sony_pic_call1(0x93);
1242
1243 for (i = 400; i > 0; i--) {
1244 if (sony_pic_camera_ready())
1245 break;
1246 msleep(10);
1247 }
1248 if (i)
1249 break;
1250 }
1251
1252 if (j == 0) {
1253 printk(KERN_WARNING "sonypi: failed to power on camera\n");
1254 return;
1255 }
1256
1257 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
1258 0x5a),
1259 ITERATIONS_SHORT);
1260
1261 spic_dev.camera_power = 1;
1262}
1263
1264static ssize_t sony_pic_camerapower_store(struct device *dev,
1265 struct device_attribute *attr,
1266 const char *buffer, size_t count)
1267{
1268 unsigned long value;
1269 if (count > 31)
1270 return -EINVAL;
1271
1272 value = simple_strtoul(buffer, NULL, 10);
1273 if (value)
1274 sony_pic_camera_on();
1275 else
1276 sony_pic_camera_off();
1277
1278 return count;
1279}
1280
1281static ssize_t sony_pic_camerapower_show(struct device *dev,
1282 struct device_attribute *attr, char *buffer)
1283{
1284 return snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.camera_power);
1285}
1286
1287/* bluetooth subsystem power state */
1288static void sony_pic_set_bluetoothpower(u8 state)
1289{
1290 state = !!state;
1291 if (spic_dev.bluetooth_power == state)
1292 return;
1293 sony_pic_call2(0x96, state);
1294 sony_pic_call1(0x82);
1295 spic_dev.bluetooth_power = state;
1296}
1297
1298static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
1299 struct device_attribute *attr,
1300 const char *buffer, size_t count)
1301{
1302 unsigned long value;
1303 if (count > 31)
1304 return -EINVAL;
1305
1306 value = simple_strtoul(buffer, NULL, 10);
1307 sony_pic_set_bluetoothpower(value);
1308
1309 return count;
1310}
1311
1312static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
1313 struct device_attribute *attr, char *buffer)
1314{
1315 return snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
1316}
1317
1318/* fan speed */
1319/* FAN0 information (reverse engineered from ACPI tables) */
1320#define SONY_PIC_FAN0_STATUS 0x93
1321static ssize_t sony_pic_fanspeed_store(struct device *dev,
1322 struct device_attribute *attr,
1323 const char *buffer, size_t count)
1324{
1325 unsigned long value;
1326 if (count > 31)
1327 return -EINVAL;
1328
1329 value = simple_strtoul(buffer, NULL, 10);
1330 if (ec_write(SONY_PIC_FAN0_STATUS, value))
1331 return -EIO;
1332
1333 return count;
1334}
1335
1336static ssize_t sony_pic_fanspeed_show(struct device *dev,
1337 struct device_attribute *attr, char *buffer)
1338{
1339 u8 value = 0;
1340 if (ec_read(SONY_PIC_FAN0_STATUS, &value))
1341 return -EIO;
1342
1343 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
1344}
1345
1346#define SPIC_ATTR(_name, _mode) \
1347struct device_attribute spic_attr_##_name = __ATTR(_name, \
1348 _mode, sony_pic_## _name ##_show, \
1349 sony_pic_## _name ##_store)
1350
1351static SPIC_ATTR(camerapower, 0644);
1352static SPIC_ATTR(bluetoothpower, 0644);
1353static SPIC_ATTR(fanspeed, 0644);
1354
1355static struct attribute *spic_attributes[] = {
1356 &spic_attr_camerapower.attr,
1357 &spic_attr_bluetoothpower.attr,
1358 &spic_attr_fanspeed.attr,
1359 NULL
1360};
1361
1362static struct attribute_group spic_attribute_group = {
1363 .attrs = spic_attributes
1364};
1365
malattia@linux.it1549ee62007-04-09 10:19:08 +02001366/*
malattia@linux.it33a04452007-04-09 19:26:03 +02001367 * ACPI callbacks
malattia@linux.it1549ee62007-04-09 10:19:08 +02001368 */
malattia@linux.it33a04452007-04-09 19:26:03 +02001369static acpi_status
1370sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
1371{
1372 u32 i;
1373 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
1374
1375 switch (resource->type) {
1376 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
1377 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
1378 return AE_OK;
1379
1380 case ACPI_RESOURCE_TYPE_IRQ:
1381 {
1382 struct acpi_resource_irq *p = &resource->data.irq;
1383 struct sony_pic_irq *interrupt = NULL;
1384 if (!p || !p->interrupt_count) {
1385 /*
1386 * IRQ descriptors may have no IRQ# bits set,
1387 * particularly those those w/ _STA disabled
1388 */
1389 dprintk("Blank IRQ resource\n");
1390 return AE_OK;
1391 }
1392 for (i = 0; i < p->interrupt_count; i++) {
1393 if (!p->interrupts[i]) {
1394 printk(KERN_WARNING DRV_PFX
1395 "Invalid IRQ %d\n",
1396 p->interrupts[i]);
1397 continue;
1398 }
1399 interrupt = kzalloc(sizeof(*interrupt),
1400 GFP_KERNEL);
1401 if (!interrupt)
1402 return AE_ERROR;
1403
1404 list_add(&interrupt->list, &dev->interrupts);
1405 interrupt->irq.triggering = p->triggering;
1406 interrupt->irq.polarity = p->polarity;
1407 interrupt->irq.sharable = p->sharable;
1408 interrupt->irq.interrupt_count = 1;
1409 interrupt->irq.interrupts[0] = p->interrupts[i];
1410 }
1411 return AE_OK;
1412 }
1413 case ACPI_RESOURCE_TYPE_IO:
1414 {
1415 struct acpi_resource_io *io = &resource->data.io;
1416 struct sony_pic_ioport *ioport = NULL;
1417 if (!io) {
1418 dprintk("Blank IO resource\n");
1419 return AE_OK;
1420 }
1421
1422 ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
1423 if (!ioport)
1424 return AE_ERROR;
1425
1426 list_add(&ioport->list, &dev->ioports);
1427 memcpy(&ioport->io, io, sizeof(*io));
1428 return AE_OK;
1429 }
1430 default:
1431 dprintk("Resource %d isn't an IRQ nor an IO port\n",
1432 resource->type);
1433
1434 case ACPI_RESOURCE_TYPE_END_TAG:
1435 return AE_OK;
1436 }
1437 return AE_CTRL_TERMINATE;
1438}
1439
1440static int sony_pic_possible_resources(struct acpi_device *device)
1441{
1442 int result = 0;
1443 acpi_status status = AE_OK;
1444
1445 if (!device)
1446 return -EINVAL;
1447
1448 /* get device status */
1449 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
1450 dprintk("Evaluating _STA\n");
1451 result = acpi_bus_get_status(device);
1452 if (result) {
1453 printk(KERN_WARNING DRV_PFX "Unable to read status\n");
1454 goto end;
1455 }
1456
1457 if (!device->status.enabled)
1458 dprintk("Device disabled\n");
1459 else
1460 dprintk("Device enabled\n");
1461
1462 /*
1463 * Query and parse 'method'
1464 */
1465 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
1466 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
1467 sony_pic_read_possible_resource, &spic_dev);
1468 if (ACPI_FAILURE(status)) {
1469 printk(KERN_WARNING DRV_PFX
1470 "Failure evaluating %s\n",
1471 METHOD_NAME__PRS);
1472 result = -ENODEV;
1473 }
1474end:
1475 return result;
1476}
1477
1478/*
1479 * Disable the spic device by calling its _DIS method
1480 */
1481static int sony_pic_disable(struct acpi_device *device)
1482{
1483 if (ACPI_FAILURE(acpi_evaluate_object(device->handle, "_DIS", 0, NULL)))
1484 return -ENXIO;
1485
1486 dprintk("Device disabled\n");
1487 return 0;
1488}
1489
1490
1491/*
1492 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
1493 *
1494 * Call _SRS to set current resources
1495 */
1496static int sony_pic_enable(struct acpi_device *device,
1497 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
1498{
1499 acpi_status status;
1500 int result = 0;
1501 struct {
1502 struct acpi_resource io_res;
1503 struct acpi_resource irq_res;
1504 struct acpi_resource end;
1505 } *resource;
1506 struct acpi_buffer buffer = { 0, NULL };
1507
1508 if (!ioport || !irq)
1509 return -EINVAL;
1510
1511 /* init acpi_buffer */
1512 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
1513 if (!resource)
1514 return -ENOMEM;
1515
1516 buffer.length = sizeof(*resource) + 1;
1517 buffer.pointer = resource;
1518
1519 /* setup io resource */
1520 resource->io_res.type = ACPI_RESOURCE_TYPE_IO;
1521 resource->io_res.length = sizeof(struct acpi_resource);
1522 memcpy(&resource->io_res.data.io, &ioport->io,
1523 sizeof(struct acpi_resource_io));
1524
1525 /* setup irq resource */
1526 resource->irq_res.type = ACPI_RESOURCE_TYPE_IRQ;
1527 resource->irq_res.length = sizeof(struct acpi_resource);
1528 memcpy(&resource->irq_res.data.irq, &irq->irq,
1529 sizeof(struct acpi_resource_irq));
1530 /* we requested a shared irq */
1531 resource->irq_res.data.irq.sharable = ACPI_SHARED;
1532
1533 resource->end.type = ACPI_RESOURCE_TYPE_END_TAG;
1534
1535 /* Attempt to set the resource */
1536 dprintk("Evaluating _SRS\n");
1537 status = acpi_set_current_resources(device->handle, &buffer);
1538
1539 /* check for total failure */
1540 if (ACPI_FAILURE(status)) {
1541 printk(KERN_ERR DRV_PFX "Error evaluating _SRS");
1542 result = -ENODEV;
1543 goto end;
1544 }
1545
1546 /* Necessary device initializations calls (from sonypi) */
1547 sony_pic_call1(0x82);
1548 sony_pic_call2(0x81, 0xff);
1549 sony_pic_call1(compat ? 0x92 : 0x82);
1550
1551end:
1552 kfree(resource);
1553 return result;
1554}
1555
1556/*****************
1557 *
1558 * ISR: some event is available
1559 *
1560 *****************/
1561static irqreturn_t sony_pic_irq(int irq, void *dev_id)
1562{
1563 int i, j;
1564 u32 port_val = 0;
1565 u8 ev = 0;
1566 u8 data_mask = 0;
1567 u8 device_event = 0;
1568
1569 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
1570
1571 acpi_os_read_port(dev->cur_ioport->io.minimum, &port_val,
1572 dev->cur_ioport->io.address_length);
1573 ev = port_val & SONY_PIC_EV_MASK;
1574 data_mask = 0xff & (port_val >> (dev->cur_ioport->io.address_length - 8));
1575
1576 dprintk("event (0x%.8x [%.2x] [%.2x]) at port 0x%.4x\n",
1577 port_val, ev, data_mask, dev->cur_ioport->io.minimum);
1578
1579 if (ev == 0x00 || ev == 0xff)
1580 return IRQ_HANDLED;
1581
1582 for (i = 0; sony_pic_eventtypes[i].model; i++) {
1583
1584 if (spic_dev.model != sony_pic_eventtypes[i].model)
1585 continue;
1586
1587 if ((data_mask & sony_pic_eventtypes[i].data) !=
1588 sony_pic_eventtypes[i].data)
1589 continue;
1590
1591 if (!(mask & sony_pic_eventtypes[i].mask))
1592 continue;
1593
1594 for (j = 0; sony_pic_eventtypes[i].events[j].event; j++) {
1595 if (ev == sony_pic_eventtypes[i].events[j].data) {
1596 device_event =
1597 sony_pic_eventtypes[i].events[j].event;
1598 goto found;
1599 }
1600 }
1601 }
1602 return IRQ_HANDLED;
1603
1604found:
malattia@linux.it1549ee62007-04-09 10:19:08 +02001605 sony_laptop_report_input_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02001606 acpi_bus_generate_event(spic_dev.acpi_dev, 1, device_event);
1607
1608 return IRQ_HANDLED;
1609}
1610
1611/*****************
1612 *
1613 * ACPI driver
1614 *
1615 *****************/
1616static int sony_pic_remove(struct acpi_device *device, int type)
1617{
1618 struct sony_pic_ioport *io, *tmp_io;
1619 struct sony_pic_irq *irq, *tmp_irq;
1620
1621 if (sony_pic_disable(device)) {
1622 printk(KERN_ERR DRV_PFX "Couldn't disable device.\n");
1623 return -ENXIO;
1624 }
1625
1626 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
1627 release_region(spic_dev.cur_ioport->io.minimum,
1628 spic_dev.cur_ioport->io.address_length);
1629
malattia@linux.it1549ee62007-04-09 10:19:08 +02001630 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02001631
malattia@linux.it49a11de2007-04-09 19:28:56 +02001632 /* pf attrs */
1633 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
1634 sony_pf_remove();
1635
malattia@linux.it33a04452007-04-09 19:26:03 +02001636 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
1637 list_del(&io->list);
1638 kfree(io);
1639 }
1640 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
1641 list_del(&irq->list);
1642 kfree(irq);
1643 }
1644 spic_dev.cur_ioport = NULL;
1645 spic_dev.cur_irq = NULL;
1646
malattia@linux.itf6119b02007-04-09 19:31:06 +02001647 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02001648 return 0;
1649}
1650
1651static int sony_pic_add(struct acpi_device *device)
1652{
1653 int result;
1654 struct sony_pic_ioport *io, *tmp_io;
1655 struct sony_pic_irq *irq, *tmp_irq;
1656
malattia@linux.itf6119b02007-04-09 19:31:06 +02001657 printk(KERN_INFO DRV_PFX "%s v%s.\n",
1658 SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.it33a04452007-04-09 19:26:03 +02001659
1660 spic_dev.acpi_dev = device;
1661 strcpy(acpi_device_class(device), "sony/hotkey");
1662 spic_dev.model = sony_pic_detect_device_type();
1663
1664 /* read _PRS resources */
1665 result = sony_pic_possible_resources(device);
1666 if (result) {
1667 printk(KERN_ERR DRV_PFX
1668 "Unabe to read possible resources.\n");
1669 goto err_free_resources;
1670 }
1671
1672 /* setup input devices and helper fifo */
malattia@linux.it1549ee62007-04-09 10:19:08 +02001673 result = sony_laptop_setup_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02001674 if (result) {
1675 printk(KERN_ERR DRV_PFX
1676 "Unabe to create input devices.\n");
1677 goto err_free_resources;
1678 }
1679
1680 /* request io port */
1681 list_for_each_entry(io, &spic_dev.ioports, list) {
1682 if (request_region(io->io.minimum, io->io.address_length,
1683 "Sony Programable I/O Device")) {
1684 dprintk("I/O port: 0x%.4x (0x%.4x) + 0x%.2x\n",
1685 io->io.minimum, io->io.maximum,
1686 io->io.address_length);
1687 spic_dev.cur_ioport = io;
1688 break;
1689 }
1690 }
1691 if (!spic_dev.cur_ioport) {
1692 printk(KERN_ERR DRV_PFX "Failed to request_region.\n");
1693 result = -ENODEV;
1694 goto err_remove_input;
1695 }
1696
1697 /* request IRQ */
1698 list_for_each_entry(irq, &spic_dev.interrupts, list) {
1699 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
1700 IRQF_SHARED, "sony-laptop", &spic_dev)) {
1701 dprintk("IRQ: %d - triggering: %d - "
1702 "polarity: %d - shr: %d\n",
1703 irq->irq.interrupts[0],
1704 irq->irq.triggering,
1705 irq->irq.polarity,
1706 irq->irq.sharable);
1707 spic_dev.cur_irq = irq;
1708 break;
1709 }
1710 }
1711 if (!spic_dev.cur_irq) {
1712 printk(KERN_ERR DRV_PFX "Failed to request_irq.\n");
1713 result = -ENODEV;
1714 goto err_release_region;
1715 }
1716
1717 /* set resource status _SRS */
1718 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
1719 if (result) {
1720 printk(KERN_ERR DRV_PFX "Couldn't enable device.\n");
1721 goto err_free_irq;
1722 }
1723
malattia@linux.it49a11de2007-04-09 19:28:56 +02001724 spic_dev.bluetooth_power = -1;
1725 /* create device attributes */
1726 result = sony_pf_add();
1727 if (result)
1728 goto err_disable_device;
1729
1730 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
1731 if (result)
1732 goto err_remove_pf;
1733
malattia@linux.it33a04452007-04-09 19:26:03 +02001734 return 0;
1735
malattia@linux.it49a11de2007-04-09 19:28:56 +02001736err_remove_pf:
1737 sony_pf_remove();
1738
1739err_disable_device:
1740 sony_pic_disable(device);
1741
malattia@linux.it33a04452007-04-09 19:26:03 +02001742err_free_irq:
1743 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
1744
1745err_release_region:
1746 release_region(spic_dev.cur_ioport->io.minimum,
1747 spic_dev.cur_ioport->io.address_length);
1748
1749err_remove_input:
malattia@linux.it1549ee62007-04-09 10:19:08 +02001750 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02001751
1752err_free_resources:
1753 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
1754 list_del(&io->list);
1755 kfree(io);
1756 }
1757 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
1758 list_del(&irq->list);
1759 kfree(irq);
1760 }
1761 spic_dev.cur_ioport = NULL;
1762 spic_dev.cur_irq = NULL;
1763
1764 return result;
1765}
1766
1767static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
1768{
1769 if (sony_pic_disable(device))
1770 return -ENXIO;
1771 return 0;
1772}
1773
1774static int sony_pic_resume(struct acpi_device *device)
1775{
1776 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
1777 return 0;
1778}
1779
1780static struct acpi_driver sony_pic_driver = {
1781 .name = SONY_PIC_DRIVER_NAME,
1782 .class = SONY_PIC_CLASS,
1783 .ids = SONY_PIC_HID,
1784 .owner = THIS_MODULE,
1785 .ops = {
1786 .add = sony_pic_add,
1787 .remove = sony_pic_remove,
1788 .suspend = sony_pic_suspend,
1789 .resume = sony_pic_resume,
1790 },
1791};
1792
1793static struct dmi_system_id __initdata sonypi_dmi_table[] = {
1794 {
1795 .ident = "Sony Vaio",
1796 .matches = {
1797 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
1798 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
1799 },
1800 },
1801 {
1802 .ident = "Sony Vaio",
1803 .matches = {
1804 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
1805 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
1806 },
1807 },
1808 { }
1809};
1810
malattia@linux.it59b19102007-04-09 10:19:04 +02001811static int __init sony_laptop_init(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01001812{
malattia@linux.it33a04452007-04-09 19:26:03 +02001813 int result;
1814
1815 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
1816 result = acpi_bus_register_driver(&sony_pic_driver);
1817 if (result) {
1818 printk(KERN_ERR DRV_PFX
1819 "Unable to register SPIC driver.");
1820 goto out;
1821 }
1822 }
1823
1824 result = acpi_bus_register_driver(&sony_nc_driver);
1825 if (result) {
1826 printk(KERN_ERR DRV_PFX "Unable to register SNC driver.");
1827 goto out_unregister_pic;
1828 }
1829
1830 return 0;
1831
1832out_unregister_pic:
1833 if (!no_spic)
1834 acpi_bus_unregister_driver(&sony_pic_driver);
1835out:
1836 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +01001837}
1838
malattia@linux.it59b19102007-04-09 10:19:04 +02001839static void __exit sony_laptop_exit(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01001840{
malattia@linux.it59b19102007-04-09 10:19:04 +02001841 acpi_bus_unregister_driver(&sony_nc_driver);
malattia@linux.it33a04452007-04-09 19:26:03 +02001842 if (!no_spic)
1843 acpi_bus_unregister_driver(&sony_pic_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +01001844}
1845
malattia@linux.it59b19102007-04-09 10:19:04 +02001846module_init(sony_laptop_init);
1847module_exit(sony_laptop_exit);