Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 1 | /* |
Ruslan Pisarev | d0058645 | 2010-10-20 06:35:54 -0300 | [diff] [blame] | 2 | * tm6000-input.c - driver for TM5600/TM6000/TM6010 USB video capture devices |
| 3 | * |
| 4 | * Copyright (C) 2010 Stefan Ringel <stefan.ringel@arcor.de> |
| 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 |
| 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 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/delay.h> |
| 23 | |
| 24 | #include <linux/input.h> |
| 25 | #include <linux/usb.h> |
| 26 | |
Mauro Carvalho Chehab | 6bda964 | 2010-11-17 13:28:38 -0300 | [diff] [blame] | 27 | #include <media/rc-core.h> |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 28 | |
| 29 | #include "tm6000.h" |
| 30 | #include "tm6000-regs.h" |
| 31 | |
| 32 | static unsigned int ir_debug; |
| 33 | module_param(ir_debug, int, 0644); |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 34 | MODULE_PARM_DESC(ir_debug, "debug message level"); |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 35 | |
| 36 | static unsigned int enable_ir = 1; |
| 37 | module_param(enable_ir, int, 0644); |
Stefan Ringel | 401ad27 | 2010-08-24 16:36:09 -0300 | [diff] [blame] | 38 | MODULE_PARM_DESC(enable_ir, "enable ir (default is enable)"); |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 39 | |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 40 | static unsigned int ir_clock_mhz = 12; |
| 41 | module_param(ir_clock_mhz, int, 0644); |
| 42 | MODULE_PARM_DESC(enable_ir, "ir clock, in MHz"); |
| 43 | |
| 44 | #define URB_SUBMIT_DELAY 100 /* ms - Delay to submit an URB request on retrial and init */ |
| 45 | #define URB_INT_LED_DELAY 100 /* ms - Delay to turn led on again on int mode */ |
Dmitri Belimov | 641d211 | 2010-12-22 05:57:46 -0300 | [diff] [blame] | 46 | |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 47 | #undef dprintk |
| 48 | |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 49 | #define dprintk(level, fmt, arg...) do {\ |
| 50 | if (ir_debug >= level) \ |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 51 | printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \ |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 52 | } while (0) |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 53 | |
| 54 | struct tm6000_ir_poll_result { |
Stefan Ringel | 1b376da | 2010-09-09 14:45:22 -0300 | [diff] [blame] | 55 | u16 rc_data; |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | struct tm6000_IR { |
| 59 | struct tm6000_core *dev; |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 60 | struct rc_dev *rc; |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 61 | char name[32]; |
| 62 | char phys[32]; |
| 63 | |
| 64 | /* poll expernal decoder */ |
| 65 | int polling; |
| 66 | struct delayed_work work; |
| 67 | u8 wait:1; |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 68 | u8 pwled:2; |
| 69 | u8 submit_urb:1; |
Dmitri Belimov | 641d211 | 2010-12-22 05:57:46 -0300 | [diff] [blame] | 70 | u16 key_addr; |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 71 | struct urb *int_urb; |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 72 | |
| 73 | /* IR device properties */ |
Mauro Carvalho Chehab | 52b6614 | 2010-11-17 14:20:52 -0300 | [diff] [blame] | 74 | u64 rc_type; |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 75 | }; |
| 76 | |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 77 | void tm6000_ir_wait(struct tm6000_core *dev, u8 state) |
| 78 | { |
| 79 | struct tm6000_IR *ir = dev->ir; |
| 80 | |
| 81 | if (!dev->ir) |
| 82 | return; |
| 83 | |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 84 | dprintk(2, "%s: %i\n",__func__, ir->wait); |
| 85 | |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 86 | if (state) |
| 87 | ir->wait = 1; |
| 88 | else |
| 89 | ir->wait = 0; |
| 90 | } |
| 91 | |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 92 | static int tm6000_ir_config(struct tm6000_IR *ir) |
| 93 | { |
| 94 | struct tm6000_core *dev = ir->dev; |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 95 | u32 pulse = 0, leader = 0; |
| 96 | |
| 97 | dprintk(2, "%s\n",__func__); |
| 98 | |
| 99 | /* |
| 100 | * The IR decoder supports RC-5 or NEC, with a configurable timing. |
| 101 | * The timing configuration there is not that accurate, as it uses |
| 102 | * approximate values. The NEC spec mentions a 562.5 unit period, |
| 103 | * and RC-5 uses a 888.8 period. |
| 104 | * Currently, driver assumes a clock provided by a 12 MHz XTAL, but |
| 105 | * a modprobe parameter can adjust it. |
| 106 | * Adjustments are required for other timings. |
| 107 | * It seems that the 900ms timing for NEC is used to detect a RC-5 |
| 108 | * IR, in order to discard such decoding |
| 109 | */ |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 110 | |
Dmitri Belimov | 641d211 | 2010-12-22 05:57:46 -0300 | [diff] [blame] | 111 | switch (ir->rc_type) { |
| 112 | case RC_TYPE_NEC: |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 113 | leader = 900; /* ms */ |
| 114 | pulse = 700; /* ms - the actual value would be 562 */ |
Dmitri Belimov | 641d211 | 2010-12-22 05:57:46 -0300 | [diff] [blame] | 115 | break; |
| 116 | default: |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 117 | case RC_TYPE_RC5: |
| 118 | leader = 900; /* ms - from the NEC decoding */ |
| 119 | pulse = 1780; /* ms - The actual value would be 1776 */ |
Dmitri Belimov | 641d211 | 2010-12-22 05:57:46 -0300 | [diff] [blame] | 120 | break; |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 121 | } |
Dmitri Belimov | 641d211 | 2010-12-22 05:57:46 -0300 | [diff] [blame] | 122 | |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 123 | pulse = ir_clock_mhz * pulse; |
| 124 | leader = ir_clock_mhz * leader; |
| 125 | if (ir->rc_type == RC_TYPE_NEC) |
| 126 | leader = leader | 0x8000; |
| 127 | |
| 128 | dprintk(2, "%s: %s, %d MHz, leader = 0x%04x, pulse = 0x%06x \n", |
| 129 | __func__, |
| 130 | (ir->rc_type == RC_TYPE_NEC) ? "NEC" : "RC-5", |
| 131 | ir_clock_mhz, leader, pulse); |
| 132 | |
| 133 | /* Remote WAKEUP = enable, normal mode, from IR decoder output */ |
| 134 | tm6000_set_reg(dev, TM6010_REQ07_RE5_REMOTE_WAKEUP, 0xfe); |
| 135 | |
| 136 | /* Enable IR reception on non-busrt mode */ |
| 137 | tm6000_set_reg(dev, TM6010_REQ07_RD8_IR, 0x2f); |
| 138 | |
| 139 | /* IR_WKUP_SEL = Low byte in decoded IR data */ |
| 140 | tm6000_set_reg(dev, TM6010_REQ07_RDA_IR_WAKEUP_SEL, 0xff); |
| 141 | /* IR_WKU_ADD code */ |
| 142 | tm6000_set_reg(dev, TM6010_REQ07_RDB_IR_WAKEUP_ADD, 0xff); |
| 143 | |
| 144 | tm6000_set_reg(dev, TM6010_REQ07_RDC_IR_LEADER1, leader >> 8); |
| 145 | tm6000_set_reg(dev, TM6010_REQ07_RDD_IR_LEADER0, leader); |
| 146 | |
| 147 | tm6000_set_reg(dev, TM6010_REQ07_RDE_IR_PULSE_CNT1, pulse >> 8); |
| 148 | tm6000_set_reg(dev, TM6010_REQ07_RDF_IR_PULSE_CNT0, pulse); |
| 149 | |
| 150 | if (!ir->polling) |
| 151 | tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 0); |
| 152 | else |
| 153 | tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 1); |
| 154 | msleep(10); |
| 155 | |
| 156 | /* Shows that IR is working via the LED */ |
| 157 | tm6000_flash_led(dev, 0); |
| 158 | msleep(100); |
| 159 | tm6000_flash_led(dev, 1); |
| 160 | ir->pwled = 1; |
| 161 | |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | static void tm6000_ir_urb_received(struct urb *urb) |
| 166 | { |
| 167 | struct tm6000_core *dev = urb->context; |
| 168 | struct tm6000_IR *ir = dev->ir; |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 169 | struct tm6000_ir_poll_result poll_result; |
| 170 | char *buf; |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 171 | int rc; |
Mauro Carvalho Chehab | 02c7105 | 2010-07-05 17:09:11 -0300 | [diff] [blame] | 172 | |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 173 | dprintk(2, "%s\n",__func__); |
| 174 | if (urb->status < 0 || urb->actual_length <= 0) { |
| 175 | printk(KERN_INFO "tm6000: IR URB failure: status: %i, length %i\n", |
| 176 | urb->status, urb->actual_length); |
| 177 | ir->submit_urb = 1; |
| 178 | schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY)); |
| 179 | return; |
Stefan Ringel | 1b376da | 2010-09-09 14:45:22 -0300 | [diff] [blame] | 180 | } |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 181 | buf = urb->transfer_buffer; |
| 182 | |
| 183 | if (ir_debug) |
| 184 | print_hex_dump(KERN_DEBUG, "tm6000: IR data: ", |
| 185 | DUMP_PREFIX_OFFSET,16, 1, |
| 186 | buf, urb->actual_length, false); |
| 187 | |
| 188 | poll_result.rc_data = buf[0]; |
| 189 | if (urb->actual_length > 1) |
| 190 | poll_result.rc_data |= buf[1] << 8; |
| 191 | |
| 192 | dprintk(1, "%s, scancode: 0x%04x\n",__func__, poll_result.rc_data); |
| 193 | rc_keydown(ir->rc, poll_result.rc_data, 0); |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 194 | |
| 195 | rc = usb_submit_urb(urb, GFP_ATOMIC); |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 196 | /* |
| 197 | * Flash the led. We can't do it here, as it is running on IRQ context. |
| 198 | * So, use the scheduler to do it, in a few ms. |
| 199 | */ |
| 200 | ir->pwled = 2; |
| 201 | schedule_delayed_work(&ir->work, msecs_to_jiffies(10)); |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 202 | } |
| 203 | |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 204 | static void tm6000_ir_handle_key(struct work_struct *work) |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 205 | { |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 206 | struct tm6000_IR *ir = container_of(work, struct tm6000_IR, work.work); |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 207 | struct tm6000_core *dev = ir->dev; |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 208 | struct tm6000_ir_poll_result poll_result; |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 209 | int rc; |
| 210 | u8 buf[2]; |
| 211 | |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 212 | if (ir->wait) |
| 213 | return; |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 214 | |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 215 | dprintk(3, "%s\n",__func__); |
| 216 | |
| 217 | rc = tm6000_read_write_usb(dev, USB_DIR_IN | |
| 218 | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 219 | REQ_02_GET_IR_CODE, 0, 0, buf, 2); |
| 220 | if (rc < 0) |
| 221 | return; |
| 222 | |
| 223 | if (rc > 1) |
| 224 | poll_result.rc_data = buf[0] | buf[1] << 8; |
| 225 | else |
| 226 | poll_result.rc_data = buf[0]; |
| 227 | |
| 228 | /* Check if something was read */ |
| 229 | if ((poll_result.rc_data & 0xff) == 0xff) { |
| 230 | if (!ir->pwled) { |
| 231 | tm6000_flash_led(dev, 1); |
| 232 | ir->pwled = 1; |
Dmitri Belimov | 641d211 | 2010-12-22 05:57:46 -0300 | [diff] [blame] | 233 | } |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 234 | return; |
| 235 | } |
| 236 | |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 237 | dprintk(1, "%s, scancode: 0x%04x\n",__func__, poll_result.rc_data); |
| 238 | rc_keydown(ir->rc, poll_result.rc_data, 0); |
| 239 | tm6000_flash_led(dev, 0); |
| 240 | ir->pwled = 0; |
Dmitri Belimov | 641d211 | 2010-12-22 05:57:46 -0300 | [diff] [blame] | 241 | |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 242 | /* Re-schedule polling */ |
| 243 | schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling)); |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 244 | } |
| 245 | |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 246 | static void tm6000_ir_int_work(struct work_struct *work) |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 247 | { |
| 248 | struct tm6000_IR *ir = container_of(work, struct tm6000_IR, work.work); |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 249 | struct tm6000_core *dev = ir->dev; |
| 250 | int rc; |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 251 | |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 252 | dprintk(3, "%s, submit_urb = %d, pwled = %d\n",__func__, ir->submit_urb, |
| 253 | ir->pwled); |
| 254 | |
| 255 | if (ir->submit_urb) { |
| 256 | dprintk(3, "Resubmit urb\n"); |
| 257 | tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 0); |
| 258 | |
| 259 | rc = usb_submit_urb(ir->int_urb, GFP_ATOMIC); |
| 260 | if (rc < 0) { |
| 261 | printk(KERN_ERR "tm6000: Can't submit an IR interrupt. Error %i\n", |
| 262 | rc); |
| 263 | /* Retry in 100 ms */ |
| 264 | schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY)); |
| 265 | return; |
| 266 | } |
| 267 | ir->submit_urb = 0; |
| 268 | } |
| 269 | |
| 270 | /* Led is enabled only if USB submit doesn't fail */ |
| 271 | if (ir->pwled == 2) { |
| 272 | tm6000_flash_led(dev, 0); |
| 273 | ir->pwled = 0; |
| 274 | schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_INT_LED_DELAY)); |
| 275 | } else if (!ir->pwled) { |
| 276 | tm6000_flash_led(dev, 1); |
| 277 | ir->pwled = 1; |
| 278 | } |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 279 | } |
| 280 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 281 | static int tm6000_ir_start(struct rc_dev *rc) |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 282 | { |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 283 | struct tm6000_IR *ir = rc->priv; |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 284 | |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 285 | dprintk(2, "%s\n",__func__); |
| 286 | |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 287 | schedule_delayed_work(&ir->work, 0); |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 292 | static void tm6000_ir_stop(struct rc_dev *rc) |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 293 | { |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 294 | struct tm6000_IR *ir = rc->priv; |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 295 | |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 296 | dprintk(2, "%s\n",__func__); |
| 297 | |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 298 | cancel_delayed_work_sync(&ir->work); |
| 299 | } |
| 300 | |
Thierry Reding | 3d1a51d | 2011-08-04 04:14:01 -0300 | [diff] [blame] | 301 | static int tm6000_ir_change_protocol(struct rc_dev *rc, u64 rc_type) |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 302 | { |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 303 | struct tm6000_IR *ir = rc->priv; |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 304 | |
Dmitri Belimov | 641d211 | 2010-12-22 05:57:46 -0300 | [diff] [blame] | 305 | if (!ir) |
| 306 | return 0; |
| 307 | |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 308 | dprintk(2, "%s\n",__func__); |
| 309 | |
Dmitri Belimov | 641d211 | 2010-12-22 05:57:46 -0300 | [diff] [blame] | 310 | if ((rc->rc_map.scan) && (rc_type == RC_TYPE_NEC)) |
| 311 | ir->key_addr = ((rc->rc_map.scan[0].scancode >> 8) & 0xffff); |
| 312 | |
Dmitri Belimov | 641d211 | 2010-12-22 05:57:46 -0300 | [diff] [blame] | 313 | ir->rc_type = rc_type; |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 314 | |
| 315 | tm6000_ir_config(ir); |
| 316 | /* TODO */ |
| 317 | return 0; |
| 318 | } |
| 319 | |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 320 | static int __tm6000_ir_int_start(struct rc_dev *rc) |
Dmitri Belimov | 641d211 | 2010-12-22 05:57:46 -0300 | [diff] [blame] | 321 | { |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 322 | struct tm6000_IR *ir = rc->priv; |
| 323 | struct tm6000_core *dev = ir->dev; |
Dmitri Belimov | 641d211 | 2010-12-22 05:57:46 -0300 | [diff] [blame] | 324 | int pipe, size; |
| 325 | int err = -ENOMEM; |
| 326 | |
Dmitri Belimov | 641d211 | 2010-12-22 05:57:46 -0300 | [diff] [blame] | 327 | if (!ir) |
| 328 | return -ENODEV; |
| 329 | |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 330 | dprintk(2, "%s\n",__func__); |
| 331 | |
| 332 | ir->int_urb = usb_alloc_urb(0, GFP_ATOMIC); |
Dan Carpenter | 2591418 | 2011-01-03 08:44:38 +0300 | [diff] [blame] | 333 | if (!ir->int_urb) |
| 334 | return -ENOMEM; |
Dmitri Belimov | 641d211 | 2010-12-22 05:57:46 -0300 | [diff] [blame] | 335 | |
| 336 | pipe = usb_rcvintpipe(dev->udev, |
| 337 | dev->int_in.endp->desc.bEndpointAddress |
| 338 | & USB_ENDPOINT_NUMBER_MASK); |
| 339 | |
| 340 | size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe)); |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 341 | dprintk(1, "IR max size: %d\n", size); |
Dmitri Belimov | 641d211 | 2010-12-22 05:57:46 -0300 | [diff] [blame] | 342 | |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 343 | ir->int_urb->transfer_buffer = kzalloc(size, GFP_ATOMIC); |
Dmitri Belimov | 641d211 | 2010-12-22 05:57:46 -0300 | [diff] [blame] | 344 | if (ir->int_urb->transfer_buffer == NULL) { |
| 345 | usb_free_urb(ir->int_urb); |
| 346 | return err; |
| 347 | } |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 348 | dprintk(1, "int interval: %d\n", dev->int_in.endp->desc.bInterval); |
| 349 | |
Dmitri Belimov | 641d211 | 2010-12-22 05:57:46 -0300 | [diff] [blame] | 350 | usb_fill_int_urb(ir->int_urb, dev->udev, pipe, |
| 351 | ir->int_urb->transfer_buffer, size, |
| 352 | tm6000_ir_urb_received, dev, |
| 353 | dev->int_in.endp->desc.bInterval); |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 354 | |
| 355 | ir->submit_urb = 1; |
| 356 | schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY)); |
Dmitri Belimov | 641d211 | 2010-12-22 05:57:46 -0300 | [diff] [blame] | 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 361 | static void __tm6000_ir_int_stop(struct rc_dev *rc) |
| 362 | { |
| 363 | struct tm6000_IR *ir = rc->priv; |
| 364 | |
| 365 | if (!ir || !ir->int_urb) |
| 366 | return; |
| 367 | |
| 368 | dprintk(2, "%s\n",__func__); |
| 369 | |
| 370 | usb_kill_urb(ir->int_urb); |
| 371 | kfree(ir->int_urb->transfer_buffer); |
| 372 | usb_free_urb(ir->int_urb); |
| 373 | ir->int_urb = NULL; |
| 374 | } |
| 375 | |
| 376 | int tm6000_ir_int_start(struct tm6000_core *dev) |
| 377 | { |
| 378 | struct tm6000_IR *ir = dev->ir; |
| 379 | |
| 380 | return __tm6000_ir_int_start(ir->rc); |
| 381 | } |
| 382 | |
Dmitri Belimov | 641d211 | 2010-12-22 05:57:46 -0300 | [diff] [blame] | 383 | void tm6000_ir_int_stop(struct tm6000_core *dev) |
| 384 | { |
| 385 | struct tm6000_IR *ir = dev->ir; |
| 386 | |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 387 | __tm6000_ir_int_stop(ir->rc); |
Dmitri Belimov | 641d211 | 2010-12-22 05:57:46 -0300 | [diff] [blame] | 388 | } |
| 389 | |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 390 | int tm6000_ir_init(struct tm6000_core *dev) |
| 391 | { |
| 392 | struct tm6000_IR *ir; |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 393 | struct rc_dev *rc; |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 394 | int err = -ENOMEM; |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 395 | |
| 396 | if (!enable_ir) |
| 397 | return -ENODEV; |
| 398 | |
| 399 | if (!dev->caps.has_remote) |
| 400 | return 0; |
| 401 | |
| 402 | if (!dev->ir_codes) |
| 403 | return 0; |
| 404 | |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 405 | dprintk(2, "%s\n",__func__); |
| 406 | |
| 407 | ir = kzalloc(sizeof(*ir), GFP_ATOMIC); |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 408 | rc = rc_allocate_device(); |
Dan Carpenter | b01627c | 2011-01-03 08:47:40 +0300 | [diff] [blame] | 409 | if (!ir || !rc) |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 410 | goto out; |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 411 | |
| 412 | /* record handles to ourself */ |
| 413 | ir->dev = dev; |
| 414 | dev->ir = ir; |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 415 | ir->rc = rc; |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 416 | |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 417 | /* input setup */ |
Mauro Carvalho Chehab | 52b6614 | 2010-11-17 14:20:52 -0300 | [diff] [blame] | 418 | rc->allowed_protos = RC_TYPE_RC5 | RC_TYPE_NEC; |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 419 | rc->priv = ir; |
| 420 | rc->change_protocol = tm6000_ir_change_protocol; |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 421 | if (&dev->int_in) { |
| 422 | rc->open = __tm6000_ir_int_start; |
| 423 | rc->close = __tm6000_ir_int_stop; |
| 424 | INIT_DELAYED_WORK(&ir->work, tm6000_ir_int_work); |
| 425 | } else { |
| 426 | rc->open = tm6000_ir_start; |
| 427 | rc->close = tm6000_ir_stop; |
| 428 | ir->polling = 50; |
| 429 | INIT_DELAYED_WORK(&ir->work, tm6000_ir_handle_key); |
| 430 | } |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 431 | rc->driver_type = RC_DRIVER_SCANCODE; |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 432 | |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 433 | snprintf(ir->name, sizeof(ir->name), "tm5600/60x0 IR (%s)", |
| 434 | dev->name); |
| 435 | |
| 436 | usb_make_path(dev->udev, ir->phys, sizeof(ir->phys)); |
| 437 | strlcat(ir->phys, "/input0", sizeof(ir->phys)); |
| 438 | |
Mauro Carvalho Chehab | 52b6614 | 2010-11-17 14:20:52 -0300 | [diff] [blame] | 439 | tm6000_ir_change_protocol(rc, RC_TYPE_UNKNOWN); |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 440 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 441 | rc->input_name = ir->name; |
| 442 | rc->input_phys = ir->phys; |
| 443 | rc->input_id.bustype = BUS_USB; |
| 444 | rc->input_id.version = 1; |
| 445 | rc->input_id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor); |
| 446 | rc->input_id.product = le16_to_cpu(dev->udev->descriptor.idProduct); |
| 447 | rc->map_name = dev->ir_codes; |
| 448 | rc->driver_name = "tm6000"; |
| 449 | rc->dev.parent = &dev->udev->dev; |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 450 | |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 451 | /* ir register */ |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 452 | err = rc_register_device(rc); |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 453 | if (err) |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 454 | goto out; |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 455 | |
| 456 | return 0; |
| 457 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 458 | out: |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 459 | dev->ir = NULL; |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 460 | rc_free_device(rc); |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 461 | kfree(ir); |
| 462 | return err; |
| 463 | } |
| 464 | |
| 465 | int tm6000_ir_fini(struct tm6000_core *dev) |
| 466 | { |
| 467 | struct tm6000_IR *ir = dev->ir; |
| 468 | |
| 469 | /* skip detach on non attached board */ |
| 470 | |
| 471 | if (!ir) |
| 472 | return 0; |
| 473 | |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 474 | dprintk(2, "%s\n",__func__); |
| 475 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 476 | rc_unregister_device(ir->rc); |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 477 | |
Mauro Carvalho Chehab | 4a83b01 | 2011-11-29 11:30:30 -0300 | [diff] [blame^] | 478 | if (!ir->polling) |
| 479 | __tm6000_ir_int_stop(ir->rc); |
| 480 | |
| 481 | tm6000_ir_stop(ir->rc); |
| 482 | |
| 483 | /* Turn off the led */ |
| 484 | tm6000_flash_led(dev, 0); |
| 485 | ir->pwled = 0; |
| 486 | |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 487 | |
Stefan Ringel | d064f96 | 2010-06-20 17:16:52 -0300 | [diff] [blame] | 488 | kfree(ir); |
| 489 | dev->ir = NULL; |
| 490 | |
| 491 | return 0; |
| 492 | } |