blob: f3cff2bfb5ba028a91f7b19f8be6ad667d147320 [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
Frank Schaefer768da3db2013-01-13 10:20:41 -030065 /* poll decoder */
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -030066 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
Frank Schaefer768da3db2013-01-13 10:20:41 -030072 /* external device (if used) */
73 struct i2c_client *i2c_dev;
74
Frank Schaefer146b7ee2013-01-13 10:20:42 -030075 int (*get_key_i2c)(struct i2c_client *, u32 *);
Devin Heitmueller4b922532008-11-13 03:15:55 -030076 int (*get_key)(struct em28xx_IR *, struct em28xx_ir_poll_result *);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -030077};
78
79/**********************************************************
80 I2C IR based get keycodes - should be used with ir-kbd-i2c
81 **********************************************************/
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -080082
Frank Schaefer146b7ee2013-01-13 10:20:42 -030083static int em28xx_get_key_terratec(struct i2c_client *i2c_dev, u32 *ir_key)
Markus Rechbergere43f14a2005-11-08 21:37:35 -080084{
85 unsigned char b;
86
87 /* poll IR chip */
Frank Schaefer768da3db2013-01-13 10:20:41 -030088 if (1 != i2c_master_recv(i2c_dev, &b, 1))
Markus Rechbergere43f14a2005-11-08 21:37:35 -080089 return -EIO;
Markus Rechbergere43f14a2005-11-08 21:37:35 -080090
91 /* it seems that 0xFE indicates that a button is still hold
Frank Schaefer450c7dd2013-01-13 10:20:43 -030092 down, while 0xff indicates that no button is hold down. */
Markus Rechbergere43f14a2005-11-08 21:37:35 -080093
Mauro Carvalho Chehab4f9c05a2005-11-08 21:37:36 -080094 if (b == 0xff)
Markus Rechbergere43f14a2005-11-08 21:37:35 -080095 return 0;
96
Mauro Carvalho Chehab4f9c05a2005-11-08 21:37:36 -080097 if (b == 0xfe)
Markus Rechbergere43f14a2005-11-08 21:37:35 -080098 /* keep old data */
99 return 1;
100
101 *ir_key = b;
Markus Rechbergere43f14a2005-11-08 21:37:35 -0800102 return 1;
103}
104
Frank Schaefer146b7ee2013-01-13 10:20:42 -0300105static int em28xx_get_key_em_haup(struct i2c_client *i2c_dev, u32 *ir_key)
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -0800106{
107 unsigned char buf[2];
Mauro Carvalho Chehabb7799742009-12-05 23:24:50 -0300108 u16 code;
109 int size;
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -0800110
111 /* poll IR chip */
Frank Schaefer768da3db2013-01-13 10:20:41 -0300112 size = i2c_master_recv(i2c_dev, buf, sizeof(buf));
Mauro Carvalho Chehabb7799742009-12-05 23:24:50 -0300113
114 if (size != 2)
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -0800115 return -EIO;
116
117 /* Does eliminate repeated parity code */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300118 if (buf[1] == 0xff)
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -0800119 return 0;
120
Mauro Carvalho Chehabb7799742009-12-05 23:24:50 -0300121 /*
122 * Rearranges bits to the right order.
123 * The bit order were determined experimentally by using
124 * The original Hauppauge Grey IR and another RC5 that uses addr=0x08
125 * The RC5 code has 14 bits, but we've experimentally determined
126 * the meaning for only 11 bits.
127 * So, the code translation is not complete. Yet, it is enough to
128 * work with the provided RC5 IR.
129 */
130 code =
131 ((buf[0] & 0x01) ? 0x0020 : 0) | /* 0010 0000 */
132 ((buf[0] & 0x02) ? 0x0010 : 0) | /* 0001 0000 */
133 ((buf[0] & 0x04) ? 0x0008 : 0) | /* 0000 1000 */
134 ((buf[0] & 0x08) ? 0x0004 : 0) | /* 0000 0100 */
135 ((buf[0] & 0x10) ? 0x0002 : 0) | /* 0000 0010 */
136 ((buf[0] & 0x20) ? 0x0001 : 0) | /* 0000 0001 */
137 ((buf[1] & 0x08) ? 0x1000 : 0) | /* 0001 0000 */
138 ((buf[1] & 0x10) ? 0x0800 : 0) | /* 0000 1000 */
139 ((buf[1] & 0x20) ? 0x0400 : 0) | /* 0000 0100 */
140 ((buf[1] & 0x40) ? 0x0200 : 0) | /* 0000 0010 */
141 ((buf[1] & 0x80) ? 0x0100 : 0); /* 0000 0001 */
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -0800142
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -0800143 /* return key */
144 *ir_key = code;
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -0800145 return 1;
146}
147
Frank Schaefer768da3db2013-01-13 10:20:41 -0300148static int em28xx_get_key_pinnacle_usb_grey(struct i2c_client *i2c_dev,
Frank Schaefer146b7ee2013-01-13 10:20:42 -0300149 u32 *ir_key)
Markus Rechberger366cc642006-01-15 21:04:27 -0200150{
151 unsigned char buf[3];
152
153 /* poll IR chip */
154
Frank Schaefer768da3db2013-01-13 10:20:41 -0300155 if (3 != i2c_master_recv(i2c_dev, buf, 3))
Markus Rechberger366cc642006-01-15 21:04:27 -0200156 return -EIO;
Markus Rechberger366cc642006-01-15 21:04:27 -0200157
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300158 if (buf[0] != 0x00)
Markus Rechberger366cc642006-01-15 21:04:27 -0200159 return 0;
Markus Rechberger366cc642006-01-15 21:04:27 -0200160
161 *ir_key = buf[2]&0x3f;
Markus Rechberger366cc642006-01-15 21:04:27 -0200162
163 return 1;
164}
165
Frank Schaefer768da3db2013-01-13 10:20:41 -0300166static int em28xx_get_key_winfast_usbii_deluxe(struct i2c_client *i2c_dev,
Frank Schaefer146b7ee2013-01-13 10:20:42 -0300167 u32 *ir_key)
Magnus Almca39d842009-11-13 05:48:24 -0300168{
169 unsigned char subaddr, keydetect, key;
170
Frank Schaefer768da3db2013-01-13 10:20:41 -0300171 struct i2c_msg msg[] = { { .addr = i2c_dev->addr, .flags = 0, .buf = &subaddr, .len = 1},
Frank Schaefer450c7dd2013-01-13 10:20:43 -0300172 { .addr = i2c_dev->addr, .flags = I2C_M_RD, .buf = &keydetect, .len = 1} };
Magnus Almca39d842009-11-13 05:48:24 -0300173
174 subaddr = 0x10;
Frank Schaefer768da3db2013-01-13 10:20:41 -0300175 if (2 != i2c_transfer(i2c_dev->adapter, msg, 2))
Magnus Almca39d842009-11-13 05:48:24 -0300176 return -EIO;
Magnus Almca39d842009-11-13 05:48:24 -0300177 if (keydetect == 0x00)
178 return 0;
179
180 subaddr = 0x00;
181 msg[1].buf = &key;
Frank Schaefer768da3db2013-01-13 10:20:41 -0300182 if (2 != i2c_transfer(i2c_dev->adapter, msg, 2))
Frank Schaefer62ec3f82013-01-13 10:20:40 -0300183 return -EIO;
Magnus Almca39d842009-11-13 05:48:24 -0300184 if (key == 0x00)
185 return 0;
186
187 *ir_key = key;
Magnus Almca39d842009-11-13 05:48:24 -0300188 return 1;
189}
190
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300191/**********************************************************
192 Poll based get keycode functions
193 **********************************************************/
194
Devin Heitmueller4b922532008-11-13 03:15:55 -0300195/* This is for the em2860/em2880 */
196static int default_polling_getkey(struct em28xx_IR *ir,
197 struct em28xx_ir_poll_result *poll_result)
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300198{
199 struct em28xx *dev = ir->dev;
200 int rc;
Devin Heitmueller4b922532008-11-13 03:15:55 -0300201 u8 msg[3] = { 0, 0, 0 };
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300202
Mauro Carvalho Chehab0a6b8a82008-11-12 18:46:01 -0300203 /* Read key toggle, brand, and key code
204 on registers 0x45, 0x46 and 0x47
205 */
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300206 rc = dev->em28xx_read_reg_req_len(dev, 0, EM28XX_R45_IR,
Mauro Carvalho Chehab0a6b8a82008-11-12 18:46:01 -0300207 msg, sizeof(msg));
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300208 if (rc < 0)
209 return rc;
210
Devin Heitmueller4b922532008-11-13 03:15:55 -0300211 /* Infrared toggle (Reg 0x45[7]) */
212 poll_result->toggle_bit = (msg[0] >> 7);
213
214 /* Infrared read count (Reg 0x45[6:0] */
215 poll_result->read_count = (msg[0] & 0x7f);
216
Mauro Carvalho Chehab105e3682012-12-15 08:29:11 -0300217 /* Remote Control Address/Data (Regs 0x46/0x47) */
218 poll_result->scancode = msg[1] << 8 | msg[2];
Devin Heitmueller4b922532008-11-13 03:15:55 -0300219
220 return 0;
221}
222
223static int em2874_polling_getkey(struct em28xx_IR *ir,
224 struct em28xx_ir_poll_result *poll_result)
225{
226 struct em28xx *dev = ir->dev;
227 int rc;
228 u8 msg[5] = { 0, 0, 0, 0, 0 };
229
230 /* Read key toggle, brand, and key code
231 on registers 0x51-55
232 */
233 rc = dev->em28xx_read_reg_req_len(dev, 0, EM2874_R51_IR,
234 msg, sizeof(msg));
235 if (rc < 0)
236 return rc;
237
238 /* Infrared toggle (Reg 0x51[7]) */
239 poll_result->toggle_bit = (msg[0] >> 7);
240
241 /* Infrared read count (Reg 0x51[6:0] */
242 poll_result->read_count = (msg[0] & 0x7f);
243
Mauro Carvalho Chehab105e3682012-12-15 08:29:11 -0300244 /*
245 * Remote Control Address (Reg 0x52)
246 * Remote Control Data (Reg 0x53-0x55)
247 */
248 switch (ir->rc_type) {
249 case RC_BIT_RC5:
250 poll_result->scancode = msg[1] << 8 | msg[2];
251 break;
252 case RC_BIT_NEC:
253 if ((msg[3] ^ msg[4]) != 0xff) /* 32 bits NEC */
254 poll_result->scancode = (msg[1] << 24) |
255 (msg[2] << 16) |
256 (msg[3] << 8) |
257 msg[4];
258 else if ((msg[1] ^ msg[2]) != 0xff) /* 24 bits NEC */
259 poll_result->scancode = (msg[1] << 16) |
260 (msg[2] << 8) |
261 msg[3];
262 else /* Normal NEC */
263 poll_result->scancode = msg[1] << 8 | msg[3];
264 break;
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300265 case RC_BIT_RC6_0:
266 poll_result->scancode = msg[1] << 8 | msg[2];
267 break;
Mauro Carvalho Chehab105e3682012-12-15 08:29:11 -0300268 default:
269 poll_result->scancode = (msg[1] << 24) | (msg[2] << 16) |
270 (msg[3] << 8) | msg[4];
271 break;
272 }
Devin Heitmueller4b922532008-11-13 03:15:55 -0300273
274 return 0;
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300275}
276
277/**********************************************************
278 Polling code for em28xx
279 **********************************************************/
280
Frank Schaefer768da3db2013-01-13 10:20:41 -0300281static int em28xx_i2c_ir_handle_key(struct em28xx_IR *ir)
282{
Frank Schaefer146b7ee2013-01-13 10:20:42 -0300283 static u32 ir_key;
Frank Schaefer768da3db2013-01-13 10:20:41 -0300284 int rc;
285
Frank Schaefer146b7ee2013-01-13 10:20:42 -0300286 rc = ir->get_key_i2c(ir->i2c_dev, &ir_key);
Frank Schaefer768da3db2013-01-13 10:20:41 -0300287 if (rc < 0) {
288 dprintk("ir->get_key_i2c() failed: %d\n", rc);
289 return rc;
290 }
291
292 if (rc) {
293 dprintk("%s: keycode = 0x%04x\n", __func__, ir_key);
294 rc_keydown(ir->rc, ir_key, 0);
295 }
296 return 0;
297}
298
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300299static void em28xx_ir_handle_key(struct em28xx_IR *ir)
300{
Devin Heitmueller4b922532008-11-13 03:15:55 -0300301 int result;
Devin Heitmueller4b922532008-11-13 03:15:55 -0300302 struct em28xx_ir_poll_result poll_result;
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300303
Devin Heitmueller4b922532008-11-13 03:15:55 -0300304 /* read the registers containing the IR status */
305 result = ir->get_key(ir, &poll_result);
Mauro Carvalho Chehab603044d2010-06-27 08:32:11 -0300306 if (unlikely(result < 0)) {
Frank Schaefer768da3db2013-01-13 10:20:41 -0300307 dprintk("ir->get_key() failed: %d\n", result);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300308 return;
Devin Heitmueller4b922532008-11-13 03:15:55 -0300309 }
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300310
Mauro Carvalho Chehab603044d2010-06-27 08:32:11 -0300311 if (unlikely(poll_result.read_count != ir->last_readcount)) {
Mauro Carvalho Chehab105e3682012-12-15 08:29:11 -0300312 dprintk("%s: toggle: %d, count: %d, key 0x%04x\n", __func__,
Mauro Carvalho Chehab603044d2010-06-27 08:32:11 -0300313 poll_result.toggle_bit, poll_result.read_count,
Mauro Carvalho Chehab105e3682012-12-15 08:29:11 -0300314 poll_result.scancode);
David Härdemana4695852010-06-07 16:32:23 -0300315 if (ir->full_code)
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300316 rc_keydown(ir->rc,
Mauro Carvalho Chehab105e3682012-12-15 08:29:11 -0300317 poll_result.scancode,
David Härdemana4695852010-06-07 16:32:23 -0300318 poll_result.toggle_bit);
319 else
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300320 rc_keydown(ir->rc,
Mauro Carvalho Chehab105e3682012-12-15 08:29:11 -0300321 poll_result.scancode & 0xff,
David Härdemana4695852010-06-07 16:32:23 -0300322 poll_result.toggle_bit);
David Härdemana4695852010-06-07 16:32:23 -0300323
Mauro Carvalho Chehab20ae9742011-11-20 12:23:54 -0200324 if (ir->dev->chip_id == CHIP_ID_EM2874 ||
325 ir->dev->chip_id == CHIP_ID_EM2884)
Mauro Carvalho Chehab603044d2010-06-27 08:32:11 -0300326 /* The em2874 clears the readcount field every time the
327 register is read. The em2860/2880 datasheet says that it
328 is supposed to clear the readcount, but it doesn't. So with
329 the em2874, we are looking for a non-zero read count as
330 opposed to a readcount that is incrementing */
331 ir->last_readcount = 0;
332 else
333 ir->last_readcount = poll_result.read_count;
334 }
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300335}
336
Frank Schaefer768da3db2013-01-13 10:20:41 -0300337static void em28xx_i2c_ir_work(struct work_struct *work)
338{
339 struct em28xx_IR *ir = container_of(work, struct em28xx_IR, work.work);
340
341 em28xx_i2c_ir_handle_key(ir);
342 schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
343}
344
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300345static void em28xx_ir_work(struct work_struct *work)
346{
Jean Delvaref263bac2009-03-07 07:43:01 -0300347 struct em28xx_IR *ir = container_of(work, struct em28xx_IR, work.work);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300348
349 em28xx_ir_handle_key(ir);
Jean Delvaref263bac2009-03-07 07:43:01 -0300350 schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300351}
352
David Härdemand8b4b582010-10-29 16:08:23 -0300353static int em28xx_ir_start(struct rc_dev *rc)
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300354{
David Härdemand8b4b582010-10-29 16:08:23 -0300355 struct em28xx_IR *ir = rc->priv;
Mauro Carvalho Chehabc373cab2010-04-07 22:57:04 -0300356
Frank Schaefer768da3db2013-01-13 10:20:41 -0300357 if (ir->i2c_dev) /* external i2c device */
358 INIT_DELAYED_WORK(&ir->work, em28xx_i2c_ir_work);
359 else /* internal device */
360 INIT_DELAYED_WORK(&ir->work, em28xx_ir_work);
Jean Delvaref263bac2009-03-07 07:43:01 -0300361 schedule_delayed_work(&ir->work, 0);
Mauro Carvalho Chehabc373cab2010-04-07 22:57:04 -0300362
363 return 0;
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300364}
365
David Härdemand8b4b582010-10-29 16:08:23 -0300366static void em28xx_ir_stop(struct rc_dev *rc)
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300367{
David Härdemand8b4b582010-10-29 16:08:23 -0300368 struct em28xx_IR *ir = rc->priv;
Mauro Carvalho Chehabc373cab2010-04-07 22:57:04 -0300369
Jean Delvaref263bac2009-03-07 07:43:01 -0300370 cancel_delayed_work_sync(&ir->work);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300371}
372
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300373static int em2860_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_type)
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300374{
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300375 struct em28xx_IR *ir = rc_dev->priv;
376 struct em28xx *dev = ir->dev;
377
378 /* Adjust xclk based on IR table for RC5/NEC tables */
379 if (*rc_type & RC_BIT_RC5) {
380 dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
381 ir->full_code = 1;
382 *rc_type = RC_BIT_RC5;
383 } else if (*rc_type & RC_BIT_NEC) {
384 dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
385 ir->full_code = 1;
386 *rc_type = RC_BIT_NEC;
387 } else if (*rc_type & RC_BIT_UNKNOWN) {
388 *rc_type = RC_BIT_UNKNOWN;
389 } else {
390 *rc_type = ir->rc_type;
391 return -EINVAL;
392 }
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300393 em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
394 EM28XX_XCLK_IR_RC5_MODE);
395
396 ir->rc_type = *rc_type;
397
398 return 0;
399}
400
401static int em2874_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_type)
402{
David Härdemand8b4b582010-10-29 16:08:23 -0300403 struct em28xx_IR *ir = rc_dev->priv;
Mauro Carvalho Chehab950b0f52009-12-14 02:54:22 -0300404 struct em28xx *dev = ir->dev;
405 u8 ir_config = EM2874_IR_RC5;
Mauro Carvalho Chehab1bad4292009-12-05 08:27:49 -0300406
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300407 /* Adjust xclk and set type based on IR table for RC5/NEC/RC6 tables */
David Härdemanc003ab12012-10-11 19:11:54 -0300408 if (*rc_type & RC_BIT_RC5) {
Mauro Carvalho Chehab1bad4292009-12-05 08:27:49 -0300409 dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
410 ir->full_code = 1;
David Härdemanc003ab12012-10-11 19:11:54 -0300411 *rc_type = RC_BIT_RC5;
412 } else if (*rc_type & RC_BIT_NEC) {
Mauro Carvalho Chehab1bad4292009-12-05 08:27:49 -0300413 dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
Mauro Carvalho Chehab105e3682012-12-15 08:29:11 -0300414 ir_config = EM2874_IR_NEC | EM2874_IR_NEC_NO_PARITY;
Mauro Carvalho Chehab1bad4292009-12-05 08:27:49 -0300415 ir->full_code = 1;
David Härdemanc003ab12012-10-11 19:11:54 -0300416 *rc_type = RC_BIT_NEC;
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300417 } else if (*rc_type & RC_BIT_RC6_0) {
418 dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
419 ir_config = EM2874_IR_RC6_MODE_0;
420 ir->full_code = 1;
421 *rc_type = RC_BIT_RC6_0;
422 } else if (*rc_type & RC_BIT_UNKNOWN) {
423 *rc_type = RC_BIT_UNKNOWN;
424 } else {
425 *rc_type = ir->rc_type;
426 return -EINVAL;
427 }
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300428 em28xx_write_regs(dev, EM2874_R50_IR_CONFIG, &ir_config, 1);
Mauro Carvalho Chehab1bad4292009-12-05 08:27:49 -0300429 em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
430 EM28XX_XCLK_IR_RC5_MODE);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300431
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300432 ir->rc_type = *rc_type;
433
434 return 0;
435}
436static int em28xx_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_type)
437{
438 struct em28xx_IR *ir = rc_dev->priv;
439 struct em28xx *dev = ir->dev;
440
Devin Heitmueller4b922532008-11-13 03:15:55 -0300441 /* Setup the proper handler based on the chip */
442 switch (dev->chip_id) {
443 case CHIP_ID_EM2860:
444 case CHIP_ID_EM2883:
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300445 return em2860_ir_change_protocol(rc_dev, rc_type);
Mauro Carvalho Chehab20ae9742011-11-20 12:23:54 -0200446 case CHIP_ID_EM2884:
Devin Heitmueller4b922532008-11-13 03:15:55 -0300447 case CHIP_ID_EM2874:
Antti Palosaaria24ec442011-05-25 16:56:37 -0300448 case CHIP_ID_EM28174:
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300449 return em2874_ir_change_protocol(rc_dev, rc_type);
Devin Heitmueller4b922532008-11-13 03:15:55 -0300450 default:
Mauro Carvalho Chehab20ae9742011-11-20 12:23:54 -0200451 printk("Unrecognized em28xx chip id 0x%02x: IR not supported\n",
452 dev->chip_id);
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300453 return -EINVAL;
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300454 }
Mauro Carvalho Chehab950b0f52009-12-14 02:54:22 -0300455}
456
Frank Schaefer768da3db2013-01-13 10:20:41 -0300457static struct i2c_client *em28xx_probe_i2c_ir(struct em28xx *dev)
Ezequiel García9d9f4792012-03-26 09:13:33 -0300458{
Frank Schaefer768da3db2013-01-13 10:20:41 -0300459 int i = 0;
460 struct i2c_client *i2c_dev = NULL;
Ezequiel García9d9f4792012-03-26 09:13:33 -0300461 /* Leadtek winfast tv USBII deluxe can find a non working IR-device */
462 /* at address 0x18, so if that address is needed for another board in */
463 /* the future, please put it after 0x1f. */
Ezequiel García9d9f4792012-03-26 09:13:33 -0300464 const unsigned short addr_list[] = {
465 0x1f, 0x30, 0x47, I2C_CLIENT_END
466 };
467
Frank Schaefer768da3db2013-01-13 10:20:41 -0300468 while (addr_list[i] != I2C_CLIENT_END) {
469 if (i2c_probe_func_quick_read(&dev->i2c_adap, addr_list[i]) == 1) {
470 i2c_dev = kzalloc(sizeof(*i2c_dev), GFP_KERNEL);
471 if (i2c_dev) {
472 i2c_dev->addr = addr_list[i];
473 i2c_dev->adapter = &dev->i2c_adap;
474 /* NOTE: as long as we don't register the device
475 * at the i2c subsystem, no other fields need to
476 * be set up */
477 }
478 break;
479 }
480 i++;
Ezequiel García9d9f4792012-03-26 09:13:33 -0300481 }
482
Frank Schaefer768da3db2013-01-13 10:20:41 -0300483 return i2c_dev;
Ezequiel García9d9f4792012-03-26 09:13:33 -0300484}
485
Ezequiel García769af212012-03-26 09:13:34 -0300486/**********************************************************
487 Handle Webcam snapshot button
488 **********************************************************/
489
490static void em28xx_query_sbutton(struct work_struct *work)
491{
492 /* Poll the register and see if the button is depressed */
493 struct em28xx *dev =
494 container_of(work, struct em28xx, sbutton_query_work.work);
495 int ret;
496
497 ret = em28xx_read_reg(dev, EM28XX_R0C_USBSUSP);
498
499 if (ret & EM28XX_R0C_USBSUSP_SNAPSHOT) {
500 u8 cleared;
501 /* Button is depressed, clear the register */
502 cleared = ((u8) ret) & ~EM28XX_R0C_USBSUSP_SNAPSHOT;
503 em28xx_write_regs(dev, EM28XX_R0C_USBSUSP, &cleared, 1);
504
505 /* Not emulate the keypress */
506 input_report_key(dev->sbutton_input_dev, EM28XX_SNAPSHOT_KEY,
507 1);
508 /* Now unpress the key */
509 input_report_key(dev->sbutton_input_dev, EM28XX_SNAPSHOT_KEY,
510 0);
511 }
512
513 /* Schedule next poll */
514 schedule_delayed_work(&dev->sbutton_query_work,
515 msecs_to_jiffies(EM28XX_SBUTTON_QUERY_INTERVAL));
516}
517
518static void em28xx_register_snapshot_button(struct em28xx *dev)
519{
520 struct input_dev *input_dev;
521 int err;
522
523 em28xx_info("Registering snapshot button...\n");
524 input_dev = input_allocate_device();
525 if (!input_dev) {
526 em28xx_errdev("input_allocate_device failed\n");
527 return;
528 }
529
530 usb_make_path(dev->udev, dev->snapshot_button_path,
531 sizeof(dev->snapshot_button_path));
532 strlcat(dev->snapshot_button_path, "/sbutton",
533 sizeof(dev->snapshot_button_path));
534 INIT_DELAYED_WORK(&dev->sbutton_query_work, em28xx_query_sbutton);
535
536 input_dev->name = "em28xx snapshot button";
537 input_dev->phys = dev->snapshot_button_path;
538 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
539 set_bit(EM28XX_SNAPSHOT_KEY, input_dev->keybit);
540 input_dev->keycodesize = 0;
541 input_dev->keycodemax = 0;
542 input_dev->id.bustype = BUS_USB;
543 input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
544 input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
545 input_dev->id.version = 1;
546 input_dev->dev.parent = &dev->udev->dev;
547
548 err = input_register_device(input_dev);
549 if (err) {
550 em28xx_errdev("input_register_device failed\n");
551 input_free_device(input_dev);
552 return;
553 }
554
555 dev->sbutton_input_dev = input_dev;
556 schedule_delayed_work(&dev->sbutton_query_work,
557 msecs_to_jiffies(EM28XX_SBUTTON_QUERY_INTERVAL));
558 return;
559
560}
561
562static void em28xx_deregister_snapshot_button(struct em28xx *dev)
563{
564 if (dev->sbutton_input_dev != NULL) {
565 em28xx_info("Deregistering snapshot button\n");
566 cancel_delayed_work_sync(&dev->sbutton_query_work);
567 input_unregister_device(dev->sbutton_input_dev);
568 dev->sbutton_input_dev = NULL;
569 }
570 return;
571}
572
Ezequiel Garcíaf4d4e762012-03-26 09:13:35 -0300573static int em28xx_ir_init(struct em28xx *dev)
Mauro Carvalho Chehab950b0f52009-12-14 02:54:22 -0300574{
575 struct em28xx_IR *ir;
David Härdemand8b4b582010-10-29 16:08:23 -0300576 struct rc_dev *rc;
Mauro Carvalho Chehab950b0f52009-12-14 02:54:22 -0300577 int err = -ENOMEM;
David Härdemanc003ab12012-10-11 19:11:54 -0300578 u64 rc_type;
Frank Schaefer768da3db2013-01-13 10:20:41 -0300579 struct i2c_client *i2c_rc_dev = NULL;
Mauro Carvalho Chehab950b0f52009-12-14 02:54:22 -0300580
Mauro Carvalho Chehab8303dc92013-01-04 17:27:47 -0300581 if (dev->board.has_snapshot_button)
582 em28xx_register_snapshot_button(dev);
583
584 if (dev->board.has_ir_i2c) {
Frank Schaefer768da3db2013-01-13 10:20:41 -0300585 i2c_rc_dev = em28xx_probe_i2c_ir(dev);
586 if (!i2c_rc_dev) {
587 dev->board.has_ir_i2c = 0;
588 em28xx_warn("No i2c IR remote control device found.\n");
589 return -ENODEV;
590 }
Mauro Carvalho Chehab8303dc92013-01-04 17:27:47 -0300591 }
592
Frank Schaefer768da3db2013-01-13 10:20:41 -0300593 if (dev->board.ir_codes == NULL && !dev->board.has_ir_i2c) {
Mauro Carvalho Chehab950b0f52009-12-14 02:54:22 -0300594 /* No remote control support */
Martin Blumenstinglb83f6712012-05-30 15:47:40 -0300595 em28xx_warn("Remote control support is not available for "
596 "this card.\n");
Mauro Carvalho Chehab950b0f52009-12-14 02:54:22 -0300597 return 0;
598 }
599
600 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
David Härdemand8b4b582010-10-29 16:08:23 -0300601 rc = rc_allocate_device();
602 if (!ir || !rc)
Frank Schaefer2f5741a2012-12-22 10:13:38 -0300603 goto error;
Mauro Carvalho Chehab950b0f52009-12-14 02:54:22 -0300604
605 /* record handles to ourself */
606 ir->dev = dev;
607 dev->ir = ir;
David Härdemand8b4b582010-10-29 16:08:23 -0300608 ir->rc = rc;
Mauro Carvalho Chehab950b0f52009-12-14 02:54:22 -0300609
David Härdemand8b4b582010-10-29 16:08:23 -0300610 rc->priv = ir;
David Härdemand8b4b582010-10-29 16:08:23 -0300611 rc->open = em28xx_ir_start;
612 rc->close = em28xx_ir_stop;
Mauro Carvalho Chehabc373cab2010-04-07 22:57:04 -0300613
Frank Schaefer768da3db2013-01-13 10:20:41 -0300614 if (dev->board.has_ir_i2c) { /* external i2c device */
615 switch (dev->model) {
616 case EM2800_BOARD_TERRATEC_CINERGY_200:
617 case EM2820_BOARD_TERRATEC_CINERGY_250:
618 rc->map_name = RC_MAP_EM_TERRATEC;
619 ir->get_key_i2c = em28xx_get_key_terratec;
620 break;
621 case EM2820_BOARD_PINNACLE_USB_2:
622 rc->map_name = RC_MAP_PINNACLE_GREY;
623 ir->get_key_i2c = em28xx_get_key_pinnacle_usb_grey;
624 break;
625 case EM2820_BOARD_HAUPPAUGE_WINTV_USB_2:
626 rc->map_name = RC_MAP_HAUPPAUGE;
627 ir->get_key_i2c = em28xx_get_key_em_haup;
628 rc->allowed_protos = RC_BIT_RC5;
629 break;
630 case EM2820_BOARD_LEADTEK_WINFAST_USBII_DELUXE:
631 rc->map_name = RC_MAP_WINFAST_USBII_DELUXE;
632 ir->get_key_i2c = em28xx_get_key_winfast_usbii_deluxe;
633 break;
634 default:
635 err = -ENODEV;
636 goto error;
637 }
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300638
Frank Schaefer768da3db2013-01-13 10:20:41 -0300639 ir->i2c_dev = i2c_rc_dev;
640 } else { /* internal device */
641 switch (dev->chip_id) {
642 case CHIP_ID_EM2860:
643 case CHIP_ID_EM2883:
644 rc->allowed_protos = RC_BIT_RC5 | RC_BIT_NEC;
645 ir->get_key = default_polling_getkey;
646 break;
647 case CHIP_ID_EM2884:
648 case CHIP_ID_EM2874:
649 case CHIP_ID_EM28174:
650 ir->get_key = em2874_polling_getkey;
651 rc->allowed_protos = RC_BIT_RC5 | RC_BIT_NEC |
652 RC_BIT_RC6_0;
653 break;
654 default:
655 err = -ENODEV;
656 goto error;
657 }
658
659 rc->change_protocol = em28xx_ir_change_protocol;
660 rc->map_name = dev->board.ir_codes;
661
662 /* By default, keep protocol field untouched */
663 rc_type = RC_BIT_UNKNOWN;
664 err = em28xx_ir_change_protocol(rc, &rc_type);
665 if (err)
666 goto error;
667 }
Mauro Carvalho Chehab950b0f52009-12-14 02:54:22 -0300668
Devin Heitmueller4b922532008-11-13 03:15:55 -0300669 /* This is how often we ask the chip for IR information */
670 ir->polling = 100; /* ms */
671
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300672 /* init input device */
Frank Schaefer768da3db2013-01-13 10:20:41 -0300673 snprintf(ir->name, sizeof(ir->name), "em28xx IR (%s)", dev->name);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300674
675 usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
676 strlcat(ir->phys, "/input0", sizeof(ir->phys));
677
David Härdemand8b4b582010-10-29 16:08:23 -0300678 rc->input_name = ir->name;
679 rc->input_phys = ir->phys;
680 rc->input_id.bustype = BUS_USB;
681 rc->input_id.version = 1;
682 rc->input_id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
683 rc->input_id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
684 rc->dev.parent = &dev->udev->dev;
David Härdemand8b4b582010-10-29 16:08:23 -0300685 rc->driver_name = MODULE_NAME;
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300686
687 /* all done */
David Härdemand8b4b582010-10-29 16:08:23 -0300688 err = rc_register_device(rc);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300689 if (err)
Frank Schaefer2f5741a2012-12-22 10:13:38 -0300690 goto error;
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300691
692 return 0;
David Härdemand8b4b582010-10-29 16:08:23 -0300693
Frank Schaefer2f5741a2012-12-22 10:13:38 -0300694error:
Frank Schaefer768da3db2013-01-13 10:20:41 -0300695 if (ir && ir->i2c_dev)
696 kfree(ir->i2c_dev);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300697 dev->ir = NULL;
David Härdemand8b4b582010-10-29 16:08:23 -0300698 rc_free_device(rc);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300699 kfree(ir);
700 return err;
701}
702
Ezequiel Garcíaf4d4e762012-03-26 09:13:35 -0300703static int em28xx_ir_fini(struct em28xx *dev)
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300704{
705 struct em28xx_IR *ir = dev->ir;
706
Ezequiel García2fd6f8d2012-03-26 09:13:32 -0300707 em28xx_deregister_snapshot_button(dev);
708
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300709 /* skip detach on non attached boards */
710 if (!ir)
711 return 0;
712
Mauro Carvalho Chehab6d514772011-07-29 02:26:59 -0300713 if (ir->rc)
714 rc_unregister_device(ir->rc);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300715
Frank Schaefer768da3db2013-01-13 10:20:41 -0300716 kfree(ir->i2c_dev);
717
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300718 /* done */
Mauro Carvalho Chehab6d514772011-07-29 02:26:59 -0300719 kfree(ir);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300720 dev->ir = NULL;
721 return 0;
722}
723
Ezequiel Garcíaf4d4e762012-03-26 09:13:35 -0300724static struct em28xx_ops rc_ops = {
725 .id = EM28XX_RC,
726 .name = "Em28xx Input Extension",
727 .init = em28xx_ir_init,
728 .fini = em28xx_ir_fini,
729};
730
731static int __init em28xx_rc_register(void)
732{
733 return em28xx_register_extension(&rc_ops);
734}
735
736static void __exit em28xx_rc_unregister(void)
737{
738 em28xx_unregister_extension(&rc_ops);
739}
740
741MODULE_LICENSE("GPL");
742MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
743MODULE_DESCRIPTION("Em28xx Input driver");
744
745module_init(em28xx_rc_register);
746module_exit(em28xx_rc_unregister);