Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * Remote Controller core header
0004  *
0005  * Copyright (C) 2009-2010 by Mauro Carvalho Chehab
0006  */
0007 
0008 #ifndef _RC_CORE
0009 #define _RC_CORE
0010 
0011 #include <linux/spinlock.h>
0012 #include <linux/cdev.h>
0013 #include <linux/kfifo.h>
0014 #include <linux/time.h>
0015 #include <linux/timer.h>
0016 #include <media/rc-map.h>
0017 
0018 /**
0019  * enum rc_driver_type - type of the RC driver.
0020  *
0021  * @RC_DRIVER_SCANCODE:  Driver or hardware generates a scancode.
0022  * @RC_DRIVER_IR_RAW:    Driver or hardware generates pulse/space sequences.
0023  *           It needs a Infra-Red pulse/space decoder
0024  * @RC_DRIVER_IR_RAW_TX: Device transmitter only,
0025  *           driver requires pulse/space data sequence.
0026  */
0027 enum rc_driver_type {
0028     RC_DRIVER_SCANCODE = 0,
0029     RC_DRIVER_IR_RAW,
0030     RC_DRIVER_IR_RAW_TX,
0031 };
0032 
0033 /**
0034  * struct rc_scancode_filter - Filter scan codes.
0035  * @data:   Scancode data to match.
0036  * @mask:   Mask of bits of scancode to compare.
0037  */
0038 struct rc_scancode_filter {
0039     u32 data;
0040     u32 mask;
0041 };
0042 
0043 /**
0044  * enum rc_filter_type - Filter type constants.
0045  * @RC_FILTER_NORMAL:   Filter for normal operation.
0046  * @RC_FILTER_WAKEUP:   Filter for waking from suspend.
0047  * @RC_FILTER_MAX:  Number of filter types.
0048  */
0049 enum rc_filter_type {
0050     RC_FILTER_NORMAL = 0,
0051     RC_FILTER_WAKEUP,
0052 
0053     RC_FILTER_MAX
0054 };
0055 
0056 /**
0057  * struct lirc_fh - represents an open lirc file
0058  * @list: list of open file handles
0059  * @rc: rcdev for this lirc chardev
0060  * @carrier_low: when setting the carrier range, first the low end must be
0061  *  set with an ioctl and then the high end with another ioctl
0062  * @rawir: queue for incoming raw IR
0063  * @scancodes: queue for incoming decoded scancodes
0064  * @wait_poll: poll struct for lirc device
0065  * @send_mode: lirc mode for sending, either LIRC_MODE_SCANCODE or
0066  *  LIRC_MODE_PULSE
0067  * @rec_mode: lirc mode for receiving, either LIRC_MODE_SCANCODE or
0068  *  LIRC_MODE_MODE2
0069  */
0070 struct lirc_fh {
0071     struct list_head list;
0072     struct rc_dev *rc;
0073     int             carrier_low;
0074     DECLARE_KFIFO_PTR(rawir, unsigned int);
0075     DECLARE_KFIFO_PTR(scancodes, struct lirc_scancode);
0076     wait_queue_head_t       wait_poll;
0077     u8              send_mode;
0078     u8              rec_mode;
0079 };
0080 
0081 /**
0082  * struct rc_dev - represents a remote control device
0083  * @dev: driver model's view of this device
0084  * @managed_alloc: devm_rc_allocate_device was used to create rc_dev
0085  * @sysfs_groups: sysfs attribute groups
0086  * @device_name: name of the rc child device
0087  * @input_phys: physical path to the input child device
0088  * @input_id: id of the input child device (struct input_id)
0089  * @driver_name: name of the hardware driver which registered this device
0090  * @map_name: name of the default keymap
0091  * @rc_map: current scan/key table
0092  * @lock: used to ensure we've filled in all protocol details before
0093  *  anyone can call show_protocols or store_protocols
0094  * @minor: unique minor remote control device number
0095  * @raw: additional data for raw pulse/space devices
0096  * @input_dev: the input child device used to communicate events to userspace
0097  * @driver_type: specifies if protocol decoding is done in hardware or software
0098  * @idle: used to keep track of RX state
0099  * @encode_wakeup: wakeup filtering uses IR encode API, therefore the allowed
0100  *  wakeup protocols is the set of all raw encoders
0101  * @allowed_protocols: bitmask with the supported RC_PROTO_BIT_* protocols
0102  * @enabled_protocols: bitmask with the enabled RC_PROTO_BIT_* protocols
0103  * @allowed_wakeup_protocols: bitmask with the supported RC_PROTO_BIT_* wakeup
0104  *  protocols
0105  * @wakeup_protocol: the enabled RC_PROTO_* wakeup protocol or
0106  *  RC_PROTO_UNKNOWN if disabled.
0107  * @scancode_filter: scancode filter
0108  * @scancode_wakeup_filter: scancode wakeup filters
0109  * @scancode_mask: some hardware decoders are not capable of providing the full
0110  *  scancode to the application. As this is a hardware limit, we can't do
0111  *  anything with it. Yet, as the same keycode table can be used with other
0112  *  devices, a mask is provided to allow its usage. Drivers should generally
0113  *  leave this field in blank
0114  * @users: number of current users of the device
0115  * @priv: driver-specific data
0116  * @keylock: protects the remaining members of the struct
0117  * @keypressed: whether a key is currently pressed
0118  * @keyup_jiffies: time (in jiffies) when the current keypress should be released
0119  * @timer_keyup: timer for releasing a keypress
0120  * @timer_repeat: timer for autorepeat events. This is needed for CEC, which
0121  *  has non-standard repeats.
0122  * @last_keycode: keycode of last keypress
0123  * @last_protocol: protocol of last keypress
0124  * @last_scancode: scancode of last keypress
0125  * @last_toggle: toggle value of last command
0126  * @timeout: optional time after which device stops sending data
0127  * @min_timeout: minimum timeout supported by device
0128  * @max_timeout: maximum timeout supported by device
0129  * @rx_resolution : resolution (in us) of input sampler
0130  * @tx_resolution: resolution (in us) of output sampler
0131  * @lirc_dev: lirc device
0132  * @lirc_cdev: lirc char cdev
0133  * @gap_start: start time for gap after timeout if non-zero
0134  * @lirc_fh_lock: protects lirc_fh list
0135  * @lirc_fh: list of open files
0136  * @registered: set to true by rc_register_device(), false by
0137  *  rc_unregister_device
0138  * @change_protocol: allow changing the protocol used on hardware decoders
0139  * @open: callback to allow drivers to enable polling/irq when IR input device
0140  *  is opened.
0141  * @close: callback to allow drivers to disable polling/irq when IR input device
0142  *  is opened.
0143  * @s_tx_mask: set transmitter mask (for devices with multiple tx outputs)
0144  * @s_tx_carrier: set transmit carrier frequency
0145  * @s_tx_duty_cycle: set transmit duty cycle (0% - 100%)
0146  * @s_rx_carrier_range: inform driver about carrier it is expected to handle
0147  * @tx_ir: transmit IR
0148  * @s_idle: enable/disable hardware idle mode, upon which,
0149  *  device doesn't interrupt host until it sees IR pulses
0150  * @s_wideband_receiver: enable wide band receiver used for learning
0151  * @s_carrier_report: enable carrier reports
0152  * @s_filter: set the scancode filter
0153  * @s_wakeup_filter: set the wakeup scancode filter. If the mask is zero
0154  *  then wakeup should be disabled. wakeup_protocol will be set to
0155  *  a valid protocol if mask is nonzero.
0156  * @s_timeout: set hardware timeout in us
0157  */
0158 struct rc_dev {
0159     struct device           dev;
0160     bool                managed_alloc;
0161     const struct attribute_group    *sysfs_groups[5];
0162     const char          *device_name;
0163     const char          *input_phys;
0164     struct input_id         input_id;
0165     const char          *driver_name;
0166     const char          *map_name;
0167     struct rc_map           rc_map;
0168     struct mutex            lock;
0169     unsigned int            minor;
0170     struct ir_raw_event_ctrl    *raw;
0171     struct input_dev        *input_dev;
0172     enum rc_driver_type     driver_type;
0173     bool                idle;
0174     bool                encode_wakeup;
0175     u64             allowed_protocols;
0176     u64             enabled_protocols;
0177     u64             allowed_wakeup_protocols;
0178     enum rc_proto           wakeup_protocol;
0179     struct rc_scancode_filter   scancode_filter;
0180     struct rc_scancode_filter   scancode_wakeup_filter;
0181     u32             scancode_mask;
0182     u32             users;
0183     void                *priv;
0184     spinlock_t          keylock;
0185     bool                keypressed;
0186     unsigned long           keyup_jiffies;
0187     struct timer_list       timer_keyup;
0188     struct timer_list       timer_repeat;
0189     u32             last_keycode;
0190     enum rc_proto           last_protocol;
0191     u64             last_scancode;
0192     u8              last_toggle;
0193     u32             timeout;
0194     u32             min_timeout;
0195     u32             max_timeout;
0196     u32             rx_resolution;
0197     u32             tx_resolution;
0198 #ifdef CONFIG_LIRC
0199     struct device           lirc_dev;
0200     struct cdev         lirc_cdev;
0201     ktime_t             gap_start;
0202     spinlock_t          lirc_fh_lock;
0203     struct list_head        lirc_fh;
0204 #endif
0205     bool                registered;
0206     int             (*change_protocol)(struct rc_dev *dev, u64 *rc_proto);
0207     int             (*open)(struct rc_dev *dev);
0208     void                (*close)(struct rc_dev *dev);
0209     int             (*s_tx_mask)(struct rc_dev *dev, u32 mask);
0210     int             (*s_tx_carrier)(struct rc_dev *dev, u32 carrier);
0211     int             (*s_tx_duty_cycle)(struct rc_dev *dev, u32 duty_cycle);
0212     int             (*s_rx_carrier_range)(struct rc_dev *dev, u32 min, u32 max);
0213     int             (*tx_ir)(struct rc_dev *dev, unsigned *txbuf, unsigned n);
0214     void                (*s_idle)(struct rc_dev *dev, bool enable);
0215     int             (*s_wideband_receiver)(struct rc_dev *dev, int enable);
0216     int             (*s_carrier_report) (struct rc_dev *dev, int enable);
0217     int             (*s_filter)(struct rc_dev *dev,
0218                             struct rc_scancode_filter *filter);
0219     int             (*s_wakeup_filter)(struct rc_dev *dev,
0220                                struct rc_scancode_filter *filter);
0221     int             (*s_timeout)(struct rc_dev *dev,
0222                              unsigned int timeout);
0223 };
0224 
0225 #define to_rc_dev(d) container_of(d, struct rc_dev, dev)
0226 
0227 /*
0228  * From rc-main.c
0229  * Those functions can be used on any type of Remote Controller. They
0230  * basically creates an input_dev and properly reports the device as a
0231  * Remote Controller, at sys/class/rc.
0232  */
0233 
0234 /**
0235  * rc_allocate_device - Allocates a RC device
0236  *
0237  * @rc_driver_type: specifies the type of the RC output to be allocated
0238  * returns a pointer to struct rc_dev.
0239  */
0240 struct rc_dev *rc_allocate_device(enum rc_driver_type);
0241 
0242 /**
0243  * devm_rc_allocate_device - Managed RC device allocation
0244  *
0245  * @dev: pointer to struct device
0246  * @rc_driver_type: specifies the type of the RC output to be allocated
0247  * returns a pointer to struct rc_dev.
0248  */
0249 struct rc_dev *devm_rc_allocate_device(struct device *dev, enum rc_driver_type);
0250 
0251 /**
0252  * rc_free_device - Frees a RC device
0253  *
0254  * @dev: pointer to struct rc_dev.
0255  */
0256 void rc_free_device(struct rc_dev *dev);
0257 
0258 /**
0259  * rc_register_device - Registers a RC device
0260  *
0261  * @dev: pointer to struct rc_dev.
0262  */
0263 int rc_register_device(struct rc_dev *dev);
0264 
0265 /**
0266  * devm_rc_register_device - Manageded registering of a RC device
0267  *
0268  * @parent: pointer to struct device.
0269  * @dev: pointer to struct rc_dev.
0270  */
0271 int devm_rc_register_device(struct device *parent, struct rc_dev *dev);
0272 
0273 /**
0274  * rc_unregister_device - Unregisters a RC device
0275  *
0276  * @dev: pointer to struct rc_dev.
0277  */
0278 void rc_unregister_device(struct rc_dev *dev);
0279 
0280 void rc_repeat(struct rc_dev *dev);
0281 void rc_keydown(struct rc_dev *dev, enum rc_proto protocol, u64 scancode,
0282         u8 toggle);
0283 void rc_keydown_notimeout(struct rc_dev *dev, enum rc_proto protocol,
0284               u64 scancode, u8 toggle);
0285 void rc_keyup(struct rc_dev *dev);
0286 u32 rc_g_keycode_from_table(struct rc_dev *dev, u64 scancode);
0287 
0288 /*
0289  * From rc-raw.c
0290  * The Raw interface is specific to InfraRed. It may be a good idea to
0291  * split it later into a separate header.
0292  */
0293 struct ir_raw_event {
0294     union {
0295         u32             duration;
0296         u32             carrier;
0297     };
0298     u8                      duty_cycle;
0299 
0300     unsigned                pulse:1;
0301     unsigned                overflow:1;
0302     unsigned                timeout:1;
0303     unsigned                carrier_report:1;
0304 };
0305 
0306 #define US_TO_NS(usec)      ((usec) * 1000)
0307 #define MS_TO_US(msec)      ((msec) * 1000)
0308 #define IR_MAX_DURATION     MS_TO_US(500)
0309 #define IR_DEFAULT_TIMEOUT  MS_TO_US(125)
0310 #define IR_MAX_TIMEOUT      LIRC_VALUE_MASK
0311 
0312 void ir_raw_event_handle(struct rc_dev *dev);
0313 int ir_raw_event_store(struct rc_dev *dev, struct ir_raw_event *ev);
0314 int ir_raw_event_store_edge(struct rc_dev *dev, bool pulse);
0315 int ir_raw_event_store_with_filter(struct rc_dev *dev,
0316                    struct ir_raw_event *ev);
0317 int ir_raw_event_store_with_timeout(struct rc_dev *dev,
0318                     struct ir_raw_event *ev);
0319 void ir_raw_event_set_idle(struct rc_dev *dev, bool idle);
0320 int ir_raw_encode_scancode(enum rc_proto protocol, u32 scancode,
0321                struct ir_raw_event *events, unsigned int max);
0322 int ir_raw_encode_carrier(enum rc_proto protocol);
0323 
0324 static inline void ir_raw_event_overflow(struct rc_dev *dev)
0325 {
0326     ir_raw_event_store(dev, &((struct ir_raw_event) { .overflow = true }));
0327     dev->idle = true;
0328     ir_raw_event_handle(dev);
0329 }
0330 
0331 /* extract mask bits out of data and pack them into the result */
0332 static inline u32 ir_extract_bits(u32 data, u32 mask)
0333 {
0334     u32 vbit = 1, value = 0;
0335 
0336     do {
0337         if (mask & 1) {
0338             if (data & 1)
0339                 value |= vbit;
0340             vbit <<= 1;
0341         }
0342         data >>= 1;
0343     } while (mask >>= 1);
0344 
0345     return value;
0346 }
0347 
0348 /* Get NEC scancode and protocol type from address and command bytes */
0349 static inline u32 ir_nec_bytes_to_scancode(u8 address, u8 not_address,
0350                        u8 command, u8 not_command,
0351                        enum rc_proto *protocol)
0352 {
0353     u32 scancode;
0354 
0355     if ((command ^ not_command) != 0xff) {
0356         /* NEC transport, but modified protocol, used by at
0357          * least Apple and TiVo remotes
0358          */
0359         scancode = not_address << 24 |
0360             address     << 16 |
0361             not_command <<  8 |
0362             command;
0363         *protocol = RC_PROTO_NEC32;
0364     } else if ((address ^ not_address) != 0xff) {
0365         /* Extended NEC */
0366         scancode = address     << 16 |
0367                not_address <<  8 |
0368                command;
0369         *protocol = RC_PROTO_NECX;
0370     } else {
0371         /* Normal NEC */
0372         scancode = address << 8 | command;
0373         *protocol = RC_PROTO_NEC;
0374     }
0375 
0376     return scancode;
0377 }
0378 
0379 #endif /* _RC_CORE */