Source
x
// SPDX-License-Identifier: GPL-2.0+
//
// Copyright (C) 2018 Sean Young <sean@mess.org>
/* Each bit is 250us */
struct imon {
struct device *dev;
struct urb *ir_urb;
struct rc_dev *rcdev;
u8 ir_buf[8] __aligned(__alignof__(u64));
char phys[64];
};
/*
* The first 5 bytes of data represent IR pulse or space. Each bit, starting
* from highest bit in the first byte, represents 250µs of data. It is 1
* for space and 0 for pulse.
*
* The station sends 10 packets, and the 7th byte will be number 1 to 10, so
* when we receive 10 we assume all the data has arrived.
*/
static void imon_ir_data(struct imon *imon)
{
struct ir_raw_event rawir = {};
u64 d = be64_to_cpup((__be64 *)imon->ir_buf) >> 24;
int offset = 40;
int bit;
dev_dbg(imon->dev, "data: %*ph", 8, imon->ir_buf);
do {
bit = fls64(d & (BIT_ULL(offset) - 1));
if (bit < offset) {
dev_dbg(imon->dev, "pulse: %d bits", offset - bit);
rawir.pulse = true;
rawir.duration = (offset - bit) * BIT_DURATION;
ir_raw_event_store_with_filter(imon->rcdev, &rawir);
if (bit == 0)
break;
offset = bit;
}
bit = fls64(~d & (BIT_ULL(offset) - 1));
dev_dbg(imon->dev, "space: %d bits", offset - bit);
rawir.pulse = false;
rawir.duration = (offset - bit) * BIT_DURATION;
ir_raw_event_store_with_filter(imon->rcdev, &rawir);
offset = bit;
} while (offset > 0);
if (imon->ir_buf[7] == 0x0a) {
ir_raw_event_set_idle(imon->rcdev, true);
ir_raw_event_handle(imon->rcdev);
}
}
static void imon_ir_rx(struct urb *urb)
{
struct imon *imon = urb->context;
int ret;
switch (urb->status) {
case 0:
if (imon->ir_buf[7] != 0xff)
imon_ir_data(imon);
break;
case -ECONNRESET:
case -ENOENT:
case -ESHUTDOWN:
usb_unlink_urb(urb);
return;
case -EPIPE:
default:
dev_dbg(imon->dev, "error: urb status = %d", urb->status);
break;
}
ret = usb_submit_urb(urb, GFP_ATOMIC);
if (ret && ret != -ENODEV)
dev_warn(imon->dev, "failed to resubmit urb: %d", ret);
}