allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / misc / sony-laptop.c
blobdeda486f0f093ce9c8563cdc63acd09a12ef589e
1 /*
2 * ACPI Sony Notebook Control Driver (SNC and SPIC)
4 * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
5 * Copyright (C) 2007 Mattia Dongili <malattia@linux.it>
7 * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
8 * which are copyrighted by their respective authors.
10 * The SNY6001 driver part is based on the sonypi driver which includes
11 * material from:
13 * Copyright (C) 2001-2005 Stelian Pop <stelian@popies.net>
15 * Copyright (C) 2005 Narayanan R S <nars@kadamba.org>
17 * Copyright (C) 2001-2002 AlcĂ´ve <www.alcove.com>
19 * Copyright (C) 2001 Michael Ashley <m.ashley@unsw.edu.au>
21 * Copyright (C) 2001 Junichi Morita <jun1m@mars.dti.ne.jp>
23 * Copyright (C) 2000 Takaya Kinjo <t-kinjo@tc4.so-net.ne.jp>
25 * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
27 * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
29 * 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.
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.
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.
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>
50 #include <linux/backlight.h>
51 #include <linux/platform_device.h>
52 #include <linux/err.h>
53 #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>
61 #include <acpi/acpi_drivers.h>
62 #include <acpi/acpi_bus.h>
63 #include <asm/uaccess.h>
64 #include <linux/sonypi.h>
65 #include <linux/sony-laptop.h>
66 #ifdef CONFIG_SONYPI_COMPAT
67 #include <linux/poll.h>
68 #include <linux/miscdevice.h>
69 #endif
71 #define DRV_PFX "sony-laptop: "
72 #define dprintk(msg...) do { \
73 if (debug) printk(KERN_WARNING DRV_PFX msg); \
74 } while (0)
76 #define SONY_LAPTOP_DRIVER_VERSION "0.5"
78 #define SONY_NC_CLASS "sony-nc"
79 #define SONY_NC_HID "SNY5001"
80 #define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
82 #define SONY_PIC_CLASS "sony-pic"
83 #define SONY_PIC_HID "SNY6001"
84 #define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
86 MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
87 MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
88 MODULE_LICENSE("GPL");
89 MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
91 static int debug;
92 module_param(debug, int, 0);
93 MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
94 "the development of this driver");
96 static int no_spic; /* = 0 */
97 module_param(no_spic, int, 0444);
98 MODULE_PARM_DESC(no_spic,
99 "set this if you don't want to enable the SPIC device");
101 static int compat; /* = 0 */
102 module_param(compat, int, 0444);
103 MODULE_PARM_DESC(compat,
104 "set this if you want to enable backward compatibility mode");
106 static unsigned long mask = 0xffffffff;
107 module_param(mask, ulong, 0644);
108 MODULE_PARM_DESC(mask,
109 "set this to the mask of event you want to enable (see doc)");
111 static int camera; /* = 0 */
112 module_param(camera, int, 0444);
113 MODULE_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)");
117 #ifdef CONFIG_SONYPI_COMPAT
118 static int minor = -1;
119 module_param(minor, int, 0);
120 MODULE_PARM_DESC(minor,
121 "minor number of the misc device for the SPIC compatibility code, "
122 "default is -1 (automatic)");
123 #endif
125 /*********** Input Devices ***********/
127 #define SONY_LAPTOP_BUF_SIZE 128
128 struct 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;
136 static struct sony_laptop_input_s sony_laptop_input = {
137 .users = ATOMIC_INIT(0),
140 struct sony_laptop_keypress {
141 struct input_dev *dev;
142 int key;
145 /* Correspondance table between sonypi events and input layer events */
146 static 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 },
184 /* release buttons after a short delay if pressed */
185 static void do_sony_laptop_release_key(struct work_struct *work)
187 struct sony_laptop_keypress kp;
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);
196 static DECLARE_WORK(sony_laptop_release_key_work,
197 do_sony_laptop_release_key);
199 /* forward event to the input subsytem */
200 static void sony_laptop_report_input_event(u8 event)
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;
207 if (event == SONYPI_EVENT_FNKEY_RELEASED) {
208 /* Nothing, not all VAIOs generate this event */
209 return;
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;
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;
227 /* key_dev events */
228 case SONYPI_EVENT_JOGDIAL_PRESSED:
229 kp.key = BTN_MIDDLE;
230 kp.dev = jog_dev;
231 break;
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;
240 break;
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));
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);
256 static int sony_laptop_setup_input(void)
258 struct input_dev *jog_dev;
259 struct input_dev *key_dev;
260 int i;
261 int error;
263 /* don't run again if already initialized */
264 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
265 return 0;
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;
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;
287 /* input keys */
288 key_dev = input_allocate_device();
289 if (!key_dev) {
290 error = -ENOMEM;
291 goto err_destroy_wq;
294 key_dev->name = "Sony Vaio Keys";
295 key_dev->id.bustype = BUS_ISA;
296 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
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);
305 error = input_register_device(key_dev);
306 if (error)
307 goto err_free_keydev;
309 sony_laptop_input.key_dev = key_dev;
311 /* jogdial */
312 jog_dev = input_allocate_device();
313 if (!jog_dev) {
314 error = -ENOMEM;
315 goto err_unregister_keydev;
318 jog_dev->name = "Sony Vaio Jogdial";
319 jog_dev->id.bustype = BUS_ISA;
320 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
322 jog_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
323 jog_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_MIDDLE);
324 jog_dev->relbit[0] = BIT_MASK(REL_WHEEL);
326 error = input_register_device(jog_dev);
327 if (error)
328 goto err_free_jogdev;
330 sony_laptop_input.jog_dev = jog_dev;
332 return 0;
334 err_free_jogdev:
335 input_free_device(jog_dev);
337 err_unregister_keydev:
338 input_unregister_device(key_dev);
339 /* to avoid kref underflow below at input_free_device */
340 key_dev = NULL;
342 err_free_keydev:
343 input_free_device(key_dev);
345 err_destroy_wq:
346 destroy_workqueue(sony_laptop_input.wq);
348 err_free_kfifo:
349 kfifo_free(sony_laptop_input.fifo);
351 err_dec_users:
352 atomic_dec(&sony_laptop_input.users);
353 return error;
356 static void sony_laptop_remove_input(void)
358 /* cleanup only after the last user has gone */
359 if (!atomic_dec_and_test(&sony_laptop_input.users))
360 return;
362 /* flush workqueue first */
363 flush_workqueue(sony_laptop_input.wq);
365 /* destroy input devs */
366 input_unregister_device(sony_laptop_input.key_dev);
367 sony_laptop_input.key_dev = NULL;
369 if (sony_laptop_input.jog_dev) {
370 input_unregister_device(sony_laptop_input.jog_dev);
371 sony_laptop_input.jog_dev = NULL;
374 destroy_workqueue(sony_laptop_input.wq);
375 kfifo_free(sony_laptop_input.fifo);
378 /*********** Platform Device ***********/
380 static atomic_t sony_pf_users = ATOMIC_INIT(0);
381 static struct platform_driver sony_pf_driver = {
382 .driver = {
383 .name = "sony-laptop",
384 .owner = THIS_MODULE,
387 static struct platform_device *sony_pf_device;
389 static int sony_pf_add(void)
391 int ret = 0;
393 /* don't run again if already initialized */
394 if (atomic_add_return(1, &sony_pf_users) > 1)
395 return 0;
397 ret = platform_driver_register(&sony_pf_driver);
398 if (ret)
399 goto out;
401 sony_pf_device = platform_device_alloc("sony-laptop", -1);
402 if (!sony_pf_device) {
403 ret = -ENOMEM;
404 goto out_platform_registered;
407 ret = platform_device_add(sony_pf_device);
408 if (ret)
409 goto out_platform_alloced;
411 return 0;
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;
423 static void sony_pf_remove(void)
425 /* deregister only after the last user has gone */
426 if (!atomic_dec_and_test(&sony_pf_users))
427 return;
429 platform_device_del(sony_pf_device);
430 platform_device_put(sony_pf_device);
431 platform_driver_unregister(&sony_pf_driver);
434 /*********** SNC (SNY5001) Device ***********/
436 /* the device uses 1-based values, while the backlight subsystem uses
437 0-based values */
438 #define SONY_MAX_BRIGHTNESS 8
440 #define SNC_VALIDATE_IN 0
441 #define SNC_VALIDATE_OUT 1
443 static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
444 char *);
445 static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
446 const char *, size_t);
447 static int boolean_validate(const int, const int);
448 static int brightness_default_validate(const int, const int);
450 struct sony_nc_value {
451 char *name; /* name of the entry */
452 char **acpiget; /* names of the ACPI get function */
453 char **acpiset; /* names of the ACPI set function */
454 int (*validate)(const int, const int); /* input/output validation */
455 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 */
461 #define SNC_HANDLE_NAMES(_name, _values...) \
462 static char *snc_##_name[] = { _values, NULL }
464 #define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
466 .name = __stringify(_name), \
467 .acpiget = _getters, \
468 .acpiset = _setters, \
469 .validate = _validate, \
470 .debug = _debug, \
471 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
474 #define SNC_HANDLE_NULL { .name = NULL }
476 SNC_HANDLE_NAMES(fnkey_get, "GHKE");
478 SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
479 SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
481 SNC_HANDLE_NAMES(cdpower_get, "GCDP");
482 SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
484 SNC_HANDLE_NAMES(audiopower_get, "GAZP");
485 SNC_HANDLE_NAMES(audiopower_set, "AZPW");
487 SNC_HANDLE_NAMES(lanpower_get, "GLNP");
488 SNC_HANDLE_NAMES(lanpower_set, "LNPW");
490 SNC_HANDLE_NAMES(PID_get, "GPID");
492 SNC_HANDLE_NAMES(CTR_get, "GCTR");
493 SNC_HANDLE_NAMES(CTR_set, "SCTR");
495 SNC_HANDLE_NAMES(PCR_get, "GPCR");
496 SNC_HANDLE_NAMES(PCR_set, "SPCR");
498 SNC_HANDLE_NAMES(CMI_get, "GCMI");
499 SNC_HANDLE_NAMES(CMI_set, "SCMI");
501 static struct sony_nc_value sony_nc_values[] = {
502 SNC_HANDLE(brightness_default, snc_brightness_def_get,
503 snc_brightness_def_set, brightness_default_validate, 0),
504 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
505 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
506 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
507 boolean_validate, 0),
508 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
509 boolean_validate, 1),
510 /* unknown methods */
511 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
512 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
513 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
514 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
515 SNC_HANDLE_NULL
518 static acpi_handle sony_nc_acpi_handle;
519 static struct acpi_device *sony_nc_acpi_device = NULL;
522 * acpi_evaluate_object wrappers
524 static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
526 struct acpi_buffer output;
527 union acpi_object out_obj;
528 acpi_status status;
530 output.length = sizeof(out_obj);
531 output.pointer = &out_obj;
533 status = acpi_evaluate_object(handle, name, NULL, &output);
534 if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
535 *result = out_obj.integer.value;
536 return 0;
539 printk(KERN_WARNING DRV_PFX "acpi_callreadfunc failed\n");
541 return -1;
544 static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
545 int *result)
547 struct acpi_object_list params;
548 union acpi_object in_obj;
549 struct acpi_buffer output;
550 union acpi_object out_obj;
551 acpi_status status;
553 params.count = 1;
554 params.pointer = &in_obj;
555 in_obj.type = ACPI_TYPE_INTEGER;
556 in_obj.integer.value = value;
558 output.length = sizeof(out_obj);
559 output.pointer = &out_obj;
561 status = acpi_evaluate_object(handle, name, &params, &output);
562 if (status == AE_OK) {
563 if (result != NULL) {
564 if (out_obj.type != ACPI_TYPE_INTEGER) {
565 printk(KERN_WARNING DRV_PFX "acpi_evaluate_object bad "
566 "return type\n");
567 return -1;
569 *result = out_obj.integer.value;
571 return 0;
574 printk(KERN_WARNING DRV_PFX "acpi_evaluate_object failed\n");
576 return -1;
580 * sony_nc_values input/output validate functions
583 /* brightness_default_validate:
585 * manipulate input output values to keep consistency with the
586 * backlight framework for which brightness values are 0-based.
588 static int brightness_default_validate(const int direction, const int value)
590 switch (direction) {
591 case SNC_VALIDATE_OUT:
592 return value - 1;
593 case SNC_VALIDATE_IN:
594 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
595 return value + 1;
597 return -EINVAL;
600 /* boolean_validate:
602 * on input validate boolean values 0/1, on output just pass the
603 * received value.
605 static int boolean_validate(const int direction, const int value)
607 if (direction == SNC_VALIDATE_IN) {
608 if (value != 0 && value != 1)
609 return -EINVAL;
611 return value;
615 * Sysfs show/store common to all sony_nc_values
617 static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
618 char *buffer)
620 int value;
621 struct sony_nc_value *item =
622 container_of(attr, struct sony_nc_value, devattr);
624 if (!*item->acpiget)
625 return -EIO;
627 if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
628 return -EIO;
630 if (item->validate)
631 value = item->validate(SNC_VALIDATE_OUT, value);
633 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
636 static ssize_t sony_nc_sysfs_store(struct device *dev,
637 struct device_attribute *attr,
638 const char *buffer, size_t count)
640 int value;
641 struct sony_nc_value *item =
642 container_of(attr, struct sony_nc_value, devattr);
644 if (!item->acpiset)
645 return -EIO;
647 if (count > 31)
648 return -EINVAL;
650 value = simple_strtoul(buffer, NULL, 10);
652 if (item->validate)
653 value = item->validate(SNC_VALIDATE_IN, value);
655 if (value < 0)
656 return value;
658 if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
659 return -EIO;
660 item->value = value;
661 item->valid = 1;
662 return count;
667 * Backlight device
669 static int sony_backlight_update_status(struct backlight_device *bd)
671 return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
672 bd->props.brightness + 1, NULL);
675 static int sony_backlight_get_brightness(struct backlight_device *bd)
677 int value;
679 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
680 return 0;
681 /* brightness levels are 1-based, while backlight ones are 0-based */
682 return value - 1;
685 static struct backlight_device *sony_backlight_device;
686 static struct backlight_ops sony_backlight_ops = {
687 .update_status = sony_backlight_update_status,
688 .get_brightness = sony_backlight_get_brightness,
692 * ACPI callbacks
694 static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
696 dprintk("sony_acpi_notify, event: %d\n", event);
697 sony_laptop_report_input_event(event);
698 acpi_bus_generate_event(sony_nc_acpi_device, 1, event);
701 static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
702 void *context, void **return_value)
704 struct acpi_namespace_node *node;
705 union acpi_operand_object *operand;
707 node = (struct acpi_namespace_node *)handle;
708 operand = (union acpi_operand_object *)node->object;
710 printk(KERN_WARNING DRV_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
711 (u32) operand->method.param_count);
713 return AE_OK;
717 * ACPI device
719 static int sony_nc_resume(struct acpi_device *device)
721 struct sony_nc_value *item;
723 for (item = sony_nc_values; item->name; item++) {
724 int ret;
726 if (!item->valid)
727 continue;
728 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
729 item->value, NULL);
730 if (ret < 0) {
731 printk("%s: %d\n", __FUNCTION__, ret);
732 break;
735 return 0;
738 static int sony_nc_add(struct acpi_device *device)
740 acpi_status status;
741 int result = 0;
742 acpi_handle handle;
743 struct sony_nc_value *item;
745 printk(KERN_INFO DRV_PFX "%s v%s.\n",
746 SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
748 sony_nc_acpi_device = device;
749 strcpy(acpi_device_class(device), "sony/hotkey");
751 sony_nc_acpi_handle = device->handle;
753 if (debug) {
754 status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_nc_acpi_handle,
755 1, sony_walk_callback, NULL, NULL);
756 if (ACPI_FAILURE(status)) {
757 printk(KERN_WARNING DRV_PFX "unable to walk acpi resources\n");
758 result = -ENODEV;
759 goto outwalk;
763 /* setup input devices and helper fifo */
764 result = sony_laptop_setup_input();
765 if (result) {
766 printk(KERN_ERR DRV_PFX
767 "Unabe to create input devices.\n");
768 goto outwalk;
771 status = acpi_install_notify_handler(sony_nc_acpi_handle,
772 ACPI_DEVICE_NOTIFY,
773 sony_acpi_notify, NULL);
774 if (ACPI_FAILURE(status)) {
775 printk(KERN_WARNING DRV_PFX "unable to install notify handler\n");
776 result = -ENODEV;
777 goto outinput;
780 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT", &handle))) {
781 sony_backlight_device = backlight_device_register("sony", NULL,
782 NULL,
783 &sony_backlight_ops);
785 if (IS_ERR(sony_backlight_device)) {
786 printk(KERN_WARNING DRV_PFX "unable to register backlight device\n");
787 sony_backlight_device = NULL;
788 } else {
789 sony_backlight_device->props.brightness =
790 sony_backlight_get_brightness
791 (sony_backlight_device);
792 sony_backlight_device->props.max_brightness =
793 SONY_MAX_BRIGHTNESS - 1;
798 result = sony_pf_add();
799 if (result)
800 goto outbacklight;
802 /* create sony_pf sysfs attributes related to the SNC device */
803 for (item = sony_nc_values; item->name; ++item) {
805 if (!debug && item->debug)
806 continue;
808 /* find the available acpiget as described in the DSDT */
809 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
810 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
811 *item->acpiget,
812 &handle))) {
813 dprintk("Found %s getter: %s\n",
814 item->name, *item->acpiget);
815 item->devattr.attr.mode |= S_IRUGO;
816 break;
820 /* find the available acpiset as described in the DSDT */
821 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
822 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
823 *item->acpiset,
824 &handle))) {
825 dprintk("Found %s setter: %s\n",
826 item->name, *item->acpiset);
827 item->devattr.attr.mode |= S_IWUSR;
828 break;
832 if (item->devattr.attr.mode != 0) {
833 result =
834 device_create_file(&sony_pf_device->dev,
835 &item->devattr);
836 if (result)
837 goto out_sysfs;
841 return 0;
843 out_sysfs:
844 for (item = sony_nc_values; item->name; ++item) {
845 device_remove_file(&sony_pf_device->dev, &item->devattr);
847 sony_pf_remove();
849 outbacklight:
850 if (sony_backlight_device)
851 backlight_device_unregister(sony_backlight_device);
853 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
854 ACPI_DEVICE_NOTIFY,
855 sony_acpi_notify);
856 if (ACPI_FAILURE(status))
857 printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
859 outinput:
860 sony_laptop_remove_input();
862 outwalk:
863 return result;
866 static int sony_nc_remove(struct acpi_device *device, int type)
868 acpi_status status;
869 struct sony_nc_value *item;
871 if (sony_backlight_device)
872 backlight_device_unregister(sony_backlight_device);
874 sony_nc_acpi_device = NULL;
876 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
877 ACPI_DEVICE_NOTIFY,
878 sony_acpi_notify);
879 if (ACPI_FAILURE(status))
880 printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
882 for (item = sony_nc_values; item->name; ++item) {
883 device_remove_file(&sony_pf_device->dev, &item->devattr);
886 sony_pf_remove();
887 sony_laptop_remove_input();
888 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
890 return 0;
893 static struct acpi_driver sony_nc_driver = {
894 .name = SONY_NC_DRIVER_NAME,
895 .class = SONY_NC_CLASS,
896 .ids = SONY_NC_HID,
897 .owner = THIS_MODULE,
898 .ops = {
899 .add = sony_nc_add,
900 .remove = sony_nc_remove,
901 .resume = sony_nc_resume,
905 /*********** SPIC (SNY6001) Device ***********/
907 #define SONYPI_DEVICE_TYPE1 0x00000001
908 #define SONYPI_DEVICE_TYPE2 0x00000002
909 #define SONYPI_DEVICE_TYPE3 0x00000004
911 #define SONYPI_TYPE1_OFFSET 0x04
912 #define SONYPI_TYPE2_OFFSET 0x12
913 #define SONYPI_TYPE3_OFFSET 0x12
915 struct sony_pic_ioport {
916 struct acpi_resource_io io;
917 struct list_head list;
920 struct sony_pic_irq {
921 struct acpi_resource_irq irq;
922 struct list_head list;
925 struct sony_pic_dev {
926 int model;
927 u16 evport_offset;
928 u8 camera_power;
929 u8 bluetooth_power;
930 u8 wwan_power;
931 struct acpi_device *acpi_dev;
932 struct sony_pic_irq *cur_irq;
933 struct sony_pic_ioport *cur_ioport;
934 struct list_head interrupts;
935 struct list_head ioports;
936 struct mutex lock;
939 static struct sony_pic_dev spic_dev = {
940 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
941 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
944 /* Event masks */
945 #define SONYPI_JOGGER_MASK 0x00000001
946 #define SONYPI_CAPTURE_MASK 0x00000002
947 #define SONYPI_FNKEY_MASK 0x00000004
948 #define SONYPI_BLUETOOTH_MASK 0x00000008
949 #define SONYPI_PKEY_MASK 0x00000010
950 #define SONYPI_BACK_MASK 0x00000020
951 #define SONYPI_HELP_MASK 0x00000040
952 #define SONYPI_LID_MASK 0x00000080
953 #define SONYPI_ZOOM_MASK 0x00000100
954 #define SONYPI_THUMBPHRASE_MASK 0x00000200
955 #define SONYPI_MEYE_MASK 0x00000400
956 #define SONYPI_MEMORYSTICK_MASK 0x00000800
957 #define SONYPI_BATTERY_MASK 0x00001000
958 #define SONYPI_WIRELESS_MASK 0x00002000
960 struct sonypi_event {
961 u8 data;
962 u8 event;
965 /* The set of possible button release events */
966 static struct sonypi_event sonypi_releaseev[] = {
967 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
968 { 0, 0 }
971 /* The set of possible jogger events */
972 static struct sonypi_event sonypi_joggerev[] = {
973 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
974 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
975 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
976 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
977 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
978 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
979 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
980 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
981 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
982 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
983 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
984 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
985 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
986 { 0, 0 }
989 /* The set of possible capture button events */
990 static struct sonypi_event sonypi_captureev[] = {
991 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
992 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
993 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
994 { 0, 0 }
997 /* The set of possible fnkeys events */
998 static struct sonypi_event sonypi_fnkeyev[] = {
999 { 0x10, SONYPI_EVENT_FNKEY_ESC },
1000 { 0x11, SONYPI_EVENT_FNKEY_F1 },
1001 { 0x12, SONYPI_EVENT_FNKEY_F2 },
1002 { 0x13, SONYPI_EVENT_FNKEY_F3 },
1003 { 0x14, SONYPI_EVENT_FNKEY_F4 },
1004 { 0x15, SONYPI_EVENT_FNKEY_F5 },
1005 { 0x16, SONYPI_EVENT_FNKEY_F6 },
1006 { 0x17, SONYPI_EVENT_FNKEY_F7 },
1007 { 0x18, SONYPI_EVENT_FNKEY_F8 },
1008 { 0x19, SONYPI_EVENT_FNKEY_F9 },
1009 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
1010 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
1011 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
1012 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
1013 { 0x21, SONYPI_EVENT_FNKEY_1 },
1014 { 0x22, SONYPI_EVENT_FNKEY_2 },
1015 { 0x31, SONYPI_EVENT_FNKEY_D },
1016 { 0x32, SONYPI_EVENT_FNKEY_E },
1017 { 0x33, SONYPI_EVENT_FNKEY_F },
1018 { 0x34, SONYPI_EVENT_FNKEY_S },
1019 { 0x35, SONYPI_EVENT_FNKEY_B },
1020 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
1021 { 0, 0 }
1024 /* The set of possible program key events */
1025 static struct sonypi_event sonypi_pkeyev[] = {
1026 { 0x01, SONYPI_EVENT_PKEY_P1 },
1027 { 0x02, SONYPI_EVENT_PKEY_P2 },
1028 { 0x04, SONYPI_EVENT_PKEY_P3 },
1029 { 0x5c, SONYPI_EVENT_PKEY_P1 },
1030 { 0, 0 }
1033 /* The set of possible bluetooth events */
1034 static struct sonypi_event sonypi_blueev[] = {
1035 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
1036 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
1037 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
1038 { 0, 0 }
1041 /* The set of possible wireless events */
1042 static struct sonypi_event sonypi_wlessev[] = {
1043 { 0x59, SONYPI_EVENT_WIRELESS_ON },
1044 { 0x5a, SONYPI_EVENT_WIRELESS_OFF },
1045 { 0, 0 }
1048 /* The set of possible back button events */
1049 static struct sonypi_event sonypi_backev[] = {
1050 { 0x20, SONYPI_EVENT_BACK_PRESSED },
1051 { 0, 0 }
1054 /* The set of possible help button events */
1055 static struct sonypi_event sonypi_helpev[] = {
1056 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
1057 { 0, 0 }
1061 /* The set of possible lid events */
1062 static struct sonypi_event sonypi_lidev[] = {
1063 { 0x51, SONYPI_EVENT_LID_CLOSED },
1064 { 0x50, SONYPI_EVENT_LID_OPENED },
1065 { 0, 0 }
1068 /* The set of possible zoom events */
1069 static struct sonypi_event sonypi_zoomev[] = {
1070 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
1071 { 0, 0 }
1074 /* The set of possible thumbphrase events */
1075 static struct sonypi_event sonypi_thumbphraseev[] = {
1076 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
1077 { 0, 0 }
1080 /* The set of possible motioneye camera events */
1081 static struct sonypi_event sonypi_meyeev[] = {
1082 { 0x00, SONYPI_EVENT_MEYE_FACE },
1083 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
1084 { 0, 0 }
1087 /* The set of possible memorystick events */
1088 static struct sonypi_event sonypi_memorystickev[] = {
1089 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
1090 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
1091 { 0, 0 }
1094 /* The set of possible battery events */
1095 static struct sonypi_event sonypi_batteryev[] = {
1096 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
1097 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
1098 { 0, 0 }
1101 static struct sonypi_eventtypes {
1102 int model;
1103 u8 data;
1104 unsigned long mask;
1105 struct sonypi_event * events;
1106 } sony_pic_eventtypes[] = {
1107 { SONYPI_DEVICE_TYPE1, 0, 0xffffffff, sonypi_releaseev },
1108 { SONYPI_DEVICE_TYPE1, 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
1109 { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_LID_MASK, sonypi_lidev },
1110 { SONYPI_DEVICE_TYPE1, 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
1111 { SONYPI_DEVICE_TYPE1, 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
1112 { SONYPI_DEVICE_TYPE1, 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1113 { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1114 { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
1115 { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1116 { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
1118 { SONYPI_DEVICE_TYPE2, 0, 0xffffffff, sonypi_releaseev },
1119 { SONYPI_DEVICE_TYPE2, 0x38, SONYPI_LID_MASK, sonypi_lidev },
1120 { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
1121 { SONYPI_DEVICE_TYPE2, 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
1122 { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1123 { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1124 { SONYPI_DEVICE_TYPE2, 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
1125 { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_BACK_MASK, sonypi_backev },
1126 { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_HELP_MASK, sonypi_helpev },
1127 { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
1128 { SONYPI_DEVICE_TYPE2, 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
1129 { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1130 { SONYPI_DEVICE_TYPE2, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1131 { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1133 { SONYPI_DEVICE_TYPE3, 0, 0xffffffff, sonypi_releaseev },
1134 { SONYPI_DEVICE_TYPE3, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1135 { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
1136 { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1137 { SONYPI_DEVICE_TYPE3, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1138 { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1139 { 0 }
1142 static int sony_pic_detect_device_type(void)
1144 struct pci_dev *pcidev;
1145 int model = 0;
1147 if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1148 PCI_DEVICE_ID_INTEL_82371AB_3, NULL)))
1149 model = SONYPI_DEVICE_TYPE1;
1151 else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1152 PCI_DEVICE_ID_INTEL_ICH6_1, NULL)))
1153 model = SONYPI_DEVICE_TYPE3;
1155 else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1156 PCI_DEVICE_ID_INTEL_ICH7_1, NULL)))
1157 model = SONYPI_DEVICE_TYPE3;
1159 else
1160 model = SONYPI_DEVICE_TYPE2;
1162 if (pcidev)
1163 pci_dev_put(pcidev);
1165 printk(KERN_INFO DRV_PFX "detected Type%d model\n",
1166 model == SONYPI_DEVICE_TYPE1 ? 1 :
1167 model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
1168 return model;
1171 #define ITERATIONS_LONG 10000
1172 #define ITERATIONS_SHORT 10
1173 #define wait_on_command(command, iterations) { \
1174 unsigned int n = iterations; \
1175 while (--n && (command)) \
1176 udelay(1); \
1177 if (!n) \
1178 dprintk("command failed at %s : %s (line %d)\n", \
1179 __FILE__, __FUNCTION__, __LINE__); \
1182 static u8 sony_pic_call1(u8 dev)
1184 u8 v1, v2;
1186 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1187 ITERATIONS_LONG);
1188 outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1189 v1 = inb_p(spic_dev.cur_ioport->io.minimum + 4);
1190 v2 = inb_p(spic_dev.cur_ioport->io.minimum);
1191 dprintk("sony_pic_call1: 0x%.4x\n", (v2 << 8) | v1);
1192 return v2;
1195 static u8 sony_pic_call2(u8 dev, u8 fn)
1197 u8 v1;
1199 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1200 ITERATIONS_LONG);
1201 outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1202 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1203 ITERATIONS_LONG);
1204 outb(fn, spic_dev.cur_ioport->io.minimum);
1205 v1 = inb_p(spic_dev.cur_ioport->io.minimum);
1206 dprintk("sony_pic_call2: 0x%.4x\n", v1);
1207 return v1;
1210 static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
1212 u8 v1;
1214 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
1215 outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1216 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
1217 outb(fn, spic_dev.cur_ioport->io.minimum);
1218 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
1219 outb(v, spic_dev.cur_ioport->io.minimum);
1220 v1 = inb_p(spic_dev.cur_ioport->io.minimum);
1221 dprintk("sony_pic_call3: 0x%.4x\n", v1);
1222 return v1;
1225 /* camera tests and poweron/poweroff */
1226 #define SONYPI_CAMERA_PICTURE 5
1227 #define SONYPI_CAMERA_CONTROL 0x10
1229 #define SONYPI_CAMERA_BRIGHTNESS 0
1230 #define SONYPI_CAMERA_CONTRAST 1
1231 #define SONYPI_CAMERA_HUE 2
1232 #define SONYPI_CAMERA_COLOR 3
1233 #define SONYPI_CAMERA_SHARPNESS 4
1235 #define SONYPI_CAMERA_EXPOSURE_MASK 0xC
1236 #define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
1237 #define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
1238 #define SONYPI_CAMERA_MUTE_MASK 0x40
1240 /* the rest don't need a loop until not 0xff */
1241 #define SONYPI_CAMERA_AGC 6
1242 #define SONYPI_CAMERA_AGC_MASK 0x30
1243 #define SONYPI_CAMERA_SHUTTER_MASK 0x7
1245 #define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
1246 #define SONYPI_CAMERA_CONTROL 0x10
1248 #define SONYPI_CAMERA_STATUS 7
1249 #define SONYPI_CAMERA_STATUS_READY 0x2
1250 #define SONYPI_CAMERA_STATUS_POSITION 0x4
1252 #define SONYPI_DIRECTION_BACKWARDS 0x4
1254 #define SONYPI_CAMERA_REVISION 8
1255 #define SONYPI_CAMERA_ROMVERSION 9
1257 static int __sony_pic_camera_ready(void)
1259 u8 v;
1261 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
1262 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
1265 static int __sony_pic_camera_off(void)
1267 if (!camera) {
1268 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1269 return -ENODEV;
1272 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
1273 SONYPI_CAMERA_MUTE_MASK),
1274 ITERATIONS_SHORT);
1276 if (spic_dev.camera_power) {
1277 sony_pic_call2(0x91, 0);
1278 spic_dev.camera_power = 0;
1280 return 0;
1283 static int __sony_pic_camera_on(void)
1285 int i, j, x;
1287 if (!camera) {
1288 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1289 return -ENODEV;
1292 if (spic_dev.camera_power)
1293 return 0;
1295 for (j = 5; j > 0; j--) {
1297 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
1298 msleep(10);
1299 sony_pic_call1(0x93);
1301 for (i = 400; i > 0; i--) {
1302 if (__sony_pic_camera_ready())
1303 break;
1304 msleep(10);
1306 if (i)
1307 break;
1310 if (j == 0) {
1311 printk(KERN_WARNING DRV_PFX "failed to power on camera\n");
1312 return -ENODEV;
1315 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
1316 0x5a),
1317 ITERATIONS_SHORT);
1319 spic_dev.camera_power = 1;
1320 return 0;
1323 /* External camera command (exported to the motion eye v4l driver) */
1324 int sony_pic_camera_command(int command, u8 value)
1326 if (!camera)
1327 return -EIO;
1329 mutex_lock(&spic_dev.lock);
1331 switch (command) {
1332 case SONY_PIC_COMMAND_SETCAMERA:
1333 if (value)
1334 __sony_pic_camera_on();
1335 else
1336 __sony_pic_camera_off();
1337 break;
1338 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
1339 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
1340 ITERATIONS_SHORT);
1341 break;
1342 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
1343 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
1344 ITERATIONS_SHORT);
1345 break;
1346 case SONY_PIC_COMMAND_SETCAMERAHUE:
1347 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
1348 ITERATIONS_SHORT);
1349 break;
1350 case SONY_PIC_COMMAND_SETCAMERACOLOR:
1351 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
1352 ITERATIONS_SHORT);
1353 break;
1354 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
1355 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
1356 ITERATIONS_SHORT);
1357 break;
1358 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
1359 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
1360 ITERATIONS_SHORT);
1361 break;
1362 case SONY_PIC_COMMAND_SETCAMERAAGC:
1363 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
1364 ITERATIONS_SHORT);
1365 break;
1366 default:
1367 printk(KERN_ERR DRV_PFX "sony_pic_camera_command invalid: %d\n",
1368 command);
1369 break;
1371 mutex_unlock(&spic_dev.lock);
1372 return 0;
1374 EXPORT_SYMBOL(sony_pic_camera_command);
1376 /* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
1377 static void sony_pic_set_wwanpower(u8 state)
1379 state = !!state;
1380 mutex_lock(&spic_dev.lock);
1381 if (spic_dev.wwan_power == state) {
1382 mutex_unlock(&spic_dev.lock);
1383 return;
1385 sony_pic_call2(0xB0, state);
1386 spic_dev.wwan_power = state;
1387 mutex_unlock(&spic_dev.lock);
1390 static ssize_t sony_pic_wwanpower_store(struct device *dev,
1391 struct device_attribute *attr,
1392 const char *buffer, size_t count)
1394 unsigned long value;
1395 if (count > 31)
1396 return -EINVAL;
1398 value = simple_strtoul(buffer, NULL, 10);
1399 sony_pic_set_wwanpower(value);
1401 return count;
1404 static ssize_t sony_pic_wwanpower_show(struct device *dev,
1405 struct device_attribute *attr, char *buffer)
1407 ssize_t count;
1408 mutex_lock(&spic_dev.lock);
1409 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
1410 mutex_unlock(&spic_dev.lock);
1411 return count;
1414 /* bluetooth subsystem power state */
1415 static void __sony_pic_set_bluetoothpower(u8 state)
1417 state = !!state;
1418 if (spic_dev.bluetooth_power == state)
1419 return;
1420 sony_pic_call2(0x96, state);
1421 sony_pic_call1(0x82);
1422 spic_dev.bluetooth_power = state;
1425 static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
1426 struct device_attribute *attr,
1427 const char *buffer, size_t count)
1429 unsigned long value;
1430 if (count > 31)
1431 return -EINVAL;
1433 value = simple_strtoul(buffer, NULL, 10);
1434 mutex_lock(&spic_dev.lock);
1435 __sony_pic_set_bluetoothpower(value);
1436 mutex_unlock(&spic_dev.lock);
1438 return count;
1441 static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
1442 struct device_attribute *attr, char *buffer)
1444 ssize_t count = 0;
1445 mutex_lock(&spic_dev.lock);
1446 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
1447 mutex_unlock(&spic_dev.lock);
1448 return count;
1451 /* fan speed */
1452 /* FAN0 information (reverse engineered from ACPI tables) */
1453 #define SONY_PIC_FAN0_STATUS 0x93
1454 static int sony_pic_set_fanspeed(unsigned long value)
1456 return ec_write(SONY_PIC_FAN0_STATUS, value);
1459 static int sony_pic_get_fanspeed(u8 *value)
1461 return ec_read(SONY_PIC_FAN0_STATUS, value);
1464 static ssize_t sony_pic_fanspeed_store(struct device *dev,
1465 struct device_attribute *attr,
1466 const char *buffer, size_t count)
1468 unsigned long value;
1469 if (count > 31)
1470 return -EINVAL;
1472 value = simple_strtoul(buffer, NULL, 10);
1473 if (sony_pic_set_fanspeed(value))
1474 return -EIO;
1476 return count;
1479 static ssize_t sony_pic_fanspeed_show(struct device *dev,
1480 struct device_attribute *attr, char *buffer)
1482 u8 value = 0;
1483 if (sony_pic_get_fanspeed(&value))
1484 return -EIO;
1486 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
1489 #define SPIC_ATTR(_name, _mode) \
1490 struct device_attribute spic_attr_##_name = __ATTR(_name, \
1491 _mode, sony_pic_## _name ##_show, \
1492 sony_pic_## _name ##_store)
1494 static SPIC_ATTR(bluetoothpower, 0644);
1495 static SPIC_ATTR(wwanpower, 0644);
1496 static SPIC_ATTR(fanspeed, 0644);
1498 static struct attribute *spic_attributes[] = {
1499 &spic_attr_bluetoothpower.attr,
1500 &spic_attr_wwanpower.attr,
1501 &spic_attr_fanspeed.attr,
1502 NULL
1505 static struct attribute_group spic_attribute_group = {
1506 .attrs = spic_attributes
1509 /******** SONYPI compatibility **********/
1510 #ifdef CONFIG_SONYPI_COMPAT
1512 /* battery / brightness / temperature addresses */
1513 #define SONYPI_BAT_FLAGS 0x81
1514 #define SONYPI_LCD_LIGHT 0x96
1515 #define SONYPI_BAT1_PCTRM 0xa0
1516 #define SONYPI_BAT1_LEFT 0xa2
1517 #define SONYPI_BAT1_MAXRT 0xa4
1518 #define SONYPI_BAT2_PCTRM 0xa8
1519 #define SONYPI_BAT2_LEFT 0xaa
1520 #define SONYPI_BAT2_MAXRT 0xac
1521 #define SONYPI_BAT1_MAXTK 0xb0
1522 #define SONYPI_BAT1_FULL 0xb2
1523 #define SONYPI_BAT2_MAXTK 0xb8
1524 #define SONYPI_BAT2_FULL 0xba
1525 #define SONYPI_TEMP_STATUS 0xC1
1527 struct sonypi_compat_s {
1528 struct fasync_struct *fifo_async;
1529 struct kfifo *fifo;
1530 spinlock_t fifo_lock;
1531 wait_queue_head_t fifo_proc_list;
1532 atomic_t open_count;
1534 static struct sonypi_compat_s sonypi_compat = {
1535 .open_count = ATOMIC_INIT(0),
1538 static int sonypi_misc_fasync(int fd, struct file *filp, int on)
1540 int retval;
1542 retval = fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
1543 if (retval < 0)
1544 return retval;
1545 return 0;
1548 static int sonypi_misc_release(struct inode *inode, struct file *file)
1550 sonypi_misc_fasync(-1, file, 0);
1551 atomic_dec(&sonypi_compat.open_count);
1552 return 0;
1555 static int sonypi_misc_open(struct inode *inode, struct file *file)
1557 /* Flush input queue on first open */
1558 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
1559 kfifo_reset(sonypi_compat.fifo);
1560 return 0;
1563 static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
1564 size_t count, loff_t *pos)
1566 ssize_t ret;
1567 unsigned char c;
1569 if ((kfifo_len(sonypi_compat.fifo) == 0) &&
1570 (file->f_flags & O_NONBLOCK))
1571 return -EAGAIN;
1573 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
1574 kfifo_len(sonypi_compat.fifo) != 0);
1575 if (ret)
1576 return ret;
1578 while (ret < count &&
1579 (kfifo_get(sonypi_compat.fifo, &c, sizeof(c)) == sizeof(c))) {
1580 if (put_user(c, buf++))
1581 return -EFAULT;
1582 ret++;
1585 if (ret > 0) {
1586 struct inode *inode = file->f_path.dentry->d_inode;
1587 inode->i_atime = current_fs_time(inode->i_sb);
1590 return ret;
1593 static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
1595 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
1596 if (kfifo_len(sonypi_compat.fifo))
1597 return POLLIN | POLLRDNORM;
1598 return 0;
1601 static int ec_read16(u8 addr, u16 *value)
1603 u8 val_lb, val_hb;
1604 if (ec_read(addr, &val_lb))
1605 return -1;
1606 if (ec_read(addr + 1, &val_hb))
1607 return -1;
1608 *value = val_lb | (val_hb << 8);
1609 return 0;
1612 static int sonypi_misc_ioctl(struct inode *ip, struct file *fp,
1613 unsigned int cmd, unsigned long arg)
1615 int ret = 0;
1616 void __user *argp = (void __user *)arg;
1617 u8 val8;
1618 u16 val16;
1619 int value;
1621 mutex_lock(&spic_dev.lock);
1622 switch (cmd) {
1623 case SONYPI_IOCGBRT:
1624 if (sony_backlight_device == NULL) {
1625 ret = -EIO;
1626 break;
1628 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
1629 ret = -EIO;
1630 break;
1632 val8 = ((value & 0xff) - 1) << 5;
1633 if (copy_to_user(argp, &val8, sizeof(val8)))
1634 ret = -EFAULT;
1635 break;
1636 case SONYPI_IOCSBRT:
1637 if (sony_backlight_device == NULL) {
1638 ret = -EIO;
1639 break;
1641 if (copy_from_user(&val8, argp, sizeof(val8))) {
1642 ret = -EFAULT;
1643 break;
1645 if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
1646 (val8 >> 5) + 1, NULL)) {
1647 ret = -EIO;
1648 break;
1650 /* sync the backlight device status */
1651 sony_backlight_device->props.brightness =
1652 sony_backlight_get_brightness(sony_backlight_device);
1653 break;
1654 case SONYPI_IOCGBAT1CAP:
1655 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
1656 ret = -EIO;
1657 break;
1659 if (copy_to_user(argp, &val16, sizeof(val16)))
1660 ret = -EFAULT;
1661 break;
1662 case SONYPI_IOCGBAT1REM:
1663 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
1664 ret = -EIO;
1665 break;
1667 if (copy_to_user(argp, &val16, sizeof(val16)))
1668 ret = -EFAULT;
1669 break;
1670 case SONYPI_IOCGBAT2CAP:
1671 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
1672 ret = -EIO;
1673 break;
1675 if (copy_to_user(argp, &val16, sizeof(val16)))
1676 ret = -EFAULT;
1677 break;
1678 case SONYPI_IOCGBAT2REM:
1679 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
1680 ret = -EIO;
1681 break;
1683 if (copy_to_user(argp, &val16, sizeof(val16)))
1684 ret = -EFAULT;
1685 break;
1686 case SONYPI_IOCGBATFLAGS:
1687 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
1688 ret = -EIO;
1689 break;
1691 val8 &= 0x07;
1692 if (copy_to_user(argp, &val8, sizeof(val8)))
1693 ret = -EFAULT;
1694 break;
1695 case SONYPI_IOCGBLUE:
1696 val8 = spic_dev.bluetooth_power;
1697 if (copy_to_user(argp, &val8, sizeof(val8)))
1698 ret = -EFAULT;
1699 break;
1700 case SONYPI_IOCSBLUE:
1701 if (copy_from_user(&val8, argp, sizeof(val8))) {
1702 ret = -EFAULT;
1703 break;
1705 __sony_pic_set_bluetoothpower(val8);
1706 break;
1707 /* FAN Controls */
1708 case SONYPI_IOCGFAN:
1709 if (sony_pic_get_fanspeed(&val8)) {
1710 ret = -EIO;
1711 break;
1713 if (copy_to_user(argp, &val8, sizeof(val8)))
1714 ret = -EFAULT;
1715 break;
1716 case SONYPI_IOCSFAN:
1717 if (copy_from_user(&val8, argp, sizeof(val8))) {
1718 ret = -EFAULT;
1719 break;
1721 if (sony_pic_set_fanspeed(val8))
1722 ret = -EIO;
1723 break;
1724 /* GET Temperature (useful under APM) */
1725 case SONYPI_IOCGTEMP:
1726 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
1727 ret = -EIO;
1728 break;
1730 if (copy_to_user(argp, &val8, sizeof(val8)))
1731 ret = -EFAULT;
1732 break;
1733 default:
1734 ret = -EINVAL;
1736 mutex_unlock(&spic_dev.lock);
1737 return ret;
1740 static const struct file_operations sonypi_misc_fops = {
1741 .owner = THIS_MODULE,
1742 .read = sonypi_misc_read,
1743 .poll = sonypi_misc_poll,
1744 .open = sonypi_misc_open,
1745 .release = sonypi_misc_release,
1746 .fasync = sonypi_misc_fasync,
1747 .ioctl = sonypi_misc_ioctl,
1750 static struct miscdevice sonypi_misc_device = {
1751 .minor = MISC_DYNAMIC_MINOR,
1752 .name = "sonypi",
1753 .fops = &sonypi_misc_fops,
1756 static void sonypi_compat_report_event(u8 event)
1758 kfifo_put(sonypi_compat.fifo, (unsigned char *)&event, sizeof(event));
1759 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
1760 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
1763 static int sonypi_compat_init(void)
1765 int error;
1767 spin_lock_init(&sonypi_compat.fifo_lock);
1768 sonypi_compat.fifo = kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
1769 &sonypi_compat.fifo_lock);
1770 if (IS_ERR(sonypi_compat.fifo)) {
1771 printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
1772 return PTR_ERR(sonypi_compat.fifo);
1775 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
1777 if (minor != -1)
1778 sonypi_misc_device.minor = minor;
1779 error = misc_register(&sonypi_misc_device);
1780 if (error) {
1781 printk(KERN_ERR DRV_PFX "misc_register failed\n");
1782 goto err_free_kfifo;
1784 if (minor == -1)
1785 printk(KERN_INFO DRV_PFX "device allocated minor is %d\n",
1786 sonypi_misc_device.minor);
1788 return 0;
1790 err_free_kfifo:
1791 kfifo_free(sonypi_compat.fifo);
1792 return error;
1795 static void sonypi_compat_exit(void)
1797 misc_deregister(&sonypi_misc_device);
1798 kfifo_free(sonypi_compat.fifo);
1800 #else
1801 static int sonypi_compat_init(void) { return 0; }
1802 static void sonypi_compat_exit(void) { }
1803 static void sonypi_compat_report_event(u8 event) { }
1804 #endif /* CONFIG_SONYPI_COMPAT */
1807 * ACPI callbacks
1809 static acpi_status
1810 sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
1812 u32 i;
1813 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
1815 switch (resource->type) {
1816 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
1817 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
1818 return AE_OK;
1820 case ACPI_RESOURCE_TYPE_IRQ:
1822 struct acpi_resource_irq *p = &resource->data.irq;
1823 struct sony_pic_irq *interrupt = NULL;
1824 if (!p || !p->interrupt_count) {
1826 * IRQ descriptors may have no IRQ# bits set,
1827 * particularly those those w/ _STA disabled
1829 dprintk("Blank IRQ resource\n");
1830 return AE_OK;
1832 for (i = 0; i < p->interrupt_count; i++) {
1833 if (!p->interrupts[i]) {
1834 printk(KERN_WARNING DRV_PFX
1835 "Invalid IRQ %d\n",
1836 p->interrupts[i]);
1837 continue;
1839 interrupt = kzalloc(sizeof(*interrupt),
1840 GFP_KERNEL);
1841 if (!interrupt)
1842 return AE_ERROR;
1844 list_add_tail(&interrupt->list, &dev->interrupts);
1845 interrupt->irq.triggering = p->triggering;
1846 interrupt->irq.polarity = p->polarity;
1847 interrupt->irq.sharable = p->sharable;
1848 interrupt->irq.interrupt_count = 1;
1849 interrupt->irq.interrupts[0] = p->interrupts[i];
1851 return AE_OK;
1853 case ACPI_RESOURCE_TYPE_IO:
1855 struct acpi_resource_io *io = &resource->data.io;
1856 struct sony_pic_ioport *ioport = NULL;
1857 if (!io) {
1858 dprintk("Blank IO resource\n");
1859 return AE_OK;
1862 ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
1863 if (!ioport)
1864 return AE_ERROR;
1866 list_add_tail(&ioport->list, &dev->ioports);
1867 memcpy(&ioport->io, io, sizeof(*io));
1868 return AE_OK;
1870 default:
1871 dprintk("Resource %d isn't an IRQ nor an IO port\n",
1872 resource->type);
1874 case ACPI_RESOURCE_TYPE_END_TAG:
1875 return AE_OK;
1877 return AE_CTRL_TERMINATE;
1880 static int sony_pic_possible_resources(struct acpi_device *device)
1882 int result = 0;
1883 acpi_status status = AE_OK;
1885 if (!device)
1886 return -EINVAL;
1888 /* get device status */
1889 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
1890 dprintk("Evaluating _STA\n");
1891 result = acpi_bus_get_status(device);
1892 if (result) {
1893 printk(KERN_WARNING DRV_PFX "Unable to read status\n");
1894 goto end;
1897 if (!device->status.enabled)
1898 dprintk("Device disabled\n");
1899 else
1900 dprintk("Device enabled\n");
1903 * Query and parse 'method'
1905 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
1906 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
1907 sony_pic_read_possible_resource, &spic_dev);
1908 if (ACPI_FAILURE(status)) {
1909 printk(KERN_WARNING DRV_PFX
1910 "Failure evaluating %s\n",
1911 METHOD_NAME__PRS);
1912 result = -ENODEV;
1914 end:
1915 return result;
1919 * Disable the spic device by calling its _DIS method
1921 static int sony_pic_disable(struct acpi_device *device)
1923 if (ACPI_FAILURE(acpi_evaluate_object(device->handle, "_DIS", 0, NULL)))
1924 return -ENXIO;
1926 dprintk("Device disabled\n");
1927 return 0;
1932 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
1934 * Call _SRS to set current resources
1936 static int sony_pic_enable(struct acpi_device *device,
1937 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
1939 acpi_status status;
1940 int result = 0;
1941 struct {
1942 struct acpi_resource io_res;
1943 struct acpi_resource irq_res;
1944 struct acpi_resource end;
1945 } *resource;
1946 struct acpi_buffer buffer = { 0, NULL };
1948 if (!ioport || !irq)
1949 return -EINVAL;
1951 /* init acpi_buffer */
1952 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
1953 if (!resource)
1954 return -ENOMEM;
1956 buffer.length = sizeof(*resource) + 1;
1957 buffer.pointer = resource;
1959 /* setup io resource */
1960 resource->io_res.type = ACPI_RESOURCE_TYPE_IO;
1961 resource->io_res.length = sizeof(struct acpi_resource);
1962 memcpy(&resource->io_res.data.io, &ioport->io,
1963 sizeof(struct acpi_resource_io));
1965 /* setup irq resource */
1966 resource->irq_res.type = ACPI_RESOURCE_TYPE_IRQ;
1967 resource->irq_res.length = sizeof(struct acpi_resource);
1968 memcpy(&resource->irq_res.data.irq, &irq->irq,
1969 sizeof(struct acpi_resource_irq));
1970 /* we requested a shared irq */
1971 resource->irq_res.data.irq.sharable = ACPI_SHARED;
1973 resource->end.type = ACPI_RESOURCE_TYPE_END_TAG;
1975 /* Attempt to set the resource */
1976 dprintk("Evaluating _SRS\n");
1977 status = acpi_set_current_resources(device->handle, &buffer);
1979 /* check for total failure */
1980 if (ACPI_FAILURE(status)) {
1981 printk(KERN_ERR DRV_PFX "Error evaluating _SRS");
1982 result = -ENODEV;
1983 goto end;
1986 /* Necessary device initializations calls (from sonypi) */
1987 sony_pic_call1(0x82);
1988 sony_pic_call2(0x81, 0xff);
1989 sony_pic_call1(compat ? 0x92 : 0x82);
1991 end:
1992 kfree(resource);
1993 return result;
1996 /*****************
1998 * ISR: some event is available
2000 *****************/
2001 static irqreturn_t sony_pic_irq(int irq, void *dev_id)
2003 int i, j;
2004 u8 ev = 0;
2005 u8 data_mask = 0;
2006 u8 device_event = 0;
2008 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
2010 ev = inb_p(dev->cur_ioport->io.minimum);
2011 data_mask = inb_p(dev->cur_ioport->io.minimum + dev->evport_offset);
2013 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
2014 ev, data_mask, dev->cur_ioport->io.minimum, dev->evport_offset);
2016 if (ev == 0x00 || ev == 0xff)
2017 return IRQ_HANDLED;
2019 for (i = 0; sony_pic_eventtypes[i].model; i++) {
2021 if (spic_dev.model != sony_pic_eventtypes[i].model)
2022 continue;
2024 if ((data_mask & sony_pic_eventtypes[i].data) !=
2025 sony_pic_eventtypes[i].data)
2026 continue;
2028 if (!(mask & sony_pic_eventtypes[i].mask))
2029 continue;
2031 for (j = 0; sony_pic_eventtypes[i].events[j].event; j++) {
2032 if (ev == sony_pic_eventtypes[i].events[j].data) {
2033 device_event =
2034 sony_pic_eventtypes[i].events[j].event;
2035 goto found;
2039 return IRQ_HANDLED;
2041 found:
2042 sony_laptop_report_input_event(device_event);
2043 acpi_bus_generate_event(spic_dev.acpi_dev, 1, device_event);
2044 sonypi_compat_report_event(device_event);
2046 return IRQ_HANDLED;
2049 /*****************
2051 * ACPI driver
2053 *****************/
2054 static int sony_pic_remove(struct acpi_device *device, int type)
2056 struct sony_pic_ioport *io, *tmp_io;
2057 struct sony_pic_irq *irq, *tmp_irq;
2059 if (sony_pic_disable(device)) {
2060 printk(KERN_ERR DRV_PFX "Couldn't disable device.\n");
2061 return -ENXIO;
2064 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2065 release_region(spic_dev.cur_ioport->io.minimum,
2066 spic_dev.cur_ioport->io.address_length);
2068 sonypi_compat_exit();
2070 sony_laptop_remove_input();
2072 /* pf attrs */
2073 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2074 sony_pf_remove();
2076 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2077 list_del(&io->list);
2078 kfree(io);
2080 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2081 list_del(&irq->list);
2082 kfree(irq);
2084 spic_dev.cur_ioport = NULL;
2085 spic_dev.cur_irq = NULL;
2087 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
2088 return 0;
2091 static int sony_pic_add(struct acpi_device *device)
2093 int result;
2094 struct sony_pic_ioport *io, *tmp_io;
2095 struct sony_pic_irq *irq, *tmp_irq;
2097 printk(KERN_INFO DRV_PFX "%s v%s.\n",
2098 SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
2100 spic_dev.acpi_dev = device;
2101 strcpy(acpi_device_class(device), "sony/hotkey");
2102 spic_dev.model = sony_pic_detect_device_type();
2103 mutex_init(&spic_dev.lock);
2105 /* model specific characteristics */
2106 switch(spic_dev.model) {
2107 case SONYPI_DEVICE_TYPE1:
2108 spic_dev.evport_offset = SONYPI_TYPE1_OFFSET;
2109 break;
2110 case SONYPI_DEVICE_TYPE3:
2111 spic_dev.evport_offset = SONYPI_TYPE3_OFFSET;
2112 break;
2113 case SONYPI_DEVICE_TYPE2:
2114 default:
2115 spic_dev.evport_offset = SONYPI_TYPE2_OFFSET;
2116 break;
2119 /* read _PRS resources */
2120 result = sony_pic_possible_resources(device);
2121 if (result) {
2122 printk(KERN_ERR DRV_PFX
2123 "Unabe to read possible resources.\n");
2124 goto err_free_resources;
2127 /* setup input devices and helper fifo */
2128 result = sony_laptop_setup_input();
2129 if (result) {
2130 printk(KERN_ERR DRV_PFX
2131 "Unabe to create input devices.\n");
2132 goto err_free_resources;
2135 if (sonypi_compat_init())
2136 goto err_remove_input;
2138 /* request io port */
2139 list_for_each_entry(io, &spic_dev.ioports, list) {
2140 if (request_region(io->io.minimum, io->io.address_length,
2141 "Sony Programable I/O Device")) {
2142 dprintk("I/O port: 0x%.4x (0x%.4x) + 0x%.2x\n",
2143 io->io.minimum, io->io.maximum,
2144 io->io.address_length);
2145 spic_dev.cur_ioport = io;
2146 break;
2149 if (!spic_dev.cur_ioport) {
2150 printk(KERN_ERR DRV_PFX "Failed to request_region.\n");
2151 result = -ENODEV;
2152 goto err_remove_compat;
2155 /* request IRQ */
2156 list_for_each_entry(irq, &spic_dev.interrupts, list) {
2157 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
2158 IRQF_SHARED, "sony-laptop", &spic_dev)) {
2159 dprintk("IRQ: %d - triggering: %d - "
2160 "polarity: %d - shr: %d\n",
2161 irq->irq.interrupts[0],
2162 irq->irq.triggering,
2163 irq->irq.polarity,
2164 irq->irq.sharable);
2165 spic_dev.cur_irq = irq;
2166 break;
2169 if (!spic_dev.cur_irq) {
2170 printk(KERN_ERR DRV_PFX "Failed to request_irq.\n");
2171 result = -ENODEV;
2172 goto err_release_region;
2175 /* set resource status _SRS */
2176 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2177 if (result) {
2178 printk(KERN_ERR DRV_PFX "Couldn't enable device.\n");
2179 goto err_free_irq;
2182 spic_dev.bluetooth_power = -1;
2183 /* create device attributes */
2184 result = sony_pf_add();
2185 if (result)
2186 goto err_disable_device;
2188 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2189 if (result)
2190 goto err_remove_pf;
2192 return 0;
2194 err_remove_pf:
2195 sony_pf_remove();
2197 err_disable_device:
2198 sony_pic_disable(device);
2200 err_free_irq:
2201 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2203 err_release_region:
2204 release_region(spic_dev.cur_ioport->io.minimum,
2205 spic_dev.cur_ioport->io.address_length);
2207 err_remove_compat:
2208 sonypi_compat_exit();
2210 err_remove_input:
2211 sony_laptop_remove_input();
2213 err_free_resources:
2214 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2215 list_del(&io->list);
2216 kfree(io);
2218 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2219 list_del(&irq->list);
2220 kfree(irq);
2222 spic_dev.cur_ioport = NULL;
2223 spic_dev.cur_irq = NULL;
2225 return result;
2228 static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
2230 if (sony_pic_disable(device))
2231 return -ENXIO;
2232 return 0;
2235 static int sony_pic_resume(struct acpi_device *device)
2237 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2238 return 0;
2241 static struct acpi_driver sony_pic_driver = {
2242 .name = SONY_PIC_DRIVER_NAME,
2243 .class = SONY_PIC_CLASS,
2244 .ids = SONY_PIC_HID,
2245 .owner = THIS_MODULE,
2246 .ops = {
2247 .add = sony_pic_add,
2248 .remove = sony_pic_remove,
2249 .suspend = sony_pic_suspend,
2250 .resume = sony_pic_resume,
2254 static struct dmi_system_id __initdata sonypi_dmi_table[] = {
2256 .ident = "Sony Vaio",
2257 .matches = {
2258 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2259 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
2263 .ident = "Sony Vaio",
2264 .matches = {
2265 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2266 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
2272 static int __init sony_laptop_init(void)
2274 int result;
2276 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
2277 result = acpi_bus_register_driver(&sony_pic_driver);
2278 if (result) {
2279 printk(KERN_ERR DRV_PFX
2280 "Unable to register SPIC driver.");
2281 goto out;
2285 result = acpi_bus_register_driver(&sony_nc_driver);
2286 if (result) {
2287 printk(KERN_ERR DRV_PFX "Unable to register SNC driver.");
2288 goto out_unregister_pic;
2291 return 0;
2293 out_unregister_pic:
2294 if (!no_spic)
2295 acpi_bus_unregister_driver(&sony_pic_driver);
2296 out:
2297 return result;
2300 static void __exit sony_laptop_exit(void)
2302 acpi_bus_unregister_driver(&sony_nc_driver);
2303 if (!no_spic)
2304 acpi_bus_unregister_driver(&sony_pic_driver);
2307 module_init(sony_laptop_init);
2308 module_exit(sony_laptop_exit);