forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-05-10 23fa18eaa71266feff7ba8d83022d9e1cc83c65a
kernel/drivers/media/rc/imon_raw.c
....@@ -8,59 +8,70 @@
88 #include <media/rc-core.h>
99
1010 /* Each bit is 250us */
11
-#define BIT_DURATION 250000
11
+#define BIT_DURATION 250
1212
1313 struct imon {
1414 struct device *dev;
1515 struct urb *ir_urb;
1616 struct rc_dev *rcdev;
17
- u8 ir_buf[8];
17
+ __be64 ir_buf;
1818 char phys[64];
1919 };
2020
2121 /*
22
- * ffs/find_next_bit() searches in the wrong direction, so open-code our own.
22
+ * The first 5 bytes of data represent IR pulse or space. Each bit, starting
23
+ * from highest bit in the first byte, represents 250µs of data. It is 1
24
+ * for space and 0 for pulse.
25
+ *
26
+ * The station sends 10 packets, and the 7th byte will be number 1 to 10, so
27
+ * when we receive 10 we assume all the data has arrived.
2328 */
24
-static inline int is_bit_set(const u8 *buf, int bit)
25
-{
26
- return buf[bit / 8] & (0x80 >> (bit & 7));
27
-}
28
-
2929 static void imon_ir_data(struct imon *imon)
3030 {
31
- DEFINE_IR_RAW_EVENT(rawir);
32
- int offset = 0, size = 5 * 8;
31
+ struct ir_raw_event rawir = {};
32
+ u64 data = be64_to_cpu(imon->ir_buf);
33
+ u8 packet_no = data & 0xff;
34
+ int offset = 40;
3335 int bit;
3436
35
- dev_dbg(imon->dev, "data: %*ph", 8, imon->ir_buf);
37
+ if (packet_no == 0xff)
38
+ return;
3639
37
- while (offset < size) {
38
- bit = offset;
39
- while (!is_bit_set(imon->ir_buf, bit) && bit < size)
40
- bit++;
41
- dev_dbg(imon->dev, "pulse: %d bits", bit - offset);
42
- if (bit > offset) {
43
- rawir.pulse = true;
44
- rawir.duration = (bit - offset) * BIT_DURATION;
40
+ dev_dbg(imon->dev, "data: %*ph", 8, &imon->ir_buf);
41
+
42
+ /*
43
+ * Only the first 5 bytes contain IR data. Right shift so we move
44
+ * the IR bits to the lower 40 bits.
45
+ */
46
+ data >>= 24;
47
+
48
+ do {
49
+ /*
50
+ * Find highest set bit which is less or equal to offset
51
+ *
52
+ * offset is the bit above (base 0) where we start looking.
53
+ *
54
+ * data & (BIT_ULL(offset) - 1) masks off any unwanted bits,
55
+ * so we have just bits less than offset.
56
+ *
57
+ * fls will tell us the highest bit set plus 1 (or 0 if no
58
+ * bits are set).
59
+ */
60
+ rawir.pulse = !rawir.pulse;
61
+ bit = fls64(data & (BIT_ULL(offset) - 1));
62
+ if (bit < offset) {
63
+ dev_dbg(imon->dev, "%s: %d bits",
64
+ rawir.pulse ? "pulse" : "space", offset - bit);
65
+ rawir.duration = (offset - bit) * BIT_DURATION;
4566 ir_raw_event_store_with_filter(imon->rcdev, &rawir);
67
+
68
+ offset = bit;
4669 }
4770
48
- if (bit >= size)
49
- break;
71
+ data = ~data;
72
+ } while (offset > 0);
5073
51
- offset = bit;
52
- while (is_bit_set(imon->ir_buf, bit) && bit < size)
53
- bit++;
54
- dev_dbg(imon->dev, "space: %d bits", bit - offset);
55
-
56
- rawir.pulse = false;
57
- rawir.duration = (bit - offset) * BIT_DURATION;
58
- ir_raw_event_store_with_filter(imon->rcdev, &rawir);
59
-
60
- offset = bit;
61
- }
62
-
63
- if (imon->ir_buf[7] == 0x0a) {
74
+ if (packet_no == 0x0a && !imon->rcdev->idle) {
6475 ir_raw_event_set_idle(imon->rcdev, true);
6576 ir_raw_event_handle(imon->rcdev);
6677 }
....@@ -73,8 +84,7 @@
7384
7485 switch (urb->status) {
7586 case 0:
76
- if (imon->ir_buf[7] != 0xff)
77
- imon_ir_data(imon);
87
+ imon_ir_data(imon);
7888 break;
7989 case -ECONNRESET:
8090 case -ENOENT:
....@@ -130,7 +140,7 @@
130140 imon->dev = &intf->dev;
131141 usb_fill_int_urb(imon->ir_urb, udev,
132142 usb_rcvintpipe(udev, ir_ep->bEndpointAddress),
133
- imon->ir_buf, sizeof(imon->ir_buf),
143
+ &imon->ir_buf, sizeof(imon->ir_buf),
134144 imon_ir_rx, imon, ir_ep->bInterval);
135145
136146 rcdev = devm_rc_allocate_device(&intf->dev, RC_DRIVER_IR_RAW);