1/* 2 * 3 * keyboard input driver for i2c IR remote controls 4 * 5 * Copyright (c) 2000-2003 Gerd Knorr <kraxel@bytesex.org> 6 * modified for PixelView (BT878P+W/FM) by 7 * Michal Kochanowicz <mkochano@pld.org.pl> 8 * Christoph Bartelmus <lirc@bartelmus.de> 9 * modified for KNC ONE TV Station/Anubis Typhoon TView Tuner by 10 * Ulrich Mueller <ulrich.mueller42@web.de> 11 * modified for em2820 based USB TV tuners by 12 * Markus Rechberger <mrechberger@gmail.com> 13 * 14 * This program is free software; you can redistribute it and/or modify 15 * it under the terms of the GNU General Public License as published by 16 * the Free Software Foundation; either version 2 of the License, or 17 * (at your option) any later version. 18 * 19 * This program is distributed in the hope that it will be useful, 20 * but WITHOUT ANY WARRANTY; without even the implied warranty of 21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 * GNU General Public License for more details. 23 * 24 * You should have received a copy of the GNU General Public License 25 * along with this program; if not, write to the Free Software 26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 27 * 28 */ 29 30#include <linux/module.h> 31#include <linux/moduleparam.h> 32#include <linux/init.h> 33#include <linux/kernel.h> 34#include <linux/string.h> 35#include <linux/timer.h> 36#include <linux/delay.h> 37#include <linux/errno.h> 38#include <linux/slab.h> 39#include <linux/i2c.h> 40#include <linux/workqueue.h> 41#include <asm/semaphore.h> 42 43#include <media/ir-common.h> 44#include <media/ir-kbd-i2c.h> 45 46/* ----------------------------------------------------------------------- */ 47/* insmod parameters */ 48 49static int debug; 50module_param(debug, int, 0644); /* debug level (0,1,2) */ 51 52static int hauppauge = 0; 53module_param(hauppauge, int, 0644); /* Choose Hauppauge remote */ 54MODULE_PARM_DESC(hauppauge, "Specify Hauppauge remote: 0=black, 1=grey (defaults to 0)"); 55 56 57#define DEVNAME "ir-kbd-i2c" 58#define dprintk(level, fmt, arg...) if (debug >= level) \ 59 printk(KERN_DEBUG DEVNAME ": " fmt , ## arg) 60 61/* ----------------------------------------------------------------------- */ 62 63static int get_key_haup(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw) 64{ 65 unsigned char buf[3]; 66 int start, range, toggle, dev, code; 67 68 /* poll IR chip */ 69 if (3 != i2c_master_recv(&ir->c,buf,3)) 70 return -EIO; 71 72 /* split rc5 data block ... */ 73 start = (buf[0] >> 7) & 1; 74 range = (buf[0] >> 6) & 1; 75 toggle = (buf[0] >> 5) & 1; 76 dev = buf[0] & 0x1f; 77 code = (buf[1] >> 2) & 0x3f; 78 79 /* rc5 has two start bits 80 * the first bit must be one 81 * the second bit defines the command range (1 = 0-63, 0 = 64 - 127) 82 */ 83 if (!start) 84 /* no key pressed */ 85 return 0; 86 87 if (!range) 88 code += 64; 89 90 dprintk(1,"ir hauppauge (rc5): s%d r%d t%d dev=%d code=%d\n", 91 start, range, toggle, dev, code); 92 93 /* return key */ 94 *ir_key = code; 95 *ir_raw = (start << 12) | (toggle << 11) | (dev << 6) | code; 96 return 1; 97} 98 99static int get_key_pixelview(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw) 100{ 101 unsigned char b; 102 103 /* poll IR chip */ 104 if (1 != i2c_master_recv(&ir->c,&b,1)) { 105 dprintk(1,"read error\n"); 106 return -EIO; 107 } 108 *ir_key = b; 109 *ir_raw = b; 110 return 1; 111} 112 113static int get_key_pv951(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw) 114{ 115 unsigned char b; 116 117 /* poll IR chip */ 118 if (1 != i2c_master_recv(&ir->c,&b,1)) { 119 dprintk(1,"read error\n"); 120 return -EIO; 121 } 122 123 /* ignore 0xaa */ 124 if (b==0xaa) 125 return 0; 126 dprintk(2,"key %02x\n", b); 127 128 *ir_key = b; 129 *ir_raw = b; 130 return 1; 131} 132 133static int get_key_knc1(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw) 134{ 135 unsigned char b; 136 137 /* poll IR chip */ 138 if (1 != i2c_master_recv(&ir->c,&b,1)) { 139 dprintk(1,"read error\n"); 140 return -EIO; 141 } 142 143 /* it seems that 0xFE indicates that a button is still hold 144 down, while 0xff indicates that no button is hold 145 down. 0xfe sequences are sometimes interrupted by 0xFF */ 146 147 dprintk(2,"key %02x\n", b); 148 149 if (b == 0xff) 150 return 0; 151 152 if (b == 0xfe) 153 /* keep old data */ 154 return 1; 155 156 *ir_key = b; 157 *ir_raw = b; 158 return 1; 159} 160 161/* Common (grey or coloured) pinnacle PCTV remote handling 162 * 163 */ 164static int get_key_pinnacle(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw, 165 int parity_offset, int marker, int code_modulo) 166{ 167 unsigned char b[4]; 168 unsigned int start = 0,parity = 0,code = 0; 169 170 /* poll IR chip */ 171 if (4 != i2c_master_recv(&ir->c,b,4)) { 172 dprintk(2,"read error\n"); 173 return -EIO; 174 } 175 176 for (start = 0; start < ARRAY_SIZE(b); start++) { 177 if (b[start] == marker) { 178 code=b[(start+parity_offset+1)%4]; 179 parity=b[(start+parity_offset)%4]; 180 } 181 } 182 183 /* Empty Request */ 184 if (parity==0) 185 return 0; 186 187 /* Repeating... */ 188 if (ir->old == parity) 189 return 0; 190 191 ir->old = parity; 192 193 /* drop special codes when a key is held down a long time for the grey controller 194 In this case, the second bit of the code is asserted */ 195 if (marker == 0xfe && (code & 0x40)) 196 return 0; 197 198 code %= code_modulo; 199 200 *ir_raw = code; 201 *ir_key = code; 202 203 dprintk(1,"Pinnacle PCTV key %02x\n", code); 204 205 return 1; 206} 207 208int get_key_pinnacle_grey(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw) 209{ 210 211 return get_key_pinnacle(ir, ir_key, ir_raw, 1, 0xfe, 0xff); 212} 213 214EXPORT_SYMBOL_GPL(get_key_pinnacle_grey); 215 216 217/* The new pinnacle PCTV remote (with the colored buttons) 218 * 219 * Ricardo Cerqueira <v4l@cerqueira.org> 220 */ 221int get_key_pinnacle_color(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw) 222{ 223 /* code_modulo parameter (0x88) is used to reduce code value to fit inside IR_KEYTAB_SIZE 224 * 225 * this is the only value that results in 42 unique 226 * codes < 128 227 */ 228 229 return get_key_pinnacle(ir, ir_key, ir_raw, 2, 0x80, 0x88); 230} 231 232EXPORT_SYMBOL_GPL(get_key_pinnacle_color); 233 234/* ----------------------------------------------------------------------- */ 235 236static void ir_key_poll(struct IR_i2c *ir) 237{ 238 static u32 ir_key, ir_raw; 239 int rc; 240 241 dprintk(2,"ir_poll_key\n"); 242 rc = ir->get_key(ir, &ir_key, &ir_raw); 243 if (rc < 0) { 244 dprintk(2,"error\n"); 245 return; 246 } 247 248 if (0 == rc) { 249 ir_input_nokey(ir->input, &ir->ir); 250 } else { 251 ir_input_keydown(ir->input, &ir->ir, ir_key, ir_raw); 252 } 253} 254 255static void ir_timer(unsigned long data) 256{ 257 struct IR_i2c *ir = (struct IR_i2c*)data; 258 schedule_work(&ir->work); 259} 260 261static void ir_work(struct work_struct *work) 262{ 263 struct IR_i2c *ir = container_of(work, struct IR_i2c, work); 264 ir_key_poll(ir); 265 mod_timer(&ir->timer, jiffies+HZ/10); 266} 267 268/* ----------------------------------------------------------------------- */ 269 270static int ir_attach(struct i2c_adapter *adap, int addr, 271 unsigned short flags, int kind); 272static int ir_detach(struct i2c_client *client); 273static int ir_probe(struct i2c_adapter *adap); 274 275static struct i2c_driver driver = { 276 .driver = { 277 .name = "ir-kbd-i2c", 278 }, 279 .id = I2C_DRIVERID_INFRARED, 280 .attach_adapter = ir_probe, 281 .detach_client = ir_detach, 282}; 283 284static struct i2c_client client_template = 285{ 286 .name = "unset", 287 .driver = &driver 288}; 289 290static int ir_attach(struct i2c_adapter *adap, int addr, 291 unsigned short flags, int kind) 292{ 293 IR_KEYTAB_TYPE *ir_codes = NULL; 294 char *name; 295 int ir_type; 296 struct IR_i2c *ir; 297 struct input_dev *input_dev; 298 int err; 299 300 ir = kzalloc(sizeof(struct IR_i2c),GFP_KERNEL); 301 input_dev = input_allocate_device(); 302 if (!ir || !input_dev) { 303 err = -ENOMEM; 304 goto err_out_free; 305 } 306 307 ir->c = client_template; 308 ir->input = input_dev; 309 310 ir->c.adapter = adap; 311 ir->c.addr = addr; 312 313 i2c_set_clientdata(&ir->c, ir); 314 315 switch(addr) { 316 case 0x64: 317 name = "Pixelview"; 318 ir->get_key = get_key_pixelview; 319 ir_type = IR_TYPE_OTHER; 320 ir_codes = ir_codes_empty; 321 break; 322 case 0x4b: 323 name = "PV951"; 324 ir->get_key = get_key_pv951; 325 ir_type = IR_TYPE_OTHER; 326 ir_codes = ir_codes_pv951; 327 break; 328 case 0x18: 329 case 0x1a: 330 name = "Hauppauge"; 331 ir->get_key = get_key_haup; 332 ir_type = IR_TYPE_RC5; 333 if (hauppauge == 1) { 334 ir_codes = ir_codes_hauppauge_new; 335 } else { 336 ir_codes = ir_codes_rc5_tv; 337 } 338 break; 339 case 0x30: 340 name = "KNC One"; 341 ir->get_key = get_key_knc1; 342 ir_type = IR_TYPE_OTHER; 343 ir_codes = ir_codes_empty; 344 break; 345 case 0x7a: 346 case 0x47: 347 case 0x71: 348 /* Handled by saa7134-input */ 349 name = "SAA713x remote"; 350 ir_type = IR_TYPE_OTHER; 351 break; 352 default: 353 /* shouldn't happen */ 354 printk(DEVNAME ": Huh? unknown i2c address (0x%02x)?\n", addr); 355 err = -ENODEV; 356 goto err_out_free; 357 } 358 359 /* Sets name */ 360 snprintf(ir->c.name, sizeof(ir->c.name), "i2c IR (%s)", name); 361 ir->ir_codes = ir_codes; 362 363 /* register i2c device 364 * At device register, IR codes may be changed to be 365 * board dependent. 366 */ 367 err = i2c_attach_client(&ir->c); 368 if (err) 369 goto err_out_free; 370 371 /* If IR not supported or disabled, unregisters driver */ 372 if (ir->get_key == NULL) { 373 err = -ENODEV; 374 goto err_out_detach; 375 } 376 377 /* Phys addr can only be set after attaching (for ir->c.dev.bus_id) */ 378 snprintf(ir->phys, sizeof(ir->phys), "%s/%s/ir0", 379 ir->c.adapter->dev.bus_id, 380 ir->c.dev.bus_id); 381 382 /* init + register input device */ 383 ir_input_init(input_dev, &ir->ir, ir_type, ir->ir_codes); 384 input_dev->id.bustype = BUS_I2C; 385 input_dev->name = ir->c.name; 386 input_dev->phys = ir->phys; 387 388 err = input_register_device(ir->input); 389 if (err) 390 goto err_out_detach; 391 392 printk(DEVNAME ": %s detected at %s [%s]\n", 393 ir->input->name, ir->input->phys, adap->name); 394 395 /* start polling via eventd */ 396 INIT_WORK(&ir->work, ir_work); 397 init_timer(&ir->timer); 398 ir->timer.function = ir_timer; 399 ir->timer.data = (unsigned long)ir; 400 schedule_work(&ir->work); 401 402 return 0; 403 404 err_out_detach: 405 i2c_detach_client(&ir->c); 406 err_out_free: 407 input_free_device(input_dev); 408 kfree(ir); 409 return err; 410} 411 412static int ir_detach(struct i2c_client *client) 413{ 414 struct IR_i2c *ir = i2c_get_clientdata(client); 415 416 /* kill outstanding polls */ 417 del_timer_sync(&ir->timer); 418 flush_scheduled_work(); 419 420 /* unregister devices */ 421 input_unregister_device(ir->input); 422 i2c_detach_client(&ir->c); 423 424 /* free memory */ 425 kfree(ir); 426 return 0; 427} 428 429static int ir_probe(struct i2c_adapter *adap) 430{ 431 432 /* The external IR receiver is at i2c address 0x34 (0x35 for 433 reads). Future Hauppauge cards will have an internal 434 receiver at 0x30 (0x31 for reads). In theory, both can be 435 fitted, and Hauppauge suggest an external overrides an 436 internal. 437 438 That's why we probe 0x1a (~0x34) first. CB 439 */ 440 441 static const int probe_bttv[] = { 0x1a, 0x18, 0x4b, 0x64, 0x30, -1}; 442 static const int probe_saa7134[] = { 0x7a, 0x47, 0x71, -1 }; 443 static const int probe_em28XX[] = { 0x30, 0x47, -1 }; 444 const int *probe = NULL; 445 struct i2c_client c; 446 unsigned char buf; 447 int i,rc; 448 449 switch (adap->id) { 450 case I2C_HW_B_BT848: 451 probe = probe_bttv; 452 break; 453 case I2C_HW_B_CX2341X: 454 probe = probe_bttv; 455 break; 456 case I2C_HW_SAA7134: 457 probe = probe_saa7134; 458 break; 459 case I2C_HW_B_EM28XX: 460 probe = probe_em28XX; 461 break; 462 } 463 if (NULL == probe) 464 return 0; 465 466 memset(&c,0,sizeof(c)); 467 c.adapter = adap; 468 for (i = 0; -1 != probe[i]; i++) { 469 c.addr = probe[i]; 470 rc = i2c_master_recv(&c,&buf,0); 471 dprintk(1,"probe 0x%02x @ %s: %s\n", 472 probe[i], adap->name, 473 (0 == rc) ? "yes" : "no"); 474 if (0 == rc) { 475 ir_attach(adap,probe[i],0,0); 476 break; 477 } 478 } 479 return 0; 480} 481 482/* ----------------------------------------------------------------------- */ 483 484MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, Ulrich Mueller"); 485MODULE_DESCRIPTION("input driver for i2c IR remote controls"); 486MODULE_LICENSE("GPL"); 487 488static int __init ir_init(void) 489{ 490 return i2c_add_driver(&driver); 491} 492 493static void __exit ir_fini(void) 494{ 495 i2c_del_driver(&driver); 496} 497 498module_init(ir_init); 499module_exit(ir_fini); 500 501/* 502 * Overrides for Emacs so that we follow Linus's tabbing style. 503 * --------------------------------------------------------------------------- 504 * Local variables: 505 * c-basic-offset: 8 506 * End: 507 */ 508