blob: edcd6978f2e109d958af390756c8a1ae85e935bd [file] [log] [blame]
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001/*
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08002 handle em28xx IR remotes via linux kernel input layer.
3
4 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5 Markus Rechberger <mrechberger@gmail.com>
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -03006 Mauro Carvalho Chehab <mchehab@infradead.org>
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08007 Sascha Sommer <saschasommer@freenet.de>
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
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -080022 */
23
24#include <linux/module.h>
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -080025#include <linux/init.h>
26#include <linux/delay.h>
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -080027#include <linux/interrupt.h>
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -080028#include <linux/usb.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -080030
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080031#include "em28xx.h"
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -080032
Devin Heitmuellera9fc52b2008-06-28 08:57:06 -030033#define EM28XX_SNAPSHOT_KEY KEY_CAMERA
34#define EM28XX_SBUTTON_QUERY_INTERVAL 500
35#define EM28XX_R0C_USBSUSP_SNAPSHOT 0x20
36
Mauro Carvalho Chehabc8793b02008-01-13 15:42:17 -030037static unsigned int ir_debug;
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -080038module_param(ir_debug, int, 0644);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030039MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -080040
Mauro Carvalho Chehab727e6252010-03-12 21:18:14 -030041#define MODULE_NAME "em28xx"
42
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -030043#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 em28xx IR's
50 **********************************************************/
51
Devin Heitmueller4b922532008-11-13 03:15:55 -030052struct em28xx_ir_poll_result {
53 unsigned int toggle_bit:1;
54 unsigned int read_count:7;
Mauro Carvalho Chehab105e3682012-12-15 08:29:11 -030055
56 u32 scancode;
Devin Heitmueller4b922532008-11-13 03:15:55 -030057};
58
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -030059struct em28xx_IR {
60 struct em28xx *dev;
David Härdemand8b4b582010-10-29 16:08:23 -030061 struct rc_dev *rc;
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -030062 char name[32];
63 char phys[32];
64
65 /* poll external decoder */
66 int polling;
Jean Delvaref263bac2009-03-07 07:43:01 -030067 struct delayed_work work;
Mauro Carvalho Chehab02781552009-11-27 23:28:40 -030068 unsigned int full_code:1;
Devin Heitmueller4b922532008-11-13 03:15:55 -030069 unsigned int last_readcount;
Mauro Carvalho Chehab105e3682012-12-15 08:29:11 -030070 u64 rc_type;
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -030071
Devin Heitmueller4b922532008-11-13 03:15:55 -030072 int (*get_key)(struct em28xx_IR *, struct em28xx_ir_poll_result *);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -030073};
74
75/**********************************************************
76 I2C IR based get keycodes - should be used with ir-kbd-i2c
77 **********************************************************/
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -080078
Ezequiel García769af212012-03-26 09:13:34 -030079static int em28xx_get_key_terratec(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
Markus Rechbergere43f14a2005-11-08 21:37:35 -080080{
81 unsigned char b;
82
83 /* poll IR chip */
Frank Schaefer62ec3f82013-01-13 10:20:40 -030084 if (1 != i2c_master_recv(ir->c, &b, 1))
Markus Rechbergere43f14a2005-11-08 21:37:35 -080085 return -EIO;
Markus Rechbergere43f14a2005-11-08 21:37:35 -080086
87 /* it seems that 0xFE indicates that a button is still hold
Mauro Carvalho Chehab4f9c05a2005-11-08 21:37:36 -080088 down, while 0xff indicates that no button is hold
89 down. 0xfe sequences are sometimes interrupted by 0xFF */
Markus Rechbergere43f14a2005-11-08 21:37:35 -080090
Mauro Carvalho Chehab4f9c05a2005-11-08 21:37:36 -080091 if (b == 0xff)
Markus Rechbergere43f14a2005-11-08 21:37:35 -080092 return 0;
93
Mauro Carvalho Chehab4f9c05a2005-11-08 21:37:36 -080094 if (b == 0xfe)
Markus Rechbergere43f14a2005-11-08 21:37:35 -080095 /* keep old data */
96 return 1;
97
98 *ir_key = b;
99 *ir_raw = b;
100 return 1;
101}
102
Ezequiel García769af212012-03-26 09:13:34 -0300103static int em28xx_get_key_em_haup(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -0800104{
105 unsigned char buf[2];
Mauro Carvalho Chehabb7799742009-12-05 23:24:50 -0300106 u16 code;
107 int size;
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -0800108
109 /* poll IR chip */
Mauro Carvalho Chehabb7799742009-12-05 23:24:50 -0300110 size = i2c_master_recv(ir->c, buf, sizeof(buf));
111
112 if (size != 2)
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -0800113 return -EIO;
114
115 /* Does eliminate repeated parity code */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300116 if (buf[1] == 0xff)
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -0800117 return 0;
118
Mauro Carvalho Chehabb7799742009-12-05 23:24:50 -0300119 /*
120 * Rearranges bits to the right order.
121 * The bit order were determined experimentally by using
122 * The original Hauppauge Grey IR and another RC5 that uses addr=0x08
123 * The RC5 code has 14 bits, but we've experimentally determined
124 * the meaning for only 11 bits.
125 * So, the code translation is not complete. Yet, it is enough to
126 * work with the provided RC5 IR.
127 */
128 code =
129 ((buf[0] & 0x01) ? 0x0020 : 0) | /* 0010 0000 */
130 ((buf[0] & 0x02) ? 0x0010 : 0) | /* 0001 0000 */
131 ((buf[0] & 0x04) ? 0x0008 : 0) | /* 0000 1000 */
132 ((buf[0] & 0x08) ? 0x0004 : 0) | /* 0000 0100 */
133 ((buf[0] & 0x10) ? 0x0002 : 0) | /* 0000 0010 */
134 ((buf[0] & 0x20) ? 0x0001 : 0) | /* 0000 0001 */
135 ((buf[1] & 0x08) ? 0x1000 : 0) | /* 0001 0000 */
136 ((buf[1] & 0x10) ? 0x0800 : 0) | /* 0000 1000 */
137 ((buf[1] & 0x20) ? 0x0400 : 0) | /* 0000 0100 */
138 ((buf[1] & 0x40) ? 0x0200 : 0) | /* 0000 0010 */
139 ((buf[1] & 0x80) ? 0x0100 : 0); /* 0000 0001 */
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -0800140
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -0800141 /* return key */
142 *ir_key = code;
143 *ir_raw = code;
144 return 1;
145}
146
Ezequiel García769af212012-03-26 09:13:34 -0300147static int em28xx_get_key_pinnacle_usb_grey(struct IR_i2c *ir, u32 *ir_key,
Mauro Carvalho Chehabc8793b02008-01-13 15:42:17 -0300148 u32 *ir_raw)
Markus Rechberger366cc642006-01-15 21:04:27 -0200149{
150 unsigned char buf[3];
151
152 /* poll IR chip */
153
Frank Schaefer62ec3f82013-01-13 10:20:40 -0300154 if (3 != i2c_master_recv(ir->c, buf, 3))
Markus Rechberger366cc642006-01-15 21:04:27 -0200155 return -EIO;
Markus Rechberger366cc642006-01-15 21:04:27 -0200156
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300157 if (buf[0] != 0x00)
Markus Rechberger366cc642006-01-15 21:04:27 -0200158 return 0;
Markus Rechberger366cc642006-01-15 21:04:27 -0200159
160 *ir_key = buf[2]&0x3f;
161 *ir_raw = buf[2]&0x3f;
162
163 return 1;
164}
165
Ezequiel García769af212012-03-26 09:13:34 -0300166static int em28xx_get_key_winfast_usbii_deluxe(struct IR_i2c *ir, u32 *ir_key,
167 u32 *ir_raw)
Magnus Almca39d842009-11-13 05:48:24 -0300168{
169 unsigned char subaddr, keydetect, key;
170
171 struct i2c_msg msg[] = { { .addr = ir->c->addr, .flags = 0, .buf = &subaddr, .len = 1},
172
173 { .addr = ir->c->addr, .flags = I2C_M_RD, .buf = &keydetect, .len = 1} };
174
175 subaddr = 0x10;
Frank Schaefer62ec3f82013-01-13 10:20:40 -0300176 if (2 != i2c_transfer(ir->c->adapter, msg, 2))
Magnus Almca39d842009-11-13 05:48:24 -0300177 return -EIO;
Magnus Almca39d842009-11-13 05:48:24 -0300178 if (keydetect == 0x00)
179 return 0;
180
181 subaddr = 0x00;
182 msg[1].buf = &key;
Frank Schaefer62ec3f82013-01-13 10:20:40 -0300183 if (2 != i2c_transfer(ir->c->adapter, msg, 2))
184 return -EIO;
Magnus Almca39d842009-11-13 05:48:24 -0300185 if (key == 0x00)
186 return 0;
187
188 *ir_key = key;
189 *ir_raw = key;
190 return 1;
191}
192
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300193/**********************************************************
194 Poll based get keycode functions
195 **********************************************************/
196
Devin Heitmueller4b922532008-11-13 03:15:55 -0300197/* This is for the em2860/em2880 */
198static int default_polling_getkey(struct em28xx_IR *ir,
199 struct em28xx_ir_poll_result *poll_result)
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300200{
201 struct em28xx *dev = ir->dev;
202 int rc;
Devin Heitmueller4b922532008-11-13 03:15:55 -0300203 u8 msg[3] = { 0, 0, 0 };
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300204
Mauro Carvalho Chehab0a6b8a82008-11-12 18:46:01 -0300205 /* Read key toggle, brand, and key code
206 on registers 0x45, 0x46 and 0x47
207 */
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300208 rc = dev->em28xx_read_reg_req_len(dev, 0, EM28XX_R45_IR,
Mauro Carvalho Chehab0a6b8a82008-11-12 18:46:01 -0300209 msg, sizeof(msg));
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300210 if (rc < 0)
211 return rc;
212
Devin Heitmueller4b922532008-11-13 03:15:55 -0300213 /* Infrared toggle (Reg 0x45[7]) */
214 poll_result->toggle_bit = (msg[0] >> 7);
215
216 /* Infrared read count (Reg 0x45[6:0] */
217 poll_result->read_count = (msg[0] & 0x7f);
218
Mauro Carvalho Chehab105e3682012-12-15 08:29:11 -0300219 /* Remote Control Address/Data (Regs 0x46/0x47) */
220 poll_result->scancode = msg[1] << 8 | msg[2];
Devin Heitmueller4b922532008-11-13 03:15:55 -0300221
222 return 0;
223}
224
225static int em2874_polling_getkey(struct em28xx_IR *ir,
226 struct em28xx_ir_poll_result *poll_result)
227{
228 struct em28xx *dev = ir->dev;
229 int rc;
230 u8 msg[5] = { 0, 0, 0, 0, 0 };
231
232 /* Read key toggle, brand, and key code
233 on registers 0x51-55
234 */
235 rc = dev->em28xx_read_reg_req_len(dev, 0, EM2874_R51_IR,
236 msg, sizeof(msg));
237 if (rc < 0)
238 return rc;
239
240 /* Infrared toggle (Reg 0x51[7]) */
241 poll_result->toggle_bit = (msg[0] >> 7);
242
243 /* Infrared read count (Reg 0x51[6:0] */
244 poll_result->read_count = (msg[0] & 0x7f);
245
Mauro Carvalho Chehab105e3682012-12-15 08:29:11 -0300246 /*
247 * Remote Control Address (Reg 0x52)
248 * Remote Control Data (Reg 0x53-0x55)
249 */
250 switch (ir->rc_type) {
251 case RC_BIT_RC5:
252 poll_result->scancode = msg[1] << 8 | msg[2];
253 break;
254 case RC_BIT_NEC:
255 if ((msg[3] ^ msg[4]) != 0xff) /* 32 bits NEC */
256 poll_result->scancode = (msg[1] << 24) |
257 (msg[2] << 16) |
258 (msg[3] << 8) |
259 msg[4];
260 else if ((msg[1] ^ msg[2]) != 0xff) /* 24 bits NEC */
261 poll_result->scancode = (msg[1] << 16) |
262 (msg[2] << 8) |
263 msg[3];
264 else /* Normal NEC */
265 poll_result->scancode = msg[1] << 8 | msg[3];
266 break;
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300267 case RC_BIT_RC6_0:
268 poll_result->scancode = msg[1] << 8 | msg[2];
269 break;
Mauro Carvalho Chehab105e3682012-12-15 08:29:11 -0300270 default:
271 poll_result->scancode = (msg[1] << 24) | (msg[2] << 16) |
272 (msg[3] << 8) | msg[4];
273 break;
274 }
Devin Heitmueller4b922532008-11-13 03:15:55 -0300275
276 return 0;
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300277}
278
279/**********************************************************
280 Polling code for em28xx
281 **********************************************************/
282
283static void em28xx_ir_handle_key(struct em28xx_IR *ir)
284{
Devin Heitmueller4b922532008-11-13 03:15:55 -0300285 int result;
Devin Heitmueller4b922532008-11-13 03:15:55 -0300286 struct em28xx_ir_poll_result poll_result;
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300287
Devin Heitmueller4b922532008-11-13 03:15:55 -0300288 /* read the registers containing the IR status */
289 result = ir->get_key(ir, &poll_result);
Mauro Carvalho Chehab603044d2010-06-27 08:32:11 -0300290 if (unlikely(result < 0)) {
Devin Heitmueller4b922532008-11-13 03:15:55 -0300291 dprintk("ir->get_key() failed %d\n", result);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300292 return;
Devin Heitmueller4b922532008-11-13 03:15:55 -0300293 }
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300294
Mauro Carvalho Chehab603044d2010-06-27 08:32:11 -0300295 if (unlikely(poll_result.read_count != ir->last_readcount)) {
Mauro Carvalho Chehab105e3682012-12-15 08:29:11 -0300296 dprintk("%s: toggle: %d, count: %d, key 0x%04x\n", __func__,
Mauro Carvalho Chehab603044d2010-06-27 08:32:11 -0300297 poll_result.toggle_bit, poll_result.read_count,
Mauro Carvalho Chehab105e3682012-12-15 08:29:11 -0300298 poll_result.scancode);
David Härdemana4695852010-06-07 16:32:23 -0300299 if (ir->full_code)
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300300 rc_keydown(ir->rc,
Mauro Carvalho Chehab105e3682012-12-15 08:29:11 -0300301 poll_result.scancode,
David Härdemana4695852010-06-07 16:32:23 -0300302 poll_result.toggle_bit);
303 else
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300304 rc_keydown(ir->rc,
Mauro Carvalho Chehab105e3682012-12-15 08:29:11 -0300305 poll_result.scancode & 0xff,
David Härdemana4695852010-06-07 16:32:23 -0300306 poll_result.toggle_bit);
David Härdemana4695852010-06-07 16:32:23 -0300307
Mauro Carvalho Chehab20ae9742011-11-20 12:23:54 -0200308 if (ir->dev->chip_id == CHIP_ID_EM2874 ||
309 ir->dev->chip_id == CHIP_ID_EM2884)
Mauro Carvalho Chehab603044d2010-06-27 08:32:11 -0300310 /* The em2874 clears the readcount field every time the
311 register is read. The em2860/2880 datasheet says that it
312 is supposed to clear the readcount, but it doesn't. So with
313 the em2874, we are looking for a non-zero read count as
314 opposed to a readcount that is incrementing */
315 ir->last_readcount = 0;
316 else
317 ir->last_readcount = poll_result.read_count;
318 }
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300319}
320
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300321static void em28xx_ir_work(struct work_struct *work)
322{
Jean Delvaref263bac2009-03-07 07:43:01 -0300323 struct em28xx_IR *ir = container_of(work, struct em28xx_IR, work.work);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300324
325 em28xx_ir_handle_key(ir);
Jean Delvaref263bac2009-03-07 07:43:01 -0300326 schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300327}
328
David Härdemand8b4b582010-10-29 16:08:23 -0300329static int em28xx_ir_start(struct rc_dev *rc)
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300330{
David Härdemand8b4b582010-10-29 16:08:23 -0300331 struct em28xx_IR *ir = rc->priv;
Mauro Carvalho Chehabc373cab2010-04-07 22:57:04 -0300332
Jean Delvaref263bac2009-03-07 07:43:01 -0300333 INIT_DELAYED_WORK(&ir->work, em28xx_ir_work);
334 schedule_delayed_work(&ir->work, 0);
Mauro Carvalho Chehabc373cab2010-04-07 22:57:04 -0300335
336 return 0;
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300337}
338
David Härdemand8b4b582010-10-29 16:08:23 -0300339static void em28xx_ir_stop(struct rc_dev *rc)
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300340{
David Härdemand8b4b582010-10-29 16:08:23 -0300341 struct em28xx_IR *ir = rc->priv;
Mauro Carvalho Chehabc373cab2010-04-07 22:57:04 -0300342
Jean Delvaref263bac2009-03-07 07:43:01 -0300343 cancel_delayed_work_sync(&ir->work);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300344}
345
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300346static int em2860_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_type)
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300347{
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300348 struct em28xx_IR *ir = rc_dev->priv;
349 struct em28xx *dev = ir->dev;
350
351 /* Adjust xclk based on IR table for RC5/NEC tables */
352 if (*rc_type & RC_BIT_RC5) {
353 dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
354 ir->full_code = 1;
355 *rc_type = RC_BIT_RC5;
356 } else if (*rc_type & RC_BIT_NEC) {
357 dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
358 ir->full_code = 1;
359 *rc_type = RC_BIT_NEC;
360 } else if (*rc_type & RC_BIT_UNKNOWN) {
361 *rc_type = RC_BIT_UNKNOWN;
362 } else {
363 *rc_type = ir->rc_type;
364 return -EINVAL;
365 }
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300366 em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
367 EM28XX_XCLK_IR_RC5_MODE);
368
369 ir->rc_type = *rc_type;
370
371 return 0;
372}
373
374static int em2874_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_type)
375{
David Härdemand8b4b582010-10-29 16:08:23 -0300376 struct em28xx_IR *ir = rc_dev->priv;
Mauro Carvalho Chehab950b0f52009-12-14 02:54:22 -0300377 struct em28xx *dev = ir->dev;
378 u8 ir_config = EM2874_IR_RC5;
Mauro Carvalho Chehab1bad4292009-12-05 08:27:49 -0300379
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300380 /* Adjust xclk and set type based on IR table for RC5/NEC/RC6 tables */
David Härdemanc003ab12012-10-11 19:11:54 -0300381 if (*rc_type & RC_BIT_RC5) {
Mauro Carvalho Chehab1bad4292009-12-05 08:27:49 -0300382 dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
383 ir->full_code = 1;
David Härdemanc003ab12012-10-11 19:11:54 -0300384 *rc_type = RC_BIT_RC5;
385 } else if (*rc_type & RC_BIT_NEC) {
Mauro Carvalho Chehab1bad4292009-12-05 08:27:49 -0300386 dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
Mauro Carvalho Chehab105e3682012-12-15 08:29:11 -0300387 ir_config = EM2874_IR_NEC | EM2874_IR_NEC_NO_PARITY;
Mauro Carvalho Chehab1bad4292009-12-05 08:27:49 -0300388 ir->full_code = 1;
David Härdemanc003ab12012-10-11 19:11:54 -0300389 *rc_type = RC_BIT_NEC;
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300390 } else if (*rc_type & RC_BIT_RC6_0) {
391 dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
392 ir_config = EM2874_IR_RC6_MODE_0;
393 ir->full_code = 1;
394 *rc_type = RC_BIT_RC6_0;
395 } else if (*rc_type & RC_BIT_UNKNOWN) {
396 *rc_type = RC_BIT_UNKNOWN;
397 } else {
398 *rc_type = ir->rc_type;
399 return -EINVAL;
400 }
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300401 em28xx_write_regs(dev, EM2874_R50_IR_CONFIG, &ir_config, 1);
Mauro Carvalho Chehab1bad4292009-12-05 08:27:49 -0300402 em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
403 EM28XX_XCLK_IR_RC5_MODE);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300404
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300405 ir->rc_type = *rc_type;
406
407 return 0;
408}
409static int em28xx_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_type)
410{
411 struct em28xx_IR *ir = rc_dev->priv;
412 struct em28xx *dev = ir->dev;
413
Devin Heitmueller4b922532008-11-13 03:15:55 -0300414 /* Setup the proper handler based on the chip */
415 switch (dev->chip_id) {
416 case CHIP_ID_EM2860:
417 case CHIP_ID_EM2883:
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300418 return em2860_ir_change_protocol(rc_dev, rc_type);
Mauro Carvalho Chehab20ae9742011-11-20 12:23:54 -0200419 case CHIP_ID_EM2884:
Devin Heitmueller4b922532008-11-13 03:15:55 -0300420 case CHIP_ID_EM2874:
Antti Palosaaria24ec442011-05-25 16:56:37 -0300421 case CHIP_ID_EM28174:
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300422 return em2874_ir_change_protocol(rc_dev, rc_type);
Devin Heitmueller4b922532008-11-13 03:15:55 -0300423 default:
Mauro Carvalho Chehab20ae9742011-11-20 12:23:54 -0200424 printk("Unrecognized em28xx chip id 0x%02x: IR not supported\n",
425 dev->chip_id);
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300426 return -EINVAL;
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300427 }
Mauro Carvalho Chehab950b0f52009-12-14 02:54:22 -0300428}
429
Ezequiel García769af212012-03-26 09:13:34 -0300430static void em28xx_register_i2c_ir(struct em28xx *dev)
Ezequiel García9d9f4792012-03-26 09:13:33 -0300431{
432 /* Leadtek winfast tv USBII deluxe can find a non working IR-device */
433 /* at address 0x18, so if that address is needed for another board in */
434 /* the future, please put it after 0x1f. */
435 struct i2c_board_info info;
436 const unsigned short addr_list[] = {
437 0x1f, 0x30, 0x47, I2C_CLIENT_END
438 };
439
440 memset(&info, 0, sizeof(struct i2c_board_info));
441 memset(&dev->init_data, 0, sizeof(dev->init_data));
442 strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
443
444 /* detect & configure */
445 switch (dev->model) {
446 case EM2800_BOARD_TERRATEC_CINERGY_200:
447 case EM2820_BOARD_TERRATEC_CINERGY_250:
448 dev->init_data.ir_codes = RC_MAP_EM_TERRATEC;
449 dev->init_data.get_key = em28xx_get_key_terratec;
Mauro Carvalho Chehabd40580e2013-01-04 17:37:26 -0300450 dev->init_data.name = "Terratec Cinergy 200/250";
Ezequiel García9d9f4792012-03-26 09:13:33 -0300451 break;
452 case EM2820_BOARD_PINNACLE_USB_2:
453 dev->init_data.ir_codes = RC_MAP_PINNACLE_GREY;
454 dev->init_data.get_key = em28xx_get_key_pinnacle_usb_grey;
Mauro Carvalho Chehabd40580e2013-01-04 17:37:26 -0300455 dev->init_data.name = "Pinnacle USB2";
Ezequiel García9d9f4792012-03-26 09:13:33 -0300456 break;
457 case EM2820_BOARD_HAUPPAUGE_WINTV_USB_2:
458 dev->init_data.ir_codes = RC_MAP_HAUPPAUGE;
459 dev->init_data.get_key = em28xx_get_key_em_haup;
Mauro Carvalho Chehabd40580e2013-01-04 17:37:26 -0300460 dev->init_data.name = "WinTV USB2";
Mauro Carvalho Chehab3ac693c2013-01-04 18:01:59 -0300461 dev->init_data.type = RC_BIT_RC5;
Ezequiel García9d9f4792012-03-26 09:13:33 -0300462 break;
463 case EM2820_BOARD_LEADTEK_WINFAST_USBII_DELUXE:
464 dev->init_data.ir_codes = RC_MAP_WINFAST_USBII_DELUXE;
465 dev->init_data.get_key = em28xx_get_key_winfast_usbii_deluxe;
Mauro Carvalho Chehabd40580e2013-01-04 17:37:26 -0300466 dev->init_data.name = "Winfast TV USBII Deluxe";
Ezequiel García9d9f4792012-03-26 09:13:33 -0300467 break;
468 }
469
470 if (dev->init_data.name)
471 info.platform_data = &dev->init_data;
472 i2c_new_probed_device(&dev->i2c_adap, &info, addr_list, NULL);
473}
474
Ezequiel García769af212012-03-26 09:13:34 -0300475/**********************************************************
476 Handle Webcam snapshot button
477 **********************************************************/
478
479static void em28xx_query_sbutton(struct work_struct *work)
480{
481 /* Poll the register and see if the button is depressed */
482 struct em28xx *dev =
483 container_of(work, struct em28xx, sbutton_query_work.work);
484 int ret;
485
486 ret = em28xx_read_reg(dev, EM28XX_R0C_USBSUSP);
487
488 if (ret & EM28XX_R0C_USBSUSP_SNAPSHOT) {
489 u8 cleared;
490 /* Button is depressed, clear the register */
491 cleared = ((u8) ret) & ~EM28XX_R0C_USBSUSP_SNAPSHOT;
492 em28xx_write_regs(dev, EM28XX_R0C_USBSUSP, &cleared, 1);
493
494 /* Not emulate the keypress */
495 input_report_key(dev->sbutton_input_dev, EM28XX_SNAPSHOT_KEY,
496 1);
497 /* Now unpress the key */
498 input_report_key(dev->sbutton_input_dev, EM28XX_SNAPSHOT_KEY,
499 0);
500 }
501
502 /* Schedule next poll */
503 schedule_delayed_work(&dev->sbutton_query_work,
504 msecs_to_jiffies(EM28XX_SBUTTON_QUERY_INTERVAL));
505}
506
507static void em28xx_register_snapshot_button(struct em28xx *dev)
508{
509 struct input_dev *input_dev;
510 int err;
511
512 em28xx_info("Registering snapshot button...\n");
513 input_dev = input_allocate_device();
514 if (!input_dev) {
515 em28xx_errdev("input_allocate_device failed\n");
516 return;
517 }
518
519 usb_make_path(dev->udev, dev->snapshot_button_path,
520 sizeof(dev->snapshot_button_path));
521 strlcat(dev->snapshot_button_path, "/sbutton",
522 sizeof(dev->snapshot_button_path));
523 INIT_DELAYED_WORK(&dev->sbutton_query_work, em28xx_query_sbutton);
524
525 input_dev->name = "em28xx snapshot button";
526 input_dev->phys = dev->snapshot_button_path;
527 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
528 set_bit(EM28XX_SNAPSHOT_KEY, input_dev->keybit);
529 input_dev->keycodesize = 0;
530 input_dev->keycodemax = 0;
531 input_dev->id.bustype = BUS_USB;
532 input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
533 input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
534 input_dev->id.version = 1;
535 input_dev->dev.parent = &dev->udev->dev;
536
537 err = input_register_device(input_dev);
538 if (err) {
539 em28xx_errdev("input_register_device failed\n");
540 input_free_device(input_dev);
541 return;
542 }
543
544 dev->sbutton_input_dev = input_dev;
545 schedule_delayed_work(&dev->sbutton_query_work,
546 msecs_to_jiffies(EM28XX_SBUTTON_QUERY_INTERVAL));
547 return;
548
549}
550
551static void em28xx_deregister_snapshot_button(struct em28xx *dev)
552{
553 if (dev->sbutton_input_dev != NULL) {
554 em28xx_info("Deregistering snapshot button\n");
555 cancel_delayed_work_sync(&dev->sbutton_query_work);
556 input_unregister_device(dev->sbutton_input_dev);
557 dev->sbutton_input_dev = NULL;
558 }
559 return;
560}
561
Ezequiel Garcíaf4d4e762012-03-26 09:13:35 -0300562static int em28xx_ir_init(struct em28xx *dev)
Mauro Carvalho Chehab950b0f52009-12-14 02:54:22 -0300563{
564 struct em28xx_IR *ir;
David Härdemand8b4b582010-10-29 16:08:23 -0300565 struct rc_dev *rc;
Mauro Carvalho Chehab950b0f52009-12-14 02:54:22 -0300566 int err = -ENOMEM;
David Härdemanc003ab12012-10-11 19:11:54 -0300567 u64 rc_type;
Mauro Carvalho Chehab950b0f52009-12-14 02:54:22 -0300568
Mauro Carvalho Chehab8303dc92013-01-04 17:27:47 -0300569 if (dev->board.has_snapshot_button)
570 em28xx_register_snapshot_button(dev);
571
572 if (dev->board.has_ir_i2c) {
573 em28xx_register_i2c_ir(dev);
574#if defined(CONFIG_MODULES) && defined(MODULE)
575 request_module("ir-kbd-i2c");
576#endif
577 return 0;
578 }
579
Mauro Carvalho Chehab950b0f52009-12-14 02:54:22 -0300580 if (dev->board.ir_codes == NULL) {
581 /* No remote control support */
Martin Blumenstinglb83f6712012-05-30 15:47:40 -0300582 em28xx_warn("Remote control support is not available for "
583 "this card.\n");
Mauro Carvalho Chehab950b0f52009-12-14 02:54:22 -0300584 return 0;
585 }
586
587 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
David Härdemand8b4b582010-10-29 16:08:23 -0300588 rc = rc_allocate_device();
589 if (!ir || !rc)
Frank Schaefer2f5741a2012-12-22 10:13:38 -0300590 goto error;
Mauro Carvalho Chehab950b0f52009-12-14 02:54:22 -0300591
592 /* record handles to ourself */
593 ir->dev = dev;
594 dev->ir = ir;
David Härdemand8b4b582010-10-29 16:08:23 -0300595 ir->rc = rc;
Mauro Carvalho Chehab950b0f52009-12-14 02:54:22 -0300596
597 /*
598 * em2874 supports more protocols. For now, let's just announce
599 * the two protocols that were already tested
600 */
David Härdemanc003ab12012-10-11 19:11:54 -0300601 rc->allowed_protos = RC_BIT_RC5 | RC_BIT_NEC;
David Härdemand8b4b582010-10-29 16:08:23 -0300602 rc->priv = ir;
603 rc->change_protocol = em28xx_ir_change_protocol;
604 rc->open = em28xx_ir_start;
605 rc->close = em28xx_ir_stop;
Mauro Carvalho Chehabc373cab2010-04-07 22:57:04 -0300606
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300607 switch (dev->chip_id) {
608 case CHIP_ID_EM2860:
609 case CHIP_ID_EM2883:
610 rc->allowed_protos = RC_BIT_RC5 | RC_BIT_NEC;
Frank Schaefer6ea887e2012-12-27 19:02:47 -0300611 ir->get_key = default_polling_getkey;
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300612 break;
613 case CHIP_ID_EM2884:
614 case CHIP_ID_EM2874:
615 case CHIP_ID_EM28174:
Frank Schaefer6ea887e2012-12-27 19:02:47 -0300616 ir->get_key = em2874_polling_getkey;
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300617 rc->allowed_protos = RC_BIT_RC5 | RC_BIT_NEC | RC_BIT_RC6_0;
618 break;
619 default:
620 err = -ENODEV;
Frank Schaefer2f5741a2012-12-22 10:13:38 -0300621 goto error;
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300622 }
623
Mauro Carvalho Chehabc373cab2010-04-07 22:57:04 -0300624 /* By default, keep protocol field untouched */
David Härdemanc003ab12012-10-11 19:11:54 -0300625 rc_type = RC_BIT_UNKNOWN;
626 err = em28xx_ir_change_protocol(rc, &rc_type);
Mauro Carvalho Chehabc373cab2010-04-07 22:57:04 -0300627 if (err)
Frank Schaefer2f5741a2012-12-22 10:13:38 -0300628 goto error;
Mauro Carvalho Chehab950b0f52009-12-14 02:54:22 -0300629
Devin Heitmueller4b922532008-11-13 03:15:55 -0300630 /* This is how often we ask the chip for IR information */
631 ir->polling = 100; /* ms */
632
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300633 /* init input device */
634 snprintf(ir->name, sizeof(ir->name), "em28xx IR (%s)",
635 dev->name);
636
637 usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
638 strlcat(ir->phys, "/input0", sizeof(ir->phys));
639
David Härdemand8b4b582010-10-29 16:08:23 -0300640 rc->input_name = ir->name;
641 rc->input_phys = ir->phys;
642 rc->input_id.bustype = BUS_USB;
643 rc->input_id.version = 1;
644 rc->input_id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
645 rc->input_id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
646 rc->dev.parent = &dev->udev->dev;
647 rc->map_name = dev->board.ir_codes;
648 rc->driver_name = MODULE_NAME;
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300649
650 /* all done */
David Härdemand8b4b582010-10-29 16:08:23 -0300651 err = rc_register_device(rc);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300652 if (err)
Frank Schaefer2f5741a2012-12-22 10:13:38 -0300653 goto error;
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300654
655 return 0;
David Härdemand8b4b582010-10-29 16:08:23 -0300656
Frank Schaefer2f5741a2012-12-22 10:13:38 -0300657error:
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300658 dev->ir = NULL;
David Härdemand8b4b582010-10-29 16:08:23 -0300659 rc_free_device(rc);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300660 kfree(ir);
661 return err;
662}
663
Ezequiel Garcíaf4d4e762012-03-26 09:13:35 -0300664static int em28xx_ir_fini(struct em28xx *dev)
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300665{
666 struct em28xx_IR *ir = dev->ir;
667
Ezequiel García2fd6f8d2012-03-26 09:13:32 -0300668 em28xx_deregister_snapshot_button(dev);
669
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300670 /* skip detach on non attached boards */
671 if (!ir)
672 return 0;
673
Mauro Carvalho Chehab6d514772011-07-29 02:26:59 -0300674 if (ir->rc)
675 rc_unregister_device(ir->rc);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300676
677 /* done */
Mauro Carvalho Chehab6d514772011-07-29 02:26:59 -0300678 kfree(ir);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300679 dev->ir = NULL;
680 return 0;
681}
682
Ezequiel Garcíaf4d4e762012-03-26 09:13:35 -0300683static struct em28xx_ops rc_ops = {
684 .id = EM28XX_RC,
685 .name = "Em28xx Input Extension",
686 .init = em28xx_ir_init,
687 .fini = em28xx_ir_fini,
688};
689
690static int __init em28xx_rc_register(void)
691{
692 return em28xx_register_extension(&rc_ops);
693}
694
695static void __exit em28xx_rc_unregister(void)
696{
697 em28xx_unregister_extension(&rc_ops);
698}
699
700MODULE_LICENSE("GPL");
701MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
702MODULE_DESCRIPTION("Em28xx Input driver");
703
704module_init(em28xx_rc_register);
705module_exit(em28xx_rc_unregister);