Path: blob/master/drivers/media/video/cx23885/cx23885-input.c
17726 views
/*1* Driver for the Conexant CX23885/7/8 PCIe bridge2*3* Infrared remote control input device4*5* Most of this file is6*7* Copyright (C) 2009 Andy Walls <[email protected]>8*9* However, the cx23885_input_{init,fini} functions contained herein are10* derived from Linux kernel files linux/media/video/.../...-input.c marked as:11*12* Copyright (C) 2008 <srinivasa.deevi at conexant dot com>13* Copyright (C) 2005 Ludovico Cavedon <[email protected]>14* Markus Rechberger <[email protected]>15* Mauro Carvalho Chehab <[email protected]>16* Sascha Sommer <[email protected]>17* Copyright (C) 2004, 2005 Chris Pascoe18* Copyright (C) 2003, 2004 Gerd Knorr19* Copyright (C) 2003 Pavel Machek20*21* This program is free software; you can redistribute it and/or22* modify it under the terms of the GNU General Public License23* as published by the Free Software Foundation; either version 224* of the License, or (at your option) any later version.25*26* This program is distributed in the hope that it will be useful,27* but WITHOUT ANY WARRANTY; without even the implied warranty of28* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the29* GNU General Public License for more details.30*31* You should have received a copy of the GNU General Public License32* along with this program; if not, write to the Free Software33* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA34* 02110-1301, USA.35*/3637#include <linux/slab.h>38#include <media/rc-core.h>39#include <media/v4l2-subdev.h>4041#include "cx23885.h"4243#define MODULE_NAME "cx23885"4445static void cx23885_input_process_measurements(struct cx23885_dev *dev,46bool overrun)47{48struct cx23885_kernel_ir *kernel_ir = dev->kernel_ir;4950ssize_t num;51int count, i;52bool handle = false;53struct ir_raw_event ir_core_event[64];5455do {56num = 0;57v4l2_subdev_call(dev->sd_ir, ir, rx_read, (u8 *) ir_core_event,58sizeof(ir_core_event), &num);5960count = num / sizeof(struct ir_raw_event);6162for (i = 0; i < count; i++) {63ir_raw_event_store(kernel_ir->rc,64&ir_core_event[i]);65handle = true;66}67} while (num != 0);6869if (overrun)70ir_raw_event_reset(kernel_ir->rc);71else if (handle)72ir_raw_event_handle(kernel_ir->rc);73}7475void cx23885_input_rx_work_handler(struct cx23885_dev *dev, u32 events)76{77struct v4l2_subdev_ir_parameters params;78int overrun, data_available;7980if (dev->sd_ir == NULL || events == 0)81return;8283switch (dev->board) {84case CX23885_BOARD_HAUPPAUGE_HVR1850:85case CX23885_BOARD_HAUPPAUGE_HVR1290:86case CX23885_BOARD_TEVII_S470:87case CX23885_BOARD_HAUPPAUGE_HVR1250:88/*89* The only boards we handle right now. However other boards90* using the CX2388x integrated IR controller should be similar91*/92break;93default:94return;95}9697overrun = events & (V4L2_SUBDEV_IR_RX_SW_FIFO_OVERRUN |98V4L2_SUBDEV_IR_RX_HW_FIFO_OVERRUN);99100data_available = events & (V4L2_SUBDEV_IR_RX_END_OF_RX_DETECTED |101V4L2_SUBDEV_IR_RX_FIFO_SERVICE_REQ);102103if (overrun) {104/* If there was a FIFO overrun, stop the device */105v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, ¶ms);106params.enable = false;107/* Mitigate race with cx23885_input_ir_stop() */108params.shutdown = atomic_read(&dev->ir_input_stopping);109v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, ¶ms);110}111112if (data_available)113cx23885_input_process_measurements(dev, overrun);114115if (overrun) {116/* If there was a FIFO overrun, clear & restart the device */117params.enable = true;118/* Mitigate race with cx23885_input_ir_stop() */119params.shutdown = atomic_read(&dev->ir_input_stopping);120v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, ¶ms);121}122}123124static int cx23885_input_ir_start(struct cx23885_dev *dev)125{126struct v4l2_subdev_ir_parameters params;127128if (dev->sd_ir == NULL)129return -ENODEV;130131atomic_set(&dev->ir_input_stopping, 0);132133v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, ¶ms);134switch (dev->board) {135case CX23885_BOARD_HAUPPAUGE_HVR1850:136case CX23885_BOARD_HAUPPAUGE_HVR1290:137case CX23885_BOARD_HAUPPAUGE_HVR1250:138/*139* The IR controller on this board only returns pulse widths.140* Any other mode setting will fail to set up the device.141*/142params.mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;143params.enable = true;144params.interrupt_enable = true;145params.shutdown = false;146147/* Setup for baseband compatible with both RC-5 and RC-6A */148params.modulation = false;149/* RC-5: 2,222,222 ns = 1/36 kHz * 32 cycles * 2 marks * 1.25*/150/* RC-6A: 3,333,333 ns = 1/36 kHz * 16 cycles * 6 marks * 1.25*/151params.max_pulse_width = 3333333; /* ns */152/* RC-5: 666,667 ns = 1/36 kHz * 32 cycles * 1 mark * 0.75 */153/* RC-6A: 333,333 ns = 1/36 kHz * 16 cycles * 1 mark * 0.75 */154params.noise_filter_min_width = 333333; /* ns */155/*156* This board has inverted receive sense:157* mark is received as low logic level;158* falling edges are detected as rising edges; etc.159*/160params.invert_level = true;161break;162case CX23885_BOARD_TEVII_S470:163/*164* The IR controller on this board only returns pulse widths.165* Any other mode setting will fail to set up the device.166*/167params.mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;168params.enable = true;169params.interrupt_enable = true;170params.shutdown = false;171172/* Setup for a standard NEC protocol */173params.carrier_freq = 37917; /* Hz, 455 kHz/12 for NEC */174params.carrier_range_lower = 33000; /* Hz */175params.carrier_range_upper = 43000; /* Hz */176params.duty_cycle = 33; /* percent, 33 percent for NEC */177178/*179* NEC max pulse width: (64/3)/(455 kHz/12) * 16 nec_units180* (64/3)/(455 kHz/12) * 16 nec_units * 1.375 = 12378022 ns181*/182params.max_pulse_width = 12378022; /* ns */183184/*185* NEC noise filter min width: (64/3)/(455 kHz/12) * 1 nec_unit186* (64/3)/(455 kHz/12) * 1 nec_units * 0.625 = 351648 ns187*/188params.noise_filter_min_width = 351648; /* ns */189190params.modulation = false;191params.invert_level = true;192break;193}194v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, ¶ms);195return 0;196}197198static int cx23885_input_ir_open(struct rc_dev *rc)199{200struct cx23885_kernel_ir *kernel_ir = rc->priv;201202if (kernel_ir->cx == NULL)203return -ENODEV;204205return cx23885_input_ir_start(kernel_ir->cx);206}207208static void cx23885_input_ir_stop(struct cx23885_dev *dev)209{210struct v4l2_subdev_ir_parameters params;211212if (dev->sd_ir == NULL)213return;214215/*216* Stop the sd_ir subdevice from generating notifications and217* scheduling work.218* It is shutdown this way in order to mitigate a race with219* cx23885_input_rx_work_handler() in the overrun case, which could220* re-enable the subdevice.221*/222atomic_set(&dev->ir_input_stopping, 1);223v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, ¶ms);224while (params.shutdown == false) {225params.enable = false;226params.interrupt_enable = false;227params.shutdown = true;228v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, ¶ms);229v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, ¶ms);230}231}232233static void cx23885_input_ir_close(struct rc_dev *rc)234{235struct cx23885_kernel_ir *kernel_ir = rc->priv;236237if (kernel_ir->cx != NULL)238cx23885_input_ir_stop(kernel_ir->cx);239}240241int cx23885_input_init(struct cx23885_dev *dev)242{243struct cx23885_kernel_ir *kernel_ir;244struct rc_dev *rc;245char *rc_map;246enum rc_driver_type driver_type;247unsigned long allowed_protos;248249int ret;250251/*252* If the IR device (hardware registers, chip, GPIO lines, etc.) isn't253* encapsulated in a v4l2_subdev, then I'm not going to deal with it.254*/255if (dev->sd_ir == NULL)256return -ENODEV;257258switch (dev->board) {259case CX23885_BOARD_HAUPPAUGE_HVR1850:260case CX23885_BOARD_HAUPPAUGE_HVR1290:261case CX23885_BOARD_HAUPPAUGE_HVR1250:262/* Integrated CX2388[58] IR controller */263driver_type = RC_DRIVER_IR_RAW;264allowed_protos = RC_TYPE_ALL;265/* The grey Hauppauge RC-5 remote */266rc_map = RC_MAP_HAUPPAUGE;267break;268case CX23885_BOARD_TEVII_S470:269/* Integrated CX23885 IR controller */270driver_type = RC_DRIVER_IR_RAW;271allowed_protos = RC_TYPE_ALL;272/* A guess at the remote */273rc_map = RC_MAP_TEVII_NEC;274break;275default:276return -ENODEV;277}278279/* cx23885 board instance kernel IR state */280kernel_ir = kzalloc(sizeof(struct cx23885_kernel_ir), GFP_KERNEL);281if (kernel_ir == NULL)282return -ENOMEM;283284kernel_ir->cx = dev;285kernel_ir->name = kasprintf(GFP_KERNEL, "cx23885 IR (%s)",286cx23885_boards[dev->board].name);287kernel_ir->phys = kasprintf(GFP_KERNEL, "pci-%s/ir0",288pci_name(dev->pci));289290/* input device */291rc = rc_allocate_device();292if (!rc) {293ret = -ENOMEM;294goto err_out_free;295}296297kernel_ir->rc = rc;298rc->input_name = kernel_ir->name;299rc->input_phys = kernel_ir->phys;300rc->input_id.bustype = BUS_PCI;301rc->input_id.version = 1;302if (dev->pci->subsystem_vendor) {303rc->input_id.vendor = dev->pci->subsystem_vendor;304rc->input_id.product = dev->pci->subsystem_device;305} else {306rc->input_id.vendor = dev->pci->vendor;307rc->input_id.product = dev->pci->device;308}309rc->dev.parent = &dev->pci->dev;310rc->driver_type = driver_type;311rc->allowed_protos = allowed_protos;312rc->priv = kernel_ir;313rc->open = cx23885_input_ir_open;314rc->close = cx23885_input_ir_close;315rc->map_name = rc_map;316rc->driver_name = MODULE_NAME;317318/* Go */319dev->kernel_ir = kernel_ir;320ret = rc_register_device(rc);321if (ret)322goto err_out_stop;323324return 0;325326err_out_stop:327cx23885_input_ir_stop(dev);328dev->kernel_ir = NULL;329rc_free_device(rc);330err_out_free:331kfree(kernel_ir->phys);332kfree(kernel_ir->name);333kfree(kernel_ir);334return ret;335}336337void cx23885_input_fini(struct cx23885_dev *dev)338{339/* Always stop the IR hardware from generating interrupts */340cx23885_input_ir_stop(dev);341342if (dev->kernel_ir == NULL)343return;344rc_unregister_device(dev->kernel_ir->rc);345kfree(dev->kernel_ir->phys);346kfree(dev->kernel_ir->name);347kfree(dev->kernel_ir);348dev->kernel_ir = NULL;349}350351352