blob: 0f378fe44c1da3063b250f2e3be56677b23d485b [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>
Mattia Dongilia64e62a2007-05-01 11:19:53 +090066#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +020067#include <linux/poll.h>
68#include <linux/miscdevice.h>
69#endif
Stelian Pop7f09c432007-01-13 23:04:31 +010070
malattia@linux.it33a04452007-04-09 19:26:03 +020071#define DRV_PFX "sony-laptop: "
malattia@linux.itb9a218b2007-04-09 10:19:06 +020072#define dprintk(msg...) do { \
malattia@linux.itf6119b02007-04-09 19:31:06 +020073 if (debug) printk(KERN_WARNING DRV_PFX msg); \
malattia@linux.itb9a218b2007-04-09 10:19:06 +020074} while (0)
75
malattia@linux.it33a04452007-04-09 19:26:03 +020076#define SONY_LAPTOP_DRIVER_VERSION "0.5"
Alessandro Guido50f62af2007-01-13 23:04:34 +010077
malattia@linux.it33a04452007-04-09 19:26:03 +020078#define SONY_NC_CLASS "sony-nc"
79#define SONY_NC_HID "SNY5001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020080#define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
malattia@linux.it33a04452007-04-09 19:26:03 +020081
82#define SONY_PIC_CLASS "sony-pic"
83#define SONY_PIC_HID "SNY6001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020084#define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
Stelian Pop7f09c432007-01-13 23:04:31 +010085
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010086MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
malattia@linux.it33a04452007-04-09 19:26:03 +020087MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
Stelian Pop7f09c432007-01-13 23:04:31 +010088MODULE_LICENSE("GPL");
malattia@linux.it33a04452007-04-09 19:26:03 +020089MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
Stelian Pop7f09c432007-01-13 23:04:31 +010090
91static int debug;
92module_param(debug, int, 0);
93MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
Len Browna02d1c12007-02-07 15:34:02 -050094 "the development of this driver");
Stelian Pop7f09c432007-01-13 23:04:31 +010095
malattia@linux.it33a04452007-04-09 19:26:03 +020096static int no_spic; /* = 0 */
97module_param(no_spic, int, 0444);
98MODULE_PARM_DESC(no_spic,
99 "set this if you don't want to enable the SPIC device");
100
101static int compat; /* = 0 */
102module_param(compat, int, 0444);
103MODULE_PARM_DESC(compat,
malattia@linux.it7b153f32007-04-09 19:31:25 +0200104 "set this if you want to enable backward compatibility mode");
malattia@linux.it33a04452007-04-09 19:26:03 +0200105
106static unsigned long mask = 0xffffffff;
107module_param(mask, ulong, 0644);
108MODULE_PARM_DESC(mask,
109 "set this to the mask of event you want to enable (see doc)");
110
malattia@linux.it5f3d2892007-04-28 23:18:45 +0900111static int camera; /* = 0 */
112module_param(camera, int, 0444);
113MODULE_PARM_DESC(camera,
114 "set this to 1 to enable Motion Eye camera controls "
115 "(only use it if you have a C1VE or C1VN model)");
116
Mattia Dongilia64e62a2007-05-01 11:19:53 +0900117#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +0200118static int minor = -1;
119module_param(minor, int, 0);
120MODULE_PARM_DESC(minor,
121 "minor number of the misc device for the SPIC compatibility code, "
122 "default is -1 (automatic)");
123#endif
124
malattia@linux.it1549ee62007-04-09 10:19:08 +0200125/*********** Input Devices ***********/
126
127#define SONY_LAPTOP_BUF_SIZE 128
128struct sony_laptop_input_s {
129 atomic_t users;
130 struct input_dev *jog_dev;
131 struct input_dev *key_dev;
132 struct kfifo *fifo;
133 spinlock_t fifo_lock;
134 struct workqueue_struct *wq;
135};
136static struct sony_laptop_input_s sony_laptop_input = {
137 .users = ATOMIC_INIT(0),
138};
139
140struct sony_laptop_keypress {
141 struct input_dev *dev;
142 int key;
143};
144
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
Mattia Dongili044847e2007-07-16 02:34:33 +0900490SNC_HANDLE_NAMES(lidstate_get, "GLID");
491
492SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
493SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
494
495SNC_HANDLE_NAMES(gainbass_get, "GMGB");
496SNC_HANDLE_NAMES(gainbass_set, "CMGB");
497
malattia@linux.it59b19102007-04-09 10:19:04 +0200498SNC_HANDLE_NAMES(PID_get, "GPID");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100499
malattia@linux.it59b19102007-04-09 10:19:04 +0200500SNC_HANDLE_NAMES(CTR_get, "GCTR");
501SNC_HANDLE_NAMES(CTR_set, "SCTR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100502
malattia@linux.it59b19102007-04-09 10:19:04 +0200503SNC_HANDLE_NAMES(PCR_get, "GPCR");
504SNC_HANDLE_NAMES(PCR_set, "SPCR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100505
malattia@linux.it59b19102007-04-09 10:19:04 +0200506SNC_HANDLE_NAMES(CMI_get, "GCMI");
507SNC_HANDLE_NAMES(CMI_set, "SCMI");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100508
malattia@linux.it59b19102007-04-09 10:19:04 +0200509static struct sony_nc_value sony_nc_values[] = {
510 SNC_HANDLE(brightness_default, snc_brightness_def_get,
Mattia Dongili156c2212007-02-12 22:01:07 +0100511 snc_brightness_def_set, brightness_default_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200512 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
513 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
514 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100515 boolean_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200516 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100517 boolean_validate, 1),
Mattia Dongili044847e2007-07-16 02:34:33 +0900518 SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
519 boolean_validate, 0),
520 SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
521 boolean_validate, 0),
522 SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
523 boolean_validate, 0),
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100524 /* unknown methods */
malattia@linux.it59b19102007-04-09 10:19:04 +0200525 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
526 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
527 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
528 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
529 SNC_HANDLE_NULL
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100530};
531
malattia@linux.it59b19102007-04-09 10:19:04 +0200532static acpi_handle sony_nc_acpi_handle;
533static struct acpi_device *sony_nc_acpi_device = NULL;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100534
Mattia Dongilid78865c2007-02-07 20:01:56 +0100535/*
536 * acpi_evaluate_object wrappers
537 */
Stelian Pop7f09c432007-01-13 23:04:31 +0100538static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
539{
540 struct acpi_buffer output;
541 union acpi_object out_obj;
542 acpi_status status;
543
544 output.length = sizeof(out_obj);
545 output.pointer = &out_obj;
546
547 status = acpi_evaluate_object(handle, name, NULL, &output);
548 if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
549 *result = out_obj.integer.value;
550 return 0;
551 }
552
malattia@linux.itf6119b02007-04-09 19:31:06 +0200553 printk(KERN_WARNING DRV_PFX "acpi_callreadfunc failed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100554
555 return -1;
556}
557
558static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
559 int *result)
560{
561 struct acpi_object_list params;
562 union acpi_object in_obj;
563 struct acpi_buffer output;
564 union acpi_object out_obj;
565 acpi_status status;
566
567 params.count = 1;
568 params.pointer = &in_obj;
569 in_obj.type = ACPI_TYPE_INTEGER;
570 in_obj.integer.value = value;
571
572 output.length = sizeof(out_obj);
573 output.pointer = &out_obj;
574
575 status = acpi_evaluate_object(handle, name, &params, &output);
576 if (status == AE_OK) {
577 if (result != NULL) {
578 if (out_obj.type != ACPI_TYPE_INTEGER) {
malattia@linux.itf6119b02007-04-09 19:31:06 +0200579 printk(KERN_WARNING DRV_PFX "acpi_evaluate_object bad "
Stelian Pop7f09c432007-01-13 23:04:31 +0100580 "return type\n");
581 return -1;
582 }
583 *result = out_obj.integer.value;
584 }
585 return 0;
586 }
587
malattia@linux.itf6119b02007-04-09 19:31:06 +0200588 printk(KERN_WARNING DRV_PFX "acpi_evaluate_object failed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100589
590 return -1;
591}
592
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100593/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200594 * sony_nc_values input/output validate functions
Mattia Dongili156c2212007-02-12 22:01:07 +0100595 */
596
597/* brightness_default_validate:
598 *
599 * manipulate input output values to keep consistency with the
600 * backlight framework for which brightness values are 0-based.
601 */
602static int brightness_default_validate(const int direction, const int value)
603{
604 switch (direction) {
605 case SNC_VALIDATE_OUT:
606 return value - 1;
607 case SNC_VALIDATE_IN:
608 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
609 return value + 1;
610 }
611 return -EINVAL;
612}
613
614/* boolean_validate:
615 *
616 * on input validate boolean values 0/1, on output just pass the
617 * received value.
618 */
619static int boolean_validate(const int direction, const int value)
620{
621 if (direction == SNC_VALIDATE_IN) {
622 if (value != 0 && value != 1)
623 return -EINVAL;
624 }
625 return value;
626}
627
628/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200629 * Sysfs show/store common to all sony_nc_values
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100630 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200631static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
Len Browna02d1c12007-02-07 15:34:02 -0500632 char *buffer)
Stelian Pop7f09c432007-01-13 23:04:31 +0100633{
Stelian Pop7f09c432007-01-13 23:04:31 +0100634 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200635 struct sony_nc_value *item =
636 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100637
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100638 if (!*item->acpiget)
Stelian Pop7f09c432007-01-13 23:04:31 +0100639 return -EIO;
640
malattia@linux.it59b19102007-04-09 10:19:04 +0200641 if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100642 return -EIO;
643
Mattia Dongili156c2212007-02-12 22:01:07 +0100644 if (item->validate)
645 value = item->validate(SNC_VALIDATE_OUT, value);
646
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100647 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
Stelian Pop7f09c432007-01-13 23:04:31 +0100648}
649
malattia@linux.it59b19102007-04-09 10:19:04 +0200650static ssize_t sony_nc_sysfs_store(struct device *dev,
Len Browna02d1c12007-02-07 15:34:02 -0500651 struct device_attribute *attr,
652 const char *buffer, size_t count)
Stelian Pop7f09c432007-01-13 23:04:31 +0100653{
Stelian Pop7f09c432007-01-13 23:04:31 +0100654 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200655 struct sony_nc_value *item =
656 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100657
658 if (!item->acpiset)
659 return -EIO;
660
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100661 if (count > 31)
662 return -EINVAL;
663
664 value = simple_strtoul(buffer, NULL, 10);
Stelian Pop7f09c432007-01-13 23:04:31 +0100665
Mattia Dongili156c2212007-02-12 22:01:07 +0100666 if (item->validate)
667 value = item->validate(SNC_VALIDATE_IN, value);
668
669 if (value < 0)
670 return value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100671
malattia@linux.it59b19102007-04-09 10:19:04 +0200672 if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100673 return -EIO;
Andrew Morton3f4f4612007-01-13 23:04:32 +0100674 item->value = value;
675 item->valid = 1;
Stelian Pop7f09c432007-01-13 23:04:31 +0100676 return count;
677}
678
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100679
Mattia Dongilid78865c2007-02-07 20:01:56 +0100680/*
681 * Backlight device
682 */
683static int sony_backlight_update_status(struct backlight_device *bd)
Andrew Morton3f4f4612007-01-13 23:04:32 +0100684{
malattia@linux.it59b19102007-04-09 10:19:04 +0200685 return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
Richard Purdie321709c2007-02-10 15:04:08 +0000686 bd->props.brightness + 1, NULL);
Andrew Morton3f4f4612007-01-13 23:04:32 +0100687}
688
Mattia Dongilid78865c2007-02-07 20:01:56 +0100689static int sony_backlight_get_brightness(struct backlight_device *bd)
690{
691 int value;
692
malattia@linux.it59b19102007-04-09 10:19:04 +0200693 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
Mattia Dongilid78865c2007-02-07 20:01:56 +0100694 return 0;
695 /* brightness levels are 1-based, while backlight ones are 0-based */
696 return value - 1;
697}
698
699static struct backlight_device *sony_backlight_device;
Richard Purdie321709c2007-02-10 15:04:08 +0000700static struct backlight_ops sony_backlight_ops = {
Len Browna02d1c12007-02-07 15:34:02 -0500701 .update_status = sony_backlight_update_status,
702 .get_brightness = sony_backlight_get_brightness,
Mattia Dongilid78865c2007-02-07 20:01:56 +0100703};
704
705/*
706 * ACPI callbacks
707 */
Stelian Pop7f09c432007-01-13 23:04:31 +0100708static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
709{
malattia@linux.itb9a218b2007-04-09 10:19:06 +0200710 dprintk("sony_acpi_notify, event: %d\n", event);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200711 sony_laptop_report_input_event(event);
malattia@linux.it59b19102007-04-09 10:19:04 +0200712 acpi_bus_generate_event(sony_nc_acpi_device, 1, event);
Stelian Pop7f09c432007-01-13 23:04:31 +0100713}
714
715static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
716 void *context, void **return_value)
717{
718 struct acpi_namespace_node *node;
719 union acpi_operand_object *operand;
720
Len Browna02d1c12007-02-07 15:34:02 -0500721 node = (struct acpi_namespace_node *)handle;
722 operand = (union acpi_operand_object *)node->object;
Stelian Pop7f09c432007-01-13 23:04:31 +0100723
malattia@linux.itf6119b02007-04-09 19:31:06 +0200724 printk(KERN_WARNING DRV_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
Stelian Pop7f09c432007-01-13 23:04:31 +0100725 (u32) operand->method.param_count);
726
727 return AE_OK;
728}
729
Mattia Dongilid78865c2007-02-07 20:01:56 +0100730/*
731 * ACPI device
732 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200733static int sony_nc_resume(struct acpi_device *device)
Mattia Dongilid78865c2007-02-07 20:01:56 +0100734{
malattia@linux.it59b19102007-04-09 10:19:04 +0200735 struct sony_nc_value *item;
Mattia Dongilid78865c2007-02-07 20:01:56 +0100736
malattia@linux.it59b19102007-04-09 10:19:04 +0200737 for (item = sony_nc_values; item->name; item++) {
Mattia Dongilid78865c2007-02-07 20:01:56 +0100738 int ret;
739
740 if (!item->valid)
741 continue;
malattia@linux.it59b19102007-04-09 10:19:04 +0200742 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
Len Browna02d1c12007-02-07 15:34:02 -0500743 item->value, NULL);
Mattia Dongilid78865c2007-02-07 20:01:56 +0100744 if (ret < 0) {
745 printk("%s: %d\n", __FUNCTION__, ret);
746 break;
747 }
748 }
749 return 0;
750}
751
malattia@linux.it59b19102007-04-09 10:19:04 +0200752static int sony_nc_add(struct acpi_device *device)
Stelian Pop7f09c432007-01-13 23:04:31 +0100753{
754 acpi_status status;
Andrew Morton8607c672007-03-06 02:29:42 -0800755 int result = 0;
Alessandro Guido50f62af2007-01-13 23:04:34 +0100756 acpi_handle handle;
malattia@linux.it56b87562007-04-09 10:19:05 +0200757 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +0100758
malattia@linux.itf6119b02007-04-09 19:31:06 +0200759 printk(KERN_INFO DRV_PFX "%s v%s.\n",
760 SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
761
malattia@linux.it59b19102007-04-09 10:19:04 +0200762 sony_nc_acpi_device = device;
malattia@linux.it33a04452007-04-09 19:26:03 +0200763 strcpy(acpi_device_class(device), "sony/hotkey");
Stelian Popc5611622007-01-13 23:04:37 +0100764
malattia@linux.it59b19102007-04-09 10:19:04 +0200765 sony_nc_acpi_handle = device->handle;
Stelian Pop7f09c432007-01-13 23:04:31 +0100766
Stelian Pop7f09c432007-01-13 23:04:31 +0100767 if (debug) {
malattia@linux.it59b19102007-04-09 10:19:04 +0200768 status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_nc_acpi_handle,
Stelian Pop7f09c432007-01-13 23:04:31 +0100769 1, sony_walk_callback, NULL, NULL);
770 if (ACPI_FAILURE(status)) {
malattia@linux.itf6119b02007-04-09 19:31:06 +0200771 printk(KERN_WARNING DRV_PFX "unable to walk acpi resources\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100772 result = -ENODEV;
773 goto outwalk;
774 }
Stelian Popc5611622007-01-13 23:04:37 +0100775 }
Stelian Pop7f09c432007-01-13 23:04:31 +0100776
malattia@linux.it1549ee62007-04-09 10:19:08 +0200777 /* setup input devices and helper fifo */
778 result = sony_laptop_setup_input();
779 if (result) {
780 printk(KERN_ERR DRV_PFX
781 "Unabe to create input devices.\n");
782 goto outwalk;
783 }
784
malattia@linux.it59b19102007-04-09 10:19:04 +0200785 status = acpi_install_notify_handler(sony_nc_acpi_handle,
Stelian Popc5611622007-01-13 23:04:37 +0100786 ACPI_DEVICE_NOTIFY,
Len Browna02d1c12007-02-07 15:34:02 -0500787 sony_acpi_notify, NULL);
Stelian Popc5611622007-01-13 23:04:37 +0100788 if (ACPI_FAILURE(status)) {
malattia@linux.itf6119b02007-04-09 19:31:06 +0200789 printk(KERN_WARNING DRV_PFX "unable to install notify handler\n");
Stelian Popc5611622007-01-13 23:04:37 +0100790 result = -ENODEV;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200791 goto outinput;
Stelian Pop7f09c432007-01-13 23:04:31 +0100792 }
793
malattia@linux.it59b19102007-04-09 10:19:04 +0200794 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT", &handle))) {
Alessandro Guido50f62af2007-01-13 23:04:34 +0100795 sony_backlight_device = backlight_device_register("sony", NULL,
Len Browna02d1c12007-02-07 15:34:02 -0500796 NULL,
Richard Purdie321709c2007-02-10 15:04:08 +0000797 &sony_backlight_ops);
Mattia Dongili7df03b82007-01-13 23:04:41 +0100798
Len Browna02d1c12007-02-07 15:34:02 -0500799 if (IS_ERR(sony_backlight_device)) {
malattia@linux.itf6119b02007-04-09 19:31:06 +0200800 printk(KERN_WARNING DRV_PFX "unable to register backlight device\n");
Mattia Dongili7df03b82007-01-13 23:04:41 +0100801 sony_backlight_device = NULL;
Richard Purdie321709c2007-02-10 15:04:08 +0000802 } else {
803 sony_backlight_device->props.brightness =
Len Browna02d1c12007-02-07 15:34:02 -0500804 sony_backlight_get_brightness
805 (sony_backlight_device);
Richard Purdie321709c2007-02-10 15:04:08 +0000806 sony_backlight_device->props.max_brightness =
807 SONY_MAX_BRIGHTNESS - 1;
808 }
809
Alessandro Guido50f62af2007-01-13 23:04:34 +0100810 }
Stelian Pop7f09c432007-01-13 23:04:31 +0100811
malattia@linux.it49a11de2007-04-09 19:28:56 +0200812 result = sony_pf_add();
813 if (result)
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100814 goto outbacklight;
Stelian Pop7f09c432007-01-13 23:04:31 +0100815
malattia@linux.it56b87562007-04-09 10:19:05 +0200816 /* create sony_pf sysfs attributes related to the SNC device */
817 for (item = sony_nc_values; item->name; ++item) {
818
819 if (!debug && item->debug)
820 continue;
821
822 /* find the available acpiget as described in the DSDT */
823 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
824 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
825 *item->acpiget,
826 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +0200827 dprintk("Found %s getter: %s\n",
828 item->name, *item->acpiget);
malattia@linux.it56b87562007-04-09 10:19:05 +0200829 item->devattr.attr.mode |= S_IRUGO;
830 break;
831 }
832 }
833
834 /* find the available acpiset as described in the DSDT */
835 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
836 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
837 *item->acpiset,
838 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +0200839 dprintk("Found %s setter: %s\n",
840 item->name, *item->acpiset);
malattia@linux.it56b87562007-04-09 10:19:05 +0200841 item->devattr.attr.mode |= S_IWUSR;
842 break;
843 }
844 }
845
846 if (item->devattr.attr.mode != 0) {
847 result =
848 device_create_file(&sony_pf_device->dev,
849 &item->devattr);
850 if (result)
851 goto out_sysfs;
852 }
853 }
854
Stelian Pop7f09c432007-01-13 23:04:31 +0100855 return 0;
856
malattia@linux.it56b87562007-04-09 10:19:05 +0200857 out_sysfs:
858 for (item = sony_nc_values; item->name; ++item) {
859 device_remove_file(&sony_pf_device->dev, &item->devattr);
860 }
861 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +0200862
Len Browna02d1c12007-02-07 15:34:02 -0500863 outbacklight:
Mattia Dongili7df03b82007-01-13 23:04:41 +0100864 if (sony_backlight_device)
865 backlight_device_unregister(sony_backlight_device);
866
malattia@linux.it59b19102007-04-09 10:19:04 +0200867 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
Stelian Popc5611622007-01-13 23:04:37 +0100868 ACPI_DEVICE_NOTIFY,
869 sony_acpi_notify);
870 if (ACPI_FAILURE(status))
malattia@linux.itf6119b02007-04-09 19:31:06 +0200871 printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
malattia@linux.it1549ee62007-04-09 10:19:08 +0200872
873 outinput:
874 sony_laptop_remove_input();
875
Len Browna02d1c12007-02-07 15:34:02 -0500876 outwalk:
Stelian Pop7f09c432007-01-13 23:04:31 +0100877 return result;
878}
879
malattia@linux.it59b19102007-04-09 10:19:04 +0200880static int sony_nc_remove(struct acpi_device *device, int type)
Stelian Pop7f09c432007-01-13 23:04:31 +0100881{
882 acpi_status status;
malattia@linux.it56b87562007-04-09 10:19:05 +0200883 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +0100884
Alessandro Guido50f62af2007-01-13 23:04:34 +0100885 if (sony_backlight_device)
886 backlight_device_unregister(sony_backlight_device);
887
malattia@linux.it59b19102007-04-09 10:19:04 +0200888 sony_nc_acpi_device = NULL;
Stelian Popc5611622007-01-13 23:04:37 +0100889
malattia@linux.it59b19102007-04-09 10:19:04 +0200890 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
Stelian Popc5611622007-01-13 23:04:37 +0100891 ACPI_DEVICE_NOTIFY,
892 sony_acpi_notify);
893 if (ACPI_FAILURE(status))
malattia@linux.itf6119b02007-04-09 19:31:06 +0200894 printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100895
malattia@linux.it56b87562007-04-09 10:19:05 +0200896 for (item = sony_nc_values; item->name; ++item) {
897 device_remove_file(&sony_pf_device->dev, &item->devattr);
898 }
899
900 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +0200901 sony_laptop_remove_input();
malattia@linux.itf6119b02007-04-09 19:31:06 +0200902 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100903
904 return 0;
905}
906
malattia@linux.it59b19102007-04-09 10:19:04 +0200907static struct acpi_driver sony_nc_driver = {
908 .name = SONY_NC_DRIVER_NAME,
909 .class = SONY_NC_CLASS,
910 .ids = SONY_NC_HID,
malattia@linux.it33a04452007-04-09 19:26:03 +0200911 .owner = THIS_MODULE,
Len Browna02d1c12007-02-07 15:34:02 -0500912 .ops = {
malattia@linux.it59b19102007-04-09 10:19:04 +0200913 .add = sony_nc_add,
914 .remove = sony_nc_remove,
915 .resume = sony_nc_resume,
Len Browna02d1c12007-02-07 15:34:02 -0500916 },
Andrew Morton3f4f4612007-01-13 23:04:32 +0100917};
918
malattia@linux.it33a04452007-04-09 19:26:03 +0200919/*********** SPIC (SNY6001) Device ***********/
920
921#define SONYPI_DEVICE_TYPE1 0x00000001
922#define SONYPI_DEVICE_TYPE2 0x00000002
923#define SONYPI_DEVICE_TYPE3 0x00000004
924
malattia@linux.it33a04452007-04-09 19:26:03 +0200925#define SONY_PIC_EV_MASK 0xff
926
malattia@linux.it33a04452007-04-09 19:26:03 +0200927struct sony_pic_ioport {
928 struct acpi_resource_io io;
929 struct list_head list;
930};
931
932struct sony_pic_irq {
933 struct acpi_resource_irq irq;
934 struct list_head list;
935};
936
937struct sony_pic_dev {
938 int model;
malattia@linux.it49a11de2007-04-09 19:28:56 +0200939 u8 camera_power;
940 u8 bluetooth_power;
malattia@linux.it9476cdf2007-04-28 23:21:42 +0900941 u8 wwan_power;
malattia@linux.it33a04452007-04-09 19:26:03 +0200942 struct acpi_device *acpi_dev;
943 struct sony_pic_irq *cur_irq;
944 struct sony_pic_ioport *cur_ioport;
945 struct list_head interrupts;
946 struct list_head ioports;
malattia@linux.it9f9f0762007-04-28 23:19:36 +0900947 struct mutex lock;
malattia@linux.it33a04452007-04-09 19:26:03 +0200948};
949
950static struct sony_pic_dev spic_dev = {
951 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
952 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
953};
954
955/* Event masks */
956#define SONYPI_JOGGER_MASK 0x00000001
957#define SONYPI_CAPTURE_MASK 0x00000002
958#define SONYPI_FNKEY_MASK 0x00000004
959#define SONYPI_BLUETOOTH_MASK 0x00000008
960#define SONYPI_PKEY_MASK 0x00000010
961#define SONYPI_BACK_MASK 0x00000020
962#define SONYPI_HELP_MASK 0x00000040
963#define SONYPI_LID_MASK 0x00000080
964#define SONYPI_ZOOM_MASK 0x00000100
965#define SONYPI_THUMBPHRASE_MASK 0x00000200
966#define SONYPI_MEYE_MASK 0x00000400
967#define SONYPI_MEMORYSTICK_MASK 0x00000800
968#define SONYPI_BATTERY_MASK 0x00001000
969#define SONYPI_WIRELESS_MASK 0x00002000
970
971struct sonypi_event {
972 u8 data;
973 u8 event;
974};
975
976/* The set of possible button release events */
977static struct sonypi_event sonypi_releaseev[] = {
978 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
979 { 0, 0 }
980};
981
982/* The set of possible jogger events */
983static struct sonypi_event sonypi_joggerev[] = {
984 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
985 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
986 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
987 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
988 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
989 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
990 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
991 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
992 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
993 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
994 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
995 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
996 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
997 { 0, 0 }
998};
999
1000/* The set of possible capture button events */
1001static struct sonypi_event sonypi_captureev[] = {
1002 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
1003 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
1004 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
1005 { 0, 0 }
1006};
1007
1008/* The set of possible fnkeys events */
1009static struct sonypi_event sonypi_fnkeyev[] = {
1010 { 0x10, SONYPI_EVENT_FNKEY_ESC },
1011 { 0x11, SONYPI_EVENT_FNKEY_F1 },
1012 { 0x12, SONYPI_EVENT_FNKEY_F2 },
1013 { 0x13, SONYPI_EVENT_FNKEY_F3 },
1014 { 0x14, SONYPI_EVENT_FNKEY_F4 },
1015 { 0x15, SONYPI_EVENT_FNKEY_F5 },
1016 { 0x16, SONYPI_EVENT_FNKEY_F6 },
1017 { 0x17, SONYPI_EVENT_FNKEY_F7 },
1018 { 0x18, SONYPI_EVENT_FNKEY_F8 },
1019 { 0x19, SONYPI_EVENT_FNKEY_F9 },
1020 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
1021 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
1022 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
1023 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
1024 { 0x21, SONYPI_EVENT_FNKEY_1 },
1025 { 0x22, SONYPI_EVENT_FNKEY_2 },
1026 { 0x31, SONYPI_EVENT_FNKEY_D },
1027 { 0x32, SONYPI_EVENT_FNKEY_E },
1028 { 0x33, SONYPI_EVENT_FNKEY_F },
1029 { 0x34, SONYPI_EVENT_FNKEY_S },
1030 { 0x35, SONYPI_EVENT_FNKEY_B },
1031 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
1032 { 0, 0 }
1033};
1034
1035/* The set of possible program key events */
1036static struct sonypi_event sonypi_pkeyev[] = {
1037 { 0x01, SONYPI_EVENT_PKEY_P1 },
1038 { 0x02, SONYPI_EVENT_PKEY_P2 },
1039 { 0x04, SONYPI_EVENT_PKEY_P3 },
1040 { 0x5c, SONYPI_EVENT_PKEY_P1 },
1041 { 0, 0 }
1042};
1043
1044/* The set of possible bluetooth events */
1045static struct sonypi_event sonypi_blueev[] = {
1046 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
1047 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
1048 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
1049 { 0, 0 }
1050};
1051
1052/* The set of possible wireless events */
1053static struct sonypi_event sonypi_wlessev[] = {
1054 { 0x59, SONYPI_EVENT_WIRELESS_ON },
1055 { 0x5a, SONYPI_EVENT_WIRELESS_OFF },
1056 { 0, 0 }
1057};
1058
1059/* The set of possible back button events */
1060static struct sonypi_event sonypi_backev[] = {
1061 { 0x20, SONYPI_EVENT_BACK_PRESSED },
1062 { 0, 0 }
1063};
1064
1065/* The set of possible help button events */
1066static struct sonypi_event sonypi_helpev[] = {
1067 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
1068 { 0, 0 }
1069};
1070
1071
1072/* The set of possible lid events */
1073static struct sonypi_event sonypi_lidev[] = {
1074 { 0x51, SONYPI_EVENT_LID_CLOSED },
1075 { 0x50, SONYPI_EVENT_LID_OPENED },
1076 { 0, 0 }
1077};
1078
1079/* The set of possible zoom events */
1080static struct sonypi_event sonypi_zoomev[] = {
1081 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
1082 { 0, 0 }
1083};
1084
1085/* The set of possible thumbphrase events */
1086static struct sonypi_event sonypi_thumbphraseev[] = {
1087 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
1088 { 0, 0 }
1089};
1090
1091/* The set of possible motioneye camera events */
1092static struct sonypi_event sonypi_meyeev[] = {
1093 { 0x00, SONYPI_EVENT_MEYE_FACE },
1094 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
1095 { 0, 0 }
1096};
1097
1098/* The set of possible memorystick events */
1099static struct sonypi_event sonypi_memorystickev[] = {
1100 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
1101 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
1102 { 0, 0 }
1103};
1104
1105/* The set of possible battery events */
1106static struct sonypi_event sonypi_batteryev[] = {
1107 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
1108 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
1109 { 0, 0 }
1110};
1111
1112static struct sonypi_eventtypes {
1113 int model;
1114 u8 data;
1115 unsigned long mask;
1116 struct sonypi_event * events;
1117} sony_pic_eventtypes[] = {
1118 { SONYPI_DEVICE_TYPE1, 0, 0xffffffff, sonypi_releaseev },
1119 { SONYPI_DEVICE_TYPE1, 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
1120 { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_LID_MASK, sonypi_lidev },
1121 { SONYPI_DEVICE_TYPE1, 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
1122 { SONYPI_DEVICE_TYPE1, 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
1123 { SONYPI_DEVICE_TYPE1, 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1124 { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1125 { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
1126 { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1127 { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
1128
1129 { SONYPI_DEVICE_TYPE2, 0, 0xffffffff, sonypi_releaseev },
1130 { SONYPI_DEVICE_TYPE2, 0x38, SONYPI_LID_MASK, sonypi_lidev },
1131 { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
1132 { SONYPI_DEVICE_TYPE2, 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
1133 { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1134 { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1135 { SONYPI_DEVICE_TYPE2, 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
1136 { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_BACK_MASK, sonypi_backev },
1137 { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_HELP_MASK, sonypi_helpev },
1138 { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
1139 { SONYPI_DEVICE_TYPE2, 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
1140 { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1141 { SONYPI_DEVICE_TYPE2, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1142 { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1143
1144 { SONYPI_DEVICE_TYPE3, 0, 0xffffffff, sonypi_releaseev },
1145 { SONYPI_DEVICE_TYPE3, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1146 { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
1147 { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1148 { SONYPI_DEVICE_TYPE3, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1149 { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1150 { 0 }
1151};
1152
1153static int sony_pic_detect_device_type(void)
1154{
1155 struct pci_dev *pcidev;
1156 int model = 0;
1157
1158 if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1159 PCI_DEVICE_ID_INTEL_82371AB_3, NULL)))
1160 model = SONYPI_DEVICE_TYPE1;
1161
1162 else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1163 PCI_DEVICE_ID_INTEL_ICH6_1, NULL)))
1164 model = SONYPI_DEVICE_TYPE3;
1165
1166 else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1167 PCI_DEVICE_ID_INTEL_ICH7_1, NULL)))
1168 model = SONYPI_DEVICE_TYPE3;
1169
1170 else
1171 model = SONYPI_DEVICE_TYPE2;
1172
1173 if (pcidev)
1174 pci_dev_put(pcidev);
1175
1176 printk(KERN_INFO DRV_PFX "detected Type%d model\n",
1177 model == SONYPI_DEVICE_TYPE1 ? 1 :
1178 model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
1179 return model;
1180}
1181
1182#define ITERATIONS_LONG 10000
1183#define ITERATIONS_SHORT 10
1184#define wait_on_command(command, iterations) { \
1185 unsigned int n = iterations; \
1186 while (--n && (command)) \
1187 udelay(1); \
1188 if (!n) \
1189 dprintk("command failed at %s : %s (line %d)\n", \
1190 __FILE__, __FUNCTION__, __LINE__); \
1191}
1192
1193static u8 sony_pic_call1(u8 dev)
1194{
1195 u8 v1, v2;
1196
1197 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1198 ITERATIONS_LONG);
1199 outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1200 v1 = inb_p(spic_dev.cur_ioport->io.minimum + 4);
1201 v2 = inb_p(spic_dev.cur_ioport->io.minimum);
1202 dprintk("sony_pic_call1: 0x%.4x\n", (v2 << 8) | v1);
1203 return v2;
1204}
1205
1206static u8 sony_pic_call2(u8 dev, u8 fn)
1207{
1208 u8 v1;
1209
1210 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1211 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,
1214 ITERATIONS_LONG);
1215 outb(fn, spic_dev.cur_ioport->io.minimum);
1216 v1 = inb_p(spic_dev.cur_ioport->io.minimum);
1217 dprintk("sony_pic_call2: 0x%.4x\n", v1);
1218 return v1;
1219}
1220
malattia@linux.it49a11de2007-04-09 19:28:56 +02001221static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
1222{
1223 u8 v1;
1224
1225 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
1226 outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1227 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
1228 outb(fn, spic_dev.cur_ioport->io.minimum);
1229 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
1230 outb(v, spic_dev.cur_ioport->io.minimum);
1231 v1 = inb_p(spic_dev.cur_ioport->io.minimum);
1232 dprintk("sony_pic_call3: 0x%.4x\n", v1);
1233 return v1;
1234}
1235
1236/* camera tests and poweron/poweroff */
1237#define SONYPI_CAMERA_PICTURE 5
malattia@linux.it49a11de2007-04-09 19:28:56 +02001238#define SONYPI_CAMERA_CONTROL 0x10
malattia@linux.ite3646322007-04-28 23:34:22 +09001239
1240#define SONYPI_CAMERA_BRIGHTNESS 0
1241#define SONYPI_CAMERA_CONTRAST 1
1242#define SONYPI_CAMERA_HUE 2
1243#define SONYPI_CAMERA_COLOR 3
1244#define SONYPI_CAMERA_SHARPNESS 4
1245
1246#define SONYPI_CAMERA_EXPOSURE_MASK 0xC
1247#define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
1248#define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
1249#define SONYPI_CAMERA_MUTE_MASK 0x40
1250
1251/* the rest don't need a loop until not 0xff */
1252#define SONYPI_CAMERA_AGC 6
1253#define SONYPI_CAMERA_AGC_MASK 0x30
1254#define SONYPI_CAMERA_SHUTTER_MASK 0x7
1255
1256#define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
1257#define SONYPI_CAMERA_CONTROL 0x10
1258
1259#define SONYPI_CAMERA_STATUS 7
1260#define SONYPI_CAMERA_STATUS_READY 0x2
1261#define SONYPI_CAMERA_STATUS_POSITION 0x4
1262
1263#define SONYPI_DIRECTION_BACKWARDS 0x4
1264
1265#define SONYPI_CAMERA_REVISION 8
1266#define SONYPI_CAMERA_ROMVERSION 9
malattia@linux.it49a11de2007-04-09 19:28:56 +02001267
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001268static int __sony_pic_camera_ready(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001269{
1270 u8 v;
1271
1272 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
1273 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
1274}
1275
malattia@linux.ite3646322007-04-28 23:34:22 +09001276static int __sony_pic_camera_off(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001277{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001278 if (!camera) {
1279 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1280 return -ENODEV;
1281 }
1282
malattia@linux.it49a11de2007-04-09 19:28:56 +02001283 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
1284 SONYPI_CAMERA_MUTE_MASK),
1285 ITERATIONS_SHORT);
1286
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001287 if (spic_dev.camera_power) {
1288 sony_pic_call2(0x91, 0);
1289 spic_dev.camera_power = 0;
1290 }
1291 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001292}
1293
malattia@linux.ite3646322007-04-28 23:34:22 +09001294static int __sony_pic_camera_on(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001295{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001296 int i, j, x;
1297
1298 if (!camera) {
1299 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1300 return -ENODEV;
1301 }
malattia@linux.it49a11de2007-04-09 19:28:56 +02001302
1303 if (spic_dev.camera_power)
malattia@linux.ite3646322007-04-28 23:34:22 +09001304 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001305
1306 for (j = 5; j > 0; j--) {
1307
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001308 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001309 msleep(10);
1310 sony_pic_call1(0x93);
1311
1312 for (i = 400; i > 0; i--) {
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001313 if (__sony_pic_camera_ready())
malattia@linux.it49a11de2007-04-09 19:28:56 +02001314 break;
1315 msleep(10);
1316 }
1317 if (i)
1318 break;
1319 }
1320
1321 if (j == 0) {
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001322 printk(KERN_WARNING DRV_PFX "failed to power on camera\n");
malattia@linux.ite3646322007-04-28 23:34:22 +09001323 return -ENODEV;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001324 }
1325
1326 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
1327 0x5a),
1328 ITERATIONS_SHORT);
1329
1330 spic_dev.camera_power = 1;
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001331 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001332}
1333
malattia@linux.ite3646322007-04-28 23:34:22 +09001334/* External camera command (exported to the motion eye v4l driver) */
1335int sony_pic_camera_command(int command, u8 value)
1336{
1337 if (!camera)
1338 return -EIO;
1339
1340 mutex_lock(&spic_dev.lock);
1341
1342 switch (command) {
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001343 case SONY_PIC_COMMAND_SETCAMERA:
malattia@linux.ite3646322007-04-28 23:34:22 +09001344 if (value)
1345 __sony_pic_camera_on();
1346 else
1347 __sony_pic_camera_off();
1348 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001349 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09001350 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
1351 ITERATIONS_SHORT);
1352 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001353 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
malattia@linux.ite3646322007-04-28 23:34:22 +09001354 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
1355 ITERATIONS_SHORT);
1356 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001357 case SONY_PIC_COMMAND_SETCAMERAHUE:
malattia@linux.ite3646322007-04-28 23:34:22 +09001358 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
1359 ITERATIONS_SHORT);
1360 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001361 case SONY_PIC_COMMAND_SETCAMERACOLOR:
malattia@linux.ite3646322007-04-28 23:34:22 +09001362 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
1363 ITERATIONS_SHORT);
1364 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001365 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09001366 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
1367 ITERATIONS_SHORT);
1368 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001369 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
malattia@linux.ite3646322007-04-28 23:34:22 +09001370 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
1371 ITERATIONS_SHORT);
1372 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001373 case SONY_PIC_COMMAND_SETCAMERAAGC:
malattia@linux.ite3646322007-04-28 23:34:22 +09001374 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
1375 ITERATIONS_SHORT);
1376 break;
1377 default:
1378 printk(KERN_ERR DRV_PFX "sony_pic_camera_command invalid: %d\n",
1379 command);
1380 break;
1381 }
1382 mutex_unlock(&spic_dev.lock);
1383 return 0;
1384}
1385EXPORT_SYMBOL(sony_pic_camera_command);
1386
malattia@linux.it9476cdf2007-04-28 23:21:42 +09001387/* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
1388static void sony_pic_set_wwanpower(u8 state)
1389{
1390 state = !!state;
1391 mutex_lock(&spic_dev.lock);
1392 if (spic_dev.wwan_power == state) {
1393 mutex_unlock(&spic_dev.lock);
1394 return;
1395 }
1396 sony_pic_call2(0xB0, state);
1397 spic_dev.wwan_power = state;
1398 mutex_unlock(&spic_dev.lock);
1399}
1400
1401static ssize_t sony_pic_wwanpower_store(struct device *dev,
1402 struct device_attribute *attr,
1403 const char *buffer, size_t count)
1404{
1405 unsigned long value;
1406 if (count > 31)
1407 return -EINVAL;
1408
1409 value = simple_strtoul(buffer, NULL, 10);
1410 sony_pic_set_wwanpower(value);
1411
1412 return count;
1413}
1414
1415static ssize_t sony_pic_wwanpower_show(struct device *dev,
1416 struct device_attribute *attr, char *buffer)
1417{
1418 ssize_t count;
1419 mutex_lock(&spic_dev.lock);
1420 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
1421 mutex_unlock(&spic_dev.lock);
1422 return count;
1423}
1424
malattia@linux.it49a11de2007-04-09 19:28:56 +02001425/* bluetooth subsystem power state */
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001426static void __sony_pic_set_bluetoothpower(u8 state)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001427{
1428 state = !!state;
1429 if (spic_dev.bluetooth_power == state)
1430 return;
1431 sony_pic_call2(0x96, state);
1432 sony_pic_call1(0x82);
1433 spic_dev.bluetooth_power = state;
1434}
1435
1436static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
1437 struct device_attribute *attr,
1438 const char *buffer, size_t count)
1439{
1440 unsigned long value;
1441 if (count > 31)
1442 return -EINVAL;
1443
1444 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001445 mutex_lock(&spic_dev.lock);
1446 __sony_pic_set_bluetoothpower(value);
1447 mutex_unlock(&spic_dev.lock);
malattia@linux.it49a11de2007-04-09 19:28:56 +02001448
1449 return count;
1450}
1451
1452static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
1453 struct device_attribute *attr, char *buffer)
1454{
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001455 ssize_t count = 0;
1456 mutex_lock(&spic_dev.lock);
1457 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
1458 mutex_unlock(&spic_dev.lock);
1459 return count;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001460}
1461
1462/* fan speed */
1463/* FAN0 information (reverse engineered from ACPI tables) */
1464#define SONY_PIC_FAN0_STATUS 0x93
malattia@linux.it7b153f32007-04-09 19:31:25 +02001465static int sony_pic_set_fanspeed(unsigned long value)
1466{
1467 return ec_write(SONY_PIC_FAN0_STATUS, value);
1468}
1469
1470static int sony_pic_get_fanspeed(u8 *value)
1471{
1472 return ec_read(SONY_PIC_FAN0_STATUS, value);
1473}
1474
malattia@linux.it49a11de2007-04-09 19:28:56 +02001475static ssize_t sony_pic_fanspeed_store(struct device *dev,
1476 struct device_attribute *attr,
1477 const char *buffer, size_t count)
1478{
1479 unsigned long value;
1480 if (count > 31)
1481 return -EINVAL;
1482
1483 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it7b153f32007-04-09 19:31:25 +02001484 if (sony_pic_set_fanspeed(value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02001485 return -EIO;
1486
1487 return count;
1488}
1489
1490static ssize_t sony_pic_fanspeed_show(struct device *dev,
1491 struct device_attribute *attr, char *buffer)
1492{
1493 u8 value = 0;
malattia@linux.it7b153f32007-04-09 19:31:25 +02001494 if (sony_pic_get_fanspeed(&value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02001495 return -EIO;
1496
1497 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
1498}
1499
1500#define SPIC_ATTR(_name, _mode) \
1501struct device_attribute spic_attr_##_name = __ATTR(_name, \
1502 _mode, sony_pic_## _name ##_show, \
1503 sony_pic_## _name ##_store)
1504
malattia@linux.it49a11de2007-04-09 19:28:56 +02001505static SPIC_ATTR(bluetoothpower, 0644);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09001506static SPIC_ATTR(wwanpower, 0644);
malattia@linux.it49a11de2007-04-09 19:28:56 +02001507static SPIC_ATTR(fanspeed, 0644);
1508
1509static struct attribute *spic_attributes[] = {
malattia@linux.it49a11de2007-04-09 19:28:56 +02001510 &spic_attr_bluetoothpower.attr,
malattia@linux.it9476cdf2007-04-28 23:21:42 +09001511 &spic_attr_wwanpower.attr,
malattia@linux.it49a11de2007-04-09 19:28:56 +02001512 &spic_attr_fanspeed.attr,
1513 NULL
1514};
1515
1516static struct attribute_group spic_attribute_group = {
1517 .attrs = spic_attributes
1518};
1519
malattia@linux.it7b153f32007-04-09 19:31:25 +02001520/******** SONYPI compatibility **********/
Mattia Dongilia64e62a2007-05-01 11:19:53 +09001521#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +02001522
1523/* battery / brightness / temperature addresses */
1524#define SONYPI_BAT_FLAGS 0x81
1525#define SONYPI_LCD_LIGHT 0x96
1526#define SONYPI_BAT1_PCTRM 0xa0
1527#define SONYPI_BAT1_LEFT 0xa2
1528#define SONYPI_BAT1_MAXRT 0xa4
1529#define SONYPI_BAT2_PCTRM 0xa8
1530#define SONYPI_BAT2_LEFT 0xaa
1531#define SONYPI_BAT2_MAXRT 0xac
1532#define SONYPI_BAT1_MAXTK 0xb0
1533#define SONYPI_BAT1_FULL 0xb2
1534#define SONYPI_BAT2_MAXTK 0xb8
1535#define SONYPI_BAT2_FULL 0xba
1536#define SONYPI_TEMP_STATUS 0xC1
1537
1538struct sonypi_compat_s {
1539 struct fasync_struct *fifo_async;
1540 struct kfifo *fifo;
1541 spinlock_t fifo_lock;
1542 wait_queue_head_t fifo_proc_list;
1543 atomic_t open_count;
1544};
1545static struct sonypi_compat_s sonypi_compat = {
1546 .open_count = ATOMIC_INIT(0),
1547};
1548
1549static int sonypi_misc_fasync(int fd, struct file *filp, int on)
1550{
1551 int retval;
1552
1553 retval = fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
1554 if (retval < 0)
1555 return retval;
1556 return 0;
1557}
1558
1559static int sonypi_misc_release(struct inode *inode, struct file *file)
1560{
1561 sonypi_misc_fasync(-1, file, 0);
1562 atomic_dec(&sonypi_compat.open_count);
1563 return 0;
1564}
1565
1566static int sonypi_misc_open(struct inode *inode, struct file *file)
1567{
1568 /* Flush input queue on first open */
1569 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
1570 kfifo_reset(sonypi_compat.fifo);
1571 return 0;
1572}
1573
1574static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
1575 size_t count, loff_t *pos)
1576{
1577 ssize_t ret;
1578 unsigned char c;
1579
1580 if ((kfifo_len(sonypi_compat.fifo) == 0) &&
1581 (file->f_flags & O_NONBLOCK))
1582 return -EAGAIN;
1583
1584 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
1585 kfifo_len(sonypi_compat.fifo) != 0);
1586 if (ret)
1587 return ret;
1588
1589 while (ret < count &&
1590 (kfifo_get(sonypi_compat.fifo, &c, sizeof(c)) == sizeof(c))) {
1591 if (put_user(c, buf++))
1592 return -EFAULT;
1593 ret++;
1594 }
1595
1596 if (ret > 0) {
1597 struct inode *inode = file->f_path.dentry->d_inode;
1598 inode->i_atime = current_fs_time(inode->i_sb);
1599 }
1600
1601 return ret;
1602}
1603
1604static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
1605{
1606 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
1607 if (kfifo_len(sonypi_compat.fifo))
1608 return POLLIN | POLLRDNORM;
1609 return 0;
1610}
1611
1612static int ec_read16(u8 addr, u16 *value)
1613{
1614 u8 val_lb, val_hb;
1615 if (ec_read(addr, &val_lb))
1616 return -1;
1617 if (ec_read(addr + 1, &val_hb))
1618 return -1;
1619 *value = val_lb | (val_hb << 8);
1620 return 0;
1621}
1622
1623static int sonypi_misc_ioctl(struct inode *ip, struct file *fp,
1624 unsigned int cmd, unsigned long arg)
1625{
1626 int ret = 0;
1627 void __user *argp = (void __user *)arg;
1628 u8 val8;
1629 u16 val16;
1630 int value;
1631
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001632 mutex_lock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02001633 switch (cmd) {
1634 case SONYPI_IOCGBRT:
1635 if (sony_backlight_device == NULL) {
1636 ret = -EIO;
1637 break;
1638 }
1639 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
1640 ret = -EIO;
1641 break;
1642 }
1643 val8 = ((value & 0xff) - 1) << 5;
1644 if (copy_to_user(argp, &val8, sizeof(val8)))
1645 ret = -EFAULT;
1646 break;
1647 case SONYPI_IOCSBRT:
1648 if (sony_backlight_device == NULL) {
1649 ret = -EIO;
1650 break;
1651 }
1652 if (copy_from_user(&val8, argp, sizeof(val8))) {
1653 ret = -EFAULT;
1654 break;
1655 }
1656 if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
1657 (val8 >> 5) + 1, NULL)) {
1658 ret = -EIO;
1659 break;
1660 }
1661 /* sync the backlight device status */
1662 sony_backlight_device->props.brightness =
1663 sony_backlight_get_brightness(sony_backlight_device);
1664 break;
1665 case SONYPI_IOCGBAT1CAP:
1666 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
1667 ret = -EIO;
1668 break;
1669 }
1670 if (copy_to_user(argp, &val16, sizeof(val16)))
1671 ret = -EFAULT;
1672 break;
1673 case SONYPI_IOCGBAT1REM:
1674 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
1675 ret = -EIO;
1676 break;
1677 }
1678 if (copy_to_user(argp, &val16, sizeof(val16)))
1679 ret = -EFAULT;
1680 break;
1681 case SONYPI_IOCGBAT2CAP:
1682 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
1683 ret = -EIO;
1684 break;
1685 }
1686 if (copy_to_user(argp, &val16, sizeof(val16)))
1687 ret = -EFAULT;
1688 break;
1689 case SONYPI_IOCGBAT2REM:
1690 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
1691 ret = -EIO;
1692 break;
1693 }
1694 if (copy_to_user(argp, &val16, sizeof(val16)))
1695 ret = -EFAULT;
1696 break;
1697 case SONYPI_IOCGBATFLAGS:
1698 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
1699 ret = -EIO;
1700 break;
1701 }
1702 val8 &= 0x07;
1703 if (copy_to_user(argp, &val8, sizeof(val8)))
1704 ret = -EFAULT;
1705 break;
1706 case SONYPI_IOCGBLUE:
1707 val8 = spic_dev.bluetooth_power;
1708 if (copy_to_user(argp, &val8, sizeof(val8)))
1709 ret = -EFAULT;
1710 break;
1711 case SONYPI_IOCSBLUE:
1712 if (copy_from_user(&val8, argp, sizeof(val8))) {
1713 ret = -EFAULT;
1714 break;
1715 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001716 __sony_pic_set_bluetoothpower(val8);
malattia@linux.it7b153f32007-04-09 19:31:25 +02001717 break;
1718 /* FAN Controls */
1719 case SONYPI_IOCGFAN:
1720 if (sony_pic_get_fanspeed(&val8)) {
1721 ret = -EIO;
1722 break;
1723 }
1724 if (copy_to_user(argp, &val8, sizeof(val8)))
1725 ret = -EFAULT;
1726 break;
1727 case SONYPI_IOCSFAN:
1728 if (copy_from_user(&val8, argp, sizeof(val8))) {
1729 ret = -EFAULT;
1730 break;
1731 }
1732 if (sony_pic_set_fanspeed(val8))
1733 ret = -EIO;
1734 break;
1735 /* GET Temperature (useful under APM) */
1736 case SONYPI_IOCGTEMP:
1737 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
1738 ret = -EIO;
1739 break;
1740 }
1741 if (copy_to_user(argp, &val8, sizeof(val8)))
1742 ret = -EFAULT;
1743 break;
1744 default:
1745 ret = -EINVAL;
1746 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001747 mutex_unlock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02001748 return ret;
1749}
1750
1751static const struct file_operations sonypi_misc_fops = {
1752 .owner = THIS_MODULE,
1753 .read = sonypi_misc_read,
1754 .poll = sonypi_misc_poll,
1755 .open = sonypi_misc_open,
1756 .release = sonypi_misc_release,
1757 .fasync = sonypi_misc_fasync,
1758 .ioctl = sonypi_misc_ioctl,
1759};
1760
1761static struct miscdevice sonypi_misc_device = {
1762 .minor = MISC_DYNAMIC_MINOR,
1763 .name = "sonypi",
1764 .fops = &sonypi_misc_fops,
1765};
1766
1767static void sonypi_compat_report_event(u8 event)
1768{
1769 kfifo_put(sonypi_compat.fifo, (unsigned char *)&event, sizeof(event));
1770 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
1771 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
1772}
1773
1774static int sonypi_compat_init(void)
1775{
1776 int error;
1777
1778 spin_lock_init(&sonypi_compat.fifo_lock);
1779 sonypi_compat.fifo = kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
1780 &sonypi_compat.fifo_lock);
1781 if (IS_ERR(sonypi_compat.fifo)) {
1782 printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
1783 return PTR_ERR(sonypi_compat.fifo);
1784 }
1785
1786 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
malattia@linux.it7b153f32007-04-09 19:31:25 +02001787
1788 if (minor != -1)
1789 sonypi_misc_device.minor = minor;
1790 error = misc_register(&sonypi_misc_device);
1791 if (error) {
1792 printk(KERN_ERR DRV_PFX "misc_register failed\n");
1793 goto err_free_kfifo;
1794 }
1795 if (minor == -1)
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001796 printk(KERN_INFO DRV_PFX "device allocated minor is %d\n",
malattia@linux.it7b153f32007-04-09 19:31:25 +02001797 sonypi_misc_device.minor);
1798
1799 return 0;
1800
1801err_free_kfifo:
1802 kfifo_free(sonypi_compat.fifo);
1803 return error;
1804}
1805
1806static void sonypi_compat_exit(void)
1807{
1808 misc_deregister(&sonypi_misc_device);
1809 kfifo_free(sonypi_compat.fifo);
1810}
1811#else
1812static int sonypi_compat_init(void) { return 0; }
1813static void sonypi_compat_exit(void) { }
1814static void sonypi_compat_report_event(u8 event) { }
Mattia Dongilia64e62a2007-05-01 11:19:53 +09001815#endif /* CONFIG_SONYPI_COMPAT */
malattia@linux.it7b153f32007-04-09 19:31:25 +02001816
malattia@linux.it1549ee62007-04-09 10:19:08 +02001817/*
malattia@linux.it33a04452007-04-09 19:26:03 +02001818 * ACPI callbacks
malattia@linux.it1549ee62007-04-09 10:19:08 +02001819 */
malattia@linux.it33a04452007-04-09 19:26:03 +02001820static acpi_status
1821sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
1822{
1823 u32 i;
1824 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
1825
1826 switch (resource->type) {
1827 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
1828 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
1829 return AE_OK;
1830
1831 case ACPI_RESOURCE_TYPE_IRQ:
1832 {
1833 struct acpi_resource_irq *p = &resource->data.irq;
1834 struct sony_pic_irq *interrupt = NULL;
1835 if (!p || !p->interrupt_count) {
1836 /*
1837 * IRQ descriptors may have no IRQ# bits set,
1838 * particularly those those w/ _STA disabled
1839 */
1840 dprintk("Blank IRQ resource\n");
1841 return AE_OK;
1842 }
1843 for (i = 0; i < p->interrupt_count; i++) {
1844 if (!p->interrupts[i]) {
1845 printk(KERN_WARNING DRV_PFX
1846 "Invalid IRQ %d\n",
1847 p->interrupts[i]);
1848 continue;
1849 }
1850 interrupt = kzalloc(sizeof(*interrupt),
1851 GFP_KERNEL);
1852 if (!interrupt)
1853 return AE_ERROR;
1854
malattia@linux.it1a3e3232007-04-28 23:34:10 +09001855 list_add_tail(&interrupt->list, &dev->interrupts);
malattia@linux.it33a04452007-04-09 19:26:03 +02001856 interrupt->irq.triggering = p->triggering;
1857 interrupt->irq.polarity = p->polarity;
1858 interrupt->irq.sharable = p->sharable;
1859 interrupt->irq.interrupt_count = 1;
1860 interrupt->irq.interrupts[0] = p->interrupts[i];
1861 }
1862 return AE_OK;
1863 }
1864 case ACPI_RESOURCE_TYPE_IO:
1865 {
1866 struct acpi_resource_io *io = &resource->data.io;
1867 struct sony_pic_ioport *ioport = NULL;
1868 if (!io) {
1869 dprintk("Blank IO resource\n");
1870 return AE_OK;
1871 }
1872
1873 ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
1874 if (!ioport)
1875 return AE_ERROR;
1876
malattia@linux.it1a3e3232007-04-28 23:34:10 +09001877 list_add_tail(&ioport->list, &dev->ioports);
malattia@linux.it33a04452007-04-09 19:26:03 +02001878 memcpy(&ioport->io, io, sizeof(*io));
1879 return AE_OK;
1880 }
1881 default:
1882 dprintk("Resource %d isn't an IRQ nor an IO port\n",
1883 resource->type);
1884
1885 case ACPI_RESOURCE_TYPE_END_TAG:
1886 return AE_OK;
1887 }
1888 return AE_CTRL_TERMINATE;
1889}
1890
1891static int sony_pic_possible_resources(struct acpi_device *device)
1892{
1893 int result = 0;
1894 acpi_status status = AE_OK;
1895
1896 if (!device)
1897 return -EINVAL;
1898
1899 /* get device status */
1900 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
1901 dprintk("Evaluating _STA\n");
1902 result = acpi_bus_get_status(device);
1903 if (result) {
1904 printk(KERN_WARNING DRV_PFX "Unable to read status\n");
1905 goto end;
1906 }
1907
1908 if (!device->status.enabled)
1909 dprintk("Device disabled\n");
1910 else
1911 dprintk("Device enabled\n");
1912
1913 /*
1914 * Query and parse 'method'
1915 */
1916 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
1917 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
1918 sony_pic_read_possible_resource, &spic_dev);
1919 if (ACPI_FAILURE(status)) {
1920 printk(KERN_WARNING DRV_PFX
1921 "Failure evaluating %s\n",
1922 METHOD_NAME__PRS);
1923 result = -ENODEV;
1924 }
1925end:
1926 return result;
1927}
1928
1929/*
1930 * Disable the spic device by calling its _DIS method
1931 */
1932static int sony_pic_disable(struct acpi_device *device)
1933{
Randy Dunlapf3a740c2007-07-15 23:40:49 -07001934 if (ACPI_FAILURE(acpi_evaluate_object(device->handle,
1935 "_DIS", NULL, NULL)))
malattia@linux.it33a04452007-04-09 19:26:03 +02001936 return -ENXIO;
1937
1938 dprintk("Device disabled\n");
1939 return 0;
1940}
1941
1942
1943/*
1944 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
1945 *
1946 * Call _SRS to set current resources
1947 */
1948static int sony_pic_enable(struct acpi_device *device,
1949 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
1950{
1951 acpi_status status;
1952 int result = 0;
1953 struct {
1954 struct acpi_resource io_res;
1955 struct acpi_resource irq_res;
1956 struct acpi_resource end;
1957 } *resource;
1958 struct acpi_buffer buffer = { 0, NULL };
1959
1960 if (!ioport || !irq)
1961 return -EINVAL;
1962
1963 /* init acpi_buffer */
1964 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
1965 if (!resource)
1966 return -ENOMEM;
1967
1968 buffer.length = sizeof(*resource) + 1;
1969 buffer.pointer = resource;
1970
1971 /* setup io resource */
1972 resource->io_res.type = ACPI_RESOURCE_TYPE_IO;
1973 resource->io_res.length = sizeof(struct acpi_resource);
1974 memcpy(&resource->io_res.data.io, &ioport->io,
1975 sizeof(struct acpi_resource_io));
1976
1977 /* setup irq resource */
1978 resource->irq_res.type = ACPI_RESOURCE_TYPE_IRQ;
1979 resource->irq_res.length = sizeof(struct acpi_resource);
1980 memcpy(&resource->irq_res.data.irq, &irq->irq,
1981 sizeof(struct acpi_resource_irq));
1982 /* we requested a shared irq */
1983 resource->irq_res.data.irq.sharable = ACPI_SHARED;
1984
1985 resource->end.type = ACPI_RESOURCE_TYPE_END_TAG;
1986
1987 /* Attempt to set the resource */
1988 dprintk("Evaluating _SRS\n");
1989 status = acpi_set_current_resources(device->handle, &buffer);
1990
1991 /* check for total failure */
1992 if (ACPI_FAILURE(status)) {
1993 printk(KERN_ERR DRV_PFX "Error evaluating _SRS");
1994 result = -ENODEV;
1995 goto end;
1996 }
1997
1998 /* Necessary device initializations calls (from sonypi) */
1999 sony_pic_call1(0x82);
2000 sony_pic_call2(0x81, 0xff);
2001 sony_pic_call1(compat ? 0x92 : 0x82);
2002
2003end:
2004 kfree(resource);
2005 return result;
2006}
2007
2008/*****************
2009 *
2010 * ISR: some event is available
2011 *
2012 *****************/
2013static irqreturn_t sony_pic_irq(int irq, void *dev_id)
2014{
2015 int i, j;
2016 u32 port_val = 0;
2017 u8 ev = 0;
2018 u8 data_mask = 0;
2019 u8 device_event = 0;
2020
2021 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
2022
2023 acpi_os_read_port(dev->cur_ioport->io.minimum, &port_val,
2024 dev->cur_ioport->io.address_length);
2025 ev = port_val & SONY_PIC_EV_MASK;
2026 data_mask = 0xff & (port_val >> (dev->cur_ioport->io.address_length - 8));
2027
2028 dprintk("event (0x%.8x [%.2x] [%.2x]) at port 0x%.4x\n",
2029 port_val, ev, data_mask, dev->cur_ioport->io.minimum);
2030
2031 if (ev == 0x00 || ev == 0xff)
2032 return IRQ_HANDLED;
2033
2034 for (i = 0; sony_pic_eventtypes[i].model; i++) {
2035
2036 if (spic_dev.model != sony_pic_eventtypes[i].model)
2037 continue;
2038
2039 if ((data_mask & sony_pic_eventtypes[i].data) !=
2040 sony_pic_eventtypes[i].data)
2041 continue;
2042
2043 if (!(mask & sony_pic_eventtypes[i].mask))
2044 continue;
2045
2046 for (j = 0; sony_pic_eventtypes[i].events[j].event; j++) {
2047 if (ev == sony_pic_eventtypes[i].events[j].data) {
2048 device_event =
2049 sony_pic_eventtypes[i].events[j].event;
2050 goto found;
2051 }
2052 }
2053 }
2054 return IRQ_HANDLED;
2055
2056found:
malattia@linux.it1549ee62007-04-09 10:19:08 +02002057 sony_laptop_report_input_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02002058 acpi_bus_generate_event(spic_dev.acpi_dev, 1, device_event);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002059 sonypi_compat_report_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02002060
2061 return IRQ_HANDLED;
2062}
2063
2064/*****************
2065 *
2066 * ACPI driver
2067 *
2068 *****************/
2069static int sony_pic_remove(struct acpi_device *device, int type)
2070{
2071 struct sony_pic_ioport *io, *tmp_io;
2072 struct sony_pic_irq *irq, *tmp_irq;
2073
malattia@linux.it7b153f32007-04-09 19:31:25 +02002074 sonypi_compat_exit();
2075
malattia@linux.it33a04452007-04-09 19:26:03 +02002076 if (sony_pic_disable(device)) {
2077 printk(KERN_ERR DRV_PFX "Couldn't disable device.\n");
2078 return -ENXIO;
2079 }
2080
2081 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2082 release_region(spic_dev.cur_ioport->io.minimum,
2083 spic_dev.cur_ioport->io.address_length);
2084
malattia@linux.it1549ee62007-04-09 10:19:08 +02002085 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02002086
malattia@linux.it49a11de2007-04-09 19:28:56 +02002087 /* pf attrs */
2088 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2089 sony_pf_remove();
2090
malattia@linux.it33a04452007-04-09 19:26:03 +02002091 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2092 list_del(&io->list);
2093 kfree(io);
2094 }
2095 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2096 list_del(&irq->list);
2097 kfree(irq);
2098 }
2099 spic_dev.cur_ioport = NULL;
2100 spic_dev.cur_irq = NULL;
2101
malattia@linux.itf6119b02007-04-09 19:31:06 +02002102 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002103 return 0;
2104}
2105
2106static int sony_pic_add(struct acpi_device *device)
2107{
2108 int result;
2109 struct sony_pic_ioport *io, *tmp_io;
2110 struct sony_pic_irq *irq, *tmp_irq;
2111
malattia@linux.itf6119b02007-04-09 19:31:06 +02002112 printk(KERN_INFO DRV_PFX "%s v%s.\n",
2113 SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.it33a04452007-04-09 19:26:03 +02002114
2115 spic_dev.acpi_dev = device;
2116 strcpy(acpi_device_class(device), "sony/hotkey");
2117 spic_dev.model = sony_pic_detect_device_type();
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002118 mutex_init(&spic_dev.lock);
malattia@linux.it33a04452007-04-09 19:26:03 +02002119
2120 /* read _PRS resources */
2121 result = sony_pic_possible_resources(device);
2122 if (result) {
2123 printk(KERN_ERR DRV_PFX
2124 "Unabe to read possible resources.\n");
2125 goto err_free_resources;
2126 }
2127
2128 /* setup input devices and helper fifo */
malattia@linux.it1549ee62007-04-09 10:19:08 +02002129 result = sony_laptop_setup_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02002130 if (result) {
2131 printk(KERN_ERR DRV_PFX
2132 "Unabe to create input devices.\n");
2133 goto err_free_resources;
2134 }
2135
2136 /* request io port */
2137 list_for_each_entry(io, &spic_dev.ioports, list) {
2138 if (request_region(io->io.minimum, io->io.address_length,
2139 "Sony Programable I/O Device")) {
2140 dprintk("I/O port: 0x%.4x (0x%.4x) + 0x%.2x\n",
2141 io->io.minimum, io->io.maximum,
2142 io->io.address_length);
2143 spic_dev.cur_ioport = io;
2144 break;
2145 }
2146 }
2147 if (!spic_dev.cur_ioport) {
2148 printk(KERN_ERR DRV_PFX "Failed to request_region.\n");
2149 result = -ENODEV;
2150 goto err_remove_input;
2151 }
2152
2153 /* request IRQ */
2154 list_for_each_entry(irq, &spic_dev.interrupts, list) {
2155 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
2156 IRQF_SHARED, "sony-laptop", &spic_dev)) {
2157 dprintk("IRQ: %d - triggering: %d - "
2158 "polarity: %d - shr: %d\n",
2159 irq->irq.interrupts[0],
2160 irq->irq.triggering,
2161 irq->irq.polarity,
2162 irq->irq.sharable);
2163 spic_dev.cur_irq = irq;
2164 break;
2165 }
2166 }
2167 if (!spic_dev.cur_irq) {
2168 printk(KERN_ERR DRV_PFX "Failed to request_irq.\n");
2169 result = -ENODEV;
2170 goto err_release_region;
2171 }
2172
2173 /* set resource status _SRS */
2174 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2175 if (result) {
2176 printk(KERN_ERR DRV_PFX "Couldn't enable device.\n");
2177 goto err_free_irq;
2178 }
2179
malattia@linux.it49a11de2007-04-09 19:28:56 +02002180 spic_dev.bluetooth_power = -1;
2181 /* create device attributes */
2182 result = sony_pf_add();
2183 if (result)
2184 goto err_disable_device;
2185
2186 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2187 if (result)
2188 goto err_remove_pf;
2189
malattia@linux.it7b153f32007-04-09 19:31:25 +02002190 if (sonypi_compat_init())
2191 goto err_remove_pf;
2192
malattia@linux.it33a04452007-04-09 19:26:03 +02002193 return 0;
2194
malattia@linux.it49a11de2007-04-09 19:28:56 +02002195err_remove_pf:
2196 sony_pf_remove();
2197
2198err_disable_device:
2199 sony_pic_disable(device);
2200
malattia@linux.it33a04452007-04-09 19:26:03 +02002201err_free_irq:
2202 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2203
2204err_release_region:
2205 release_region(spic_dev.cur_ioport->io.minimum,
2206 spic_dev.cur_ioport->io.address_length);
2207
2208err_remove_input:
malattia@linux.it1549ee62007-04-09 10:19:08 +02002209 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02002210
2211err_free_resources:
2212 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2213 list_del(&io->list);
2214 kfree(io);
2215 }
2216 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2217 list_del(&irq->list);
2218 kfree(irq);
2219 }
2220 spic_dev.cur_ioport = NULL;
2221 spic_dev.cur_irq = NULL;
2222
2223 return result;
2224}
2225
2226static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
2227{
2228 if (sony_pic_disable(device))
2229 return -ENXIO;
2230 return 0;
2231}
2232
2233static int sony_pic_resume(struct acpi_device *device)
2234{
2235 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2236 return 0;
2237}
2238
2239static struct acpi_driver sony_pic_driver = {
2240 .name = SONY_PIC_DRIVER_NAME,
2241 .class = SONY_PIC_CLASS,
2242 .ids = SONY_PIC_HID,
2243 .owner = THIS_MODULE,
2244 .ops = {
2245 .add = sony_pic_add,
2246 .remove = sony_pic_remove,
2247 .suspend = sony_pic_suspend,
2248 .resume = sony_pic_resume,
2249 },
2250};
2251
2252static struct dmi_system_id __initdata sonypi_dmi_table[] = {
2253 {
2254 .ident = "Sony Vaio",
2255 .matches = {
2256 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2257 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
2258 },
2259 },
2260 {
2261 .ident = "Sony Vaio",
2262 .matches = {
2263 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2264 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
2265 },
2266 },
2267 { }
2268};
2269
malattia@linux.it59b19102007-04-09 10:19:04 +02002270static int __init sony_laptop_init(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01002271{
malattia@linux.it33a04452007-04-09 19:26:03 +02002272 int result;
2273
2274 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
2275 result = acpi_bus_register_driver(&sony_pic_driver);
2276 if (result) {
2277 printk(KERN_ERR DRV_PFX
2278 "Unable to register SPIC driver.");
2279 goto out;
2280 }
2281 }
2282
2283 result = acpi_bus_register_driver(&sony_nc_driver);
2284 if (result) {
2285 printk(KERN_ERR DRV_PFX "Unable to register SNC driver.");
2286 goto out_unregister_pic;
2287 }
2288
2289 return 0;
2290
2291out_unregister_pic:
2292 if (!no_spic)
2293 acpi_bus_unregister_driver(&sony_pic_driver);
2294out:
2295 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +01002296}
2297
malattia@linux.it59b19102007-04-09 10:19:04 +02002298static void __exit sony_laptop_exit(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01002299{
malattia@linux.it59b19102007-04-09 10:19:04 +02002300 acpi_bus_unregister_driver(&sony_nc_driver);
malattia@linux.it33a04452007-04-09 19:26:03 +02002301 if (!no_spic)
2302 acpi_bus_unregister_driver(&sony_pic_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +01002303}
2304
malattia@linux.it59b19102007-04-09 10:19:04 +02002305module_init(sony_laptop_init);
2306module_exit(sony_laptop_exit);