| 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@kernel.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 "cx23885.h" |
| 34 | #include "cx23885-input.h" |
| 35 | |
| 36 | #include <linux/slab.h> |
| 37 | #include <media/rc-core.h> |
| 38 | #include <media/v4l2-subdev.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 | case CX23885_BOARD_HAUPPAUGE_HVR1265_K4: |
| 98 | /* |
| 99 | * The only boards we handle right now. However other boards |
| 100 | * using the CX2388x integrated IR controller should be similar |
| 101 | */ |
| 102 | break; |
| 103 | default: |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | overrun = events & (V4L2_SUBDEV_IR_RX_SW_FIFO_OVERRUN | |
| 108 | V4L2_SUBDEV_IR_RX_HW_FIFO_OVERRUN); |
| 109 | |
| 110 | data_available = events & (V4L2_SUBDEV_IR_RX_END_OF_RX_DETECTED | |
| 111 | V4L2_SUBDEV_IR_RX_FIFO_SERVICE_REQ); |
| 112 | |
| 113 | if (overrun) { |
| 114 | /* If there was a FIFO overrun, stop the device */ |
| 115 | v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, ¶ms); |
| 116 | params.enable = false; |
| 117 | /* Mitigate race with cx23885_input_ir_stop() */ |
| 118 | params.shutdown = atomic_read(&dev->ir_input_stopping); |
| 119 | v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, ¶ms); |
| 120 | } |
| 121 | |
| 122 | if (data_available) |
| 123 | cx23885_input_process_measurements(dev, overrun); |
| 124 | |
| 125 | if (overrun) { |
| 126 | /* If there was a FIFO overrun, clear & restart the device */ |
| 127 | params.enable = true; |
| 128 | /* Mitigate race with cx23885_input_ir_stop() */ |
| 129 | params.shutdown = atomic_read(&dev->ir_input_stopping); |
| 130 | v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, ¶ms); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | static int cx23885_input_ir_start(struct cx23885_dev *dev) |
| 135 | { |
| 136 | struct v4l2_subdev_ir_parameters params; |
| 137 | |
| 138 | if (dev->sd_ir == NULL) |
| 139 | return -ENODEV; |
| 140 | |
| 141 | atomic_set(&dev->ir_input_stopping, 0); |
| 142 | |
| 143 | v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, ¶ms); |
| 144 | switch (dev->board) { |
| 145 | case CX23885_BOARD_HAUPPAUGE_HVR1270: |
| 146 | case CX23885_BOARD_HAUPPAUGE_HVR1850: |
| 147 | case CX23885_BOARD_HAUPPAUGE_HVR1290: |
| 148 | case CX23885_BOARD_HAUPPAUGE_HVR1250: |
| 149 | case CX23885_BOARD_MYGICA_X8507: |
| 150 | case CX23885_BOARD_DVBSKY_T9580: |
| 151 | case CX23885_BOARD_DVBSKY_T980C: |
| 152 | case CX23885_BOARD_DVBSKY_S950C: |
| 153 | case CX23885_BOARD_TT_CT2_4500_CI: |
| 154 | case CX23885_BOARD_DVBSKY_S950: |
| 155 | case CX23885_BOARD_DVBSKY_S952: |
| 156 | case CX23885_BOARD_DVBSKY_T982: |
| 157 | case CX23885_BOARD_HAUPPAUGE_HVR1265_K4: |
| 158 | /* |
| 159 | * The IR controller on this board only returns pulse widths. |
| 160 | * Any other mode setting will fail to set up the device. |
| 161 | */ |
| 162 | params.mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH; |
| 163 | params.enable = true; |
| 164 | params.interrupt_enable = true; |
| 165 | params.shutdown = false; |
| 166 | |
| 167 | /* Setup for baseband compatible with both RC-5 and RC-6A */ |
| 168 | params.modulation = false; |
| 169 | /* RC-5: 2,222,222 ns = 1/36 kHz * 32 cycles * 2 marks * 1.25*/ |
| 170 | /* RC-6A: 3,333,333 ns = 1/36 kHz * 16 cycles * 6 marks * 1.25*/ |
| 171 | params.max_pulse_width = 3333333; /* ns */ |
| 172 | /* RC-5: 666,667 ns = 1/36 kHz * 32 cycles * 1 mark * 0.75 */ |
| 173 | /* RC-6A: 333,333 ns = 1/36 kHz * 16 cycles * 1 mark * 0.75 */ |
| 174 | params.noise_filter_min_width = 333333; /* ns */ |
| 175 | /* |
| 176 | * This board has inverted receive sense: |
| 177 | * mark is received as low logic level; |
| 178 | * falling edges are detected as rising edges; etc. |
| 179 | */ |
| 180 | params.invert_level = true; |
| 181 | break; |
| 182 | case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL: |
| 183 | case CX23885_BOARD_TEVII_S470: |
| 184 | case CX23885_BOARD_TBS_6980: |
| 185 | case CX23885_BOARD_TBS_6981: |
| 186 | /* |
| 187 | * The IR controller on this board only returns pulse widths. |
| 188 | * Any other mode setting will fail to set up the device. |
| 189 | */ |
| 190 | params.mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH; |
| 191 | params.enable = true; |
| 192 | params.interrupt_enable = true; |
| 193 | params.shutdown = false; |
| 194 | |
| 195 | /* Setup for a standard NEC protocol */ |
| 196 | params.carrier_freq = 37917; /* Hz, 455 kHz/12 for NEC */ |
| 197 | params.carrier_range_lower = 33000; /* Hz */ |
| 198 | params.carrier_range_upper = 43000; /* Hz */ |
| 199 | params.duty_cycle = 33; /* percent, 33 percent for NEC */ |
| 200 | |
| 201 | /* |
| 202 | * NEC max pulse width: (64/3)/(455 kHz/12) * 16 nec_units |
| 203 | * (64/3)/(455 kHz/12) * 16 nec_units * 1.375 = 12378022 ns |
| 204 | */ |
| 205 | params.max_pulse_width = 12378022; /* ns */ |
| 206 | |
| 207 | /* |
| 208 | * NEC noise filter min width: (64/3)/(455 kHz/12) * 1 nec_unit |
| 209 | * (64/3)/(455 kHz/12) * 1 nec_units * 0.625 = 351648 ns |
| 210 | */ |
| 211 | params.noise_filter_min_width = 351648; /* ns */ |
| 212 | |
| 213 | params.modulation = false; |
| 214 | params.invert_level = true; |
| 215 | break; |
| 216 | } |
| 217 | v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, ¶ms); |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | static int cx23885_input_ir_open(struct rc_dev *rc) |
| 222 | { |
| 223 | struct cx23885_kernel_ir *kernel_ir = rc->priv; |
| 224 | |
| 225 | if (kernel_ir->cx == NULL) |
| 226 | return -ENODEV; |
| 227 | |
| 228 | return cx23885_input_ir_start(kernel_ir->cx); |
| 229 | } |
| 230 | |
| 231 | static void cx23885_input_ir_stop(struct cx23885_dev *dev) |
| 232 | { |
| 233 | struct v4l2_subdev_ir_parameters params; |
| 234 | |
| 235 | if (dev->sd_ir == NULL) |
| 236 | return; |
| 237 | |
| 238 | /* |
| 239 | * Stop the sd_ir subdevice from generating notifications and |
| 240 | * scheduling work. |
| 241 | * It is shutdown this way in order to mitigate a race with |
| 242 | * cx23885_input_rx_work_handler() in the overrun case, which could |
| 243 | * re-enable the subdevice. |
| 244 | */ |
| 245 | atomic_set(&dev->ir_input_stopping, 1); |
| 246 | v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, ¶ms); |
| 247 | while (params.shutdown == false) { |
| 248 | params.enable = false; |
| 249 | params.interrupt_enable = false; |
| 250 | params.shutdown = true; |
| 251 | v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, ¶ms); |
| 252 | v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, ¶ms); |
| 253 | } |
| 254 | flush_work(&dev->cx25840_work); |
| 255 | flush_work(&dev->ir_rx_work); |
| 256 | flush_work(&dev->ir_tx_work); |
| 257 | } |
| 258 | |
| 259 | static void cx23885_input_ir_close(struct rc_dev *rc) |
| 260 | { |
| 261 | struct cx23885_kernel_ir *kernel_ir = rc->priv; |
| 262 | |
| 263 | if (kernel_ir->cx != NULL) |
| 264 | cx23885_input_ir_stop(kernel_ir->cx); |
| 265 | } |
| 266 | |
| 267 | int cx23885_input_init(struct cx23885_dev *dev) |
| 268 | { |
| 269 | struct cx23885_kernel_ir *kernel_ir; |
| 270 | struct rc_dev *rc; |
| 271 | char *rc_map; |
| 272 | u64 allowed_protos; |
| 273 | |
| 274 | int ret; |
| 275 | |
| 276 | /* |
| 277 | * If the IR device (hardware registers, chip, GPIO lines, etc.) isn't |
| 278 | * encapsulated in a v4l2_subdev, then I'm not going to deal with it. |
| 279 | */ |
| 280 | if (dev->sd_ir == NULL) |
| 281 | return -ENODEV; |
| 282 | |
| 283 | switch (dev->board) { |
| 284 | case CX23885_BOARD_HAUPPAUGE_HVR1270: |
| 285 | case CX23885_BOARD_HAUPPAUGE_HVR1850: |
| 286 | case CX23885_BOARD_HAUPPAUGE_HVR1290: |
| 287 | case CX23885_BOARD_HAUPPAUGE_HVR1250: |
| 288 | case CX23885_BOARD_HAUPPAUGE_HVR1265_K4: |
| 289 | /* Integrated CX2388[58] IR controller */ |
| 290 | allowed_protos = RC_PROTO_BIT_ALL_IR_DECODER; |
| 291 | /* The grey Hauppauge RC-5 remote */ |
| 292 | rc_map = RC_MAP_HAUPPAUGE; |
| 293 | break; |
| 294 | case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL: |
| 295 | /* Integrated CX23885 IR controller */ |
| 296 | allowed_protos = RC_PROTO_BIT_ALL_IR_DECODER; |
| 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 | allowed_protos = RC_PROTO_BIT_ALL_IR_DECODER; |
| 303 | /* A guess at the remote */ |
| 304 | rc_map = RC_MAP_TEVII_NEC; |
| 305 | break; |
| 306 | case CX23885_BOARD_MYGICA_X8507: |
| 307 | /* Integrated CX23885 IR controller */ |
| 308 | allowed_protos = RC_PROTO_BIT_ALL_IR_DECODER; |
| 309 | /* A guess at the remote */ |
| 310 | rc_map = RC_MAP_TOTAL_MEDIA_IN_HAND_02; |
| 311 | break; |
| 312 | case CX23885_BOARD_TBS_6980: |
| 313 | case CX23885_BOARD_TBS_6981: |
| 314 | /* Integrated CX23885 IR controller */ |
| 315 | allowed_protos = RC_PROTO_BIT_ALL_IR_DECODER; |
| 316 | /* A guess at the remote */ |
| 317 | rc_map = RC_MAP_TBS_NEC; |
| 318 | break; |
| 319 | case CX23885_BOARD_DVBSKY_T9580: |
| 320 | case CX23885_BOARD_DVBSKY_T980C: |
| 321 | case CX23885_BOARD_DVBSKY_S950C: |
| 322 | case CX23885_BOARD_DVBSKY_S950: |
| 323 | case CX23885_BOARD_DVBSKY_S952: |
| 324 | case CX23885_BOARD_DVBSKY_T982: |
| 325 | /* Integrated CX23885 IR controller */ |
| 326 | allowed_protos = RC_PROTO_BIT_ALL_IR_DECODER; |
| 327 | rc_map = RC_MAP_DVBSKY; |
| 328 | break; |
| 329 | case CX23885_BOARD_TT_CT2_4500_CI: |
| 330 | /* Integrated CX23885 IR controller */ |
| 331 | allowed_protos = RC_PROTO_BIT_ALL_IR_DECODER; |
| 332 | rc_map = RC_MAP_TT_1500; |
| 333 | break; |
| 334 | default: |
| 335 | return -ENODEV; |
| 336 | } |
| 337 | |
| 338 | /* cx23885 board instance kernel IR state */ |
| 339 | kernel_ir = kzalloc(sizeof(struct cx23885_kernel_ir), GFP_KERNEL); |
| 340 | if (kernel_ir == NULL) |
| 341 | return -ENOMEM; |
| 342 | |
| 343 | kernel_ir->cx = dev; |
| 344 | kernel_ir->name = kasprintf(GFP_KERNEL, "cx23885 IR (%s)", |
| 345 | cx23885_boards[dev->board].name); |
| 346 | if (!kernel_ir->name) { |
| 347 | ret = -ENOMEM; |
| 348 | goto err_out_free; |
| 349 | } |
| 350 | |
| 351 | kernel_ir->phys = kasprintf(GFP_KERNEL, "pci-%s/ir0", |
| 352 | pci_name(dev->pci)); |
| 353 | if (!kernel_ir->phys) { |
| 354 | ret = -ENOMEM; |
| 355 | goto err_out_free_name; |
| 356 | } |
| 357 | |
| 358 | /* input device */ |
| 359 | rc = rc_allocate_device(RC_DRIVER_IR_RAW); |
| 360 | if (!rc) { |
| 361 | ret = -ENOMEM; |
| 362 | goto err_out_free_phys; |
| 363 | } |
| 364 | |
| 365 | kernel_ir->rc = rc; |
| 366 | rc->device_name = kernel_ir->name; |
| 367 | rc->input_phys = kernel_ir->phys; |
| 368 | rc->input_id.bustype = BUS_PCI; |
| 369 | rc->input_id.version = 1; |
| 370 | if (dev->pci->subsystem_vendor) { |
| 371 | rc->input_id.vendor = dev->pci->subsystem_vendor; |
| 372 | rc->input_id.product = dev->pci->subsystem_device; |
| 373 | } else { |
| 374 | rc->input_id.vendor = dev->pci->vendor; |
| 375 | rc->input_id.product = dev->pci->device; |
| 376 | } |
| 377 | rc->dev.parent = &dev->pci->dev; |
| 378 | rc->allowed_protocols = allowed_protos; |
| 379 | rc->priv = kernel_ir; |
| 380 | rc->open = cx23885_input_ir_open; |
| 381 | rc->close = cx23885_input_ir_close; |
| 382 | rc->map_name = rc_map; |
| 383 | rc->driver_name = MODULE_NAME; |
| 384 | |
| 385 | /* Go */ |
| 386 | dev->kernel_ir = kernel_ir; |
| 387 | ret = rc_register_device(rc); |
| 388 | if (ret) |
| 389 | goto err_out_stop; |
| 390 | |
| 391 | return 0; |
| 392 | |
| 393 | err_out_stop: |
| 394 | cx23885_input_ir_stop(dev); |
| 395 | dev->kernel_ir = NULL; |
| 396 | rc_free_device(rc); |
| 397 | err_out_free_phys: |
| 398 | kfree(kernel_ir->phys); |
| 399 | err_out_free_name: |
| 400 | kfree(kernel_ir->name); |
| 401 | err_out_free: |
| 402 | kfree(kernel_ir); |
| 403 | return ret; |
| 404 | } |
| 405 | |
| 406 | void cx23885_input_fini(struct cx23885_dev *dev) |
| 407 | { |
| 408 | /* Always stop the IR hardware from generating interrupts */ |
| 409 | cx23885_input_ir_stop(dev); |
| 410 | |
| 411 | if (dev->kernel_ir == NULL) |
| 412 | return; |
| 413 | rc_unregister_device(dev->kernel_ir->rc); |
| 414 | kfree(dev->kernel_ir->phys); |
| 415 | kfree(dev->kernel_ir->name); |
| 416 | kfree(dev->kernel_ir); |
| 417 | dev->kernel_ir = NULL; |
| 418 | } |