1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun //
3*4882a593Smuzhiyun // handle em28xx IR remotes via linux kernel input layer.
4*4882a593Smuzhiyun //
5*4882a593Smuzhiyun // Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6*4882a593Smuzhiyun // Markus Rechberger <mrechberger@gmail.com>
7*4882a593Smuzhiyun // Mauro Carvalho Chehab <mchehab@kernel.org>
8*4882a593Smuzhiyun // Sascha Sommer <saschasommer@freenet.de>
9*4882a593Smuzhiyun //
10*4882a593Smuzhiyun // This program is free software; you can redistribute it and/or modify
11*4882a593Smuzhiyun // it under the terms of the GNU General Public License as published by
12*4882a593Smuzhiyun // the Free Software Foundation; either version 2 of the License, or
13*4882a593Smuzhiyun // (at your option) any later version.
14*4882a593Smuzhiyun //
15*4882a593Smuzhiyun // This program is distributed in the hope that it will be useful,
16*4882a593Smuzhiyun // but WITHOUT ANY WARRANTY; without even the implied warranty of
17*4882a593Smuzhiyun // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18*4882a593Smuzhiyun // GNU General Public License for more details.
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #include "em28xx.h"
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun #include <linux/module.h>
23*4882a593Smuzhiyun #include <linux/init.h>
24*4882a593Smuzhiyun #include <linux/delay.h>
25*4882a593Smuzhiyun #include <linux/interrupt.h>
26*4882a593Smuzhiyun #include <linux/usb.h>
27*4882a593Smuzhiyun #include <linux/usb/input.h>
28*4882a593Smuzhiyun #include <linux/slab.h>
29*4882a593Smuzhiyun #include <linux/bitrev.h>
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #define EM28XX_SNAPSHOT_KEY KEY_CAMERA
32*4882a593Smuzhiyun #define EM28XX_BUTTONS_DEBOUNCED_QUERY_INTERVAL 500 /* [ms] */
33*4882a593Smuzhiyun #define EM28XX_BUTTONS_VOLATILE_QUERY_INTERVAL 100 /* [ms] */
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun static unsigned int ir_debug;
36*4882a593Smuzhiyun module_param(ir_debug, int, 0644);
37*4882a593Smuzhiyun MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun #define MODULE_NAME "em28xx"
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun #define dprintk(fmt, arg...) do { \
42*4882a593Smuzhiyun if (ir_debug) \
43*4882a593Smuzhiyun dev_printk(KERN_DEBUG, &ir->dev->intf->dev, \
44*4882a593Smuzhiyun "input: %s: " fmt, __func__, ## arg); \
45*4882a593Smuzhiyun } while (0)
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun /*
48*4882a593Smuzhiyun * Polling structure used by em28xx IR's
49*4882a593Smuzhiyun */
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun struct em28xx_ir_poll_result {
52*4882a593Smuzhiyun unsigned int toggle_bit:1;
53*4882a593Smuzhiyun unsigned int read_count:7;
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun enum rc_proto protocol;
56*4882a593Smuzhiyun u32 scancode;
57*4882a593Smuzhiyun };
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun struct em28xx_IR {
60*4882a593Smuzhiyun struct em28xx *dev;
61*4882a593Smuzhiyun struct rc_dev *rc;
62*4882a593Smuzhiyun char phys[32];
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun /* poll decoder */
65*4882a593Smuzhiyun int polling;
66*4882a593Smuzhiyun struct delayed_work work;
67*4882a593Smuzhiyun unsigned int full_code:1;
68*4882a593Smuzhiyun unsigned int last_readcount;
69*4882a593Smuzhiyun u64 rc_proto;
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun struct i2c_client *i2c_client;
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun int (*get_key_i2c)(struct i2c_client *ir, enum rc_proto *protocol,
74*4882a593Smuzhiyun u32 *scancode);
75*4882a593Smuzhiyun int (*get_key)(struct em28xx_IR *ir, struct em28xx_ir_poll_result *r);
76*4882a593Smuzhiyun };
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun /*
79*4882a593Smuzhiyun * I2C IR based get keycodes - should be used with ir-kbd-i2c
80*4882a593Smuzhiyun */
81*4882a593Smuzhiyun
em28xx_get_key_terratec(struct i2c_client * i2c_dev,enum rc_proto * protocol,u32 * scancode)82*4882a593Smuzhiyun static int em28xx_get_key_terratec(struct i2c_client *i2c_dev,
83*4882a593Smuzhiyun enum rc_proto *protocol, u32 *scancode)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun int rc;
86*4882a593Smuzhiyun unsigned char b;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun /* poll IR chip */
89*4882a593Smuzhiyun rc = i2c_master_recv(i2c_dev, &b, 1);
90*4882a593Smuzhiyun if (rc != 1) {
91*4882a593Smuzhiyun if (rc < 0)
92*4882a593Smuzhiyun return rc;
93*4882a593Smuzhiyun return -EIO;
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun /*
97*4882a593Smuzhiyun * it seems that 0xFE indicates that a button is still hold
98*4882a593Smuzhiyun * down, while 0xff indicates that no button is hold down.
99*4882a593Smuzhiyun */
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun if (b == 0xff)
102*4882a593Smuzhiyun return 0;
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun if (b == 0xfe)
105*4882a593Smuzhiyun /* keep old data */
106*4882a593Smuzhiyun return 1;
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun *protocol = RC_PROTO_UNKNOWN;
109*4882a593Smuzhiyun *scancode = b;
110*4882a593Smuzhiyun return 1;
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun
em28xx_get_key_em_haup(struct i2c_client * i2c_dev,enum rc_proto * protocol,u32 * scancode)113*4882a593Smuzhiyun static int em28xx_get_key_em_haup(struct i2c_client *i2c_dev,
114*4882a593Smuzhiyun enum rc_proto *protocol, u32 *scancode)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun unsigned char buf[2];
117*4882a593Smuzhiyun int size;
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun /* poll IR chip */
120*4882a593Smuzhiyun size = i2c_master_recv(i2c_dev, buf, sizeof(buf));
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun if (size != 2)
123*4882a593Smuzhiyun return -EIO;
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun /* Does eliminate repeated parity code */
126*4882a593Smuzhiyun if (buf[1] == 0xff)
127*4882a593Smuzhiyun return 0;
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun /*
130*4882a593Smuzhiyun * Rearranges bits to the right order.
131*4882a593Smuzhiyun * The bit order were determined experimentally by using
132*4882a593Smuzhiyun * The original Hauppauge Grey IR and another RC5 that uses addr=0x08
133*4882a593Smuzhiyun * The RC5 code has 14 bits, but we've experimentally determined
134*4882a593Smuzhiyun * the meaning for only 11 bits.
135*4882a593Smuzhiyun * So, the code translation is not complete. Yet, it is enough to
136*4882a593Smuzhiyun * work with the provided RC5 IR.
137*4882a593Smuzhiyun */
138*4882a593Smuzhiyun *protocol = RC_PROTO_RC5;
139*4882a593Smuzhiyun *scancode = (bitrev8(buf[1]) & 0x1f) << 8 | bitrev8(buf[0]) >> 2;
140*4882a593Smuzhiyun return 1;
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun
em28xx_get_key_pinnacle_usb_grey(struct i2c_client * i2c_dev,enum rc_proto * protocol,u32 * scancode)143*4882a593Smuzhiyun static int em28xx_get_key_pinnacle_usb_grey(struct i2c_client *i2c_dev,
144*4882a593Smuzhiyun enum rc_proto *protocol,
145*4882a593Smuzhiyun u32 *scancode)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun unsigned char buf[3];
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun /* poll IR chip */
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun if (i2c_master_recv(i2c_dev, buf, 3) != 3)
152*4882a593Smuzhiyun return -EIO;
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun if (buf[0] != 0x00)
155*4882a593Smuzhiyun return 0;
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun *protocol = RC_PROTO_UNKNOWN;
158*4882a593Smuzhiyun *scancode = buf[2] & 0x3f;
159*4882a593Smuzhiyun return 1;
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun
em28xx_get_key_winfast_usbii_deluxe(struct i2c_client * i2c_dev,enum rc_proto * protocol,u32 * scancode)162*4882a593Smuzhiyun static int em28xx_get_key_winfast_usbii_deluxe(struct i2c_client *i2c_dev,
163*4882a593Smuzhiyun enum rc_proto *protocol,
164*4882a593Smuzhiyun u32 *scancode)
165*4882a593Smuzhiyun {
166*4882a593Smuzhiyun unsigned char subaddr, keydetect, key;
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun struct i2c_msg msg[] = {
169*4882a593Smuzhiyun {
170*4882a593Smuzhiyun .addr = i2c_dev->addr,
171*4882a593Smuzhiyun .flags = 0,
172*4882a593Smuzhiyun .buf = &subaddr, .len = 1
173*4882a593Smuzhiyun }, {
174*4882a593Smuzhiyun .addr = i2c_dev->addr,
175*4882a593Smuzhiyun .flags = I2C_M_RD,
176*4882a593Smuzhiyun .buf = &keydetect,
177*4882a593Smuzhiyun .len = 1
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun };
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun subaddr = 0x10;
182*4882a593Smuzhiyun if (i2c_transfer(i2c_dev->adapter, msg, 2) != 2)
183*4882a593Smuzhiyun return -EIO;
184*4882a593Smuzhiyun if (keydetect == 0x00)
185*4882a593Smuzhiyun return 0;
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun subaddr = 0x00;
188*4882a593Smuzhiyun msg[1].buf = &key;
189*4882a593Smuzhiyun if (i2c_transfer(i2c_dev->adapter, msg, 2) != 2)
190*4882a593Smuzhiyun return -EIO;
191*4882a593Smuzhiyun if (key == 0x00)
192*4882a593Smuzhiyun return 0;
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun *protocol = RC_PROTO_UNKNOWN;
195*4882a593Smuzhiyun *scancode = key;
196*4882a593Smuzhiyun return 1;
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun /*
200*4882a593Smuzhiyun * Poll based get keycode functions
201*4882a593Smuzhiyun */
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun /* This is for the em2860/em2880 */
default_polling_getkey(struct em28xx_IR * ir,struct em28xx_ir_poll_result * poll_result)204*4882a593Smuzhiyun static int default_polling_getkey(struct em28xx_IR *ir,
205*4882a593Smuzhiyun struct em28xx_ir_poll_result *poll_result)
206*4882a593Smuzhiyun {
207*4882a593Smuzhiyun struct em28xx *dev = ir->dev;
208*4882a593Smuzhiyun int rc;
209*4882a593Smuzhiyun u8 msg[3] = { 0, 0, 0 };
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun /*
212*4882a593Smuzhiyun * Read key toggle, brand, and key code
213*4882a593Smuzhiyun * on registers 0x45, 0x46 and 0x47
214*4882a593Smuzhiyun */
215*4882a593Smuzhiyun rc = dev->em28xx_read_reg_req_len(dev, 0, EM28XX_R45_IR,
216*4882a593Smuzhiyun msg, sizeof(msg));
217*4882a593Smuzhiyun if (rc < 0)
218*4882a593Smuzhiyun return rc;
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun /* Infrared toggle (Reg 0x45[7]) */
221*4882a593Smuzhiyun poll_result->toggle_bit = (msg[0] >> 7);
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun /* Infrared read count (Reg 0x45[6:0] */
224*4882a593Smuzhiyun poll_result->read_count = (msg[0] & 0x7f);
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun /* Remote Control Address/Data (Regs 0x46/0x47) */
227*4882a593Smuzhiyun switch (ir->rc_proto) {
228*4882a593Smuzhiyun case RC_PROTO_BIT_RC5:
229*4882a593Smuzhiyun poll_result->protocol = RC_PROTO_RC5;
230*4882a593Smuzhiyun poll_result->scancode = RC_SCANCODE_RC5(msg[1], msg[2]);
231*4882a593Smuzhiyun break;
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun case RC_PROTO_BIT_NEC:
234*4882a593Smuzhiyun poll_result->protocol = RC_PROTO_NEC;
235*4882a593Smuzhiyun poll_result->scancode = RC_SCANCODE_NEC(msg[1], msg[2]);
236*4882a593Smuzhiyun break;
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun default:
239*4882a593Smuzhiyun poll_result->protocol = RC_PROTO_UNKNOWN;
240*4882a593Smuzhiyun poll_result->scancode = msg[1] << 8 | msg[2];
241*4882a593Smuzhiyun break;
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun return 0;
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun
em2874_polling_getkey(struct em28xx_IR * ir,struct em28xx_ir_poll_result * poll_result)247*4882a593Smuzhiyun static int em2874_polling_getkey(struct em28xx_IR *ir,
248*4882a593Smuzhiyun struct em28xx_ir_poll_result *poll_result)
249*4882a593Smuzhiyun {
250*4882a593Smuzhiyun struct em28xx *dev = ir->dev;
251*4882a593Smuzhiyun int rc;
252*4882a593Smuzhiyun u8 msg[5] = { 0, 0, 0, 0, 0 };
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun /*
255*4882a593Smuzhiyun * Read key toggle, brand, and key code
256*4882a593Smuzhiyun * on registers 0x51-55
257*4882a593Smuzhiyun */
258*4882a593Smuzhiyun rc = dev->em28xx_read_reg_req_len(dev, 0, EM2874_R51_IR,
259*4882a593Smuzhiyun msg, sizeof(msg));
260*4882a593Smuzhiyun if (rc < 0)
261*4882a593Smuzhiyun return rc;
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun /* Infrared toggle (Reg 0x51[7]) */
264*4882a593Smuzhiyun poll_result->toggle_bit = (msg[0] >> 7);
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun /* Infrared read count (Reg 0x51[6:0] */
267*4882a593Smuzhiyun poll_result->read_count = (msg[0] & 0x7f);
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun /*
270*4882a593Smuzhiyun * Remote Control Address (Reg 0x52)
271*4882a593Smuzhiyun * Remote Control Data (Reg 0x53-0x55)
272*4882a593Smuzhiyun */
273*4882a593Smuzhiyun switch (ir->rc_proto) {
274*4882a593Smuzhiyun case RC_PROTO_BIT_RC5:
275*4882a593Smuzhiyun poll_result->protocol = RC_PROTO_RC5;
276*4882a593Smuzhiyun poll_result->scancode = RC_SCANCODE_RC5(msg[1], msg[2]);
277*4882a593Smuzhiyun break;
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun case RC_PROTO_BIT_NEC:
280*4882a593Smuzhiyun poll_result->scancode = ir_nec_bytes_to_scancode(msg[1], msg[2], msg[3], msg[4],
281*4882a593Smuzhiyun &poll_result->protocol);
282*4882a593Smuzhiyun break;
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun case RC_PROTO_BIT_RC6_0:
285*4882a593Smuzhiyun poll_result->protocol = RC_PROTO_RC6_0;
286*4882a593Smuzhiyun poll_result->scancode = RC_SCANCODE_RC6_0(msg[1], msg[2]);
287*4882a593Smuzhiyun break;
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun default:
290*4882a593Smuzhiyun poll_result->protocol = RC_PROTO_UNKNOWN;
291*4882a593Smuzhiyun poll_result->scancode = (msg[1] << 24) | (msg[2] << 16) |
292*4882a593Smuzhiyun (msg[3] << 8) | msg[4];
293*4882a593Smuzhiyun break;
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun return 0;
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun /*
300*4882a593Smuzhiyun * Polling code for em28xx
301*4882a593Smuzhiyun */
302*4882a593Smuzhiyun
em28xx_i2c_ir_handle_key(struct em28xx_IR * ir)303*4882a593Smuzhiyun static int em28xx_i2c_ir_handle_key(struct em28xx_IR *ir)
304*4882a593Smuzhiyun {
305*4882a593Smuzhiyun static u32 scancode;
306*4882a593Smuzhiyun enum rc_proto protocol;
307*4882a593Smuzhiyun int rc;
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun rc = ir->get_key_i2c(ir->i2c_client, &protocol, &scancode);
310*4882a593Smuzhiyun if (rc < 0) {
311*4882a593Smuzhiyun dprintk("ir->get_key_i2c() failed: %d\n", rc);
312*4882a593Smuzhiyun return rc;
313*4882a593Smuzhiyun }
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun if (rc) {
316*4882a593Smuzhiyun dprintk("%s: proto = 0x%04x, scancode = 0x%04x\n",
317*4882a593Smuzhiyun __func__, protocol, scancode);
318*4882a593Smuzhiyun rc_keydown(ir->rc, protocol, scancode, 0);
319*4882a593Smuzhiyun }
320*4882a593Smuzhiyun return 0;
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun
em28xx_ir_handle_key(struct em28xx_IR * ir)323*4882a593Smuzhiyun static void em28xx_ir_handle_key(struct em28xx_IR *ir)
324*4882a593Smuzhiyun {
325*4882a593Smuzhiyun int result;
326*4882a593Smuzhiyun struct em28xx_ir_poll_result poll_result;
327*4882a593Smuzhiyun
328*4882a593Smuzhiyun /* read the registers containing the IR status */
329*4882a593Smuzhiyun result = ir->get_key(ir, &poll_result);
330*4882a593Smuzhiyun if (unlikely(result < 0)) {
331*4882a593Smuzhiyun dprintk("ir->get_key() failed: %d\n", result);
332*4882a593Smuzhiyun return;
333*4882a593Smuzhiyun }
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun if (unlikely(poll_result.read_count != ir->last_readcount)) {
336*4882a593Smuzhiyun dprintk("%s: toggle: %d, count: %d, key 0x%04x\n", __func__,
337*4882a593Smuzhiyun poll_result.toggle_bit, poll_result.read_count,
338*4882a593Smuzhiyun poll_result.scancode);
339*4882a593Smuzhiyun if (ir->full_code)
340*4882a593Smuzhiyun rc_keydown(ir->rc,
341*4882a593Smuzhiyun poll_result.protocol,
342*4882a593Smuzhiyun poll_result.scancode,
343*4882a593Smuzhiyun poll_result.toggle_bit);
344*4882a593Smuzhiyun else
345*4882a593Smuzhiyun rc_keydown(ir->rc,
346*4882a593Smuzhiyun RC_PROTO_UNKNOWN,
347*4882a593Smuzhiyun poll_result.scancode & 0xff,
348*4882a593Smuzhiyun poll_result.toggle_bit);
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun if (ir->dev->chip_id == CHIP_ID_EM2874 ||
351*4882a593Smuzhiyun ir->dev->chip_id == CHIP_ID_EM2884)
352*4882a593Smuzhiyun /*
353*4882a593Smuzhiyun * The em2874 clears the readcount field every time the
354*4882a593Smuzhiyun * register is read. The em2860/2880 datasheet says
355*4882a593Smuzhiyun * that it is supposed to clear the readcount, but it
356*4882a593Smuzhiyun * doesn't. So with the em2874, we are looking for a
357*4882a593Smuzhiyun * non-zero read count as opposed to a readcount
358*4882a593Smuzhiyun * that is incrementing
359*4882a593Smuzhiyun */
360*4882a593Smuzhiyun ir->last_readcount = 0;
361*4882a593Smuzhiyun else
362*4882a593Smuzhiyun ir->last_readcount = poll_result.read_count;
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun }
365*4882a593Smuzhiyun
em28xx_ir_work(struct work_struct * work)366*4882a593Smuzhiyun static void em28xx_ir_work(struct work_struct *work)
367*4882a593Smuzhiyun {
368*4882a593Smuzhiyun struct em28xx_IR *ir = container_of(work, struct em28xx_IR, work.work);
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun if (ir->i2c_client) /* external i2c device */
371*4882a593Smuzhiyun em28xx_i2c_ir_handle_key(ir);
372*4882a593Smuzhiyun else /* internal device */
373*4882a593Smuzhiyun em28xx_ir_handle_key(ir);
374*4882a593Smuzhiyun schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
375*4882a593Smuzhiyun }
376*4882a593Smuzhiyun
em28xx_ir_start(struct rc_dev * rc)377*4882a593Smuzhiyun static int em28xx_ir_start(struct rc_dev *rc)
378*4882a593Smuzhiyun {
379*4882a593Smuzhiyun struct em28xx_IR *ir = rc->priv;
380*4882a593Smuzhiyun
381*4882a593Smuzhiyun INIT_DELAYED_WORK(&ir->work, em28xx_ir_work);
382*4882a593Smuzhiyun schedule_delayed_work(&ir->work, 0);
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun return 0;
385*4882a593Smuzhiyun }
386*4882a593Smuzhiyun
em28xx_ir_stop(struct rc_dev * rc)387*4882a593Smuzhiyun static void em28xx_ir_stop(struct rc_dev *rc)
388*4882a593Smuzhiyun {
389*4882a593Smuzhiyun struct em28xx_IR *ir = rc->priv;
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun cancel_delayed_work_sync(&ir->work);
392*4882a593Smuzhiyun }
393*4882a593Smuzhiyun
em2860_ir_change_protocol(struct rc_dev * rc_dev,u64 * rc_proto)394*4882a593Smuzhiyun static int em2860_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_proto)
395*4882a593Smuzhiyun {
396*4882a593Smuzhiyun struct em28xx_IR *ir = rc_dev->priv;
397*4882a593Smuzhiyun struct em28xx *dev = ir->dev;
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun /* Adjust xclk based on IR table for RC5/NEC tables */
400*4882a593Smuzhiyun if (*rc_proto & RC_PROTO_BIT_RC5) {
401*4882a593Smuzhiyun dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
402*4882a593Smuzhiyun ir->full_code = 1;
403*4882a593Smuzhiyun *rc_proto = RC_PROTO_BIT_RC5;
404*4882a593Smuzhiyun } else if (*rc_proto & RC_PROTO_BIT_NEC) {
405*4882a593Smuzhiyun dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
406*4882a593Smuzhiyun ir->full_code = 1;
407*4882a593Smuzhiyun *rc_proto = RC_PROTO_BIT_NEC;
408*4882a593Smuzhiyun } else if (*rc_proto & RC_PROTO_BIT_UNKNOWN) {
409*4882a593Smuzhiyun *rc_proto = RC_PROTO_BIT_UNKNOWN;
410*4882a593Smuzhiyun } else {
411*4882a593Smuzhiyun *rc_proto = ir->rc_proto;
412*4882a593Smuzhiyun return -EINVAL;
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
415*4882a593Smuzhiyun EM28XX_XCLK_IR_RC5_MODE);
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun ir->rc_proto = *rc_proto;
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun return 0;
420*4882a593Smuzhiyun }
421*4882a593Smuzhiyun
em2874_ir_change_protocol(struct rc_dev * rc_dev,u64 * rc_proto)422*4882a593Smuzhiyun static int em2874_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_proto)
423*4882a593Smuzhiyun {
424*4882a593Smuzhiyun struct em28xx_IR *ir = rc_dev->priv;
425*4882a593Smuzhiyun struct em28xx *dev = ir->dev;
426*4882a593Smuzhiyun u8 ir_config = EM2874_IR_RC5;
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun /* Adjust xclk and set type based on IR table for RC5/NEC/RC6 tables */
429*4882a593Smuzhiyun if (*rc_proto & RC_PROTO_BIT_RC5) {
430*4882a593Smuzhiyun dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
431*4882a593Smuzhiyun ir->full_code = 1;
432*4882a593Smuzhiyun *rc_proto = RC_PROTO_BIT_RC5;
433*4882a593Smuzhiyun } else if (*rc_proto & RC_PROTO_BIT_NEC) {
434*4882a593Smuzhiyun dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
435*4882a593Smuzhiyun ir_config = EM2874_IR_NEC | EM2874_IR_NEC_NO_PARITY;
436*4882a593Smuzhiyun ir->full_code = 1;
437*4882a593Smuzhiyun *rc_proto = RC_PROTO_BIT_NEC;
438*4882a593Smuzhiyun } else if (*rc_proto & RC_PROTO_BIT_RC6_0) {
439*4882a593Smuzhiyun dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
440*4882a593Smuzhiyun ir_config = EM2874_IR_RC6_MODE_0;
441*4882a593Smuzhiyun ir->full_code = 1;
442*4882a593Smuzhiyun *rc_proto = RC_PROTO_BIT_RC6_0;
443*4882a593Smuzhiyun } else if (*rc_proto & RC_PROTO_BIT_UNKNOWN) {
444*4882a593Smuzhiyun *rc_proto = RC_PROTO_BIT_UNKNOWN;
445*4882a593Smuzhiyun } else {
446*4882a593Smuzhiyun *rc_proto = ir->rc_proto;
447*4882a593Smuzhiyun return -EINVAL;
448*4882a593Smuzhiyun }
449*4882a593Smuzhiyun em28xx_write_regs(dev, EM2874_R50_IR_CONFIG, &ir_config, 1);
450*4882a593Smuzhiyun em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
451*4882a593Smuzhiyun EM28XX_XCLK_IR_RC5_MODE);
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun ir->rc_proto = *rc_proto;
454*4882a593Smuzhiyun
455*4882a593Smuzhiyun return 0;
456*4882a593Smuzhiyun }
457*4882a593Smuzhiyun
em28xx_ir_change_protocol(struct rc_dev * rc_dev,u64 * rc_proto)458*4882a593Smuzhiyun static int em28xx_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_proto)
459*4882a593Smuzhiyun {
460*4882a593Smuzhiyun struct em28xx_IR *ir = rc_dev->priv;
461*4882a593Smuzhiyun struct em28xx *dev = ir->dev;
462*4882a593Smuzhiyun
463*4882a593Smuzhiyun /* Setup the proper handler based on the chip */
464*4882a593Smuzhiyun switch (dev->chip_id) {
465*4882a593Smuzhiyun case CHIP_ID_EM2860:
466*4882a593Smuzhiyun case CHIP_ID_EM2883:
467*4882a593Smuzhiyun return em2860_ir_change_protocol(rc_dev, rc_proto);
468*4882a593Smuzhiyun case CHIP_ID_EM2884:
469*4882a593Smuzhiyun case CHIP_ID_EM2874:
470*4882a593Smuzhiyun case CHIP_ID_EM28174:
471*4882a593Smuzhiyun case CHIP_ID_EM28178:
472*4882a593Smuzhiyun return em2874_ir_change_protocol(rc_dev, rc_proto);
473*4882a593Smuzhiyun default:
474*4882a593Smuzhiyun dev_err(&ir->dev->intf->dev,
475*4882a593Smuzhiyun "Unrecognized em28xx chip id 0x%02x: IR not supported\n",
476*4882a593Smuzhiyun dev->chip_id);
477*4882a593Smuzhiyun return -EINVAL;
478*4882a593Smuzhiyun }
479*4882a593Smuzhiyun }
480*4882a593Smuzhiyun
em28xx_probe_i2c_ir(struct em28xx * dev)481*4882a593Smuzhiyun static int em28xx_probe_i2c_ir(struct em28xx *dev)
482*4882a593Smuzhiyun {
483*4882a593Smuzhiyun int i = 0;
484*4882a593Smuzhiyun /*
485*4882a593Smuzhiyun * Leadtek winfast tv USBII deluxe can find a non working IR-device
486*4882a593Smuzhiyun * at address 0x18, so if that address is needed for another board in
487*4882a593Smuzhiyun * the future, please put it after 0x1f.
488*4882a593Smuzhiyun */
489*4882a593Smuzhiyun static const unsigned short addr_list[] = {
490*4882a593Smuzhiyun 0x1f, 0x30, 0x47, I2C_CLIENT_END
491*4882a593Smuzhiyun };
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun while (addr_list[i] != I2C_CLIENT_END) {
494*4882a593Smuzhiyun if (i2c_probe_func_quick_read(&dev->i2c_adap[dev->def_i2c_bus],
495*4882a593Smuzhiyun addr_list[i]) == 1)
496*4882a593Smuzhiyun return addr_list[i];
497*4882a593Smuzhiyun i++;
498*4882a593Smuzhiyun }
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun return -ENODEV;
501*4882a593Smuzhiyun }
502*4882a593Smuzhiyun
503*4882a593Smuzhiyun /*
504*4882a593Smuzhiyun * Handle buttons
505*4882a593Smuzhiyun */
506*4882a593Smuzhiyun
em28xx_query_buttons(struct work_struct * work)507*4882a593Smuzhiyun static void em28xx_query_buttons(struct work_struct *work)
508*4882a593Smuzhiyun {
509*4882a593Smuzhiyun struct em28xx *dev =
510*4882a593Smuzhiyun container_of(work, struct em28xx, buttons_query_work.work);
511*4882a593Smuzhiyun u8 i, j;
512*4882a593Smuzhiyun int regval;
513*4882a593Smuzhiyun bool is_pressed, was_pressed;
514*4882a593Smuzhiyun const struct em28xx_led *led;
515*4882a593Smuzhiyun
516*4882a593Smuzhiyun /* Poll and evaluate all addresses */
517*4882a593Smuzhiyun for (i = 0; i < dev->num_button_polling_addresses; i++) {
518*4882a593Smuzhiyun /* Read value from register */
519*4882a593Smuzhiyun regval = em28xx_read_reg(dev, dev->button_polling_addresses[i]);
520*4882a593Smuzhiyun if (regval < 0)
521*4882a593Smuzhiyun continue;
522*4882a593Smuzhiyun /* Check states of the buttons and act */
523*4882a593Smuzhiyun j = 0;
524*4882a593Smuzhiyun while (dev->board.buttons[j].role >= 0 &&
525*4882a593Smuzhiyun dev->board.buttons[j].role < EM28XX_NUM_BUTTON_ROLES) {
526*4882a593Smuzhiyun const struct em28xx_button *button;
527*4882a593Smuzhiyun
528*4882a593Smuzhiyun button = &dev->board.buttons[j];
529*4882a593Smuzhiyun
530*4882a593Smuzhiyun /* Check if button uses the current address */
531*4882a593Smuzhiyun if (button->reg_r != dev->button_polling_addresses[i]) {
532*4882a593Smuzhiyun j++;
533*4882a593Smuzhiyun continue;
534*4882a593Smuzhiyun }
535*4882a593Smuzhiyun /* Determine if button is and was pressed last time */
536*4882a593Smuzhiyun is_pressed = regval & button->mask;
537*4882a593Smuzhiyun was_pressed = dev->button_polling_last_values[i]
538*4882a593Smuzhiyun & button->mask;
539*4882a593Smuzhiyun if (button->inverted) {
540*4882a593Smuzhiyun is_pressed = !is_pressed;
541*4882a593Smuzhiyun was_pressed = !was_pressed;
542*4882a593Smuzhiyun }
543*4882a593Smuzhiyun /* Clear button state (if needed) */
544*4882a593Smuzhiyun if (is_pressed && button->reg_clearing)
545*4882a593Smuzhiyun em28xx_write_reg(dev, button->reg_clearing,
546*4882a593Smuzhiyun (~regval & button->mask)
547*4882a593Smuzhiyun | (regval & ~button->mask));
548*4882a593Smuzhiyun /* Handle button state */
549*4882a593Smuzhiyun if (!is_pressed || was_pressed) {
550*4882a593Smuzhiyun j++;
551*4882a593Smuzhiyun continue;
552*4882a593Smuzhiyun }
553*4882a593Smuzhiyun switch (button->role) {
554*4882a593Smuzhiyun case EM28XX_BUTTON_SNAPSHOT:
555*4882a593Smuzhiyun /* Emulate the keypress */
556*4882a593Smuzhiyun input_report_key(dev->sbutton_input_dev,
557*4882a593Smuzhiyun EM28XX_SNAPSHOT_KEY, 1);
558*4882a593Smuzhiyun /* Unpress the key */
559*4882a593Smuzhiyun input_report_key(dev->sbutton_input_dev,
560*4882a593Smuzhiyun EM28XX_SNAPSHOT_KEY, 0);
561*4882a593Smuzhiyun break;
562*4882a593Smuzhiyun case EM28XX_BUTTON_ILLUMINATION:
563*4882a593Smuzhiyun led = em28xx_find_led(dev,
564*4882a593Smuzhiyun EM28XX_LED_ILLUMINATION);
565*4882a593Smuzhiyun /* Switch illumination LED on/off */
566*4882a593Smuzhiyun if (led)
567*4882a593Smuzhiyun em28xx_toggle_reg_bits(dev,
568*4882a593Smuzhiyun led->gpio_reg,
569*4882a593Smuzhiyun led->gpio_mask);
570*4882a593Smuzhiyun break;
571*4882a593Smuzhiyun default:
572*4882a593Smuzhiyun WARN_ONCE(1, "BUG: unhandled button role.");
573*4882a593Smuzhiyun }
574*4882a593Smuzhiyun /* Next button */
575*4882a593Smuzhiyun j++;
576*4882a593Smuzhiyun }
577*4882a593Smuzhiyun /* Save current value for comparison during the next polling */
578*4882a593Smuzhiyun dev->button_polling_last_values[i] = regval;
579*4882a593Smuzhiyun }
580*4882a593Smuzhiyun /* Schedule next poll */
581*4882a593Smuzhiyun schedule_delayed_work(&dev->buttons_query_work,
582*4882a593Smuzhiyun msecs_to_jiffies(dev->button_polling_interval));
583*4882a593Smuzhiyun }
584*4882a593Smuzhiyun
em28xx_register_snapshot_button(struct em28xx * dev)585*4882a593Smuzhiyun static int em28xx_register_snapshot_button(struct em28xx *dev)
586*4882a593Smuzhiyun {
587*4882a593Smuzhiyun struct usb_device *udev = interface_to_usbdev(dev->intf);
588*4882a593Smuzhiyun struct input_dev *input_dev;
589*4882a593Smuzhiyun int err;
590*4882a593Smuzhiyun
591*4882a593Smuzhiyun dev_info(&dev->intf->dev, "Registering snapshot button...\n");
592*4882a593Smuzhiyun input_dev = input_allocate_device();
593*4882a593Smuzhiyun if (!input_dev)
594*4882a593Smuzhiyun return -ENOMEM;
595*4882a593Smuzhiyun
596*4882a593Smuzhiyun usb_make_path(udev, dev->snapshot_button_path,
597*4882a593Smuzhiyun sizeof(dev->snapshot_button_path));
598*4882a593Smuzhiyun strlcat(dev->snapshot_button_path, "/sbutton",
599*4882a593Smuzhiyun sizeof(dev->snapshot_button_path));
600*4882a593Smuzhiyun
601*4882a593Smuzhiyun input_dev->name = "em28xx snapshot button";
602*4882a593Smuzhiyun input_dev->phys = dev->snapshot_button_path;
603*4882a593Smuzhiyun input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
604*4882a593Smuzhiyun set_bit(EM28XX_SNAPSHOT_KEY, input_dev->keybit);
605*4882a593Smuzhiyun input_dev->keycodesize = 0;
606*4882a593Smuzhiyun input_dev->keycodemax = 0;
607*4882a593Smuzhiyun usb_to_input_id(udev, &input_dev->id);
608*4882a593Smuzhiyun input_dev->dev.parent = &dev->intf->dev;
609*4882a593Smuzhiyun
610*4882a593Smuzhiyun err = input_register_device(input_dev);
611*4882a593Smuzhiyun if (err) {
612*4882a593Smuzhiyun dev_err(&dev->intf->dev, "input_register_device failed\n");
613*4882a593Smuzhiyun input_free_device(input_dev);
614*4882a593Smuzhiyun return err;
615*4882a593Smuzhiyun }
616*4882a593Smuzhiyun
617*4882a593Smuzhiyun dev->sbutton_input_dev = input_dev;
618*4882a593Smuzhiyun return 0;
619*4882a593Smuzhiyun }
620*4882a593Smuzhiyun
em28xx_init_buttons(struct em28xx * dev)621*4882a593Smuzhiyun static void em28xx_init_buttons(struct em28xx *dev)
622*4882a593Smuzhiyun {
623*4882a593Smuzhiyun u8 i = 0, j = 0;
624*4882a593Smuzhiyun bool addr_new = false;
625*4882a593Smuzhiyun
626*4882a593Smuzhiyun dev->button_polling_interval = EM28XX_BUTTONS_DEBOUNCED_QUERY_INTERVAL;
627*4882a593Smuzhiyun while (dev->board.buttons[i].role >= 0 &&
628*4882a593Smuzhiyun dev->board.buttons[i].role < EM28XX_NUM_BUTTON_ROLES) {
629*4882a593Smuzhiyun const struct em28xx_button *button = &dev->board.buttons[i];
630*4882a593Smuzhiyun
631*4882a593Smuzhiyun /* Check if polling address is already on the list */
632*4882a593Smuzhiyun addr_new = true;
633*4882a593Smuzhiyun for (j = 0; j < dev->num_button_polling_addresses; j++) {
634*4882a593Smuzhiyun if (button->reg_r == dev->button_polling_addresses[j]) {
635*4882a593Smuzhiyun addr_new = false;
636*4882a593Smuzhiyun break;
637*4882a593Smuzhiyun }
638*4882a593Smuzhiyun }
639*4882a593Smuzhiyun /* Check if max. number of polling addresses is exceeded */
640*4882a593Smuzhiyun if (addr_new && dev->num_button_polling_addresses
641*4882a593Smuzhiyun >= EM28XX_NUM_BUTTON_ADDRESSES_MAX) {
642*4882a593Smuzhiyun WARN_ONCE(1, "BUG: maximum number of button polling addresses exceeded.");
643*4882a593Smuzhiyun goto next_button;
644*4882a593Smuzhiyun }
645*4882a593Smuzhiyun /* Button role specific checks and actions */
646*4882a593Smuzhiyun if (button->role == EM28XX_BUTTON_SNAPSHOT) {
647*4882a593Smuzhiyun /* Register input device */
648*4882a593Smuzhiyun if (em28xx_register_snapshot_button(dev) < 0)
649*4882a593Smuzhiyun goto next_button;
650*4882a593Smuzhiyun } else if (button->role == EM28XX_BUTTON_ILLUMINATION) {
651*4882a593Smuzhiyun /* Check sanity */
652*4882a593Smuzhiyun if (!em28xx_find_led(dev, EM28XX_LED_ILLUMINATION)) {
653*4882a593Smuzhiyun dev_err(&dev->intf->dev,
654*4882a593Smuzhiyun "BUG: illumination button defined, but no illumination LED.\n");
655*4882a593Smuzhiyun goto next_button;
656*4882a593Smuzhiyun }
657*4882a593Smuzhiyun }
658*4882a593Smuzhiyun /* Add read address to list of polling addresses */
659*4882a593Smuzhiyun if (addr_new) {
660*4882a593Smuzhiyun unsigned int index = dev->num_button_polling_addresses;
661*4882a593Smuzhiyun
662*4882a593Smuzhiyun dev->button_polling_addresses[index] = button->reg_r;
663*4882a593Smuzhiyun dev->num_button_polling_addresses++;
664*4882a593Smuzhiyun }
665*4882a593Smuzhiyun /* Reduce polling interval if necessary */
666*4882a593Smuzhiyun if (!button->reg_clearing)
667*4882a593Smuzhiyun dev->button_polling_interval =
668*4882a593Smuzhiyun EM28XX_BUTTONS_VOLATILE_QUERY_INTERVAL;
669*4882a593Smuzhiyun next_button:
670*4882a593Smuzhiyun /* Next button */
671*4882a593Smuzhiyun i++;
672*4882a593Smuzhiyun }
673*4882a593Smuzhiyun
674*4882a593Smuzhiyun /* Start polling */
675*4882a593Smuzhiyun if (dev->num_button_polling_addresses) {
676*4882a593Smuzhiyun memset(dev->button_polling_last_values, 0,
677*4882a593Smuzhiyun EM28XX_NUM_BUTTON_ADDRESSES_MAX);
678*4882a593Smuzhiyun schedule_delayed_work(&dev->buttons_query_work,
679*4882a593Smuzhiyun msecs_to_jiffies(dev->button_polling_interval));
680*4882a593Smuzhiyun }
681*4882a593Smuzhiyun }
682*4882a593Smuzhiyun
em28xx_shutdown_buttons(struct em28xx * dev)683*4882a593Smuzhiyun static void em28xx_shutdown_buttons(struct em28xx *dev)
684*4882a593Smuzhiyun {
685*4882a593Smuzhiyun /* Cancel polling */
686*4882a593Smuzhiyun cancel_delayed_work_sync(&dev->buttons_query_work);
687*4882a593Smuzhiyun /* Clear polling addresses list */
688*4882a593Smuzhiyun dev->num_button_polling_addresses = 0;
689*4882a593Smuzhiyun /* Deregister input devices */
690*4882a593Smuzhiyun if (dev->sbutton_input_dev) {
691*4882a593Smuzhiyun dev_info(&dev->intf->dev, "Deregistering snapshot button\n");
692*4882a593Smuzhiyun input_unregister_device(dev->sbutton_input_dev);
693*4882a593Smuzhiyun dev->sbutton_input_dev = NULL;
694*4882a593Smuzhiyun }
695*4882a593Smuzhiyun }
696*4882a593Smuzhiyun
em28xx_ir_init(struct em28xx * dev)697*4882a593Smuzhiyun static int em28xx_ir_init(struct em28xx *dev)
698*4882a593Smuzhiyun {
699*4882a593Smuzhiyun struct usb_device *udev = interface_to_usbdev(dev->intf);
700*4882a593Smuzhiyun struct em28xx_IR *ir;
701*4882a593Smuzhiyun struct rc_dev *rc;
702*4882a593Smuzhiyun int err = -ENOMEM;
703*4882a593Smuzhiyun u64 rc_proto;
704*4882a593Smuzhiyun u16 i2c_rc_dev_addr = 0;
705*4882a593Smuzhiyun
706*4882a593Smuzhiyun if (dev->is_audio_only) {
707*4882a593Smuzhiyun /* Shouldn't initialize IR for this interface */
708*4882a593Smuzhiyun return 0;
709*4882a593Smuzhiyun }
710*4882a593Smuzhiyun
711*4882a593Smuzhiyun kref_get(&dev->ref);
712*4882a593Smuzhiyun INIT_DELAYED_WORK(&dev->buttons_query_work, em28xx_query_buttons);
713*4882a593Smuzhiyun
714*4882a593Smuzhiyun if (dev->board.buttons)
715*4882a593Smuzhiyun em28xx_init_buttons(dev);
716*4882a593Smuzhiyun
717*4882a593Smuzhiyun if (dev->board.has_ir_i2c) {
718*4882a593Smuzhiyun i2c_rc_dev_addr = em28xx_probe_i2c_ir(dev);
719*4882a593Smuzhiyun if (!i2c_rc_dev_addr) {
720*4882a593Smuzhiyun dev->board.has_ir_i2c = 0;
721*4882a593Smuzhiyun dev_warn(&dev->intf->dev,
722*4882a593Smuzhiyun "No i2c IR remote control device found.\n");
723*4882a593Smuzhiyun err = -ENODEV;
724*4882a593Smuzhiyun goto ref_put;
725*4882a593Smuzhiyun }
726*4882a593Smuzhiyun }
727*4882a593Smuzhiyun
728*4882a593Smuzhiyun if (!dev->board.ir_codes && !dev->board.has_ir_i2c) {
729*4882a593Smuzhiyun /* No remote control support */
730*4882a593Smuzhiyun dev_warn(&dev->intf->dev,
731*4882a593Smuzhiyun "Remote control support is not available for this card.\n");
732*4882a593Smuzhiyun return 0;
733*4882a593Smuzhiyun }
734*4882a593Smuzhiyun
735*4882a593Smuzhiyun dev_info(&dev->intf->dev, "Registering input extension\n");
736*4882a593Smuzhiyun
737*4882a593Smuzhiyun ir = kzalloc(sizeof(*ir), GFP_KERNEL);
738*4882a593Smuzhiyun if (!ir)
739*4882a593Smuzhiyun goto ref_put;
740*4882a593Smuzhiyun rc = rc_allocate_device(RC_DRIVER_SCANCODE);
741*4882a593Smuzhiyun if (!rc)
742*4882a593Smuzhiyun goto error;
743*4882a593Smuzhiyun
744*4882a593Smuzhiyun /* record handles to ourself */
745*4882a593Smuzhiyun ir->dev = dev;
746*4882a593Smuzhiyun dev->ir = ir;
747*4882a593Smuzhiyun ir->rc = rc;
748*4882a593Smuzhiyun
749*4882a593Smuzhiyun rc->priv = ir;
750*4882a593Smuzhiyun rc->open = em28xx_ir_start;
751*4882a593Smuzhiyun rc->close = em28xx_ir_stop;
752*4882a593Smuzhiyun
753*4882a593Smuzhiyun if (dev->board.has_ir_i2c) { /* external i2c device */
754*4882a593Smuzhiyun switch (dev->model) {
755*4882a593Smuzhiyun case EM2800_BOARD_TERRATEC_CINERGY_200:
756*4882a593Smuzhiyun case EM2820_BOARD_TERRATEC_CINERGY_250:
757*4882a593Smuzhiyun rc->map_name = RC_MAP_EM_TERRATEC;
758*4882a593Smuzhiyun ir->get_key_i2c = em28xx_get_key_terratec;
759*4882a593Smuzhiyun break;
760*4882a593Smuzhiyun case EM2820_BOARD_PINNACLE_USB_2:
761*4882a593Smuzhiyun rc->map_name = RC_MAP_PINNACLE_GREY;
762*4882a593Smuzhiyun ir->get_key_i2c = em28xx_get_key_pinnacle_usb_grey;
763*4882a593Smuzhiyun break;
764*4882a593Smuzhiyun case EM2820_BOARD_HAUPPAUGE_WINTV_USB_2:
765*4882a593Smuzhiyun rc->map_name = RC_MAP_HAUPPAUGE;
766*4882a593Smuzhiyun ir->get_key_i2c = em28xx_get_key_em_haup;
767*4882a593Smuzhiyun rc->allowed_protocols = RC_PROTO_BIT_RC5;
768*4882a593Smuzhiyun break;
769*4882a593Smuzhiyun case EM2820_BOARD_LEADTEK_WINFAST_USBII_DELUXE:
770*4882a593Smuzhiyun rc->map_name = RC_MAP_WINFAST_USBII_DELUXE;
771*4882a593Smuzhiyun ir->get_key_i2c = em28xx_get_key_winfast_usbii_deluxe;
772*4882a593Smuzhiyun break;
773*4882a593Smuzhiyun default:
774*4882a593Smuzhiyun err = -ENODEV;
775*4882a593Smuzhiyun goto error;
776*4882a593Smuzhiyun }
777*4882a593Smuzhiyun
778*4882a593Smuzhiyun ir->i2c_client = kzalloc(sizeof(*ir->i2c_client), GFP_KERNEL);
779*4882a593Smuzhiyun if (!ir->i2c_client)
780*4882a593Smuzhiyun goto error;
781*4882a593Smuzhiyun ir->i2c_client->adapter = &ir->dev->i2c_adap[dev->def_i2c_bus];
782*4882a593Smuzhiyun ir->i2c_client->addr = i2c_rc_dev_addr;
783*4882a593Smuzhiyun ir->i2c_client->flags = 0;
784*4882a593Smuzhiyun /* NOTE: all other fields of i2c_client are unused */
785*4882a593Smuzhiyun } else { /* internal device */
786*4882a593Smuzhiyun switch (dev->chip_id) {
787*4882a593Smuzhiyun case CHIP_ID_EM2860:
788*4882a593Smuzhiyun case CHIP_ID_EM2883:
789*4882a593Smuzhiyun rc->allowed_protocols = RC_PROTO_BIT_RC5 |
790*4882a593Smuzhiyun RC_PROTO_BIT_NEC;
791*4882a593Smuzhiyun ir->get_key = default_polling_getkey;
792*4882a593Smuzhiyun break;
793*4882a593Smuzhiyun case CHIP_ID_EM2884:
794*4882a593Smuzhiyun case CHIP_ID_EM2874:
795*4882a593Smuzhiyun case CHIP_ID_EM28174:
796*4882a593Smuzhiyun case CHIP_ID_EM28178:
797*4882a593Smuzhiyun ir->get_key = em2874_polling_getkey;
798*4882a593Smuzhiyun rc->allowed_protocols = RC_PROTO_BIT_RC5 |
799*4882a593Smuzhiyun RC_PROTO_BIT_NEC | RC_PROTO_BIT_NECX |
800*4882a593Smuzhiyun RC_PROTO_BIT_NEC32 | RC_PROTO_BIT_RC6_0;
801*4882a593Smuzhiyun break;
802*4882a593Smuzhiyun default:
803*4882a593Smuzhiyun err = -ENODEV;
804*4882a593Smuzhiyun goto error;
805*4882a593Smuzhiyun }
806*4882a593Smuzhiyun
807*4882a593Smuzhiyun rc->change_protocol = em28xx_ir_change_protocol;
808*4882a593Smuzhiyun rc->map_name = dev->board.ir_codes;
809*4882a593Smuzhiyun
810*4882a593Smuzhiyun /* By default, keep protocol field untouched */
811*4882a593Smuzhiyun rc_proto = RC_PROTO_BIT_UNKNOWN;
812*4882a593Smuzhiyun err = em28xx_ir_change_protocol(rc, &rc_proto);
813*4882a593Smuzhiyun if (err)
814*4882a593Smuzhiyun goto error;
815*4882a593Smuzhiyun }
816*4882a593Smuzhiyun
817*4882a593Smuzhiyun /* This is how often we ask the chip for IR information */
818*4882a593Smuzhiyun ir->polling = 100; /* ms */
819*4882a593Smuzhiyun
820*4882a593Smuzhiyun usb_make_path(udev, ir->phys, sizeof(ir->phys));
821*4882a593Smuzhiyun strlcat(ir->phys, "/input0", sizeof(ir->phys));
822*4882a593Smuzhiyun
823*4882a593Smuzhiyun rc->device_name = em28xx_boards[dev->model].name;
824*4882a593Smuzhiyun rc->input_phys = ir->phys;
825*4882a593Smuzhiyun usb_to_input_id(udev, &rc->input_id);
826*4882a593Smuzhiyun rc->dev.parent = &dev->intf->dev;
827*4882a593Smuzhiyun rc->driver_name = MODULE_NAME;
828*4882a593Smuzhiyun
829*4882a593Smuzhiyun /* all done */
830*4882a593Smuzhiyun err = rc_register_device(rc);
831*4882a593Smuzhiyun if (err)
832*4882a593Smuzhiyun goto error;
833*4882a593Smuzhiyun
834*4882a593Smuzhiyun dev_info(&dev->intf->dev, "Input extension successfully initialized\n");
835*4882a593Smuzhiyun
836*4882a593Smuzhiyun return 0;
837*4882a593Smuzhiyun
838*4882a593Smuzhiyun error:
839*4882a593Smuzhiyun kfree(ir->i2c_client);
840*4882a593Smuzhiyun dev->ir = NULL;
841*4882a593Smuzhiyun rc_free_device(rc);
842*4882a593Smuzhiyun kfree(ir);
843*4882a593Smuzhiyun ref_put:
844*4882a593Smuzhiyun em28xx_shutdown_buttons(dev);
845*4882a593Smuzhiyun return err;
846*4882a593Smuzhiyun }
847*4882a593Smuzhiyun
em28xx_ir_fini(struct em28xx * dev)848*4882a593Smuzhiyun static int em28xx_ir_fini(struct em28xx *dev)
849*4882a593Smuzhiyun {
850*4882a593Smuzhiyun struct em28xx_IR *ir = dev->ir;
851*4882a593Smuzhiyun
852*4882a593Smuzhiyun if (dev->is_audio_only) {
853*4882a593Smuzhiyun /* Shouldn't initialize IR for this interface */
854*4882a593Smuzhiyun return 0;
855*4882a593Smuzhiyun }
856*4882a593Smuzhiyun
857*4882a593Smuzhiyun dev_info(&dev->intf->dev, "Closing input extension\n");
858*4882a593Smuzhiyun
859*4882a593Smuzhiyun em28xx_shutdown_buttons(dev);
860*4882a593Smuzhiyun
861*4882a593Smuzhiyun /* skip detach on non attached boards */
862*4882a593Smuzhiyun if (!ir)
863*4882a593Smuzhiyun goto ref_put;
864*4882a593Smuzhiyun
865*4882a593Smuzhiyun rc_unregister_device(ir->rc);
866*4882a593Smuzhiyun
867*4882a593Smuzhiyun kfree(ir->i2c_client);
868*4882a593Smuzhiyun
869*4882a593Smuzhiyun /* done */
870*4882a593Smuzhiyun kfree(ir);
871*4882a593Smuzhiyun dev->ir = NULL;
872*4882a593Smuzhiyun
873*4882a593Smuzhiyun ref_put:
874*4882a593Smuzhiyun kref_put(&dev->ref, em28xx_free_device);
875*4882a593Smuzhiyun
876*4882a593Smuzhiyun return 0;
877*4882a593Smuzhiyun }
878*4882a593Smuzhiyun
em28xx_ir_suspend(struct em28xx * dev)879*4882a593Smuzhiyun static int em28xx_ir_suspend(struct em28xx *dev)
880*4882a593Smuzhiyun {
881*4882a593Smuzhiyun struct em28xx_IR *ir = dev->ir;
882*4882a593Smuzhiyun
883*4882a593Smuzhiyun if (dev->is_audio_only)
884*4882a593Smuzhiyun return 0;
885*4882a593Smuzhiyun
886*4882a593Smuzhiyun dev_info(&dev->intf->dev, "Suspending input extension\n");
887*4882a593Smuzhiyun if (ir)
888*4882a593Smuzhiyun cancel_delayed_work_sync(&ir->work);
889*4882a593Smuzhiyun cancel_delayed_work_sync(&dev->buttons_query_work);
890*4882a593Smuzhiyun /*
891*4882a593Smuzhiyun * is canceling delayed work sufficient or does the rc event
892*4882a593Smuzhiyun * kthread needs stopping? kthread is stopped in
893*4882a593Smuzhiyun * ir_raw_event_unregister()
894*4882a593Smuzhiyun */
895*4882a593Smuzhiyun return 0;
896*4882a593Smuzhiyun }
897*4882a593Smuzhiyun
em28xx_ir_resume(struct em28xx * dev)898*4882a593Smuzhiyun static int em28xx_ir_resume(struct em28xx *dev)
899*4882a593Smuzhiyun {
900*4882a593Smuzhiyun struct em28xx_IR *ir = dev->ir;
901*4882a593Smuzhiyun
902*4882a593Smuzhiyun if (dev->is_audio_only)
903*4882a593Smuzhiyun return 0;
904*4882a593Smuzhiyun
905*4882a593Smuzhiyun dev_info(&dev->intf->dev, "Resuming input extension\n");
906*4882a593Smuzhiyun /*
907*4882a593Smuzhiyun * if suspend calls ir_raw_event_unregister(), the should call
908*4882a593Smuzhiyun * ir_raw_event_register()
909*4882a593Smuzhiyun */
910*4882a593Smuzhiyun if (ir)
911*4882a593Smuzhiyun schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
912*4882a593Smuzhiyun if (dev->num_button_polling_addresses)
913*4882a593Smuzhiyun schedule_delayed_work(&dev->buttons_query_work,
914*4882a593Smuzhiyun msecs_to_jiffies(dev->button_polling_interval));
915*4882a593Smuzhiyun return 0;
916*4882a593Smuzhiyun }
917*4882a593Smuzhiyun
918*4882a593Smuzhiyun static struct em28xx_ops rc_ops = {
919*4882a593Smuzhiyun .id = EM28XX_RC,
920*4882a593Smuzhiyun .name = "Em28xx Input Extension",
921*4882a593Smuzhiyun .init = em28xx_ir_init,
922*4882a593Smuzhiyun .fini = em28xx_ir_fini,
923*4882a593Smuzhiyun .suspend = em28xx_ir_suspend,
924*4882a593Smuzhiyun .resume = em28xx_ir_resume,
925*4882a593Smuzhiyun };
926*4882a593Smuzhiyun
em28xx_rc_register(void)927*4882a593Smuzhiyun static int __init em28xx_rc_register(void)
928*4882a593Smuzhiyun {
929*4882a593Smuzhiyun return em28xx_register_extension(&rc_ops);
930*4882a593Smuzhiyun }
931*4882a593Smuzhiyun
em28xx_rc_unregister(void)932*4882a593Smuzhiyun static void __exit em28xx_rc_unregister(void)
933*4882a593Smuzhiyun {
934*4882a593Smuzhiyun em28xx_unregister_extension(&rc_ops);
935*4882a593Smuzhiyun }
936*4882a593Smuzhiyun
937*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
938*4882a593Smuzhiyun MODULE_AUTHOR("Mauro Carvalho Chehab");
939*4882a593Smuzhiyun MODULE_DESCRIPTION(DRIVER_DESC " - input interface");
940*4882a593Smuzhiyun MODULE_VERSION(EM28XX_VERSION);
941*4882a593Smuzhiyun
942*4882a593Smuzhiyun module_init(em28xx_rc_register);
943*4882a593Smuzhiyun module_exit(em28xx_rc_unregister);
944