blob: 9c2e80b3824150a448b555c377278543c30cae7b [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>
malattia@linux.it1ce82c12007-04-28 23:34:36 +090065#include <linux/sony-laptop.h>
malattia@linux.it7b153f32007-04-09 19:31:25 +020066#ifdef CONFIG_SONY_LAPTOP_OLD
67#include <linux/poll.h>
68#include <linux/miscdevice.h>
69#endif
Stelian Pop7f09c432007-01-13 23:04:31 +010070
malattia@linux.it33a04452007-04-09 19:26:03 +020071#define DRV_PFX "sony-laptop: "
malattia@linux.itb9a218b2007-04-09 10:19:06 +020072#define dprintk(msg...) do { \
malattia@linux.itf6119b02007-04-09 19:31:06 +020073 if (debug) printk(KERN_WARNING DRV_PFX msg); \
malattia@linux.itb9a218b2007-04-09 10:19:06 +020074} while (0)
75
malattia@linux.it33a04452007-04-09 19:26:03 +020076#define SONY_LAPTOP_DRIVER_VERSION "0.5"
Alessandro Guido50f62af2007-01-13 23:04:34 +010077
malattia@linux.it33a04452007-04-09 19:26:03 +020078#define SONY_NC_CLASS "sony-nc"
79#define SONY_NC_HID "SNY5001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020080#define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
malattia@linux.it33a04452007-04-09 19:26:03 +020081
82#define SONY_PIC_CLASS "sony-pic"
83#define SONY_PIC_HID "SNY6001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020084#define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
Stelian Pop7f09c432007-01-13 23:04:31 +010085
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010086MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
malattia@linux.it33a04452007-04-09 19:26:03 +020087MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
Stelian Pop7f09c432007-01-13 23:04:31 +010088MODULE_LICENSE("GPL");
malattia@linux.it33a04452007-04-09 19:26:03 +020089MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
Stelian Pop7f09c432007-01-13 23:04:31 +010090
91static int debug;
92module_param(debug, int, 0);
93MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
Len Browna02d1c12007-02-07 15:34:02 -050094 "the development of this driver");
Stelian Pop7f09c432007-01-13 23:04:31 +010095
malattia@linux.it33a04452007-04-09 19:26:03 +020096static int no_spic; /* = 0 */
97module_param(no_spic, int, 0444);
98MODULE_PARM_DESC(no_spic,
99 "set this if you don't want to enable the SPIC device");
100
101static int compat; /* = 0 */
102module_param(compat, int, 0444);
103MODULE_PARM_DESC(compat,
malattia@linux.it7b153f32007-04-09 19:31:25 +0200104 "set this if you want to enable backward compatibility mode");
malattia@linux.it33a04452007-04-09 19:26:03 +0200105
106static unsigned long mask = 0xffffffff;
107module_param(mask, ulong, 0644);
108MODULE_PARM_DESC(mask,
109 "set this to the mask of event you want to enable (see doc)");
110
malattia@linux.it5f3d2892007-04-28 23:18:45 +0900111static int camera; /* = 0 */
112module_param(camera, int, 0444);
113MODULE_PARM_DESC(camera,
114 "set this to 1 to enable Motion Eye camera controls "
115 "(only use it if you have a C1VE or C1VN model)");
116
malattia@linux.it7b153f32007-04-09 19:31:25 +0200117#ifdef CONFIG_SONY_LAPTOP_OLD
118static int minor = -1;
119module_param(minor, int, 0);
120MODULE_PARM_DESC(minor,
121 "minor number of the misc device for the SPIC compatibility code, "
122 "default is -1 (automatic)");
123#endif
124
malattia@linux.it1549ee62007-04-09 10:19:08 +0200125/*********** Input Devices ***********/
126
127#define SONY_LAPTOP_BUF_SIZE 128
128struct sony_laptop_input_s {
129 atomic_t users;
130 struct input_dev *jog_dev;
131 struct input_dev *key_dev;
132 struct kfifo *fifo;
133 spinlock_t fifo_lock;
134 struct workqueue_struct *wq;
135};
136static struct sony_laptop_input_s sony_laptop_input = {
137 .users = ATOMIC_INIT(0),
138};
139
140struct sony_laptop_keypress {
141 struct input_dev *dev;
142 int key;
143};
144
145/* Correspondance table between sonypi events and input layer events */
146static struct {
147 int sonypiev;
148 int inputev;
149} sony_laptop_inputkeys[] = {
150 { SONYPI_EVENT_CAPTURE_PRESSED, KEY_CAMERA },
151 { SONYPI_EVENT_FNKEY_ONLY, KEY_FN },
152 { SONYPI_EVENT_FNKEY_ESC, KEY_FN_ESC },
153 { SONYPI_EVENT_FNKEY_F1, KEY_FN_F1 },
154 { SONYPI_EVENT_FNKEY_F2, KEY_FN_F2 },
155 { SONYPI_EVENT_FNKEY_F3, KEY_FN_F3 },
156 { SONYPI_EVENT_FNKEY_F4, KEY_FN_F4 },
157 { SONYPI_EVENT_FNKEY_F5, KEY_FN_F5 },
158 { SONYPI_EVENT_FNKEY_F6, KEY_FN_F6 },
159 { SONYPI_EVENT_FNKEY_F7, KEY_FN_F7 },
160 { SONYPI_EVENT_FNKEY_F8, KEY_FN_F8 },
161 { SONYPI_EVENT_FNKEY_F9, KEY_FN_F9 },
162 { SONYPI_EVENT_FNKEY_F10, KEY_FN_F10 },
163 { SONYPI_EVENT_FNKEY_F11, KEY_FN_F11 },
164 { SONYPI_EVENT_FNKEY_F12, KEY_FN_F12 },
165 { SONYPI_EVENT_FNKEY_1, KEY_FN_1 },
166 { SONYPI_EVENT_FNKEY_2, KEY_FN_2 },
167 { SONYPI_EVENT_FNKEY_D, KEY_FN_D },
168 { SONYPI_EVENT_FNKEY_E, KEY_FN_E },
169 { SONYPI_EVENT_FNKEY_F, KEY_FN_F },
170 { SONYPI_EVENT_FNKEY_S, KEY_FN_S },
171 { SONYPI_EVENT_FNKEY_B, KEY_FN_B },
172 { SONYPI_EVENT_BLUETOOTH_PRESSED, KEY_BLUE },
173 { SONYPI_EVENT_BLUETOOTH_ON, KEY_BLUE },
174 { SONYPI_EVENT_PKEY_P1, KEY_PROG1 },
175 { SONYPI_EVENT_PKEY_P2, KEY_PROG2 },
176 { SONYPI_EVENT_PKEY_P3, KEY_PROG3 },
177 { SONYPI_EVENT_BACK_PRESSED, KEY_BACK },
178 { SONYPI_EVENT_HELP_PRESSED, KEY_HELP },
179 { SONYPI_EVENT_ZOOM_PRESSED, KEY_ZOOM },
180 { SONYPI_EVENT_THUMBPHRASE_PRESSED, BTN_THUMB },
181 { 0, 0 },
182};
183
184/* release buttons after a short delay if pressed */
185static void do_sony_laptop_release_key(struct work_struct *work)
186{
187 struct sony_laptop_keypress kp;
188
189 while (kfifo_get(sony_laptop_input.fifo, (unsigned char *)&kp,
190 sizeof(kp)) == sizeof(kp)) {
191 msleep(10);
192 input_report_key(kp.dev, kp.key, 0);
193 input_sync(kp.dev);
194 }
195}
196static DECLARE_WORK(sony_laptop_release_key_work,
197 do_sony_laptop_release_key);
198
199/* forward event to the input subsytem */
200static void sony_laptop_report_input_event(u8 event)
201{
202 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
203 struct input_dev *key_dev = sony_laptop_input.key_dev;
204 struct sony_laptop_keypress kp = { NULL };
205 int i;
206
207 if (event == SONYPI_EVENT_FNKEY_RELEASED) {
208 /* Nothing, not all VAIOs generate this event */
209 return;
210 }
211
212 /* report events */
213 switch (event) {
214 /* jog_dev events */
215 case SONYPI_EVENT_JOGDIAL_UP:
216 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
217 input_report_rel(jog_dev, REL_WHEEL, 1);
218 input_sync(jog_dev);
219 return;
220
221 case SONYPI_EVENT_JOGDIAL_DOWN:
222 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
223 input_report_rel(jog_dev, REL_WHEEL, -1);
224 input_sync(jog_dev);
225 return;
226
227 /* key_dev events */
228 case SONYPI_EVENT_JOGDIAL_PRESSED:
229 kp.key = BTN_MIDDLE;
230 kp.dev = jog_dev;
231 break;
232
233 default:
234 for (i = 0; sony_laptop_inputkeys[i].sonypiev; i++)
235 if (event == sony_laptop_inputkeys[i].sonypiev) {
236 kp.dev = key_dev;
237 kp.key = sony_laptop_inputkeys[i].inputev;
238 break;
239 }
240 break;
241 }
242
243 if (kp.dev) {
244 input_report_key(kp.dev, kp.key, 1);
245 input_sync(kp.dev);
246 kfifo_put(sony_laptop_input.fifo,
247 (unsigned char *)&kp, sizeof(kp));
248
249 if (!work_pending(&sony_laptop_release_key_work))
250 queue_work(sony_laptop_input.wq,
251 &sony_laptop_release_key_work);
252 } else
253 dprintk("unknown input event %.2x\n", event);
254}
255
256static int sony_laptop_setup_input(void)
257{
258 struct input_dev *jog_dev;
259 struct input_dev *key_dev;
260 int i;
261 int error;
262
263 /* don't run again if already initialized */
264 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
265 return 0;
266
267 /* kfifo */
268 spin_lock_init(&sony_laptop_input.fifo_lock);
269 sony_laptop_input.fifo =
270 kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
271 &sony_laptop_input.fifo_lock);
272 if (IS_ERR(sony_laptop_input.fifo)) {
273 printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
274 error = PTR_ERR(sony_laptop_input.fifo);
275 goto err_dec_users;
276 }
277
278 /* init workqueue */
279 sony_laptop_input.wq = create_singlethread_workqueue("sony-laptop");
280 if (!sony_laptop_input.wq) {
281 printk(KERN_ERR DRV_PFX
282 "Unabe to create workqueue.\n");
283 error = -ENXIO;
284 goto err_free_kfifo;
285 }
286
287 /* input keys */
288 key_dev = input_allocate_device();
289 if (!key_dev) {
290 error = -ENOMEM;
291 goto err_destroy_wq;
292 }
293
294 key_dev->name = "Sony Vaio Keys";
295 key_dev->id.bustype = BUS_ISA;
296 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
297
298 /* Initialize the Input Drivers: special keys */
299 key_dev->evbit[0] = BIT(EV_KEY);
300 for (i = 0; sony_laptop_inputkeys[i].sonypiev; i++)
301 if (sony_laptop_inputkeys[i].inputev)
302 set_bit(sony_laptop_inputkeys[i].inputev,
303 key_dev->keybit);
304
305 error = input_register_device(key_dev);
306 if (error)
307 goto err_free_keydev;
308
309 sony_laptop_input.key_dev = key_dev;
310
311 /* jogdial */
312 jog_dev = input_allocate_device();
313 if (!jog_dev) {
314 error = -ENOMEM;
315 goto err_unregister_keydev;
316 }
317
318 jog_dev->name = "Sony Vaio Jogdial";
319 jog_dev->id.bustype = BUS_ISA;
320 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
321
322 jog_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
323 jog_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_MIDDLE);
324 jog_dev->relbit[0] = BIT(REL_WHEEL);
325
326 error = input_register_device(jog_dev);
327 if (error)
328 goto err_free_jogdev;
329
330 sony_laptop_input.jog_dev = jog_dev;
331
332 return 0;
333
334err_free_jogdev:
335 input_free_device(jog_dev);
336
337err_unregister_keydev:
338 input_unregister_device(key_dev);
339 /* to avoid kref underflow below at input_free_device */
340 key_dev = NULL;
341
342err_free_keydev:
343 input_free_device(key_dev);
344
345err_destroy_wq:
346 destroy_workqueue(sony_laptop_input.wq);
347
348err_free_kfifo:
349 kfifo_free(sony_laptop_input.fifo);
350
351err_dec_users:
352 atomic_dec(&sony_laptop_input.users);
353 return error;
354}
355
356static void sony_laptop_remove_input(void)
357{
358 /* cleanup only after the last user has gone */
359 if (!atomic_dec_and_test(&sony_laptop_input.users))
360 return;
361
362 /* flush workqueue first */
363 flush_workqueue(sony_laptop_input.wq);
364
365 /* destroy input devs */
366 input_unregister_device(sony_laptop_input.key_dev);
367 sony_laptop_input.key_dev = NULL;
368
369 if (sony_laptop_input.jog_dev) {
370 input_unregister_device(sony_laptop_input.jog_dev);
371 sony_laptop_input.jog_dev = NULL;
372 }
373
374 destroy_workqueue(sony_laptop_input.wq);
375 kfifo_free(sony_laptop_input.fifo);
376}
377
malattia@linux.it56b87562007-04-09 10:19:05 +0200378/*********** Platform Device ***********/
379
380static atomic_t sony_pf_users = ATOMIC_INIT(0);
381static struct platform_driver sony_pf_driver = {
382 .driver = {
383 .name = "sony-laptop",
384 .owner = THIS_MODULE,
385 }
386};
387static struct platform_device *sony_pf_device;
388
389static int sony_pf_add(void)
390{
391 int ret = 0;
392
393 /* don't run again if already initialized */
394 if (atomic_add_return(1, &sony_pf_users) > 1)
395 return 0;
396
397 ret = platform_driver_register(&sony_pf_driver);
398 if (ret)
399 goto out;
400
401 sony_pf_device = platform_device_alloc("sony-laptop", -1);
402 if (!sony_pf_device) {
403 ret = -ENOMEM;
404 goto out_platform_registered;
405 }
406
407 ret = platform_device_add(sony_pf_device);
408 if (ret)
409 goto out_platform_alloced;
410
411 return 0;
412
413 out_platform_alloced:
414 platform_device_put(sony_pf_device);
415 sony_pf_device = NULL;
416 out_platform_registered:
417 platform_driver_unregister(&sony_pf_driver);
418 out:
419 atomic_dec(&sony_pf_users);
420 return ret;
421}
422
423static void sony_pf_remove(void)
424{
425 /* deregister only after the last user has gone */
426 if (!atomic_dec_and_test(&sony_pf_users))
427 return;
428
429 platform_device_del(sony_pf_device);
430 platform_device_put(sony_pf_device);
431 platform_driver_unregister(&sony_pf_driver);
432}
433
434/*********** SNC (SNY5001) Device ***********/
435
malattia@linux.it33a04452007-04-09 19:26:03 +0200436/* the device uses 1-based values, while the backlight subsystem uses
437 0-based values */
438#define SONY_MAX_BRIGHTNESS 8
439
440#define SNC_VALIDATE_IN 0
441#define SNC_VALIDATE_OUT 1
442
malattia@linux.it59b19102007-04-09 10:19:04 +0200443static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500444 char *);
malattia@linux.it59b19102007-04-09 10:19:04 +0200445static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500446 const char *, size_t);
Mattia Dongili156c2212007-02-12 22:01:07 +0100447static int boolean_validate(const int, const int);
448static int brightness_default_validate(const int, const int);
449
malattia@linux.it59b19102007-04-09 10:19:04 +0200450struct sony_nc_value {
Len Browna02d1c12007-02-07 15:34:02 -0500451 char *name; /* name of the entry */
452 char **acpiget; /* names of the ACPI get function */
453 char **acpiset; /* names of the ACPI set function */
Mattia Dongili156c2212007-02-12 22:01:07 +0100454 int (*validate)(const int, const int); /* input/output validation */
Len Browna02d1c12007-02-07 15:34:02 -0500455 int value; /* current setting */
456 int valid; /* Has ever been set */
457 int debug; /* active only in debug mode ? */
458 struct device_attribute devattr; /* sysfs atribute */
Stelian Pop7f09c432007-01-13 23:04:31 +0100459};
460
malattia@linux.it59b19102007-04-09 10:19:04 +0200461#define SNC_HANDLE_NAMES(_name, _values...) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100462 static char *snc_##_name[] = { _values, NULL }
463
malattia@linux.it59b19102007-04-09 10:19:04 +0200464#define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100465 { \
466 .name = __stringify(_name), \
467 .acpiget = _getters, \
468 .acpiset = _setters, \
Mattia Dongili156c2212007-02-12 22:01:07 +0100469 .validate = _validate, \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100470 .debug = _debug, \
malattia@linux.it59b19102007-04-09 10:19:04 +0200471 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100472 }
473
malattia@linux.it59b19102007-04-09 10:19:04 +0200474#define SNC_HANDLE_NULL { .name = NULL }
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100475
malattia@linux.it59b19102007-04-09 10:19:04 +0200476SNC_HANDLE_NAMES(fnkey_get, "GHKE");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100477
malattia@linux.it59b19102007-04-09 10:19:04 +0200478SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
479SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100480
malattia@linux.it59b19102007-04-09 10:19:04 +0200481SNC_HANDLE_NAMES(cdpower_get, "GCDP");
482SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100483
malattia@linux.it59b19102007-04-09 10:19:04 +0200484SNC_HANDLE_NAMES(audiopower_get, "GAZP");
485SNC_HANDLE_NAMES(audiopower_set, "AZPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100486
malattia@linux.it59b19102007-04-09 10:19:04 +0200487SNC_HANDLE_NAMES(lanpower_get, "GLNP");
488SNC_HANDLE_NAMES(lanpower_set, "LNPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100489
malattia@linux.it59b19102007-04-09 10:19:04 +0200490SNC_HANDLE_NAMES(PID_get, "GPID");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100491
malattia@linux.it59b19102007-04-09 10:19:04 +0200492SNC_HANDLE_NAMES(CTR_get, "GCTR");
493SNC_HANDLE_NAMES(CTR_set, "SCTR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100494
malattia@linux.it59b19102007-04-09 10:19:04 +0200495SNC_HANDLE_NAMES(PCR_get, "GPCR");
496SNC_HANDLE_NAMES(PCR_set, "SPCR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100497
malattia@linux.it59b19102007-04-09 10:19:04 +0200498SNC_HANDLE_NAMES(CMI_get, "GCMI");
499SNC_HANDLE_NAMES(CMI_set, "SCMI");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100500
malattia@linux.it59b19102007-04-09 10:19:04 +0200501static struct sony_nc_value sony_nc_values[] = {
502 SNC_HANDLE(brightness_default, snc_brightness_def_get,
Mattia Dongili156c2212007-02-12 22:01:07 +0100503 snc_brightness_def_set, brightness_default_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200504 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
505 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
506 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100507 boolean_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200508 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100509 boolean_validate, 1),
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100510 /* unknown methods */
malattia@linux.it59b19102007-04-09 10:19:04 +0200511 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
512 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
513 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
514 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
515 SNC_HANDLE_NULL
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100516};
517
malattia@linux.it59b19102007-04-09 10:19:04 +0200518static acpi_handle sony_nc_acpi_handle;
519static struct acpi_device *sony_nc_acpi_device = NULL;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100520
Mattia Dongilid78865c2007-02-07 20:01:56 +0100521/*
522 * acpi_evaluate_object wrappers
523 */
Stelian Pop7f09c432007-01-13 23:04:31 +0100524static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
525{
526 struct acpi_buffer output;
527 union acpi_object out_obj;
528 acpi_status status;
529
530 output.length = sizeof(out_obj);
531 output.pointer = &out_obj;
532
533 status = acpi_evaluate_object(handle, name, NULL, &output);
534 if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
535 *result = out_obj.integer.value;
536 return 0;
537 }
538
malattia@linux.itf6119b02007-04-09 19:31:06 +0200539 printk(KERN_WARNING DRV_PFX "acpi_callreadfunc failed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100540
541 return -1;
542}
543
544static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
545 int *result)
546{
547 struct acpi_object_list params;
548 union acpi_object in_obj;
549 struct acpi_buffer output;
550 union acpi_object out_obj;
551 acpi_status status;
552
553 params.count = 1;
554 params.pointer = &in_obj;
555 in_obj.type = ACPI_TYPE_INTEGER;
556 in_obj.integer.value = value;
557
558 output.length = sizeof(out_obj);
559 output.pointer = &out_obj;
560
561 status = acpi_evaluate_object(handle, name, &params, &output);
562 if (status == AE_OK) {
563 if (result != NULL) {
564 if (out_obj.type != ACPI_TYPE_INTEGER) {
malattia@linux.itf6119b02007-04-09 19:31:06 +0200565 printk(KERN_WARNING DRV_PFX "acpi_evaluate_object bad "
Stelian Pop7f09c432007-01-13 23:04:31 +0100566 "return type\n");
567 return -1;
568 }
569 *result = out_obj.integer.value;
570 }
571 return 0;
572 }
573
malattia@linux.itf6119b02007-04-09 19:31:06 +0200574 printk(KERN_WARNING DRV_PFX "acpi_evaluate_object failed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100575
576 return -1;
577}
578
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100579/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200580 * sony_nc_values input/output validate functions
Mattia Dongili156c2212007-02-12 22:01:07 +0100581 */
582
583/* brightness_default_validate:
584 *
585 * manipulate input output values to keep consistency with the
586 * backlight framework for which brightness values are 0-based.
587 */
588static int brightness_default_validate(const int direction, const int value)
589{
590 switch (direction) {
591 case SNC_VALIDATE_OUT:
592 return value - 1;
593 case SNC_VALIDATE_IN:
594 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
595 return value + 1;
596 }
597 return -EINVAL;
598}
599
600/* boolean_validate:
601 *
602 * on input validate boolean values 0/1, on output just pass the
603 * received value.
604 */
605static int boolean_validate(const int direction, const int value)
606{
607 if (direction == SNC_VALIDATE_IN) {
608 if (value != 0 && value != 1)
609 return -EINVAL;
610 }
611 return value;
612}
613
614/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200615 * Sysfs show/store common to all sony_nc_values
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100616 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200617static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
Len Browna02d1c12007-02-07 15:34:02 -0500618 char *buffer)
Stelian Pop7f09c432007-01-13 23:04:31 +0100619{
Stelian Pop7f09c432007-01-13 23:04:31 +0100620 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200621 struct sony_nc_value *item =
622 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100623
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100624 if (!*item->acpiget)
Stelian Pop7f09c432007-01-13 23:04:31 +0100625 return -EIO;
626
malattia@linux.it59b19102007-04-09 10:19:04 +0200627 if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100628 return -EIO;
629
Mattia Dongili156c2212007-02-12 22:01:07 +0100630 if (item->validate)
631 value = item->validate(SNC_VALIDATE_OUT, value);
632
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100633 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
Stelian Pop7f09c432007-01-13 23:04:31 +0100634}
635
malattia@linux.it59b19102007-04-09 10:19:04 +0200636static ssize_t sony_nc_sysfs_store(struct device *dev,
Len Browna02d1c12007-02-07 15:34:02 -0500637 struct device_attribute *attr,
638 const char *buffer, size_t count)
Stelian Pop7f09c432007-01-13 23:04:31 +0100639{
Stelian Pop7f09c432007-01-13 23:04:31 +0100640 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200641 struct sony_nc_value *item =
642 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100643
644 if (!item->acpiset)
645 return -EIO;
646
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100647 if (count > 31)
648 return -EINVAL;
649
650 value = simple_strtoul(buffer, NULL, 10);
Stelian Pop7f09c432007-01-13 23:04:31 +0100651
Mattia Dongili156c2212007-02-12 22:01:07 +0100652 if (item->validate)
653 value = item->validate(SNC_VALIDATE_IN, value);
654
655 if (value < 0)
656 return value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100657
malattia@linux.it59b19102007-04-09 10:19:04 +0200658 if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100659 return -EIO;
Andrew Morton3f4f4612007-01-13 23:04:32 +0100660 item->value = value;
661 item->valid = 1;
Stelian Pop7f09c432007-01-13 23:04:31 +0100662 return count;
663}
664
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100665
Mattia Dongilid78865c2007-02-07 20:01:56 +0100666/*
667 * Backlight device
668 */
669static int sony_backlight_update_status(struct backlight_device *bd)
Andrew Morton3f4f4612007-01-13 23:04:32 +0100670{
malattia@linux.it59b19102007-04-09 10:19:04 +0200671 return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
Richard Purdie321709c2007-02-10 15:04:08 +0000672 bd->props.brightness + 1, NULL);
Andrew Morton3f4f4612007-01-13 23:04:32 +0100673}
674
Mattia Dongilid78865c2007-02-07 20:01:56 +0100675static int sony_backlight_get_brightness(struct backlight_device *bd)
676{
677 int value;
678
malattia@linux.it59b19102007-04-09 10:19:04 +0200679 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
Mattia Dongilid78865c2007-02-07 20:01:56 +0100680 return 0;
681 /* brightness levels are 1-based, while backlight ones are 0-based */
682 return value - 1;
683}
684
685static struct backlight_device *sony_backlight_device;
Richard Purdie321709c2007-02-10 15:04:08 +0000686static struct backlight_ops sony_backlight_ops = {
Len Browna02d1c12007-02-07 15:34:02 -0500687 .update_status = sony_backlight_update_status,
688 .get_brightness = sony_backlight_get_brightness,
Mattia Dongilid78865c2007-02-07 20:01:56 +0100689};
690
691/*
692 * ACPI callbacks
693 */
Stelian Pop7f09c432007-01-13 23:04:31 +0100694static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
695{
malattia@linux.itb9a218b2007-04-09 10:19:06 +0200696 dprintk("sony_acpi_notify, event: %d\n", event);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200697 sony_laptop_report_input_event(event);
malattia@linux.it59b19102007-04-09 10:19:04 +0200698 acpi_bus_generate_event(sony_nc_acpi_device, 1, event);
Stelian Pop7f09c432007-01-13 23:04:31 +0100699}
700
701static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
702 void *context, void **return_value)
703{
704 struct acpi_namespace_node *node;
705 union acpi_operand_object *operand;
706
Len Browna02d1c12007-02-07 15:34:02 -0500707 node = (struct acpi_namespace_node *)handle;
708 operand = (union acpi_operand_object *)node->object;
Stelian Pop7f09c432007-01-13 23:04:31 +0100709
malattia@linux.itf6119b02007-04-09 19:31:06 +0200710 printk(KERN_WARNING DRV_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
Stelian Pop7f09c432007-01-13 23:04:31 +0100711 (u32) operand->method.param_count);
712
713 return AE_OK;
714}
715
Mattia Dongilid78865c2007-02-07 20:01:56 +0100716/*
717 * ACPI device
718 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200719static int sony_nc_resume(struct acpi_device *device)
Mattia Dongilid78865c2007-02-07 20:01:56 +0100720{
malattia@linux.it59b19102007-04-09 10:19:04 +0200721 struct sony_nc_value *item;
Mattia Dongilid78865c2007-02-07 20:01:56 +0100722
malattia@linux.it59b19102007-04-09 10:19:04 +0200723 for (item = sony_nc_values; item->name; item++) {
Mattia Dongilid78865c2007-02-07 20:01:56 +0100724 int ret;
725
726 if (!item->valid)
727 continue;
malattia@linux.it59b19102007-04-09 10:19:04 +0200728 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
Len Browna02d1c12007-02-07 15:34:02 -0500729 item->value, NULL);
Mattia Dongilid78865c2007-02-07 20:01:56 +0100730 if (ret < 0) {
731 printk("%s: %d\n", __FUNCTION__, ret);
732 break;
733 }
734 }
735 return 0;
736}
737
malattia@linux.it59b19102007-04-09 10:19:04 +0200738static int sony_nc_add(struct acpi_device *device)
Stelian Pop7f09c432007-01-13 23:04:31 +0100739{
740 acpi_status status;
Andrew Morton8607c672007-03-06 02:29:42 -0800741 int result = 0;
Alessandro Guido50f62af2007-01-13 23:04:34 +0100742 acpi_handle handle;
malattia@linux.it56b87562007-04-09 10:19:05 +0200743 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +0100744
malattia@linux.itf6119b02007-04-09 19:31:06 +0200745 printk(KERN_INFO DRV_PFX "%s v%s.\n",
746 SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
747
malattia@linux.it59b19102007-04-09 10:19:04 +0200748 sony_nc_acpi_device = device;
malattia@linux.it33a04452007-04-09 19:26:03 +0200749 strcpy(acpi_device_class(device), "sony/hotkey");
Stelian Popc5611622007-01-13 23:04:37 +0100750
malattia@linux.it59b19102007-04-09 10:19:04 +0200751 sony_nc_acpi_handle = device->handle;
Stelian Pop7f09c432007-01-13 23:04:31 +0100752
Stelian Pop7f09c432007-01-13 23:04:31 +0100753 if (debug) {
malattia@linux.it59b19102007-04-09 10:19:04 +0200754 status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_nc_acpi_handle,
Stelian Pop7f09c432007-01-13 23:04:31 +0100755 1, sony_walk_callback, NULL, NULL);
756 if (ACPI_FAILURE(status)) {
malattia@linux.itf6119b02007-04-09 19:31:06 +0200757 printk(KERN_WARNING DRV_PFX "unable to walk acpi resources\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100758 result = -ENODEV;
759 goto outwalk;
760 }
Stelian Popc5611622007-01-13 23:04:37 +0100761 }
Stelian Pop7f09c432007-01-13 23:04:31 +0100762
malattia@linux.it1549ee62007-04-09 10:19:08 +0200763 /* setup input devices and helper fifo */
764 result = sony_laptop_setup_input();
765 if (result) {
766 printk(KERN_ERR DRV_PFX
767 "Unabe to create input devices.\n");
768 goto outwalk;
769 }
770
malattia@linux.it59b19102007-04-09 10:19:04 +0200771 status = acpi_install_notify_handler(sony_nc_acpi_handle,
Stelian Popc5611622007-01-13 23:04:37 +0100772 ACPI_DEVICE_NOTIFY,
Len Browna02d1c12007-02-07 15:34:02 -0500773 sony_acpi_notify, NULL);
Stelian Popc5611622007-01-13 23:04:37 +0100774 if (ACPI_FAILURE(status)) {
malattia@linux.itf6119b02007-04-09 19:31:06 +0200775 printk(KERN_WARNING DRV_PFX "unable to install notify handler\n");
Stelian Popc5611622007-01-13 23:04:37 +0100776 result = -ENODEV;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200777 goto outinput;
Stelian Pop7f09c432007-01-13 23:04:31 +0100778 }
779
malattia@linux.it59b19102007-04-09 10:19:04 +0200780 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT", &handle))) {
Alessandro Guido50f62af2007-01-13 23:04:34 +0100781 sony_backlight_device = backlight_device_register("sony", NULL,
Len Browna02d1c12007-02-07 15:34:02 -0500782 NULL,
Richard Purdie321709c2007-02-10 15:04:08 +0000783 &sony_backlight_ops);
Mattia Dongili7df03b82007-01-13 23:04:41 +0100784
Len Browna02d1c12007-02-07 15:34:02 -0500785 if (IS_ERR(sony_backlight_device)) {
malattia@linux.itf6119b02007-04-09 19:31:06 +0200786 printk(KERN_WARNING DRV_PFX "unable to register backlight device\n");
Mattia Dongili7df03b82007-01-13 23:04:41 +0100787 sony_backlight_device = NULL;
Richard Purdie321709c2007-02-10 15:04:08 +0000788 } else {
789 sony_backlight_device->props.brightness =
Len Browna02d1c12007-02-07 15:34:02 -0500790 sony_backlight_get_brightness
791 (sony_backlight_device);
Richard Purdie321709c2007-02-10 15:04:08 +0000792 sony_backlight_device->props.max_brightness =
793 SONY_MAX_BRIGHTNESS - 1;
794 }
795
Alessandro Guido50f62af2007-01-13 23:04:34 +0100796 }
Stelian Pop7f09c432007-01-13 23:04:31 +0100797
malattia@linux.it49a11de2007-04-09 19:28:56 +0200798 result = sony_pf_add();
799 if (result)
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100800 goto outbacklight;
Stelian Pop7f09c432007-01-13 23:04:31 +0100801
malattia@linux.it56b87562007-04-09 10:19:05 +0200802 /* create sony_pf sysfs attributes related to the SNC device */
803 for (item = sony_nc_values; item->name; ++item) {
804
805 if (!debug && item->debug)
806 continue;
807
808 /* find the available acpiget as described in the DSDT */
809 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
810 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
811 *item->acpiget,
812 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +0200813 dprintk("Found %s getter: %s\n",
814 item->name, *item->acpiget);
malattia@linux.it56b87562007-04-09 10:19:05 +0200815 item->devattr.attr.mode |= S_IRUGO;
816 break;
817 }
818 }
819
820 /* find the available acpiset as described in the DSDT */
821 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
822 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
823 *item->acpiset,
824 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +0200825 dprintk("Found %s setter: %s\n",
826 item->name, *item->acpiset);
malattia@linux.it56b87562007-04-09 10:19:05 +0200827 item->devattr.attr.mode |= S_IWUSR;
828 break;
829 }
830 }
831
832 if (item->devattr.attr.mode != 0) {
833 result =
834 device_create_file(&sony_pf_device->dev,
835 &item->devattr);
836 if (result)
837 goto out_sysfs;
838 }
839 }
840
Stelian Pop7f09c432007-01-13 23:04:31 +0100841 return 0;
842
malattia@linux.it56b87562007-04-09 10:19:05 +0200843 out_sysfs:
844 for (item = sony_nc_values; item->name; ++item) {
845 device_remove_file(&sony_pf_device->dev, &item->devattr);
846 }
847 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +0200848
Len Browna02d1c12007-02-07 15:34:02 -0500849 outbacklight:
Mattia Dongili7df03b82007-01-13 23:04:41 +0100850 if (sony_backlight_device)
851 backlight_device_unregister(sony_backlight_device);
852
malattia@linux.it59b19102007-04-09 10:19:04 +0200853 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
Stelian Popc5611622007-01-13 23:04:37 +0100854 ACPI_DEVICE_NOTIFY,
855 sony_acpi_notify);
856 if (ACPI_FAILURE(status))
malattia@linux.itf6119b02007-04-09 19:31:06 +0200857 printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
malattia@linux.it1549ee62007-04-09 10:19:08 +0200858
859 outinput:
860 sony_laptop_remove_input();
861
Len Browna02d1c12007-02-07 15:34:02 -0500862 outwalk:
Stelian Pop7f09c432007-01-13 23:04:31 +0100863 return result;
864}
865
malattia@linux.it59b19102007-04-09 10:19:04 +0200866static int sony_nc_remove(struct acpi_device *device, int type)
Stelian Pop7f09c432007-01-13 23:04:31 +0100867{
868 acpi_status status;
malattia@linux.it56b87562007-04-09 10:19:05 +0200869 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +0100870
Alessandro Guido50f62af2007-01-13 23:04:34 +0100871 if (sony_backlight_device)
872 backlight_device_unregister(sony_backlight_device);
873
malattia@linux.it59b19102007-04-09 10:19:04 +0200874 sony_nc_acpi_device = NULL;
Stelian Popc5611622007-01-13 23:04:37 +0100875
malattia@linux.it59b19102007-04-09 10:19:04 +0200876 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
Stelian Popc5611622007-01-13 23:04:37 +0100877 ACPI_DEVICE_NOTIFY,
878 sony_acpi_notify);
879 if (ACPI_FAILURE(status))
malattia@linux.itf6119b02007-04-09 19:31:06 +0200880 printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100881
malattia@linux.it56b87562007-04-09 10:19:05 +0200882 for (item = sony_nc_values; item->name; ++item) {
883 device_remove_file(&sony_pf_device->dev, &item->devattr);
884 }
885
886 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +0200887 sony_laptop_remove_input();
malattia@linux.itf6119b02007-04-09 19:31:06 +0200888 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100889
890 return 0;
891}
892
malattia@linux.it59b19102007-04-09 10:19:04 +0200893static struct acpi_driver sony_nc_driver = {
894 .name = SONY_NC_DRIVER_NAME,
895 .class = SONY_NC_CLASS,
896 .ids = SONY_NC_HID,
malattia@linux.it33a04452007-04-09 19:26:03 +0200897 .owner = THIS_MODULE,
Len Browna02d1c12007-02-07 15:34:02 -0500898 .ops = {
malattia@linux.it59b19102007-04-09 10:19:04 +0200899 .add = sony_nc_add,
900 .remove = sony_nc_remove,
901 .resume = sony_nc_resume,
Len Browna02d1c12007-02-07 15:34:02 -0500902 },
Andrew Morton3f4f4612007-01-13 23:04:32 +0100903};
904
malattia@linux.it33a04452007-04-09 19:26:03 +0200905/*********** SPIC (SNY6001) Device ***********/
906
907#define SONYPI_DEVICE_TYPE1 0x00000001
908#define SONYPI_DEVICE_TYPE2 0x00000002
909#define SONYPI_DEVICE_TYPE3 0x00000004
910
malattia@linux.it33a04452007-04-09 19:26:03 +0200911#define SONY_PIC_EV_MASK 0xff
912
malattia@linux.it33a04452007-04-09 19:26:03 +0200913struct sony_pic_ioport {
914 struct acpi_resource_io io;
915 struct list_head list;
916};
917
918struct sony_pic_irq {
919 struct acpi_resource_irq irq;
920 struct list_head list;
921};
922
923struct sony_pic_dev {
924 int model;
malattia@linux.it49a11de2007-04-09 19:28:56 +0200925 u8 camera_power;
926 u8 bluetooth_power;
malattia@linux.it9476cdf2007-04-28 23:21:42 +0900927 u8 wwan_power;
malattia@linux.it33a04452007-04-09 19:26:03 +0200928 struct acpi_device *acpi_dev;
929 struct sony_pic_irq *cur_irq;
930 struct sony_pic_ioport *cur_ioport;
931 struct list_head interrupts;
932 struct list_head ioports;
malattia@linux.it9f9f0762007-04-28 23:19:36 +0900933 struct mutex lock;
malattia@linux.it33a04452007-04-09 19:26:03 +0200934};
935
936static struct sony_pic_dev spic_dev = {
937 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
938 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
939};
940
941/* Event masks */
942#define SONYPI_JOGGER_MASK 0x00000001
943#define SONYPI_CAPTURE_MASK 0x00000002
944#define SONYPI_FNKEY_MASK 0x00000004
945#define SONYPI_BLUETOOTH_MASK 0x00000008
946#define SONYPI_PKEY_MASK 0x00000010
947#define SONYPI_BACK_MASK 0x00000020
948#define SONYPI_HELP_MASK 0x00000040
949#define SONYPI_LID_MASK 0x00000080
950#define SONYPI_ZOOM_MASK 0x00000100
951#define SONYPI_THUMBPHRASE_MASK 0x00000200
952#define SONYPI_MEYE_MASK 0x00000400
953#define SONYPI_MEMORYSTICK_MASK 0x00000800
954#define SONYPI_BATTERY_MASK 0x00001000
955#define SONYPI_WIRELESS_MASK 0x00002000
956
957struct sonypi_event {
958 u8 data;
959 u8 event;
960};
961
962/* The set of possible button release events */
963static struct sonypi_event sonypi_releaseev[] = {
964 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
965 { 0, 0 }
966};
967
968/* The set of possible jogger events */
969static struct sonypi_event sonypi_joggerev[] = {
970 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
971 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
972 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
973 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
974 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
975 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
976 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
977 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
978 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
979 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
980 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
981 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
982 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
983 { 0, 0 }
984};
985
986/* The set of possible capture button events */
987static struct sonypi_event sonypi_captureev[] = {
988 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
989 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
990 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
991 { 0, 0 }
992};
993
994/* The set of possible fnkeys events */
995static struct sonypi_event sonypi_fnkeyev[] = {
996 { 0x10, SONYPI_EVENT_FNKEY_ESC },
997 { 0x11, SONYPI_EVENT_FNKEY_F1 },
998 { 0x12, SONYPI_EVENT_FNKEY_F2 },
999 { 0x13, SONYPI_EVENT_FNKEY_F3 },
1000 { 0x14, SONYPI_EVENT_FNKEY_F4 },
1001 { 0x15, SONYPI_EVENT_FNKEY_F5 },
1002 { 0x16, SONYPI_EVENT_FNKEY_F6 },
1003 { 0x17, SONYPI_EVENT_FNKEY_F7 },
1004 { 0x18, SONYPI_EVENT_FNKEY_F8 },
1005 { 0x19, SONYPI_EVENT_FNKEY_F9 },
1006 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
1007 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
1008 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
1009 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
1010 { 0x21, SONYPI_EVENT_FNKEY_1 },
1011 { 0x22, SONYPI_EVENT_FNKEY_2 },
1012 { 0x31, SONYPI_EVENT_FNKEY_D },
1013 { 0x32, SONYPI_EVENT_FNKEY_E },
1014 { 0x33, SONYPI_EVENT_FNKEY_F },
1015 { 0x34, SONYPI_EVENT_FNKEY_S },
1016 { 0x35, SONYPI_EVENT_FNKEY_B },
1017 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
1018 { 0, 0 }
1019};
1020
1021/* The set of possible program key events */
1022static struct sonypi_event sonypi_pkeyev[] = {
1023 { 0x01, SONYPI_EVENT_PKEY_P1 },
1024 { 0x02, SONYPI_EVENT_PKEY_P2 },
1025 { 0x04, SONYPI_EVENT_PKEY_P3 },
1026 { 0x5c, SONYPI_EVENT_PKEY_P1 },
1027 { 0, 0 }
1028};
1029
1030/* The set of possible bluetooth events */
1031static struct sonypi_event sonypi_blueev[] = {
1032 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
1033 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
1034 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
1035 { 0, 0 }
1036};
1037
1038/* The set of possible wireless events */
1039static struct sonypi_event sonypi_wlessev[] = {
1040 { 0x59, SONYPI_EVENT_WIRELESS_ON },
1041 { 0x5a, SONYPI_EVENT_WIRELESS_OFF },
1042 { 0, 0 }
1043};
1044
1045/* The set of possible back button events */
1046static struct sonypi_event sonypi_backev[] = {
1047 { 0x20, SONYPI_EVENT_BACK_PRESSED },
1048 { 0, 0 }
1049};
1050
1051/* The set of possible help button events */
1052static struct sonypi_event sonypi_helpev[] = {
1053 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
1054 { 0, 0 }
1055};
1056
1057
1058/* The set of possible lid events */
1059static struct sonypi_event sonypi_lidev[] = {
1060 { 0x51, SONYPI_EVENT_LID_CLOSED },
1061 { 0x50, SONYPI_EVENT_LID_OPENED },
1062 { 0, 0 }
1063};
1064
1065/* The set of possible zoom events */
1066static struct sonypi_event sonypi_zoomev[] = {
1067 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
1068 { 0, 0 }
1069};
1070
1071/* The set of possible thumbphrase events */
1072static struct sonypi_event sonypi_thumbphraseev[] = {
1073 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
1074 { 0, 0 }
1075};
1076
1077/* The set of possible motioneye camera events */
1078static struct sonypi_event sonypi_meyeev[] = {
1079 { 0x00, SONYPI_EVENT_MEYE_FACE },
1080 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
1081 { 0, 0 }
1082};
1083
1084/* The set of possible memorystick events */
1085static struct sonypi_event sonypi_memorystickev[] = {
1086 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
1087 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
1088 { 0, 0 }
1089};
1090
1091/* The set of possible battery events */
1092static struct sonypi_event sonypi_batteryev[] = {
1093 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
1094 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
1095 { 0, 0 }
1096};
1097
1098static struct sonypi_eventtypes {
1099 int model;
1100 u8 data;
1101 unsigned long mask;
1102 struct sonypi_event * events;
1103} sony_pic_eventtypes[] = {
1104 { SONYPI_DEVICE_TYPE1, 0, 0xffffffff, sonypi_releaseev },
1105 { SONYPI_DEVICE_TYPE1, 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
1106 { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_LID_MASK, sonypi_lidev },
1107 { SONYPI_DEVICE_TYPE1, 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
1108 { SONYPI_DEVICE_TYPE1, 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
1109 { SONYPI_DEVICE_TYPE1, 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1110 { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1111 { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
1112 { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1113 { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
1114
1115 { SONYPI_DEVICE_TYPE2, 0, 0xffffffff, sonypi_releaseev },
1116 { SONYPI_DEVICE_TYPE2, 0x38, SONYPI_LID_MASK, sonypi_lidev },
1117 { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
1118 { SONYPI_DEVICE_TYPE2, 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
1119 { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1120 { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1121 { SONYPI_DEVICE_TYPE2, 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
1122 { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_BACK_MASK, sonypi_backev },
1123 { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_HELP_MASK, sonypi_helpev },
1124 { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
1125 { SONYPI_DEVICE_TYPE2, 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
1126 { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1127 { SONYPI_DEVICE_TYPE2, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1128 { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1129
1130 { SONYPI_DEVICE_TYPE3, 0, 0xffffffff, sonypi_releaseev },
1131 { SONYPI_DEVICE_TYPE3, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1132 { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
1133 { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1134 { SONYPI_DEVICE_TYPE3, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1135 { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1136 { 0 }
1137};
1138
1139static int sony_pic_detect_device_type(void)
1140{
1141 struct pci_dev *pcidev;
1142 int model = 0;
1143
1144 if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1145 PCI_DEVICE_ID_INTEL_82371AB_3, NULL)))
1146 model = SONYPI_DEVICE_TYPE1;
1147
1148 else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1149 PCI_DEVICE_ID_INTEL_ICH6_1, NULL)))
1150 model = SONYPI_DEVICE_TYPE3;
1151
1152 else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1153 PCI_DEVICE_ID_INTEL_ICH7_1, NULL)))
1154 model = SONYPI_DEVICE_TYPE3;
1155
1156 else
1157 model = SONYPI_DEVICE_TYPE2;
1158
1159 if (pcidev)
1160 pci_dev_put(pcidev);
1161
1162 printk(KERN_INFO DRV_PFX "detected Type%d model\n",
1163 model == SONYPI_DEVICE_TYPE1 ? 1 :
1164 model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
1165 return model;
1166}
1167
1168#define ITERATIONS_LONG 10000
1169#define ITERATIONS_SHORT 10
1170#define wait_on_command(command, iterations) { \
1171 unsigned int n = iterations; \
1172 while (--n && (command)) \
1173 udelay(1); \
1174 if (!n) \
1175 dprintk("command failed at %s : %s (line %d)\n", \
1176 __FILE__, __FUNCTION__, __LINE__); \
1177}
1178
1179static u8 sony_pic_call1(u8 dev)
1180{
1181 u8 v1, v2;
1182
1183 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1184 ITERATIONS_LONG);
1185 outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1186 v1 = inb_p(spic_dev.cur_ioport->io.minimum + 4);
1187 v2 = inb_p(spic_dev.cur_ioport->io.minimum);
1188 dprintk("sony_pic_call1: 0x%.4x\n", (v2 << 8) | v1);
1189 return v2;
1190}
1191
1192static u8 sony_pic_call2(u8 dev, u8 fn)
1193{
1194 u8 v1;
1195
1196 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1197 ITERATIONS_LONG);
1198 outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1199 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1200 ITERATIONS_LONG);
1201 outb(fn, spic_dev.cur_ioport->io.minimum);
1202 v1 = inb_p(spic_dev.cur_ioport->io.minimum);
1203 dprintk("sony_pic_call2: 0x%.4x\n", v1);
1204 return v1;
1205}
1206
malattia@linux.it49a11de2007-04-09 19:28:56 +02001207static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
1208{
1209 u8 v1;
1210
1211 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
1212 outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1213 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
1214 outb(fn, spic_dev.cur_ioport->io.minimum);
1215 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
1216 outb(v, spic_dev.cur_ioport->io.minimum);
1217 v1 = inb_p(spic_dev.cur_ioport->io.minimum);
1218 dprintk("sony_pic_call3: 0x%.4x\n", v1);
1219 return v1;
1220}
1221
1222/* camera tests and poweron/poweroff */
1223#define SONYPI_CAMERA_PICTURE 5
malattia@linux.it49a11de2007-04-09 19:28:56 +02001224#define SONYPI_CAMERA_CONTROL 0x10
malattia@linux.ite3646322007-04-28 23:34:22 +09001225
1226#define SONYPI_CAMERA_BRIGHTNESS 0
1227#define SONYPI_CAMERA_CONTRAST 1
1228#define SONYPI_CAMERA_HUE 2
1229#define SONYPI_CAMERA_COLOR 3
1230#define SONYPI_CAMERA_SHARPNESS 4
1231
1232#define SONYPI_CAMERA_EXPOSURE_MASK 0xC
1233#define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
1234#define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
1235#define SONYPI_CAMERA_MUTE_MASK 0x40
1236
1237/* the rest don't need a loop until not 0xff */
1238#define SONYPI_CAMERA_AGC 6
1239#define SONYPI_CAMERA_AGC_MASK 0x30
1240#define SONYPI_CAMERA_SHUTTER_MASK 0x7
1241
1242#define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
1243#define SONYPI_CAMERA_CONTROL 0x10
1244
1245#define SONYPI_CAMERA_STATUS 7
1246#define SONYPI_CAMERA_STATUS_READY 0x2
1247#define SONYPI_CAMERA_STATUS_POSITION 0x4
1248
1249#define SONYPI_DIRECTION_BACKWARDS 0x4
1250
1251#define SONYPI_CAMERA_REVISION 8
1252#define SONYPI_CAMERA_ROMVERSION 9
malattia@linux.it49a11de2007-04-09 19:28:56 +02001253
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001254static int __sony_pic_camera_ready(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001255{
1256 u8 v;
1257
1258 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
1259 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
1260}
1261
malattia@linux.ite3646322007-04-28 23:34:22 +09001262static int __sony_pic_camera_off(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001263{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001264 if (!camera) {
1265 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1266 return -ENODEV;
1267 }
1268
malattia@linux.it49a11de2007-04-09 19:28:56 +02001269 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
1270 SONYPI_CAMERA_MUTE_MASK),
1271 ITERATIONS_SHORT);
1272
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001273 if (spic_dev.camera_power) {
1274 sony_pic_call2(0x91, 0);
1275 spic_dev.camera_power = 0;
1276 }
1277 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001278}
1279
malattia@linux.ite3646322007-04-28 23:34:22 +09001280static int __sony_pic_camera_on(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001281{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001282 int i, j, x;
1283
1284 if (!camera) {
1285 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1286 return -ENODEV;
1287 }
malattia@linux.it49a11de2007-04-09 19:28:56 +02001288
1289 if (spic_dev.camera_power)
malattia@linux.ite3646322007-04-28 23:34:22 +09001290 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001291
1292 for (j = 5; j > 0; j--) {
1293
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001294 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001295 msleep(10);
1296 sony_pic_call1(0x93);
1297
1298 for (i = 400; i > 0; i--) {
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001299 if (__sony_pic_camera_ready())
malattia@linux.it49a11de2007-04-09 19:28:56 +02001300 break;
1301 msleep(10);
1302 }
1303 if (i)
1304 break;
1305 }
1306
1307 if (j == 0) {
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001308 printk(KERN_WARNING DRV_PFX "failed to power on camera\n");
malattia@linux.ite3646322007-04-28 23:34:22 +09001309 return -ENODEV;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001310 }
1311
1312 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
1313 0x5a),
1314 ITERATIONS_SHORT);
1315
1316 spic_dev.camera_power = 1;
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001317 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001318}
1319
1320static ssize_t sony_pic_camerapower_store(struct device *dev,
1321 struct device_attribute *attr,
1322 const char *buffer, size_t count)
1323{
1324 unsigned long value;
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001325 int result;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001326 if (count > 31)
1327 return -EINVAL;
1328
1329 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.ite3646322007-04-28 23:34:22 +09001330
1331 mutex_lock(&spic_dev.lock);
malattia@linux.it49a11de2007-04-09 19:28:56 +02001332 if (value)
malattia@linux.ite3646322007-04-28 23:34:22 +09001333 result = __sony_pic_camera_on();
malattia@linux.it49a11de2007-04-09 19:28:56 +02001334 else
malattia@linux.ite3646322007-04-28 23:34:22 +09001335 result = __sony_pic_camera_off();
1336 mutex_unlock(&spic_dev.lock);
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001337
1338 if (result)
1339 return result;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001340
1341 return count;
1342}
1343
1344static ssize_t sony_pic_camerapower_show(struct device *dev,
1345 struct device_attribute *attr, char *buffer)
1346{
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001347 ssize_t count;
1348 mutex_lock(&spic_dev.lock);
1349 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.camera_power);
1350 mutex_unlock(&spic_dev.lock);
1351 return count;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001352}
1353
malattia@linux.ite3646322007-04-28 23:34:22 +09001354/* External camera command (exported to the motion eye v4l driver) */
1355int sony_pic_camera_command(int command, u8 value)
1356{
1357 if (!camera)
1358 return -EIO;
1359
1360 mutex_lock(&spic_dev.lock);
1361
1362 switch (command) {
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001363 case SONY_PIC_COMMAND_SETCAMERA:
malattia@linux.ite3646322007-04-28 23:34:22 +09001364 if (value)
1365 __sony_pic_camera_on();
1366 else
1367 __sony_pic_camera_off();
1368 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001369 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09001370 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
1371 ITERATIONS_SHORT);
1372 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001373 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
malattia@linux.ite3646322007-04-28 23:34:22 +09001374 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
1375 ITERATIONS_SHORT);
1376 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001377 case SONY_PIC_COMMAND_SETCAMERAHUE:
malattia@linux.ite3646322007-04-28 23:34:22 +09001378 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
1379 ITERATIONS_SHORT);
1380 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001381 case SONY_PIC_COMMAND_SETCAMERACOLOR:
malattia@linux.ite3646322007-04-28 23:34:22 +09001382 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
1383 ITERATIONS_SHORT);
1384 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001385 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09001386 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
1387 ITERATIONS_SHORT);
1388 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001389 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
malattia@linux.ite3646322007-04-28 23:34:22 +09001390 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
1391 ITERATIONS_SHORT);
1392 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001393 case SONY_PIC_COMMAND_SETCAMERAAGC:
malattia@linux.ite3646322007-04-28 23:34:22 +09001394 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
1395 ITERATIONS_SHORT);
1396 break;
1397 default:
1398 printk(KERN_ERR DRV_PFX "sony_pic_camera_command invalid: %d\n",
1399 command);
1400 break;
1401 }
1402 mutex_unlock(&spic_dev.lock);
1403 return 0;
1404}
1405EXPORT_SYMBOL(sony_pic_camera_command);
1406
malattia@linux.it9476cdf2007-04-28 23:21:42 +09001407/* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
1408static void sony_pic_set_wwanpower(u8 state)
1409{
1410 state = !!state;
1411 mutex_lock(&spic_dev.lock);
1412 if (spic_dev.wwan_power == state) {
1413 mutex_unlock(&spic_dev.lock);
1414 return;
1415 }
1416 sony_pic_call2(0xB0, state);
1417 spic_dev.wwan_power = state;
1418 mutex_unlock(&spic_dev.lock);
1419}
1420
1421static ssize_t sony_pic_wwanpower_store(struct device *dev,
1422 struct device_attribute *attr,
1423 const char *buffer, size_t count)
1424{
1425 unsigned long value;
1426 if (count > 31)
1427 return -EINVAL;
1428
1429 value = simple_strtoul(buffer, NULL, 10);
1430 sony_pic_set_wwanpower(value);
1431
1432 return count;
1433}
1434
1435static ssize_t sony_pic_wwanpower_show(struct device *dev,
1436 struct device_attribute *attr, char *buffer)
1437{
1438 ssize_t count;
1439 mutex_lock(&spic_dev.lock);
1440 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
1441 mutex_unlock(&spic_dev.lock);
1442 return count;
1443}
1444
malattia@linux.it49a11de2007-04-09 19:28:56 +02001445/* bluetooth subsystem power state */
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001446static void __sony_pic_set_bluetoothpower(u8 state)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001447{
1448 state = !!state;
1449 if (spic_dev.bluetooth_power == state)
1450 return;
1451 sony_pic_call2(0x96, state);
1452 sony_pic_call1(0x82);
1453 spic_dev.bluetooth_power = state;
1454}
1455
1456static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
1457 struct device_attribute *attr,
1458 const char *buffer, size_t count)
1459{
1460 unsigned long value;
1461 if (count > 31)
1462 return -EINVAL;
1463
1464 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001465 mutex_lock(&spic_dev.lock);
1466 __sony_pic_set_bluetoothpower(value);
1467 mutex_unlock(&spic_dev.lock);
malattia@linux.it49a11de2007-04-09 19:28:56 +02001468
1469 return count;
1470}
1471
1472static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
1473 struct device_attribute *attr, char *buffer)
1474{
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001475 ssize_t count = 0;
1476 mutex_lock(&spic_dev.lock);
1477 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
1478 mutex_unlock(&spic_dev.lock);
1479 return count;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001480}
1481
1482/* fan speed */
1483/* FAN0 information (reverse engineered from ACPI tables) */
1484#define SONY_PIC_FAN0_STATUS 0x93
malattia@linux.it7b153f32007-04-09 19:31:25 +02001485static int sony_pic_set_fanspeed(unsigned long value)
1486{
1487 return ec_write(SONY_PIC_FAN0_STATUS, value);
1488}
1489
1490static int sony_pic_get_fanspeed(u8 *value)
1491{
1492 return ec_read(SONY_PIC_FAN0_STATUS, value);
1493}
1494
malattia@linux.it49a11de2007-04-09 19:28:56 +02001495static ssize_t sony_pic_fanspeed_store(struct device *dev,
1496 struct device_attribute *attr,
1497 const char *buffer, size_t count)
1498{
1499 unsigned long value;
1500 if (count > 31)
1501 return -EINVAL;
1502
1503 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it7b153f32007-04-09 19:31:25 +02001504 if (sony_pic_set_fanspeed(value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02001505 return -EIO;
1506
1507 return count;
1508}
1509
1510static ssize_t sony_pic_fanspeed_show(struct device *dev,
1511 struct device_attribute *attr, char *buffer)
1512{
1513 u8 value = 0;
malattia@linux.it7b153f32007-04-09 19:31:25 +02001514 if (sony_pic_get_fanspeed(&value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02001515 return -EIO;
1516
1517 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
1518}
1519
1520#define SPIC_ATTR(_name, _mode) \
1521struct device_attribute spic_attr_##_name = __ATTR(_name, \
1522 _mode, sony_pic_## _name ##_show, \
1523 sony_pic_## _name ##_store)
1524
1525static SPIC_ATTR(camerapower, 0644);
1526static SPIC_ATTR(bluetoothpower, 0644);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09001527static SPIC_ATTR(wwanpower, 0644);
malattia@linux.it49a11de2007-04-09 19:28:56 +02001528static SPIC_ATTR(fanspeed, 0644);
1529
1530static struct attribute *spic_attributes[] = {
1531 &spic_attr_camerapower.attr,
1532 &spic_attr_bluetoothpower.attr,
malattia@linux.it9476cdf2007-04-28 23:21:42 +09001533 &spic_attr_wwanpower.attr,
malattia@linux.it49a11de2007-04-09 19:28:56 +02001534 &spic_attr_fanspeed.attr,
1535 NULL
1536};
1537
1538static struct attribute_group spic_attribute_group = {
1539 .attrs = spic_attributes
1540};
1541
malattia@linux.it7b153f32007-04-09 19:31:25 +02001542/******** SONYPI compatibility **********/
1543#ifdef CONFIG_SONY_LAPTOP_OLD
1544
1545/* battery / brightness / temperature addresses */
1546#define SONYPI_BAT_FLAGS 0x81
1547#define SONYPI_LCD_LIGHT 0x96
1548#define SONYPI_BAT1_PCTRM 0xa0
1549#define SONYPI_BAT1_LEFT 0xa2
1550#define SONYPI_BAT1_MAXRT 0xa4
1551#define SONYPI_BAT2_PCTRM 0xa8
1552#define SONYPI_BAT2_LEFT 0xaa
1553#define SONYPI_BAT2_MAXRT 0xac
1554#define SONYPI_BAT1_MAXTK 0xb0
1555#define SONYPI_BAT1_FULL 0xb2
1556#define SONYPI_BAT2_MAXTK 0xb8
1557#define SONYPI_BAT2_FULL 0xba
1558#define SONYPI_TEMP_STATUS 0xC1
1559
1560struct sonypi_compat_s {
1561 struct fasync_struct *fifo_async;
1562 struct kfifo *fifo;
1563 spinlock_t fifo_lock;
1564 wait_queue_head_t fifo_proc_list;
1565 atomic_t open_count;
1566};
1567static struct sonypi_compat_s sonypi_compat = {
1568 .open_count = ATOMIC_INIT(0),
1569};
1570
1571static int sonypi_misc_fasync(int fd, struct file *filp, int on)
1572{
1573 int retval;
1574
1575 retval = fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
1576 if (retval < 0)
1577 return retval;
1578 return 0;
1579}
1580
1581static int sonypi_misc_release(struct inode *inode, struct file *file)
1582{
1583 sonypi_misc_fasync(-1, file, 0);
1584 atomic_dec(&sonypi_compat.open_count);
1585 return 0;
1586}
1587
1588static int sonypi_misc_open(struct inode *inode, struct file *file)
1589{
1590 /* Flush input queue on first open */
1591 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
1592 kfifo_reset(sonypi_compat.fifo);
1593 return 0;
1594}
1595
1596static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
1597 size_t count, loff_t *pos)
1598{
1599 ssize_t ret;
1600 unsigned char c;
1601
1602 if ((kfifo_len(sonypi_compat.fifo) == 0) &&
1603 (file->f_flags & O_NONBLOCK))
1604 return -EAGAIN;
1605
1606 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
1607 kfifo_len(sonypi_compat.fifo) != 0);
1608 if (ret)
1609 return ret;
1610
1611 while (ret < count &&
1612 (kfifo_get(sonypi_compat.fifo, &c, sizeof(c)) == sizeof(c))) {
1613 if (put_user(c, buf++))
1614 return -EFAULT;
1615 ret++;
1616 }
1617
1618 if (ret > 0) {
1619 struct inode *inode = file->f_path.dentry->d_inode;
1620 inode->i_atime = current_fs_time(inode->i_sb);
1621 }
1622
1623 return ret;
1624}
1625
1626static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
1627{
1628 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
1629 if (kfifo_len(sonypi_compat.fifo))
1630 return POLLIN | POLLRDNORM;
1631 return 0;
1632}
1633
1634static int ec_read16(u8 addr, u16 *value)
1635{
1636 u8 val_lb, val_hb;
1637 if (ec_read(addr, &val_lb))
1638 return -1;
1639 if (ec_read(addr + 1, &val_hb))
1640 return -1;
1641 *value = val_lb | (val_hb << 8);
1642 return 0;
1643}
1644
1645static int sonypi_misc_ioctl(struct inode *ip, struct file *fp,
1646 unsigned int cmd, unsigned long arg)
1647{
1648 int ret = 0;
1649 void __user *argp = (void __user *)arg;
1650 u8 val8;
1651 u16 val16;
1652 int value;
1653
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001654 mutex_lock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02001655 switch (cmd) {
1656 case SONYPI_IOCGBRT:
1657 if (sony_backlight_device == NULL) {
1658 ret = -EIO;
1659 break;
1660 }
1661 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
1662 ret = -EIO;
1663 break;
1664 }
1665 val8 = ((value & 0xff) - 1) << 5;
1666 if (copy_to_user(argp, &val8, sizeof(val8)))
1667 ret = -EFAULT;
1668 break;
1669 case SONYPI_IOCSBRT:
1670 if (sony_backlight_device == NULL) {
1671 ret = -EIO;
1672 break;
1673 }
1674 if (copy_from_user(&val8, argp, sizeof(val8))) {
1675 ret = -EFAULT;
1676 break;
1677 }
1678 if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
1679 (val8 >> 5) + 1, NULL)) {
1680 ret = -EIO;
1681 break;
1682 }
1683 /* sync the backlight device status */
1684 sony_backlight_device->props.brightness =
1685 sony_backlight_get_brightness(sony_backlight_device);
1686 break;
1687 case SONYPI_IOCGBAT1CAP:
1688 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
1689 ret = -EIO;
1690 break;
1691 }
1692 if (copy_to_user(argp, &val16, sizeof(val16)))
1693 ret = -EFAULT;
1694 break;
1695 case SONYPI_IOCGBAT1REM:
1696 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
1697 ret = -EIO;
1698 break;
1699 }
1700 if (copy_to_user(argp, &val16, sizeof(val16)))
1701 ret = -EFAULT;
1702 break;
1703 case SONYPI_IOCGBAT2CAP:
1704 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
1705 ret = -EIO;
1706 break;
1707 }
1708 if (copy_to_user(argp, &val16, sizeof(val16)))
1709 ret = -EFAULT;
1710 break;
1711 case SONYPI_IOCGBAT2REM:
1712 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
1713 ret = -EIO;
1714 break;
1715 }
1716 if (copy_to_user(argp, &val16, sizeof(val16)))
1717 ret = -EFAULT;
1718 break;
1719 case SONYPI_IOCGBATFLAGS:
1720 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
1721 ret = -EIO;
1722 break;
1723 }
1724 val8 &= 0x07;
1725 if (copy_to_user(argp, &val8, sizeof(val8)))
1726 ret = -EFAULT;
1727 break;
1728 case SONYPI_IOCGBLUE:
1729 val8 = spic_dev.bluetooth_power;
1730 if (copy_to_user(argp, &val8, sizeof(val8)))
1731 ret = -EFAULT;
1732 break;
1733 case SONYPI_IOCSBLUE:
1734 if (copy_from_user(&val8, argp, sizeof(val8))) {
1735 ret = -EFAULT;
1736 break;
1737 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001738 __sony_pic_set_bluetoothpower(val8);
malattia@linux.it7b153f32007-04-09 19:31:25 +02001739 break;
1740 /* FAN Controls */
1741 case SONYPI_IOCGFAN:
1742 if (sony_pic_get_fanspeed(&val8)) {
1743 ret = -EIO;
1744 break;
1745 }
1746 if (copy_to_user(argp, &val8, sizeof(val8)))
1747 ret = -EFAULT;
1748 break;
1749 case SONYPI_IOCSFAN:
1750 if (copy_from_user(&val8, argp, sizeof(val8))) {
1751 ret = -EFAULT;
1752 break;
1753 }
1754 if (sony_pic_set_fanspeed(val8))
1755 ret = -EIO;
1756 break;
1757 /* GET Temperature (useful under APM) */
1758 case SONYPI_IOCGTEMP:
1759 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
1760 ret = -EIO;
1761 break;
1762 }
1763 if (copy_to_user(argp, &val8, sizeof(val8)))
1764 ret = -EFAULT;
1765 break;
1766 default:
1767 ret = -EINVAL;
1768 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001769 mutex_unlock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02001770 return ret;
1771}
1772
1773static const struct file_operations sonypi_misc_fops = {
1774 .owner = THIS_MODULE,
1775 .read = sonypi_misc_read,
1776 .poll = sonypi_misc_poll,
1777 .open = sonypi_misc_open,
1778 .release = sonypi_misc_release,
1779 .fasync = sonypi_misc_fasync,
1780 .ioctl = sonypi_misc_ioctl,
1781};
1782
1783static struct miscdevice sonypi_misc_device = {
1784 .minor = MISC_DYNAMIC_MINOR,
1785 .name = "sonypi",
1786 .fops = &sonypi_misc_fops,
1787};
1788
1789static void sonypi_compat_report_event(u8 event)
1790{
1791 kfifo_put(sonypi_compat.fifo, (unsigned char *)&event, sizeof(event));
1792 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
1793 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
1794}
1795
1796static int sonypi_compat_init(void)
1797{
1798 int error;
1799
1800 spin_lock_init(&sonypi_compat.fifo_lock);
1801 sonypi_compat.fifo = kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
1802 &sonypi_compat.fifo_lock);
1803 if (IS_ERR(sonypi_compat.fifo)) {
1804 printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
1805 return PTR_ERR(sonypi_compat.fifo);
1806 }
1807
1808 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
malattia@linux.it7b153f32007-04-09 19:31:25 +02001809
1810 if (minor != -1)
1811 sonypi_misc_device.minor = minor;
1812 error = misc_register(&sonypi_misc_device);
1813 if (error) {
1814 printk(KERN_ERR DRV_PFX "misc_register failed\n");
1815 goto err_free_kfifo;
1816 }
1817 if (minor == -1)
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001818 printk(KERN_INFO DRV_PFX "device allocated minor is %d\n",
malattia@linux.it7b153f32007-04-09 19:31:25 +02001819 sonypi_misc_device.minor);
1820
1821 return 0;
1822
1823err_free_kfifo:
1824 kfifo_free(sonypi_compat.fifo);
1825 return error;
1826}
1827
1828static void sonypi_compat_exit(void)
1829{
1830 misc_deregister(&sonypi_misc_device);
1831 kfifo_free(sonypi_compat.fifo);
1832}
1833#else
1834static int sonypi_compat_init(void) { return 0; }
1835static void sonypi_compat_exit(void) { }
1836static void sonypi_compat_report_event(u8 event) { }
1837#endif /* CONFIG_SONY_LAPTOP_OLD */
1838
malattia@linux.it1549ee62007-04-09 10:19:08 +02001839/*
malattia@linux.it33a04452007-04-09 19:26:03 +02001840 * ACPI callbacks
malattia@linux.it1549ee62007-04-09 10:19:08 +02001841 */
malattia@linux.it33a04452007-04-09 19:26:03 +02001842static acpi_status
1843sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
1844{
1845 u32 i;
1846 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
1847
1848 switch (resource->type) {
1849 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
1850 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
1851 return AE_OK;
1852
1853 case ACPI_RESOURCE_TYPE_IRQ:
1854 {
1855 struct acpi_resource_irq *p = &resource->data.irq;
1856 struct sony_pic_irq *interrupt = NULL;
1857 if (!p || !p->interrupt_count) {
1858 /*
1859 * IRQ descriptors may have no IRQ# bits set,
1860 * particularly those those w/ _STA disabled
1861 */
1862 dprintk("Blank IRQ resource\n");
1863 return AE_OK;
1864 }
1865 for (i = 0; i < p->interrupt_count; i++) {
1866 if (!p->interrupts[i]) {
1867 printk(KERN_WARNING DRV_PFX
1868 "Invalid IRQ %d\n",
1869 p->interrupts[i]);
1870 continue;
1871 }
1872 interrupt = kzalloc(sizeof(*interrupt),
1873 GFP_KERNEL);
1874 if (!interrupt)
1875 return AE_ERROR;
1876
malattia@linux.it1a3e3232007-04-28 23:34:10 +09001877 list_add_tail(&interrupt->list, &dev->interrupts);
malattia@linux.it33a04452007-04-09 19:26:03 +02001878 interrupt->irq.triggering = p->triggering;
1879 interrupt->irq.polarity = p->polarity;
1880 interrupt->irq.sharable = p->sharable;
1881 interrupt->irq.interrupt_count = 1;
1882 interrupt->irq.interrupts[0] = p->interrupts[i];
1883 }
1884 return AE_OK;
1885 }
1886 case ACPI_RESOURCE_TYPE_IO:
1887 {
1888 struct acpi_resource_io *io = &resource->data.io;
1889 struct sony_pic_ioport *ioport = NULL;
1890 if (!io) {
1891 dprintk("Blank IO resource\n");
1892 return AE_OK;
1893 }
1894
1895 ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
1896 if (!ioport)
1897 return AE_ERROR;
1898
malattia@linux.it1a3e3232007-04-28 23:34:10 +09001899 list_add_tail(&ioport->list, &dev->ioports);
malattia@linux.it33a04452007-04-09 19:26:03 +02001900 memcpy(&ioport->io, io, sizeof(*io));
1901 return AE_OK;
1902 }
1903 default:
1904 dprintk("Resource %d isn't an IRQ nor an IO port\n",
1905 resource->type);
1906
1907 case ACPI_RESOURCE_TYPE_END_TAG:
1908 return AE_OK;
1909 }
1910 return AE_CTRL_TERMINATE;
1911}
1912
1913static int sony_pic_possible_resources(struct acpi_device *device)
1914{
1915 int result = 0;
1916 acpi_status status = AE_OK;
1917
1918 if (!device)
1919 return -EINVAL;
1920
1921 /* get device status */
1922 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
1923 dprintk("Evaluating _STA\n");
1924 result = acpi_bus_get_status(device);
1925 if (result) {
1926 printk(KERN_WARNING DRV_PFX "Unable to read status\n");
1927 goto end;
1928 }
1929
1930 if (!device->status.enabled)
1931 dprintk("Device disabled\n");
1932 else
1933 dprintk("Device enabled\n");
1934
1935 /*
1936 * Query and parse 'method'
1937 */
1938 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
1939 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
1940 sony_pic_read_possible_resource, &spic_dev);
1941 if (ACPI_FAILURE(status)) {
1942 printk(KERN_WARNING DRV_PFX
1943 "Failure evaluating %s\n",
1944 METHOD_NAME__PRS);
1945 result = -ENODEV;
1946 }
1947end:
1948 return result;
1949}
1950
1951/*
1952 * Disable the spic device by calling its _DIS method
1953 */
1954static int sony_pic_disable(struct acpi_device *device)
1955{
1956 if (ACPI_FAILURE(acpi_evaluate_object(device->handle, "_DIS", 0, NULL)))
1957 return -ENXIO;
1958
1959 dprintk("Device disabled\n");
1960 return 0;
1961}
1962
1963
1964/*
1965 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
1966 *
1967 * Call _SRS to set current resources
1968 */
1969static int sony_pic_enable(struct acpi_device *device,
1970 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
1971{
1972 acpi_status status;
1973 int result = 0;
1974 struct {
1975 struct acpi_resource io_res;
1976 struct acpi_resource irq_res;
1977 struct acpi_resource end;
1978 } *resource;
1979 struct acpi_buffer buffer = { 0, NULL };
1980
1981 if (!ioport || !irq)
1982 return -EINVAL;
1983
1984 /* init acpi_buffer */
1985 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
1986 if (!resource)
1987 return -ENOMEM;
1988
1989 buffer.length = sizeof(*resource) + 1;
1990 buffer.pointer = resource;
1991
1992 /* setup io resource */
1993 resource->io_res.type = ACPI_RESOURCE_TYPE_IO;
1994 resource->io_res.length = sizeof(struct acpi_resource);
1995 memcpy(&resource->io_res.data.io, &ioport->io,
1996 sizeof(struct acpi_resource_io));
1997
1998 /* setup irq resource */
1999 resource->irq_res.type = ACPI_RESOURCE_TYPE_IRQ;
2000 resource->irq_res.length = sizeof(struct acpi_resource);
2001 memcpy(&resource->irq_res.data.irq, &irq->irq,
2002 sizeof(struct acpi_resource_irq));
2003 /* we requested a shared irq */
2004 resource->irq_res.data.irq.sharable = ACPI_SHARED;
2005
2006 resource->end.type = ACPI_RESOURCE_TYPE_END_TAG;
2007
2008 /* Attempt to set the resource */
2009 dprintk("Evaluating _SRS\n");
2010 status = acpi_set_current_resources(device->handle, &buffer);
2011
2012 /* check for total failure */
2013 if (ACPI_FAILURE(status)) {
2014 printk(KERN_ERR DRV_PFX "Error evaluating _SRS");
2015 result = -ENODEV;
2016 goto end;
2017 }
2018
2019 /* Necessary device initializations calls (from sonypi) */
2020 sony_pic_call1(0x82);
2021 sony_pic_call2(0x81, 0xff);
2022 sony_pic_call1(compat ? 0x92 : 0x82);
2023
2024end:
2025 kfree(resource);
2026 return result;
2027}
2028
2029/*****************
2030 *
2031 * ISR: some event is available
2032 *
2033 *****************/
2034static irqreturn_t sony_pic_irq(int irq, void *dev_id)
2035{
2036 int i, j;
2037 u32 port_val = 0;
2038 u8 ev = 0;
2039 u8 data_mask = 0;
2040 u8 device_event = 0;
2041
2042 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
2043
2044 acpi_os_read_port(dev->cur_ioport->io.minimum, &port_val,
2045 dev->cur_ioport->io.address_length);
2046 ev = port_val & SONY_PIC_EV_MASK;
2047 data_mask = 0xff & (port_val >> (dev->cur_ioport->io.address_length - 8));
2048
2049 dprintk("event (0x%.8x [%.2x] [%.2x]) at port 0x%.4x\n",
2050 port_val, ev, data_mask, dev->cur_ioport->io.minimum);
2051
2052 if (ev == 0x00 || ev == 0xff)
2053 return IRQ_HANDLED;
2054
2055 for (i = 0; sony_pic_eventtypes[i].model; i++) {
2056
2057 if (spic_dev.model != sony_pic_eventtypes[i].model)
2058 continue;
2059
2060 if ((data_mask & sony_pic_eventtypes[i].data) !=
2061 sony_pic_eventtypes[i].data)
2062 continue;
2063
2064 if (!(mask & sony_pic_eventtypes[i].mask))
2065 continue;
2066
2067 for (j = 0; sony_pic_eventtypes[i].events[j].event; j++) {
2068 if (ev == sony_pic_eventtypes[i].events[j].data) {
2069 device_event =
2070 sony_pic_eventtypes[i].events[j].event;
2071 goto found;
2072 }
2073 }
2074 }
2075 return IRQ_HANDLED;
2076
2077found:
malattia@linux.it1549ee62007-04-09 10:19:08 +02002078 sony_laptop_report_input_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02002079 acpi_bus_generate_event(spic_dev.acpi_dev, 1, device_event);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002080 sonypi_compat_report_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02002081
2082 return IRQ_HANDLED;
2083}
2084
2085/*****************
2086 *
2087 * ACPI driver
2088 *
2089 *****************/
2090static int sony_pic_remove(struct acpi_device *device, int type)
2091{
2092 struct sony_pic_ioport *io, *tmp_io;
2093 struct sony_pic_irq *irq, *tmp_irq;
2094
malattia@linux.it7b153f32007-04-09 19:31:25 +02002095 sonypi_compat_exit();
2096
malattia@linux.it33a04452007-04-09 19:26:03 +02002097 if (sony_pic_disable(device)) {
2098 printk(KERN_ERR DRV_PFX "Couldn't disable device.\n");
2099 return -ENXIO;
2100 }
2101
2102 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2103 release_region(spic_dev.cur_ioport->io.minimum,
2104 spic_dev.cur_ioport->io.address_length);
2105
malattia@linux.it1549ee62007-04-09 10:19:08 +02002106 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02002107
malattia@linux.it49a11de2007-04-09 19:28:56 +02002108 /* pf attrs */
2109 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2110 sony_pf_remove();
2111
malattia@linux.it33a04452007-04-09 19:26:03 +02002112 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2113 list_del(&io->list);
2114 kfree(io);
2115 }
2116 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2117 list_del(&irq->list);
2118 kfree(irq);
2119 }
2120 spic_dev.cur_ioport = NULL;
2121 spic_dev.cur_irq = NULL;
2122
malattia@linux.itf6119b02007-04-09 19:31:06 +02002123 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002124 return 0;
2125}
2126
2127static int sony_pic_add(struct acpi_device *device)
2128{
2129 int result;
2130 struct sony_pic_ioport *io, *tmp_io;
2131 struct sony_pic_irq *irq, *tmp_irq;
2132
malattia@linux.itf6119b02007-04-09 19:31:06 +02002133 printk(KERN_INFO DRV_PFX "%s v%s.\n",
2134 SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.it33a04452007-04-09 19:26:03 +02002135
2136 spic_dev.acpi_dev = device;
2137 strcpy(acpi_device_class(device), "sony/hotkey");
2138 spic_dev.model = sony_pic_detect_device_type();
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002139 mutex_init(&spic_dev.lock);
malattia@linux.it33a04452007-04-09 19:26:03 +02002140
2141 /* read _PRS resources */
2142 result = sony_pic_possible_resources(device);
2143 if (result) {
2144 printk(KERN_ERR DRV_PFX
2145 "Unabe to read possible resources.\n");
2146 goto err_free_resources;
2147 }
2148
2149 /* setup input devices and helper fifo */
malattia@linux.it1549ee62007-04-09 10:19:08 +02002150 result = sony_laptop_setup_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02002151 if (result) {
2152 printk(KERN_ERR DRV_PFX
2153 "Unabe to create input devices.\n");
2154 goto err_free_resources;
2155 }
2156
2157 /* request io port */
2158 list_for_each_entry(io, &spic_dev.ioports, list) {
2159 if (request_region(io->io.minimum, io->io.address_length,
2160 "Sony Programable I/O Device")) {
2161 dprintk("I/O port: 0x%.4x (0x%.4x) + 0x%.2x\n",
2162 io->io.minimum, io->io.maximum,
2163 io->io.address_length);
2164 spic_dev.cur_ioport = io;
2165 break;
2166 }
2167 }
2168 if (!spic_dev.cur_ioport) {
2169 printk(KERN_ERR DRV_PFX "Failed to request_region.\n");
2170 result = -ENODEV;
2171 goto err_remove_input;
2172 }
2173
2174 /* request IRQ */
2175 list_for_each_entry(irq, &spic_dev.interrupts, list) {
2176 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
2177 IRQF_SHARED, "sony-laptop", &spic_dev)) {
2178 dprintk("IRQ: %d - triggering: %d - "
2179 "polarity: %d - shr: %d\n",
2180 irq->irq.interrupts[0],
2181 irq->irq.triggering,
2182 irq->irq.polarity,
2183 irq->irq.sharable);
2184 spic_dev.cur_irq = irq;
2185 break;
2186 }
2187 }
2188 if (!spic_dev.cur_irq) {
2189 printk(KERN_ERR DRV_PFX "Failed to request_irq.\n");
2190 result = -ENODEV;
2191 goto err_release_region;
2192 }
2193
2194 /* set resource status _SRS */
2195 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2196 if (result) {
2197 printk(KERN_ERR DRV_PFX "Couldn't enable device.\n");
2198 goto err_free_irq;
2199 }
2200
malattia@linux.it49a11de2007-04-09 19:28:56 +02002201 spic_dev.bluetooth_power = -1;
2202 /* create device attributes */
2203 result = sony_pf_add();
2204 if (result)
2205 goto err_disable_device;
2206
2207 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2208 if (result)
2209 goto err_remove_pf;
2210
malattia@linux.it7b153f32007-04-09 19:31:25 +02002211 if (sonypi_compat_init())
2212 goto err_remove_pf;
2213
malattia@linux.it33a04452007-04-09 19:26:03 +02002214 return 0;
2215
malattia@linux.it49a11de2007-04-09 19:28:56 +02002216err_remove_pf:
2217 sony_pf_remove();
2218
2219err_disable_device:
2220 sony_pic_disable(device);
2221
malattia@linux.it33a04452007-04-09 19:26:03 +02002222err_free_irq:
2223 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2224
2225err_release_region:
2226 release_region(spic_dev.cur_ioport->io.minimum,
2227 spic_dev.cur_ioport->io.address_length);
2228
2229err_remove_input:
malattia@linux.it1549ee62007-04-09 10:19:08 +02002230 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02002231
2232err_free_resources:
2233 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2234 list_del(&io->list);
2235 kfree(io);
2236 }
2237 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2238 list_del(&irq->list);
2239 kfree(irq);
2240 }
2241 spic_dev.cur_ioport = NULL;
2242 spic_dev.cur_irq = NULL;
2243
2244 return result;
2245}
2246
2247static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
2248{
2249 if (sony_pic_disable(device))
2250 return -ENXIO;
2251 return 0;
2252}
2253
2254static int sony_pic_resume(struct acpi_device *device)
2255{
2256 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2257 return 0;
2258}
2259
2260static struct acpi_driver sony_pic_driver = {
2261 .name = SONY_PIC_DRIVER_NAME,
2262 .class = SONY_PIC_CLASS,
2263 .ids = SONY_PIC_HID,
2264 .owner = THIS_MODULE,
2265 .ops = {
2266 .add = sony_pic_add,
2267 .remove = sony_pic_remove,
2268 .suspend = sony_pic_suspend,
2269 .resume = sony_pic_resume,
2270 },
2271};
2272
2273static struct dmi_system_id __initdata sonypi_dmi_table[] = {
2274 {
2275 .ident = "Sony Vaio",
2276 .matches = {
2277 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2278 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
2279 },
2280 },
2281 {
2282 .ident = "Sony Vaio",
2283 .matches = {
2284 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2285 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
2286 },
2287 },
2288 { }
2289};
2290
malattia@linux.it59b19102007-04-09 10:19:04 +02002291static int __init sony_laptop_init(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01002292{
malattia@linux.it33a04452007-04-09 19:26:03 +02002293 int result;
2294
2295 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
2296 result = acpi_bus_register_driver(&sony_pic_driver);
2297 if (result) {
2298 printk(KERN_ERR DRV_PFX
2299 "Unable to register SPIC driver.");
2300 goto out;
2301 }
2302 }
2303
2304 result = acpi_bus_register_driver(&sony_nc_driver);
2305 if (result) {
2306 printk(KERN_ERR DRV_PFX "Unable to register SNC driver.");
2307 goto out_unregister_pic;
2308 }
2309
2310 return 0;
2311
2312out_unregister_pic:
2313 if (!no_spic)
2314 acpi_bus_unregister_driver(&sony_pic_driver);
2315out:
2316 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +01002317}
2318
malattia@linux.it59b19102007-04-09 10:19:04 +02002319static void __exit sony_laptop_exit(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01002320{
malattia@linux.it59b19102007-04-09 10:19:04 +02002321 acpi_bus_unregister_driver(&sony_nc_driver);
malattia@linux.it33a04452007-04-09 19:26:03 +02002322 if (!no_spic)
2323 acpi_bus_unregister_driver(&sony_pic_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +01002324}
2325
malattia@linux.it59b19102007-04-09 10:19:04 +02002326module_init(sony_laptop_init);
2327module_exit(sony_laptop_exit);