Skip to content

Commit de142c3

Browse files
seanyoungmchehab
authored andcommitted
media: lirc: implement reading scancode
This implements LIRC_MODE_SCANCODE reading from the lirc device. The scancode can be read from the input device too, but with this interface you get the rc protocol, keycode, toggle and repeat status in addition to just the scancode. int main() { int fd, mode, rc; fd = open("/dev/lirc0", O_RDWR); mode = LIRC_MODE_SCANCODE; if (ioctl(fd, LIRC_SET_REC_MODE, &mode)) { // kernel too old or lirc does not support transmit } struct lirc_scancode scancode; while (read(fd, &scancode, sizeof(scancode)) == sizeof(scancode)) { printf("protocol:%d scancode:0x%x toggle:%d repeat:%d\n", scancode.rc_proto, scancode.scancode, !!(scancode.flags & LIRC_SCANCODE_FLAG_TOGGLE), !!(scancode.flags & LIRC_SCANCODE_FLAG_REPEAT)); } close(fd); } Signed-off-by: Sean Young <sean@mess.org> Signed-off-by: Mauro Carvalho Chehab <mchehab@s-opensource.com>
1 parent a6ddd4f commit de142c3

6 files changed

Lines changed: 122 additions & 15 deletions

File tree

‎drivers/media/rc/ir-lirc-codec.c‎

Lines changed: 89 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,21 @@ void ir_lirc_raw_event(struct rc_dev *dev, struct ir_raw_event ev)
8888
wake_up_poll(&dev->wait_poll, POLLIN | POLLRDNORM);
8989
}
9090

91+
/**
92+
* ir_lirc_scancode_event() - Send scancode data to lirc to be relayed to
93+
* userspace
94+
* @dev: the struct rc_dev descriptor of the device
95+
* @lsc: the struct lirc_scancode describing the decoded scancode
96+
*/
97+
void ir_lirc_scancode_event(struct rc_dev *dev, struct lirc_scancode *lsc)
98+
{
99+
lsc->timestamp = ktime_get_ns();
100+
101+
if (kfifo_put(&dev->scancodes, *lsc))
102+
wake_up_poll(&dev->wait_poll, POLLIN | POLLRDNORM);
103+
}
104+
EXPORT_SYMBOL_GPL(ir_lirc_scancode_event);
105+
91106
static int ir_lirc_open(struct inode *inode, struct file *file)
92107
{
93108
struct rc_dev *dev = container_of(inode->i_cdev, struct rc_dev,
@@ -114,6 +129,8 @@ static int ir_lirc_open(struct inode *inode, struct file *file)
114129

115130
if (dev->driver_type == RC_DRIVER_IR_RAW)
116131
kfifo_reset_out(&dev->rawir);
132+
if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
133+
kfifo_reset_out(&dev->scancodes);
117134

118135
dev->lirc_open++;
119136
file->private_data = dev;
@@ -288,7 +305,7 @@ static long ir_lirc_ioctl(struct file *filep, unsigned int cmd,
288305
switch (cmd) {
289306
case LIRC_GET_FEATURES:
290307
if (dev->driver_type == RC_DRIVER_IR_RAW) {
291-
val |= LIRC_CAN_REC_MODE2;
308+
val |= LIRC_CAN_REC_MODE2 | LIRC_CAN_REC_SCANCODE;
292309
if (dev->rx_resolution)
293310
val |= LIRC_CAN_GET_REC_RESOLUTION;
294311
}
@@ -323,15 +340,17 @@ static long ir_lirc_ioctl(struct file *filep, unsigned int cmd,
323340
if (dev->driver_type == RC_DRIVER_IR_RAW_TX)
324341
return -ENOTTY;
325342

326-
val = LIRC_MODE_MODE2;
343+
val = dev->rec_mode;
327344
break;
328345

329346
case LIRC_SET_REC_MODE:
330347
if (dev->driver_type == RC_DRIVER_IR_RAW_TX)
331348
return -ENOTTY;
332349

333-
if (val != LIRC_MODE_MODE2)
350+
if (!(val == LIRC_MODE_MODE2 || val == LIRC_MODE_SCANCODE))
334351
return -EINVAL;
352+
353+
dev->rec_mode = val;
335354
return 0;
336355

337356
case LIRC_GET_SEND_MODE:
@@ -471,31 +490,31 @@ static unsigned int ir_lirc_poll(struct file *file,
471490

472491
poll_wait(file, &rcdev->wait_poll, wait);
473492

474-
if (!rcdev->registered)
493+
if (!rcdev->registered) {
475494
events = POLLHUP | POLLERR;
476-
else if (rcdev->driver_type == RC_DRIVER_IR_RAW &&
477-
!kfifo_is_empty(&rcdev->rawir))
478-
events = POLLIN | POLLRDNORM;
495+
} else if (rcdev->driver_type != RC_DRIVER_IR_RAW_TX) {
496+
if (rcdev->rec_mode == LIRC_MODE_SCANCODE &&
497+
!kfifo_is_empty(&rcdev->scancodes))
498+
events = POLLIN | POLLRDNORM;
499+
500+
if (rcdev->rec_mode == LIRC_MODE_MODE2 &&
501+
!kfifo_is_empty(&rcdev->rawir))
502+
events = POLLIN | POLLRDNORM;
503+
}
479504

480505
return events;
481506
}
482507

483-
static ssize_t ir_lirc_read(struct file *file, char __user *buffer,
484-
size_t length, loff_t *ppos)
508+
static ssize_t ir_lirc_read_mode2(struct file *file, char __user *buffer,
509+
size_t length)
485510
{
486511
struct rc_dev *rcdev = file->private_data;
487512
unsigned int copied;
488513
int ret;
489514

490-
if (rcdev->driver_type == RC_DRIVER_IR_RAW_TX)
491-
return -EINVAL;
492-
493515
if (length < sizeof(unsigned int) || length % sizeof(unsigned int))
494516
return -EINVAL;
495517

496-
if (!rcdev->registered)
497-
return -ENODEV;
498-
499518
do {
500519
if (kfifo_is_empty(&rcdev->rawir)) {
501520
if (file->f_flags & O_NONBLOCK)
@@ -523,6 +542,61 @@ static ssize_t ir_lirc_read(struct file *file, char __user *buffer,
523542
return copied;
524543
}
525544

545+
static ssize_t ir_lirc_read_scancode(struct file *file, char __user *buffer,
546+
size_t length)
547+
{
548+
struct rc_dev *rcdev = file->private_data;
549+
unsigned int copied;
550+
int ret;
551+
552+
if (length < sizeof(struct lirc_scancode) ||
553+
length % sizeof(struct lirc_scancode))
554+
return -EINVAL;
555+
556+
do {
557+
if (kfifo_is_empty(&rcdev->scancodes)) {
558+
if (file->f_flags & O_NONBLOCK)
559+
return -EAGAIN;
560+
561+
ret = wait_event_interruptible(rcdev->wait_poll,
562+
!kfifo_is_empty(&rcdev->scancodes) ||
563+
!rcdev->registered);
564+
if (ret)
565+
return ret;
566+
}
567+
568+
if (!rcdev->registered)
569+
return -ENODEV;
570+
571+
ret = mutex_lock_interruptible(&rcdev->lock);
572+
if (ret)
573+
return ret;
574+
ret = kfifo_to_user(&rcdev->scancodes, buffer, length, &copied);
575+
mutex_unlock(&rcdev->lock);
576+
if (ret)
577+
return ret;
578+
} while (copied == 0);
579+
580+
return copied;
581+
}
582+
583+
static ssize_t ir_lirc_read(struct file *file, char __user *buffer,
584+
size_t length, loff_t *ppos)
585+
{
586+
struct rc_dev *rcdev = file->private_data;
587+
588+
if (rcdev->driver_type == RC_DRIVER_IR_RAW_TX)
589+
return -EINVAL;
590+
591+
if (!rcdev->registered)
592+
return -ENODEV;
593+
594+
if (rcdev->rec_mode == LIRC_MODE_MODE2)
595+
return ir_lirc_read_mode2(file, buffer, length);
596+
else /* LIRC_MODE_SCANCODE */
597+
return ir_lirc_read_scancode(file, buffer, length);
598+
}
599+
526600
const struct file_operations lirc_fops = {
527601
.owner = THIS_MODULE,
528602
.write = ir_lirc_transmit_ir,

‎drivers/media/rc/ir-mce_kbd-decoder.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ static int ir_mce_kbd_decode(struct rc_dev *dev, struct ir_raw_event ev)
215215
struct mce_kbd_dec *data = &dev->raw->mce_kbd;
216216
u32 scancode;
217217
unsigned long delay;
218+
struct lirc_scancode lsc = {};
218219

219220
if (!is_timing_event(ev)) {
220221
if (ev.reset)
@@ -326,18 +327,22 @@ static int ir_mce_kbd_decode(struct rc_dev *dev, struct ir_raw_event ev)
326327
mod_timer(&data->rx_timeout, jiffies + delay);
327328
/* Pass data to keyboard buffer parser */
328329
ir_mce_kbd_process_keyboard_data(data->idev, scancode);
330+
lsc.rc_proto = RC_PROTO_MCIR2_KBD;
329331
break;
330332
case MCIR2_MOUSE_NBITS:
331333
scancode = data->body & 0x1fffff;
332334
IR_dprintk(1, "mouse data 0x%06x\n", scancode);
333335
/* Pass data to mouse buffer parser */
334336
ir_mce_kbd_process_mouse_data(data->idev, scancode);
337+
lsc.rc_proto = RC_PROTO_MCIR2_MSE;
335338
break;
336339
default:
337340
IR_dprintk(1, "not keyboard or mouse data\n");
338341
goto out;
339342
}
340343

344+
lsc.scancode = scancode;
345+
ir_lirc_scancode_event(dev, &lsc);
341346
data->state = STATE_INACTIVE;
342347
input_event(data->idev, EV_MSC, MSC_SCAN, scancode);
343348
input_sync(data->idev);

‎drivers/media/rc/lirc_dev.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ static void lirc_release_device(struct device *ld)
4242

4343
if (rcdev->driver_type == RC_DRIVER_IR_RAW)
4444
kfifo_free(&rcdev->rawir);
45+
if (rcdev->driver_type != RC_DRIVER_IR_RAW_TX)
46+
kfifo_free(&rcdev->scancodes);
4547

4648
put_device(&rcdev->dev);
4749
}
@@ -55,11 +57,20 @@ int ir_lirc_register(struct rc_dev *dev)
5557
dev->lirc_dev.release = lirc_release_device;
5658
dev->send_mode = LIRC_MODE_PULSE;
5759

60+
dev->rec_mode = LIRC_MODE_MODE2;
61+
5862
if (dev->driver_type == RC_DRIVER_IR_RAW) {
5963
if (kfifo_alloc(&dev->rawir, MAX_IR_EVENT_SIZE, GFP_KERNEL))
6064
return -ENOMEM;
6165
}
6266

67+
if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
68+
if (kfifo_alloc(&dev->scancodes, 32, GFP_KERNEL)) {
69+
kfifo_free(&dev->rawir);
70+
return -ENOMEM;
71+
}
72+
}
73+
6374
init_waitqueue_head(&dev->wait_poll);
6475

6576
minor = ida_simple_get(&lirc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
@@ -90,6 +101,8 @@ int ir_lirc_register(struct rc_dev *dev)
90101
out_kfifo:
91102
if (dev->driver_type == RC_DRIVER_IR_RAW)
92103
kfifo_free(&dev->rawir);
104+
if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
105+
kfifo_free(&dev->scancodes);
93106
return err;
94107
}
95108

‎drivers/media/rc/rc-core-priv.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ void ir_raw_init(void);
276276
int lirc_dev_init(void);
277277
void lirc_dev_exit(void);
278278
void ir_lirc_raw_event(struct rc_dev *dev, struct ir_raw_event ev);
279+
void ir_lirc_scancode_event(struct rc_dev *dev, struct lirc_scancode *lsc);
279280
int ir_lirc_register(struct rc_dev *dev);
280281
void ir_lirc_unregister(struct rc_dev *dev);
281282

@@ -285,6 +286,8 @@ static inline int lirc_dev_init(void) { return 0; }
285286
static inline void lirc_dev_exit(void) {}
286287
static inline void ir_lirc_raw_event(struct rc_dev *dev,
287288
struct ir_raw_event ev) { }
289+
static inline void ir_lirc_scancode_event(struct rc_dev *dev,
290+
struct lirc_scancode *lsc) { }
288291
static inline int ir_lirc_register(struct rc_dev *dev) { return 0; }
289292
static inline void ir_lirc_unregister(struct rc_dev *dev) { }
290293
#endif

‎drivers/media/rc/rc-main.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,13 @@ static void ir_do_keydown(struct rc_dev *dev, enum rc_proto protocol,
697697
dev->last_protocol != protocol ||
698698
dev->last_scancode != scancode ||
699699
dev->last_toggle != toggle);
700+
struct lirc_scancode sc = {
701+
.scancode = scancode, .rc_proto = protocol,
702+
.flags = toggle ? LIRC_SCANCODE_FLAG_TOGGLE : 0,
703+
.keycode = keycode
704+
};
705+
706+
ir_lirc_scancode_event(dev, &sc);
700707

701708
if (new_event && dev->keypressed)
702709
ir_do_keyup(dev, false);

‎include/media/rc-core.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,12 @@ enum rc_filter_type {
126126
* @gap: true if we're in a gap
127127
* @send_timeout_reports: report timeouts in lirc raw IR.
128128
* @rawir: queue for incoming raw IR
129+
* @scancodes: queue for incoming decoded scancodes
129130
* @wait_poll: poll struct for lirc device
130131
* @send_mode: lirc mode for sending, either LIRC_MODE_SCANCODE or
131132
* LIRC_MODE_PULSE
133+
* @rec_mode: lirc mode for receiving, either LIRC_MODE_SCANCODE or
134+
* LIRC_MODE_MODE2
132135
* @registered: set to true by rc_register_device(), false by
133136
* rc_unregister_device
134137
* @change_protocol: allow changing the protocol used on hardware decoders
@@ -200,8 +203,10 @@ struct rc_dev {
200203
bool gap;
201204
bool send_timeout_reports;
202205
DECLARE_KFIFO_PTR(rawir, unsigned int);
206+
DECLARE_KFIFO_PTR(scancodes, struct lirc_scancode);
203207
wait_queue_head_t wait_poll;
204208
u8 send_mode;
209+
u8 rec_mode;
205210
#endif
206211
bool registered;
207212
int (*change_protocol)(struct rc_dev *dev, u64 *rc_proto);

0 commit comments

Comments
 (0)