Merge git://git.kernel.org/pub/scm/linux/kernel/git/holtmann/bluetooth-2.6
[powerpc.git] / drivers / misc / sony-laptop.c
index b797c8c..8ee0321 100644 (file)
 #include <acpi/acpi_bus.h>
 #include <asm/uaccess.h>
 #include <linux/sonypi.h>
+#include <linux/sony-laptop.h>
+#ifdef CONFIG_SONYPI_COMPAT
+#include <linux/poll.h>
+#include <linux/miscdevice.h>
+#endif
 
 #define DRV_PFX                        "sony-laptop: "
-#define LOG_PFX                        KERN_WARNING DRV_PFX
 #define dprintk(msg...)                do {                    \
-       if (debug) printk(LOG_PFX  msg);                \
+       if (debug) printk(KERN_WARNING DRV_PFX  msg);   \
 } while (0)
 
 #define SONY_LAPTOP_DRIVER_VERSION     "0.5"
 
 #define SONY_NC_CLASS          "sony-nc"
 #define SONY_NC_HID            "SNY5001"
-#define SONY_NC_DRIVER_NAME    "Sony Notebook Control"
+#define SONY_NC_DRIVER_NAME    "Sony Notebook Control Driver"
 
 #define SONY_PIC_CLASS         "sony-pic"
 #define SONY_PIC_HID           "SNY6001"
-#define SONY_PIC_DRIVER_NAME   "Sony Programmable IO Control"
+#define SONY_PIC_DRIVER_NAME   "Sony Programmable IO Control Driver"
 
 MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
 MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
@@ -99,16 +103,278 @@ module_param(compat, int, 0444);
 MODULE_PARM_DESC(compat,
                 "set this if you want to enable backward compatibility mode");
 
-static int force_jog;          /* = 0 */
-module_param(force_jog, int, 0444);
-MODULE_PARM_DESC(force_jog,
-                "set this if the driver doesn't detect your jogdial");
-
 static unsigned long mask = 0xffffffff;
 module_param(mask, ulong, 0644);
 MODULE_PARM_DESC(mask,
                 "set this to the mask of event you want to enable (see doc)");
 
+static int camera;             /* = 0 */
+module_param(camera, int, 0444);
+MODULE_PARM_DESC(camera,
+                "set this to 1 to enable Motion Eye camera controls "
+                "(only use it if you have a C1VE or C1VN model)");
+
+#ifdef CONFIG_SONYPI_COMPAT
+static int minor = -1;
+module_param(minor, int, 0);
+MODULE_PARM_DESC(minor,
+                "minor number of the misc device for the SPIC compatibility code, "
+                "default is -1 (automatic)");
+#endif
+
+/*********** Input Devices ***********/
+
+#define SONY_LAPTOP_BUF_SIZE   128
+struct sony_laptop_input_s {
+       atomic_t                users;
+       struct input_dev        *jog_dev;
+       struct input_dev        *key_dev;
+       struct kfifo            *fifo;
+       spinlock_t              fifo_lock;
+       struct workqueue_struct *wq;
+};
+static struct sony_laptop_input_s sony_laptop_input = {
+       .users = ATOMIC_INIT(0),
+};
+
+struct sony_laptop_keypress {
+       struct input_dev *dev;
+       int key;
+};
+
+/* Correspondance table between sonypi events and input layer events */
+static struct {
+       int sonypiev;
+       int inputev;
+} sony_laptop_inputkeys[] = {
+       { SONYPI_EVENT_CAPTURE_PRESSED,         KEY_CAMERA },
+       { SONYPI_EVENT_FNKEY_ONLY,              KEY_FN },
+       { SONYPI_EVENT_FNKEY_ESC,               KEY_FN_ESC },
+       { SONYPI_EVENT_FNKEY_F1,                KEY_FN_F1 },
+       { SONYPI_EVENT_FNKEY_F2,                KEY_FN_F2 },
+       { SONYPI_EVENT_FNKEY_F3,                KEY_FN_F3 },
+       { SONYPI_EVENT_FNKEY_F4,                KEY_FN_F4 },
+       { SONYPI_EVENT_FNKEY_F5,                KEY_FN_F5 },
+       { SONYPI_EVENT_FNKEY_F6,                KEY_FN_F6 },
+       { SONYPI_EVENT_FNKEY_F7,                KEY_FN_F7 },
+       { SONYPI_EVENT_FNKEY_F8,                KEY_FN_F8 },
+       { SONYPI_EVENT_FNKEY_F9,                KEY_FN_F9 },
+       { SONYPI_EVENT_FNKEY_F10,               KEY_FN_F10 },
+       { SONYPI_EVENT_FNKEY_F11,               KEY_FN_F11 },
+       { SONYPI_EVENT_FNKEY_F12,               KEY_FN_F12 },
+       { SONYPI_EVENT_FNKEY_1,                 KEY_FN_1 },
+       { SONYPI_EVENT_FNKEY_2,                 KEY_FN_2 },
+       { SONYPI_EVENT_FNKEY_D,                 KEY_FN_D },
+       { SONYPI_EVENT_FNKEY_E,                 KEY_FN_E },
+       { SONYPI_EVENT_FNKEY_F,                 KEY_FN_F },
+       { SONYPI_EVENT_FNKEY_S,                 KEY_FN_S },
+       { SONYPI_EVENT_FNKEY_B,                 KEY_FN_B },
+       { SONYPI_EVENT_BLUETOOTH_PRESSED,       KEY_BLUE },
+       { SONYPI_EVENT_BLUETOOTH_ON,            KEY_BLUE },
+       { SONYPI_EVENT_PKEY_P1,                 KEY_PROG1 },
+       { SONYPI_EVENT_PKEY_P2,                 KEY_PROG2 },
+       { SONYPI_EVENT_PKEY_P3,                 KEY_PROG3 },
+       { SONYPI_EVENT_BACK_PRESSED,            KEY_BACK },
+       { SONYPI_EVENT_HELP_PRESSED,            KEY_HELP },
+       { SONYPI_EVENT_ZOOM_PRESSED,            KEY_ZOOM },
+       { SONYPI_EVENT_THUMBPHRASE_PRESSED,     BTN_THUMB },
+       { 0, 0 },
+};
+
+/* release buttons after a short delay if pressed */
+static void do_sony_laptop_release_key(struct work_struct *work)
+{
+       struct sony_laptop_keypress kp;
+
+       while (kfifo_get(sony_laptop_input.fifo, (unsigned char *)&kp,
+                        sizeof(kp)) == sizeof(kp)) {
+               msleep(10);
+               input_report_key(kp.dev, kp.key, 0);
+               input_sync(kp.dev);
+       }
+}
+static DECLARE_WORK(sony_laptop_release_key_work,
+               do_sony_laptop_release_key);
+
+/* forward event to the input subsytem */
+static void sony_laptop_report_input_event(u8 event)
+{
+       struct input_dev *jog_dev = sony_laptop_input.jog_dev;
+       struct input_dev *key_dev = sony_laptop_input.key_dev;
+       struct sony_laptop_keypress kp = { NULL };
+       int i;
+
+       if (event == SONYPI_EVENT_FNKEY_RELEASED) {
+               /* Nothing, not all VAIOs generate this event */
+               return;
+       }
+
+       /* report events */
+       switch (event) {
+       /* jog_dev events */
+       case SONYPI_EVENT_JOGDIAL_UP:
+       case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
+               input_report_rel(jog_dev, REL_WHEEL, 1);
+               input_sync(jog_dev);
+               return;
+
+       case SONYPI_EVENT_JOGDIAL_DOWN:
+       case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
+               input_report_rel(jog_dev, REL_WHEEL, -1);
+               input_sync(jog_dev);
+               return;
+
+       /* key_dev events */
+       case SONYPI_EVENT_JOGDIAL_PRESSED:
+               kp.key = BTN_MIDDLE;
+               kp.dev = jog_dev;
+               break;
+
+       default:
+               for (i = 0; sony_laptop_inputkeys[i].sonypiev; i++)
+                       if (event == sony_laptop_inputkeys[i].sonypiev) {
+                               kp.dev = key_dev;
+                               kp.key = sony_laptop_inputkeys[i].inputev;
+                               break;
+                       }
+               break;
+       }
+
+       if (kp.dev) {
+               input_report_key(kp.dev, kp.key, 1);
+               input_sync(kp.dev);
+               kfifo_put(sony_laptop_input.fifo,
+                         (unsigned char *)&kp, sizeof(kp));
+
+               if (!work_pending(&sony_laptop_release_key_work))
+                       queue_work(sony_laptop_input.wq,
+                                       &sony_laptop_release_key_work);
+       } else
+               dprintk("unknown input event %.2x\n", event);
+}
+
+static int sony_laptop_setup_input(void)
+{
+       struct input_dev *jog_dev;
+       struct input_dev *key_dev;
+       int i;
+       int error;
+
+       /* don't run again if already initialized */
+       if (atomic_add_return(1, &sony_laptop_input.users) > 1)
+               return 0;
+
+       /* kfifo */
+       spin_lock_init(&sony_laptop_input.fifo_lock);
+       sony_laptop_input.fifo =
+               kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
+                           &sony_laptop_input.fifo_lock);
+       if (IS_ERR(sony_laptop_input.fifo)) {
+               printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
+               error = PTR_ERR(sony_laptop_input.fifo);
+               goto err_dec_users;
+       }
+
+       /* init workqueue */
+       sony_laptop_input.wq = create_singlethread_workqueue("sony-laptop");
+       if (!sony_laptop_input.wq) {
+               printk(KERN_ERR DRV_PFX
+                               "Unabe to create workqueue.\n");
+               error = -ENXIO;
+               goto err_free_kfifo;
+       }
+
+       /* input keys */
+       key_dev = input_allocate_device();
+       if (!key_dev) {
+               error = -ENOMEM;
+               goto err_destroy_wq;
+       }
+
+       key_dev->name = "Sony Vaio Keys";
+       key_dev->id.bustype = BUS_ISA;
+       key_dev->id.vendor = PCI_VENDOR_ID_SONY;
+
+       /* Initialize the Input Drivers: special keys */
+       key_dev->evbit[0] = BIT(EV_KEY);
+       for (i = 0; sony_laptop_inputkeys[i].sonypiev; i++)
+               if (sony_laptop_inputkeys[i].inputev)
+                       set_bit(sony_laptop_inputkeys[i].inputev,
+                                       key_dev->keybit);
+
+       error = input_register_device(key_dev);
+       if (error)
+               goto err_free_keydev;
+
+       sony_laptop_input.key_dev = key_dev;
+
+       /* jogdial */
+       jog_dev = input_allocate_device();
+       if (!jog_dev) {
+               error = -ENOMEM;
+               goto err_unregister_keydev;
+       }
+
+       jog_dev->name = "Sony Vaio Jogdial";
+       jog_dev->id.bustype = BUS_ISA;
+       jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
+
+       jog_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
+       jog_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_MIDDLE);
+       jog_dev->relbit[0] = BIT(REL_WHEEL);
+
+       error = input_register_device(jog_dev);
+       if (error)
+               goto err_free_jogdev;
+
+       sony_laptop_input.jog_dev = jog_dev;
+
+       return 0;
+
+err_free_jogdev:
+       input_free_device(jog_dev);
+
+err_unregister_keydev:
+       input_unregister_device(key_dev);
+       /* to avoid kref underflow below at input_free_device */
+       key_dev = NULL;
+
+err_free_keydev:
+       input_free_device(key_dev);
+
+err_destroy_wq:
+       destroy_workqueue(sony_laptop_input.wq);
+
+err_free_kfifo:
+       kfifo_free(sony_laptop_input.fifo);
+
+err_dec_users:
+       atomic_dec(&sony_laptop_input.users);
+       return error;
+}
+
+static void sony_laptop_remove_input(void)
+{
+       /* cleanup only after the last user has gone */
+       if (!atomic_dec_and_test(&sony_laptop_input.users))
+               return;
+
+       /* flush workqueue first */
+       flush_workqueue(sony_laptop_input.wq);
+
+       /* destroy input devs */
+       input_unregister_device(sony_laptop_input.key_dev);
+       sony_laptop_input.key_dev = NULL;
+
+       if (sony_laptop_input.jog_dev) {
+               input_unregister_device(sony_laptop_input.jog_dev);
+               sony_laptop_input.jog_dev = NULL;
+       }
+
+       destroy_workqueue(sony_laptop_input.wq);
+       kfifo_free(sony_laptop_input.fifo);
+}
+
 /*********** Platform Device ***********/
 
 static atomic_t sony_pf_users = ATOMIC_INIT(0);
@@ -270,7 +536,7 @@ static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
                return 0;
        }
 
-       printk(LOG_PFX "acpi_callreadfunc failed\n");
+       printk(KERN_WARNING DRV_PFX "acpi_callreadfunc failed\n");
 
        return -1;
 }
@@ -296,7 +562,7 @@ static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
        if (status == AE_OK) {
                if (result != NULL) {
                        if (out_obj.type != ACPI_TYPE_INTEGER) {
-                               printk(LOG_PFX "acpi_evaluate_object bad "
+                               printk(KERN_WARNING DRV_PFX "acpi_evaluate_object bad "
                                       "return type\n");
                                return -1;
                        }
@@ -305,7 +571,7 @@ static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
                return 0;
        }
 
-       printk(LOG_PFX "acpi_evaluate_object failed\n");
+       printk(KERN_WARNING DRV_PFX "acpi_evaluate_object failed\n");
 
        return -1;
 }
@@ -428,6 +694,7 @@ static struct backlight_ops sony_backlight_ops = {
 static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
 {
        dprintk("sony_acpi_notify, event: %d\n", event);
+       sony_laptop_report_input_event(event);
        acpi_bus_generate_event(sony_nc_acpi_device, 1, event);
 }
 
@@ -440,7 +707,7 @@ static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
        node = (struct acpi_namespace_node *)handle;
        operand = (union acpi_operand_object *)node->object;
 
-       printk(LOG_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
+       printk(KERN_WARNING DRV_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
               (u32) operand->method.param_count);
 
        return AE_OK;
@@ -475,6 +742,9 @@ static int sony_nc_add(struct acpi_device *device)
        acpi_handle handle;
        struct sony_nc_value *item;
 
+       printk(KERN_INFO DRV_PFX "%s v%s.\n",
+               SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
+
        sony_nc_acpi_device = device;
        strcpy(acpi_device_class(device), "sony/hotkey");
 
@@ -484,19 +754,27 @@ static int sony_nc_add(struct acpi_device *device)
                status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_nc_acpi_handle,
                                             1, sony_walk_callback, NULL, NULL);
                if (ACPI_FAILURE(status)) {
-                       printk(LOG_PFX "unable to walk acpi resources\n");
+                       printk(KERN_WARNING DRV_PFX "unable to walk acpi resources\n");
                        result = -ENODEV;
                        goto outwalk;
                }
        }
 
+       /* setup input devices and helper fifo */
+       result = sony_laptop_setup_input();
+       if (result) {
+               printk(KERN_ERR DRV_PFX
+                               "Unabe to create input devices.\n");
+               goto outwalk;
+       }
+
        status = acpi_install_notify_handler(sony_nc_acpi_handle,
                                             ACPI_DEVICE_NOTIFY,
                                             sony_acpi_notify, NULL);
        if (ACPI_FAILURE(status)) {
-               printk(LOG_PFX "unable to install notify handler\n");
+               printk(KERN_WARNING DRV_PFX "unable to install notify handler\n");
                result = -ENODEV;
-               goto outwalk;
+               goto outinput;
        }
 
        if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT", &handle))) {
@@ -505,7 +783,7 @@ static int sony_nc_add(struct acpi_device *device)
                                                                  &sony_backlight_ops);
 
                if (IS_ERR(sony_backlight_device)) {
-                       printk(LOG_PFX "unable to register backlight device\n");
+                       printk(KERN_WARNING DRV_PFX "unable to register backlight device\n");
                        sony_backlight_device = NULL;
                } else {
                        sony_backlight_device->props.brightness =
@@ -517,7 +795,8 @@ static int sony_nc_add(struct acpi_device *device)
 
        }
 
-       if (sony_pf_add())
+       result = sony_pf_add();
+       if (result)
                goto outbacklight;
 
        /* create sony_pf sysfs attributes related to the SNC device */
@@ -559,8 +838,6 @@ static int sony_nc_add(struct acpi_device *device)
                }
        }
 
-       printk(KERN_INFO SONY_NC_DRIVER_NAME " successfully installed\n");
-
        return 0;
 
       out_sysfs:
@@ -568,6 +845,7 @@ static int sony_nc_add(struct acpi_device *device)
                device_remove_file(&sony_pf_device->dev, &item->devattr);
        }
        sony_pf_remove();
+
       outbacklight:
        if (sony_backlight_device)
                backlight_device_unregister(sony_backlight_device);
@@ -576,7 +854,11 @@ static int sony_nc_add(struct acpi_device *device)
                                            ACPI_DEVICE_NOTIFY,
                                            sony_acpi_notify);
        if (ACPI_FAILURE(status))
-               printk(LOG_PFX "unable to remove notify handler\n");
+               printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
+
+      outinput:
+       sony_laptop_remove_input();
+
       outwalk:
        return result;
 }
@@ -595,15 +877,15 @@ static int sony_nc_remove(struct acpi_device *device, int type)
                                            ACPI_DEVICE_NOTIFY,
                                            sony_acpi_notify);
        if (ACPI_FAILURE(status))
-               printk(LOG_PFX "unable to remove notify handler\n");
+               printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
 
        for (item = sony_nc_values; item->name; ++item) {
                device_remove_file(&sony_pf_device->dev, &item->devattr);
        }
 
        sony_pf_remove();
-
-       printk(KERN_INFO SONY_NC_DRIVER_NAME " successfully removed\n");
+       sony_laptop_remove_input();
+       dprintk(SONY_NC_DRIVER_NAME " removed.\n");
 
        return 0;
 }
@@ -626,13 +908,8 @@ static struct acpi_driver sony_nc_driver = {
 #define SONYPI_DEVICE_TYPE2    0x00000002
 #define SONYPI_DEVICE_TYPE3    0x00000004
 
-#define SONY_EC_JOGB           0x82
-#define SONY_EC_JOGB_MASK      0x02
-
 #define SONY_PIC_EV_MASK       0xff
 
-#define SONYPI_BUF_SIZE        128
-
 struct sony_pic_ioport {
        struct acpi_resource_io io;
        struct list_head        list;
@@ -645,17 +922,15 @@ struct sony_pic_irq {
 
 struct sony_pic_dev {
        int                     model;
+       u8                      camera_power;
+       u8                      bluetooth_power;
+       u8                      wwan_power;
        struct acpi_device      *acpi_dev;
        struct sony_pic_irq     *cur_irq;
        struct sony_pic_ioport  *cur_ioport;
        struct list_head        interrupts;
        struct list_head        ioports;
-
-       struct input_dev        *input_jog_dev;
-       struct input_dev        *input_key_dev;
-       struct kfifo            *input_fifo;
-       spinlock_t              input_fifo_lock;
-       struct workqueue_struct *sony_pic_wq;
+       struct mutex            lock;
 };
 
 static struct sony_pic_dev spic_dev = {
@@ -929,250 +1204,605 @@ static u8 sony_pic_call2(u8 dev, u8 fn)
        return v1;
 }
 
-/*****************
- *
- * INPUT Device
- *
- *****************/
-struct sony_pic_keypress {
-       struct input_dev *dev;
-       int key;
-};
+static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
+{
+       u8 v1;
 
-/* Correspondance table between sonypi events and input layer events */
-static struct {
-       int sonypiev;
-       int inputev;
-} sony_pic_inputkeys[] = {
-       { SONYPI_EVENT_CAPTURE_PRESSED,         KEY_CAMERA },
-       { SONYPI_EVENT_FNKEY_ONLY,              KEY_FN },
-       { SONYPI_EVENT_FNKEY_ESC,               KEY_FN_ESC },
-       { SONYPI_EVENT_FNKEY_F1,                KEY_FN_F1 },
-       { SONYPI_EVENT_FNKEY_F2,                KEY_FN_F2 },
-       { SONYPI_EVENT_FNKEY_F3,                KEY_FN_F3 },
-       { SONYPI_EVENT_FNKEY_F4,                KEY_FN_F4 },
-       { SONYPI_EVENT_FNKEY_F5,                KEY_FN_F5 },
-       { SONYPI_EVENT_FNKEY_F6,                KEY_FN_F6 },
-       { SONYPI_EVENT_FNKEY_F7,                KEY_FN_F7 },
-       { SONYPI_EVENT_FNKEY_F8,                KEY_FN_F8 },
-       { SONYPI_EVENT_FNKEY_F9,                KEY_FN_F9 },
-       { SONYPI_EVENT_FNKEY_F10,               KEY_FN_F10 },
-       { SONYPI_EVENT_FNKEY_F11,               KEY_FN_F11 },
-       { SONYPI_EVENT_FNKEY_F12,               KEY_FN_F12 },
-       { SONYPI_EVENT_FNKEY_1,                 KEY_FN_1 },
-       { SONYPI_EVENT_FNKEY_2,                 KEY_FN_2 },
-       { SONYPI_EVENT_FNKEY_D,                 KEY_FN_D },
-       { SONYPI_EVENT_FNKEY_E,                 KEY_FN_E },
-       { SONYPI_EVENT_FNKEY_F,                 KEY_FN_F },
-       { SONYPI_EVENT_FNKEY_S,                 KEY_FN_S },
-       { SONYPI_EVENT_FNKEY_B,                 KEY_FN_B },
-       { SONYPI_EVENT_BLUETOOTH_PRESSED,       KEY_BLUE },
-       { SONYPI_EVENT_BLUETOOTH_ON,            KEY_BLUE },
-       { SONYPI_EVENT_PKEY_P1,                 KEY_PROG1 },
-       { SONYPI_EVENT_PKEY_P2,                 KEY_PROG2 },
-       { SONYPI_EVENT_PKEY_P3,                 KEY_PROG3 },
-       { SONYPI_EVENT_BACK_PRESSED,            KEY_BACK },
-       { SONYPI_EVENT_HELP_PRESSED,            KEY_HELP },
-       { SONYPI_EVENT_ZOOM_PRESSED,            KEY_ZOOM },
-       { SONYPI_EVENT_THUMBPHRASE_PRESSED,     BTN_THUMB },
-       { 0, 0 },
-};
+       wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
+       outb(dev, spic_dev.cur_ioport->io.minimum + 4);
+       wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
+       outb(fn, spic_dev.cur_ioport->io.minimum);
+       wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
+       outb(v, spic_dev.cur_ioport->io.minimum);
+       v1 = inb_p(spic_dev.cur_ioport->io.minimum);
+       dprintk("sony_pic_call3: 0x%.4x\n", v1);
+       return v1;
+}
 
-/* release buttons after a short delay if pressed */
-static void do_sony_pic_release_key(struct work_struct *work)
+/* camera tests and poweron/poweroff */
+#define SONYPI_CAMERA_PICTURE          5
+#define SONYPI_CAMERA_CONTROL          0x10
+
+#define SONYPI_CAMERA_BRIGHTNESS               0
+#define SONYPI_CAMERA_CONTRAST                 1
+#define SONYPI_CAMERA_HUE                      2
+#define SONYPI_CAMERA_COLOR                    3
+#define SONYPI_CAMERA_SHARPNESS                        4
+
+#define SONYPI_CAMERA_EXPOSURE_MASK            0xC
+#define SONYPI_CAMERA_WHITE_BALANCE_MASK       0x3
+#define SONYPI_CAMERA_PICTURE_MODE_MASK                0x30
+#define SONYPI_CAMERA_MUTE_MASK                        0x40
+
+/* the rest don't need a loop until not 0xff */
+#define SONYPI_CAMERA_AGC                      6
+#define SONYPI_CAMERA_AGC_MASK                 0x30
+#define SONYPI_CAMERA_SHUTTER_MASK             0x7
+
+#define SONYPI_CAMERA_SHUTDOWN_REQUEST         7
+#define SONYPI_CAMERA_CONTROL                  0x10
+
+#define SONYPI_CAMERA_STATUS                   7
+#define SONYPI_CAMERA_STATUS_READY             0x2
+#define SONYPI_CAMERA_STATUS_POSITION          0x4
+
+#define SONYPI_DIRECTION_BACKWARDS             0x4
+
+#define SONYPI_CAMERA_REVISION                         8
+#define SONYPI_CAMERA_ROMVERSION               9
+
+static int __sony_pic_camera_ready(void)
 {
-       struct sony_pic_keypress kp;
+       u8 v;
 
-       while (kfifo_get(spic_dev.input_fifo, (unsigned char *)&kp,
-                        sizeof(kp)) == sizeof(kp)) {
-               msleep(10);
-               input_report_key(kp.dev, kp.key, 0);
-               input_sync(kp.dev);
+       v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
+       return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
+}
+
+static int __sony_pic_camera_off(void)
+{
+       if (!camera) {
+               printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
+               return -ENODEV;
        }
+
+       wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
+                               SONYPI_CAMERA_MUTE_MASK),
+                       ITERATIONS_SHORT);
+
+       if (spic_dev.camera_power) {
+               sony_pic_call2(0x91, 0);
+               spic_dev.camera_power = 0;
+       }
+       return 0;
 }
-static DECLARE_WORK(sony_pic_release_key_work,
-               do_sony_pic_release_key);
 
-/* forward event to the input subsytem */
-static void sony_pic_report_input_event(u8 event)
+static int __sony_pic_camera_on(void)
 {
-       struct input_dev *jog_dev = spic_dev.input_jog_dev;
-       struct input_dev *key_dev = spic_dev.input_key_dev;
-       struct sony_pic_keypress kp = { NULL };
-       int i;
+       int i, j, x;
 
-       if (event == SONYPI_EVENT_FNKEY_RELEASED) {
-               /* Nothing, not all VAIOs generate this event */
-               return;
+       if (!camera) {
+               printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
+               return -ENODEV;
        }
 
-       /* report jog_dev events */
-       if (jog_dev) {
-               switch (event) {
-               case SONYPI_EVENT_JOGDIAL_UP:
-               case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
-                       input_report_rel(jog_dev, REL_WHEEL, 1);
-                       input_sync(jog_dev);
-                       return;
-
-               case SONYPI_EVENT_JOGDIAL_DOWN:
-               case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
-                       input_report_rel(jog_dev, REL_WHEEL, -1);
-                       input_sync(jog_dev);
-                       return;
-
-               default:
-                       break;
+       if (spic_dev.camera_power)
+               return 0;
+
+       for (j = 5; j > 0; j--) {
+
+               for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
+                       msleep(10);
+               sony_pic_call1(0x93);
+
+               for (i = 400; i > 0; i--) {
+                       if (__sony_pic_camera_ready())
+                               break;
+                       msleep(10);
                }
+               if (i)
+                       break;
        }
 
-       switch (event) {
-       case SONYPI_EVENT_JOGDIAL_PRESSED:
-               kp.key = BTN_MIDDLE;
-               kp.dev = jog_dev;
-               break;
+       if (j == 0) {
+               printk(KERN_WARNING DRV_PFX "failed to power on camera\n");
+               return -ENODEV;
+       }
+
+       wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
+                               0x5a),
+                       ITERATIONS_SHORT);
+
+       spic_dev.camera_power = 1;
+       return 0;
+}
+
+/* External camera command (exported to the motion eye v4l driver) */
+int sony_pic_camera_command(int command, u8 value)
+{
+       if (!camera)
+               return -EIO;
 
+       mutex_lock(&spic_dev.lock);
+
+       switch (command) {
+       case SONY_PIC_COMMAND_SETCAMERA:
+               if (value)
+                       __sony_pic_camera_on();
+               else
+                       __sony_pic_camera_off();
+               break;
+       case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
+               wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
+                               ITERATIONS_SHORT);
+               break;
+       case SONY_PIC_COMMAND_SETCAMERACONTRAST:
+               wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
+                               ITERATIONS_SHORT);
+               break;
+       case SONY_PIC_COMMAND_SETCAMERAHUE:
+               wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
+                               ITERATIONS_SHORT);
+               break;
+       case SONY_PIC_COMMAND_SETCAMERACOLOR:
+               wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
+                               ITERATIONS_SHORT);
+               break;
+       case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
+               wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
+                               ITERATIONS_SHORT);
+               break;
+       case SONY_PIC_COMMAND_SETCAMERAPICTURE:
+               wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
+                               ITERATIONS_SHORT);
+               break;
+       case SONY_PIC_COMMAND_SETCAMERAAGC:
+               wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
+                               ITERATIONS_SHORT);
+               break;
        default:
-               for (i = 0; sony_pic_inputkeys[i].sonypiev; i++)
-                       if (event == sony_pic_inputkeys[i].sonypiev) {
-                               kp.dev = key_dev;
-                               kp.key = sony_pic_inputkeys[i].inputev;
-                               break;
-                       }
+               printk(KERN_ERR DRV_PFX "sony_pic_camera_command invalid: %d\n",
+                      command);
                break;
        }
+       mutex_unlock(&spic_dev.lock);
+       return 0;
+}
+EXPORT_SYMBOL(sony_pic_camera_command);
 
-       if (kp.dev) {
-               input_report_key(kp.dev, kp.key, 1);
-               input_sync(kp.dev);
-               kfifo_put(spic_dev.input_fifo,
-                         (unsigned char *)&kp, sizeof(kp));
-
-               if (!work_pending(&sony_pic_release_key_work))
-                       queue_work(spic_dev.sony_pic_wq,
-                                       &sony_pic_release_key_work);
+/* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
+static void sony_pic_set_wwanpower(u8 state)
+{
+       state = !!state;
+       mutex_lock(&spic_dev.lock);
+       if (spic_dev.wwan_power == state) {
+               mutex_unlock(&spic_dev.lock);
+               return;
        }
+       sony_pic_call2(0xB0, state);
+       spic_dev.wwan_power = state;
+       mutex_unlock(&spic_dev.lock);
 }
 
-static int sony_pic_setup_input(void)
+static ssize_t sony_pic_wwanpower_store(struct device *dev,
+               struct device_attribute *attr,
+               const char *buffer, size_t count)
 {
-       struct input_dev *jog_dev;
-       struct input_dev *key_dev;
-       int i;
-       int error;
-       u8 jog_present = 0;
+       unsigned long value;
+       if (count > 31)
+               return -EINVAL;
 
-       /* kfifo */
-       spin_lock_init(&spic_dev.input_fifo_lock);
-       spic_dev.input_fifo =
-               kfifo_alloc(SONYPI_BUF_SIZE, GFP_KERNEL,
-                           &spic_dev.input_fifo_lock);
-       if (IS_ERR(spic_dev.input_fifo)) {
-               printk(KERN_ERR "sonypi: kfifo_alloc failed\n");
-               return PTR_ERR(spic_dev.input_fifo);
-       }
+       value = simple_strtoul(buffer, NULL, 10);
+       sony_pic_set_wwanpower(value);
 
-       /* init workqueue */
-       spic_dev.sony_pic_wq = create_singlethread_workqueue("sony-pic");
-       if (!spic_dev.sony_pic_wq) {
-               printk(KERN_ERR DRV_PFX
-                               "Unabe to create workqueue.\n");
-               error = -ENXIO;
-               goto err_free_kfifo;
-       }
+       return count;
+}
 
-       /* input keys */
-       key_dev = input_allocate_device();
-       if (!key_dev) {
-               error = -ENOMEM;
-               goto err_destroy_wq;
-       }
+static ssize_t sony_pic_wwanpower_show(struct device *dev,
+               struct device_attribute *attr, char *buffer)
+{
+       ssize_t count;
+       mutex_lock(&spic_dev.lock);
+       count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
+       mutex_unlock(&spic_dev.lock);
+       return count;
+}
 
-       key_dev->name = "Sony Vaio Keys";
-       key_dev->id.bustype = BUS_ISA;
-       key_dev->id.vendor = PCI_VENDOR_ID_SONY;
+/* bluetooth subsystem power state */
+static void __sony_pic_set_bluetoothpower(u8 state)
+{
+       state = !!state;
+       if (spic_dev.bluetooth_power == state)
+               return;
+       sony_pic_call2(0x96, state);
+       sony_pic_call1(0x82);
+       spic_dev.bluetooth_power = state;
+}
 
-       /* Initialize the Input Drivers: special keys */
-       key_dev->evbit[0] = BIT(EV_KEY);
-       for (i = 0; sony_pic_inputkeys[i].sonypiev; i++)
-               if (sony_pic_inputkeys[i].inputev)
-                       set_bit(sony_pic_inputkeys[i].inputev, key_dev->keybit);
+static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
+               struct device_attribute *attr,
+               const char *buffer, size_t count)
+{
+       unsigned long value;
+       if (count > 31)
+               return -EINVAL;
 
-       error = input_register_device(key_dev);
-       if (error)
-               goto err_free_keydev;
+       value = simple_strtoul(buffer, NULL, 10);
+       mutex_lock(&spic_dev.lock);
+       __sony_pic_set_bluetoothpower(value);
+       mutex_unlock(&spic_dev.lock);
 
-       spic_dev.input_key_dev = key_dev;
+       return count;
+}
 
-       /* jogdial - really reliable ? */
-       ec_read(SONY_EC_JOGB, &jog_present);
-       if (jog_present & SONY_EC_JOGB_MASK || force_jog) {
-               jog_dev = input_allocate_device();
-               if (!jog_dev) {
-                       error = -ENOMEM;
-                       goto err_unregister_keydev;
-               }
+static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
+               struct device_attribute *attr, char *buffer)
+{
+       ssize_t count = 0;
+       mutex_lock(&spic_dev.lock);
+       count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
+       mutex_unlock(&spic_dev.lock);
+       return count;
+}
+
+/* fan speed */
+/* FAN0 information (reverse engineered from ACPI tables) */
+#define SONY_PIC_FAN0_STATUS   0x93
+static int sony_pic_set_fanspeed(unsigned long value)
+{
+       return ec_write(SONY_PIC_FAN0_STATUS, value);
+}
+
+static int sony_pic_get_fanspeed(u8 *value)
+{
+       return ec_read(SONY_PIC_FAN0_STATUS, value);
+}
 
-               jog_dev->name = "Sony Vaio Jogdial";
-               jog_dev->id.bustype = BUS_ISA;
-               jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
+static ssize_t sony_pic_fanspeed_store(struct device *dev,
+               struct device_attribute *attr,
+               const char *buffer, size_t count)
+{
+       unsigned long value;
+       if (count > 31)
+               return -EINVAL;
+
+       value = simple_strtoul(buffer, NULL, 10);
+       if (sony_pic_set_fanspeed(value))
+               return -EIO;
+
+       return count;
+}
+
+static ssize_t sony_pic_fanspeed_show(struct device *dev,
+               struct device_attribute *attr, char *buffer)
+{
+       u8 value = 0;
+       if (sony_pic_get_fanspeed(&value))
+               return -EIO;
+
+       return snprintf(buffer, PAGE_SIZE, "%d\n", value);
+}
 
-               jog_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
-               jog_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_MIDDLE);
-               jog_dev->relbit[0] = BIT(REL_WHEEL);
+#define SPIC_ATTR(_name, _mode)                                        \
+struct device_attribute spic_attr_##_name = __ATTR(_name,      \
+               _mode, sony_pic_## _name ##_show,               \
+               sony_pic_## _name ##_store)
 
-               error = input_register_device(jog_dev);
-               if (error)
-                       goto err_free_jogdev;
+static SPIC_ATTR(bluetoothpower, 0644);
+static SPIC_ATTR(wwanpower, 0644);
+static SPIC_ATTR(fanspeed, 0644);
 
-               spic_dev.input_jog_dev = jog_dev;
+static struct attribute *spic_attributes[] = {
+       &spic_attr_bluetoothpower.attr,
+       &spic_attr_wwanpower.attr,
+       &spic_attr_fanspeed.attr,
+       NULL
+};
+
+static struct attribute_group spic_attribute_group = {
+       .attrs = spic_attributes
+};
+
+/******** SONYPI compatibility **********/
+#ifdef CONFIG_SONYPI_COMPAT
+
+/* battery / brightness / temperature  addresses */
+#define SONYPI_BAT_FLAGS       0x81
+#define SONYPI_LCD_LIGHT       0x96
+#define SONYPI_BAT1_PCTRM      0xa0
+#define SONYPI_BAT1_LEFT       0xa2
+#define SONYPI_BAT1_MAXRT      0xa4
+#define SONYPI_BAT2_PCTRM      0xa8
+#define SONYPI_BAT2_LEFT       0xaa
+#define SONYPI_BAT2_MAXRT      0xac
+#define SONYPI_BAT1_MAXTK      0xb0
+#define SONYPI_BAT1_FULL       0xb2
+#define SONYPI_BAT2_MAXTK      0xb8
+#define SONYPI_BAT2_FULL       0xba
+#define SONYPI_TEMP_STATUS     0xC1
+
+struct sonypi_compat_s {
+       struct fasync_struct    *fifo_async;
+       struct kfifo            *fifo;
+       spinlock_t              fifo_lock;
+       wait_queue_head_t       fifo_proc_list;
+       atomic_t                open_count;
+};
+static struct sonypi_compat_s sonypi_compat = {
+       .open_count = ATOMIC_INIT(0),
+};
+
+static int sonypi_misc_fasync(int fd, struct file *filp, int on)
+{
+       int retval;
+
+       retval = fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
+       if (retval < 0)
+               return retval;
+       return 0;
+}
+
+static int sonypi_misc_release(struct inode *inode, struct file *file)
+{
+       sonypi_misc_fasync(-1, file, 0);
+       atomic_dec(&sonypi_compat.open_count);
+       return 0;
+}
+
+static int sonypi_misc_open(struct inode *inode, struct file *file)
+{
+       /* Flush input queue on first open */
+       if (atomic_inc_return(&sonypi_compat.open_count) == 1)
+               kfifo_reset(sonypi_compat.fifo);
+       return 0;
+}
+
+static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
+                               size_t count, loff_t *pos)
+{
+       ssize_t ret;
+       unsigned char c;
+
+       if ((kfifo_len(sonypi_compat.fifo) == 0) &&
+           (file->f_flags & O_NONBLOCK))
+               return -EAGAIN;
+
+       ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
+                                      kfifo_len(sonypi_compat.fifo) != 0);
+       if (ret)
+               return ret;
+
+       while (ret < count &&
+              (kfifo_get(sonypi_compat.fifo, &c, sizeof(c)) == sizeof(c))) {
+               if (put_user(c, buf++))
+                       return -EFAULT;
+               ret++;
+       }
+
+       if (ret > 0) {
+               struct inode *inode = file->f_path.dentry->d_inode;
+               inode->i_atime = current_fs_time(inode->i_sb);
        }
 
+       return ret;
+}
+
+static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
+{
+       poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
+       if (kfifo_len(sonypi_compat.fifo))
+               return POLLIN | POLLRDNORM;
        return 0;
+}
 
-err_free_jogdev:
-       input_free_device(jog_dev);
+static int ec_read16(u8 addr, u16 *value)
+{
+       u8 val_lb, val_hb;
+       if (ec_read(addr, &val_lb))
+               return -1;
+       if (ec_read(addr + 1, &val_hb))
+               return -1;
+       *value = val_lb | (val_hb << 8);
+       return 0;
+}
 
-err_unregister_keydev:
-       input_unregister_device(key_dev);
-       /* to avoid kref underflow below at input_free_device */
-       key_dev = NULL;
+static int sonypi_misc_ioctl(struct inode *ip, struct file *fp,
+                            unsigned int cmd, unsigned long arg)
+{
+       int ret = 0;
+       void __user *argp = (void __user *)arg;
+       u8 val8;
+       u16 val16;
+       int value;
 
-err_free_keydev:
-       input_free_device(key_dev);
+       mutex_lock(&spic_dev.lock);
+       switch (cmd) {
+       case SONYPI_IOCGBRT:
+               if (sony_backlight_device == NULL) {
+                       ret = -EIO;
+                       break;
+               }
+               if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
+                       ret = -EIO;
+                       break;
+               }
+               val8 = ((value & 0xff) - 1) << 5;
+               if (copy_to_user(argp, &val8, sizeof(val8)))
+                               ret = -EFAULT;
+               break;
+       case SONYPI_IOCSBRT:
+               if (sony_backlight_device == NULL) {
+                       ret = -EIO;
+                       break;
+               }
+               if (copy_from_user(&val8, argp, sizeof(val8))) {
+                       ret = -EFAULT;
+                       break;
+               }
+               if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
+                               (val8 >> 5) + 1, NULL)) {
+                       ret = -EIO;
+                       break;
+               }
+               /* sync the backlight device status */
+               sony_backlight_device->props.brightness =
+                   sony_backlight_get_brightness(sony_backlight_device);
+               break;
+       case SONYPI_IOCGBAT1CAP:
+               if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
+                       ret = -EIO;
+                       break;
+               }
+               if (copy_to_user(argp, &val16, sizeof(val16)))
+                       ret = -EFAULT;
+               break;
+       case SONYPI_IOCGBAT1REM:
+               if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
+                       ret = -EIO;
+                       break;
+               }
+               if (copy_to_user(argp, &val16, sizeof(val16)))
+                       ret = -EFAULT;
+               break;
+       case SONYPI_IOCGBAT2CAP:
+               if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
+                       ret = -EIO;
+                       break;
+               }
+               if (copy_to_user(argp, &val16, sizeof(val16)))
+                       ret = -EFAULT;
+               break;
+       case SONYPI_IOCGBAT2REM:
+               if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
+                       ret = -EIO;
+                       break;
+               }
+               if (copy_to_user(argp, &val16, sizeof(val16)))
+                       ret = -EFAULT;
+               break;
+       case SONYPI_IOCGBATFLAGS:
+               if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
+                       ret = -EIO;
+                       break;
+               }
+               val8 &= 0x07;
+               if (copy_to_user(argp, &val8, sizeof(val8)))
+                       ret = -EFAULT;
+               break;
+       case SONYPI_IOCGBLUE:
+               val8 = spic_dev.bluetooth_power;
+               if (copy_to_user(argp, &val8, sizeof(val8)))
+                       ret = -EFAULT;
+               break;
+       case SONYPI_IOCSBLUE:
+               if (copy_from_user(&val8, argp, sizeof(val8))) {
+                       ret = -EFAULT;
+                       break;
+               }
+               __sony_pic_set_bluetoothpower(val8);
+               break;
+       /* FAN Controls */
+       case SONYPI_IOCGFAN:
+               if (sony_pic_get_fanspeed(&val8)) {
+                       ret = -EIO;
+                       break;
+               }
+               if (copy_to_user(argp, &val8, sizeof(val8)))
+                       ret = -EFAULT;
+               break;
+       case SONYPI_IOCSFAN:
+               if (copy_from_user(&val8, argp, sizeof(val8))) {
+                       ret = -EFAULT;
+                       break;
+               }
+               if (sony_pic_set_fanspeed(val8))
+                       ret = -EIO;
+               break;
+       /* GET Temperature (useful under APM) */
+       case SONYPI_IOCGTEMP:
+               if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
+                       ret = -EIO;
+                       break;
+               }
+               if (copy_to_user(argp, &val8, sizeof(val8)))
+                       ret = -EFAULT;
+               break;
+       default:
+               ret = -EINVAL;
+       }
+       mutex_unlock(&spic_dev.lock);
+       return ret;
+}
 
-err_destroy_wq:
-       destroy_workqueue(spic_dev.sony_pic_wq);
+static const struct file_operations sonypi_misc_fops = {
+       .owner          = THIS_MODULE,
+       .read           = sonypi_misc_read,
+       .poll           = sonypi_misc_poll,
+       .open           = sonypi_misc_open,
+       .release        = sonypi_misc_release,
+       .fasync         = sonypi_misc_fasync,
+       .ioctl          = sonypi_misc_ioctl,
+};
 
-err_free_kfifo:
-       kfifo_free(spic_dev.input_fifo);
+static struct miscdevice sonypi_misc_device = {
+       .minor          = MISC_DYNAMIC_MINOR,
+       .name           = "sonypi",
+       .fops           = &sonypi_misc_fops,
+};
 
-       return error;
+static void sonypi_compat_report_event(u8 event)
+{
+       kfifo_put(sonypi_compat.fifo, (unsigned char *)&event, sizeof(event));
+       kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
+       wake_up_interruptible(&sonypi_compat.fifo_proc_list);
 }
 
-static void sony_pic_remove_input(void)
+static int sonypi_compat_init(void)
 {
-       /* flush workqueue first */
-       flush_workqueue(spic_dev.sony_pic_wq);
+       int error;
 
-       /* destroy input devs */
-       input_unregister_device(spic_dev.input_key_dev);
-       spic_dev.input_key_dev = NULL;
+       spin_lock_init(&sonypi_compat.fifo_lock);
+       sonypi_compat.fifo = kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
+                                        &sonypi_compat.fifo_lock);
+       if (IS_ERR(sonypi_compat.fifo)) {
+               printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
+               return PTR_ERR(sonypi_compat.fifo);
+       }
 
-       if (spic_dev.input_jog_dev) {
-               input_unregister_device(spic_dev.input_jog_dev);
-               spic_dev.input_jog_dev = NULL;
+       init_waitqueue_head(&sonypi_compat.fifo_proc_list);
+
+       if (minor != -1)
+               sonypi_misc_device.minor = minor;
+       error = misc_register(&sonypi_misc_device);
+       if (error) {
+               printk(KERN_ERR DRV_PFX "misc_register failed\n");
+               goto err_free_kfifo;
        }
+       if (minor == -1)
+               printk(KERN_INFO DRV_PFX "device allocated minor is %d\n",
+                      sonypi_misc_device.minor);
+
+       return 0;
 
-       destroy_workqueue(spic_dev.sony_pic_wq);
-       kfifo_free(spic_dev.input_fifo);
+err_free_kfifo:
+       kfifo_free(sonypi_compat.fifo);
+       return error;
 }
 
-/********************
- *
+static void sonypi_compat_exit(void)
+{
+       misc_deregister(&sonypi_misc_device);
+       kfifo_free(sonypi_compat.fifo);
+}
+#else
+static int sonypi_compat_init(void) { return 0; }
+static void sonypi_compat_exit(void) { }
+static void sonypi_compat_report_event(u8 event) { }
+#endif /* CONFIG_SONYPI_COMPAT */
+
+/*
  * ACPI callbacks
- *
- ********************/
+ */
 static acpi_status
 sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
 {
@@ -1208,7 +1838,7 @@ sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
                                if (!interrupt)
                                        return AE_ERROR;
 
-                               list_add(&interrupt->list, &dev->interrupts);
+                               list_add_tail(&interrupt->list, &dev->interrupts);
                                interrupt->irq.triggering = p->triggering;
                                interrupt->irq.polarity = p->polarity;
                                interrupt->irq.sharable = p->sharable;
@@ -1230,7 +1860,7 @@ sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
                        if (!ioport)
                                return AE_ERROR;
 
-                       list_add(&ioport->list, &dev->ioports);
+                       list_add_tail(&ioport->list, &dev->ioports);
                        memcpy(&ioport->io, io, sizeof(*io));
                        return AE_OK;
                }
@@ -1409,8 +2039,9 @@ static irqreturn_t sony_pic_irq(int irq, void *dev_id)
        return IRQ_HANDLED;
 
 found:
-       sony_pic_report_input_event(device_event);
+       sony_laptop_report_input_event(device_event);
        acpi_bus_generate_event(spic_dev.acpi_dev, 1, device_event);
+       sonypi_compat_report_event(device_event);
 
        return IRQ_HANDLED;
 }
@@ -1425,6 +2056,8 @@ static int sony_pic_remove(struct acpi_device *device, int type)
        struct sony_pic_ioport *io, *tmp_io;
        struct sony_pic_irq *irq, *tmp_irq;
 
+       sonypi_compat_exit();
+
        if (sony_pic_disable(device)) {
                printk(KERN_ERR DRV_PFX "Couldn't disable device.\n");
                return -ENXIO;
@@ -1434,7 +2067,11 @@ static int sony_pic_remove(struct acpi_device *device, int type)
        release_region(spic_dev.cur_ioport->io.minimum,
                        spic_dev.cur_ioport->io.address_length);
 
-       sony_pic_remove_input();
+       sony_laptop_remove_input();
+
+       /* pf attrs */
+       sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
+       sony_pf_remove();
 
        list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
                list_del(&io->list);
@@ -1447,7 +2084,7 @@ static int sony_pic_remove(struct acpi_device *device, int type)
        spic_dev.cur_ioport = NULL;
        spic_dev.cur_irq = NULL;
 
-       dprintk("removed.\n");
+       dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
        return 0;
 }
 
@@ -1457,13 +2094,13 @@ static int sony_pic_add(struct acpi_device *device)
        struct sony_pic_ioport *io, *tmp_io;
        struct sony_pic_irq *irq, *tmp_irq;
 
-       printk(KERN_INFO DRV_PFX
-               "Sony Programmable I/O Controller Driver v%s.\n",
-               SONY_LAPTOP_DRIVER_VERSION);
+       printk(KERN_INFO DRV_PFX "%s v%s.\n",
+               SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
 
        spic_dev.acpi_dev = device;
        strcpy(acpi_device_class(device), "sony/hotkey");
        spic_dev.model = sony_pic_detect_device_type();
+       mutex_init(&spic_dev.lock);
 
        /* read _PRS resources */
        result = sony_pic_possible_resources(device);
@@ -1474,7 +2111,7 @@ static int sony_pic_add(struct acpi_device *device)
        }
 
        /* setup input devices and helper fifo */
-       result = sony_pic_setup_input();
+       result = sony_laptop_setup_input();
        if (result) {
                printk(KERN_ERR DRV_PFX
                                "Unabe to create input devices.\n");
@@ -1525,8 +2162,27 @@ static int sony_pic_add(struct acpi_device *device)
                goto err_free_irq;
        }
 
+       spic_dev.bluetooth_power = -1;
+       /* create device attributes */
+       result = sony_pf_add();
+       if (result)
+               goto err_disable_device;
+
+       result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
+       if (result)
+               goto err_remove_pf;
+
+       if (sonypi_compat_init())
+               goto err_remove_pf;
+
        return 0;
 
+err_remove_pf:
+       sony_pf_remove();
+
+err_disable_device:
+       sony_pic_disable(device);
+
 err_free_irq:
        free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
 
@@ -1535,7 +2191,7 @@ err_release_region:
                        spic_dev.cur_ioport->io.address_length);
 
 err_remove_input:
-       sony_pic_remove_input();
+       sony_laptop_remove_input();
 
 err_free_resources:
        list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {