Commit | Line | Data |
---|---|---|
d5e52653 | 1 | /* |
f7abcd38 MCC |
2 | 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> | |
2e7c6dc3 | 6 | Mauro Carvalho Chehab <mchehab@infradead.org> |
f7abcd38 MCC |
7 | 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 | |
d5e52653 MCC |
22 | */ |
23 | ||
24 | #include <linux/module.h> | |
d5e52653 MCC |
25 | #include <linux/init.h> |
26 | #include <linux/delay.h> | |
d5e52653 | 27 | #include <linux/interrupt.h> |
d5e52653 | 28 | #include <linux/usb.h> |
5a0e3ad6 | 29 | #include <linux/slab.h> |
d5e52653 | 30 | |
f7abcd38 | 31 | #include "em28xx.h" |
d5e52653 | 32 | |
0ff950a7 FS |
33 | #define EM28XX_SNAPSHOT_KEY KEY_CAMERA |
34 | #define EM28XX_BUTTONS_DEBOUNCED_QUERY_INTERVAL 500 /* [ms] */ | |
35 | #define EM28XX_BUTTONS_VOLATILE_QUERY_INTERVAL 100 /* [ms] */ | |
a9fc52bc | 36 | |
c8793b03 | 37 | static unsigned int ir_debug; |
d5e52653 | 38 | module_param(ir_debug, int, 0644); |
6ea54d93 | 39 | MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]"); |
d5e52653 | 40 | |
727e625c MCC |
41 | #define MODULE_NAME "em28xx" |
42 | ||
a924a499 MCC |
43 | #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 | ||
4b92253a DH |
52 | struct em28xx_ir_poll_result { |
53 | unsigned int toggle_bit:1; | |
54 | unsigned int read_count:7; | |
105e3687 MCC |
55 | |
56 | u32 scancode; | |
4b92253a DH |
57 | }; |
58 | ||
a924a499 MCC |
59 | struct em28xx_IR { |
60 | struct em28xx *dev; | |
d8b4b582 | 61 | struct rc_dev *rc; |
a924a499 MCC |
62 | char name[32]; |
63 | char phys[32]; | |
64 | ||
768da3db | 65 | /* poll decoder */ |
a924a499 | 66 | int polling; |
f263bac9 | 67 | struct delayed_work work; |
0278155c | 68 | unsigned int full_code:1; |
4b92253a | 69 | unsigned int last_readcount; |
105e3687 | 70 | u64 rc_type; |
a924a499 | 71 | |
1d968cda FS |
72 | /* i2c slave address of external device (if used) */ |
73 | u16 i2c_dev_addr; | |
768da3db | 74 | |
146b7ee6 | 75 | int (*get_key_i2c)(struct i2c_client *, u32 *); |
4b92253a | 76 | int (*get_key)(struct em28xx_IR *, struct em28xx_ir_poll_result *); |
a924a499 MCC |
77 | }; |
78 | ||
79 | /********************************************************** | |
80 | I2C IR based get keycodes - should be used with ir-kbd-i2c | |
81 | **********************************************************/ | |
d5e52653 | 82 | |
146b7ee6 | 83 | static int em28xx_get_key_terratec(struct i2c_client *i2c_dev, u32 *ir_key) |
e43f14af MR |
84 | { |
85 | unsigned char b; | |
86 | ||
87 | /* poll IR chip */ | |
768da3db | 88 | if (1 != i2c_master_recv(i2c_dev, &b, 1)) |
e43f14af | 89 | return -EIO; |
e43f14af MR |
90 | |
91 | /* it seems that 0xFE indicates that a button is still hold | |
450c7dd6 | 92 | down, while 0xff indicates that no button is hold down. */ |
e43f14af | 93 | |
4f9c05aa | 94 | if (b == 0xff) |
e43f14af MR |
95 | return 0; |
96 | ||
4f9c05aa | 97 | if (b == 0xfe) |
e43f14af MR |
98 | /* keep old data */ |
99 | return 1; | |
100 | ||
101 | *ir_key = b; | |
e43f14af MR |
102 | return 1; |
103 | } | |
104 | ||
146b7ee6 | 105 | static int em28xx_get_key_em_haup(struct i2c_client *i2c_dev, u32 *ir_key) |
d5e52653 MCC |
106 | { |
107 | unsigned char buf[2]; | |
b779974b MCC |
108 | u16 code; |
109 | int size; | |
d5e52653 MCC |
110 | |
111 | /* poll IR chip */ | |
768da3db | 112 | size = i2c_master_recv(i2c_dev, buf, sizeof(buf)); |
b779974b MCC |
113 | |
114 | if (size != 2) | |
d5e52653 MCC |
115 | return -EIO; |
116 | ||
117 | /* Does eliminate repeated parity code */ | |
6ea54d93 | 118 | if (buf[1] == 0xff) |
d5e52653 MCC |
119 | return 0; |
120 | ||
b779974b MCC |
121 | /* |
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 */ | |
142 | ||
d5e52653 MCC |
143 | /* return key */ |
144 | *ir_key = code; | |
d5e52653 MCC |
145 | return 1; |
146 | } | |
147 | ||
768da3db | 148 | static int em28xx_get_key_pinnacle_usb_grey(struct i2c_client *i2c_dev, |
146b7ee6 | 149 | u32 *ir_key) |
366cc64b MR |
150 | { |
151 | unsigned char buf[3]; | |
152 | ||
153 | /* poll IR chip */ | |
154 | ||
768da3db | 155 | if (3 != i2c_master_recv(i2c_dev, buf, 3)) |
366cc64b | 156 | return -EIO; |
366cc64b | 157 | |
6ea54d93 | 158 | if (buf[0] != 0x00) |
366cc64b | 159 | return 0; |
366cc64b MR |
160 | |
161 | *ir_key = buf[2]&0x3f; | |
366cc64b MR |
162 | |
163 | return 1; | |
164 | } | |
165 | ||
768da3db | 166 | static int em28xx_get_key_winfast_usbii_deluxe(struct i2c_client *i2c_dev, |
146b7ee6 | 167 | u32 *ir_key) |
ca39d84d MA |
168 | { |
169 | unsigned char subaddr, keydetect, key; | |
170 | ||
768da3db | 171 | struct i2c_msg msg[] = { { .addr = i2c_dev->addr, .flags = 0, .buf = &subaddr, .len = 1}, |
450c7dd6 | 172 | { .addr = i2c_dev->addr, .flags = I2C_M_RD, .buf = &keydetect, .len = 1} }; |
ca39d84d MA |
173 | |
174 | subaddr = 0x10; | |
768da3db | 175 | if (2 != i2c_transfer(i2c_dev->adapter, msg, 2)) |
ca39d84d | 176 | return -EIO; |
ca39d84d MA |
177 | if (keydetect == 0x00) |
178 | return 0; | |
179 | ||
180 | subaddr = 0x00; | |
181 | msg[1].buf = &key; | |
768da3db | 182 | if (2 != i2c_transfer(i2c_dev->adapter, msg, 2)) |
62ec3f86 | 183 | return -EIO; |
ca39d84d MA |
184 | if (key == 0x00) |
185 | return 0; | |
186 | ||
187 | *ir_key = key; | |
ca39d84d MA |
188 | return 1; |
189 | } | |
190 | ||
a924a499 MCC |
191 | /********************************************************** |
192 | Poll based get keycode functions | |
193 | **********************************************************/ | |
194 | ||
4b92253a DH |
195 | /* This is for the em2860/em2880 */ |
196 | static int default_polling_getkey(struct em28xx_IR *ir, | |
197 | struct em28xx_ir_poll_result *poll_result) | |
a924a499 MCC |
198 | { |
199 | struct em28xx *dev = ir->dev; | |
200 | int rc; | |
4b92253a | 201 | u8 msg[3] = { 0, 0, 0 }; |
a924a499 | 202 | |
0a6b8a85 MCC |
203 | /* Read key toggle, brand, and key code |
204 | on registers 0x45, 0x46 and 0x47 | |
205 | */ | |
a924a499 | 206 | rc = dev->em28xx_read_reg_req_len(dev, 0, EM28XX_R45_IR, |
0a6b8a85 | 207 | msg, sizeof(msg)); |
a924a499 MCC |
208 | if (rc < 0) |
209 | return rc; | |
210 | ||
4b92253a DH |
211 | /* 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 | ||
105e3687 MCC |
217 | /* Remote Control Address/Data (Regs 0x46/0x47) */ |
218 | poll_result->scancode = msg[1] << 8 | msg[2]; | |
4b92253a DH |
219 | |
220 | return 0; | |
221 | } | |
222 | ||
223 | static 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 | ||
105e3687 MCC |
244 | /* |
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; | |
0dae8839 MCC |
265 | case RC_BIT_RC6_0: |
266 | poll_result->scancode = msg[1] << 8 | msg[2]; | |
267 | break; | |
105e3687 MCC |
268 | default: |
269 | poll_result->scancode = (msg[1] << 24) | (msg[2] << 16) | | |
270 | (msg[3] << 8) | msg[4]; | |
271 | break; | |
272 | } | |
4b92253a DH |
273 | |
274 | return 0; | |
a924a499 MCC |
275 | } |
276 | ||
277 | /********************************************************** | |
278 | Polling code for em28xx | |
279 | **********************************************************/ | |
280 | ||
768da3db FS |
281 | static int em28xx_i2c_ir_handle_key(struct em28xx_IR *ir) |
282 | { | |
c7a45e5b | 283 | struct em28xx *dev = ir->dev; |
146b7ee6 | 284 | static u32 ir_key; |
768da3db | 285 | int rc; |
1d968cda | 286 | struct i2c_client client; |
768da3db | 287 | |
c7a45e5b | 288 | client.adapter = &ir->dev->i2c_adap[dev->def_i2c_bus]; |
1d968cda FS |
289 | client.addr = ir->i2c_dev_addr; |
290 | ||
291 | rc = ir->get_key_i2c(&client, &ir_key); | |
768da3db FS |
292 | if (rc < 0) { |
293 | dprintk("ir->get_key_i2c() failed: %d\n", rc); | |
294 | return rc; | |
295 | } | |
296 | ||
297 | if (rc) { | |
298 | dprintk("%s: keycode = 0x%04x\n", __func__, ir_key); | |
299 | rc_keydown(ir->rc, ir_key, 0); | |
300 | } | |
301 | return 0; | |
302 | } | |
303 | ||
a924a499 MCC |
304 | static void em28xx_ir_handle_key(struct em28xx_IR *ir) |
305 | { | |
4b92253a | 306 | int result; |
4b92253a DH |
307 | struct em28xx_ir_poll_result poll_result; |
308 | ||
309 | /* read the registers containing the IR status */ | |
310 | result = ir->get_key(ir, &poll_result); | |
603044d8 | 311 | if (unlikely(result < 0)) { |
768da3db | 312 | dprintk("ir->get_key() failed: %d\n", result); |
a924a499 | 313 | return; |
4b92253a | 314 | } |
a924a499 | 315 | |
603044d8 | 316 | if (unlikely(poll_result.read_count != ir->last_readcount)) { |
105e3687 | 317 | dprintk("%s: toggle: %d, count: %d, key 0x%04x\n", __func__, |
603044d8 | 318 | poll_result.toggle_bit, poll_result.read_count, |
105e3687 | 319 | poll_result.scancode); |
a469585b | 320 | if (ir->full_code) |
ca86674b | 321 | rc_keydown(ir->rc, |
105e3687 | 322 | poll_result.scancode, |
a469585b DH |
323 | poll_result.toggle_bit); |
324 | else | |
ca86674b | 325 | rc_keydown(ir->rc, |
105e3687 | 326 | poll_result.scancode & 0xff, |
a469585b | 327 | poll_result.toggle_bit); |
a469585b | 328 | |
20ae9742 MCC |
329 | if (ir->dev->chip_id == CHIP_ID_EM2874 || |
330 | ir->dev->chip_id == CHIP_ID_EM2884) | |
603044d8 MCC |
331 | /* The em2874 clears the readcount field every time the |
332 | register is read. The em2860/2880 datasheet says that it | |
333 | is supposed to clear the readcount, but it doesn't. So with | |
334 | the em2874, we are looking for a non-zero read count as | |
335 | opposed to a readcount that is incrementing */ | |
336 | ir->last_readcount = 0; | |
337 | else | |
338 | ir->last_readcount = poll_result.read_count; | |
339 | } | |
a924a499 MCC |
340 | } |
341 | ||
a924a499 MCC |
342 | static void em28xx_ir_work(struct work_struct *work) |
343 | { | |
f263bac9 | 344 | struct em28xx_IR *ir = container_of(work, struct em28xx_IR, work.work); |
a924a499 | 345 | |
9b4539be FS |
346 | if (ir->i2c_dev_addr) /* external i2c device */ |
347 | em28xx_i2c_ir_handle_key(ir); | |
348 | else /* internal device */ | |
349 | em28xx_ir_handle_key(ir); | |
f263bac9 | 350 | schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling)); |
a924a499 MCC |
351 | } |
352 | ||
d8b4b582 | 353 | static int em28xx_ir_start(struct rc_dev *rc) |
a924a499 | 354 | { |
d8b4b582 | 355 | struct em28xx_IR *ir = rc->priv; |
c373cabf | 356 | |
9b4539be | 357 | INIT_DELAYED_WORK(&ir->work, em28xx_ir_work); |
f263bac9 | 358 | schedule_delayed_work(&ir->work, 0); |
c373cabf MCC |
359 | |
360 | return 0; | |
a924a499 MCC |
361 | } |
362 | ||
d8b4b582 | 363 | static void em28xx_ir_stop(struct rc_dev *rc) |
a924a499 | 364 | { |
d8b4b582 | 365 | struct em28xx_IR *ir = rc->priv; |
c373cabf | 366 | |
f263bac9 | 367 | cancel_delayed_work_sync(&ir->work); |
a924a499 MCC |
368 | } |
369 | ||
0dae8839 | 370 | static int em2860_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_type) |
a924a499 | 371 | { |
d8b4b582 | 372 | struct em28xx_IR *ir = rc_dev->priv; |
950b0f5a | 373 | struct em28xx *dev = ir->dev; |
1bad429e | 374 | |
0dae8839 MCC |
375 | /* Adjust xclk based on IR table for RC5/NEC tables */ |
376 | if (*rc_type & RC_BIT_RC5) { | |
377 | dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE; | |
378 | ir->full_code = 1; | |
379 | *rc_type = RC_BIT_RC5; | |
380 | } else if (*rc_type & RC_BIT_NEC) { | |
381 | dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE; | |
382 | ir->full_code = 1; | |
383 | *rc_type = RC_BIT_NEC; | |
384 | } else if (*rc_type & RC_BIT_UNKNOWN) { | |
385 | *rc_type = RC_BIT_UNKNOWN; | |
386 | } else { | |
387 | *rc_type = ir->rc_type; | |
388 | return -EINVAL; | |
389 | } | |
0dae8839 MCC |
390 | em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk, |
391 | EM28XX_XCLK_IR_RC5_MODE); | |
392 | ||
393 | ir->rc_type = *rc_type; | |
950b0f5a | 394 | |
0dae8839 MCC |
395 | return 0; |
396 | } | |
397 | ||
398 | static int em2874_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_type) | |
399 | { | |
400 | struct em28xx_IR *ir = rc_dev->priv; | |
401 | struct em28xx *dev = ir->dev; | |
402 | u8 ir_config = EM2874_IR_RC5; | |
403 | ||
404 | /* Adjust xclk and set type based on IR table for RC5/NEC/RC6 tables */ | |
c003ab1b | 405 | if (*rc_type & RC_BIT_RC5) { |
1bad429e MCC |
406 | dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE; |
407 | ir->full_code = 1; | |
c003ab1b DH |
408 | *rc_type = RC_BIT_RC5; |
409 | } else if (*rc_type & RC_BIT_NEC) { | |
1bad429e | 410 | dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE; |
105e3687 | 411 | ir_config = EM2874_IR_NEC | EM2874_IR_NEC_NO_PARITY; |
1bad429e | 412 | ir->full_code = 1; |
c003ab1b | 413 | *rc_type = RC_BIT_NEC; |
0dae8839 MCC |
414 | } else if (*rc_type & RC_BIT_RC6_0) { |
415 | dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE; | |
416 | ir_config = EM2874_IR_RC6_MODE_0; | |
417 | ir->full_code = 1; | |
418 | *rc_type = RC_BIT_RC6_0; | |
419 | } else if (*rc_type & RC_BIT_UNKNOWN) { | |
420 | *rc_type = RC_BIT_UNKNOWN; | |
421 | } else { | |
422 | *rc_type = ir->rc_type; | |
423 | return -EINVAL; | |
424 | } | |
0dae8839 | 425 | em28xx_write_regs(dev, EM2874_R50_IR_CONFIG, &ir_config, 1); |
1bad429e MCC |
426 | em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk, |
427 | EM28XX_XCLK_IR_RC5_MODE); | |
a924a499 | 428 | |
0dae8839 MCC |
429 | ir->rc_type = *rc_type; |
430 | ||
431 | return 0; | |
432 | } | |
433 | static int em28xx_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_type) | |
434 | { | |
435 | struct em28xx_IR *ir = rc_dev->priv; | |
436 | struct em28xx *dev = ir->dev; | |
437 | ||
4b92253a DH |
438 | /* Setup the proper handler based on the chip */ |
439 | switch (dev->chip_id) { | |
440 | case CHIP_ID_EM2860: | |
441 | case CHIP_ID_EM2883: | |
0dae8839 | 442 | return em2860_ir_change_protocol(rc_dev, rc_type); |
20ae9742 | 443 | case CHIP_ID_EM2884: |
4b92253a | 444 | case CHIP_ID_EM2874: |
a24ec448 | 445 | case CHIP_ID_EM28174: |
9f1d0bda | 446 | case CHIP_ID_EM28178: |
0dae8839 | 447 | return em2874_ir_change_protocol(rc_dev, rc_type); |
4b92253a | 448 | default: |
20ae9742 MCC |
449 | printk("Unrecognized em28xx chip id 0x%02x: IR not supported\n", |
450 | dev->chip_id); | |
0dae8839 | 451 | return -EINVAL; |
950b0f5a | 452 | } |
950b0f5a MCC |
453 | } |
454 | ||
1d968cda | 455 | static int em28xx_probe_i2c_ir(struct em28xx *dev) |
9d9f479b | 456 | { |
768da3db | 457 | int i = 0; |
9d9f479b EG |
458 | /* Leadtek winfast tv USBII deluxe can find a non working IR-device */ |
459 | /* at address 0x18, so if that address is needed for another board in */ | |
460 | /* the future, please put it after 0x1f. */ | |
9d9f479b EG |
461 | const unsigned short addr_list[] = { |
462 | 0x1f, 0x30, 0x47, I2C_CLIENT_END | |
463 | }; | |
464 | ||
768da3db | 465 | while (addr_list[i] != I2C_CLIENT_END) { |
c7a45e5b | 466 | if (i2c_probe_func_quick_read(&dev->i2c_adap[dev->def_i2c_bus], addr_list[i]) == 1) |
1d968cda | 467 | return addr_list[i]; |
768da3db | 468 | i++; |
9d9f479b EG |
469 | } |
470 | ||
1d968cda | 471 | return -ENODEV; |
9d9f479b EG |
472 | } |
473 | ||
769af214 | 474 | /********************************************************** |
f5222609 | 475 | Handle buttons |
769af214 EG |
476 | **********************************************************/ |
477 | ||
f5222609 | 478 | static void em28xx_query_buttons(struct work_struct *work) |
769af214 | 479 | { |
769af214 | 480 | struct em28xx *dev = |
f5222609 FS |
481 | container_of(work, struct em28xx, buttons_query_work.work); |
482 | u8 i, j; | |
483 | int regval; | |
7763481a | 484 | bool is_pressed, was_pressed; |
6063d077 | 485 | const struct em28xx_led *led; |
f5222609 FS |
486 | |
487 | /* Poll and evaluate all addresses */ | |
488 | for (i = 0; i < dev->num_button_polling_addresses; i++) { | |
489 | /* Read value from register */ | |
490 | regval = em28xx_read_reg(dev, dev->button_polling_addresses[i]); | |
491 | if (regval < 0) | |
492 | continue; | |
493 | /* Check states of the buttons and act */ | |
494 | j = 0; | |
495 | while (dev->board.buttons[j].role >= 0 && | |
496 | dev->board.buttons[j].role < EM28XX_NUM_BUTTON_ROLES) { | |
497 | struct em28xx_button *button = &dev->board.buttons[j]; | |
498 | /* Check if button uses the current address */ | |
499 | if (button->reg_r != dev->button_polling_addresses[i]) { | |
500 | j++; | |
501 | continue; | |
502 | } | |
7763481a FS |
503 | /* Determine if button is and was pressed last time */ |
504 | is_pressed = regval & button->mask; | |
505 | was_pressed = dev->button_polling_last_values[i] | |
506 | & button->mask; | |
507 | if (button->inverted) { | |
508 | is_pressed = !is_pressed; | |
509 | was_pressed = !was_pressed; | |
510 | } | |
511 | /* Clear button state (if needed) */ | |
512 | if (is_pressed && button->reg_clearing) | |
513 | em28xx_write_reg(dev, button->reg_clearing, | |
514 | (~regval & button->mask) | |
515 | | (regval & ~button->mask)); | |
f5222609 | 516 | /* Handle button state */ |
7763481a | 517 | if (!is_pressed || was_pressed) { |
f5222609 FS |
518 | j++; |
519 | continue; | |
520 | } | |
521 | switch (button->role) { | |
522 | case EM28XX_BUTTON_SNAPSHOT: | |
523 | /* Emulate the keypress */ | |
524 | input_report_key(dev->sbutton_input_dev, | |
525 | EM28XX_SNAPSHOT_KEY, 1); | |
526 | /* Unpress the key */ | |
527 | input_report_key(dev->sbutton_input_dev, | |
528 | EM28XX_SNAPSHOT_KEY, 0); | |
529 | break; | |
6063d077 FS |
530 | case EM28XX_BUTTON_ILLUMINATION: |
531 | led = em28xx_find_led(dev, | |
532 | EM28XX_LED_ILLUMINATION); | |
533 | /* Switch illumination LED on/off */ | |
534 | if (led) | |
535 | em28xx_toggle_reg_bits(dev, | |
536 | led->gpio_reg, | |
537 | led->gpio_mask); | |
538 | break; | |
f5222609 FS |
539 | default: |
540 | WARN_ONCE(1, "BUG: unhandled button role."); | |
541 | } | |
f5222609 FS |
542 | /* Next button */ |
543 | j++; | |
544 | } | |
7763481a FS |
545 | /* Save current value for comparison during the next polling */ |
546 | dev->button_polling_last_values[i] = regval; | |
769af214 | 547 | } |
769af214 | 548 | /* Schedule next poll */ |
f5222609 | 549 | schedule_delayed_work(&dev->buttons_query_work, |
0ff950a7 | 550 | msecs_to_jiffies(dev->button_polling_interval)); |
769af214 EG |
551 | } |
552 | ||
f5222609 | 553 | static int em28xx_register_snapshot_button(struct em28xx *dev) |
769af214 EG |
554 | { |
555 | struct input_dev *input_dev; | |
556 | int err; | |
557 | ||
558 | em28xx_info("Registering snapshot button...\n"); | |
559 | input_dev = input_allocate_device(); | |
560 | if (!input_dev) { | |
561 | em28xx_errdev("input_allocate_device failed\n"); | |
f5222609 | 562 | return -ENOMEM; |
769af214 EG |
563 | } |
564 | ||
565 | usb_make_path(dev->udev, dev->snapshot_button_path, | |
566 | sizeof(dev->snapshot_button_path)); | |
567 | strlcat(dev->snapshot_button_path, "/sbutton", | |
568 | sizeof(dev->snapshot_button_path)); | |
769af214 EG |
569 | |
570 | input_dev->name = "em28xx snapshot button"; | |
571 | input_dev->phys = dev->snapshot_button_path; | |
572 | input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP); | |
573 | set_bit(EM28XX_SNAPSHOT_KEY, input_dev->keybit); | |
574 | input_dev->keycodesize = 0; | |
575 | input_dev->keycodemax = 0; | |
576 | input_dev->id.bustype = BUS_USB; | |
577 | input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor); | |
578 | input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct); | |
579 | input_dev->id.version = 1; | |
580 | input_dev->dev.parent = &dev->udev->dev; | |
581 | ||
582 | err = input_register_device(input_dev); | |
583 | if (err) { | |
584 | em28xx_errdev("input_register_device failed\n"); | |
585 | input_free_device(input_dev); | |
f5222609 | 586 | return err; |
769af214 EG |
587 | } |
588 | ||
589 | dev->sbutton_input_dev = input_dev; | |
f5222609 FS |
590 | return 0; |
591 | } | |
769af214 | 592 | |
f5222609 FS |
593 | static void em28xx_init_buttons(struct em28xx *dev) |
594 | { | |
595 | u8 i = 0, j = 0; | |
596 | bool addr_new = 0; | |
597 | ||
0ff950a7 | 598 | dev->button_polling_interval = EM28XX_BUTTONS_DEBOUNCED_QUERY_INTERVAL; |
f5222609 FS |
599 | while (dev->board.buttons[i].role >= 0 && |
600 | dev->board.buttons[i].role < EM28XX_NUM_BUTTON_ROLES) { | |
601 | struct em28xx_button *button = &dev->board.buttons[i]; | |
602 | /* Check if polling address is already on the list */ | |
603 | addr_new = 1; | |
604 | for (j = 0; j < dev->num_button_polling_addresses; j++) { | |
605 | if (button->reg_r == dev->button_polling_addresses[j]) { | |
606 | addr_new = 0; | |
607 | break; | |
608 | } | |
609 | } | |
610 | /* Check if max. number of polling addresses is exceeded */ | |
611 | if (addr_new && dev->num_button_polling_addresses | |
612 | >= EM28XX_NUM_BUTTON_ADDRESSES_MAX) { | |
613 | WARN_ONCE(1, "BUG: maximum number of button polling addresses exceeded."); | |
0ff950a7 | 614 | goto next_button; |
f5222609 | 615 | } |
6063d077 | 616 | /* Button role specific checks and actions */ |
f5222609 | 617 | if (button->role == EM28XX_BUTTON_SNAPSHOT) { |
6063d077 | 618 | /* Register input device */ |
f5222609 | 619 | if (em28xx_register_snapshot_button(dev) < 0) |
0ff950a7 | 620 | goto next_button; |
6063d077 FS |
621 | } else if (button->role == EM28XX_BUTTON_ILLUMINATION) { |
622 | /* Check sanity */ | |
623 | if (!em28xx_find_led(dev, EM28XX_LED_ILLUMINATION)) { | |
624 | em28xx_errdev("BUG: illumination button defined, but no illumination LED.\n"); | |
0ff950a7 | 625 | goto next_button; |
6063d077 | 626 | } |
f5222609 FS |
627 | } |
628 | /* Add read address to list of polling addresses */ | |
629 | if (addr_new) { | |
630 | unsigned int index = dev->num_button_polling_addresses; | |
631 | dev->button_polling_addresses[index] = button->reg_r; | |
632 | dev->num_button_polling_addresses++; | |
633 | } | |
0ff950a7 FS |
634 | /* Reduce polling interval if necessary */ |
635 | if (!button->reg_clearing) | |
636 | dev->button_polling_interval = | |
637 | EM28XX_BUTTONS_VOLATILE_QUERY_INTERVAL; | |
638 | next_button: | |
f5222609 FS |
639 | /* Next button */ |
640 | i++; | |
641 | } | |
642 | ||
643 | /* Start polling */ | |
644 | if (dev->num_button_polling_addresses) { | |
7763481a FS |
645 | memset(dev->button_polling_last_values, 0, |
646 | EM28XX_NUM_BUTTON_ADDRESSES_MAX); | |
f5222609 FS |
647 | INIT_DELAYED_WORK(&dev->buttons_query_work, |
648 | em28xx_query_buttons); | |
649 | schedule_delayed_work(&dev->buttons_query_work, | |
0ff950a7 | 650 | msecs_to_jiffies(dev->button_polling_interval)); |
f5222609 | 651 | } |
769af214 EG |
652 | } |
653 | ||
f5222609 | 654 | static void em28xx_shutdown_buttons(struct em28xx *dev) |
769af214 | 655 | { |
f5222609 FS |
656 | /* Cancel polling */ |
657 | cancel_delayed_work_sync(&dev->buttons_query_work); | |
658 | /* Clear polling addresses list */ | |
659 | dev->num_button_polling_addresses = 0; | |
660 | /* Deregister input devices */ | |
769af214 EG |
661 | if (dev->sbutton_input_dev != NULL) { |
662 | em28xx_info("Deregistering snapshot button\n"); | |
769af214 EG |
663 | input_unregister_device(dev->sbutton_input_dev); |
664 | dev->sbutton_input_dev = NULL; | |
665 | } | |
769af214 EG |
666 | } |
667 | ||
f4d4e765 | 668 | static int em28xx_ir_init(struct em28xx *dev) |
950b0f5a MCC |
669 | { |
670 | struct em28xx_IR *ir; | |
d8b4b582 | 671 | struct rc_dev *rc; |
950b0f5a | 672 | int err = -ENOMEM; |
c003ab1b | 673 | u64 rc_type; |
1d968cda | 674 | u16 i2c_rc_dev_addr = 0; |
950b0f5a | 675 | |
f5222609 FS |
676 | if (dev->board.buttons) |
677 | em28xx_init_buttons(dev); | |
8303dc99 MCC |
678 | |
679 | if (dev->board.has_ir_i2c) { | |
1d968cda FS |
680 | i2c_rc_dev_addr = em28xx_probe_i2c_ir(dev); |
681 | if (!i2c_rc_dev_addr) { | |
768da3db FS |
682 | dev->board.has_ir_i2c = 0; |
683 | em28xx_warn("No i2c IR remote control device found.\n"); | |
684 | return -ENODEV; | |
685 | } | |
8303dc99 MCC |
686 | } |
687 | ||
768da3db | 688 | if (dev->board.ir_codes == NULL && !dev->board.has_ir_i2c) { |
950b0f5a | 689 | /* No remote control support */ |
b83f6715 MB |
690 | em28xx_warn("Remote control support is not available for " |
691 | "this card.\n"); | |
950b0f5a | 692 | return 0; |
a924a499 MCC |
693 | } |
694 | ||
950b0f5a | 695 | ir = kzalloc(sizeof(*ir), GFP_KERNEL); |
d8b4b582 DH |
696 | rc = rc_allocate_device(); |
697 | if (!ir || !rc) | |
2f5741aa | 698 | goto error; |
950b0f5a MCC |
699 | |
700 | /* record handles to ourself */ | |
701 | ir->dev = dev; | |
702 | dev->ir = ir; | |
d8b4b582 | 703 | ir->rc = rc; |
950b0f5a | 704 | |
d8b4b582 | 705 | rc->priv = ir; |
d8b4b582 DH |
706 | rc->open = em28xx_ir_start; |
707 | rc->close = em28xx_ir_stop; | |
c373cabf | 708 | |
768da3db FS |
709 | if (dev->board.has_ir_i2c) { /* external i2c device */ |
710 | switch (dev->model) { | |
711 | case EM2800_BOARD_TERRATEC_CINERGY_200: | |
712 | case EM2820_BOARD_TERRATEC_CINERGY_250: | |
713 | rc->map_name = RC_MAP_EM_TERRATEC; | |
714 | ir->get_key_i2c = em28xx_get_key_terratec; | |
715 | break; | |
716 | case EM2820_BOARD_PINNACLE_USB_2: | |
717 | rc->map_name = RC_MAP_PINNACLE_GREY; | |
718 | ir->get_key_i2c = em28xx_get_key_pinnacle_usb_grey; | |
719 | break; | |
720 | case EM2820_BOARD_HAUPPAUGE_WINTV_USB_2: | |
721 | rc->map_name = RC_MAP_HAUPPAUGE; | |
722 | ir->get_key_i2c = em28xx_get_key_em_haup; | |
723 | rc->allowed_protos = RC_BIT_RC5; | |
724 | break; | |
725 | case EM2820_BOARD_LEADTEK_WINFAST_USBII_DELUXE: | |
726 | rc->map_name = RC_MAP_WINFAST_USBII_DELUXE; | |
727 | ir->get_key_i2c = em28xx_get_key_winfast_usbii_deluxe; | |
728 | break; | |
729 | default: | |
730 | err = -ENODEV; | |
731 | goto error; | |
732 | } | |
733 | ||
1d968cda | 734 | ir->i2c_dev_addr = i2c_rc_dev_addr; |
768da3db FS |
735 | } else { /* internal device */ |
736 | switch (dev->chip_id) { | |
737 | case CHIP_ID_EM2860: | |
738 | case CHIP_ID_EM2883: | |
739 | rc->allowed_protos = RC_BIT_RC5 | RC_BIT_NEC; | |
740 | ir->get_key = default_polling_getkey; | |
741 | break; | |
742 | case CHIP_ID_EM2884: | |
743 | case CHIP_ID_EM2874: | |
744 | case CHIP_ID_EM28174: | |
9f1d0bda | 745 | case CHIP_ID_EM28178: |
768da3db FS |
746 | ir->get_key = em2874_polling_getkey; |
747 | rc->allowed_protos = RC_BIT_RC5 | RC_BIT_NEC | | |
748 | RC_BIT_RC6_0; | |
749 | break; | |
750 | default: | |
751 | err = -ENODEV; | |
752 | goto error; | |
753 | } | |
754 | ||
755 | rc->change_protocol = em28xx_ir_change_protocol; | |
756 | rc->map_name = dev->board.ir_codes; | |
757 | ||
758 | /* By default, keep protocol field untouched */ | |
759 | rc_type = RC_BIT_UNKNOWN; | |
760 | err = em28xx_ir_change_protocol(rc, &rc_type); | |
761 | if (err) | |
762 | goto error; | |
0dae8839 MCC |
763 | } |
764 | ||
4b92253a DH |
765 | /* This is how often we ask the chip for IR information */ |
766 | ir->polling = 100; /* ms */ | |
767 | ||
a924a499 | 768 | /* init input device */ |
768da3db | 769 | snprintf(ir->name, sizeof(ir->name), "em28xx IR (%s)", dev->name); |
a924a499 MCC |
770 | |
771 | usb_make_path(dev->udev, ir->phys, sizeof(ir->phys)); | |
772 | strlcat(ir->phys, "/input0", sizeof(ir->phys)); | |
773 | ||
d8b4b582 DH |
774 | rc->input_name = ir->name; |
775 | rc->input_phys = ir->phys; | |
776 | rc->input_id.bustype = BUS_USB; | |
777 | rc->input_id.version = 1; | |
778 | rc->input_id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor); | |
779 | rc->input_id.product = le16_to_cpu(dev->udev->descriptor.idProduct); | |
780 | rc->dev.parent = &dev->udev->dev; | |
d8b4b582 | 781 | rc->driver_name = MODULE_NAME; |
a924a499 MCC |
782 | |
783 | /* all done */ | |
d8b4b582 | 784 | err = rc_register_device(rc); |
a924a499 | 785 | if (err) |
2f5741aa | 786 | goto error; |
a924a499 MCC |
787 | |
788 | return 0; | |
d8b4b582 | 789 | |
2f5741aa | 790 | error: |
a924a499 | 791 | dev->ir = NULL; |
d8b4b582 | 792 | rc_free_device(rc); |
a924a499 MCC |
793 | kfree(ir); |
794 | return err; | |
795 | } | |
796 | ||
f4d4e765 | 797 | static int em28xx_ir_fini(struct em28xx *dev) |
a924a499 MCC |
798 | { |
799 | struct em28xx_IR *ir = dev->ir; | |
800 | ||
f5222609 | 801 | em28xx_shutdown_buttons(dev); |
2fd6f8d1 | 802 | |
a924a499 MCC |
803 | /* skip detach on non attached boards */ |
804 | if (!ir) | |
805 | return 0; | |
806 | ||
6d514774 MCC |
807 | if (ir->rc) |
808 | rc_unregister_device(ir->rc); | |
a924a499 MCC |
809 | |
810 | /* done */ | |
6d514774 | 811 | kfree(ir); |
a924a499 MCC |
812 | dev->ir = NULL; |
813 | return 0; | |
814 | } | |
815 | ||
f4d4e765 EG |
816 | static struct em28xx_ops rc_ops = { |
817 | .id = EM28XX_RC, | |
818 | .name = "Em28xx Input Extension", | |
819 | .init = em28xx_ir_init, | |
820 | .fini = em28xx_ir_fini, | |
821 | }; | |
822 | ||
823 | static int __init em28xx_rc_register(void) | |
824 | { | |
825 | return em28xx_register_extension(&rc_ops); | |
826 | } | |
827 | ||
828 | static void __exit em28xx_rc_unregister(void) | |
829 | { | |
830 | em28xx_unregister_extension(&rc_ops); | |
831 | } | |
832 | ||
833 | MODULE_LICENSE("GPL"); | |
834 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>"); | |
835 | MODULE_DESCRIPTION("Em28xx Input driver"); | |
836 | ||
837 | module_init(em28xx_rc_register); | |
838 | module_exit(em28xx_rc_unregister); |