1 /* 2 * Driver for the Conexant CX23885/7/8 PCIe bridge 3 * 4 * Infrared remote control input device 5 * 6 * Most of this file is 7 * 8 * Copyright (C) 2009 Andy Walls <awalls@md.metrocast.net> 9 * 10 * However, the cx23885_input_{init,fini} functions contained herein are 11 * derived from Linux kernel files linux/media/video/.../...-input.c marked as: 12 * 13 * Copyright (C) 2008 <srinivasa.deevi at conexant dot com> 14 * Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it> 15 * Markus Rechberger <mrechberger@gmail.com> 16 * Mauro Carvalho Chehab <mchehab@infradead.org> 17 * Sascha Sommer <saschasommer@freenet.de> 18 * Copyright (C) 2004, 2005 Chris Pascoe 19 * Copyright (C) 2003, 2004 Gerd Knorr 20 * Copyright (C) 2003 Pavel Machek 21 * 22 * This program is free software; you can redistribute it and/or 23 * modify it under the terms of the GNU General Public License 24 * as published by the Free Software Foundation; either version 2 25 * of the License, or (at your option) any later version. 26 * 27 * This program is distributed in the hope that it will be useful, 28 * but WITHOUT ANY WARRANTY; without even the implied warranty of 29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 30 * GNU General Public License for more details. 31 */ 32 33 #include <linux/slab.h> 34 #include <media/rc-core.h> 35 #include <media/v4l2-subdev.h> 36 37 #include "cx23885.h" 38 #include "cx23885-input.h" 39 40 #define MODULE_NAME "cx23885" 41 42 static void cx23885_input_process_measurements(struct cx23885_dev *dev, 43 bool overrun) 44 { 45 struct cx23885_kernel_ir *kernel_ir = dev->kernel_ir; 46 47 ssize_t num; 48 int count, i; 49 bool handle = false; 50 struct ir_raw_event ir_core_event[64]; 51 52 do { 53 num = 0; 54 v4l2_subdev_call(dev->sd_ir, ir, rx_read, (u8 *) ir_core_event, 55 sizeof(ir_core_event), &num); 56 57 count = num / sizeof(struct ir_raw_event); 58 59 for (i = 0; i < count; i++) { 60 ir_raw_event_store(kernel_ir->rc, 61 &ir_core_event[i]); 62 handle = true; 63 } 64 } while (num != 0); 65 66 if (overrun) 67 ir_raw_event_reset(kernel_ir->rc); 68 else if (handle) 69 ir_raw_event_handle(kernel_ir->rc); 70 } 71 72 void cx23885_input_rx_work_handler(struct cx23885_dev *dev, u32 events) 73 { 74 struct v4l2_subdev_ir_parameters params; 75 int overrun, data_available; 76 77 if (dev->sd_ir == NULL || events == 0) 78 return; 79 80 switch (dev->board) { 81 case CX23885_BOARD_HAUPPAUGE_HVR1270: 82 case CX23885_BOARD_HAUPPAUGE_HVR1850: 83 case CX23885_BOARD_HAUPPAUGE_HVR1290: 84 case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL: 85 case CX23885_BOARD_TEVII_S470: 86 case CX23885_BOARD_HAUPPAUGE_HVR1250: 87 case CX23885_BOARD_MYGICA_X8507: 88 case CX23885_BOARD_TBS_6980: 89 case CX23885_BOARD_TBS_6981: 90 case CX23885_BOARD_DVBSKY_T9580: 91 case CX23885_BOARD_DVBSKY_T980C: 92 case CX23885_BOARD_DVBSKY_S950C: 93 case CX23885_BOARD_TT_CT2_4500_CI: 94 case CX23885_BOARD_DVBSKY_S950: 95 case CX23885_BOARD_DVBSKY_S952: 96 case CX23885_BOARD_DVBSKY_T982: 97 /* 98 * The only boards we handle right now. However other boards 99 * using the CX2388x integrated IR controller should be similar 100 */ 101 break; 102 default: 103 return; 104 } 105 106 overrun = events & (V4L2_SUBDEV_IR_RX_SW_FIFO_OVERRUN | 107 V4L2_SUBDEV_IR_RX_HW_FIFO_OVERRUN); 108 109 data_available = events & (V4L2_SUBDEV_IR_RX_END_OF_RX_DETECTED | 110 V4L2_SUBDEV_IR_RX_FIFO_SERVICE_REQ); 111 112 if (overrun) { 113 /* If there was a FIFO overrun, stop the device */ 114 v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, ¶ms); 115 params.enable = false; 116 /* Mitigate race with cx23885_input_ir_stop() */ 117 params.shutdown = atomic_read(&dev->ir_input_stopping); 118 v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, ¶ms); 119 } 120 121 if (data_available) 122 cx23885_input_process_measurements(dev, overrun); 123 124 if (overrun) { 125 /* If there was a FIFO overrun, clear & restart the device */ 126 params.enable = true; 127 /* Mitigate race with cx23885_input_ir_stop() */ 128 params.shutdown = atomic_read(&dev->ir_input_stopping); 129 v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, ¶ms); 130 } 131 } 132 133 static int cx23885_input_ir_start(struct cx23885_dev *dev) 134 { 135 struct v4l2_subdev_ir_parameters params; 136 137 if (dev->sd_ir == NULL) 138 return -ENODEV; 139 140 atomic_set(&dev->ir_input_stopping, 0); 141 142 v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, ¶ms); 143 switch (dev->board) { 144 case CX23885_BOARD_HAUPPAUGE_HVR1270: 145 case CX23885_BOARD_HAUPPAUGE_HVR1850: 146 case CX23885_BOARD_HAUPPAUGE_HVR1290: 147 case CX23885_BOARD_HAUPPAUGE_HVR1250: 148 case CX23885_BOARD_MYGICA_X8507: 149 case CX23885_BOARD_DVBSKY_T9580: 150 case CX23885_BOARD_DVBSKY_T980C: 151 case CX23885_BOARD_DVBSKY_S950C: 152 case CX23885_BOARD_TT_CT2_4500_CI: 153 case CX23885_BOARD_DVBSKY_S950: 154 case CX23885_BOARD_DVBSKY_S952: 155 case CX23885_BOARD_DVBSKY_T982: 156 /* 157 * The IR controller on this board only returns pulse widths. 158 * Any other mode setting will fail to set up the device. 159 */ 160 params.mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH; 161 params.enable = true; 162 params.interrupt_enable = true; 163 params.shutdown = false; 164 165 /* Setup for baseband compatible with both RC-5 and RC-6A */ 166 params.modulation = false; 167 /* RC-5: 2,222,222 ns = 1/36 kHz * 32 cycles * 2 marks * 1.25*/ 168 /* RC-6A: 3,333,333 ns = 1/36 kHz * 16 cycles * 6 marks * 1.25*/ 169 params.max_pulse_width = 3333333; /* ns */ 170 /* RC-5: 666,667 ns = 1/36 kHz * 32 cycles * 1 mark * 0.75 */ 171 /* RC-6A: 333,333 ns = 1/36 kHz * 16 cycles * 1 mark * 0.75 */ 172 params.noise_filter_min_width = 333333; /* ns */ 173 /* 174 * This board has inverted receive sense: 175 * mark is received as low logic level; 176 * falling edges are detected as rising edges; etc. 177 */ 178 params.invert_level = true; 179 break; 180 case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL: 181 case CX23885_BOARD_TEVII_S470: 182 case CX23885_BOARD_TBS_6980: 183 case CX23885_BOARD_TBS_6981: 184 /* 185 * The IR controller on this board only returns pulse widths. 186 * Any other mode setting will fail to set up the device. 187 */ 188 params.mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH; 189 params.enable = true; 190 params.interrupt_enable = true; 191 params.shutdown = false; 192 193 /* Setup for a standard NEC protocol */ 194 params.carrier_freq = 37917; /* Hz, 455 kHz/12 for NEC */ 195 params.carrier_range_lower = 33000; /* Hz */ 196 params.carrier_range_upper = 43000; /* Hz */ 197 params.duty_cycle = 33; /* percent, 33 percent for NEC */ 198 199 /* 200 * NEC max pulse width: (64/3)/(455 kHz/12) * 16 nec_units 201 * (64/3)/(455 kHz/12) * 16 nec_units * 1.375 = 12378022 ns 202 */ 203 params.max_pulse_width = 12378022; /* ns */ 204 205 /* 206 * NEC noise filter min width: (64/3)/(455 kHz/12) * 1 nec_unit 207 * (64/3)/(455 kHz/12) * 1 nec_units * 0.625 = 351648 ns 208 */ 209 params.noise_filter_min_width = 351648; /* ns */ 210 211 params.modulation = false; 212 params.invert_level = true; 213 break; 214 } 215 v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, ¶ms); 216 return 0; 217 } 218 219 static int cx23885_input_ir_open(struct rc_dev *rc) 220 { 221 struct cx23885_kernel_ir *kernel_ir = rc->priv; 222 223 if (kernel_ir->cx == NULL) 224 return -ENODEV; 225 226 return cx23885_input_ir_start(kernel_ir->cx); 227 } 228 229 static void cx23885_input_ir_stop(struct cx23885_dev *dev) 230 { 231 struct v4l2_subdev_ir_parameters params; 232 233 if (dev->sd_ir == NULL) 234 return; 235 236 /* 237 * Stop the sd_ir subdevice from generating notifications and 238 * scheduling work. 239 * It is shutdown this way in order to mitigate a race with 240 * cx23885_input_rx_work_handler() in the overrun case, which could 241 * re-enable the subdevice. 242 */ 243 atomic_set(&dev->ir_input_stopping, 1); 244 v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, ¶ms); 245 while (params.shutdown == false) { 246 params.enable = false; 247 params.interrupt_enable = false; 248 params.shutdown = true; 249 v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, ¶ms); 250 v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, ¶ms); 251 } 252 flush_work(&dev->cx25840_work); 253 flush_work(&dev->ir_rx_work); 254 flush_work(&dev->ir_tx_work); 255 } 256 257 static void cx23885_input_ir_close(struct rc_dev *rc) 258 { 259 struct cx23885_kernel_ir *kernel_ir = rc->priv; 260 261 if (kernel_ir->cx != NULL) 262 cx23885_input_ir_stop(kernel_ir->cx); 263 } 264 265 int cx23885_input_init(struct cx23885_dev *dev) 266 { 267 struct cx23885_kernel_ir *kernel_ir; 268 struct rc_dev *rc; 269 char *rc_map; 270 enum rc_driver_type driver_type; 271 unsigned long allowed_protos; 272 273 int ret; 274 275 /* 276 * If the IR device (hardware registers, chip, GPIO lines, etc.) isn't 277 * encapsulated in a v4l2_subdev, then I'm not going to deal with it. 278 */ 279 if (dev->sd_ir == NULL) 280 return -ENODEV; 281 282 switch (dev->board) { 283 case CX23885_BOARD_HAUPPAUGE_HVR1270: 284 case CX23885_BOARD_HAUPPAUGE_HVR1850: 285 case CX23885_BOARD_HAUPPAUGE_HVR1290: 286 case CX23885_BOARD_HAUPPAUGE_HVR1250: 287 /* Integrated CX2388[58] IR controller */ 288 driver_type = RC_DRIVER_IR_RAW; 289 allowed_protos = RC_BIT_ALL; 290 /* The grey Hauppauge RC-5 remote */ 291 rc_map = RC_MAP_HAUPPAUGE; 292 break; 293 case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL: 294 /* Integrated CX23885 IR controller */ 295 driver_type = RC_DRIVER_IR_RAW; 296 allowed_protos = RC_BIT_NEC; 297 /* The grey Terratec remote with orange buttons */ 298 rc_map = RC_MAP_NEC_TERRATEC_CINERGY_XS; 299 break; 300 case CX23885_BOARD_TEVII_S470: 301 /* Integrated CX23885 IR controller */ 302 driver_type = RC_DRIVER_IR_RAW; 303 allowed_protos = RC_BIT_ALL; 304 /* A guess at the remote */ 305 rc_map = RC_MAP_TEVII_NEC; 306 break; 307 case CX23885_BOARD_MYGICA_X8507: 308 /* Integrated CX23885 IR controller */ 309 driver_type = RC_DRIVER_IR_RAW; 310 allowed_protos = RC_BIT_ALL; 311 /* A guess at the remote */ 312 rc_map = RC_MAP_TOTAL_MEDIA_IN_HAND_02; 313 break; 314 case CX23885_BOARD_TBS_6980: 315 case CX23885_BOARD_TBS_6981: 316 /* Integrated CX23885 IR controller */ 317 driver_type = RC_DRIVER_IR_RAW; 318 allowed_protos = RC_BIT_ALL; 319 /* A guess at the remote */ 320 rc_map = RC_MAP_TBS_NEC; 321 break; 322 case CX23885_BOARD_DVBSKY_T9580: 323 case CX23885_BOARD_DVBSKY_T980C: 324 case CX23885_BOARD_DVBSKY_S950C: 325 case CX23885_BOARD_DVBSKY_S950: 326 case CX23885_BOARD_DVBSKY_S952: 327 case CX23885_BOARD_DVBSKY_T982: 328 /* Integrated CX23885 IR controller */ 329 driver_type = RC_DRIVER_IR_RAW; 330 allowed_protos = RC_BIT_ALL; 331 rc_map = RC_MAP_DVBSKY; 332 break; 333 case CX23885_BOARD_TT_CT2_4500_CI: 334 /* Integrated CX23885 IR controller */ 335 driver_type = RC_DRIVER_IR_RAW; 336 allowed_protos = RC_BIT_ALL; 337 rc_map = RC_MAP_TT_1500; 338 break; 339 default: 340 return -ENODEV; 341 } 342 343 /* cx23885 board instance kernel IR state */ 344 kernel_ir = kzalloc(sizeof(struct cx23885_kernel_ir), GFP_KERNEL); 345 if (kernel_ir == NULL) 346 return -ENOMEM; 347 348 kernel_ir->cx = dev; 349 kernel_ir->name = kasprintf(GFP_KERNEL, "cx23885 IR (%s)", 350 cx23885_boards[dev->board].name); 351 kernel_ir->phys = kasprintf(GFP_KERNEL, "pci-%s/ir0", 352 pci_name(dev->pci)); 353 354 /* input device */ 355 rc = rc_allocate_device(); 356 if (!rc) { 357 ret = -ENOMEM; 358 goto err_out_free; 359 } 360 361 kernel_ir->rc = rc; 362 rc->input_name = kernel_ir->name; 363 rc->input_phys = kernel_ir->phys; 364 rc->input_id.bustype = BUS_PCI; 365 rc->input_id.version = 1; 366 if (dev->pci->subsystem_vendor) { 367 rc->input_id.vendor = dev->pci->subsystem_vendor; 368 rc->input_id.product = dev->pci->subsystem_device; 369 } else { 370 rc->input_id.vendor = dev->pci->vendor; 371 rc->input_id.product = dev->pci->device; 372 } 373 rc->dev.parent = &dev->pci->dev; 374 rc->driver_type = driver_type; 375 rc->allowed_protocols = allowed_protos; 376 rc->priv = kernel_ir; 377 rc->open = cx23885_input_ir_open; 378 rc->close = cx23885_input_ir_close; 379 rc->map_name = rc_map; 380 rc->driver_name = MODULE_NAME; 381 382 /* Go */ 383 dev->kernel_ir = kernel_ir; 384 ret = rc_register_device(rc); 385 if (ret) 386 goto err_out_stop; 387 388 return 0; 389 390 err_out_stop: 391 cx23885_input_ir_stop(dev); 392 dev->kernel_ir = NULL; 393 rc_free_device(rc); 394 err_out_free: 395 kfree(kernel_ir->phys); 396 kfree(kernel_ir->name); 397 kfree(kernel_ir); 398 return ret; 399 } 400 401 void cx23885_input_fini(struct cx23885_dev *dev) 402 { 403 /* Always stop the IR hardware from generating interrupts */ 404 cx23885_input_ir_stop(dev); 405 406 if (dev->kernel_ir == NULL) 407 return; 408 rc_unregister_device(dev->kernel_ir->rc); 409 kfree(dev->kernel_ir->phys); 410 kfree(dev->kernel_ir->name); 411 kfree(dev->kernel_ir); 412 dev->kernel_ir = NULL; 413 } 414