Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1 | /* |
| 2 | handle cx231xx IR remotes via linux kernel input layer. |
| 3 | |
| 4 | Copyright (C) 2008 <srinivasa.deevi at conexant dot com> |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 5 | Based on em28xx driver |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 6 | |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 7 | < This is a place holder for IR now.> |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 8 | |
| 9 | This program is free software; you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation; either version 2 of the License, or |
| 12 | (at your option) any later version. |
| 13 | |
| 14 | This program is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with this program; if not, write to the Free Software |
| 21 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/delay.h> |
| 27 | #include <linux/interrupt.h> |
| 28 | #include <linux/input.h> |
| 29 | #include <linux/usb.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame^] | 30 | #include <linux/slab.h> |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 31 | |
| 32 | #include "cx231xx.h" |
| 33 | |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 34 | static unsigned int ir_debug; |
| 35 | module_param(ir_debug, int, 0644); |
| 36 | MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]"); |
| 37 | |
| 38 | #define i2cdprintk(fmt, arg...) \ |
| 39 | if (ir_debug) { \ |
Jean Delvare | 1df8e98 | 2009-05-13 16:48:07 -0300 | [diff] [blame] | 40 | printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \ |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | #define dprintk(fmt, arg...) \ |
| 44 | if (ir_debug) { \ |
| 45 | printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \ |
| 46 | } |
| 47 | |
| 48 | /********************************************************** |
| 49 | Polling structure used by cx231xx IR's |
| 50 | **********************************************************/ |
| 51 | |
| 52 | struct cx231xx_ir_poll_result { |
| 53 | unsigned int toggle_bit:1; |
| 54 | unsigned int read_count:7; |
| 55 | u8 rc_address; |
| 56 | u8 rc_data[4]; |
| 57 | }; |
| 58 | |
| 59 | struct cx231xx_IR { |
| 60 | struct cx231xx *dev; |
| 61 | struct input_dev *input; |
| 62 | struct ir_input_state ir; |
| 63 | char name[32]; |
| 64 | char phys[32]; |
| 65 | |
| 66 | /* poll external decoder */ |
| 67 | int polling; |
| 68 | struct work_struct work; |
| 69 | struct timer_list timer; |
| 70 | unsigned int last_toggle:1; |
| 71 | unsigned int last_readcount; |
| 72 | unsigned int repeat_interval; |
| 73 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 74 | int (*get_key) (struct cx231xx_IR *, struct cx231xx_ir_poll_result *); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 75 | }; |
| 76 | |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 77 | /********************************************************** |
| 78 | Polling code for cx231xx |
| 79 | **********************************************************/ |
| 80 | |
| 81 | static void cx231xx_ir_handle_key(struct cx231xx_IR *ir) |
| 82 | { |
| 83 | int result; |
| 84 | int do_sendkey = 0; |
| 85 | struct cx231xx_ir_poll_result poll_result; |
| 86 | |
| 87 | /* read the registers containing the IR status */ |
| 88 | result = ir->get_key(ir, &poll_result); |
| 89 | if (result < 0) { |
| 90 | dprintk("ir->get_key() failed %d\n", result); |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | dprintk("ir->get_key result tb=%02x rc=%02x lr=%02x data=%02x\n", |
| 95 | poll_result.toggle_bit, poll_result.read_count, |
| 96 | ir->last_readcount, poll_result.rc_data[0]); |
| 97 | |
| 98 | if (ir->dev->chip_id == CHIP_ID_EM2874) { |
| 99 | /* The em2874 clears the readcount field every time the |
| 100 | register is read. The em2860/2880 datasheet says that it |
| 101 | is supposed to clear the readcount, but it doesn't. So with |
| 102 | the em2874, we are looking for a non-zero read count as |
| 103 | opposed to a readcount that is incrementing */ |
| 104 | ir->last_readcount = 0; |
| 105 | } |
| 106 | |
| 107 | if (poll_result.read_count == 0) { |
| 108 | /* The button has not been pressed since the last read */ |
| 109 | } else if (ir->last_toggle != poll_result.toggle_bit) { |
| 110 | /* A button has been pressed */ |
| 111 | dprintk("button has been pressed\n"); |
| 112 | ir->last_toggle = poll_result.toggle_bit; |
| 113 | ir->repeat_interval = 0; |
| 114 | do_sendkey = 1; |
| 115 | } else if (poll_result.toggle_bit == ir->last_toggle && |
| 116 | poll_result.read_count > 0 && |
| 117 | poll_result.read_count != ir->last_readcount) { |
| 118 | /* The button is still being held down */ |
| 119 | dprintk("button being held down\n"); |
| 120 | |
| 121 | /* Debouncer for first keypress */ |
| 122 | if (ir->repeat_interval++ > 9) { |
| 123 | /* Start repeating after 1 second */ |
| 124 | do_sendkey = 1; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if (do_sendkey) { |
| 129 | dprintk("sending keypress\n"); |
Mauro Carvalho Chehab | 8573b74 | 2009-11-27 22:40:22 -0300 | [diff] [blame] | 130 | ir_input_keydown(ir->input, &ir->ir, poll_result.rc_data[0]); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 131 | ir_input_nokey(ir->input, &ir->ir); |
| 132 | } |
| 133 | |
| 134 | ir->last_readcount = poll_result.read_count; |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | static void ir_timer(unsigned long data) |
| 139 | { |
| 140 | struct cx231xx_IR *ir = (struct cx231xx_IR *)data; |
| 141 | |
| 142 | schedule_work(&ir->work); |
| 143 | } |
| 144 | |
| 145 | static void cx231xx_ir_work(struct work_struct *work) |
| 146 | { |
| 147 | struct cx231xx_IR *ir = container_of(work, struct cx231xx_IR, work); |
| 148 | |
| 149 | cx231xx_ir_handle_key(ir); |
| 150 | mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling)); |
| 151 | } |
| 152 | |
| 153 | void cx231xx_ir_start(struct cx231xx_IR *ir) |
| 154 | { |
| 155 | setup_timer(&ir->timer, ir_timer, (unsigned long)ir); |
| 156 | INIT_WORK(&ir->work, cx231xx_ir_work); |
| 157 | schedule_work(&ir->work); |
| 158 | } |
| 159 | |
| 160 | static void cx231xx_ir_stop(struct cx231xx_IR *ir) |
| 161 | { |
| 162 | del_timer_sync(&ir->timer); |
| 163 | flush_scheduled_work(); |
| 164 | } |
| 165 | |
| 166 | int cx231xx_ir_init(struct cx231xx *dev) |
| 167 | { |
| 168 | struct cx231xx_IR *ir; |
| 169 | struct input_dev *input_dev; |
| 170 | u8 ir_config; |
| 171 | int err = -ENOMEM; |
| 172 | |
| 173 | if (dev->board.ir_codes == NULL) { |
| 174 | /* No remote control support */ |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | ir = kzalloc(sizeof(*ir), GFP_KERNEL); |
| 179 | input_dev = input_allocate_device(); |
| 180 | if (!ir || !input_dev) |
| 181 | goto err_out_free; |
| 182 | |
| 183 | ir->input = input_dev; |
| 184 | |
| 185 | /* Setup the proper handler based on the chip */ |
| 186 | switch (dev->chip_id) { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 187 | default: |
| 188 | printk("Unrecognized cx231xx chip id: IR not supported\n"); |
| 189 | goto err_out_free; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /* This is how often we ask the chip for IR information */ |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 193 | ir->polling = 100; /* ms */ |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 194 | |
| 195 | /* init input device */ |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 196 | snprintf(ir->name, sizeof(ir->name), "cx231xx IR (%s)", dev->name); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 197 | |
| 198 | usb_make_path(dev->udev, ir->phys, sizeof(ir->phys)); |
| 199 | strlcat(ir->phys, "/input0", sizeof(ir->phys)); |
| 200 | |
Mauro Carvalho Chehab | 579e7d6 | 2009-12-11 11:20:59 -0300 | [diff] [blame] | 201 | err = ir_input_init(input_dev, &ir->ir, IR_TYPE_OTHER); |
Mauro Carvalho Chehab | 055cd55 | 2009-11-29 08:19:59 -0300 | [diff] [blame] | 202 | if (err < 0) |
| 203 | goto err_out_free; |
| 204 | |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 205 | input_dev->name = ir->name; |
| 206 | input_dev->phys = ir->phys; |
| 207 | input_dev->id.bustype = BUS_USB; |
| 208 | input_dev->id.version = 1; |
| 209 | input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor); |
| 210 | input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct); |
| 211 | |
| 212 | input_dev->dev.parent = &dev->udev->dev; |
| 213 | /* record handles to ourself */ |
| 214 | ir->dev = dev; |
| 215 | dev->ir = ir; |
| 216 | |
| 217 | cx231xx_ir_start(ir); |
| 218 | |
| 219 | /* all done */ |
Mauro Carvalho Chehab | e93854d | 2009-12-14 00:16:55 -0300 | [diff] [blame] | 220 | err = ir_input_register(ir->input, dev->board.ir_codes, NULL); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 221 | if (err) |
| 222 | goto err_out_stop; |
| 223 | |
| 224 | return 0; |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 225 | err_out_stop: |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 226 | cx231xx_ir_stop(ir); |
| 227 | dev->ir = NULL; |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 228 | err_out_free: |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 229 | kfree(ir); |
| 230 | return err; |
| 231 | } |
| 232 | |
| 233 | int cx231xx_ir_fini(struct cx231xx *dev) |
| 234 | { |
| 235 | struct cx231xx_IR *ir = dev->ir; |
| 236 | |
| 237 | /* skip detach on non attached boards */ |
| 238 | if (!ir) |
| 239 | return 0; |
| 240 | |
| 241 | cx231xx_ir_stop(ir); |
Mauro Carvalho Chehab | 38ef6aa | 2009-12-11 09:47:42 -0300 | [diff] [blame] | 242 | ir_input_unregister(ir->input); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 243 | kfree(ir); |
| 244 | |
| 245 | /* done */ |
| 246 | dev->ir = NULL; |
| 247 | return 0; |
| 248 | } |