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 * You should have received a copy of the GNU General Public License 33 * along with this program; if not, write to the Free Software 34 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 35 * 02110-1301, USA. 36 */ 37 38 #include <linux/slab.h> 39 #include <media/rc-core.h> 40 #include <media/v4l2-subdev.h> 41 42 #include "cx23885.h" 43 #include "cx23885-input.h" 44 45 #define MODULE_NAME "cx23885" 46 47 static void cx23885_input_process_measurements(struct cx23885_dev *dev, 48 bool overrun) 49 { 50 struct cx23885_kernel_ir *kernel_ir = dev->kernel_ir; 51 52 ssize_t num; 53 int count, i; 54 bool handle = false; 55 struct ir_raw_event ir_core_event[64]; 56 57 do { 58 num = 0; 59 v4l2_subdev_call(dev->sd_ir, ir, rx_read, (u8 *) ir_core_event, 60 sizeof(ir_core_event), &num); 61 62 count = num / sizeof(struct ir_raw_event); 63 64 for (i = 0; i < count; i++) { 65 ir_raw_event_store(kernel_ir->rc, 66 &ir_core_event[i]); 67 handle = true; 68 } 69 } while (num != 0); 70 71 if (overrun) 72 ir_raw_event_reset(kernel_ir->rc); 73 else if (handle) 74 ir_raw_event_handle(kernel_ir->rc); 75 } 76 77 void cx23885_input_rx_work_handler(struct cx23885_dev *dev, u32 events) 78 { 79 struct v4l2_subdev_ir_parameters params; 80 int overrun, data_available; 81 82 if (dev->sd_ir == NULL || events == 0) 83 return; 84 85 switch (dev->board) { 86 case CX23885_BOARD_HAUPPAUGE_HVR1270: 87 case CX23885_BOARD_HAUPPAUGE_HVR1850: 88 case CX23885_BOARD_HAUPPAUGE_HVR1290: 89 case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL: 90 case CX23885_BOARD_TEVII_S470: 91 case CX23885_BOARD_HAUPPAUGE_HVR1250: 92 /* 93 * The only boards we handle right now. However other boards 94 * using the CX2388x integrated IR controller should be similar 95 */ 96 break; 97 default: 98 return; 99 } 100 101 overrun = events & (V4L2_SUBDEV_IR_RX_SW_FIFO_OVERRUN | 102 V4L2_SUBDEV_IR_RX_HW_FIFO_OVERRUN); 103 104 data_available = events & (V4L2_SUBDEV_IR_RX_END_OF_RX_DETECTED | 105 V4L2_SUBDEV_IR_RX_FIFO_SERVICE_REQ); 106 107 if (overrun) { 108 /* If there was a FIFO overrun, stop the device */ 109 v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, ¶ms); 110 params.enable = false; 111 /* Mitigate race with cx23885_input_ir_stop() */ 112 params.shutdown = atomic_read(&dev->ir_input_stopping); 113 v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, ¶ms); 114 } 115 116 if (data_available) 117 cx23885_input_process_measurements(dev, overrun); 118 119 if (overrun) { 120 /* If there was a FIFO overrun, clear & restart the device */ 121 params.enable = true; 122 /* Mitigate race with cx23885_input_ir_stop() */ 123 params.shutdown = atomic_read(&dev->ir_input_stopping); 124 v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, ¶ms); 125 } 126 } 127 128 static int cx23885_input_ir_start(struct cx23885_dev *dev) 129 { 130 struct v4l2_subdev_ir_parameters params; 131 132 if (dev->sd_ir == NULL) 133 return -ENODEV; 134 135 atomic_set(&dev->ir_input_stopping, 0); 136 137 v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, ¶ms); 138 switch (dev->board) { 139 case CX23885_BOARD_HAUPPAUGE_HVR1270: 140 case CX23885_BOARD_HAUPPAUGE_HVR1850: 141 case CX23885_BOARD_HAUPPAUGE_HVR1290: 142 case CX23885_BOARD_HAUPPAUGE_HVR1250: 143 /* 144 * The IR controller on this board only returns pulse widths. 145 * Any other mode setting will fail to set up the device. 146 */ 147 params.mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH; 148 params.enable = true; 149 params.interrupt_enable = true; 150 params.shutdown = false; 151 152 /* Setup for baseband compatible with both RC-5 and RC-6A */ 153 params.modulation = false; 154 /* RC-5: 2,222,222 ns = 1/36 kHz * 32 cycles * 2 marks * 1.25*/ 155 /* RC-6A: 3,333,333 ns = 1/36 kHz * 16 cycles * 6 marks * 1.25*/ 156 params.max_pulse_width = 3333333; /* ns */ 157 /* RC-5: 666,667 ns = 1/36 kHz * 32 cycles * 1 mark * 0.75 */ 158 /* RC-6A: 333,333 ns = 1/36 kHz * 16 cycles * 1 mark * 0.75 */ 159 params.noise_filter_min_width = 333333; /* ns */ 160 /* 161 * This board has inverted receive sense: 162 * mark is received as low logic level; 163 * falling edges are detected as rising edges; etc. 164 */ 165 params.invert_level = true; 166 break; 167 case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL: 168 case CX23885_BOARD_TEVII_S470: 169 /* 170 * The IR controller on this board only returns pulse widths. 171 * Any other mode setting will fail to set up the device. 172 */ 173 params.mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH; 174 params.enable = true; 175 params.interrupt_enable = true; 176 params.shutdown = false; 177 178 /* Setup for a standard NEC protocol */ 179 params.carrier_freq = 37917; /* Hz, 455 kHz/12 for NEC */ 180 params.carrier_range_lower = 33000; /* Hz */ 181 params.carrier_range_upper = 43000; /* Hz */ 182 params.duty_cycle = 33; /* percent, 33 percent for NEC */ 183 184 /* 185 * NEC max pulse width: (64/3)/(455 kHz/12) * 16 nec_units 186 * (64/3)/(455 kHz/12) * 16 nec_units * 1.375 = 12378022 ns 187 */ 188 params.max_pulse_width = 12378022; /* ns */ 189 190 /* 191 * NEC noise filter min width: (64/3)/(455 kHz/12) * 1 nec_unit 192 * (64/3)/(455 kHz/12) * 1 nec_units * 0.625 = 351648 ns 193 */ 194 params.noise_filter_min_width = 351648; /* ns */ 195 196 params.modulation = false; 197 params.invert_level = true; 198 break; 199 } 200 v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, ¶ms); 201 return 0; 202 } 203 204 static int cx23885_input_ir_open(struct rc_dev *rc) 205 { 206 struct cx23885_kernel_ir *kernel_ir = rc->priv; 207 208 if (kernel_ir->cx == NULL) 209 return -ENODEV; 210 211 return cx23885_input_ir_start(kernel_ir->cx); 212 } 213 214 static void cx23885_input_ir_stop(struct cx23885_dev *dev) 215 { 216 struct v4l2_subdev_ir_parameters params; 217 218 if (dev->sd_ir == NULL) 219 return; 220 221 /* 222 * Stop the sd_ir subdevice from generating notifications and 223 * scheduling work. 224 * It is shutdown this way in order to mitigate a race with 225 * cx23885_input_rx_work_handler() in the overrun case, which could 226 * re-enable the subdevice. 227 */ 228 atomic_set(&dev->ir_input_stopping, 1); 229 v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, ¶ms); 230 while (params.shutdown == false) { 231 params.enable = false; 232 params.interrupt_enable = false; 233 params.shutdown = true; 234 v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, ¶ms); 235 v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, ¶ms); 236 } 237 flush_work(&dev->cx25840_work); 238 flush_work(&dev->ir_rx_work); 239 flush_work(&dev->ir_tx_work); 240 } 241 242 static void cx23885_input_ir_close(struct rc_dev *rc) 243 { 244 struct cx23885_kernel_ir *kernel_ir = rc->priv; 245 246 if (kernel_ir->cx != NULL) 247 cx23885_input_ir_stop(kernel_ir->cx); 248 } 249 250 int cx23885_input_init(struct cx23885_dev *dev) 251 { 252 struct cx23885_kernel_ir *kernel_ir; 253 struct rc_dev *rc; 254 char *rc_map; 255 enum rc_driver_type driver_type; 256 unsigned long allowed_protos; 257 258 int ret; 259 260 /* 261 * If the IR device (hardware registers, chip, GPIO lines, etc.) isn't 262 * encapsulated in a v4l2_subdev, then I'm not going to deal with it. 263 */ 264 if (dev->sd_ir == NULL) 265 return -ENODEV; 266 267 switch (dev->board) { 268 case CX23885_BOARD_HAUPPAUGE_HVR1270: 269 case CX23885_BOARD_HAUPPAUGE_HVR1850: 270 case CX23885_BOARD_HAUPPAUGE_HVR1290: 271 case CX23885_BOARD_HAUPPAUGE_HVR1250: 272 /* Integrated CX2388[58] IR controller */ 273 driver_type = RC_DRIVER_IR_RAW; 274 allowed_protos = RC_BIT_ALL; 275 /* The grey Hauppauge RC-5 remote */ 276 rc_map = RC_MAP_HAUPPAUGE; 277 break; 278 case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL: 279 /* Integrated CX23885 IR controller */ 280 driver_type = RC_DRIVER_IR_RAW; 281 allowed_protos = RC_BIT_NEC; 282 /* The grey Terratec remote with orange buttons */ 283 rc_map = RC_MAP_NEC_TERRATEC_CINERGY_XS; 284 break; 285 case CX23885_BOARD_TEVII_S470: 286 /* Integrated CX23885 IR controller */ 287 driver_type = RC_DRIVER_IR_RAW; 288 allowed_protos = RC_BIT_ALL; 289 /* A guess at the remote */ 290 rc_map = RC_MAP_TEVII_NEC; 291 break; 292 default: 293 return -ENODEV; 294 } 295 296 /* cx23885 board instance kernel IR state */ 297 kernel_ir = kzalloc(sizeof(struct cx23885_kernel_ir), GFP_KERNEL); 298 if (kernel_ir == NULL) 299 return -ENOMEM; 300 301 kernel_ir->cx = dev; 302 kernel_ir->name = kasprintf(GFP_KERNEL, "cx23885 IR (%s)", 303 cx23885_boards[dev->board].name); 304 kernel_ir->phys = kasprintf(GFP_KERNEL, "pci-%s/ir0", 305 pci_name(dev->pci)); 306 307 /* input device */ 308 rc = rc_allocate_device(); 309 if (!rc) { 310 ret = -ENOMEM; 311 goto err_out_free; 312 } 313 314 kernel_ir->rc = rc; 315 rc->input_name = kernel_ir->name; 316 rc->input_phys = kernel_ir->phys; 317 rc->input_id.bustype = BUS_PCI; 318 rc->input_id.version = 1; 319 if (dev->pci->subsystem_vendor) { 320 rc->input_id.vendor = dev->pci->subsystem_vendor; 321 rc->input_id.product = dev->pci->subsystem_device; 322 } else { 323 rc->input_id.vendor = dev->pci->vendor; 324 rc->input_id.product = dev->pci->device; 325 } 326 rc->dev.parent = &dev->pci->dev; 327 rc->driver_type = driver_type; 328 rc->allowed_protos = allowed_protos; 329 rc->priv = kernel_ir; 330 rc->open = cx23885_input_ir_open; 331 rc->close = cx23885_input_ir_close; 332 rc->map_name = rc_map; 333 rc->driver_name = MODULE_NAME; 334 335 /* Go */ 336 dev->kernel_ir = kernel_ir; 337 ret = rc_register_device(rc); 338 if (ret) 339 goto err_out_stop; 340 341 return 0; 342 343 err_out_stop: 344 cx23885_input_ir_stop(dev); 345 dev->kernel_ir = NULL; 346 rc_free_device(rc); 347 err_out_free: 348 kfree(kernel_ir->phys); 349 kfree(kernel_ir->name); 350 kfree(kernel_ir); 351 return ret; 352 } 353 354 void cx23885_input_fini(struct cx23885_dev *dev) 355 { 356 /* Always stop the IR hardware from generating interrupts */ 357 cx23885_input_ir_stop(dev); 358 359 if (dev->kernel_ir == NULL) 360 return; 361 rc_unregister_device(dev->kernel_ir->rc); 362 kfree(dev->kernel_ir->phys); 363 kfree(dev->kernel_ir->name); 364 kfree(dev->kernel_ir); 365 dev->kernel_ir = NULL; 366 } 367