blob: 5300cad9cd7f919998c34b7175375dff9aea5bea [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 },
Mattia Dongili8538c362007-07-16 02:34:34 +0900180 { SONYPI_EVENT_WIRELESS_ON, KEY_WLAN },
181 { SONYPI_EVENT_WIRELESS_OFF, KEY_WLAN },
malattia@linux.it1549ee62007-04-09 10:19:08 +0200182 { SONYPI_EVENT_THUMBPHRASE_PRESSED, BTN_THUMB },
183 { 0, 0 },
184};
185
186/* release buttons after a short delay if pressed */
187static void do_sony_laptop_release_key(struct work_struct *work)
188{
189 struct sony_laptop_keypress kp;
190
191 while (kfifo_get(sony_laptop_input.fifo, (unsigned char *)&kp,
192 sizeof(kp)) == sizeof(kp)) {
193 msleep(10);
194 input_report_key(kp.dev, kp.key, 0);
195 input_sync(kp.dev);
196 }
197}
198static DECLARE_WORK(sony_laptop_release_key_work,
199 do_sony_laptop_release_key);
200
201/* forward event to the input subsytem */
202static void sony_laptop_report_input_event(u8 event)
203{
204 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
205 struct input_dev *key_dev = sony_laptop_input.key_dev;
206 struct sony_laptop_keypress kp = { NULL };
207 int i;
208
209 if (event == SONYPI_EVENT_FNKEY_RELEASED) {
210 /* Nothing, not all VAIOs generate this event */
211 return;
212 }
213
214 /* report events */
215 switch (event) {
216 /* jog_dev events */
217 case SONYPI_EVENT_JOGDIAL_UP:
218 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
219 input_report_rel(jog_dev, REL_WHEEL, 1);
220 input_sync(jog_dev);
221 return;
222
223 case SONYPI_EVENT_JOGDIAL_DOWN:
224 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
225 input_report_rel(jog_dev, REL_WHEEL, -1);
226 input_sync(jog_dev);
227 return;
228
229 /* key_dev events */
230 case SONYPI_EVENT_JOGDIAL_PRESSED:
231 kp.key = BTN_MIDDLE;
232 kp.dev = jog_dev;
233 break;
234
235 default:
236 for (i = 0; sony_laptop_inputkeys[i].sonypiev; i++)
237 if (event == sony_laptop_inputkeys[i].sonypiev) {
238 kp.dev = key_dev;
239 kp.key = sony_laptop_inputkeys[i].inputev;
240 break;
241 }
242 break;
243 }
244
245 if (kp.dev) {
246 input_report_key(kp.dev, kp.key, 1);
247 input_sync(kp.dev);
248 kfifo_put(sony_laptop_input.fifo,
249 (unsigned char *)&kp, sizeof(kp));
250
251 if (!work_pending(&sony_laptop_release_key_work))
252 queue_work(sony_laptop_input.wq,
253 &sony_laptop_release_key_work);
254 } else
255 dprintk("unknown input event %.2x\n", event);
256}
257
258static int sony_laptop_setup_input(void)
259{
260 struct input_dev *jog_dev;
261 struct input_dev *key_dev;
262 int i;
263 int error;
264
265 /* don't run again if already initialized */
266 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
267 return 0;
268
269 /* kfifo */
270 spin_lock_init(&sony_laptop_input.fifo_lock);
271 sony_laptop_input.fifo =
272 kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
273 &sony_laptop_input.fifo_lock);
274 if (IS_ERR(sony_laptop_input.fifo)) {
275 printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
276 error = PTR_ERR(sony_laptop_input.fifo);
277 goto err_dec_users;
278 }
279
280 /* init workqueue */
281 sony_laptop_input.wq = create_singlethread_workqueue("sony-laptop");
282 if (!sony_laptop_input.wq) {
283 printk(KERN_ERR DRV_PFX
284 "Unabe to create workqueue.\n");
285 error = -ENXIO;
286 goto err_free_kfifo;
287 }
288
289 /* input keys */
290 key_dev = input_allocate_device();
291 if (!key_dev) {
292 error = -ENOMEM;
293 goto err_destroy_wq;
294 }
295
296 key_dev->name = "Sony Vaio Keys";
297 key_dev->id.bustype = BUS_ISA;
298 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
299
300 /* Initialize the Input Drivers: special keys */
301 key_dev->evbit[0] = BIT(EV_KEY);
302 for (i = 0; sony_laptop_inputkeys[i].sonypiev; i++)
303 if (sony_laptop_inputkeys[i].inputev)
304 set_bit(sony_laptop_inputkeys[i].inputev,
305 key_dev->keybit);
306
307 error = input_register_device(key_dev);
308 if (error)
309 goto err_free_keydev;
310
311 sony_laptop_input.key_dev = key_dev;
312
313 /* jogdial */
314 jog_dev = input_allocate_device();
315 if (!jog_dev) {
316 error = -ENOMEM;
317 goto err_unregister_keydev;
318 }
319
320 jog_dev->name = "Sony Vaio Jogdial";
321 jog_dev->id.bustype = BUS_ISA;
322 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
323
324 jog_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
325 jog_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_MIDDLE);
326 jog_dev->relbit[0] = BIT(REL_WHEEL);
327
328 error = input_register_device(jog_dev);
329 if (error)
330 goto err_free_jogdev;
331
332 sony_laptop_input.jog_dev = jog_dev;
333
334 return 0;
335
336err_free_jogdev:
337 input_free_device(jog_dev);
338
339err_unregister_keydev:
340 input_unregister_device(key_dev);
341 /* to avoid kref underflow below at input_free_device */
342 key_dev = NULL;
343
344err_free_keydev:
345 input_free_device(key_dev);
346
347err_destroy_wq:
348 destroy_workqueue(sony_laptop_input.wq);
349
350err_free_kfifo:
351 kfifo_free(sony_laptop_input.fifo);
352
353err_dec_users:
354 atomic_dec(&sony_laptop_input.users);
355 return error;
356}
357
358static void sony_laptop_remove_input(void)
359{
360 /* cleanup only after the last user has gone */
361 if (!atomic_dec_and_test(&sony_laptop_input.users))
362 return;
363
364 /* flush workqueue first */
365 flush_workqueue(sony_laptop_input.wq);
366
367 /* destroy input devs */
368 input_unregister_device(sony_laptop_input.key_dev);
369 sony_laptop_input.key_dev = NULL;
370
371 if (sony_laptop_input.jog_dev) {
372 input_unregister_device(sony_laptop_input.jog_dev);
373 sony_laptop_input.jog_dev = NULL;
374 }
375
376 destroy_workqueue(sony_laptop_input.wq);
377 kfifo_free(sony_laptop_input.fifo);
378}
379
malattia@linux.it56b87562007-04-09 10:19:05 +0200380/*********** Platform Device ***********/
381
382static atomic_t sony_pf_users = ATOMIC_INIT(0);
383static struct platform_driver sony_pf_driver = {
384 .driver = {
385 .name = "sony-laptop",
386 .owner = THIS_MODULE,
387 }
388};
389static struct platform_device *sony_pf_device;
390
391static int sony_pf_add(void)
392{
393 int ret = 0;
394
395 /* don't run again if already initialized */
396 if (atomic_add_return(1, &sony_pf_users) > 1)
397 return 0;
398
399 ret = platform_driver_register(&sony_pf_driver);
400 if (ret)
401 goto out;
402
403 sony_pf_device = platform_device_alloc("sony-laptop", -1);
404 if (!sony_pf_device) {
405 ret = -ENOMEM;
406 goto out_platform_registered;
407 }
408
409 ret = platform_device_add(sony_pf_device);
410 if (ret)
411 goto out_platform_alloced;
412
413 return 0;
414
415 out_platform_alloced:
416 platform_device_put(sony_pf_device);
417 sony_pf_device = NULL;
418 out_platform_registered:
419 platform_driver_unregister(&sony_pf_driver);
420 out:
421 atomic_dec(&sony_pf_users);
422 return ret;
423}
424
425static void sony_pf_remove(void)
426{
427 /* deregister only after the last user has gone */
428 if (!atomic_dec_and_test(&sony_pf_users))
429 return;
430
431 platform_device_del(sony_pf_device);
432 platform_device_put(sony_pf_device);
433 platform_driver_unregister(&sony_pf_driver);
434}
435
436/*********** SNC (SNY5001) Device ***********/
437
malattia@linux.it33a04452007-04-09 19:26:03 +0200438/* the device uses 1-based values, while the backlight subsystem uses
439 0-based values */
440#define SONY_MAX_BRIGHTNESS 8
441
442#define SNC_VALIDATE_IN 0
443#define SNC_VALIDATE_OUT 1
444
malattia@linux.it59b19102007-04-09 10:19:04 +0200445static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500446 char *);
malattia@linux.it59b19102007-04-09 10:19:04 +0200447static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500448 const char *, size_t);
Mattia Dongili156c2212007-02-12 22:01:07 +0100449static int boolean_validate(const int, const int);
450static int brightness_default_validate(const int, const int);
451
malattia@linux.it59b19102007-04-09 10:19:04 +0200452struct sony_nc_value {
Len Browna02d1c12007-02-07 15:34:02 -0500453 char *name; /* name of the entry */
454 char **acpiget; /* names of the ACPI get function */
455 char **acpiset; /* names of the ACPI set function */
Mattia Dongili156c2212007-02-12 22:01:07 +0100456 int (*validate)(const int, const int); /* input/output validation */
Len Browna02d1c12007-02-07 15:34:02 -0500457 int value; /* current setting */
458 int valid; /* Has ever been set */
459 int debug; /* active only in debug mode ? */
460 struct device_attribute devattr; /* sysfs atribute */
Stelian Pop7f09c432007-01-13 23:04:31 +0100461};
462
malattia@linux.it59b19102007-04-09 10:19:04 +0200463#define SNC_HANDLE_NAMES(_name, _values...) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100464 static char *snc_##_name[] = { _values, NULL }
465
malattia@linux.it59b19102007-04-09 10:19:04 +0200466#define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100467 { \
468 .name = __stringify(_name), \
469 .acpiget = _getters, \
470 .acpiset = _setters, \
Mattia Dongili156c2212007-02-12 22:01:07 +0100471 .validate = _validate, \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100472 .debug = _debug, \
malattia@linux.it59b19102007-04-09 10:19:04 +0200473 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100474 }
475
malattia@linux.it59b19102007-04-09 10:19:04 +0200476#define SNC_HANDLE_NULL { .name = NULL }
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100477
malattia@linux.it59b19102007-04-09 10:19:04 +0200478SNC_HANDLE_NAMES(fnkey_get, "GHKE");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100479
malattia@linux.it59b19102007-04-09 10:19:04 +0200480SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
481SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100482
malattia@linux.it59b19102007-04-09 10:19:04 +0200483SNC_HANDLE_NAMES(cdpower_get, "GCDP");
484SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100485
malattia@linux.it59b19102007-04-09 10:19:04 +0200486SNC_HANDLE_NAMES(audiopower_get, "GAZP");
487SNC_HANDLE_NAMES(audiopower_set, "AZPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100488
malattia@linux.it59b19102007-04-09 10:19:04 +0200489SNC_HANDLE_NAMES(lanpower_get, "GLNP");
490SNC_HANDLE_NAMES(lanpower_set, "LNPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100491
Mattia Dongili044847e2007-07-16 02:34:33 +0900492SNC_HANDLE_NAMES(lidstate_get, "GLID");
493
494SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
495SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
496
497SNC_HANDLE_NAMES(gainbass_get, "GMGB");
498SNC_HANDLE_NAMES(gainbass_set, "CMGB");
499
malattia@linux.it59b19102007-04-09 10:19:04 +0200500SNC_HANDLE_NAMES(PID_get, "GPID");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100501
malattia@linux.it59b19102007-04-09 10:19:04 +0200502SNC_HANDLE_NAMES(CTR_get, "GCTR");
503SNC_HANDLE_NAMES(CTR_set, "SCTR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100504
malattia@linux.it59b19102007-04-09 10:19:04 +0200505SNC_HANDLE_NAMES(PCR_get, "GPCR");
506SNC_HANDLE_NAMES(PCR_set, "SPCR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100507
malattia@linux.it59b19102007-04-09 10:19:04 +0200508SNC_HANDLE_NAMES(CMI_get, "GCMI");
509SNC_HANDLE_NAMES(CMI_set, "SCMI");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100510
malattia@linux.it59b19102007-04-09 10:19:04 +0200511static struct sony_nc_value sony_nc_values[] = {
512 SNC_HANDLE(brightness_default, snc_brightness_def_get,
Mattia Dongili156c2212007-02-12 22:01:07 +0100513 snc_brightness_def_set, brightness_default_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200514 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
515 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
516 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100517 boolean_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200518 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100519 boolean_validate, 1),
Mattia Dongili044847e2007-07-16 02:34:33 +0900520 SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
521 boolean_validate, 0),
522 SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
523 boolean_validate, 0),
524 SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
525 boolean_validate, 0),
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100526 /* unknown methods */
malattia@linux.it59b19102007-04-09 10:19:04 +0200527 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
528 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
529 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
530 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
531 SNC_HANDLE_NULL
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100532};
533
malattia@linux.it59b19102007-04-09 10:19:04 +0200534static acpi_handle sony_nc_acpi_handle;
535static struct acpi_device *sony_nc_acpi_device = NULL;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100536
Mattia Dongilid78865c2007-02-07 20:01:56 +0100537/*
538 * acpi_evaluate_object wrappers
539 */
Stelian Pop7f09c432007-01-13 23:04:31 +0100540static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
541{
542 struct acpi_buffer output;
543 union acpi_object out_obj;
544 acpi_status status;
545
546 output.length = sizeof(out_obj);
547 output.pointer = &out_obj;
548
549 status = acpi_evaluate_object(handle, name, NULL, &output);
550 if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
551 *result = out_obj.integer.value;
552 return 0;
553 }
554
malattia@linux.itf6119b02007-04-09 19:31:06 +0200555 printk(KERN_WARNING DRV_PFX "acpi_callreadfunc failed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100556
557 return -1;
558}
559
560static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
561 int *result)
562{
563 struct acpi_object_list params;
564 union acpi_object in_obj;
565 struct acpi_buffer output;
566 union acpi_object out_obj;
567 acpi_status status;
568
569 params.count = 1;
570 params.pointer = &in_obj;
571 in_obj.type = ACPI_TYPE_INTEGER;
572 in_obj.integer.value = value;
573
574 output.length = sizeof(out_obj);
575 output.pointer = &out_obj;
576
577 status = acpi_evaluate_object(handle, name, &params, &output);
578 if (status == AE_OK) {
579 if (result != NULL) {
580 if (out_obj.type != ACPI_TYPE_INTEGER) {
malattia@linux.itf6119b02007-04-09 19:31:06 +0200581 printk(KERN_WARNING DRV_PFX "acpi_evaluate_object bad "
Stelian Pop7f09c432007-01-13 23:04:31 +0100582 "return type\n");
583 return -1;
584 }
585 *result = out_obj.integer.value;
586 }
587 return 0;
588 }
589
malattia@linux.itf6119b02007-04-09 19:31:06 +0200590 printk(KERN_WARNING DRV_PFX "acpi_evaluate_object failed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100591
592 return -1;
593}
594
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100595/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200596 * sony_nc_values input/output validate functions
Mattia Dongili156c2212007-02-12 22:01:07 +0100597 */
598
599/* brightness_default_validate:
600 *
601 * manipulate input output values to keep consistency with the
602 * backlight framework for which brightness values are 0-based.
603 */
604static int brightness_default_validate(const int direction, const int value)
605{
606 switch (direction) {
607 case SNC_VALIDATE_OUT:
608 return value - 1;
609 case SNC_VALIDATE_IN:
610 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
611 return value + 1;
612 }
613 return -EINVAL;
614}
615
616/* boolean_validate:
617 *
618 * on input validate boolean values 0/1, on output just pass the
619 * received value.
620 */
621static int boolean_validate(const int direction, const int value)
622{
623 if (direction == SNC_VALIDATE_IN) {
624 if (value != 0 && value != 1)
625 return -EINVAL;
626 }
627 return value;
628}
629
630/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200631 * Sysfs show/store common to all sony_nc_values
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100632 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200633static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
Len Browna02d1c12007-02-07 15:34:02 -0500634 char *buffer)
Stelian Pop7f09c432007-01-13 23:04:31 +0100635{
Stelian Pop7f09c432007-01-13 23:04:31 +0100636 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200637 struct sony_nc_value *item =
638 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100639
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100640 if (!*item->acpiget)
Stelian Pop7f09c432007-01-13 23:04:31 +0100641 return -EIO;
642
malattia@linux.it59b19102007-04-09 10:19:04 +0200643 if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100644 return -EIO;
645
Mattia Dongili156c2212007-02-12 22:01:07 +0100646 if (item->validate)
647 value = item->validate(SNC_VALIDATE_OUT, value);
648
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100649 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
Stelian Pop7f09c432007-01-13 23:04:31 +0100650}
651
malattia@linux.it59b19102007-04-09 10:19:04 +0200652static ssize_t sony_nc_sysfs_store(struct device *dev,
Len Browna02d1c12007-02-07 15:34:02 -0500653 struct device_attribute *attr,
654 const char *buffer, size_t count)
Stelian Pop7f09c432007-01-13 23:04:31 +0100655{
Stelian Pop7f09c432007-01-13 23:04:31 +0100656 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200657 struct sony_nc_value *item =
658 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100659
660 if (!item->acpiset)
661 return -EIO;
662
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100663 if (count > 31)
664 return -EINVAL;
665
666 value = simple_strtoul(buffer, NULL, 10);
Stelian Pop7f09c432007-01-13 23:04:31 +0100667
Mattia Dongili156c2212007-02-12 22:01:07 +0100668 if (item->validate)
669 value = item->validate(SNC_VALIDATE_IN, value);
670
671 if (value < 0)
672 return value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100673
malattia@linux.it59b19102007-04-09 10:19:04 +0200674 if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100675 return -EIO;
Andrew Morton3f4f4612007-01-13 23:04:32 +0100676 item->value = value;
677 item->valid = 1;
Stelian Pop7f09c432007-01-13 23:04:31 +0100678 return count;
679}
680
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100681
Mattia Dongilid78865c2007-02-07 20:01:56 +0100682/*
683 * Backlight device
684 */
685static int sony_backlight_update_status(struct backlight_device *bd)
Andrew Morton3f4f4612007-01-13 23:04:32 +0100686{
malattia@linux.it59b19102007-04-09 10:19:04 +0200687 return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
Richard Purdie321709c2007-02-10 15:04:08 +0000688 bd->props.brightness + 1, NULL);
Andrew Morton3f4f4612007-01-13 23:04:32 +0100689}
690
Mattia Dongilid78865c2007-02-07 20:01:56 +0100691static int sony_backlight_get_brightness(struct backlight_device *bd)
692{
693 int value;
694
malattia@linux.it59b19102007-04-09 10:19:04 +0200695 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
Mattia Dongilid78865c2007-02-07 20:01:56 +0100696 return 0;
697 /* brightness levels are 1-based, while backlight ones are 0-based */
698 return value - 1;
699}
700
701static struct backlight_device *sony_backlight_device;
Richard Purdie321709c2007-02-10 15:04:08 +0000702static struct backlight_ops sony_backlight_ops = {
Len Browna02d1c12007-02-07 15:34:02 -0500703 .update_status = sony_backlight_update_status,
704 .get_brightness = sony_backlight_get_brightness,
Mattia Dongilid78865c2007-02-07 20:01:56 +0100705};
706
707/*
Mattia Dongili6315fd12007-07-16 02:34:35 +0900708 * New SNC-only Vaios event mapping to driver known keys
709 */
710struct sony_nc_event {
711 u8 data;
712 u8 event;
713};
714
715static struct sony_nc_event *sony_nc_events;
716
717/* Vaio C* --maybe also FE*, N* and AR* ?-- special init sequence
718 * for Fn keys
719 */
720static int sony_nc_C_enable(struct dmi_system_id *id)
721{
722 int result = 0;
723
724 printk(KERN_NOTICE DRV_PFX "detected %s\n", id->ident);
725
726 sony_nc_events = id->driver_data;
727
728 if (acpi_callsetfunc(sony_nc_acpi_handle, "SN02", 0x4, &result) < 0
729 || acpi_callsetfunc(sony_nc_acpi_handle, "SN07", 0x2, &result) < 0
730 || acpi_callsetfunc(sony_nc_acpi_handle, "SN02", 0x10, &result) < 0
731 || acpi_callsetfunc(sony_nc_acpi_handle, "SN07", 0x0, &result) < 0
732 || acpi_callsetfunc(sony_nc_acpi_handle, "SN03", 0x2, &result) < 0
733 || acpi_callsetfunc(sony_nc_acpi_handle, "SN07", 0x101, &result) < 0) {
734 printk(KERN_WARNING DRV_PFX "failed to initialize SNC, some "
735 "functionalities may be missing\n");
736 return 1;
737 }
738 return 0;
739}
740
741static struct sony_nc_event sony_C_events[] = {
742 { 0x81, SONYPI_EVENT_FNKEY_F1 },
743 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
744 { 0x85, SONYPI_EVENT_FNKEY_F5 },
745 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
746 { 0x86, SONYPI_EVENT_FNKEY_F6 },
747 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
748 { 0x87, SONYPI_EVENT_FNKEY_F7 },
749 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
750 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
751 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
752 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
753 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
754 { 0, 0 },
755};
756
757/* SNC-only model map */
758struct dmi_system_id sony_nc_ids[] = {
759 {
760 .ident = "Sony Vaio C Series",
761 .callback = sony_nc_C_enable,
762 .driver_data = sony_C_events,
763 .matches = {
764 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
765 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-C"),
766 },
767 },
768 { }
769};
770
771/*
Mattia Dongilid78865c2007-02-07 20:01:56 +0100772 * ACPI callbacks
773 */
Stelian Pop7f09c432007-01-13 23:04:31 +0100774static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
775{
Mattia Dongili6315fd12007-07-16 02:34:35 +0900776 struct sony_nc_event *evmap;
777 u32 ev = event;
778 int result;
779
780 if (ev == 0x92) {
781 /* read the key pressed from EC.GECR
782 * A call to SN07 with 0x0202 will do it as well respecting
783 * the current protocol on different OSes
784 *
785 * Note: the path for GECR may be
786 * \_SB.PCI0.LPCB.EC (C, FE, AR, N and friends)
787 * \_SB.PCI0.PIB.EC0 (VGN-FR notifications are sent directly, no GECR)
788 *
789 * TODO: we may want to do the same for the older GHKE -need
790 * dmi list- so this snippet may become one more callback.
791 */
792 if (acpi_callsetfunc(handle, "SN07", 0x0202, &result) < 0)
793 dprintk("sony_acpi_notify, unable to decode event 0x%.2x\n", ev);
794 else
795 ev = result & 0xFF;
796 }
797
798 if (sony_nc_events)
799 for (evmap = sony_nc_events; evmap->event; evmap++) {
800 if (evmap->data == ev) {
801 ev = evmap->event;
802 break;
803 }
804 }
805
806 dprintk("sony_acpi_notify, event: 0x%.2x\n", ev);
807 sony_laptop_report_input_event(ev);
808 acpi_bus_generate_event(sony_nc_acpi_device, 1, ev);
Stelian Pop7f09c432007-01-13 23:04:31 +0100809}
810
811static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
812 void *context, void **return_value)
813{
814 struct acpi_namespace_node *node;
815 union acpi_operand_object *operand;
816
Len Browna02d1c12007-02-07 15:34:02 -0500817 node = (struct acpi_namespace_node *)handle;
818 operand = (union acpi_operand_object *)node->object;
Stelian Pop7f09c432007-01-13 23:04:31 +0100819
malattia@linux.itf6119b02007-04-09 19:31:06 +0200820 printk(KERN_WARNING DRV_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
Stelian Pop7f09c432007-01-13 23:04:31 +0100821 (u32) operand->method.param_count);
822
823 return AE_OK;
824}
825
Mattia Dongilid78865c2007-02-07 20:01:56 +0100826/*
827 * ACPI device
828 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200829static int sony_nc_resume(struct acpi_device *device)
Mattia Dongilid78865c2007-02-07 20:01:56 +0100830{
malattia@linux.it59b19102007-04-09 10:19:04 +0200831 struct sony_nc_value *item;
Mattia Dongilid78865c2007-02-07 20:01:56 +0100832
malattia@linux.it59b19102007-04-09 10:19:04 +0200833 for (item = sony_nc_values; item->name; item++) {
Mattia Dongilid78865c2007-02-07 20:01:56 +0100834 int ret;
835
836 if (!item->valid)
837 continue;
malattia@linux.it59b19102007-04-09 10:19:04 +0200838 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
Len Browna02d1c12007-02-07 15:34:02 -0500839 item->value, NULL);
Mattia Dongilid78865c2007-02-07 20:01:56 +0100840 if (ret < 0) {
841 printk("%s: %d\n", __FUNCTION__, ret);
842 break;
843 }
844 }
Mattia Dongili6315fd12007-07-16 02:34:35 +0900845
846 /* re-initialize models with specific requirements */
847 dmi_check_system(sony_nc_ids);
848
Mattia Dongilid78865c2007-02-07 20:01:56 +0100849 return 0;
850}
851
malattia@linux.it59b19102007-04-09 10:19:04 +0200852static int sony_nc_add(struct acpi_device *device)
Stelian Pop7f09c432007-01-13 23:04:31 +0100853{
854 acpi_status status;
Andrew Morton8607c672007-03-06 02:29:42 -0800855 int result = 0;
Alessandro Guido50f62af2007-01-13 23:04:34 +0100856 acpi_handle handle;
malattia@linux.it56b87562007-04-09 10:19:05 +0200857 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +0100858
malattia@linux.itf6119b02007-04-09 19:31:06 +0200859 printk(KERN_INFO DRV_PFX "%s v%s.\n",
860 SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
861
malattia@linux.it59b19102007-04-09 10:19:04 +0200862 sony_nc_acpi_device = device;
malattia@linux.it33a04452007-04-09 19:26:03 +0200863 strcpy(acpi_device_class(device), "sony/hotkey");
Stelian Popc5611622007-01-13 23:04:37 +0100864
malattia@linux.it59b19102007-04-09 10:19:04 +0200865 sony_nc_acpi_handle = device->handle;
Stelian Pop7f09c432007-01-13 23:04:31 +0100866
Mattia Dongilib25b7322007-07-16 02:34:36 +0900867 /* read device status */
868 result = acpi_bus_get_status(device);
869 /* bail IFF the above call was successful and the device is not present */
870 if (!result && !device->status.present) {
871 dprintk("Device not present\n");
872 result = -ENODEV;
873 goto outwalk;
874 }
875
Stelian Pop7f09c432007-01-13 23:04:31 +0100876 if (debug) {
malattia@linux.it59b19102007-04-09 10:19:04 +0200877 status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_nc_acpi_handle,
Stelian Pop7f09c432007-01-13 23:04:31 +0100878 1, sony_walk_callback, NULL, NULL);
879 if (ACPI_FAILURE(status)) {
malattia@linux.itf6119b02007-04-09 19:31:06 +0200880 printk(KERN_WARNING DRV_PFX "unable to walk acpi resources\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100881 result = -ENODEV;
882 goto outwalk;
883 }
Stelian Popc5611622007-01-13 23:04:37 +0100884 }
Stelian Pop7f09c432007-01-13 23:04:31 +0100885
Mattia Dongilib25b7322007-07-16 02:34:36 +0900886 /* try to _INI the device if such method exists (ACPI spec 3.0-6.5.1
887 * should be respected as we already checked for the device presence above */
888 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, METHOD_NAME__INI, &handle))) {
889 dprintk("Invoking _INI\n");
890 if (ACPI_FAILURE(acpi_evaluate_object(sony_nc_acpi_handle, METHOD_NAME__INI,
891 NULL, NULL)))
892 dprintk("_INI Method failed\n");
893 }
894
malattia@linux.it1549ee62007-04-09 10:19:08 +0200895 /* setup input devices and helper fifo */
896 result = sony_laptop_setup_input();
897 if (result) {
898 printk(KERN_ERR DRV_PFX
899 "Unabe to create input devices.\n");
900 goto outwalk;
901 }
902
malattia@linux.it59b19102007-04-09 10:19:04 +0200903 status = acpi_install_notify_handler(sony_nc_acpi_handle,
Stelian Popc5611622007-01-13 23:04:37 +0100904 ACPI_DEVICE_NOTIFY,
Len Browna02d1c12007-02-07 15:34:02 -0500905 sony_acpi_notify, NULL);
Stelian Popc5611622007-01-13 23:04:37 +0100906 if (ACPI_FAILURE(status)) {
Mattia Dongilib25b7322007-07-16 02:34:36 +0900907 printk(KERN_WARNING DRV_PFX "unable to install notify handler (%u)\n", status);
Stelian Popc5611622007-01-13 23:04:37 +0100908 result = -ENODEV;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200909 goto outinput;
Stelian Pop7f09c432007-01-13 23:04:31 +0100910 }
911
malattia@linux.it59b19102007-04-09 10:19:04 +0200912 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT", &handle))) {
Alessandro Guido50f62af2007-01-13 23:04:34 +0100913 sony_backlight_device = backlight_device_register("sony", NULL,
Len Browna02d1c12007-02-07 15:34:02 -0500914 NULL,
Richard Purdie321709c2007-02-10 15:04:08 +0000915 &sony_backlight_ops);
Mattia Dongili7df03b82007-01-13 23:04:41 +0100916
Len Browna02d1c12007-02-07 15:34:02 -0500917 if (IS_ERR(sony_backlight_device)) {
malattia@linux.itf6119b02007-04-09 19:31:06 +0200918 printk(KERN_WARNING DRV_PFX "unable to register backlight device\n");
Mattia Dongili7df03b82007-01-13 23:04:41 +0100919 sony_backlight_device = NULL;
Richard Purdie321709c2007-02-10 15:04:08 +0000920 } else {
921 sony_backlight_device->props.brightness =
Len Browna02d1c12007-02-07 15:34:02 -0500922 sony_backlight_get_brightness
923 (sony_backlight_device);
Richard Purdie321709c2007-02-10 15:04:08 +0000924 sony_backlight_device->props.max_brightness =
925 SONY_MAX_BRIGHTNESS - 1;
926 }
927
Alessandro Guido50f62af2007-01-13 23:04:34 +0100928 }
Stelian Pop7f09c432007-01-13 23:04:31 +0100929
Mattia Dongili6315fd12007-07-16 02:34:35 +0900930 /* initialize models with specific requirements */
931 dmi_check_system(sony_nc_ids);
932
malattia@linux.it49a11de2007-04-09 19:28:56 +0200933 result = sony_pf_add();
934 if (result)
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100935 goto outbacklight;
Stelian Pop7f09c432007-01-13 23:04:31 +0100936
malattia@linux.it56b87562007-04-09 10:19:05 +0200937 /* create sony_pf sysfs attributes related to the SNC device */
938 for (item = sony_nc_values; item->name; ++item) {
939
940 if (!debug && item->debug)
941 continue;
942
943 /* find the available acpiget as described in the DSDT */
944 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
945 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
946 *item->acpiget,
947 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +0200948 dprintk("Found %s getter: %s\n",
949 item->name, *item->acpiget);
malattia@linux.it56b87562007-04-09 10:19:05 +0200950 item->devattr.attr.mode |= S_IRUGO;
951 break;
952 }
953 }
954
955 /* find the available acpiset as described in the DSDT */
956 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
957 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
958 *item->acpiset,
959 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +0200960 dprintk("Found %s setter: %s\n",
961 item->name, *item->acpiset);
malattia@linux.it56b87562007-04-09 10:19:05 +0200962 item->devattr.attr.mode |= S_IWUSR;
963 break;
964 }
965 }
966
967 if (item->devattr.attr.mode != 0) {
968 result =
969 device_create_file(&sony_pf_device->dev,
970 &item->devattr);
971 if (result)
972 goto out_sysfs;
973 }
974 }
975
Stelian Pop7f09c432007-01-13 23:04:31 +0100976 return 0;
977
malattia@linux.it56b87562007-04-09 10:19:05 +0200978 out_sysfs:
979 for (item = sony_nc_values; item->name; ++item) {
980 device_remove_file(&sony_pf_device->dev, &item->devattr);
981 }
982 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +0200983
Len Browna02d1c12007-02-07 15:34:02 -0500984 outbacklight:
Mattia Dongili7df03b82007-01-13 23:04:41 +0100985 if (sony_backlight_device)
986 backlight_device_unregister(sony_backlight_device);
987
malattia@linux.it59b19102007-04-09 10:19:04 +0200988 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
Stelian Popc5611622007-01-13 23:04:37 +0100989 ACPI_DEVICE_NOTIFY,
990 sony_acpi_notify);
991 if (ACPI_FAILURE(status))
malattia@linux.itf6119b02007-04-09 19:31:06 +0200992 printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
malattia@linux.it1549ee62007-04-09 10:19:08 +0200993
994 outinput:
995 sony_laptop_remove_input();
996
Len Browna02d1c12007-02-07 15:34:02 -0500997 outwalk:
Stelian Pop7f09c432007-01-13 23:04:31 +0100998 return result;
999}
1000
malattia@linux.it59b19102007-04-09 10:19:04 +02001001static int sony_nc_remove(struct acpi_device *device, int type)
Stelian Pop7f09c432007-01-13 23:04:31 +01001002{
1003 acpi_status status;
malattia@linux.it56b87562007-04-09 10:19:05 +02001004 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01001005
Alessandro Guido50f62af2007-01-13 23:04:34 +01001006 if (sony_backlight_device)
1007 backlight_device_unregister(sony_backlight_device);
1008
malattia@linux.it59b19102007-04-09 10:19:04 +02001009 sony_nc_acpi_device = NULL;
Stelian Popc5611622007-01-13 23:04:37 +01001010
malattia@linux.it59b19102007-04-09 10:19:04 +02001011 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
Stelian Popc5611622007-01-13 23:04:37 +01001012 ACPI_DEVICE_NOTIFY,
1013 sony_acpi_notify);
1014 if (ACPI_FAILURE(status))
malattia@linux.itf6119b02007-04-09 19:31:06 +02001015 printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001016
malattia@linux.it56b87562007-04-09 10:19:05 +02001017 for (item = sony_nc_values; item->name; ++item) {
1018 device_remove_file(&sony_pf_device->dev, &item->devattr);
1019 }
1020
1021 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +02001022 sony_laptop_remove_input();
malattia@linux.itf6119b02007-04-09 19:31:06 +02001023 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001024
1025 return 0;
1026}
1027
malattia@linux.it59b19102007-04-09 10:19:04 +02001028static struct acpi_driver sony_nc_driver = {
1029 .name = SONY_NC_DRIVER_NAME,
1030 .class = SONY_NC_CLASS,
1031 .ids = SONY_NC_HID,
malattia@linux.it33a04452007-04-09 19:26:03 +02001032 .owner = THIS_MODULE,
Len Browna02d1c12007-02-07 15:34:02 -05001033 .ops = {
malattia@linux.it59b19102007-04-09 10:19:04 +02001034 .add = sony_nc_add,
1035 .remove = sony_nc_remove,
1036 .resume = sony_nc_resume,
Len Browna02d1c12007-02-07 15:34:02 -05001037 },
Andrew Morton3f4f4612007-01-13 23:04:32 +01001038};
1039
malattia@linux.it33a04452007-04-09 19:26:03 +02001040/*********** SPIC (SNY6001) Device ***********/
1041
1042#define SONYPI_DEVICE_TYPE1 0x00000001
1043#define SONYPI_DEVICE_TYPE2 0x00000002
1044#define SONYPI_DEVICE_TYPE3 0x00000004
1045
malattia@linux.it33a04452007-04-09 19:26:03 +02001046#define SONY_PIC_EV_MASK 0xff
1047
malattia@linux.it33a04452007-04-09 19:26:03 +02001048struct sony_pic_ioport {
1049 struct acpi_resource_io io;
1050 struct list_head list;
1051};
1052
1053struct sony_pic_irq {
1054 struct acpi_resource_irq irq;
1055 struct list_head list;
1056};
1057
1058struct sony_pic_dev {
1059 int model;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001060 u8 camera_power;
1061 u8 bluetooth_power;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09001062 u8 wwan_power;
malattia@linux.it33a04452007-04-09 19:26:03 +02001063 struct acpi_device *acpi_dev;
1064 struct sony_pic_irq *cur_irq;
1065 struct sony_pic_ioport *cur_ioport;
1066 struct list_head interrupts;
1067 struct list_head ioports;
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001068 struct mutex lock;
malattia@linux.it33a04452007-04-09 19:26:03 +02001069};
1070
1071static struct sony_pic_dev spic_dev = {
1072 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
1073 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
1074};
1075
1076/* Event masks */
1077#define SONYPI_JOGGER_MASK 0x00000001
1078#define SONYPI_CAPTURE_MASK 0x00000002
1079#define SONYPI_FNKEY_MASK 0x00000004
1080#define SONYPI_BLUETOOTH_MASK 0x00000008
1081#define SONYPI_PKEY_MASK 0x00000010
1082#define SONYPI_BACK_MASK 0x00000020
1083#define SONYPI_HELP_MASK 0x00000040
1084#define SONYPI_LID_MASK 0x00000080
1085#define SONYPI_ZOOM_MASK 0x00000100
1086#define SONYPI_THUMBPHRASE_MASK 0x00000200
1087#define SONYPI_MEYE_MASK 0x00000400
1088#define SONYPI_MEMORYSTICK_MASK 0x00000800
1089#define SONYPI_BATTERY_MASK 0x00001000
1090#define SONYPI_WIRELESS_MASK 0x00002000
1091
1092struct sonypi_event {
1093 u8 data;
1094 u8 event;
1095};
1096
1097/* The set of possible button release events */
1098static struct sonypi_event sonypi_releaseev[] = {
1099 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
1100 { 0, 0 }
1101};
1102
1103/* The set of possible jogger events */
1104static struct sonypi_event sonypi_joggerev[] = {
1105 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
1106 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
1107 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
1108 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
1109 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
1110 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
1111 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
1112 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
1113 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
1114 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
1115 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
1116 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
1117 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
1118 { 0, 0 }
1119};
1120
1121/* The set of possible capture button events */
1122static struct sonypi_event sonypi_captureev[] = {
1123 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
1124 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
1125 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
1126 { 0, 0 }
1127};
1128
1129/* The set of possible fnkeys events */
1130static struct sonypi_event sonypi_fnkeyev[] = {
1131 { 0x10, SONYPI_EVENT_FNKEY_ESC },
1132 { 0x11, SONYPI_EVENT_FNKEY_F1 },
1133 { 0x12, SONYPI_EVENT_FNKEY_F2 },
1134 { 0x13, SONYPI_EVENT_FNKEY_F3 },
1135 { 0x14, SONYPI_EVENT_FNKEY_F4 },
1136 { 0x15, SONYPI_EVENT_FNKEY_F5 },
1137 { 0x16, SONYPI_EVENT_FNKEY_F6 },
1138 { 0x17, SONYPI_EVENT_FNKEY_F7 },
1139 { 0x18, SONYPI_EVENT_FNKEY_F8 },
1140 { 0x19, SONYPI_EVENT_FNKEY_F9 },
1141 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
1142 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
1143 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
1144 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
1145 { 0x21, SONYPI_EVENT_FNKEY_1 },
1146 { 0x22, SONYPI_EVENT_FNKEY_2 },
1147 { 0x31, SONYPI_EVENT_FNKEY_D },
1148 { 0x32, SONYPI_EVENT_FNKEY_E },
1149 { 0x33, SONYPI_EVENT_FNKEY_F },
1150 { 0x34, SONYPI_EVENT_FNKEY_S },
1151 { 0x35, SONYPI_EVENT_FNKEY_B },
1152 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
1153 { 0, 0 }
1154};
1155
1156/* The set of possible program key events */
1157static struct sonypi_event sonypi_pkeyev[] = {
1158 { 0x01, SONYPI_EVENT_PKEY_P1 },
1159 { 0x02, SONYPI_EVENT_PKEY_P2 },
1160 { 0x04, SONYPI_EVENT_PKEY_P3 },
1161 { 0x5c, SONYPI_EVENT_PKEY_P1 },
1162 { 0, 0 }
1163};
1164
1165/* The set of possible bluetooth events */
1166static struct sonypi_event sonypi_blueev[] = {
1167 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
1168 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
1169 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
1170 { 0, 0 }
1171};
1172
1173/* The set of possible wireless events */
1174static struct sonypi_event sonypi_wlessev[] = {
1175 { 0x59, SONYPI_EVENT_WIRELESS_ON },
1176 { 0x5a, SONYPI_EVENT_WIRELESS_OFF },
1177 { 0, 0 }
1178};
1179
1180/* The set of possible back button events */
1181static struct sonypi_event sonypi_backev[] = {
1182 { 0x20, SONYPI_EVENT_BACK_PRESSED },
1183 { 0, 0 }
1184};
1185
1186/* The set of possible help button events */
1187static struct sonypi_event sonypi_helpev[] = {
1188 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
1189 { 0, 0 }
1190};
1191
1192
1193/* The set of possible lid events */
1194static struct sonypi_event sonypi_lidev[] = {
1195 { 0x51, SONYPI_EVENT_LID_CLOSED },
1196 { 0x50, SONYPI_EVENT_LID_OPENED },
1197 { 0, 0 }
1198};
1199
1200/* The set of possible zoom events */
1201static struct sonypi_event sonypi_zoomev[] = {
1202 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
1203 { 0, 0 }
1204};
1205
1206/* The set of possible thumbphrase events */
1207static struct sonypi_event sonypi_thumbphraseev[] = {
1208 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
1209 { 0, 0 }
1210};
1211
1212/* The set of possible motioneye camera events */
1213static struct sonypi_event sonypi_meyeev[] = {
1214 { 0x00, SONYPI_EVENT_MEYE_FACE },
1215 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
1216 { 0, 0 }
1217};
1218
1219/* The set of possible memorystick events */
1220static struct sonypi_event sonypi_memorystickev[] = {
1221 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
1222 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
1223 { 0, 0 }
1224};
1225
1226/* The set of possible battery events */
1227static struct sonypi_event sonypi_batteryev[] = {
1228 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
1229 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
1230 { 0, 0 }
1231};
1232
1233static struct sonypi_eventtypes {
1234 int model;
1235 u8 data;
1236 unsigned long mask;
1237 struct sonypi_event * events;
1238} sony_pic_eventtypes[] = {
1239 { SONYPI_DEVICE_TYPE1, 0, 0xffffffff, sonypi_releaseev },
1240 { SONYPI_DEVICE_TYPE1, 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
1241 { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_LID_MASK, sonypi_lidev },
1242 { SONYPI_DEVICE_TYPE1, 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
1243 { SONYPI_DEVICE_TYPE1, 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
1244 { SONYPI_DEVICE_TYPE1, 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1245 { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1246 { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
1247 { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1248 { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
1249
1250 { SONYPI_DEVICE_TYPE2, 0, 0xffffffff, sonypi_releaseev },
1251 { SONYPI_DEVICE_TYPE2, 0x38, SONYPI_LID_MASK, sonypi_lidev },
1252 { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
1253 { SONYPI_DEVICE_TYPE2, 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
1254 { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1255 { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1256 { SONYPI_DEVICE_TYPE2, 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
1257 { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_BACK_MASK, sonypi_backev },
1258 { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_HELP_MASK, sonypi_helpev },
1259 { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
1260 { SONYPI_DEVICE_TYPE2, 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
1261 { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1262 { SONYPI_DEVICE_TYPE2, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1263 { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1264
1265 { SONYPI_DEVICE_TYPE3, 0, 0xffffffff, sonypi_releaseev },
1266 { SONYPI_DEVICE_TYPE3, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1267 { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
1268 { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1269 { SONYPI_DEVICE_TYPE3, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1270 { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1271 { 0 }
1272};
1273
1274static int sony_pic_detect_device_type(void)
1275{
1276 struct pci_dev *pcidev;
1277 int model = 0;
1278
1279 if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1280 PCI_DEVICE_ID_INTEL_82371AB_3, NULL)))
1281 model = SONYPI_DEVICE_TYPE1;
1282
1283 else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1284 PCI_DEVICE_ID_INTEL_ICH6_1, NULL)))
1285 model = SONYPI_DEVICE_TYPE3;
1286
1287 else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1288 PCI_DEVICE_ID_INTEL_ICH7_1, NULL)))
1289 model = SONYPI_DEVICE_TYPE3;
1290
1291 else
1292 model = SONYPI_DEVICE_TYPE2;
1293
1294 if (pcidev)
1295 pci_dev_put(pcidev);
1296
1297 printk(KERN_INFO DRV_PFX "detected Type%d model\n",
1298 model == SONYPI_DEVICE_TYPE1 ? 1 :
1299 model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
1300 return model;
1301}
1302
1303#define ITERATIONS_LONG 10000
1304#define ITERATIONS_SHORT 10
1305#define wait_on_command(command, iterations) { \
1306 unsigned int n = iterations; \
1307 while (--n && (command)) \
1308 udelay(1); \
1309 if (!n) \
1310 dprintk("command failed at %s : %s (line %d)\n", \
1311 __FILE__, __FUNCTION__, __LINE__); \
1312}
1313
1314static u8 sony_pic_call1(u8 dev)
1315{
1316 u8 v1, v2;
1317
1318 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1319 ITERATIONS_LONG);
1320 outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1321 v1 = inb_p(spic_dev.cur_ioport->io.minimum + 4);
1322 v2 = inb_p(spic_dev.cur_ioport->io.minimum);
1323 dprintk("sony_pic_call1: 0x%.4x\n", (v2 << 8) | v1);
1324 return v2;
1325}
1326
1327static u8 sony_pic_call2(u8 dev, u8 fn)
1328{
1329 u8 v1;
1330
1331 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1332 ITERATIONS_LONG);
1333 outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1334 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1335 ITERATIONS_LONG);
1336 outb(fn, spic_dev.cur_ioport->io.minimum);
1337 v1 = inb_p(spic_dev.cur_ioport->io.minimum);
1338 dprintk("sony_pic_call2: 0x%.4x\n", v1);
1339 return v1;
1340}
1341
malattia@linux.it49a11de2007-04-09 19:28:56 +02001342static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
1343{
1344 u8 v1;
1345
1346 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
1347 outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1348 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
1349 outb(fn, spic_dev.cur_ioport->io.minimum);
1350 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
1351 outb(v, spic_dev.cur_ioport->io.minimum);
1352 v1 = inb_p(spic_dev.cur_ioport->io.minimum);
1353 dprintk("sony_pic_call3: 0x%.4x\n", v1);
1354 return v1;
1355}
1356
1357/* camera tests and poweron/poweroff */
1358#define SONYPI_CAMERA_PICTURE 5
malattia@linux.it49a11de2007-04-09 19:28:56 +02001359#define SONYPI_CAMERA_CONTROL 0x10
malattia@linux.ite3646322007-04-28 23:34:22 +09001360
1361#define SONYPI_CAMERA_BRIGHTNESS 0
1362#define SONYPI_CAMERA_CONTRAST 1
1363#define SONYPI_CAMERA_HUE 2
1364#define SONYPI_CAMERA_COLOR 3
1365#define SONYPI_CAMERA_SHARPNESS 4
1366
1367#define SONYPI_CAMERA_EXPOSURE_MASK 0xC
1368#define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
1369#define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
1370#define SONYPI_CAMERA_MUTE_MASK 0x40
1371
1372/* the rest don't need a loop until not 0xff */
1373#define SONYPI_CAMERA_AGC 6
1374#define SONYPI_CAMERA_AGC_MASK 0x30
1375#define SONYPI_CAMERA_SHUTTER_MASK 0x7
1376
1377#define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
1378#define SONYPI_CAMERA_CONTROL 0x10
1379
1380#define SONYPI_CAMERA_STATUS 7
1381#define SONYPI_CAMERA_STATUS_READY 0x2
1382#define SONYPI_CAMERA_STATUS_POSITION 0x4
1383
1384#define SONYPI_DIRECTION_BACKWARDS 0x4
1385
1386#define SONYPI_CAMERA_REVISION 8
1387#define SONYPI_CAMERA_ROMVERSION 9
malattia@linux.it49a11de2007-04-09 19:28:56 +02001388
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001389static int __sony_pic_camera_ready(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001390{
1391 u8 v;
1392
1393 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
1394 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
1395}
1396
malattia@linux.ite3646322007-04-28 23:34:22 +09001397static int __sony_pic_camera_off(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001398{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001399 if (!camera) {
1400 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1401 return -ENODEV;
1402 }
1403
malattia@linux.it49a11de2007-04-09 19:28:56 +02001404 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
1405 SONYPI_CAMERA_MUTE_MASK),
1406 ITERATIONS_SHORT);
1407
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001408 if (spic_dev.camera_power) {
1409 sony_pic_call2(0x91, 0);
1410 spic_dev.camera_power = 0;
1411 }
1412 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001413}
1414
malattia@linux.ite3646322007-04-28 23:34:22 +09001415static int __sony_pic_camera_on(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001416{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001417 int i, j, x;
1418
1419 if (!camera) {
1420 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1421 return -ENODEV;
1422 }
malattia@linux.it49a11de2007-04-09 19:28:56 +02001423
1424 if (spic_dev.camera_power)
malattia@linux.ite3646322007-04-28 23:34:22 +09001425 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001426
1427 for (j = 5; j > 0; j--) {
1428
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001429 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001430 msleep(10);
1431 sony_pic_call1(0x93);
1432
1433 for (i = 400; i > 0; i--) {
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001434 if (__sony_pic_camera_ready())
malattia@linux.it49a11de2007-04-09 19:28:56 +02001435 break;
1436 msleep(10);
1437 }
1438 if (i)
1439 break;
1440 }
1441
1442 if (j == 0) {
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001443 printk(KERN_WARNING DRV_PFX "failed to power on camera\n");
malattia@linux.ite3646322007-04-28 23:34:22 +09001444 return -ENODEV;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001445 }
1446
1447 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
1448 0x5a),
1449 ITERATIONS_SHORT);
1450
1451 spic_dev.camera_power = 1;
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001452 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001453}
1454
malattia@linux.ite3646322007-04-28 23:34:22 +09001455/* External camera command (exported to the motion eye v4l driver) */
1456int sony_pic_camera_command(int command, u8 value)
1457{
1458 if (!camera)
1459 return -EIO;
1460
1461 mutex_lock(&spic_dev.lock);
1462
1463 switch (command) {
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001464 case SONY_PIC_COMMAND_SETCAMERA:
malattia@linux.ite3646322007-04-28 23:34:22 +09001465 if (value)
1466 __sony_pic_camera_on();
1467 else
1468 __sony_pic_camera_off();
1469 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001470 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09001471 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
1472 ITERATIONS_SHORT);
1473 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001474 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
malattia@linux.ite3646322007-04-28 23:34:22 +09001475 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
1476 ITERATIONS_SHORT);
1477 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001478 case SONY_PIC_COMMAND_SETCAMERAHUE:
malattia@linux.ite3646322007-04-28 23:34:22 +09001479 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
1480 ITERATIONS_SHORT);
1481 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001482 case SONY_PIC_COMMAND_SETCAMERACOLOR:
malattia@linux.ite3646322007-04-28 23:34:22 +09001483 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
1484 ITERATIONS_SHORT);
1485 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001486 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09001487 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
1488 ITERATIONS_SHORT);
1489 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001490 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
malattia@linux.ite3646322007-04-28 23:34:22 +09001491 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
1492 ITERATIONS_SHORT);
1493 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001494 case SONY_PIC_COMMAND_SETCAMERAAGC:
malattia@linux.ite3646322007-04-28 23:34:22 +09001495 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
1496 ITERATIONS_SHORT);
1497 break;
1498 default:
1499 printk(KERN_ERR DRV_PFX "sony_pic_camera_command invalid: %d\n",
1500 command);
1501 break;
1502 }
1503 mutex_unlock(&spic_dev.lock);
1504 return 0;
1505}
1506EXPORT_SYMBOL(sony_pic_camera_command);
1507
malattia@linux.it9476cdf2007-04-28 23:21:42 +09001508/* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
1509static void sony_pic_set_wwanpower(u8 state)
1510{
1511 state = !!state;
1512 mutex_lock(&spic_dev.lock);
1513 if (spic_dev.wwan_power == state) {
1514 mutex_unlock(&spic_dev.lock);
1515 return;
1516 }
1517 sony_pic_call2(0xB0, state);
1518 spic_dev.wwan_power = state;
1519 mutex_unlock(&spic_dev.lock);
1520}
1521
1522static ssize_t sony_pic_wwanpower_store(struct device *dev,
1523 struct device_attribute *attr,
1524 const char *buffer, size_t count)
1525{
1526 unsigned long value;
1527 if (count > 31)
1528 return -EINVAL;
1529
1530 value = simple_strtoul(buffer, NULL, 10);
1531 sony_pic_set_wwanpower(value);
1532
1533 return count;
1534}
1535
1536static ssize_t sony_pic_wwanpower_show(struct device *dev,
1537 struct device_attribute *attr, char *buffer)
1538{
1539 ssize_t count;
1540 mutex_lock(&spic_dev.lock);
1541 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
1542 mutex_unlock(&spic_dev.lock);
1543 return count;
1544}
1545
malattia@linux.it49a11de2007-04-09 19:28:56 +02001546/* bluetooth subsystem power state */
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001547static void __sony_pic_set_bluetoothpower(u8 state)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001548{
1549 state = !!state;
1550 if (spic_dev.bluetooth_power == state)
1551 return;
1552 sony_pic_call2(0x96, state);
1553 sony_pic_call1(0x82);
1554 spic_dev.bluetooth_power = state;
1555}
1556
1557static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
1558 struct device_attribute *attr,
1559 const char *buffer, size_t count)
1560{
1561 unsigned long value;
1562 if (count > 31)
1563 return -EINVAL;
1564
1565 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001566 mutex_lock(&spic_dev.lock);
1567 __sony_pic_set_bluetoothpower(value);
1568 mutex_unlock(&spic_dev.lock);
malattia@linux.it49a11de2007-04-09 19:28:56 +02001569
1570 return count;
1571}
1572
1573static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
1574 struct device_attribute *attr, char *buffer)
1575{
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001576 ssize_t count = 0;
1577 mutex_lock(&spic_dev.lock);
1578 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
1579 mutex_unlock(&spic_dev.lock);
1580 return count;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001581}
1582
1583/* fan speed */
1584/* FAN0 information (reverse engineered from ACPI tables) */
1585#define SONY_PIC_FAN0_STATUS 0x93
malattia@linux.it7b153f32007-04-09 19:31:25 +02001586static int sony_pic_set_fanspeed(unsigned long value)
1587{
1588 return ec_write(SONY_PIC_FAN0_STATUS, value);
1589}
1590
1591static int sony_pic_get_fanspeed(u8 *value)
1592{
1593 return ec_read(SONY_PIC_FAN0_STATUS, value);
1594}
1595
malattia@linux.it49a11de2007-04-09 19:28:56 +02001596static ssize_t sony_pic_fanspeed_store(struct device *dev,
1597 struct device_attribute *attr,
1598 const char *buffer, size_t count)
1599{
1600 unsigned long value;
1601 if (count > 31)
1602 return -EINVAL;
1603
1604 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it7b153f32007-04-09 19:31:25 +02001605 if (sony_pic_set_fanspeed(value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02001606 return -EIO;
1607
1608 return count;
1609}
1610
1611static ssize_t sony_pic_fanspeed_show(struct device *dev,
1612 struct device_attribute *attr, char *buffer)
1613{
1614 u8 value = 0;
malattia@linux.it7b153f32007-04-09 19:31:25 +02001615 if (sony_pic_get_fanspeed(&value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02001616 return -EIO;
1617
1618 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
1619}
1620
1621#define SPIC_ATTR(_name, _mode) \
1622struct device_attribute spic_attr_##_name = __ATTR(_name, \
1623 _mode, sony_pic_## _name ##_show, \
1624 sony_pic_## _name ##_store)
1625
malattia@linux.it49a11de2007-04-09 19:28:56 +02001626static SPIC_ATTR(bluetoothpower, 0644);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09001627static SPIC_ATTR(wwanpower, 0644);
malattia@linux.it49a11de2007-04-09 19:28:56 +02001628static SPIC_ATTR(fanspeed, 0644);
1629
1630static struct attribute *spic_attributes[] = {
malattia@linux.it49a11de2007-04-09 19:28:56 +02001631 &spic_attr_bluetoothpower.attr,
malattia@linux.it9476cdf2007-04-28 23:21:42 +09001632 &spic_attr_wwanpower.attr,
malattia@linux.it49a11de2007-04-09 19:28:56 +02001633 &spic_attr_fanspeed.attr,
1634 NULL
1635};
1636
1637static struct attribute_group spic_attribute_group = {
1638 .attrs = spic_attributes
1639};
1640
malattia@linux.it7b153f32007-04-09 19:31:25 +02001641/******** SONYPI compatibility **********/
Mattia Dongilia64e62a2007-05-01 11:19:53 +09001642#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +02001643
1644/* battery / brightness / temperature addresses */
1645#define SONYPI_BAT_FLAGS 0x81
1646#define SONYPI_LCD_LIGHT 0x96
1647#define SONYPI_BAT1_PCTRM 0xa0
1648#define SONYPI_BAT1_LEFT 0xa2
1649#define SONYPI_BAT1_MAXRT 0xa4
1650#define SONYPI_BAT2_PCTRM 0xa8
1651#define SONYPI_BAT2_LEFT 0xaa
1652#define SONYPI_BAT2_MAXRT 0xac
1653#define SONYPI_BAT1_MAXTK 0xb0
1654#define SONYPI_BAT1_FULL 0xb2
1655#define SONYPI_BAT2_MAXTK 0xb8
1656#define SONYPI_BAT2_FULL 0xba
1657#define SONYPI_TEMP_STATUS 0xC1
1658
1659struct sonypi_compat_s {
1660 struct fasync_struct *fifo_async;
1661 struct kfifo *fifo;
1662 spinlock_t fifo_lock;
1663 wait_queue_head_t fifo_proc_list;
1664 atomic_t open_count;
1665};
1666static struct sonypi_compat_s sonypi_compat = {
1667 .open_count = ATOMIC_INIT(0),
1668};
1669
1670static int sonypi_misc_fasync(int fd, struct file *filp, int on)
1671{
1672 int retval;
1673
1674 retval = fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
1675 if (retval < 0)
1676 return retval;
1677 return 0;
1678}
1679
1680static int sonypi_misc_release(struct inode *inode, struct file *file)
1681{
1682 sonypi_misc_fasync(-1, file, 0);
1683 atomic_dec(&sonypi_compat.open_count);
1684 return 0;
1685}
1686
1687static int sonypi_misc_open(struct inode *inode, struct file *file)
1688{
1689 /* Flush input queue on first open */
1690 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
1691 kfifo_reset(sonypi_compat.fifo);
1692 return 0;
1693}
1694
1695static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
1696 size_t count, loff_t *pos)
1697{
1698 ssize_t ret;
1699 unsigned char c;
1700
1701 if ((kfifo_len(sonypi_compat.fifo) == 0) &&
1702 (file->f_flags & O_NONBLOCK))
1703 return -EAGAIN;
1704
1705 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
1706 kfifo_len(sonypi_compat.fifo) != 0);
1707 if (ret)
1708 return ret;
1709
1710 while (ret < count &&
1711 (kfifo_get(sonypi_compat.fifo, &c, sizeof(c)) == sizeof(c))) {
1712 if (put_user(c, buf++))
1713 return -EFAULT;
1714 ret++;
1715 }
1716
1717 if (ret > 0) {
1718 struct inode *inode = file->f_path.dentry->d_inode;
1719 inode->i_atime = current_fs_time(inode->i_sb);
1720 }
1721
1722 return ret;
1723}
1724
1725static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
1726{
1727 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
1728 if (kfifo_len(sonypi_compat.fifo))
1729 return POLLIN | POLLRDNORM;
1730 return 0;
1731}
1732
1733static int ec_read16(u8 addr, u16 *value)
1734{
1735 u8 val_lb, val_hb;
1736 if (ec_read(addr, &val_lb))
1737 return -1;
1738 if (ec_read(addr + 1, &val_hb))
1739 return -1;
1740 *value = val_lb | (val_hb << 8);
1741 return 0;
1742}
1743
1744static int sonypi_misc_ioctl(struct inode *ip, struct file *fp,
1745 unsigned int cmd, unsigned long arg)
1746{
1747 int ret = 0;
1748 void __user *argp = (void __user *)arg;
1749 u8 val8;
1750 u16 val16;
1751 int value;
1752
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001753 mutex_lock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02001754 switch (cmd) {
1755 case SONYPI_IOCGBRT:
1756 if (sony_backlight_device == NULL) {
1757 ret = -EIO;
1758 break;
1759 }
1760 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
1761 ret = -EIO;
1762 break;
1763 }
1764 val8 = ((value & 0xff) - 1) << 5;
1765 if (copy_to_user(argp, &val8, sizeof(val8)))
1766 ret = -EFAULT;
1767 break;
1768 case SONYPI_IOCSBRT:
1769 if (sony_backlight_device == NULL) {
1770 ret = -EIO;
1771 break;
1772 }
1773 if (copy_from_user(&val8, argp, sizeof(val8))) {
1774 ret = -EFAULT;
1775 break;
1776 }
1777 if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
1778 (val8 >> 5) + 1, NULL)) {
1779 ret = -EIO;
1780 break;
1781 }
1782 /* sync the backlight device status */
1783 sony_backlight_device->props.brightness =
1784 sony_backlight_get_brightness(sony_backlight_device);
1785 break;
1786 case SONYPI_IOCGBAT1CAP:
1787 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
1788 ret = -EIO;
1789 break;
1790 }
1791 if (copy_to_user(argp, &val16, sizeof(val16)))
1792 ret = -EFAULT;
1793 break;
1794 case SONYPI_IOCGBAT1REM:
1795 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
1796 ret = -EIO;
1797 break;
1798 }
1799 if (copy_to_user(argp, &val16, sizeof(val16)))
1800 ret = -EFAULT;
1801 break;
1802 case SONYPI_IOCGBAT2CAP:
1803 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
1804 ret = -EIO;
1805 break;
1806 }
1807 if (copy_to_user(argp, &val16, sizeof(val16)))
1808 ret = -EFAULT;
1809 break;
1810 case SONYPI_IOCGBAT2REM:
1811 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
1812 ret = -EIO;
1813 break;
1814 }
1815 if (copy_to_user(argp, &val16, sizeof(val16)))
1816 ret = -EFAULT;
1817 break;
1818 case SONYPI_IOCGBATFLAGS:
1819 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
1820 ret = -EIO;
1821 break;
1822 }
1823 val8 &= 0x07;
1824 if (copy_to_user(argp, &val8, sizeof(val8)))
1825 ret = -EFAULT;
1826 break;
1827 case SONYPI_IOCGBLUE:
1828 val8 = spic_dev.bluetooth_power;
1829 if (copy_to_user(argp, &val8, sizeof(val8)))
1830 ret = -EFAULT;
1831 break;
1832 case SONYPI_IOCSBLUE:
1833 if (copy_from_user(&val8, argp, sizeof(val8))) {
1834 ret = -EFAULT;
1835 break;
1836 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001837 __sony_pic_set_bluetoothpower(val8);
malattia@linux.it7b153f32007-04-09 19:31:25 +02001838 break;
1839 /* FAN Controls */
1840 case SONYPI_IOCGFAN:
1841 if (sony_pic_get_fanspeed(&val8)) {
1842 ret = -EIO;
1843 break;
1844 }
1845 if (copy_to_user(argp, &val8, sizeof(val8)))
1846 ret = -EFAULT;
1847 break;
1848 case SONYPI_IOCSFAN:
1849 if (copy_from_user(&val8, argp, sizeof(val8))) {
1850 ret = -EFAULT;
1851 break;
1852 }
1853 if (sony_pic_set_fanspeed(val8))
1854 ret = -EIO;
1855 break;
1856 /* GET Temperature (useful under APM) */
1857 case SONYPI_IOCGTEMP:
1858 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
1859 ret = -EIO;
1860 break;
1861 }
1862 if (copy_to_user(argp, &val8, sizeof(val8)))
1863 ret = -EFAULT;
1864 break;
1865 default:
1866 ret = -EINVAL;
1867 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001868 mutex_unlock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02001869 return ret;
1870}
1871
1872static const struct file_operations sonypi_misc_fops = {
1873 .owner = THIS_MODULE,
1874 .read = sonypi_misc_read,
1875 .poll = sonypi_misc_poll,
1876 .open = sonypi_misc_open,
1877 .release = sonypi_misc_release,
1878 .fasync = sonypi_misc_fasync,
1879 .ioctl = sonypi_misc_ioctl,
1880};
1881
1882static struct miscdevice sonypi_misc_device = {
1883 .minor = MISC_DYNAMIC_MINOR,
1884 .name = "sonypi",
1885 .fops = &sonypi_misc_fops,
1886};
1887
1888static void sonypi_compat_report_event(u8 event)
1889{
1890 kfifo_put(sonypi_compat.fifo, (unsigned char *)&event, sizeof(event));
1891 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
1892 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
1893}
1894
1895static int sonypi_compat_init(void)
1896{
1897 int error;
1898
1899 spin_lock_init(&sonypi_compat.fifo_lock);
1900 sonypi_compat.fifo = kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
1901 &sonypi_compat.fifo_lock);
1902 if (IS_ERR(sonypi_compat.fifo)) {
1903 printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
1904 return PTR_ERR(sonypi_compat.fifo);
1905 }
1906
1907 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
malattia@linux.it7b153f32007-04-09 19:31:25 +02001908
1909 if (minor != -1)
1910 sonypi_misc_device.minor = minor;
1911 error = misc_register(&sonypi_misc_device);
1912 if (error) {
1913 printk(KERN_ERR DRV_PFX "misc_register failed\n");
1914 goto err_free_kfifo;
1915 }
1916 if (minor == -1)
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001917 printk(KERN_INFO DRV_PFX "device allocated minor is %d\n",
malattia@linux.it7b153f32007-04-09 19:31:25 +02001918 sonypi_misc_device.minor);
1919
1920 return 0;
1921
1922err_free_kfifo:
1923 kfifo_free(sonypi_compat.fifo);
1924 return error;
1925}
1926
1927static void sonypi_compat_exit(void)
1928{
1929 misc_deregister(&sonypi_misc_device);
1930 kfifo_free(sonypi_compat.fifo);
1931}
1932#else
1933static int sonypi_compat_init(void) { return 0; }
1934static void sonypi_compat_exit(void) { }
1935static void sonypi_compat_report_event(u8 event) { }
Mattia Dongilia64e62a2007-05-01 11:19:53 +09001936#endif /* CONFIG_SONYPI_COMPAT */
malattia@linux.it7b153f32007-04-09 19:31:25 +02001937
malattia@linux.it1549ee62007-04-09 10:19:08 +02001938/*
malattia@linux.it33a04452007-04-09 19:26:03 +02001939 * ACPI callbacks
malattia@linux.it1549ee62007-04-09 10:19:08 +02001940 */
malattia@linux.it33a04452007-04-09 19:26:03 +02001941static acpi_status
1942sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
1943{
1944 u32 i;
1945 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
1946
1947 switch (resource->type) {
1948 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
1949 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
1950 return AE_OK;
1951
1952 case ACPI_RESOURCE_TYPE_IRQ:
1953 {
1954 struct acpi_resource_irq *p = &resource->data.irq;
1955 struct sony_pic_irq *interrupt = NULL;
1956 if (!p || !p->interrupt_count) {
1957 /*
1958 * IRQ descriptors may have no IRQ# bits set,
1959 * particularly those those w/ _STA disabled
1960 */
1961 dprintk("Blank IRQ resource\n");
1962 return AE_OK;
1963 }
1964 for (i = 0; i < p->interrupt_count; i++) {
1965 if (!p->interrupts[i]) {
1966 printk(KERN_WARNING DRV_PFX
1967 "Invalid IRQ %d\n",
1968 p->interrupts[i]);
1969 continue;
1970 }
1971 interrupt = kzalloc(sizeof(*interrupt),
1972 GFP_KERNEL);
1973 if (!interrupt)
1974 return AE_ERROR;
1975
malattia@linux.it1a3e3232007-04-28 23:34:10 +09001976 list_add_tail(&interrupt->list, &dev->interrupts);
malattia@linux.it33a04452007-04-09 19:26:03 +02001977 interrupt->irq.triggering = p->triggering;
1978 interrupt->irq.polarity = p->polarity;
1979 interrupt->irq.sharable = p->sharable;
1980 interrupt->irq.interrupt_count = 1;
1981 interrupt->irq.interrupts[0] = p->interrupts[i];
1982 }
1983 return AE_OK;
1984 }
1985 case ACPI_RESOURCE_TYPE_IO:
1986 {
1987 struct acpi_resource_io *io = &resource->data.io;
1988 struct sony_pic_ioport *ioport = NULL;
1989 if (!io) {
1990 dprintk("Blank IO resource\n");
1991 return AE_OK;
1992 }
1993
1994 ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
1995 if (!ioport)
1996 return AE_ERROR;
1997
malattia@linux.it1a3e3232007-04-28 23:34:10 +09001998 list_add_tail(&ioport->list, &dev->ioports);
malattia@linux.it33a04452007-04-09 19:26:03 +02001999 memcpy(&ioport->io, io, sizeof(*io));
2000 return AE_OK;
2001 }
2002 default:
2003 dprintk("Resource %d isn't an IRQ nor an IO port\n",
2004 resource->type);
2005
2006 case ACPI_RESOURCE_TYPE_END_TAG:
2007 return AE_OK;
2008 }
2009 return AE_CTRL_TERMINATE;
2010}
2011
2012static int sony_pic_possible_resources(struct acpi_device *device)
2013{
2014 int result = 0;
2015 acpi_status status = AE_OK;
2016
2017 if (!device)
2018 return -EINVAL;
2019
2020 /* get device status */
2021 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
2022 dprintk("Evaluating _STA\n");
2023 result = acpi_bus_get_status(device);
2024 if (result) {
2025 printk(KERN_WARNING DRV_PFX "Unable to read status\n");
2026 goto end;
2027 }
2028
2029 if (!device->status.enabled)
2030 dprintk("Device disabled\n");
2031 else
2032 dprintk("Device enabled\n");
2033
2034 /*
2035 * Query and parse 'method'
2036 */
2037 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
2038 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
2039 sony_pic_read_possible_resource, &spic_dev);
2040 if (ACPI_FAILURE(status)) {
2041 printk(KERN_WARNING DRV_PFX
2042 "Failure evaluating %s\n",
2043 METHOD_NAME__PRS);
2044 result = -ENODEV;
2045 }
2046end:
2047 return result;
2048}
2049
2050/*
2051 * Disable the spic device by calling its _DIS method
2052 */
2053static int sony_pic_disable(struct acpi_device *device)
2054{
Randy Dunlapf3a740c2007-07-15 23:40:49 -07002055 if (ACPI_FAILURE(acpi_evaluate_object(device->handle,
2056 "_DIS", NULL, NULL)))
malattia@linux.it33a04452007-04-09 19:26:03 +02002057 return -ENXIO;
2058
2059 dprintk("Device disabled\n");
2060 return 0;
2061}
2062
2063
2064/*
2065 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
2066 *
2067 * Call _SRS to set current resources
2068 */
2069static int sony_pic_enable(struct acpi_device *device,
2070 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
2071{
2072 acpi_status status;
2073 int result = 0;
2074 struct {
2075 struct acpi_resource io_res;
2076 struct acpi_resource irq_res;
2077 struct acpi_resource end;
2078 } *resource;
2079 struct acpi_buffer buffer = { 0, NULL };
2080
2081 if (!ioport || !irq)
2082 return -EINVAL;
2083
2084 /* init acpi_buffer */
2085 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
2086 if (!resource)
2087 return -ENOMEM;
2088
2089 buffer.length = sizeof(*resource) + 1;
2090 buffer.pointer = resource;
2091
2092 /* setup io resource */
2093 resource->io_res.type = ACPI_RESOURCE_TYPE_IO;
2094 resource->io_res.length = sizeof(struct acpi_resource);
2095 memcpy(&resource->io_res.data.io, &ioport->io,
2096 sizeof(struct acpi_resource_io));
2097
2098 /* setup irq resource */
2099 resource->irq_res.type = ACPI_RESOURCE_TYPE_IRQ;
2100 resource->irq_res.length = sizeof(struct acpi_resource);
2101 memcpy(&resource->irq_res.data.irq, &irq->irq,
2102 sizeof(struct acpi_resource_irq));
2103 /* we requested a shared irq */
2104 resource->irq_res.data.irq.sharable = ACPI_SHARED;
2105
2106 resource->end.type = ACPI_RESOURCE_TYPE_END_TAG;
2107
2108 /* Attempt to set the resource */
2109 dprintk("Evaluating _SRS\n");
2110 status = acpi_set_current_resources(device->handle, &buffer);
2111
2112 /* check for total failure */
2113 if (ACPI_FAILURE(status)) {
2114 printk(KERN_ERR DRV_PFX "Error evaluating _SRS");
2115 result = -ENODEV;
2116 goto end;
2117 }
2118
2119 /* Necessary device initializations calls (from sonypi) */
2120 sony_pic_call1(0x82);
2121 sony_pic_call2(0x81, 0xff);
2122 sony_pic_call1(compat ? 0x92 : 0x82);
2123
2124end:
2125 kfree(resource);
2126 return result;
2127}
2128
2129/*****************
2130 *
2131 * ISR: some event is available
2132 *
2133 *****************/
2134static irqreturn_t sony_pic_irq(int irq, void *dev_id)
2135{
2136 int i, j;
2137 u32 port_val = 0;
2138 u8 ev = 0;
2139 u8 data_mask = 0;
2140 u8 device_event = 0;
2141
2142 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
2143
2144 acpi_os_read_port(dev->cur_ioport->io.minimum, &port_val,
2145 dev->cur_ioport->io.address_length);
2146 ev = port_val & SONY_PIC_EV_MASK;
2147 data_mask = 0xff & (port_val >> (dev->cur_ioport->io.address_length - 8));
2148
2149 dprintk("event (0x%.8x [%.2x] [%.2x]) at port 0x%.4x\n",
2150 port_val, ev, data_mask, dev->cur_ioport->io.minimum);
2151
2152 if (ev == 0x00 || ev == 0xff)
2153 return IRQ_HANDLED;
2154
2155 for (i = 0; sony_pic_eventtypes[i].model; i++) {
2156
2157 if (spic_dev.model != sony_pic_eventtypes[i].model)
2158 continue;
2159
2160 if ((data_mask & sony_pic_eventtypes[i].data) !=
2161 sony_pic_eventtypes[i].data)
2162 continue;
2163
2164 if (!(mask & sony_pic_eventtypes[i].mask))
2165 continue;
2166
2167 for (j = 0; sony_pic_eventtypes[i].events[j].event; j++) {
2168 if (ev == sony_pic_eventtypes[i].events[j].data) {
2169 device_event =
2170 sony_pic_eventtypes[i].events[j].event;
2171 goto found;
2172 }
2173 }
2174 }
2175 return IRQ_HANDLED;
2176
2177found:
malattia@linux.it1549ee62007-04-09 10:19:08 +02002178 sony_laptop_report_input_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02002179 acpi_bus_generate_event(spic_dev.acpi_dev, 1, device_event);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002180 sonypi_compat_report_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02002181
2182 return IRQ_HANDLED;
2183}
2184
2185/*****************
2186 *
2187 * ACPI driver
2188 *
2189 *****************/
2190static int sony_pic_remove(struct acpi_device *device, int type)
2191{
2192 struct sony_pic_ioport *io, *tmp_io;
2193 struct sony_pic_irq *irq, *tmp_irq;
2194
malattia@linux.it7b153f32007-04-09 19:31:25 +02002195 sonypi_compat_exit();
2196
malattia@linux.it33a04452007-04-09 19:26:03 +02002197 if (sony_pic_disable(device)) {
2198 printk(KERN_ERR DRV_PFX "Couldn't disable device.\n");
2199 return -ENXIO;
2200 }
2201
2202 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2203 release_region(spic_dev.cur_ioport->io.minimum,
2204 spic_dev.cur_ioport->io.address_length);
2205
malattia@linux.it1549ee62007-04-09 10:19:08 +02002206 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02002207
malattia@linux.it49a11de2007-04-09 19:28:56 +02002208 /* pf attrs */
2209 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2210 sony_pf_remove();
2211
malattia@linux.it33a04452007-04-09 19:26:03 +02002212 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
malattia@linux.itf6119b02007-04-09 19:31:06 +02002223 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002224 return 0;
2225}
2226
2227static int sony_pic_add(struct acpi_device *device)
2228{
2229 int result;
2230 struct sony_pic_ioport *io, *tmp_io;
2231 struct sony_pic_irq *irq, *tmp_irq;
2232
malattia@linux.itf6119b02007-04-09 19:31:06 +02002233 printk(KERN_INFO DRV_PFX "%s v%s.\n",
2234 SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.it33a04452007-04-09 19:26:03 +02002235
2236 spic_dev.acpi_dev = device;
2237 strcpy(acpi_device_class(device), "sony/hotkey");
2238 spic_dev.model = sony_pic_detect_device_type();
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002239 mutex_init(&spic_dev.lock);
malattia@linux.it33a04452007-04-09 19:26:03 +02002240
2241 /* read _PRS resources */
2242 result = sony_pic_possible_resources(device);
2243 if (result) {
2244 printk(KERN_ERR DRV_PFX
2245 "Unabe to read possible resources.\n");
2246 goto err_free_resources;
2247 }
2248
2249 /* setup input devices and helper fifo */
malattia@linux.it1549ee62007-04-09 10:19:08 +02002250 result = sony_laptop_setup_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02002251 if (result) {
2252 printk(KERN_ERR DRV_PFX
2253 "Unabe to create input devices.\n");
2254 goto err_free_resources;
2255 }
2256
2257 /* request io port */
2258 list_for_each_entry(io, &spic_dev.ioports, list) {
2259 if (request_region(io->io.minimum, io->io.address_length,
2260 "Sony Programable I/O Device")) {
2261 dprintk("I/O port: 0x%.4x (0x%.4x) + 0x%.2x\n",
2262 io->io.minimum, io->io.maximum,
2263 io->io.address_length);
2264 spic_dev.cur_ioport = io;
2265 break;
2266 }
2267 }
2268 if (!spic_dev.cur_ioport) {
2269 printk(KERN_ERR DRV_PFX "Failed to request_region.\n");
2270 result = -ENODEV;
2271 goto err_remove_input;
2272 }
2273
2274 /* request IRQ */
2275 list_for_each_entry(irq, &spic_dev.interrupts, list) {
2276 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
2277 IRQF_SHARED, "sony-laptop", &spic_dev)) {
2278 dprintk("IRQ: %d - triggering: %d - "
2279 "polarity: %d - shr: %d\n",
2280 irq->irq.interrupts[0],
2281 irq->irq.triggering,
2282 irq->irq.polarity,
2283 irq->irq.sharable);
2284 spic_dev.cur_irq = irq;
2285 break;
2286 }
2287 }
2288 if (!spic_dev.cur_irq) {
2289 printk(KERN_ERR DRV_PFX "Failed to request_irq.\n");
2290 result = -ENODEV;
2291 goto err_release_region;
2292 }
2293
2294 /* set resource status _SRS */
2295 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2296 if (result) {
2297 printk(KERN_ERR DRV_PFX "Couldn't enable device.\n");
2298 goto err_free_irq;
2299 }
2300
malattia@linux.it49a11de2007-04-09 19:28:56 +02002301 spic_dev.bluetooth_power = -1;
2302 /* create device attributes */
2303 result = sony_pf_add();
2304 if (result)
2305 goto err_disable_device;
2306
2307 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2308 if (result)
2309 goto err_remove_pf;
2310
malattia@linux.it7b153f32007-04-09 19:31:25 +02002311 if (sonypi_compat_init())
2312 goto err_remove_pf;
2313
malattia@linux.it33a04452007-04-09 19:26:03 +02002314 return 0;
2315
malattia@linux.it49a11de2007-04-09 19:28:56 +02002316err_remove_pf:
2317 sony_pf_remove();
2318
2319err_disable_device:
2320 sony_pic_disable(device);
2321
malattia@linux.it33a04452007-04-09 19:26:03 +02002322err_free_irq:
2323 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2324
2325err_release_region:
2326 release_region(spic_dev.cur_ioport->io.minimum,
2327 spic_dev.cur_ioport->io.address_length);
2328
2329err_remove_input:
malattia@linux.it1549ee62007-04-09 10:19:08 +02002330 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02002331
2332err_free_resources:
2333 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2334 list_del(&io->list);
2335 kfree(io);
2336 }
2337 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2338 list_del(&irq->list);
2339 kfree(irq);
2340 }
2341 spic_dev.cur_ioport = NULL;
2342 spic_dev.cur_irq = NULL;
2343
2344 return result;
2345}
2346
2347static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
2348{
2349 if (sony_pic_disable(device))
2350 return -ENXIO;
2351 return 0;
2352}
2353
2354static int sony_pic_resume(struct acpi_device *device)
2355{
2356 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2357 return 0;
2358}
2359
2360static struct acpi_driver sony_pic_driver = {
2361 .name = SONY_PIC_DRIVER_NAME,
2362 .class = SONY_PIC_CLASS,
2363 .ids = SONY_PIC_HID,
2364 .owner = THIS_MODULE,
2365 .ops = {
2366 .add = sony_pic_add,
2367 .remove = sony_pic_remove,
2368 .suspend = sony_pic_suspend,
2369 .resume = sony_pic_resume,
2370 },
2371};
2372
2373static struct dmi_system_id __initdata sonypi_dmi_table[] = {
2374 {
2375 .ident = "Sony Vaio",
2376 .matches = {
2377 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2378 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
2379 },
2380 },
2381 {
2382 .ident = "Sony Vaio",
2383 .matches = {
2384 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2385 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
2386 },
2387 },
2388 { }
2389};
2390
malattia@linux.it59b19102007-04-09 10:19:04 +02002391static int __init sony_laptop_init(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01002392{
malattia@linux.it33a04452007-04-09 19:26:03 +02002393 int result;
2394
2395 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
2396 result = acpi_bus_register_driver(&sony_pic_driver);
2397 if (result) {
2398 printk(KERN_ERR DRV_PFX
2399 "Unable to register SPIC driver.");
2400 goto out;
2401 }
2402 }
2403
2404 result = acpi_bus_register_driver(&sony_nc_driver);
2405 if (result) {
2406 printk(KERN_ERR DRV_PFX "Unable to register SNC driver.");
2407 goto out_unregister_pic;
2408 }
2409
2410 return 0;
2411
2412out_unregister_pic:
2413 if (!no_spic)
2414 acpi_bus_unregister_driver(&sony_pic_driver);
2415out:
2416 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +01002417}
2418
malattia@linux.it59b19102007-04-09 10:19:04 +02002419static void __exit sony_laptop_exit(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01002420{
malattia@linux.it59b19102007-04-09 10:19:04 +02002421 acpi_bus_unregister_driver(&sony_nc_driver);
malattia@linux.it33a04452007-04-09 19:26:03 +02002422 if (!no_spic)
2423 acpi_bus_unregister_driver(&sony_pic_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +01002424}
2425
malattia@linux.it59b19102007-04-09 10:19:04 +02002426module_init(sony_laptop_init);
2427module_exit(sony_laptop_exit);