/*1* Remote Controller core header2*3* Copyright (C) 2009-2010 by Mauro Carvalho Chehab <[email protected]>4*5* This program is free software; you can redistribute it and/or modify6* it under the terms of the GNU General Public License as published by7* the Free Software Foundation version 2 of the License.8*9* This program is distributed in the hope that it will be useful,10* but WITHOUT ANY WARRANTY; without even the implied warranty of11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12* GNU General Public License for more details.13*/1415#ifndef _RC_CORE16#define _RC_CORE1718#include <linux/spinlock.h>19#include <linux/kfifo.h>20#include <linux/time.h>21#include <linux/timer.h>22#include <media/rc-map.h>2324extern int rc_core_debug;25#define IR_dprintk(level, fmt, arg...) if (rc_core_debug >= level) \26printk(KERN_DEBUG "%s: " fmt , __func__, ## arg)2728enum rc_driver_type {29RC_DRIVER_SCANCODE = 0, /* Driver or hardware generates a scancode */30RC_DRIVER_IR_RAW, /* Needs a Infra-Red pulse/space decoder */31};3233/**34* struct rc_dev - represents a remote control device35* @dev: driver model's view of this device36* @input_name: name of the input child device37* @input_phys: physical path to the input child device38* @input_id: id of the input child device (struct input_id)39* @driver_name: name of the hardware driver which registered this device40* @map_name: name of the default keymap41* @rc_map: current scan/key table42* @lock: used to ensure we've filled in all protocol details before43* anyone can call show_protocols or store_protocols44* @devno: unique remote control device number45* @raw: additional data for raw pulse/space devices46* @input_dev: the input child device used to communicate events to userspace47* @driver_type: specifies if protocol decoding is done in hardware or software48* @idle: used to keep track of RX state49* @allowed_protos: bitmask with the supported RC_TYPE_* protocols50* @scanmask: some hardware decoders are not capable of providing the full51* scancode to the application. As this is a hardware limit, we can't do52* anything with it. Yet, as the same keycode table can be used with other53* devices, a mask is provided to allow its usage. Drivers should generally54* leave this field in blank55* @priv: driver-specific data56* @keylock: protects the remaining members of the struct57* @keypressed: whether a key is currently pressed58* @keyup_jiffies: time (in jiffies) when the current keypress should be released59* @timer_keyup: timer for releasing a keypress60* @last_keycode: keycode of last keypress61* @last_scancode: scancode of last keypress62* @last_toggle: toggle value of last command63* @timeout: optional time after which device stops sending data64* @min_timeout: minimum timeout supported by device65* @max_timeout: maximum timeout supported by device66* @rx_resolution : resolution (in ns) of input sampler67* @tx_resolution: resolution (in ns) of output sampler68* @change_protocol: allow changing the protocol used on hardware decoders69* @open: callback to allow drivers to enable polling/irq when IR input device70* is opened.71* @close: callback to allow drivers to disable polling/irq when IR input device72* is opened.73* @s_tx_mask: set transmitter mask (for devices with multiple tx outputs)74* @s_tx_carrier: set transmit carrier frequency75* @s_tx_duty_cycle: set transmit duty cycle (0% - 100%)76* @s_rx_carrier: inform driver about carrier it is expected to handle77* @tx_ir: transmit IR78* @s_idle: enable/disable hardware idle mode, upon which,79* device doesn't interrupt host until it sees IR pulses80* @s_learning_mode: enable wide band receiver used for learning81* @s_carrier_report: enable carrier reports82*/83struct rc_dev {84struct device dev;85const char *input_name;86const char *input_phys;87struct input_id input_id;88char *driver_name;89const char *map_name;90struct rc_map rc_map;91struct mutex lock;92unsigned long devno;93struct ir_raw_event_ctrl *raw;94struct input_dev *input_dev;95enum rc_driver_type driver_type;96bool idle;97u64 allowed_protos;98u32 scanmask;99void *priv;100spinlock_t keylock;101bool keypressed;102unsigned long keyup_jiffies;103struct timer_list timer_keyup;104u32 last_keycode;105u32 last_scancode;106u8 last_toggle;107u32 timeout;108u32 min_timeout;109u32 max_timeout;110u32 rx_resolution;111u32 tx_resolution;112int (*change_protocol)(struct rc_dev *dev, u64 rc_type);113int (*open)(struct rc_dev *dev);114void (*close)(struct rc_dev *dev);115int (*s_tx_mask)(struct rc_dev *dev, u32 mask);116int (*s_tx_carrier)(struct rc_dev *dev, u32 carrier);117int (*s_tx_duty_cycle)(struct rc_dev *dev, u32 duty_cycle);118int (*s_rx_carrier_range)(struct rc_dev *dev, u32 min, u32 max);119int (*tx_ir)(struct rc_dev *dev, int *txbuf, u32 n);120void (*s_idle)(struct rc_dev *dev, bool enable);121int (*s_learning_mode)(struct rc_dev *dev, int enable);122int (*s_carrier_report) (struct rc_dev *dev, int enable);123};124125#define to_rc_dev(d) container_of(d, struct rc_dev, dev)126127/*128* From rc-main.c129* Those functions can be used on any type of Remote Controller. They130* basically creates an input_dev and properly reports the device as a131* Remote Controller, at sys/class/rc.132*/133134struct rc_dev *rc_allocate_device(void);135void rc_free_device(struct rc_dev *dev);136int rc_register_device(struct rc_dev *dev);137void rc_unregister_device(struct rc_dev *dev);138139void rc_repeat(struct rc_dev *dev);140void rc_keydown(struct rc_dev *dev, int scancode, u8 toggle);141void rc_keydown_notimeout(struct rc_dev *dev, int scancode, u8 toggle);142void rc_keyup(struct rc_dev *dev);143u32 rc_g_keycode_from_table(struct rc_dev *dev, u32 scancode);144145/*146* From rc-raw.c147* The Raw interface is specific to InfraRed. It may be a good idea to148* split it later into a separate header.149*/150151enum raw_event_type {152IR_SPACE = (1 << 0),153IR_PULSE = (1 << 1),154IR_START_EVENT = (1 << 2),155IR_STOP_EVENT = (1 << 3),156};157158struct ir_raw_event {159union {160u32 duration;161162struct {163u32 carrier;164u8 duty_cycle;165};166};167168unsigned pulse:1;169unsigned reset:1;170unsigned timeout:1;171unsigned carrier_report:1;172};173174#define DEFINE_IR_RAW_EVENT(event) \175struct ir_raw_event event = { \176{ .duration = 0 } , \177.pulse = 0, \178.reset = 0, \179.timeout = 0, \180.carrier_report = 0 }181182static inline void init_ir_raw_event(struct ir_raw_event *ev)183{184memset(ev, 0, sizeof(*ev));185}186187#define IR_MAX_DURATION 0xFFFFFFFF /* a bit more than 4 seconds */188#define US_TO_NS(usec) ((usec) * 1000)189#define MS_TO_US(msec) ((msec) * 1000)190#define MS_TO_NS(msec) ((msec) * 1000 * 1000)191192void ir_raw_event_handle(struct rc_dev *dev);193int ir_raw_event_store(struct rc_dev *dev, struct ir_raw_event *ev);194int ir_raw_event_store_edge(struct rc_dev *dev, enum raw_event_type type);195int ir_raw_event_store_with_filter(struct rc_dev *dev,196struct ir_raw_event *ev);197void ir_raw_event_set_idle(struct rc_dev *dev, bool idle);198199static inline void ir_raw_event_reset(struct rc_dev *dev)200{201DEFINE_IR_RAW_EVENT(ev);202ev.reset = true;203204ir_raw_event_store(dev, &ev);205ir_raw_event_handle(dev);206}207208/* extract mask bits out of data and pack them into the result */209static inline u32 ir_extract_bits(u32 data, u32 mask)210{211u32 vbit = 1, value = 0;212213do {214if (mask & 1) {215if (data & 1)216value |= vbit;217vbit <<= 1;218}219data >>= 1;220} while (mask >>= 1);221222return value;223}224225#endif /* _RC_CORE */226227228