WorkStruct: make allyesconfig
[safe/jmp/linux-2.6] / drivers / media / video / ir-kbd-i2c.c
index c470103..ab87e7b 100644 (file)
 #include <linux/i2c.h>
 #include <linux/workqueue.h>
 #include <asm/semaphore.h>
+
 #include <media/ir-common.h>
 #include <media/ir-kbd-i2c.h>
 
-/* Mark Phalan <phalanm@o2.ie> */
-static IR_KEYTAB_TYPE ir_codes_pv951[IR_KEYTAB_SIZE] = {
-       [  0 ] = KEY_KP0,
-       [  1 ] = KEY_KP1,
-       [  2 ] = KEY_KP2,
-       [  3 ] = KEY_KP3,
-       [  4 ] = KEY_KP4,
-       [  5 ] = KEY_KP5,
-       [  6 ] = KEY_KP6,
-       [  7 ] = KEY_KP7,
-       [  8 ] = KEY_KP8,
-       [  9 ] = KEY_KP9,
-
-       [ 18 ] = KEY_POWER,
-       [ 16 ] = KEY_MUTE,
-       [ 31 ] = KEY_VOLUMEDOWN,
-       [ 27 ] = KEY_VOLUMEUP,
-       [ 26 ] = KEY_CHANNELUP,
-       [ 30 ] = KEY_CHANNELDOWN,
-       [ 14 ] = KEY_PAGEUP,
-       [ 29 ] = KEY_PAGEDOWN,
-       [ 19 ] = KEY_SOUND,
-
-       [ 24 ] = KEY_KPPLUSMINUS,       /* CH +/- */
-       [ 22 ] = KEY_SUBTITLE,          /* CC */
-       [ 13 ] = KEY_TEXT,              /* TTX */
-       [ 11 ] = KEY_TV,                /* AIR/CBL */
-       [ 17 ] = KEY_PC,                /* PC/TV */
-       [ 23 ] = KEY_OK,                /* CH RTN */
-       [ 25 ] = KEY_MODE,              /* FUNC */
-       [ 12 ] = KEY_SEARCH,            /* AUTOSCAN */
-
-       /* Not sure what to do with these ones! */
-       [ 15 ] = KEY_SELECT,            /* SOURCE */
-       [ 10 ] = KEY_KPPLUS,            /* +100 */
-       [ 20 ] = KEY_KPEQUAL,           /* SYNC */
-       [ 28 ] = KEY_MEDIA,             /* PC/TV */
-};
-
-static IR_KEYTAB_TYPE ir_codes_purpletv[IR_KEYTAB_SIZE] = {
-       [ 0x3  ] = KEY_POWER,
-       [ 0x6f ] = KEY_MUTE,
-       [ 0x10 ] = KEY_BACKSPACE,       /* Recall */
-
-       [ 0x11 ] = KEY_KP0,
-       [ 0x4  ] = KEY_KP1,
-       [ 0x5  ] = KEY_KP2,
-       [ 0x6  ] = KEY_KP3,
-       [ 0x8  ] = KEY_KP4,
-       [ 0x9  ] = KEY_KP5,
-       [ 0xa  ] = KEY_KP6,
-       [ 0xc  ] = KEY_KP7,
-       [ 0xd  ] = KEY_KP8,
-       [ 0xe  ] = KEY_KP9,
-       [ 0x12 ] = KEY_KPDOT,           /* 100+ */
-
-       [ 0x7  ] = KEY_VOLUMEUP,
-       [ 0xb  ] = KEY_VOLUMEDOWN,
-       [ 0x1a ] = KEY_KPPLUS,
-       [ 0x18 ] = KEY_KPMINUS,
-       [ 0x15 ] = KEY_UP,
-       [ 0x1d ] = KEY_DOWN,
-       [ 0xf  ] = KEY_CHANNELUP,
-       [ 0x13 ] = KEY_CHANNELDOWN,
-       [ 0x48 ] = KEY_ZOOM,
-
-       [ 0x1b ] = KEY_VIDEO,           /* Video source */
-       [ 0x49 ] = KEY_LANGUAGE,        /* MTS Select */
-       [ 0x19 ] = KEY_SEARCH,          /* Auto Scan */
-
-       [ 0x4b ] = KEY_RECORD,
-       [ 0x46 ] = KEY_PLAY,
-       [ 0x45 ] = KEY_PAUSE,           /* Pause */
-       [ 0x44 ] = KEY_STOP,
-       [ 0x40 ] = KEY_FORWARD,         /* Forward ? */
-       [ 0x42 ] = KEY_REWIND,          /* Backward ? */
-
-};
-
 /* ----------------------------------------------------------------------- */
 /* insmod parameters                                                       */
 
 static int debug;
 module_param(debug, int, 0644);    /* debug level (0,1,2) */
 
+static int hauppauge = 0;
+module_param(hauppauge, int, 0644);    /* Choose Hauppauge remote */
+MODULE_PARM_DESC(hauppauge, "Specify Hauppauge remote: 0=black, 1=grey (defaults to 0)");
+
+
 #define DEVNAME "ir-kbd-i2c"
 #define dprintk(level, fmt, arg...)    if (debug >= level) \
        printk(KERN_DEBUG DEVNAME ": " fmt , ## arg)
@@ -137,23 +64,32 @@ module_param(debug, int, 0644);    /* debug level (0,1,2) */
 static int get_key_haup(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
 {
        unsigned char buf[3];
-       int start, toggle, dev, code;
+       int start, range, toggle, dev, code;
 
        /* poll IR chip */
        if (3 != i2c_master_recv(&ir->c,buf,3))
                return -EIO;
 
        /* split rc5 data block ... */
-       start  = (buf[0] >> 6) &    3;
+       start  = (buf[0] >> 7) &    1;
+       range  = (buf[0] >> 6) &    1;
        toggle = (buf[0] >> 5) &    1;
        dev    =  buf[0]       & 0x1f;
        code   = (buf[1] >> 2) & 0x3f;
 
-       if (3 != start)
+       /* rc5 has two start bits
+        * the first bit must be one
+        * the second bit defines the command range (1 = 0-63, 0 = 64 - 127)
+        */
+       if (!start)
                /* no key pressed */
                return 0;
-       dprintk(1,"ir hauppauge (rc5): s%d t%d dev=%d code=%d\n",
-               start, toggle, dev, code);
+
+       if (!range)
+               code += 64;
+
+       dprintk(1,"ir hauppauge (rc5): s%d r%d t%d dev=%d code=%d\n",
+               start, range, toggle, dev, code);
 
        /* return key */
        *ir_key = code;
@@ -163,7 +99,7 @@ static int get_key_haup(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
 
 static int get_key_pixelview(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
 {
-        unsigned char b;
+       unsigned char b;
 
        /* poll IR chip */
        if (1 != i2c_master_recv(&ir->c,&b,1)) {
@@ -177,7 +113,7 @@ static int get_key_pixelview(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
 
 static int get_key_pv951(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
 {
-        unsigned char b;
+       unsigned char b;
 
        /* poll IR chip */
        if (1 != i2c_master_recv(&ir->c,&b,1)) {
@@ -206,15 +142,15 @@ static int get_key_knc1(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
        }
 
        /* it seems that 0xFE indicates that a button is still hold
-          down, while 0xFF indicates that no button is hold
-          down. 0xFE sequences are sometimes interrupted by 0xFF */
+          down, while 0xff indicates that no button is hold
+          down. 0xfe sequences are sometimes interrupted by 0xFF */
 
        dprintk(2,"key %02x\n", b);
 
-       if (b == 0xFF)
+       if (b == 0xff)
                return 0;
 
-       if (b == 0xFE)
+       if (b == 0xfe)
                /* keep old data */
                return 1;
 
@@ -223,28 +159,88 @@ static int get_key_knc1(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
        return 1;
 }
 
-static int get_key_purpletv(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
+/* Common (grey or coloured) pinnacle PCTV remote handling
+ *
+ */
+static int get_key_pinnacle(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw,
+                           int parity_offset, int marker, int code_modulo)
 {
-        unsigned char b;
+       unsigned char b[4];
+       unsigned int start = 0,parity = 0,code = 0;
 
        /* poll IR chip */
-       if (1 != i2c_master_recv(&ir->c,&b,1)) {
-               dprintk(1,"read error\n");
+       if (4 != i2c_master_recv(&ir->c,b,4)) {
+               dprintk(2,"read error\n");
                return -EIO;
        }
 
-       /* no button press */
-       if (b==0)
+       for (start = 0; start<4; start++) {
+               if (b[start] == marker) {
+                       code=b[(start+parity_offset+1)%4];
+                       parity=b[(start+parity_offset)%4];
+               }
+       }
+
+       /* Empty Request */
+       if (parity==0)
                return 0;
 
-       /* repeating */
-       if (b & 0x80)
-               return 1;
+       /* Repeating... */
+       if (ir->old == parity)
+               return 0;
+
+       ir->old = parity;
+
+       /* drop special codes when a key is held down a long time for the grey controller
+          In this case, the second bit of the code is asserted */
+       if (marker == 0xfe && (code & 0x40))
+               return 0;
+
+       code %= code_modulo;
+
+       *ir_raw = code;
+       *ir_key = code;
+
+       dprintk(1,"Pinnacle PCTV key %02x\n", code);
 
-       *ir_key = b;
-       *ir_raw = b;
        return 1;
 }
+
+/* The grey pinnacle PCTV remote
+ *
+ *  There are one issue with this remote:
+ *   - I2c packet does not change when the same key is pressed quickly. The workaround
+ *     is to hold down each key for about half a second, so that another code is generated
+ *     in the i2c packet, and the function can distinguish key presses.
+ *
+ * Sylvain Pasche <sylvain.pasche@gmail.com>
+ */
+int get_key_pinnacle_grey(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
+{
+
+       return get_key_pinnacle(ir, ir_key, ir_raw, 1, 0xfe, 0xff);
+}
+
+EXPORT_SYMBOL_GPL(get_key_pinnacle_grey);
+
+
+/* The new pinnacle PCTV remote (with the colored buttons)
+ *
+ * Ricardo Cerqueira <v4l@cerqueira.org>
+ */
+int get_key_pinnacle_color(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
+{
+       /* code_modulo parameter (0x88) is used to reduce code value to fit inside IR_KEYTAB_SIZE
+        *
+        * this is the only value that results in 42 unique
+        * codes < 128
+        */
+
+       return get_key_pinnacle(ir, ir_key, ir_raw, 2, 0x80, 0x88);
+}
+
+EXPORT_SYMBOL_GPL(get_key_pinnacle_color);
+
 /* ----------------------------------------------------------------------- */
 
 static void ir_key_poll(struct IR_i2c *ir)
@@ -272,9 +268,9 @@ static void ir_timer(unsigned long data)
        schedule_work(&ir->work);
 }
 
-static void ir_work(void *data)
+static void ir_work(struct work_struct *work)
 {
-       struct IR_i2c *ir = data;
+       struct IR_i2c *ir = container_of(work, struct IR_i2c, work);
        ir_key_poll(ir);
        mod_timer(&ir->timer, jiffies+HZ/10);
 }
@@ -287,17 +283,18 @@ static int ir_detach(struct i2c_client *client);
 static int ir_probe(struct i2c_adapter *adap);
 
 static struct i2c_driver driver = {
-        .name           = "ir remote kbd driver",
-        .id             = I2C_DRIVERID_EXP3, /* FIXME */
-        .flags          = I2C_DF_NOTIFY,
-        .attach_adapter = ir_probe,
-        .detach_client  = ir_detach,
+       .driver = {
+               .name   = "ir-kbd-i2c",
+       },
+       .id             = I2C_DRIVERID_INFRARED,
+       .attach_adapter = ir_probe,
+       .detach_client  = ir_detach,
 };
 
 static struct i2c_client client_template =
 {
-        .name = "unset",
-        .driver = &driver
+       .name = "unset",
+       .driver = &driver
 };
 
 static int ir_attach(struct i2c_adapter *adap, int addr,
@@ -306,24 +303,26 @@ static int ir_attach(struct i2c_adapter *adap, int addr,
        IR_KEYTAB_TYPE *ir_codes = NULL;
        char *name;
        int ir_type;
-        struct IR_i2c *ir;
+       struct IR_i2c *ir;
        struct input_dev *input_dev;
 
-       ir = kzalloc(sizeof(struct IR_i2c), GFP_KERNEL);
+       ir = kzalloc(sizeof(struct IR_i2c),GFP_KERNEL);
        input_dev = input_allocate_device();
        if (!ir || !input_dev) {
-               kfree(ir);
                input_free_device(input_dev);
-                return -ENOMEM;
+               kfree(ir);
+               return -ENOMEM;
        }
+       memset(ir,0,sizeof(*ir));
 
        ir->c = client_template;
        ir->input = input_dev;
 
-       i2c_set_clientdata(&ir->c, ir);
        ir->c.adapter = adap;
        ir->c.addr    = addr;
 
+       i2c_set_clientdata(&ir->c, ir);
+
        switch(addr) {
        case 0x64:
                name        = "Pixelview";
@@ -342,7 +341,11 @@ static int ir_attach(struct i2c_adapter *adap, int addr,
                name        = "Hauppauge";
                ir->get_key = get_key_haup;
                ir_type     = IR_TYPE_RC5;
-               ir_codes    = ir_codes_rc5_tv;
+               if (hauppauge == 1) {
+                       ir_codes    = ir_codes_hauppauge_new;
+               } else {
+                       ir_codes    = ir_codes_rc5_tv;
+               }
                break;
        case 0x30:
                name        = "KNC One";
@@ -351,12 +354,11 @@ static int ir_attach(struct i2c_adapter *adap, int addr,
                ir_codes    = ir_codes_empty;
                break;
        case 0x7a:
-               name        = "Purple TV";
-               ir->get_key = get_key_purpletv;
+       case 0x47:
+               /* Handled by saa7134-input */
+               name        = "SAA713x remote";
                ir_type     = IR_TYPE_OTHER;
-               ir_codes    = ir_codes_purpletv;
                break;
-
        default:
                /* shouldn't happen */
                printk(DEVNAME ": Huh? unknown i2c address (0x%02x)?\n",addr);
@@ -364,30 +366,41 @@ static int ir_attach(struct i2c_adapter *adap, int addr,
                return -1;
        }
 
-       /* Sets name and its physical addr */
+       /* Sets name */
        snprintf(ir->c.name, sizeof(ir->c.name), "i2c IR (%s)", name);
-       snprintf(ir->phys, sizeof(ir->phys), "%s/%s/ir0",
-                ir->c.adapter->dev.bus_id,
-                ir->c.dev.bus_id);
        ir->ir_codes=ir_codes;
 
        /* register i2c device
         * At device register, IR codes may be changed to be
         * board dependent.
-       */
+        */
        i2c_attach_client(&ir->c);
 
+       /* If IR not supported or disabled, unregisters driver */
+       if (ir->get_key == NULL) {
+               i2c_detach_client(&ir->c);
+               kfree(ir);
+               return -1;
+       }
+
+       /* Phys addr can only be set after attaching (for ir->c.dev.bus_id) */
+       snprintf(ir->phys, sizeof(ir->phys), "%s/%s/ir0",
+                ir->c.adapter->dev.bus_id,
+                ir->c.dev.bus_id);
+
        /* init + register input device */
-       ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
-       input_dev->id.bustype   = BUS_I2C;
-       input_dev->name         = ir->c.name;
-       input_dev->phys         = ir->phys;
+       ir_input_init(input_dev,&ir->ir,ir_type,ir->ir_codes);
+       input_dev->id.bustype = BUS_I2C;
+       input_dev->name       = ir->c.name;
+       input_dev->phys       = ir->phys;
 
        /* register event device */
        input_register_device(ir->input);
+       printk(DEVNAME ": %s detected at %s [%s]\n",
+              ir->input->name,ir->input->phys,adap->name);
 
        /* start polling via eventd */
-       INIT_WORK(&ir->work, ir_work, ir);
+       INIT_WORK(&ir->work, ir_work);
        init_timer(&ir->timer);
        ir->timer.function = ir_timer;
        ir->timer.data     = (unsigned long)ir;
@@ -398,7 +411,7 @@ static int ir_attach(struct i2c_adapter *adap, int addr,
 
 static int ir_detach(struct i2c_client *client)
 {
-        struct IR_i2c *ir = i2c_get_clientdata(client);
+       struct IR_i2c *ir = i2c_get_clientdata(client);
 
        /* kill outstanding polls */
        del_timer(&ir->timer);
@@ -426,20 +439,25 @@ static int ir_probe(struct i2c_adapter *adap)
        */
 
        static const int probe_bttv[] = { 0x1a, 0x18, 0x4b, 0x64, 0x30, -1};
-       static const int probe_saa7134[] = { 0x7a, -1 };
-       static const int probe_em2820[] = { 0x30, 0x47, -1 };
+       static const int probe_saa7134[] = { 0x7a, 0x47, -1 };
+       static const int probe_em28XX[] = { 0x30, 0x47, -1 };
        const int *probe = NULL;
-       struct i2c_client c; unsigned char buf; int i,rc;
+       struct i2c_client c;
+       unsigned char buf;
+       int i,rc;
 
        switch (adap->id) {
        case I2C_HW_B_BT848:
                probe = probe_bttv;
                break;
+       case I2C_HW_B_CX2341X:
+               probe = probe_bttv;
+               break;
        case I2C_HW_SAA7134:
                probe = probe_saa7134;
                break;
-       case I2C_HW_B_EM2820:
-               probe = probe_em2820;
+       case I2C_HW_B_EM28XX:
+               probe = probe_em28XX;
                break;
        }
        if (NULL == probe)