/* SPDX-License-Identifier: GPL-2.0-only */1/*2* Remote Controller core header3*4* Copyright (C) 2009-2010 by Mauro Carvalho Chehab5*/67#ifndef _RC_CORE8#define _RC_CORE910#include <linux/spinlock.h>11#include <linux/cdev.h>12#include <linux/kfifo.h>13#include <linux/time.h>14#include <linux/timer.h>15#include <media/rc-map.h>1617/**18* enum rc_driver_type - type of the RC driver.19*20* @RC_DRIVER_SCANCODE: Driver or hardware generates a scancode.21* @RC_DRIVER_IR_RAW: Driver or hardware generates pulse/space sequences.22* It needs a Infra-Red pulse/space decoder23* @RC_DRIVER_IR_RAW_TX: Device transmitter only,24* driver requires pulse/space data sequence.25*/26enum rc_driver_type {27RC_DRIVER_SCANCODE = 0,28RC_DRIVER_IR_RAW,29RC_DRIVER_IR_RAW_TX,30};3132/**33* struct rc_scancode_filter - Filter scan codes.34* @data: Scancode data to match.35* @mask: Mask of bits of scancode to compare.36*/37struct rc_scancode_filter {38u32 data;39u32 mask;40};4142/**43* enum rc_filter_type - Filter type constants.44* @RC_FILTER_NORMAL: Filter for normal operation.45* @RC_FILTER_WAKEUP: Filter for waking from suspend.46* @RC_FILTER_MAX: Number of filter types.47*/48enum rc_filter_type {49RC_FILTER_NORMAL = 0,50RC_FILTER_WAKEUP,5152RC_FILTER_MAX53};5455/**56* struct lirc_fh - represents an open lirc file57* @list: list of open file handles58* @rc: rcdev for this lirc chardev59* @rawir: queue for incoming raw IR60* @scancodes: queue for incoming decoded scancodes61* @wait_poll: poll struct for lirc device62* @carrier_low: when setting the carrier range, first the low end must be63* set with an ioctl and then the high end with another ioctl64* @send_mode: lirc mode for sending, either LIRC_MODE_SCANCODE or65* LIRC_MODE_PULSE66* @rec_mode: lirc mode for receiving, either LIRC_MODE_SCANCODE or67* LIRC_MODE_MODE268*/69struct lirc_fh {70struct list_head list;71struct rc_dev *rc;72DECLARE_KFIFO_PTR(rawir, unsigned int);73DECLARE_KFIFO_PTR(scancodes, struct lirc_scancode);74wait_queue_head_t wait_poll;75u32 carrier_low;76u8 send_mode;77u8 rec_mode;78};7980/**81* struct rc_dev - represents a remote control device82* @dev: driver model's view of this device83* @managed_alloc: devm_rc_allocate_device was used to create rc_dev84* @registered: set to true by rc_register_device(), false by85* rc_unregister_device86* @idle: used to keep track of RX state87* @encode_wakeup: wakeup filtering uses IR encode API, therefore the allowed88* wakeup protocols is the set of all raw encoders89* @minor: unique minor remote control device number90* @sysfs_groups: sysfs attribute groups91* @device_name: name of the rc child device92* @input_phys: physical path to the input child device93* @input_id: id of the input child device (struct input_id)94* @driver_name: name of the hardware driver which registered this device95* @map_name: name of the default keymap96* @rc_map: current scan/key table97* @lock: used to ensure we've filled in all protocol details before98* anyone can call show_protocols or store_protocols99* @raw: additional data for raw pulse/space devices100* @input_dev: the input child device used to communicate events to userspace101* @driver_type: specifies if protocol decoding is done in hardware or software102* @users: number of current users of the device103* @allowed_protocols: bitmask with the supported RC_PROTO_BIT_* protocols104* @enabled_protocols: bitmask with the enabled RC_PROTO_BIT_* protocols105* @allowed_wakeup_protocols: bitmask with the supported RC_PROTO_BIT_* wakeup106* protocols107* @wakeup_protocol: the enabled RC_PROTO_* wakeup protocol or108* RC_PROTO_UNKNOWN if disabled.109* @scancode_filter: scancode filter110* @scancode_wakeup_filter: scancode wakeup filters111* @scancode_mask: some hardware decoders are not capable of providing the full112* scancode to the application. As this is a hardware limit, we can't do113* anything with it. Yet, as the same keycode table can be used with other114* devices, a mask is provided to allow its usage. Drivers should generally115* leave this field in blank116* @priv: driver-specific data117* @keylock: protects the remaining members of the struct118* @keypressed: whether a key is currently pressed119* @last_toggle: toggle value of last command120* @last_keycode: keycode of last keypress121* @last_protocol: protocol of last keypress122* @last_scancode: scancode of last keypress123* @keyup_jiffies: time (in jiffies) when the current keypress should be released124* @timer_keyup: timer for releasing a keypress125* @timer_repeat: timer for autorepeat events. This is needed for CEC, which126* has non-standard repeats.127* @timeout: optional time after which device stops sending data128* @min_timeout: minimum timeout supported by device129* @max_timeout: maximum timeout supported by device130* @rx_resolution : resolution (in us) of input sampler131* @lirc_dev: lirc device132* @lirc_cdev: lirc char cdev133* @gap_start: start time for gap after timeout if non-zero134* @lirc_fh_lock: protects lirc_fh list135* @lirc_fh: list of open files136* @change_protocol: allow changing the protocol used on hardware decoders137* @open: callback to allow drivers to enable polling/irq when IR input device138* is opened.139* @close: callback to allow drivers to disable polling/irq when IR input device140* is opened.141* @s_tx_mask: set transmitter mask (for devices with multiple tx outputs)142* @s_tx_carrier: set transmit carrier frequency143* @s_tx_duty_cycle: set transmit duty cycle (0% - 100%)144* @s_rx_carrier_range: inform driver about carrier it is expected to handle145* @tx_ir: transmit IR146* @s_idle: enable/disable hardware idle mode, upon which,147* device doesn't interrupt host until it sees IR pulses148* @s_wideband_receiver: enable wide band receiver used for learning149* @s_carrier_report: enable carrier reports150* @s_filter: set the scancode filter151* @s_wakeup_filter: set the wakeup scancode filter. If the mask is zero152* then wakeup should be disabled. wakeup_protocol will be set to153* a valid protocol if mask is nonzero.154* @s_timeout: set hardware timeout in us155*/156struct rc_dev {157struct device dev;158bool managed_alloc;159bool registered;160bool idle;161bool encode_wakeup;162unsigned int minor;163const struct attribute_group *sysfs_groups[5];164const char *device_name;165const char *input_phys;166struct input_id input_id;167const char *driver_name;168const char *map_name;169struct rc_map rc_map;170struct mutex lock;171struct ir_raw_event_ctrl *raw;172struct input_dev *input_dev;173enum rc_driver_type driver_type;174u32 users;175u64 allowed_protocols;176u64 enabled_protocols;177u64 allowed_wakeup_protocols;178enum rc_proto wakeup_protocol;179struct rc_scancode_filter scancode_filter;180struct rc_scancode_filter scancode_wakeup_filter;181u32 scancode_mask;182void *priv;183spinlock_t keylock;184bool keypressed;185u8 last_toggle;186u32 last_keycode;187enum rc_proto last_protocol;188u64 last_scancode;189unsigned long keyup_jiffies;190struct timer_list timer_keyup;191struct timer_list timer_repeat;192u32 timeout;193u32 min_timeout;194u32 max_timeout;195u32 rx_resolution;196#ifdef CONFIG_LIRC197struct device lirc_dev;198struct cdev lirc_cdev;199ktime_t gap_start;200spinlock_t lirc_fh_lock;201struct list_head lirc_fh;202#endif203int (*change_protocol)(struct rc_dev *dev, u64 *rc_proto);204int (*open)(struct rc_dev *dev);205void (*close)(struct rc_dev *dev);206int (*s_tx_mask)(struct rc_dev *dev, u32 mask);207int (*s_tx_carrier)(struct rc_dev *dev, u32 carrier);208int (*s_tx_duty_cycle)(struct rc_dev *dev, u32 duty_cycle);209int (*s_rx_carrier_range)(struct rc_dev *dev, u32 min, u32 max);210int (*tx_ir)(struct rc_dev *dev, unsigned *txbuf, unsigned n);211void (*s_idle)(struct rc_dev *dev, bool enable);212int (*s_wideband_receiver)(struct rc_dev *dev, int enable);213int (*s_carrier_report) (struct rc_dev *dev, int enable);214int (*s_filter)(struct rc_dev *dev,215struct rc_scancode_filter *filter);216int (*s_wakeup_filter)(struct rc_dev *dev,217struct rc_scancode_filter *filter);218int (*s_timeout)(struct rc_dev *dev,219unsigned int timeout);220};221222#define to_rc_dev(d) container_of(d, struct rc_dev, dev)223224/*225* From rc-main.c226* Those functions can be used on any type of Remote Controller. They227* basically creates an input_dev and properly reports the device as a228* Remote Controller, at sys/class/rc.229*/230231/**232* rc_allocate_device - Allocates a RC device233*234* @rc_driver_type: specifies the type of the RC output to be allocated235* returns a pointer to struct rc_dev.236*/237struct rc_dev *rc_allocate_device(enum rc_driver_type);238239/**240* devm_rc_allocate_device - Managed RC device allocation241*242* @dev: pointer to struct device243* @rc_driver_type: specifies the type of the RC output to be allocated244* returns a pointer to struct rc_dev.245*/246struct rc_dev *devm_rc_allocate_device(struct device *dev, enum rc_driver_type);247248/**249* rc_free_device - Frees a RC device250*251* @dev: pointer to struct rc_dev.252*/253void rc_free_device(struct rc_dev *dev);254255/**256* rc_register_device - Registers a RC device257*258* @dev: pointer to struct rc_dev.259*/260int rc_register_device(struct rc_dev *dev);261262/**263* devm_rc_register_device - Manageded registering of a RC device264*265* @parent: pointer to struct device.266* @dev: pointer to struct rc_dev.267*/268int devm_rc_register_device(struct device *parent, struct rc_dev *dev);269270/**271* rc_unregister_device - Unregisters a RC device272*273* @dev: pointer to struct rc_dev.274*/275void rc_unregister_device(struct rc_dev *dev);276277void rc_repeat(struct rc_dev *dev);278void rc_keydown(struct rc_dev *dev, enum rc_proto protocol, u64 scancode,279u8 toggle);280void rc_keydown_notimeout(struct rc_dev *dev, enum rc_proto protocol,281u64 scancode, u8 toggle);282void rc_keyup(struct rc_dev *dev);283u32 rc_g_keycode_from_table(struct rc_dev *dev, u64 scancode);284285/*286* From rc-raw.c287* The Raw interface is specific to InfraRed. It may be a good idea to288* split it later into a separate header.289*/290struct ir_raw_event {291union {292u32 duration;293u32 carrier;294};295u8 duty_cycle;296297unsigned pulse:1;298unsigned overflow:1;299unsigned timeout:1;300unsigned carrier_report:1;301};302303#define US_TO_NS(usec) ((usec) * 1000)304#define MS_TO_US(msec) ((msec) * 1000)305#define IR_MAX_DURATION MS_TO_US(500)306#define IR_DEFAULT_TIMEOUT MS_TO_US(125)307#define IR_MAX_TIMEOUT LIRC_VALUE_MASK308309void ir_raw_event_handle(struct rc_dev *dev);310int ir_raw_event_store(struct rc_dev *dev, struct ir_raw_event *ev);311int ir_raw_event_store_edge(struct rc_dev *dev, bool pulse);312int ir_raw_event_store_with_filter(struct rc_dev *dev,313struct ir_raw_event *ev);314int ir_raw_event_store_with_timeout(struct rc_dev *dev,315struct ir_raw_event *ev);316void ir_raw_event_set_idle(struct rc_dev *dev, bool idle);317int ir_raw_encode_scancode(enum rc_proto protocol, u32 scancode,318struct ir_raw_event *events, unsigned int max);319int ir_raw_encode_carrier(enum rc_proto protocol);320321static inline void ir_raw_event_overflow(struct rc_dev *dev)322{323ir_raw_event_store(dev, &((struct ir_raw_event) { .overflow = true }));324dev->idle = true;325ir_raw_event_handle(dev);326}327328/* extract mask bits out of data and pack them into the result */329static inline u32 ir_extract_bits(u32 data, u32 mask)330{331u32 vbit = 1, value = 0;332333do {334if (mask & 1) {335if (data & 1)336value |= vbit;337vbit <<= 1;338}339data >>= 1;340} while (mask >>= 1);341342return value;343}344345/* Get NEC scancode and protocol type from address and command bytes */346static inline u32 ir_nec_bytes_to_scancode(u8 address, u8 not_address,347u8 command, u8 not_command,348enum rc_proto *protocol)349{350u32 scancode;351352if ((command ^ not_command) != 0xff) {353/* NEC transport, but modified protocol, used by at354* least Apple and TiVo remotes355*/356scancode = not_address << 24 |357address << 16 |358not_command << 8 |359command;360*protocol = RC_PROTO_NEC32;361} else if ((address ^ not_address) != 0xff) {362/* Extended NEC */363scancode = address << 16 |364not_address << 8 |365command;366*protocol = RC_PROTO_NECX;367} else {368/* Normal NEC */369scancode = address << 8 | command;370*protocol = RC_PROTO_NEC;371}372373return scancode;374}375376#endif /* _RC_CORE */377378379