1/* 2 * tm6000-input.c - driver for TM5600/TM6000/TM6010 USB video capture devices 3 * 4 * Copyright (C) 2010 Stefan Ringel <stefan.ringel@arcor.de> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation version 2 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 18 */ 19 20#include <linux/module.h> 21#include <linux/init.h> 22#include <linux/delay.h> 23 24#include <linux/input.h> 25#include <linux/usb.h> 26 27#include <media/rc-core.h> 28 29#include "tm6000.h" 30#include "tm6000-regs.h" 31 32static unsigned int ir_debug; 33module_param(ir_debug, int, 0644); 34MODULE_PARM_DESC(ir_debug, "debug message level"); 35 36static unsigned int enable_ir = 1; 37module_param(enable_ir, int, 0644); 38MODULE_PARM_DESC(enable_ir, "enable ir (default is enable)"); 39 40static unsigned int ir_clock_mhz = 12; 41module_param(ir_clock_mhz, int, 0644); 42MODULE_PARM_DESC(enable_ir, "ir clock, in MHz"); 43 44#define URB_SUBMIT_DELAY 100 /* ms - Delay to submit an URB request on retrial and init */ 45#define URB_INT_LED_DELAY 100 /* ms - Delay to turn led on again on int mode */ 46 47#undef dprintk 48 49#define dprintk(level, fmt, arg...) do {\ 50 if (ir_debug >= level) \ 51 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \ 52 } while (0) 53 54struct tm6000_ir_poll_result { 55 u16 rc_data; 56}; 57 58struct tm6000_IR { 59 struct tm6000_core *dev; 60 struct rc_dev *rc; 61 char name[32]; 62 char phys[32]; 63 64 /* poll expernal decoder */ 65 int polling; 66 struct delayed_work work; 67 u8 wait:1; 68 u8 pwled:2; 69 u8 submit_urb:1; 70 u16 key_addr; 71 struct urb *int_urb; 72 73 /* IR device properties */ 74 u64 rc_type; 75}; 76 77void tm6000_ir_wait(struct tm6000_core *dev, u8 state) 78{ 79 struct tm6000_IR *ir = dev->ir; 80 81 if (!dev->ir) 82 return; 83 84 dprintk(2, "%s: %i\n",__func__, ir->wait); 85 86 if (state) 87 ir->wait = 1; 88 else 89 ir->wait = 0; 90} 91 92static int tm6000_ir_config(struct tm6000_IR *ir) 93{ 94 struct tm6000_core *dev = ir->dev; 95 u32 pulse = 0, leader = 0; 96 97 dprintk(2, "%s\n",__func__); 98 99 /* 100 * The IR decoder supports RC-5 or NEC, with a configurable timing. 101 * The timing configuration there is not that accurate, as it uses 102 * approximate values. The NEC spec mentions a 562.5 unit period, 103 * and RC-5 uses a 888.8 period. 104 * Currently, driver assumes a clock provided by a 12 MHz XTAL, but 105 * a modprobe parameter can adjust it. 106 * Adjustments are required for other timings. 107 * It seems that the 900ms timing for NEC is used to detect a RC-5 108 * IR, in order to discard such decoding 109 */ 110 111 switch (ir->rc_type) { 112 case RC_BIT_NEC: 113 leader = 900; /* ms */ 114 pulse = 700; /* ms - the actual value would be 562 */ 115 break; 116 default: 117 case RC_BIT_RC5: 118 leader = 900; /* ms - from the NEC decoding */ 119 pulse = 1780; /* ms - The actual value would be 1776 */ 120 break; 121 } 122 123 pulse = ir_clock_mhz * pulse; 124 leader = ir_clock_mhz * leader; 125 if (ir->rc_type == RC_BIT_NEC) 126 leader = leader | 0x8000; 127 128 dprintk(2, "%s: %s, %d MHz, leader = 0x%04x, pulse = 0x%06x \n", 129 __func__, 130 (ir->rc_type == RC_BIT_NEC) ? "NEC" : "RC-5", 131 ir_clock_mhz, leader, pulse); 132 133 /* Remote WAKEUP = enable, normal mode, from IR decoder output */ 134 tm6000_set_reg(dev, TM6010_REQ07_RE5_REMOTE_WAKEUP, 0xfe); 135 136 /* Enable IR reception on non-busrt mode */ 137 tm6000_set_reg(dev, TM6010_REQ07_RD8_IR, 0x2f); 138 139 /* IR_WKUP_SEL = Low byte in decoded IR data */ 140 tm6000_set_reg(dev, TM6010_REQ07_RDA_IR_WAKEUP_SEL, 0xff); 141 /* IR_WKU_ADD code */ 142 tm6000_set_reg(dev, TM6010_REQ07_RDB_IR_WAKEUP_ADD, 0xff); 143 144 tm6000_set_reg(dev, TM6010_REQ07_RDC_IR_LEADER1, leader >> 8); 145 tm6000_set_reg(dev, TM6010_REQ07_RDD_IR_LEADER0, leader); 146 147 tm6000_set_reg(dev, TM6010_REQ07_RDE_IR_PULSE_CNT1, pulse >> 8); 148 tm6000_set_reg(dev, TM6010_REQ07_RDF_IR_PULSE_CNT0, pulse); 149 150 if (!ir->polling) 151 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 0); 152 else 153 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 1); 154 msleep(10); 155 156 /* Shows that IR is working via the LED */ 157 tm6000_flash_led(dev, 0); 158 msleep(100); 159 tm6000_flash_led(dev, 1); 160 ir->pwled = 1; 161 162 return 0; 163} 164 165static void tm6000_ir_keydown(struct tm6000_IR *ir, 166 const char *buf, unsigned int len) 167{ 168 u8 device, command; 169 u32 scancode; 170 enum rc_type protocol; 171 172 if (len < 1) 173 return; 174 175 command = buf[0]; 176 device = (len > 1 ? buf[1] : 0x0); 177 switch (ir->rc_type) { 178 case RC_BIT_RC5: 179 protocol = RC_TYPE_RC5; 180 scancode = RC_SCANCODE_RC5(device, command); 181 break; 182 case RC_BIT_NEC: 183 protocol = RC_TYPE_NEC; 184 scancode = RC_SCANCODE_NEC(device, command); 185 break; 186 default: 187 protocol = RC_TYPE_OTHER; 188 scancode = RC_SCANCODE_OTHER(device << 8 | command); 189 break; 190 } 191 192 dprintk(1, "%s, protocol: 0x%04x, scancode: 0x%08x\n", 193 __func__, protocol, scancode); 194 rc_keydown(ir->rc, protocol, scancode, 0); 195} 196 197static void tm6000_ir_urb_received(struct urb *urb) 198{ 199 struct tm6000_core *dev = urb->context; 200 struct tm6000_IR *ir = dev->ir; 201 char *buf; 202 203 dprintk(2, "%s\n",__func__); 204 if (urb->status < 0 || urb->actual_length <= 0) { 205 printk(KERN_INFO "tm6000: IR URB failure: status: %i, length %i\n", 206 urb->status, urb->actual_length); 207 ir->submit_urb = 1; 208 schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY)); 209 return; 210 } 211 buf = urb->transfer_buffer; 212 213 if (ir_debug) 214 print_hex_dump(KERN_DEBUG, "tm6000: IR data: ", 215 DUMP_PREFIX_OFFSET,16, 1, 216 buf, urb->actual_length, false); 217 218 tm6000_ir_keydown(ir, urb->transfer_buffer, urb->actual_length); 219 220 usb_submit_urb(urb, GFP_ATOMIC); 221 /* 222 * Flash the led. We can't do it here, as it is running on IRQ context. 223 * So, use the scheduler to do it, in a few ms. 224 */ 225 ir->pwled = 2; 226 schedule_delayed_work(&ir->work, msecs_to_jiffies(10)); 227} 228 229static void tm6000_ir_handle_key(struct work_struct *work) 230{ 231 struct tm6000_IR *ir = container_of(work, struct tm6000_IR, work.work); 232 struct tm6000_core *dev = ir->dev; 233 int rc; 234 u8 buf[2]; 235 236 if (ir->wait) 237 return; 238 239 dprintk(3, "%s\n",__func__); 240 241 rc = tm6000_read_write_usb(dev, USB_DIR_IN | 242 USB_TYPE_VENDOR | USB_RECIP_DEVICE, 243 REQ_02_GET_IR_CODE, 0, 0, buf, 2); 244 if (rc < 0) 245 return; 246 247 /* Check if something was read */ 248 if ((buf[0] & 0xff) == 0xff) { 249 if (!ir->pwled) { 250 tm6000_flash_led(dev, 1); 251 ir->pwled = 1; 252 } 253 return; 254 } 255 256 tm6000_ir_keydown(ir, buf, rc); 257 tm6000_flash_led(dev, 0); 258 ir->pwled = 0; 259 260 /* Re-schedule polling */ 261 schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling)); 262} 263 264static void tm6000_ir_int_work(struct work_struct *work) 265{ 266 struct tm6000_IR *ir = container_of(work, struct tm6000_IR, work.work); 267 struct tm6000_core *dev = ir->dev; 268 int rc; 269 270 dprintk(3, "%s, submit_urb = %d, pwled = %d\n",__func__, ir->submit_urb, 271 ir->pwled); 272 273 if (ir->submit_urb) { 274 dprintk(3, "Resubmit urb\n"); 275 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 0); 276 277 rc = usb_submit_urb(ir->int_urb, GFP_ATOMIC); 278 if (rc < 0) { 279 printk(KERN_ERR "tm6000: Can't submit an IR interrupt. Error %i\n", 280 rc); 281 /* Retry in 100 ms */ 282 schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY)); 283 return; 284 } 285 ir->submit_urb = 0; 286 } 287 288 /* Led is enabled only if USB submit doesn't fail */ 289 if (ir->pwled == 2) { 290 tm6000_flash_led(dev, 0); 291 ir->pwled = 0; 292 schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_INT_LED_DELAY)); 293 } else if (!ir->pwled) { 294 tm6000_flash_led(dev, 1); 295 ir->pwled = 1; 296 } 297} 298 299static int tm6000_ir_start(struct rc_dev *rc) 300{ 301 struct tm6000_IR *ir = rc->priv; 302 303 dprintk(2, "%s\n",__func__); 304 305 schedule_delayed_work(&ir->work, 0); 306 307 return 0; 308} 309 310static void tm6000_ir_stop(struct rc_dev *rc) 311{ 312 struct tm6000_IR *ir = rc->priv; 313 314 dprintk(2, "%s\n",__func__); 315 316 cancel_delayed_work_sync(&ir->work); 317} 318 319static int tm6000_ir_change_protocol(struct rc_dev *rc, u64 *rc_type) 320{ 321 struct tm6000_IR *ir = rc->priv; 322 323 if (!ir) 324 return 0; 325 326 dprintk(2, "%s\n",__func__); 327 328 if ((rc->rc_map.scan) && (*rc_type == RC_BIT_NEC)) 329 ir->key_addr = ((rc->rc_map.scan[0].scancode >> 8) & 0xffff); 330 331 ir->rc_type = *rc_type; 332 333 tm6000_ir_config(ir); 334 /* TODO */ 335 return 0; 336} 337 338static int __tm6000_ir_int_start(struct rc_dev *rc) 339{ 340 struct tm6000_IR *ir = rc->priv; 341 struct tm6000_core *dev; 342 int pipe, size; 343 int err = -ENOMEM; 344 345 if (!ir) 346 return -ENODEV; 347 dev = ir->dev; 348 349 dprintk(2, "%s\n",__func__); 350 351 ir->int_urb = usb_alloc_urb(0, GFP_ATOMIC); 352 if (!ir->int_urb) 353 return -ENOMEM; 354 355 pipe = usb_rcvintpipe(dev->udev, 356 dev->int_in.endp->desc.bEndpointAddress 357 & USB_ENDPOINT_NUMBER_MASK); 358 359 size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe)); 360 dprintk(1, "IR max size: %d\n", size); 361 362 ir->int_urb->transfer_buffer = kzalloc(size, GFP_ATOMIC); 363 if (ir->int_urb->transfer_buffer == NULL) { 364 usb_free_urb(ir->int_urb); 365 return err; 366 } 367 dprintk(1, "int interval: %d\n", dev->int_in.endp->desc.bInterval); 368 369 usb_fill_int_urb(ir->int_urb, dev->udev, pipe, 370 ir->int_urb->transfer_buffer, size, 371 tm6000_ir_urb_received, dev, 372 dev->int_in.endp->desc.bInterval); 373 374 ir->submit_urb = 1; 375 schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY)); 376 377 return 0; 378} 379 380static void __tm6000_ir_int_stop(struct rc_dev *rc) 381{ 382 struct tm6000_IR *ir = rc->priv; 383 384 if (!ir || !ir->int_urb) 385 return; 386 387 dprintk(2, "%s\n",__func__); 388 389 usb_kill_urb(ir->int_urb); 390 kfree(ir->int_urb->transfer_buffer); 391 usb_free_urb(ir->int_urb); 392 ir->int_urb = NULL; 393} 394 395int tm6000_ir_int_start(struct tm6000_core *dev) 396{ 397 struct tm6000_IR *ir = dev->ir; 398 399 if (!ir) 400 return 0; 401 402 return __tm6000_ir_int_start(ir->rc); 403} 404 405void tm6000_ir_int_stop(struct tm6000_core *dev) 406{ 407 struct tm6000_IR *ir = dev->ir; 408 409 if (!ir || !ir->rc) 410 return; 411 412 __tm6000_ir_int_stop(ir->rc); 413} 414 415int tm6000_ir_init(struct tm6000_core *dev) 416{ 417 struct tm6000_IR *ir; 418 struct rc_dev *rc; 419 int err = -ENOMEM; 420 u64 rc_type; 421 422 if (!enable_ir) 423 return -ENODEV; 424 425 if (!dev->caps.has_remote) 426 return 0; 427 428 if (!dev->ir_codes) 429 return 0; 430 431 ir = kzalloc(sizeof(*ir), GFP_ATOMIC); 432 rc = rc_allocate_device(); 433 if (!ir || !rc) 434 goto out; 435 436 dprintk(2, "%s\n", __func__); 437 438 /* record handles to ourself */ 439 ir->dev = dev; 440 dev->ir = ir; 441 ir->rc = rc; 442 443 /* input setup */ 444 rc->allowed_protocols = RC_BIT_RC5 | RC_BIT_NEC; 445 /* Neded, in order to support NEC remotes with 24 or 32 bits */ 446 rc->scancode_mask = 0xffff; 447 rc->priv = ir; 448 rc->change_protocol = tm6000_ir_change_protocol; 449 if (dev->int_in.endp) { 450 rc->open = __tm6000_ir_int_start; 451 rc->close = __tm6000_ir_int_stop; 452 INIT_DELAYED_WORK(&ir->work, tm6000_ir_int_work); 453 } else { 454 rc->open = tm6000_ir_start; 455 rc->close = tm6000_ir_stop; 456 ir->polling = 50; 457 INIT_DELAYED_WORK(&ir->work, tm6000_ir_handle_key); 458 } 459 rc->driver_type = RC_DRIVER_SCANCODE; 460 461 snprintf(ir->name, sizeof(ir->name), "tm5600/60x0 IR (%s)", 462 dev->name); 463 464 usb_make_path(dev->udev, ir->phys, sizeof(ir->phys)); 465 strlcat(ir->phys, "/input0", sizeof(ir->phys)); 466 467 rc_type = RC_BIT_UNKNOWN; 468 tm6000_ir_change_protocol(rc, &rc_type); 469 470 rc->input_name = ir->name; 471 rc->input_phys = ir->phys; 472 rc->input_id.bustype = BUS_USB; 473 rc->input_id.version = 1; 474 rc->input_id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor); 475 rc->input_id.product = le16_to_cpu(dev->udev->descriptor.idProduct); 476 rc->map_name = dev->ir_codes; 477 rc->driver_name = "tm6000"; 478 rc->dev.parent = &dev->udev->dev; 479 480 /* ir register */ 481 err = rc_register_device(rc); 482 if (err) 483 goto out; 484 485 return 0; 486 487out: 488 dev->ir = NULL; 489 rc_free_device(rc); 490 kfree(ir); 491 return err; 492} 493 494int tm6000_ir_fini(struct tm6000_core *dev) 495{ 496 struct tm6000_IR *ir = dev->ir; 497 498 /* skip detach on non attached board */ 499 500 if (!ir) 501 return 0; 502 503 dprintk(2, "%s\n",__func__); 504 505 if (!ir->polling) 506 __tm6000_ir_int_stop(ir->rc); 507 508 tm6000_ir_stop(ir->rc); 509 510 /* Turn off the led */ 511 tm6000_flash_led(dev, 0); 512 ir->pwled = 0; 513 514 rc_unregister_device(ir->rc); 515 516 kfree(ir); 517 dev->ir = NULL; 518 519 return 0; 520} 521