Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / drivers / input / joystick / twidjoy.c
index 8a98be1..076f237 100644 (file)
 #include <linux/serio.h>
 #include <linux/init.h>
 
+#define DRIVER_DESC    "Handykey Twiddler keyboard as a joystick driver"
+
+MODULE_DESCRIPTION(DRIVER_DESC);
+MODULE_LICENSE("GPL");
+
 /*
  * Constants.
  */
 
 #define TWIDJOY_MAX_LENGTH 5
 
-static char *twidjoy_name = "Handykey Twiddler";
-
 static struct twidjoy_button_spec {
        int bitshift;
        int bitmask;
@@ -90,7 +93,7 @@ twidjoy_buttons[] = {
  */
 
 struct twidjoy {
-       struct input_dev dev;
+       struct input_dev *dev;
        int idx;
        unsigned char data[TWIDJOY_MAX_LENGTH];
        char phys[32];
@@ -103,37 +106,33 @@ struct twidjoy {
 
 static void twidjoy_process_packet(struct twidjoy *twidjoy, struct pt_regs *regs)
 {
-       if (twidjoy->idx == TWIDJOY_MAX_LENGTH) {
-               struct input_dev *dev = &twidjoy->dev;
-               unsigned char *data = twidjoy->data;
-               struct twidjoy_button_spec *bp;
-               int button_bits, abs_x, abs_y;
-
-               button_bits = ((data[1] & 0x7f) << 7) | (data[0] & 0x7f);
+       struct input_dev *dev = twidjoy->dev;
+       unsigned char *data = twidjoy->data;
+       struct twidjoy_button_spec *bp;
+       int button_bits, abs_x, abs_y;
 
-               input_regs(dev, regs);
+       button_bits = ((data[1] & 0x7f) << 7) | (data[0] & 0x7f);
 
-               for (bp = twidjoy_buttons; bp->bitmask; bp++) {
-                       int value = (button_bits & (bp->bitmask << bp->bitshift)) >> bp->bitshift;
-                       int i;
+       input_regs(dev, regs);
 
-                       for (i = 0; i < bp->bitmask; i++)
-                               input_report_key(dev, bp->buttons[i], i+1 == value);
-               }
+       for (bp = twidjoy_buttons; bp->bitmask; bp++) {
+               int value = (button_bits & (bp->bitmask << bp->bitshift)) >> bp->bitshift;
+               int i;
 
-               abs_x = ((data[4] & 0x07) << 5) | ((data[3] & 0x7C) >> 2);
-               if (data[4] & 0x08) abs_x -= 256;
+               for (i = 0; i < bp->bitmask; i++)
+                       input_report_key(dev, bp->buttons[i], i+1 == value);
+       }
 
-               abs_y = ((data[3] & 0x01) << 7) | ((data[2] & 0x7F) >> 0);
-               if (data[3] & 0x02) abs_y -= 256;
+       abs_x = ((data[4] & 0x07) << 5) | ((data[3] & 0x7C) >> 2);
+       if (data[4] & 0x08) abs_x -= 256;
 
-               input_report_abs(dev, ABS_X, -abs_x);
-               input_report_abs(dev, ABS_Y, +abs_y);
+       abs_y = ((data[3] & 0x01) << 7) | ((data[2] & 0x7F) >> 0);
+       if (data[3] & 0x02) abs_y -= 256;
 
-               input_sync(dev);
-       }
+       input_report_abs(dev, ABS_X, -abs_x);
+       input_report_abs(dev, ABS_Y, +abs_y);
 
-       return;
+       input_sync(dev);
 }
 
 /*
@@ -142,9 +141,9 @@ static void twidjoy_process_packet(struct twidjoy *twidjoy, struct pt_regs *regs
  * packet processing routine.
  */
 
-static void twidjoy_interrupt(struct serio *serio, unsigned char data, unsigned int flags, struc pt_regs *regs)
+static irqreturn_t twidjoy_interrupt(struct serio *serio, unsigned char data, unsigned int flags, struct pt_regs *regs)
 {
-       struct twidjoy *twidjoy = serio->private;
+       struct twidjoy *twidjoy = serio_get_drvdata(serio);
 
        /* All Twiddler packets are 5 bytes. The fact that the first byte
         * has a MSB of 0 and all other bytes have a MSB of 1 can be used
@@ -153,7 +152,7 @@ static void twidjoy_interrupt(struct serio *serio, unsigned char data, unsigned
        if ((data & 0x80) == 0)
                twidjoy->idx = 0;       /* this byte starts a new packet */
        else if (twidjoy->idx == 0)
-               return;                 /* wrong MSB -- ignore this byte */
+               return IRQ_HANDLED;     /* wrong MSB -- ignore this byte */
 
        if (twidjoy->idx < TWIDJOY_MAX_LENGTH)
                twidjoy->data[twidjoy->idx++] = data;
@@ -163,7 +162,7 @@ static void twidjoy_interrupt(struct serio *serio, unsigned char data, unsigned
                twidjoy->idx = 0;
        }
 
-       return;
+       return IRQ_HANDLED;
 }
 
 /*
@@ -172,9 +171,11 @@ static void twidjoy_interrupt(struct serio *serio, unsigned char data, unsigned
 
 static void twidjoy_disconnect(struct serio *serio)
 {
-       struct twidjoy *twidjoy = serio->private;
-       input_unregister_device(&twidjoy->dev);
+       struct twidjoy *twidjoy = serio_get_drvdata(serio);
+
        serio_close(serio);
+       serio_set_drvdata(serio, NULL);
+       input_unregister_device(twidjoy->dev);
        kfree(twidjoy);
 }
 
@@ -184,84 +185,95 @@ static void twidjoy_disconnect(struct serio *serio)
  * it as an input device.
  */
 
-static void twidjoy_connect(struct serio *serio, struct serio_dev *dev)
+static int twidjoy_connect(struct serio *serio, struct serio_driver *drv)
 {
        struct twidjoy_button_spec *bp;
        struct twidjoy *twidjoy;
+       struct input_dev *input_dev;
+       int err = -ENOMEM;
        int i;
 
-       if (serio->type != (SERIO_RS232 | SERIO_TWIDJOY))
-               return;
-
-       if (!(twidjoy = kmalloc(sizeof(struct twidjoy), GFP_KERNEL)))
-               return;
-
-       memset(twidjoy, 0, sizeof(struct twidjoy));
-
-       sprintf(twidjoy->phys, "%s/input0", serio->phys);
-
-       init_input_dev(&twidjoy->dev);
-       twidjoy->dev.name = twidjoy_name;
-       twidjoy->dev.phys = twidjoy->phys;
-       twidjoy->dev.id.bustype = BUS_RS232;
-       twidjoy->dev.id.vendor = SERIO_TWIDJOY;
-       twidjoy->dev.id.product = 0x0001;
-       twidjoy->dev.id.version = 0x0100;
-
-       twidjoy->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);      
-
-       for (bp = twidjoy_buttons; bp->bitmask; bp++) {
+       twidjoy = kzalloc(sizeof(struct twidjoy), GFP_KERNEL);
+       input_dev = input_allocate_device();
+       if (!twidjoy || !input_dev)
+               goto fail;
+
+       twidjoy->dev = input_dev;
+       snprintf(twidjoy->phys, sizeof(twidjoy->phys), "%s/input0", serio->phys);
+
+       input_dev->name = "Handykey Twiddler";
+       input_dev->phys = twidjoy->phys;
+       input_dev->id.bustype = BUS_RS232;
+       input_dev->id.vendor = SERIO_TWIDJOY;
+       input_dev->id.product = 0x0001;
+       input_dev->id.version = 0x0100;
+       input_dev->cdev.dev = &serio->dev;
+       input_dev->private = twidjoy;
+
+       input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
+       input_dev->absbit[0] = BIT(ABS_X) | BIT(ABS_Y);
+       input_set_abs_params(input_dev, ABS_X, -50, 50, 4, 4);
+       input_set_abs_params(input_dev, ABS_Y, -50, 50, 4, 4);
+
+       for (bp = twidjoy_buttons; bp->bitmask; bp++)
                for (i = 0; i < bp->bitmask; i++)
-                       set_bit(bp->buttons[i], twidjoy->dev.keybit);
-       }
-
-       twidjoy->dev.absbit[0] = BIT(ABS_X) | BIT(ABS_Y);
-
-       for (i = 0; i < 2; i++) {
-               twidjoy->dev.absmax[ABS_X+i] =  50;     
-               twidjoy->dev.absmin[ABS_X+i] = -50;     
+                       set_bit(bp->buttons[i], input_dev->keybit);
 
-               /* TODO: arndt 20010708: Are these values appropriate? */
-               twidjoy->dev.absfuzz[ABS_X+i] = 4;
-               twidjoy->dev.absflat[ABS_X+i] = 4;
-       }
-
-       twidjoy->dev.private = twidjoy;
-       serio->private = twidjoy;
+       serio_set_drvdata(serio, twidjoy);
 
-       if (serio_open(serio, dev)) {
-               kfree(twidjoy);
-               return;
-       }
+       err = serio_open(serio, drv);
+       if (err)
+               goto fail;
 
-       input_register_device(&twidjoy->dev);
+       input_register_device(twidjoy->dev);
+       return 0;
 
-       printk(KERN_INFO "input: %s on %s\n", twidjoy_name, serio->phys);
+ fail: serio_set_drvdata(serio, NULL);
+       input_free_device(input_dev);
+       kfree(twidjoy);
+       return err;
 }
 
 /*
- * The serio device structure.
+ * The serio driver structure.
  */
 
-static struct serio_dev twidjoy_dev = {
-       .interrupt =    twidjoy_interrupt,
-       .connect =      twidjoy_connect,
-       .disconnect =   twidjoy_disconnect,
+static struct serio_device_id twidjoy_serio_ids[] = {
+       {
+               .type   = SERIO_RS232,
+               .proto  = SERIO_TWIDJOY,
+               .id     = SERIO_ANY,
+               .extra  = SERIO_ANY,
+       },
+       { 0 }
+};
+
+MODULE_DEVICE_TABLE(serio, twidjoy_serio_ids);
+
+static struct serio_driver twidjoy_drv = {
+       .driver         = {
+               .name   = "twidjoy",
+       },
+       .description    = DRIVER_DESC,
+       .id_table       = twidjoy_serio_ids,
+       .interrupt      = twidjoy_interrupt,
+       .connect        = twidjoy_connect,
+       .disconnect     = twidjoy_disconnect,
 };
 
 /*
  * The functions for inserting/removing us as a module.
  */
 
-int __init twidjoy_init(void)
+static int __init twidjoy_init(void)
 {
-       serio_register_device(&twidjoy_dev);
+       serio_register_driver(&twidjoy_drv);
        return 0;
 }
 
-void __exit twidjoy_exit(void)
+static void __exit twidjoy_exit(void)
 {
-       serio_unregister_device(&twidjoy_dev);
+       serio_unregister_driver(&twidjoy_drv);
 }
 
 module_init(twidjoy_init);