blob: e0acc92f0dfd30fb6903ca88058d3caf4cb4d43c [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
Frank Schaeferf52226092013-12-01 18:06:52 -030034#define EM28XX_BUTTONS_QUERY_INTERVAL 500
Devin Heitmuellera9fc52b2008-06-28 08:57:06 -030035
Mauro Carvalho Chehabc8793b02008-01-13 15:42:17 -030036static unsigned int ir_debug;
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -080037module_param(ir_debug, int, 0644);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030038MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -080039
Mauro Carvalho Chehab727e6252010-03-12 21:18:14 -030040#define MODULE_NAME "em28xx"
41
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -030042#define dprintk(fmt, arg...) \
43 if (ir_debug) { \
44 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
45 }
46
47/**********************************************************
48 Polling structure used by em28xx IR's
49 **********************************************************/
50
Devin Heitmueller4b922532008-11-13 03:15:55 -030051struct em28xx_ir_poll_result {
52 unsigned int toggle_bit:1;
53 unsigned int read_count:7;
Mauro Carvalho Chehab105e3682012-12-15 08:29:11 -030054
55 u32 scancode;
Devin Heitmueller4b922532008-11-13 03:15:55 -030056};
57
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -030058struct em28xx_IR {
59 struct em28xx *dev;
David Härdemand8b4b582010-10-29 16:08:23 -030060 struct rc_dev *rc;
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -030061 char name[32];
62 char phys[32];
63
Frank Schaefer768da3db2013-01-13 10:20:41 -030064 /* poll decoder */
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -030065 int polling;
Jean Delvaref263bac2009-03-07 07:43:01 -030066 struct delayed_work work;
Mauro Carvalho Chehab02781552009-11-27 23:28:40 -030067 unsigned int full_code:1;
Devin Heitmueller4b922532008-11-13 03:15:55 -030068 unsigned int last_readcount;
Mauro Carvalho Chehab105e3682012-12-15 08:29:11 -030069 u64 rc_type;
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -030070
Frank Schaefer1d968cd2013-01-13 10:20:44 -030071 /* i2c slave address of external device (if used) */
72 u16 i2c_dev_addr;
Frank Schaefer768da3db2013-01-13 10:20:41 -030073
Frank Schaefer146b7ee2013-01-13 10:20:42 -030074 int (*get_key_i2c)(struct i2c_client *, u32 *);
Devin Heitmueller4b922532008-11-13 03:15:55 -030075 int (*get_key)(struct em28xx_IR *, struct em28xx_ir_poll_result *);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -030076};
77
78/**********************************************************
79 I2C IR based get keycodes - should be used with ir-kbd-i2c
80 **********************************************************/
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -080081
Frank Schaefer146b7ee2013-01-13 10:20:42 -030082static int em28xx_get_key_terratec(struct i2c_client *i2c_dev, u32 *ir_key)
Markus Rechbergere43f14a2005-11-08 21:37:35 -080083{
84 unsigned char b;
85
86 /* poll IR chip */
Frank Schaefer768da3db2013-01-13 10:20:41 -030087 if (1 != i2c_master_recv(i2c_dev, &b, 1))
Markus Rechbergere43f14a2005-11-08 21:37:35 -080088 return -EIO;
Markus Rechbergere43f14a2005-11-08 21:37:35 -080089
90 /* it seems that 0xFE indicates that a button is still hold
Frank Schaefer450c7dd2013-01-13 10:20:43 -030091 down, while 0xff indicates that no button is hold down. */
Markus Rechbergere43f14a2005-11-08 21:37:35 -080092
Mauro Carvalho Chehab4f9c05a2005-11-08 21:37:36 -080093 if (b == 0xff)
Markus Rechbergere43f14a2005-11-08 21:37:35 -080094 return 0;
95
Mauro Carvalho Chehab4f9c05a2005-11-08 21:37:36 -080096 if (b == 0xfe)
Markus Rechbergere43f14a2005-11-08 21:37:35 -080097 /* keep old data */
98 return 1;
99
100 *ir_key = b;
Markus Rechbergere43f14a2005-11-08 21:37:35 -0800101 return 1;
102}
103
Frank Schaefer146b7ee2013-01-13 10:20:42 -0300104static int em28xx_get_key_em_haup(struct i2c_client *i2c_dev, u32 *ir_key)
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -0800105{
106 unsigned char buf[2];
Mauro Carvalho Chehabb7799742009-12-05 23:24:50 -0300107 u16 code;
108 int size;
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -0800109
110 /* poll IR chip */
Frank Schaefer768da3db2013-01-13 10:20:41 -0300111 size = i2c_master_recv(i2c_dev, buf, sizeof(buf));
Mauro Carvalho Chehabb7799742009-12-05 23:24:50 -0300112
113 if (size != 2)
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -0800114 return -EIO;
115
116 /* Does eliminate repeated parity code */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300117 if (buf[1] == 0xff)
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -0800118 return 0;
119
Mauro Carvalho Chehabb7799742009-12-05 23:24:50 -0300120 /*
121 * Rearranges bits to the right order.
122 * The bit order were determined experimentally by using
123 * The original Hauppauge Grey IR and another RC5 that uses addr=0x08
124 * The RC5 code has 14 bits, but we've experimentally determined
125 * the meaning for only 11 bits.
126 * So, the code translation is not complete. Yet, it is enough to
127 * work with the provided RC5 IR.
128 */
129 code =
130 ((buf[0] & 0x01) ? 0x0020 : 0) | /* 0010 0000 */
131 ((buf[0] & 0x02) ? 0x0010 : 0) | /* 0001 0000 */
132 ((buf[0] & 0x04) ? 0x0008 : 0) | /* 0000 1000 */
133 ((buf[0] & 0x08) ? 0x0004 : 0) | /* 0000 0100 */
134 ((buf[0] & 0x10) ? 0x0002 : 0) | /* 0000 0010 */
135 ((buf[0] & 0x20) ? 0x0001 : 0) | /* 0000 0001 */
136 ((buf[1] & 0x08) ? 0x1000 : 0) | /* 0001 0000 */
137 ((buf[1] & 0x10) ? 0x0800 : 0) | /* 0000 1000 */
138 ((buf[1] & 0x20) ? 0x0400 : 0) | /* 0000 0100 */
139 ((buf[1] & 0x40) ? 0x0200 : 0) | /* 0000 0010 */
140 ((buf[1] & 0x80) ? 0x0100 : 0); /* 0000 0001 */
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -0800141
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -0800142 /* return key */
143 *ir_key = code;
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -0800144 return 1;
145}
146
Frank Schaefer768da3db2013-01-13 10:20:41 -0300147static int em28xx_get_key_pinnacle_usb_grey(struct i2c_client *i2c_dev,
Frank Schaefer146b7ee2013-01-13 10:20:42 -0300148 u32 *ir_key)
Markus Rechberger366cc642006-01-15 21:04:27 -0200149{
150 unsigned char buf[3];
151
152 /* poll IR chip */
153
Frank Schaefer768da3db2013-01-13 10:20:41 -0300154 if (3 != i2c_master_recv(i2c_dev, 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;
Markus Rechberger366cc642006-01-15 21:04:27 -0200161
162 return 1;
163}
164
Frank Schaefer768da3db2013-01-13 10:20:41 -0300165static int em28xx_get_key_winfast_usbii_deluxe(struct i2c_client *i2c_dev,
Frank Schaefer146b7ee2013-01-13 10:20:42 -0300166 u32 *ir_key)
Magnus Almca39d842009-11-13 05:48:24 -0300167{
168 unsigned char subaddr, keydetect, key;
169
Frank Schaefer768da3db2013-01-13 10:20:41 -0300170 struct i2c_msg msg[] = { { .addr = i2c_dev->addr, .flags = 0, .buf = &subaddr, .len = 1},
Frank Schaefer450c7dd2013-01-13 10:20:43 -0300171 { .addr = i2c_dev->addr, .flags = I2C_M_RD, .buf = &keydetect, .len = 1} };
Magnus Almca39d842009-11-13 05:48:24 -0300172
173 subaddr = 0x10;
Frank Schaefer768da3db2013-01-13 10:20:41 -0300174 if (2 != i2c_transfer(i2c_dev->adapter, msg, 2))
Magnus Almca39d842009-11-13 05:48:24 -0300175 return -EIO;
Magnus Almca39d842009-11-13 05:48:24 -0300176 if (keydetect == 0x00)
177 return 0;
178
179 subaddr = 0x00;
180 msg[1].buf = &key;
Frank Schaefer768da3db2013-01-13 10:20:41 -0300181 if (2 != i2c_transfer(i2c_dev->adapter, msg, 2))
Frank Schaefer62ec3f82013-01-13 10:20:40 -0300182 return -EIO;
Magnus Almca39d842009-11-13 05:48:24 -0300183 if (key == 0x00)
184 return 0;
185
186 *ir_key = key;
Magnus Almca39d842009-11-13 05:48:24 -0300187 return 1;
188}
189
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300190/**********************************************************
191 Poll based get keycode functions
192 **********************************************************/
193
Devin Heitmueller4b922532008-11-13 03:15:55 -0300194/* This is for the em2860/em2880 */
195static int default_polling_getkey(struct em28xx_IR *ir,
196 struct em28xx_ir_poll_result *poll_result)
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300197{
198 struct em28xx *dev = ir->dev;
199 int rc;
Devin Heitmueller4b922532008-11-13 03:15:55 -0300200 u8 msg[3] = { 0, 0, 0 };
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300201
Mauro Carvalho Chehab0a6b8a82008-11-12 18:46:01 -0300202 /* Read key toggle, brand, and key code
203 on registers 0x45, 0x46 and 0x47
204 */
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300205 rc = dev->em28xx_read_reg_req_len(dev, 0, EM28XX_R45_IR,
Mauro Carvalho Chehab0a6b8a82008-11-12 18:46:01 -0300206 msg, sizeof(msg));
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300207 if (rc < 0)
208 return rc;
209
Devin Heitmueller4b922532008-11-13 03:15:55 -0300210 /* Infrared toggle (Reg 0x45[7]) */
211 poll_result->toggle_bit = (msg[0] >> 7);
212
213 /* Infrared read count (Reg 0x45[6:0] */
214 poll_result->read_count = (msg[0] & 0x7f);
215
Mauro Carvalho Chehab105e3682012-12-15 08:29:11 -0300216 /* Remote Control Address/Data (Regs 0x46/0x47) */
217 poll_result->scancode = msg[1] << 8 | msg[2];
Devin Heitmueller4b922532008-11-13 03:15:55 -0300218
219 return 0;
220}
221
222static int em2874_polling_getkey(struct em28xx_IR *ir,
223 struct em28xx_ir_poll_result *poll_result)
224{
225 struct em28xx *dev = ir->dev;
226 int rc;
227 u8 msg[5] = { 0, 0, 0, 0, 0 };
228
229 /* Read key toggle, brand, and key code
230 on registers 0x51-55
231 */
232 rc = dev->em28xx_read_reg_req_len(dev, 0, EM2874_R51_IR,
233 msg, sizeof(msg));
234 if (rc < 0)
235 return rc;
236
237 /* Infrared toggle (Reg 0x51[7]) */
238 poll_result->toggle_bit = (msg[0] >> 7);
239
240 /* Infrared read count (Reg 0x51[6:0] */
241 poll_result->read_count = (msg[0] & 0x7f);
242
Mauro Carvalho Chehab105e3682012-12-15 08:29:11 -0300243 /*
244 * Remote Control Address (Reg 0x52)
245 * Remote Control Data (Reg 0x53-0x55)
246 */
247 switch (ir->rc_type) {
248 case RC_BIT_RC5:
249 poll_result->scancode = msg[1] << 8 | msg[2];
250 break;
251 case RC_BIT_NEC:
252 if ((msg[3] ^ msg[4]) != 0xff) /* 32 bits NEC */
253 poll_result->scancode = (msg[1] << 24) |
254 (msg[2] << 16) |
255 (msg[3] << 8) |
256 msg[4];
257 else if ((msg[1] ^ msg[2]) != 0xff) /* 24 bits NEC */
258 poll_result->scancode = (msg[1] << 16) |
259 (msg[2] << 8) |
260 msg[3];
261 else /* Normal NEC */
262 poll_result->scancode = msg[1] << 8 | msg[3];
263 break;
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300264 case RC_BIT_RC6_0:
265 poll_result->scancode = msg[1] << 8 | msg[2];
266 break;
Mauro Carvalho Chehab105e3682012-12-15 08:29:11 -0300267 default:
268 poll_result->scancode = (msg[1] << 24) | (msg[2] << 16) |
269 (msg[3] << 8) | msg[4];
270 break;
271 }
Devin Heitmueller4b922532008-11-13 03:15:55 -0300272
273 return 0;
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300274}
275
276/**********************************************************
277 Polling code for em28xx
278 **********************************************************/
279
Frank Schaefer768da3db2013-01-13 10:20:41 -0300280static int em28xx_i2c_ir_handle_key(struct em28xx_IR *ir)
281{
Mauro Carvalho Chehabc7a45e52013-03-05 06:55:26 -0300282 struct em28xx *dev = ir->dev;
Frank Schaefer146b7ee2013-01-13 10:20:42 -0300283 static u32 ir_key;
Frank Schaefer768da3db2013-01-13 10:20:41 -0300284 int rc;
Frank Schaefer1d968cd2013-01-13 10:20:44 -0300285 struct i2c_client client;
Frank Schaefer768da3db2013-01-13 10:20:41 -0300286
Mauro Carvalho Chehabc7a45e52013-03-05 06:55:26 -0300287 client.adapter = &ir->dev->i2c_adap[dev->def_i2c_bus];
Frank Schaefer1d968cd2013-01-13 10:20:44 -0300288 client.addr = ir->i2c_dev_addr;
289
290 rc = ir->get_key_i2c(&client, &ir_key);
Frank Schaefer768da3db2013-01-13 10:20:41 -0300291 if (rc < 0) {
292 dprintk("ir->get_key_i2c() failed: %d\n", rc);
293 return rc;
294 }
295
296 if (rc) {
297 dprintk("%s: keycode = 0x%04x\n", __func__, ir_key);
298 rc_keydown(ir->rc, ir_key, 0);
299 }
300 return 0;
301}
302
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300303static void em28xx_ir_handle_key(struct em28xx_IR *ir)
304{
Devin Heitmueller4b922532008-11-13 03:15:55 -0300305 int result;
Devin Heitmueller4b922532008-11-13 03:15:55 -0300306 struct em28xx_ir_poll_result poll_result;
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300307
Devin Heitmueller4b922532008-11-13 03:15:55 -0300308 /* read the registers containing the IR status */
309 result = ir->get_key(ir, &poll_result);
Mauro Carvalho Chehab603044d2010-06-27 08:32:11 -0300310 if (unlikely(result < 0)) {
Frank Schaefer768da3db2013-01-13 10:20:41 -0300311 dprintk("ir->get_key() failed: %d\n", result);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300312 return;
Devin Heitmueller4b922532008-11-13 03:15:55 -0300313 }
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300314
Mauro Carvalho Chehab603044d2010-06-27 08:32:11 -0300315 if (unlikely(poll_result.read_count != ir->last_readcount)) {
Mauro Carvalho Chehab105e3682012-12-15 08:29:11 -0300316 dprintk("%s: toggle: %d, count: %d, key 0x%04x\n", __func__,
Mauro Carvalho Chehab603044d2010-06-27 08:32:11 -0300317 poll_result.toggle_bit, poll_result.read_count,
Mauro Carvalho Chehab105e3682012-12-15 08:29:11 -0300318 poll_result.scancode);
David Härdemana4695852010-06-07 16:32:23 -0300319 if (ir->full_code)
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,
David Härdemana4695852010-06-07 16:32:23 -0300322 poll_result.toggle_bit);
323 else
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300324 rc_keydown(ir->rc,
Mauro Carvalho Chehab105e3682012-12-15 08:29:11 -0300325 poll_result.scancode & 0xff,
David Härdemana4695852010-06-07 16:32:23 -0300326 poll_result.toggle_bit);
David Härdemana4695852010-06-07 16:32:23 -0300327
Mauro Carvalho Chehab20ae9742011-11-20 12:23:54 -0200328 if (ir->dev->chip_id == CHIP_ID_EM2874 ||
329 ir->dev->chip_id == CHIP_ID_EM2884)
Mauro Carvalho Chehab603044d2010-06-27 08:32:11 -0300330 /* The em2874 clears the readcount field every time the
331 register is read. The em2860/2880 datasheet says that it
332 is supposed to clear the readcount, but it doesn't. So with
333 the em2874, we are looking for a non-zero read count as
334 opposed to a readcount that is incrementing */
335 ir->last_readcount = 0;
336 else
337 ir->last_readcount = poll_result.read_count;
338 }
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300339}
340
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300341static void em28xx_ir_work(struct work_struct *work)
342{
Jean Delvaref263bac2009-03-07 07:43:01 -0300343 struct em28xx_IR *ir = container_of(work, struct em28xx_IR, work.work);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300344
Frank Schaefer9b4539b2013-01-13 10:20:45 -0300345 if (ir->i2c_dev_addr) /* external i2c device */
346 em28xx_i2c_ir_handle_key(ir);
347 else /* internal device */
348 em28xx_ir_handle_key(ir);
Jean Delvaref263bac2009-03-07 07:43:01 -0300349 schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300350}
351
David Härdemand8b4b582010-10-29 16:08:23 -0300352static int em28xx_ir_start(struct rc_dev *rc)
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300353{
David Härdemand8b4b582010-10-29 16:08:23 -0300354 struct em28xx_IR *ir = rc->priv;
Mauro Carvalho Chehabc373cab2010-04-07 22:57:04 -0300355
Frank Schaefer9b4539b2013-01-13 10:20:45 -0300356 INIT_DELAYED_WORK(&ir->work, em28xx_ir_work);
Jean Delvaref263bac2009-03-07 07:43:01 -0300357 schedule_delayed_work(&ir->work, 0);
Mauro Carvalho Chehabc373cab2010-04-07 22:57:04 -0300358
359 return 0;
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300360}
361
David Härdemand8b4b582010-10-29 16:08:23 -0300362static void em28xx_ir_stop(struct rc_dev *rc)
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300363{
David Härdemand8b4b582010-10-29 16:08:23 -0300364 struct em28xx_IR *ir = rc->priv;
Mauro Carvalho Chehabc373cab2010-04-07 22:57:04 -0300365
Jean Delvaref263bac2009-03-07 07:43:01 -0300366 cancel_delayed_work_sync(&ir->work);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300367}
368
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300369static int em2860_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_type)
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300370{
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300371 struct em28xx_IR *ir = rc_dev->priv;
372 struct em28xx *dev = ir->dev;
373
374 /* Adjust xclk based on IR table for RC5/NEC tables */
375 if (*rc_type & RC_BIT_RC5) {
376 dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
377 ir->full_code = 1;
378 *rc_type = RC_BIT_RC5;
379 } else if (*rc_type & RC_BIT_NEC) {
380 dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
381 ir->full_code = 1;
382 *rc_type = RC_BIT_NEC;
383 } else if (*rc_type & RC_BIT_UNKNOWN) {
384 *rc_type = RC_BIT_UNKNOWN;
385 } else {
386 *rc_type = ir->rc_type;
387 return -EINVAL;
388 }
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300389 em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
390 EM28XX_XCLK_IR_RC5_MODE);
391
392 ir->rc_type = *rc_type;
393
394 return 0;
395}
396
397static int em2874_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_type)
398{
David Härdemand8b4b582010-10-29 16:08:23 -0300399 struct em28xx_IR *ir = rc_dev->priv;
Mauro Carvalho Chehab950b0f52009-12-14 02:54:22 -0300400 struct em28xx *dev = ir->dev;
401 u8 ir_config = EM2874_IR_RC5;
Mauro Carvalho Chehab1bad4292009-12-05 08:27:49 -0300402
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300403 /* Adjust xclk and set type based on IR table for RC5/NEC/RC6 tables */
David Härdemanc003ab12012-10-11 19:11:54 -0300404 if (*rc_type & RC_BIT_RC5) {
Mauro Carvalho Chehab1bad4292009-12-05 08:27:49 -0300405 dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
406 ir->full_code = 1;
David Härdemanc003ab12012-10-11 19:11:54 -0300407 *rc_type = RC_BIT_RC5;
408 } else if (*rc_type & RC_BIT_NEC) {
Mauro Carvalho Chehab1bad4292009-12-05 08:27:49 -0300409 dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
Mauro Carvalho Chehab105e3682012-12-15 08:29:11 -0300410 ir_config = EM2874_IR_NEC | EM2874_IR_NEC_NO_PARITY;
Mauro Carvalho Chehab1bad4292009-12-05 08:27:49 -0300411 ir->full_code = 1;
David Härdemanc003ab12012-10-11 19:11:54 -0300412 *rc_type = RC_BIT_NEC;
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300413 } else if (*rc_type & RC_BIT_RC6_0) {
414 dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
415 ir_config = EM2874_IR_RC6_MODE_0;
416 ir->full_code = 1;
417 *rc_type = RC_BIT_RC6_0;
418 } else if (*rc_type & RC_BIT_UNKNOWN) {
419 *rc_type = RC_BIT_UNKNOWN;
420 } else {
421 *rc_type = ir->rc_type;
422 return -EINVAL;
423 }
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300424 em28xx_write_regs(dev, EM2874_R50_IR_CONFIG, &ir_config, 1);
Mauro Carvalho Chehab1bad4292009-12-05 08:27:49 -0300425 em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
426 EM28XX_XCLK_IR_RC5_MODE);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300427
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300428 ir->rc_type = *rc_type;
429
430 return 0;
431}
432static int em28xx_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_type)
433{
434 struct em28xx_IR *ir = rc_dev->priv;
435 struct em28xx *dev = ir->dev;
436
Devin Heitmueller4b922532008-11-13 03:15:55 -0300437 /* Setup the proper handler based on the chip */
438 switch (dev->chip_id) {
439 case CHIP_ID_EM2860:
440 case CHIP_ID_EM2883:
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300441 return em2860_ir_change_protocol(rc_dev, rc_type);
Mauro Carvalho Chehab20ae9742011-11-20 12:23:54 -0200442 case CHIP_ID_EM2884:
Devin Heitmueller4b922532008-11-13 03:15:55 -0300443 case CHIP_ID_EM2874:
Antti Palosaaria24ec442011-05-25 16:56:37 -0300444 case CHIP_ID_EM28174:
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300445 return em2874_ir_change_protocol(rc_dev, rc_type);
Devin Heitmueller4b922532008-11-13 03:15:55 -0300446 default:
Mauro Carvalho Chehab20ae9742011-11-20 12:23:54 -0200447 printk("Unrecognized em28xx chip id 0x%02x: IR not supported\n",
448 dev->chip_id);
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300449 return -EINVAL;
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300450 }
Mauro Carvalho Chehab950b0f52009-12-14 02:54:22 -0300451}
452
Frank Schaefer1d968cd2013-01-13 10:20:44 -0300453static int em28xx_probe_i2c_ir(struct em28xx *dev)
Ezequiel García9d9f4792012-03-26 09:13:33 -0300454{
Frank Schaefer768da3db2013-01-13 10:20:41 -0300455 int i = 0;
Ezequiel García9d9f4792012-03-26 09:13:33 -0300456 /* Leadtek winfast tv USBII deluxe can find a non working IR-device */
457 /* at address 0x18, so if that address is needed for another board in */
458 /* the future, please put it after 0x1f. */
Ezequiel García9d9f4792012-03-26 09:13:33 -0300459 const unsigned short addr_list[] = {
460 0x1f, 0x30, 0x47, I2C_CLIENT_END
461 };
462
Frank Schaefer768da3db2013-01-13 10:20:41 -0300463 while (addr_list[i] != I2C_CLIENT_END) {
Mauro Carvalho Chehabc7a45e52013-03-05 06:55:26 -0300464 if (i2c_probe_func_quick_read(&dev->i2c_adap[dev->def_i2c_bus], addr_list[i]) == 1)
Frank Schaefer1d968cd2013-01-13 10:20:44 -0300465 return addr_list[i];
Frank Schaefer768da3db2013-01-13 10:20:41 -0300466 i++;
Ezequiel García9d9f4792012-03-26 09:13:33 -0300467 }
468
Frank Schaefer1d968cd2013-01-13 10:20:44 -0300469 return -ENODEV;
Ezequiel García9d9f4792012-03-26 09:13:33 -0300470}
471
Ezequiel García769af212012-03-26 09:13:34 -0300472/**********************************************************
Frank Schaeferf52226092013-12-01 18:06:52 -0300473 Handle buttons
Ezequiel García769af212012-03-26 09:13:34 -0300474 **********************************************************/
475
Frank Schaeferf52226092013-12-01 18:06:52 -0300476static void em28xx_query_buttons(struct work_struct *work)
Ezequiel García769af212012-03-26 09:13:34 -0300477{
Ezequiel García769af212012-03-26 09:13:34 -0300478 struct em28xx *dev =
Frank Schaeferf52226092013-12-01 18:06:52 -0300479 container_of(work, struct em28xx, buttons_query_work.work);
480 u8 i, j;
481 int regval;
Frank Schaefer77634812013-12-01 18:06:53 -0300482 bool is_pressed, was_pressed;
Frank Schaefer6063d072013-12-01 18:06:56 -0300483 const struct em28xx_led *led;
Ezequiel García769af212012-03-26 09:13:34 -0300484
Frank Schaeferf52226092013-12-01 18:06:52 -0300485 /* Poll and evaluate all addresses */
486 for (i = 0; i < dev->num_button_polling_addresses; i++) {
487 /* Read value from register */
488 regval = em28xx_read_reg(dev, dev->button_polling_addresses[i]);
489 if (regval < 0)
490 continue;
491 /* Check states of the buttons and act */
492 j = 0;
493 while (dev->board.buttons[j].role >= 0 &&
494 dev->board.buttons[j].role < EM28XX_NUM_BUTTON_ROLES) {
495 struct em28xx_button *button = &dev->board.buttons[j];
496 /* Check if button uses the current address */
497 if (button->reg_r != dev->button_polling_addresses[i]) {
498 j++;
499 continue;
500 }
Frank Schaefer77634812013-12-01 18:06:53 -0300501 /* Determine if button is and was pressed last time */
502 is_pressed = regval & button->mask;
503 was_pressed = dev->button_polling_last_values[i]
504 & button->mask;
505 if (button->inverted) {
506 is_pressed = !is_pressed;
507 was_pressed = !was_pressed;
508 }
509 /* Clear button state (if needed) */
510 if (is_pressed && button->reg_clearing)
511 em28xx_write_reg(dev, button->reg_clearing,
512 (~regval & button->mask)
513 | (regval & ~button->mask));
Frank Schaeferf52226092013-12-01 18:06:52 -0300514 /* Handle button state */
Frank Schaefer77634812013-12-01 18:06:53 -0300515 if (!is_pressed || was_pressed) {
Frank Schaeferf52226092013-12-01 18:06:52 -0300516 j++;
517 continue;
518 }
519 switch (button->role) {
520 case EM28XX_BUTTON_SNAPSHOT:
521 /* Emulate the keypress */
522 input_report_key(dev->sbutton_input_dev,
523 EM28XX_SNAPSHOT_KEY, 1);
524 /* Unpress the key */
525 input_report_key(dev->sbutton_input_dev,
526 EM28XX_SNAPSHOT_KEY, 0);
527 break;
Frank Schaefer6063d072013-12-01 18:06:56 -0300528 case EM28XX_BUTTON_ILLUMINATION:
529 led = em28xx_find_led(dev,
530 EM28XX_LED_ILLUMINATION);
531 /* Switch illumination LED on/off */
532 if (led)
533 em28xx_toggle_reg_bits(dev,
534 led->gpio_reg,
535 led->gpio_mask);
536 break;
Frank Schaeferf52226092013-12-01 18:06:52 -0300537 default:
538 WARN_ONCE(1, "BUG: unhandled button role.");
539 }
Frank Schaeferf52226092013-12-01 18:06:52 -0300540 /* Next button */
541 j++;
542 }
Frank Schaefer77634812013-12-01 18:06:53 -0300543 /* Save current value for comparison during the next polling */
544 dev->button_polling_last_values[i] = regval;
Ezequiel García769af212012-03-26 09:13:34 -0300545 }
Ezequiel García769af212012-03-26 09:13:34 -0300546 /* Schedule next poll */
Frank Schaeferf52226092013-12-01 18:06:52 -0300547 schedule_delayed_work(&dev->buttons_query_work,
548 msecs_to_jiffies(EM28XX_BUTTONS_QUERY_INTERVAL));
Ezequiel García769af212012-03-26 09:13:34 -0300549}
550
Frank Schaeferf52226092013-12-01 18:06:52 -0300551static int em28xx_register_snapshot_button(struct em28xx *dev)
Ezequiel García769af212012-03-26 09:13:34 -0300552{
553 struct input_dev *input_dev;
554 int err;
555
556 em28xx_info("Registering snapshot button...\n");
557 input_dev = input_allocate_device();
558 if (!input_dev) {
559 em28xx_errdev("input_allocate_device failed\n");
Frank Schaeferf52226092013-12-01 18:06:52 -0300560 return -ENOMEM;
Ezequiel García769af212012-03-26 09:13:34 -0300561 }
562
563 usb_make_path(dev->udev, dev->snapshot_button_path,
564 sizeof(dev->snapshot_button_path));
565 strlcat(dev->snapshot_button_path, "/sbutton",
566 sizeof(dev->snapshot_button_path));
Ezequiel García769af212012-03-26 09:13:34 -0300567
568 input_dev->name = "em28xx snapshot button";
569 input_dev->phys = dev->snapshot_button_path;
570 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
571 set_bit(EM28XX_SNAPSHOT_KEY, input_dev->keybit);
572 input_dev->keycodesize = 0;
573 input_dev->keycodemax = 0;
574 input_dev->id.bustype = BUS_USB;
575 input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
576 input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
577 input_dev->id.version = 1;
578 input_dev->dev.parent = &dev->udev->dev;
579
580 err = input_register_device(input_dev);
581 if (err) {
582 em28xx_errdev("input_register_device failed\n");
583 input_free_device(input_dev);
Frank Schaeferf52226092013-12-01 18:06:52 -0300584 return err;
Ezequiel García769af212012-03-26 09:13:34 -0300585 }
586
587 dev->sbutton_input_dev = input_dev;
Frank Schaeferf52226092013-12-01 18:06:52 -0300588 return 0;
Ezequiel García769af212012-03-26 09:13:34 -0300589}
590
Frank Schaeferf52226092013-12-01 18:06:52 -0300591static void em28xx_init_buttons(struct em28xx *dev)
Ezequiel García769af212012-03-26 09:13:34 -0300592{
Frank Schaeferf52226092013-12-01 18:06:52 -0300593 u8 i = 0, j = 0;
594 bool addr_new = 0;
595
596 while (dev->board.buttons[i].role >= 0 &&
597 dev->board.buttons[i].role < EM28XX_NUM_BUTTON_ROLES) {
598 struct em28xx_button *button = &dev->board.buttons[i];
599 /* Check if polling address is already on the list */
600 addr_new = 1;
601 for (j = 0; j < dev->num_button_polling_addresses; j++) {
602 if (button->reg_r == dev->button_polling_addresses[j]) {
603 addr_new = 0;
604 break;
605 }
606 }
607 /* Check if max. number of polling addresses is exceeded */
608 if (addr_new && dev->num_button_polling_addresses
609 >= EM28XX_NUM_BUTTON_ADDRESSES_MAX) {
610 WARN_ONCE(1, "BUG: maximum number of button polling addresses exceeded.");
611 addr_new = 0;
612 }
Frank Schaefer6063d072013-12-01 18:06:56 -0300613 /* Button role specific checks and actions */
Frank Schaeferf52226092013-12-01 18:06:52 -0300614 if (button->role == EM28XX_BUTTON_SNAPSHOT) {
Frank Schaefer6063d072013-12-01 18:06:56 -0300615 /* Register input device */
Frank Schaeferf52226092013-12-01 18:06:52 -0300616 if (em28xx_register_snapshot_button(dev) < 0)
617 addr_new = 0;
Frank Schaefer6063d072013-12-01 18:06:56 -0300618 } else if (button->role == EM28XX_BUTTON_ILLUMINATION) {
619 /* Check sanity */
620 if (!em28xx_find_led(dev, EM28XX_LED_ILLUMINATION)) {
621 em28xx_errdev("BUG: illumination button defined, but no illumination LED.\n");
622 addr_new = 0;
623 }
Frank Schaeferf52226092013-12-01 18:06:52 -0300624 }
625 /* Add read address to list of polling addresses */
626 if (addr_new) {
627 unsigned int index = dev->num_button_polling_addresses;
628 dev->button_polling_addresses[index] = button->reg_r;
629 dev->num_button_polling_addresses++;
630 }
631 /* Next button */
632 i++;
633 }
634
635 /* Start polling */
636 if (dev->num_button_polling_addresses) {
Frank Schaefer77634812013-12-01 18:06:53 -0300637 memset(dev->button_polling_last_values, 0,
638 EM28XX_NUM_BUTTON_ADDRESSES_MAX);
Frank Schaeferf52226092013-12-01 18:06:52 -0300639 INIT_DELAYED_WORK(&dev->buttons_query_work,
640 em28xx_query_buttons);
641 schedule_delayed_work(&dev->buttons_query_work,
642 msecs_to_jiffies(EM28XX_BUTTONS_QUERY_INTERVAL));
643 }
644}
645
646static void em28xx_shutdown_buttons(struct em28xx *dev)
647{
648 /* Cancel polling */
649 cancel_delayed_work_sync(&dev->buttons_query_work);
650 /* Clear polling addresses list */
651 dev->num_button_polling_addresses = 0;
652 /* Deregister input devices */
Ezequiel García769af212012-03-26 09:13:34 -0300653 if (dev->sbutton_input_dev != NULL) {
654 em28xx_info("Deregistering snapshot button\n");
Ezequiel García769af212012-03-26 09:13:34 -0300655 input_unregister_device(dev->sbutton_input_dev);
656 dev->sbutton_input_dev = NULL;
657 }
Ezequiel García769af212012-03-26 09:13:34 -0300658}
659
Ezequiel Garcíaf4d4e762012-03-26 09:13:35 -0300660static int em28xx_ir_init(struct em28xx *dev)
Mauro Carvalho Chehab950b0f52009-12-14 02:54:22 -0300661{
662 struct em28xx_IR *ir;
David Härdemand8b4b582010-10-29 16:08:23 -0300663 struct rc_dev *rc;
Mauro Carvalho Chehab950b0f52009-12-14 02:54:22 -0300664 int err = -ENOMEM;
David Härdemanc003ab12012-10-11 19:11:54 -0300665 u64 rc_type;
Frank Schaefer1d968cd2013-01-13 10:20:44 -0300666 u16 i2c_rc_dev_addr = 0;
Mauro Carvalho Chehab950b0f52009-12-14 02:54:22 -0300667
Frank Schaeferf52226092013-12-01 18:06:52 -0300668 if (dev->board.buttons)
669 em28xx_init_buttons(dev);
Mauro Carvalho Chehab8303dc92013-01-04 17:27:47 -0300670
671 if (dev->board.has_ir_i2c) {
Frank Schaefer1d968cd2013-01-13 10:20:44 -0300672 i2c_rc_dev_addr = em28xx_probe_i2c_ir(dev);
673 if (!i2c_rc_dev_addr) {
Frank Schaefer768da3db2013-01-13 10:20:41 -0300674 dev->board.has_ir_i2c = 0;
675 em28xx_warn("No i2c IR remote control device found.\n");
676 return -ENODEV;
677 }
Mauro Carvalho Chehab8303dc92013-01-04 17:27:47 -0300678 }
679
Frank Schaefer768da3db2013-01-13 10:20:41 -0300680 if (dev->board.ir_codes == NULL && !dev->board.has_ir_i2c) {
Mauro Carvalho Chehab950b0f52009-12-14 02:54:22 -0300681 /* No remote control support */
Martin Blumenstinglb83f6712012-05-30 15:47:40 -0300682 em28xx_warn("Remote control support is not available for "
683 "this card.\n");
Mauro Carvalho Chehab950b0f52009-12-14 02:54:22 -0300684 return 0;
685 }
686
687 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
David Härdemand8b4b582010-10-29 16:08:23 -0300688 rc = rc_allocate_device();
689 if (!ir || !rc)
Frank Schaefer2f5741a2012-12-22 10:13:38 -0300690 goto error;
Mauro Carvalho Chehab950b0f52009-12-14 02:54:22 -0300691
692 /* record handles to ourself */
693 ir->dev = dev;
694 dev->ir = ir;
David Härdemand8b4b582010-10-29 16:08:23 -0300695 ir->rc = rc;
Mauro Carvalho Chehab950b0f52009-12-14 02:54:22 -0300696
David Härdemand8b4b582010-10-29 16:08:23 -0300697 rc->priv = ir;
David Härdemand8b4b582010-10-29 16:08:23 -0300698 rc->open = em28xx_ir_start;
699 rc->close = em28xx_ir_stop;
Mauro Carvalho Chehabc373cab2010-04-07 22:57:04 -0300700
Frank Schaefer768da3db2013-01-13 10:20:41 -0300701 if (dev->board.has_ir_i2c) { /* external i2c device */
702 switch (dev->model) {
703 case EM2800_BOARD_TERRATEC_CINERGY_200:
704 case EM2820_BOARD_TERRATEC_CINERGY_250:
705 rc->map_name = RC_MAP_EM_TERRATEC;
706 ir->get_key_i2c = em28xx_get_key_terratec;
707 break;
708 case EM2820_BOARD_PINNACLE_USB_2:
709 rc->map_name = RC_MAP_PINNACLE_GREY;
710 ir->get_key_i2c = em28xx_get_key_pinnacle_usb_grey;
711 break;
712 case EM2820_BOARD_HAUPPAUGE_WINTV_USB_2:
713 rc->map_name = RC_MAP_HAUPPAUGE;
714 ir->get_key_i2c = em28xx_get_key_em_haup;
715 rc->allowed_protos = RC_BIT_RC5;
716 break;
717 case EM2820_BOARD_LEADTEK_WINFAST_USBII_DELUXE:
718 rc->map_name = RC_MAP_WINFAST_USBII_DELUXE;
719 ir->get_key_i2c = em28xx_get_key_winfast_usbii_deluxe;
720 break;
721 default:
722 err = -ENODEV;
723 goto error;
724 }
Mauro Carvalho Chehab0dae8832012-12-15 08:29:12 -0300725
Frank Schaefer1d968cd2013-01-13 10:20:44 -0300726 ir->i2c_dev_addr = i2c_rc_dev_addr;
Frank Schaefer768da3db2013-01-13 10:20:41 -0300727 } else { /* internal device */
728 switch (dev->chip_id) {
729 case CHIP_ID_EM2860:
730 case CHIP_ID_EM2883:
731 rc->allowed_protos = RC_BIT_RC5 | RC_BIT_NEC;
732 ir->get_key = default_polling_getkey;
733 break;
734 case CHIP_ID_EM2884:
735 case CHIP_ID_EM2874:
736 case CHIP_ID_EM28174:
737 ir->get_key = em2874_polling_getkey;
738 rc->allowed_protos = RC_BIT_RC5 | RC_BIT_NEC |
739 RC_BIT_RC6_0;
740 break;
741 default:
742 err = -ENODEV;
743 goto error;
744 }
745
746 rc->change_protocol = em28xx_ir_change_protocol;
747 rc->map_name = dev->board.ir_codes;
748
749 /* By default, keep protocol field untouched */
750 rc_type = RC_BIT_UNKNOWN;
751 err = em28xx_ir_change_protocol(rc, &rc_type);
752 if (err)
753 goto error;
754 }
Mauro Carvalho Chehab950b0f52009-12-14 02:54:22 -0300755
Devin Heitmueller4b922532008-11-13 03:15:55 -0300756 /* This is how often we ask the chip for IR information */
757 ir->polling = 100; /* ms */
758
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300759 /* init input device */
Frank Schaefer768da3db2013-01-13 10:20:41 -0300760 snprintf(ir->name, sizeof(ir->name), "em28xx IR (%s)", dev->name);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300761
762 usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
763 strlcat(ir->phys, "/input0", sizeof(ir->phys));
764
David Härdemand8b4b582010-10-29 16:08:23 -0300765 rc->input_name = ir->name;
766 rc->input_phys = ir->phys;
767 rc->input_id.bustype = BUS_USB;
768 rc->input_id.version = 1;
769 rc->input_id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
770 rc->input_id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
771 rc->dev.parent = &dev->udev->dev;
David Härdemand8b4b582010-10-29 16:08:23 -0300772 rc->driver_name = MODULE_NAME;
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300773
774 /* all done */
David Härdemand8b4b582010-10-29 16:08:23 -0300775 err = rc_register_device(rc);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300776 if (err)
Frank Schaefer2f5741a2012-12-22 10:13:38 -0300777 goto error;
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300778
779 return 0;
David Härdemand8b4b582010-10-29 16:08:23 -0300780
Frank Schaefer2f5741a2012-12-22 10:13:38 -0300781error:
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300782 dev->ir = NULL;
David Härdemand8b4b582010-10-29 16:08:23 -0300783 rc_free_device(rc);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300784 kfree(ir);
785 return err;
786}
787
Ezequiel Garcíaf4d4e762012-03-26 09:13:35 -0300788static int em28xx_ir_fini(struct em28xx *dev)
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300789{
790 struct em28xx_IR *ir = dev->ir;
791
Frank Schaeferf52226092013-12-01 18:06:52 -0300792 em28xx_shutdown_buttons(dev);
Ezequiel García2fd6f8d2012-03-26 09:13:32 -0300793
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300794 /* skip detach on non attached boards */
795 if (!ir)
796 return 0;
797
Mauro Carvalho Chehab6d514772011-07-29 02:26:59 -0300798 if (ir->rc)
799 rc_unregister_device(ir->rc);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300800
801 /* done */
Mauro Carvalho Chehab6d514772011-07-29 02:26:59 -0300802 kfree(ir);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300803 dev->ir = NULL;
804 return 0;
805}
806
Ezequiel Garcíaf4d4e762012-03-26 09:13:35 -0300807static struct em28xx_ops rc_ops = {
808 .id = EM28XX_RC,
809 .name = "Em28xx Input Extension",
810 .init = em28xx_ir_init,
811 .fini = em28xx_ir_fini,
812};
813
814static int __init em28xx_rc_register(void)
815{
816 return em28xx_register_extension(&rc_ops);
817}
818
819static void __exit em28xx_rc_unregister(void)
820{
821 em28xx_unregister_extension(&rc_ops);
822}
823
824MODULE_LICENSE("GPL");
825MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
826MODULE_DESCRIPTION("Em28xx Input driver");
827
828module_init(em28xx_rc_register);
829module_exit(em28xx_rc_unregister);