Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 1 | /* ir-rc5-sz-decoder.c - handle RC5 Streamzap IR Pulse/Space protocol |
| 2 | * |
| 3 | * Copyright (C) 2010 by Mauro Carvalho Chehab <mchehab@redhat.com> |
| 4 | * Copyright (C) 2010 by Jarod Wilson <jarod@redhat.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | /* |
| 17 | * This code handles the 15 bit RC5-ish protocol used by the Streamzap |
| 18 | * PC Remote. |
| 19 | * It considers a carrier of 36 kHz, with a total of 15 bits, where |
| 20 | * the first two bits are start bits, and a third one is a filing bit |
| 21 | */ |
| 22 | |
Mauro Carvalho Chehab | f62de67 | 2010-11-09 23:09:57 -0300 | [diff] [blame] | 23 | #include "rc-core-priv.h" |
Paul Gortmaker | 7a707b8 | 2011-07-03 14:03:12 -0400 | [diff] [blame] | 24 | #include <linux/module.h> |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 25 | |
| 26 | #define RC5_SZ_NBITS 15 |
| 27 | #define RC5_UNIT 888888 /* ns */ |
| 28 | #define RC5_BIT_START (1 * RC5_UNIT) |
| 29 | #define RC5_BIT_END (1 * RC5_UNIT) |
| 30 | |
| 31 | enum rc5_sz_state { |
| 32 | STATE_INACTIVE, |
| 33 | STATE_BIT_START, |
| 34 | STATE_BIT_END, |
| 35 | STATE_FINISHED, |
| 36 | }; |
| 37 | |
| 38 | /** |
| 39 | * ir_rc5_sz_decode() - Decode one RC-5 Streamzap pulse or space |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 40 | * @dev: the struct rc_dev descriptor of the device |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 41 | * @ev: the struct ir_raw_event descriptor of the pulse/space |
| 42 | * |
| 43 | * This function returns -EINVAL if the pulse violates the state machine |
| 44 | */ |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 45 | static int ir_rc5_sz_decode(struct rc_dev *dev, struct ir_raw_event ev) |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 46 | { |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 47 | struct rc5_sz_dec *data = &dev->raw->rc5_sz; |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 48 | u8 toggle, command, system; |
| 49 | u32 scancode; |
| 50 | |
Mauro Carvalho Chehab | 52b6614 | 2010-11-17 14:20:52 -0300 | [diff] [blame] | 51 | if (!(dev->raw->enabled_protocols & RC_TYPE_RC5_SZ)) |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 52 | return 0; |
| 53 | |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 54 | if (!is_timing_event(ev)) { |
| 55 | if (ev.reset) |
| 56 | data->state = STATE_INACTIVE; |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | if (!geq_margin(ev.duration, RC5_UNIT, RC5_UNIT / 2)) |
| 61 | goto out; |
| 62 | |
| 63 | again: |
| 64 | IR_dprintk(2, "RC5-sz decode started at state %i (%uus %s)\n", |
| 65 | data->state, TO_US(ev.duration), TO_STR(ev.pulse)); |
| 66 | |
| 67 | if (!geq_margin(ev.duration, RC5_UNIT, RC5_UNIT / 2)) |
| 68 | return 0; |
| 69 | |
| 70 | switch (data->state) { |
| 71 | |
| 72 | case STATE_INACTIVE: |
| 73 | if (!ev.pulse) |
| 74 | break; |
| 75 | |
| 76 | data->state = STATE_BIT_START; |
| 77 | data->count = 1; |
| 78 | data->wanted_bits = RC5_SZ_NBITS; |
| 79 | decrease_duration(&ev, RC5_BIT_START); |
| 80 | goto again; |
| 81 | |
| 82 | case STATE_BIT_START: |
| 83 | if (!eq_margin(ev.duration, RC5_BIT_START, RC5_UNIT / 2)) |
| 84 | break; |
| 85 | |
| 86 | data->bits <<= 1; |
| 87 | if (!ev.pulse) |
| 88 | data->bits |= 1; |
| 89 | data->count++; |
| 90 | data->state = STATE_BIT_END; |
| 91 | return 0; |
| 92 | |
| 93 | case STATE_BIT_END: |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 94 | if (!is_transition(&ev, &dev->raw->prev_ev)) |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 95 | break; |
| 96 | |
| 97 | if (data->count == data->wanted_bits) |
| 98 | data->state = STATE_FINISHED; |
| 99 | else |
| 100 | data->state = STATE_BIT_START; |
| 101 | |
| 102 | decrease_duration(&ev, RC5_BIT_END); |
| 103 | goto again; |
| 104 | |
| 105 | case STATE_FINISHED: |
| 106 | if (ev.pulse) |
| 107 | break; |
| 108 | |
| 109 | /* RC5-sz */ |
| 110 | command = (data->bits & 0x0003F) >> 0; |
| 111 | system = (data->bits & 0x02FC0) >> 6; |
| 112 | toggle = (data->bits & 0x01000) ? 1 : 0; |
| 113 | scancode = system << 6 | command; |
| 114 | |
| 115 | IR_dprintk(1, "RC5-sz scancode 0x%04x (toggle: %u)\n", |
| 116 | scancode, toggle); |
| 117 | |
Mauro Carvalho Chehab | ca86674 | 2010-11-17 13:53:11 -0300 | [diff] [blame] | 118 | rc_keydown(dev, scancode, toggle); |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 119 | data->state = STATE_INACTIVE; |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | out: |
| 124 | IR_dprintk(1, "RC5-sz decode failed at state %i (%uus %s)\n", |
| 125 | data->state, TO_US(ev.duration), TO_STR(ev.pulse)); |
| 126 | data->state = STATE_INACTIVE; |
| 127 | return -EINVAL; |
| 128 | } |
| 129 | |
| 130 | static struct ir_raw_handler rc5_sz_handler = { |
Mauro Carvalho Chehab | 52b6614 | 2010-11-17 14:20:52 -0300 | [diff] [blame] | 131 | .protocols = RC_TYPE_RC5_SZ, |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 132 | .decode = ir_rc5_sz_decode, |
| 133 | }; |
| 134 | |
| 135 | static int __init ir_rc5_sz_decode_init(void) |
| 136 | { |
| 137 | ir_raw_handler_register(&rc5_sz_handler); |
| 138 | |
| 139 | printk(KERN_INFO "IR RC5 (streamzap) protocol handler initialized\n"); |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static void __exit ir_rc5_sz_decode_exit(void) |
| 144 | { |
| 145 | ir_raw_handler_unregister(&rc5_sz_handler); |
| 146 | } |
| 147 | |
| 148 | module_init(ir_rc5_sz_decode_init); |
| 149 | module_exit(ir_rc5_sz_decode_exit); |
| 150 | |
| 151 | MODULE_LICENSE("GPL"); |
| 152 | MODULE_AUTHOR("Jarod Wilson <jarod@redhat.com>"); |
| 153 | MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)"); |
| 154 | MODULE_DESCRIPTION("RC5 (streamzap) IR protocol decoder"); |