.. | .. |
---|
8 | 8 | #include <media/rc-core.h> |
---|
9 | 9 | |
---|
10 | 10 | /* Each bit is 250us */ |
---|
11 | | -#define BIT_DURATION 250000 |
---|
| 11 | +#define BIT_DURATION 250 |
---|
12 | 12 | |
---|
13 | 13 | struct imon { |
---|
14 | 14 | struct device *dev; |
---|
15 | 15 | struct urb *ir_urb; |
---|
16 | 16 | struct rc_dev *rcdev; |
---|
17 | | - u8 ir_buf[8]; |
---|
| 17 | + __be64 ir_buf; |
---|
18 | 18 | char phys[64]; |
---|
19 | 19 | }; |
---|
20 | 20 | |
---|
21 | 21 | /* |
---|
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. |
---|
23 | 28 | */ |
---|
24 | | -static inline int is_bit_set(const u8 *buf, int bit) |
---|
25 | | -{ |
---|
26 | | - return buf[bit / 8] & (0x80 >> (bit & 7)); |
---|
27 | | -} |
---|
28 | | - |
---|
29 | 29 | static void imon_ir_data(struct imon *imon) |
---|
30 | 30 | { |
---|
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; |
---|
33 | 35 | int bit; |
---|
34 | 36 | |
---|
35 | | - dev_dbg(imon->dev, "data: %*ph", 8, imon->ir_buf); |
---|
| 37 | + if (packet_no == 0xff) |
---|
| 38 | + return; |
---|
36 | 39 | |
---|
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; |
---|
45 | 66 | ir_raw_event_store_with_filter(imon->rcdev, &rawir); |
---|
| 67 | + |
---|
| 68 | + offset = bit; |
---|
46 | 69 | } |
---|
47 | 70 | |
---|
48 | | - if (bit >= size) |
---|
49 | | - break; |
---|
| 71 | + data = ~data; |
---|
| 72 | + } while (offset > 0); |
---|
50 | 73 | |
---|
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) { |
---|
64 | 75 | ir_raw_event_set_idle(imon->rcdev, true); |
---|
65 | 76 | ir_raw_event_handle(imon->rcdev); |
---|
66 | 77 | } |
---|
.. | .. |
---|
73 | 84 | |
---|
74 | 85 | switch (urb->status) { |
---|
75 | 86 | case 0: |
---|
76 | | - if (imon->ir_buf[7] != 0xff) |
---|
77 | | - imon_ir_data(imon); |
---|
| 87 | + imon_ir_data(imon); |
---|
78 | 88 | break; |
---|
79 | 89 | case -ECONNRESET: |
---|
80 | 90 | case -ENOENT: |
---|
.. | .. |
---|
130 | 140 | imon->dev = &intf->dev; |
---|
131 | 141 | usb_fill_int_urb(imon->ir_urb, udev, |
---|
132 | 142 | usb_rcvintpipe(udev, ir_ep->bEndpointAddress), |
---|
133 | | - imon->ir_buf, sizeof(imon->ir_buf), |
---|
| 143 | + &imon->ir_buf, sizeof(imon->ir_buf), |
---|
134 | 144 | imon_ir_rx, imon, ir_ep->bInterval); |
---|
135 | 145 | |
---|
136 | 146 | rcdev = devm_rc_allocate_device(&intf->dev, RC_DRIVER_IR_RAW); |
---|