Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 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 | * |
Andy Walls | 6afdeaf | 2010-05-23 18:53:35 -0300 | [diff] [blame] | 8 | * Copyright (C) 2009 Andy Walls <awalls@md.metrocast.net> |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 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/input.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 39 | #include <linux/slab.h> |
Andy Walls | 43c2407 | 2010-06-27 23:15:35 -0300 | [diff] [blame^] | 40 | #include <media/ir-core.h> |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 41 | #include <media/v4l2-subdev.h> |
| 42 | |
| 43 | #include "cx23885.h" |
| 44 | |
Mauro Carvalho Chehab | 727e625 | 2010-03-12 21:18:14 -0300 | [diff] [blame] | 45 | #define MODULE_NAME "cx23885" |
| 46 | |
Andy Walls | 43c2407 | 2010-06-27 23:15:35 -0300 | [diff] [blame^] | 47 | static void convert_measurement(u32 x, struct ir_raw_event *y) |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 48 | { |
Andy Walls | 43c2407 | 2010-06-27 23:15:35 -0300 | [diff] [blame^] | 49 | if (x == V4L2_SUBDEV_IR_PULSE_RX_SEQ_END) { |
| 50 | y->pulse = false; |
| 51 | y->duration = V4L2_SUBDEV_IR_PULSE_MAX_WIDTH_NS; |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | y->pulse = (x & V4L2_SUBDEV_IR_PULSE_LEVEL_MASK) ? true : false; |
| 56 | y->duration = x & V4L2_SUBDEV_IR_PULSE_MAX_WIDTH_NS; |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 57 | } |
| 58 | |
Andy Walls | 43c2407 | 2010-06-27 23:15:35 -0300 | [diff] [blame^] | 59 | static void cx23885_input_process_measurements(struct cx23885_dev *dev, |
| 60 | bool overrun) |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 61 | { |
Andy Walls | 43c2407 | 2010-06-27 23:15:35 -0300 | [diff] [blame^] | 62 | struct cx23885_kernel_ir *kernel_ir = dev->kernel_ir; |
| 63 | struct ir_raw_event kernel_ir_event; |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 64 | |
Andy Walls | 43c2407 | 2010-06-27 23:15:35 -0300 | [diff] [blame^] | 65 | u32 sd_ir_data[64]; |
| 66 | ssize_t num; |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 67 | int count, i; |
Andy Walls | 43c2407 | 2010-06-27 23:15:35 -0300 | [diff] [blame^] | 68 | bool handle = false; |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 69 | |
| 70 | do { |
Andy Walls | 43c2407 | 2010-06-27 23:15:35 -0300 | [diff] [blame^] | 71 | num = 0; |
| 72 | v4l2_subdev_call(dev->sd_ir, ir, rx_read, (u8 *) sd_ir_data, |
| 73 | sizeof(sd_ir_data), &num); |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 74 | |
| 75 | count = num / sizeof(u32); |
| 76 | |
Andy Walls | 43c2407 | 2010-06-27 23:15:35 -0300 | [diff] [blame^] | 77 | for (i = 0; i < count; i++) { |
| 78 | convert_measurement(sd_ir_data[i], &kernel_ir_event); |
| 79 | ir_raw_event_store(kernel_ir->inp_dev, |
| 80 | &kernel_ir_event); |
| 81 | handle = true; |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 82 | } |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 83 | } while (num != 0); |
Andy Walls | 43c2407 | 2010-06-27 23:15:35 -0300 | [diff] [blame^] | 84 | |
| 85 | if (overrun) |
| 86 | ir_raw_event_reset(kernel_ir->inp_dev); |
| 87 | else if (handle) |
| 88 | ir_raw_event_handle(kernel_ir->inp_dev); |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void cx23885_input_rx_work_handler(struct cx23885_dev *dev, u32 events) |
| 92 | { |
| 93 | struct v4l2_subdev_ir_parameters params; |
| 94 | int overrun, data_available; |
| 95 | |
| 96 | if (dev->sd_ir == NULL || events == 0) |
| 97 | return; |
| 98 | |
| 99 | switch (dev->board) { |
| 100 | case CX23885_BOARD_HAUPPAUGE_HVR1850: |
Michael Krufky | 7fec6fe | 2009-11-11 15:46:09 -0300 | [diff] [blame] | 101 | case CX23885_BOARD_HAUPPAUGE_HVR1290: |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 102 | /* |
| 103 | * The only board we handle right now. However other boards |
| 104 | * using the CX2388x integrated IR controller should be similar |
| 105 | */ |
| 106 | break; |
| 107 | default: |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | overrun = events & (V4L2_SUBDEV_IR_RX_SW_FIFO_OVERRUN | |
| 112 | V4L2_SUBDEV_IR_RX_HW_FIFO_OVERRUN); |
| 113 | |
| 114 | data_available = events & (V4L2_SUBDEV_IR_RX_END_OF_RX_DETECTED | |
| 115 | V4L2_SUBDEV_IR_RX_FIFO_SERVICE_REQ); |
| 116 | |
| 117 | if (overrun) { |
| 118 | /* If there was a FIFO overrun, stop the device */ |
| 119 | v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, ¶ms); |
| 120 | params.enable = false; |
| 121 | /* Mitigate race with cx23885_input_ir_stop() */ |
| 122 | params.shutdown = atomic_read(&dev->ir_input_stopping); |
| 123 | v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, ¶ms); |
| 124 | } |
| 125 | |
| 126 | if (data_available) |
Andy Walls | 43c2407 | 2010-06-27 23:15:35 -0300 | [diff] [blame^] | 127 | cx23885_input_process_measurements(dev, overrun); |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 128 | |
| 129 | if (overrun) { |
| 130 | /* If there was a FIFO overrun, clear & restart the device */ |
| 131 | params.enable = true; |
| 132 | /* Mitigate race with cx23885_input_ir_stop() */ |
| 133 | params.shutdown = atomic_read(&dev->ir_input_stopping); |
| 134 | v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, ¶ms); |
| 135 | } |
| 136 | } |
| 137 | |
Andy Walls | 43c2407 | 2010-06-27 23:15:35 -0300 | [diff] [blame^] | 138 | static int cx23885_input_ir_start(struct cx23885_dev *dev) |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 139 | { |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 140 | struct v4l2_subdev_ir_parameters params; |
| 141 | |
| 142 | if (dev->sd_ir == NULL) |
Andy Walls | 43c2407 | 2010-06-27 23:15:35 -0300 | [diff] [blame^] | 143 | return -ENODEV; |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 144 | |
| 145 | atomic_set(&dev->ir_input_stopping, 0); |
| 146 | |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 147 | v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, ¶ms); |
| 148 | switch (dev->board) { |
| 149 | case CX23885_BOARD_HAUPPAUGE_HVR1850: |
Michael Krufky | 7fec6fe | 2009-11-11 15:46:09 -0300 | [diff] [blame] | 150 | case CX23885_BOARD_HAUPPAUGE_HVR1290: |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 151 | /* |
| 152 | * The IR controller on this board only returns pulse widths. |
| 153 | * Any other mode setting will fail to set up the device. |
| 154 | */ |
| 155 | params.mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH; |
| 156 | params.enable = true; |
| 157 | params.interrupt_enable = true; |
| 158 | params.shutdown = false; |
| 159 | |
| 160 | /* Setup for baseband compatible with both RC-5 and RC-6A */ |
| 161 | params.modulation = false; |
| 162 | /* RC-5: 2,222,222 ns = 1/36 kHz * 32 cycles * 2 marks * 1.25*/ |
| 163 | /* RC-6A: 3,333,333 ns = 1/36 kHz * 16 cycles * 6 marks * 1.25*/ |
| 164 | params.max_pulse_width = 3333333; /* ns */ |
| 165 | /* RC-5: 666,667 ns = 1/36 kHz * 32 cycles * 1 mark * 0.75 */ |
| 166 | /* RC-6A: 333,333 ns = 1/36 kHz * 16 cycles * 1 mark * 0.75 */ |
| 167 | params.noise_filter_min_width = 333333; /* ns */ |
| 168 | /* |
| 169 | * This board has inverted receive sense: |
| 170 | * mark is received as low logic level; |
| 171 | * falling edges are detected as rising edges; etc. |
| 172 | */ |
| 173 | params.invert = true; |
| 174 | break; |
| 175 | } |
| 176 | v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, ¶ms); |
Andy Walls | 43c2407 | 2010-06-27 23:15:35 -0300 | [diff] [blame^] | 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static int cx23885_input_ir_open(void *priv) |
| 181 | { |
| 182 | struct cx23885_kernel_ir *kernel_ir = priv; |
| 183 | |
| 184 | if (kernel_ir->cx == NULL) |
| 185 | return -ENODEV; |
| 186 | |
| 187 | return cx23885_input_ir_start(kernel_ir->cx); |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | static void cx23885_input_ir_stop(struct cx23885_dev *dev) |
| 191 | { |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 192 | struct v4l2_subdev_ir_parameters params; |
| 193 | |
| 194 | if (dev->sd_ir == NULL) |
| 195 | return; |
| 196 | |
| 197 | /* |
| 198 | * Stop the sd_ir subdevice from generating notifications and |
| 199 | * scheduling work. |
| 200 | * It is shutdown this way in order to mitigate a race with |
| 201 | * cx23885_input_rx_work_handler() in the overrun case, which could |
| 202 | * re-enable the subdevice. |
| 203 | */ |
| 204 | atomic_set(&dev->ir_input_stopping, 1); |
| 205 | v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, ¶ms); |
| 206 | while (params.shutdown == false) { |
| 207 | params.enable = false; |
| 208 | params.interrupt_enable = false; |
| 209 | params.shutdown = true; |
| 210 | v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, ¶ms); |
| 211 | v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, ¶ms); |
| 212 | } |
| 213 | |
| 214 | flush_scheduled_work(); |
Andy Walls | 43c2407 | 2010-06-27 23:15:35 -0300 | [diff] [blame^] | 215 | } |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 216 | |
Andy Walls | 43c2407 | 2010-06-27 23:15:35 -0300 | [diff] [blame^] | 217 | static void cx23885_input_ir_close(void *priv) |
| 218 | { |
| 219 | struct cx23885_kernel_ir *kernel_ir = priv; |
| 220 | |
| 221 | if (kernel_ir->cx != NULL) |
| 222 | cx23885_input_ir_stop(kernel_ir->cx); |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | int cx23885_input_init(struct cx23885_dev *dev) |
| 226 | { |
Andy Walls | 43c2407 | 2010-06-27 23:15:35 -0300 | [diff] [blame^] | 227 | struct cx23885_kernel_ir *kernel_ir; |
| 228 | struct input_dev *inp_dev; |
| 229 | struct ir_dev_props *props; |
| 230 | |
| 231 | char *rc_map; |
| 232 | enum rc_driver_type driver_type; |
| 233 | unsigned long allowed_protos; |
| 234 | |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 235 | int ret; |
| 236 | |
| 237 | /* |
| 238 | * If the IR device (hardware registers, chip, GPIO lines, etc.) isn't |
| 239 | * encapsulated in a v4l2_subdev, then I'm not going to deal with it. |
| 240 | */ |
| 241 | if (dev->sd_ir == NULL) |
| 242 | return -ENODEV; |
| 243 | |
| 244 | switch (dev->board) { |
| 245 | case CX23885_BOARD_HAUPPAUGE_HVR1850: |
Michael Krufky | 7fec6fe | 2009-11-11 15:46:09 -0300 | [diff] [blame] | 246 | case CX23885_BOARD_HAUPPAUGE_HVR1290: |
Andy Walls | 43c2407 | 2010-06-27 23:15:35 -0300 | [diff] [blame^] | 247 | /* Integrated CX23888 IR controller */ |
| 248 | driver_type = RC_DRIVER_IR_RAW; |
| 249 | allowed_protos = IR_TYPE_ALL; |
| 250 | /* The grey Hauppauge RC-5 remote */ |
| 251 | rc_map = RC_MAP_RC5_HAUPPAUGE_NEW; |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 252 | break; |
Andy Walls | 43c2407 | 2010-06-27 23:15:35 -0300 | [diff] [blame^] | 253 | default: |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 254 | return -ENODEV; |
Andy Walls | 43c2407 | 2010-06-27 23:15:35 -0300 | [diff] [blame^] | 255 | } |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 256 | |
Andy Walls | 43c2407 | 2010-06-27 23:15:35 -0300 | [diff] [blame^] | 257 | /* cx23885 board instance kernel IR state */ |
| 258 | kernel_ir = kzalloc(sizeof(struct cx23885_kernel_ir), GFP_KERNEL); |
| 259 | if (kernel_ir == NULL) |
| 260 | return -ENOMEM; |
| 261 | |
| 262 | kernel_ir->cx = dev; |
| 263 | kernel_ir->name = kasprintf(GFP_KERNEL, "cx23885 IR (%s)", |
| 264 | cx23885_boards[dev->board].name); |
| 265 | kernel_ir->phys = kasprintf(GFP_KERNEL, "pci-%s/ir0", |
| 266 | pci_name(dev->pci)); |
| 267 | |
| 268 | /* input device */ |
| 269 | inp_dev = input_allocate_device(); |
| 270 | if (inp_dev == NULL) { |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 271 | ret = -ENOMEM; |
| 272 | goto err_out_free; |
| 273 | } |
| 274 | |
Andy Walls | 43c2407 | 2010-06-27 23:15:35 -0300 | [diff] [blame^] | 275 | kernel_ir->inp_dev = inp_dev; |
| 276 | inp_dev->name = kernel_ir->name; |
| 277 | inp_dev->phys = kernel_ir->phys; |
| 278 | inp_dev->id.bustype = BUS_PCI; |
| 279 | inp_dev->id.version = 1; |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 280 | if (dev->pci->subsystem_vendor) { |
Andy Walls | 43c2407 | 2010-06-27 23:15:35 -0300 | [diff] [blame^] | 281 | inp_dev->id.vendor = dev->pci->subsystem_vendor; |
| 282 | inp_dev->id.product = dev->pci->subsystem_device; |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 283 | } else { |
Andy Walls | 43c2407 | 2010-06-27 23:15:35 -0300 | [diff] [blame^] | 284 | inp_dev->id.vendor = dev->pci->vendor; |
| 285 | inp_dev->id.product = dev->pci->device; |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 286 | } |
Andy Walls | 43c2407 | 2010-06-27 23:15:35 -0300 | [diff] [blame^] | 287 | inp_dev->dev.parent = &dev->pci->dev; |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 288 | |
Andy Walls | 43c2407 | 2010-06-27 23:15:35 -0300 | [diff] [blame^] | 289 | /* kernel ir device properties */ |
| 290 | props = &kernel_ir->props; |
| 291 | props->driver_type = driver_type; |
| 292 | props->allowed_protos = allowed_protos; |
| 293 | props->priv = kernel_ir; |
| 294 | props->open = cx23885_input_ir_open; |
| 295 | props->close = cx23885_input_ir_close; |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 296 | |
Andy Walls | 43c2407 | 2010-06-27 23:15:35 -0300 | [diff] [blame^] | 297 | /* Go */ |
| 298 | dev->kernel_ir = kernel_ir; |
| 299 | ret = ir_input_register(inp_dev, rc_map, props, MODULE_NAME); |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 300 | if (ret) |
| 301 | goto err_out_stop; |
| 302 | |
| 303 | return 0; |
| 304 | |
| 305 | err_out_stop: |
| 306 | cx23885_input_ir_stop(dev); |
Andy Walls | 43c2407 | 2010-06-27 23:15:35 -0300 | [diff] [blame^] | 307 | dev->kernel_ir = NULL; |
| 308 | /* TODO: double check clean-up of kernel_ir->inp_dev */ |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 309 | err_out_free: |
Andy Walls | 43c2407 | 2010-06-27 23:15:35 -0300 | [diff] [blame^] | 310 | kfree(kernel_ir->phys); |
| 311 | kfree(kernel_ir->name); |
| 312 | kfree(kernel_ir); |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | void cx23885_input_fini(struct cx23885_dev *dev) |
| 317 | { |
| 318 | /* Always stop the IR hardware from generating interrupts */ |
| 319 | cx23885_input_ir_stop(dev); |
| 320 | |
Andy Walls | 43c2407 | 2010-06-27 23:15:35 -0300 | [diff] [blame^] | 321 | if (dev->kernel_ir == NULL) |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 322 | return; |
Andy Walls | 43c2407 | 2010-06-27 23:15:35 -0300 | [diff] [blame^] | 323 | ir_input_unregister(dev->kernel_ir->inp_dev); |
| 324 | kfree(dev->kernel_ir->phys); |
| 325 | kfree(dev->kernel_ir->name); |
| 326 | kfree(dev->kernel_ir); |
| 327 | dev->kernel_ir = NULL; |
Andy Walls | dbda8f7 | 2009-09-27 20:55:41 -0300 | [diff] [blame] | 328 | } |