Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
2 | * |
3 | * keyboard input driver for i2c IR remote controls | |
4 | * | |
5 | * Copyright (c) 2000-2003 Gerd Knorr <kraxel@bytesex.org> | |
6 | * modified for PixelView (BT878P+W/FM) by | |
7 | * Michal Kochanowicz <mkochano@pld.org.pl> | |
8 | * Christoph Bartelmus <lirc@bartelmus.de> | |
9 | * modified for KNC ONE TV Station/Anubis Typhoon TView Tuner by | |
10 | * Ulrich Mueller <ulrich.mueller42@web.de> | |
c365864f MR |
11 | * modified for em2820 based USB TV tuners by |
12 | * Markus Rechberger <mrechberger@gmail.com> | |
d54d6980 CZ |
13 | * modified for DViCO Fusion HDTV 5 RT GOLD by |
14 | * Chaogui Zhang <czhang1974@gmail.com> | |
ba340b40 BR |
15 | * modified for MSI TV@nywhere Plus by |
16 | * Henry Wong <henry@stuffedcow.net> | |
17 | * Mark Schultz <n9xmj@yahoo.com> | |
18 | * Brian Rogers <brian_rogers@comcast.net> | |
cb3bf504 OJ |
19 | * modified for AVerMedia Cardbus by |
20 | * Oldrich Jedlicka <oldium.pro@seznam.cz> | |
acaa34bf SY |
21 | * Zilog Transmitter portions/ideas were derived from GPLv2+ sources: |
22 | * - drivers/char/pctv_zilogir.[ch] from Hauppauge Broadway product | |
23 | * Copyright 2011 Hauppauge Computer works | |
24 | * - drivers/staging/media/lirc/lirc_zilog.c | |
25 | * Copyright (c) 2000 Gerd Knorr <kraxel@goldbach.in-berlin.de> | |
26 | * Michal Kochanowicz <mkochano@pld.org.pl> | |
27 | * Christoph Bartelmus <lirc@bartelmus.de> | |
28 | * Ulrich Mueller <ulrich.mueller42@web.de> | |
29 | * Stefan Jahn <stefan@lkcc.org> | |
30 | * Jerome Brock <jbrock@users.sourceforge.net> | |
31 | * Thomas Reitmayr (treitmayr@yahoo.com) | |
32 | * Mark Weaver <mark@npsl.co.uk> | |
33 | * Jarod Wilson <jarod@redhat.com> | |
34 | * Copyright (C) 2011 Andy Walls <awalls@md.metrocast.net> | |
1da177e4 LT |
35 | * |
36 | * This program is free software; you can redistribute it and/or modify | |
37 | * it under the terms of the GNU General Public License as published by | |
38 | * the Free Software Foundation; either version 2 of the License, or | |
39 | * (at your option) any later version. | |
40 | * | |
41 | * This program is distributed in the hope that it will be useful, | |
42 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
43 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
44 | * GNU General Public License for more details. | |
45 | * | |
1da177e4 LT |
46 | */ |
47 | ||
00bb8207 | 48 | #include <asm/unaligned.h> |
1da177e4 | 49 | #include <linux/module.h> |
1da177e4 LT |
50 | #include <linux/init.h> |
51 | #include <linux/kernel.h> | |
1da177e4 LT |
52 | #include <linux/string.h> |
53 | #include <linux/timer.h> | |
54 | #include <linux/delay.h> | |
55 | #include <linux/errno.h> | |
56 | #include <linux/slab.h> | |
57 | #include <linux/i2c.h> | |
58 | #include <linux/workqueue.h> | |
674434c6 | 59 | |
6bda9644 | 60 | #include <media/rc-core.h> |
b5dcee22 | 61 | #include <media/i2c/ir-kbd-i2c.h> |
c365864f | 62 | |
acaa34bf SY |
63 | #define FLAG_TX 1 |
64 | #define FLAG_HDPVR 2 | |
1da177e4 | 65 | |
1cb26030 SY |
66 | static bool enable_hdpvr; |
67 | module_param(enable_hdpvr, bool, 0644); | |
68 | ||
6d741bfe SY |
69 | static int get_key_haup_common(struct IR_i2c *ir, enum rc_proto *protocol, |
70 | u32 *scancode, u8 *ptoggle, int size) | |
1da177e4 | 71 | { |
39cf1e81 | 72 | unsigned char buf[6]; |
00bb8207 | 73 | int start, range, toggle, dev, code, ircode, vendor; |
1da177e4 LT |
74 | |
75 | /* poll IR chip */ | |
c668f32d | 76 | if (size != i2c_master_recv(ir->c, buf, size)) |
1da177e4 LT |
77 | return -EIO; |
78 | ||
00bb8207 SY |
79 | if (buf[0] & 0x80) { |
80 | int offset = (size == 6) ? 3 : 0; | |
1da177e4 | 81 | |
00bb8207 SY |
82 | /* split rc5 data block ... */ |
83 | start = (buf[offset] >> 7) & 1; | |
84 | range = (buf[offset] >> 6) & 1; | |
85 | toggle = (buf[offset] >> 5) & 1; | |
86 | dev = buf[offset] & 0x1f; | |
87 | code = (buf[offset+1] >> 2) & 0x3f; | |
4dd9bb91 | 88 | |
00bb8207 SY |
89 | /* rc5 has two start bits |
90 | * the first bit must be one | |
91 | * the second bit defines the command range: | |
92 | * 1 = 0-63, 0 = 64 - 127 | |
93 | */ | |
94 | if (!start) | |
95 | /* no key pressed */ | |
96 | return 0; | |
34c08029 | 97 | |
00bb8207 SY |
98 | /* filter out invalid key presses */ |
99 | ircode = (start << 12) | (toggle << 11) | (dev << 6) | code; | |
100 | if ((ircode & 0x1fff) == 0x1fff) | |
101 | return 0; | |
cc7093df | 102 | |
00bb8207 SY |
103 | if (!range) |
104 | code += 64; | |
1da177e4 | 105 | |
50a762b4 SY |
106 | dev_dbg(&ir->rc->dev, |
107 | "ir hauppauge (rc5): s%d r%d t%d dev=%d code=%d\n", | |
00bb8207 SY |
108 | start, range, toggle, dev, code); |
109 | ||
6d741bfe | 110 | *protocol = RC_PROTO_RC5; |
00bb8207 SY |
111 | *scancode = RC_SCANCODE_RC5(dev, code); |
112 | *ptoggle = toggle; | |
113 | ||
114 | return 1; | |
115 | } else if (size == 6 && (buf[0] & 0x40)) { | |
116 | code = buf[4]; | |
117 | dev = buf[3]; | |
118 | vendor = get_unaligned_be16(buf + 1); | |
119 | ||
120 | if (vendor == 0x800f) { | |
121 | *ptoggle = (dev & 0x80) != 0; | |
6d741bfe | 122 | *protocol = RC_PROTO_RC6_MCE; |
00bb8207 | 123 | dev &= 0x7f; |
50a762b4 SY |
124 | dev_dbg(&ir->rc->dev, |
125 | "ir hauppauge (rc6-mce): t%d vendor=%d dev=%d code=%d\n", | |
126 | *ptoggle, vendor, dev, code); | |
00bb8207 SY |
127 | } else { |
128 | *ptoggle = 0; | |
6d741bfe | 129 | *protocol = RC_PROTO_RC6_6A_32; |
50a762b4 SY |
130 | dev_dbg(&ir->rc->dev, |
131 | "ir hauppauge (rc6-6a-32): vendor=%d dev=%d code=%d\n", | |
132 | vendor, dev, code); | |
00bb8207 SY |
133 | } |
134 | ||
135 | *scancode = RC_SCANCODE_RC6_6A(vendor, dev, code); | |
136 | ||
137 | return 1; | |
138 | } | |
139 | ||
140 | return 0; | |
1da177e4 LT |
141 | } |
142 | ||
6d741bfe | 143 | static int get_key_haup(struct IR_i2c *ir, enum rc_proto *protocol, |
4dd9bb91 | 144 | u32 *scancode, u8 *toggle) |
39cf1e81 | 145 | { |
00bb8207 | 146 | return get_key_haup_common(ir, protocol, scancode, toggle, 3); |
39cf1e81 JF |
147 | } |
148 | ||
6d741bfe | 149 | static int get_key_haup_xvr(struct IR_i2c *ir, enum rc_proto *protocol, |
4dd9bb91 | 150 | u32 *scancode, u8 *toggle) |
39cf1e81 | 151 | { |
8df59918 JW |
152 | int ret; |
153 | unsigned char buf[1] = { 0 }; | |
154 | ||
155 | /* | |
156 | * This is the same apparent "are you ready?" poll command observed | |
157 | * watching Windows driver traffic and implemented in lirc_zilog. With | |
158 | * this added, we get far saner remote behavior with z8 chips on usb | |
159 | * connected devices, even with the default polling interval of 100ms. | |
160 | */ | |
161 | ret = i2c_master_send(ir->c, buf, 1); | |
162 | if (ret != 1) | |
163 | return (ret < 0) ? ret : -EINVAL; | |
164 | ||
00bb8207 | 165 | return get_key_haup_common(ir, protocol, scancode, toggle, 6); |
39cf1e81 JF |
166 | } |
167 | ||
6d741bfe | 168 | static int get_key_pixelview(struct IR_i2c *ir, enum rc_proto *protocol, |
4dd9bb91 | 169 | u32 *scancode, u8 *toggle) |
1da177e4 | 170 | { |
c3902dab | 171 | int rc; |
4ac97914 | 172 | unsigned char b; |
1da177e4 LT |
173 | |
174 | /* poll IR chip */ | |
c3902dab MCC |
175 | rc = i2c_master_recv(ir->c, &b, 1); |
176 | if (rc != 1) { | |
50a762b4 | 177 | dev_dbg(&ir->rc->dev, "read error\n"); |
c3902dab MCC |
178 | if (rc < 0) |
179 | return rc; | |
1da177e4 LT |
180 | return -EIO; |
181 | } | |
4dd9bb91 | 182 | |
6d741bfe | 183 | *protocol = RC_PROTO_OTHER; |
4dd9bb91 DH |
184 | *scancode = b; |
185 | *toggle = 0; | |
1da177e4 LT |
186 | return 1; |
187 | } | |
188 | ||
6d741bfe | 189 | static int get_key_fusionhdtv(struct IR_i2c *ir, enum rc_proto *protocol, |
4dd9bb91 | 190 | u32 *scancode, u8 *toggle) |
d54d6980 | 191 | { |
c3902dab | 192 | int rc; |
d54d6980 CZ |
193 | unsigned char buf[4]; |
194 | ||
195 | /* poll IR chip */ | |
c3902dab MCC |
196 | rc = i2c_master_recv(ir->c, buf, 4); |
197 | if (rc != 4) { | |
50a762b4 | 198 | dev_dbg(&ir->rc->dev, "read error\n"); |
c3902dab MCC |
199 | if (rc < 0) |
200 | return rc; | |
d54d6980 CZ |
201 | return -EIO; |
202 | } | |
203 | ||
50a762b4 SY |
204 | if (buf[0] != 0 || buf[1] != 0 || buf[2] != 0 || buf[3] != 0) |
205 | dev_dbg(&ir->rc->dev, "%s: %*ph\n", __func__, 4, buf); | |
d54d6980 CZ |
206 | |
207 | /* no key pressed or signal from other ir remote */ | |
208 | if(buf[0] != 0x1 || buf[1] != 0xfe) | |
209 | return 0; | |
210 | ||
6d741bfe | 211 | *protocol = RC_PROTO_UNKNOWN; |
4dd9bb91 DH |
212 | *scancode = buf[2]; |
213 | *toggle = 0; | |
d54d6980 | 214 | return 1; |
d54d6980 CZ |
215 | } |
216 | ||
6d741bfe | 217 | static int get_key_knc1(struct IR_i2c *ir, enum rc_proto *protocol, |
4dd9bb91 | 218 | u32 *scancode, u8 *toggle) |
1da177e4 | 219 | { |
c3902dab | 220 | int rc; |
1da177e4 LT |
221 | unsigned char b; |
222 | ||
223 | /* poll IR chip */ | |
c3902dab MCC |
224 | rc = i2c_master_recv(ir->c, &b, 1); |
225 | if (rc != 1) { | |
50a762b4 | 226 | dev_dbg(&ir->rc->dev, "read error\n"); |
c3902dab MCC |
227 | if (rc < 0) |
228 | return rc; | |
1da177e4 LT |
229 | return -EIO; |
230 | } | |
231 | ||
232 | /* it seems that 0xFE indicates that a button is still hold | |
4f9c05aa MCC |
233 | down, while 0xff indicates that no button is hold |
234 | down. 0xfe sequences are sometimes interrupted by 0xFF */ | |
1da177e4 | 235 | |
50a762b4 | 236 | dev_dbg(&ir->rc->dev, "key %02x\n", b); |
1da177e4 | 237 | |
4f9c05aa | 238 | if (b == 0xff) |
1da177e4 LT |
239 | return 0; |
240 | ||
4f9c05aa | 241 | if (b == 0xfe) |
1da177e4 LT |
242 | /* keep old data */ |
243 | return 1; | |
244 | ||
6d741bfe | 245 | *protocol = RC_PROTO_UNKNOWN; |
4dd9bb91 DH |
246 | *scancode = b; |
247 | *toggle = 0; | |
1da177e4 LT |
248 | return 1; |
249 | } | |
250 | ||
6d741bfe | 251 | static int get_key_avermedia_cardbus(struct IR_i2c *ir, enum rc_proto *protocol, |
4dd9bb91 | 252 | u32 *scancode, u8 *toggle) |
cb3bf504 OJ |
253 | { |
254 | unsigned char subaddr, key, keygroup; | |
c668f32d | 255 | struct i2c_msg msg[] = { { .addr = ir->c->addr, .flags = 0, |
cb3bf504 | 256 | .buf = &subaddr, .len = 1}, |
c668f32d | 257 | { .addr = ir->c->addr, .flags = I2C_M_RD, |
cb3bf504 OJ |
258 | .buf = &key, .len = 1} }; |
259 | subaddr = 0x0d; | |
c668f32d | 260 | if (2 != i2c_transfer(ir->c->adapter, msg, 2)) { |
50a762b4 | 261 | dev_dbg(&ir->rc->dev, "read error\n"); |
cb3bf504 OJ |
262 | return -EIO; |
263 | } | |
264 | ||
265 | if (key == 0xff) | |
266 | return 0; | |
267 | ||
268 | subaddr = 0x0b; | |
269 | msg[1].buf = &keygroup; | |
c668f32d | 270 | if (2 != i2c_transfer(ir->c->adapter, msg, 2)) { |
50a762b4 | 271 | dev_dbg(&ir->rc->dev, "read error\n"); |
cb3bf504 OJ |
272 | return -EIO; |
273 | } | |
274 | ||
275 | if (keygroup == 0xff) | |
276 | return 0; | |
277 | ||
50a762b4 | 278 | dev_dbg(&ir->rc->dev, "read key 0x%02x/0x%02x\n", key, keygroup); |
34fe2784 | 279 | if (keygroup < 2 || keygroup > 4) { |
50a762b4 SY |
280 | dev_warn(&ir->rc->dev, "warning: invalid key group 0x%02x for key 0x%02x\n", |
281 | keygroup, key); | |
cb3bf504 OJ |
282 | } |
283 | key |= (keygroup & 1) << 6; | |
284 | ||
6d741bfe | 285 | *protocol = RC_PROTO_UNKNOWN; |
4dd9bb91 DH |
286 | *scancode = key; |
287 | if (ir->c->addr == 0x41) /* AVerMedia EM78P153 */ | |
288 | *scancode |= keygroup << 8; | |
289 | *toggle = 0; | |
cb3bf504 OJ |
290 | return 1; |
291 | } | |
292 | ||
1da177e4 LT |
293 | /* ----------------------------------------------------------------------- */ |
294 | ||
90bf3aab | 295 | static int ir_key_poll(struct IR_i2c *ir) |
1da177e4 | 296 | { |
6d741bfe | 297 | enum rc_proto protocol; |
4dd9bb91 DH |
298 | u32 scancode; |
299 | u8 toggle; | |
1da177e4 LT |
300 | int rc; |
301 | ||
50a762b4 | 302 | dev_dbg(&ir->rc->dev, "%s\n", __func__); |
4dd9bb91 | 303 | rc = ir->get_key(ir, &protocol, &scancode, &toggle); |
1da177e4 | 304 | if (rc < 0) { |
50a762b4 | 305 | dev_warn(&ir->rc->dev, "error %d\n", rc); |
90bf3aab | 306 | return rc; |
1da177e4 LT |
307 | } |
308 | ||
e6bcb2f3 | 309 | if (rc) { |
50a762b4 | 310 | dev_dbg(&ir->rc->dev, "%s: proto = 0x%04x, scancode = 0x%08x\n", |
120703f9 DH |
311 | __func__, protocol, scancode); |
312 | rc_keydown(ir->rc, protocol, scancode, toggle); | |
e6bcb2f3 | 313 | } |
90bf3aab | 314 | return 0; |
1da177e4 LT |
315 | } |
316 | ||
c4028958 | 317 | static void ir_work(struct work_struct *work) |
1da177e4 | 318 | { |
90bf3aab | 319 | int rc; |
c1089bdc | 320 | struct IR_i2c *ir = container_of(work, struct IR_i2c, work.work); |
8083c520 | 321 | |
acaa34bf SY |
322 | /* |
323 | * If the transmit code is holding the lock, skip polling for | |
324 | * IR, we'll get it to it next time round | |
325 | */ | |
326 | if (mutex_trylock(&ir->lock)) { | |
327 | rc = ir_key_poll(ir); | |
328 | mutex_unlock(&ir->lock); | |
329 | if (rc == -ENODEV) { | |
330 | rc_unregister_device(ir->rc); | |
331 | ir->rc = NULL; | |
332 | return; | |
333 | } | |
90bf3aab MCC |
334 | } |
335 | ||
c72ba8e6 | 336 | schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling_interval)); |
1da177e4 LT |
337 | } |
338 | ||
a6927d81 SY |
339 | static int ir_open(struct rc_dev *dev) |
340 | { | |
341 | struct IR_i2c *ir = dev->priv; | |
342 | ||
343 | schedule_delayed_work(&ir->work, 0); | |
344 | ||
345 | return 0; | |
346 | } | |
347 | ||
348 | static void ir_close(struct rc_dev *dev) | |
349 | { | |
350 | struct IR_i2c *ir = dev->priv; | |
351 | ||
352 | cancel_delayed_work_sync(&ir->work); | |
353 | } | |
354 | ||
acaa34bf SY |
355 | /* Zilog Transmit Interface */ |
356 | #define XTAL_FREQ 18432000 | |
357 | ||
358 | #define ZILOG_SEND 0x80 | |
359 | #define ZILOG_UIR_END 0x40 | |
360 | #define ZILOG_INIT_END 0x20 | |
361 | #define ZILOG_LIR_END 0x10 | |
362 | ||
363 | #define ZILOG_STATUS_OK 0x80 | |
364 | #define ZILOG_STATUS_TX 0x40 | |
365 | #define ZILOG_STATUS_SET 0x20 | |
366 | ||
367 | /* | |
368 | * As you can see here, very few different lengths of pulse and space | |
369 | * can be encoded. This means that the hardware does not work well with | |
370 | * recorded IR. It's best to work with generated IR, like from ir-ctl or | |
371 | * the in-kernel encoders. | |
372 | */ | |
373 | struct code_block { | |
374 | u8 length; | |
375 | u16 pulse[7]; /* not aligned */ | |
376 | u8 carrier_pulse; | |
377 | u8 carrier_space; | |
378 | u16 space[8]; /* not aligned */ | |
379 | u8 codes[61]; | |
380 | u8 csum[2]; | |
381 | } __packed; | |
382 | ||
383 | static int send_data_block(struct IR_i2c *ir, int cmd, | |
384 | struct code_block *code_block) | |
385 | { | |
386 | int i, j, ret; | |
387 | u8 buf[5], *p; | |
388 | ||
389 | p = &code_block->length; | |
390 | for (i = 0; p < code_block->csum; i++) | |
391 | code_block->csum[i & 1] ^= *p++; | |
392 | ||
393 | p = &code_block->length; | |
394 | ||
395 | for (i = 0; i < sizeof(*code_block);) { | |
396 | int tosend = sizeof(*code_block) - i; | |
397 | ||
398 | if (tosend > 4) | |
399 | tosend = 4; | |
400 | buf[0] = i + 1; | |
401 | for (j = 0; j < tosend; ++j) | |
402 | buf[1 + j] = p[i + j]; | |
403 | dev_dbg(&ir->rc->dev, "%*ph", tosend + 1, buf); | |
404 | ret = i2c_master_send(ir->tx_c, buf, tosend + 1); | |
405 | if (ret != tosend + 1) { | |
406 | dev_dbg(&ir->rc->dev, | |
407 | "i2c_master_send failed with %d\n", ret); | |
408 | return ret < 0 ? ret : -EIO; | |
409 | } | |
410 | i += tosend; | |
411 | } | |
412 | ||
413 | buf[0] = 0; | |
414 | buf[1] = cmd; | |
415 | ret = i2c_master_send(ir->tx_c, buf, 2); | |
416 | if (ret != 2) { | |
417 | dev_err(&ir->rc->dev, "i2c_master_send failed with %d\n", ret); | |
418 | return ret < 0 ? ret : -EIO; | |
419 | } | |
420 | ||
421 | usleep_range(2000, 5000); | |
422 | ||
423 | ret = i2c_master_send(ir->tx_c, buf, 1); | |
424 | if (ret != 1) { | |
425 | dev_err(&ir->rc->dev, "i2c_master_send failed with %d\n", ret); | |
426 | return ret < 0 ? ret : -EIO; | |
427 | } | |
428 | ||
429 | return 0; | |
430 | } | |
431 | ||
432 | static int zilog_init(struct IR_i2c *ir) | |
433 | { | |
434 | struct code_block code_block = { .length = sizeof(code_block) }; | |
435 | u8 buf[4]; | |
436 | int ret; | |
437 | ||
438 | put_unaligned_be16(0x1000, &code_block.pulse[3]); | |
439 | ||
440 | ret = send_data_block(ir, ZILOG_INIT_END, &code_block); | |
441 | if (ret) | |
442 | return ret; | |
443 | ||
444 | ret = i2c_master_recv(ir->tx_c, buf, 4); | |
445 | if (ret != 4) { | |
446 | dev_err(&ir->c->dev, "failed to retrieve firmware version: %d\n", | |
447 | ret); | |
448 | return ret < 0 ? ret : -EIO; | |
449 | } | |
450 | ||
451 | dev_info(&ir->c->dev, "Zilog/Hauppauge IR blaster firmware version %d.%d.%d\n", | |
452 | buf[1], buf[2], buf[3]); | |
453 | ||
454 | return 0; | |
455 | } | |
456 | ||
457 | /* | |
458 | * If the last slot for pulse is the same as the current slot for pulse, | |
459 | * then use slot no 7. | |
460 | */ | |
461 | static void copy_codes(u8 *dst, u8 *src, unsigned int count) | |
462 | { | |
463 | u8 c, last = 0xff; | |
464 | ||
465 | while (count--) { | |
466 | c = *src++; | |
467 | if ((c & 0xf0) == last) { | |
468 | *dst++ = 0x70 | (c & 0xf); | |
469 | } else { | |
470 | *dst++ = c; | |
471 | last = c & 0xf0; | |
472 | } | |
473 | } | |
474 | } | |
475 | ||
476 | /* | |
477 | * When looking for repeats, we don't care about the trailing space. This | |
478 | * is set to the shortest possible anyway. | |
479 | */ | |
480 | static int cmp_no_trail(u8 *a, u8 *b, unsigned int count) | |
481 | { | |
482 | while (--count) { | |
483 | if (*a++ != *b++) | |
484 | return 1; | |
485 | } | |
486 | ||
487 | return (*a & 0xf0) - (*b & 0xf0); | |
488 | } | |
489 | ||
490 | static int find_slot(u16 *array, unsigned int size, u16 val) | |
491 | { | |
492 | int i; | |
493 | ||
494 | for (i = 0; i < size; i++) { | |
495 | if (get_unaligned_be16(&array[i]) == val) { | |
496 | return i; | |
497 | } else if (!array[i]) { | |
498 | put_unaligned_be16(val, &array[i]); | |
499 | return i; | |
500 | } | |
501 | } | |
502 | ||
503 | return -1; | |
504 | } | |
505 | ||
506 | static int zilog_ir_format(struct rc_dev *rcdev, unsigned int *txbuf, | |
507 | unsigned int count, struct code_block *code_block) | |
508 | { | |
509 | struct IR_i2c *ir = rcdev->priv; | |
510 | int rep, i, l, p = 0, s, c = 0; | |
511 | bool repeating; | |
512 | u8 codes[174]; | |
513 | ||
514 | code_block->carrier_pulse = DIV_ROUND_CLOSEST( | |
515 | ir->duty_cycle * XTAL_FREQ / 1000, ir->carrier); | |
516 | code_block->carrier_space = DIV_ROUND_CLOSEST( | |
517 | (100 - ir->duty_cycle) * XTAL_FREQ / 1000, ir->carrier); | |
518 | ||
519 | for (i = 0; i < count; i++) { | |
520 | if (c >= ARRAY_SIZE(codes) - 1) { | |
521 | dev_warn(&rcdev->dev, "IR too long, cannot transmit\n"); | |
522 | return -EINVAL; | |
523 | } | |
524 | ||
525 | /* | |
526 | * Lengths more than 142220us cannot be encoded; also | |
527 | * this checks for multiply overflow | |
528 | */ | |
529 | if (txbuf[i] > 142220) | |
530 | return -EINVAL; | |
531 | ||
532 | l = DIV_ROUND_CLOSEST((XTAL_FREQ / 1000) * txbuf[i], 40000); | |
533 | ||
534 | if (i & 1) { | |
535 | s = find_slot(code_block->space, | |
536 | ARRAY_SIZE(code_block->space), l); | |
537 | if (s == -1) { | |
538 | dev_warn(&rcdev->dev, "Too many different lengths spaces, cannot transmit"); | |
539 | return -EINVAL; | |
540 | } | |
541 | ||
542 | /* We have a pulse and space */ | |
543 | codes[c++] = (p << 4) | s; | |
544 | } else { | |
545 | p = find_slot(code_block->pulse, | |
546 | ARRAY_SIZE(code_block->pulse), l); | |
547 | if (p == -1) { | |
548 | dev_warn(&rcdev->dev, "Too many different lengths pulses, cannot transmit"); | |
549 | return -EINVAL; | |
550 | } | |
551 | } | |
552 | } | |
553 | ||
554 | /* We have to encode the trailing pulse. Find the shortest space */ | |
555 | s = 0; | |
556 | for (i = 1; i < ARRAY_SIZE(code_block->space); i++) { | |
557 | u16 d = get_unaligned_be16(&code_block->space[i]); | |
558 | ||
559 | if (get_unaligned_be16(&code_block->space[s]) > d) | |
560 | s = i; | |
561 | } | |
562 | ||
563 | codes[c++] = (p << 4) | s; | |
564 | ||
565 | dev_dbg(&rcdev->dev, "generated %d codes\n", c); | |
566 | ||
567 | /* | |
568 | * Are the last N codes (so pulse + space) repeating 3 times? | |
569 | * if so we can shorten the codes list and use code 0xc0 to repeat | |
570 | * them. | |
571 | */ | |
572 | repeating = false; | |
573 | ||
574 | for (rep = c / 3; rep >= 1; rep--) { | |
575 | if (!memcmp(&codes[c - rep * 3], &codes[c - rep * 2], rep) && | |
576 | !cmp_no_trail(&codes[c - rep], &codes[c - rep * 2], rep)) { | |
577 | repeating = true; | |
578 | break; | |
579 | } | |
580 | } | |
581 | ||
582 | if (repeating) { | |
583 | /* first copy any leading non-repeating */ | |
584 | int leading = c - rep * 3; | |
585 | ||
9863bc49 | 586 | if (leading >= ARRAY_SIZE(code_block->codes) - 3 - rep) { |
acaa34bf SY |
587 | dev_warn(&rcdev->dev, "IR too long, cannot transmit\n"); |
588 | return -EINVAL; | |
589 | } | |
590 | ||
591 | dev_dbg(&rcdev->dev, "found trailing %d repeat\n", rep); | |
592 | copy_codes(code_block->codes, codes, leading); | |
593 | code_block->codes[leading] = 0x82; | |
594 | copy_codes(code_block->codes + leading + 1, codes + leading, | |
595 | rep); | |
596 | c = leading + 1 + rep; | |
597 | code_block->codes[c++] = 0xc0; | |
598 | } else { | |
599 | if (c >= ARRAY_SIZE(code_block->codes) - 3) { | |
600 | dev_warn(&rcdev->dev, "IR too long, cannot transmit\n"); | |
601 | return -EINVAL; | |
602 | } | |
603 | ||
604 | dev_dbg(&rcdev->dev, "found no trailing repeat\n"); | |
605 | code_block->codes[0] = 0x82; | |
606 | copy_codes(code_block->codes + 1, codes, c); | |
607 | c++; | |
608 | code_block->codes[c++] = 0xc4; | |
609 | } | |
610 | ||
611 | while (c < ARRAY_SIZE(code_block->codes)) | |
612 | code_block->codes[c++] = 0x83; | |
613 | ||
614 | return 0; | |
615 | } | |
616 | ||
617 | static int zilog_tx(struct rc_dev *rcdev, unsigned int *txbuf, | |
618 | unsigned int count) | |
619 | { | |
620 | struct IR_i2c *ir = rcdev->priv; | |
621 | struct code_block code_block = { .length = sizeof(code_block) }; | |
622 | u8 buf[2]; | |
623 | int ret, i; | |
624 | ||
625 | ret = zilog_ir_format(rcdev, txbuf, count, &code_block); | |
626 | if (ret) | |
627 | return ret; | |
628 | ||
629 | ret = mutex_lock_interruptible(&ir->lock); | |
630 | if (ret) | |
631 | return ret; | |
632 | ||
633 | ret = send_data_block(ir, ZILOG_UIR_END, &code_block); | |
634 | if (ret) | |
635 | goto out_unlock; | |
636 | ||
637 | ret = i2c_master_recv(ir->tx_c, buf, 1); | |
638 | if (ret != 1) { | |
639 | dev_err(&ir->rc->dev, "i2c_master_recv failed with %d\n", ret); | |
640 | goto out_unlock; | |
641 | } | |
642 | ||
643 | dev_dbg(&ir->rc->dev, "code set status: %02x\n", buf[0]); | |
644 | ||
645 | if (buf[0] != (ZILOG_STATUS_OK | ZILOG_STATUS_SET)) { | |
646 | dev_err(&ir->rc->dev, "unexpected IR TX response %02x\n", | |
647 | buf[0]); | |
648 | ret = -EIO; | |
649 | goto out_unlock; | |
650 | } | |
651 | ||
652 | buf[0] = 0x00; | |
653 | buf[1] = ZILOG_SEND; | |
654 | ||
655 | ret = i2c_master_send(ir->tx_c, buf, 2); | |
656 | if (ret != 2) { | |
657 | dev_err(&ir->rc->dev, "i2c_master_send failed with %d\n", ret); | |
658 | if (ret >= 0) | |
659 | ret = -EIO; | |
660 | goto out_unlock; | |
661 | } | |
662 | ||
663 | dev_dbg(&ir->rc->dev, "send command sent\n"); | |
664 | ||
665 | /* | |
666 | * This bit NAKs until the device is ready, so we retry it | |
667 | * sleeping a bit each time. This seems to be what the windows | |
668 | * driver does, approximately. | |
669 | * Try for up to 1s. | |
670 | */ | |
671 | for (i = 0; i < 20; ++i) { | |
672 | set_current_state(TASK_UNINTERRUPTIBLE); | |
673 | schedule_timeout(msecs_to_jiffies(50)); | |
674 | ret = i2c_master_send(ir->tx_c, buf, 1); | |
675 | if (ret == 1) | |
676 | break; | |
677 | dev_dbg(&ir->rc->dev, | |
678 | "NAK expected: i2c_master_send failed with %d (try %d)\n", | |
679 | ret, i + 1); | |
680 | } | |
681 | ||
682 | if (ret != 1) { | |
683 | dev_err(&ir->rc->dev, | |
684 | "IR TX chip never got ready: last i2c_master_send failed with %d\n", | |
685 | ret); | |
686 | if (ret >= 0) | |
687 | ret = -EIO; | |
688 | goto out_unlock; | |
689 | } | |
690 | ||
691 | i = i2c_master_recv(ir->tx_c, buf, 1); | |
692 | if (i != 1) { | |
693 | dev_err(&ir->rc->dev, "i2c_master_recv failed with %d\n", ret); | |
694 | ret = -EIO; | |
695 | goto out_unlock; | |
696 | } else if (buf[0] != ZILOG_STATUS_OK) { | |
697 | dev_err(&ir->rc->dev, "unexpected IR TX response #2: %02x\n", | |
698 | buf[0]); | |
699 | ret = -EIO; | |
700 | goto out_unlock; | |
701 | } | |
702 | dev_dbg(&ir->rc->dev, "transmit complete\n"); | |
703 | ||
704 | /* Oh good, it worked */ | |
705 | ret = count; | |
706 | out_unlock: | |
707 | mutex_unlock(&ir->lock); | |
708 | ||
709 | return ret; | |
710 | } | |
711 | ||
712 | static int zilog_tx_carrier(struct rc_dev *dev, u32 carrier) | |
713 | { | |
714 | struct IR_i2c *ir = dev->priv; | |
715 | ||
716 | if (carrier > 500000 || carrier < 20000) | |
717 | return -EINVAL; | |
718 | ||
719 | ir->carrier = carrier; | |
720 | ||
721 | return 0; | |
722 | } | |
723 | ||
724 | static int zilog_tx_duty_cycle(struct rc_dev *dev, u32 duty_cycle) | |
725 | { | |
726 | struct IR_i2c *ir = dev->priv; | |
727 | ||
728 | ir->duty_cycle = duty_cycle; | |
729 | ||
730 | return 0; | |
731 | } | |
1da177e4 | 732 | |
c668f32d | 733 | static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id) |
1da177e4 | 734 | { |
02858eed | 735 | char *ir_codes = NULL; |
9a4cc5ac | 736 | const char *name = NULL; |
6d741bfe | 737 | u64 rc_proto = RC_PROTO_BIT_UNKNOWN; |
afd1a0c9 | 738 | struct IR_i2c *ir; |
2eb25832 | 739 | struct rc_dev *rc = NULL; |
c668f32d JD |
740 | struct i2c_adapter *adap = client->adapter; |
741 | unsigned short addr = client->addr; | |
7bc8a0de | 742 | bool probe_tx = (id->driver_data & FLAG_TX) != 0; |
b07b4783 | 743 | int err; |
1da177e4 | 744 | |
1cb26030 SY |
745 | if ((id->driver_data & FLAG_HDPVR) && !enable_hdpvr) { |
746 | dev_err(&client->dev, "IR for HDPVR is known to cause problems during recording, use enable_hdpvr modparam to enable\n"); | |
747 | return -ENODEV; | |
748 | } | |
749 | ||
c02b211d | 750 | ir = devm_kzalloc(&client->dev, sizeof(*ir), GFP_KERNEL); |
2eb25832 MCC |
751 | if (!ir) |
752 | return -ENOMEM; | |
b7df3910 | 753 | |
c668f32d | 754 | ir->c = client; |
c72ba8e6 | 755 | ir->polling_interval = DEFAULT_POLLING_INTERVAL; |
c668f32d | 756 | i2c_set_clientdata(client, ir); |
d21838dd | 757 | |
1da177e4 LT |
758 | switch(addr) { |
759 | case 0x64: | |
760 | name = "Pixelview"; | |
761 | ir->get_key = get_key_pixelview; | |
6d741bfe | 762 | rc_proto = RC_PROTO_BIT_OTHER; |
02858eed | 763 | ir_codes = RC_MAP_EMPTY; |
1da177e4 | 764 | break; |
1da177e4 | 765 | case 0x18: |
ca39d84d | 766 | case 0x1f: |
1da177e4 LT |
767 | case 0x1a: |
768 | name = "Hauppauge"; | |
769 | ir->get_key = get_key_haup; | |
6d741bfe | 770 | rc_proto = RC_PROTO_BIT_RC5; |
20624106 | 771 | ir_codes = RC_MAP_HAUPPAUGE; |
1da177e4 LT |
772 | break; |
773 | case 0x30: | |
d5e52653 MCC |
774 | name = "KNC One"; |
775 | ir->get_key = get_key_knc1; | |
6d741bfe | 776 | rc_proto = RC_PROTO_BIT_OTHER; |
02858eed | 777 | ir_codes = RC_MAP_EMPTY; |
1da177e4 | 778 | break; |
d54d6980 | 779 | case 0x6b: |
3c44358c MK |
780 | name = "FusionHDTV"; |
781 | ir->get_key = get_key_fusionhdtv; | |
6d741bfe | 782 | rc_proto = RC_PROTO_BIT_UNKNOWN; |
02858eed | 783 | ir_codes = RC_MAP_FUSIONHDTV_MCE; |
d54d6980 | 784 | break; |
cb3bf504 OJ |
785 | case 0x40: |
786 | name = "AVerMedia Cardbus remote"; | |
787 | ir->get_key = get_key_avermedia_cardbus; | |
6d741bfe | 788 | rc_proto = RC_PROTO_BIT_OTHER; |
02858eed | 789 | ir_codes = RC_MAP_AVERMEDIA_CARDBUS; |
cb3bf504 | 790 | break; |
34fe2784 OZ |
791 | case 0x41: |
792 | name = "AVerMedia EM78P153"; | |
793 | ir->get_key = get_key_avermedia_cardbus; | |
6d741bfe | 794 | rc_proto = RC_PROTO_BIT_OTHER; |
34fe2784 OZ |
795 | /* RM-KV remote, seems to be same as RM-K6 */ |
796 | ir_codes = RC_MAP_AVERMEDIA_M733A_RM_K6; | |
797 | break; | |
c69a4af6 AW |
798 | case 0x71: |
799 | name = "Hauppauge/Zilog Z8"; | |
800 | ir->get_key = get_key_haup_xvr; | |
6d741bfe SY |
801 | rc_proto = RC_PROTO_BIT_RC5 | RC_PROTO_BIT_RC6_MCE | |
802 | RC_PROTO_BIT_RC6_6A_32; | |
20624106 | 803 | ir_codes = RC_MAP_HAUPPAUGE; |
7bc8a0de | 804 | probe_tx = true; |
c69a4af6 | 805 | break; |
1da177e4 LT |
806 | } |
807 | ||
4d7a2d67 JD |
808 | /* Let the caller override settings */ |
809 | if (client->dev.platform_data) { | |
810 | const struct IR_i2c_init_data *init_data = | |
811 | client->dev.platform_data; | |
812 | ||
813 | ir_codes = init_data->ir_codes; | |
2eb25832 MCC |
814 | rc = init_data->rc_dev; |
815 | ||
4d7a2d67 | 816 | name = init_data->name; |
1b6e59e3 | 817 | if (init_data->type) |
6d741bfe | 818 | rc_proto = init_data->type; |
1b6e59e3 | 819 | |
c72ba8e6 MCC |
820 | if (init_data->polling_interval) |
821 | ir->polling_interval = init_data->polling_interval; | |
822 | ||
1b6e59e3 AW |
823 | switch (init_data->internal_get_key_func) { |
824 | case IR_KBD_GET_KEY_CUSTOM: | |
825 | /* The bridge driver provided us its own function */ | |
826 | ir->get_key = init_data->get_key; | |
827 | break; | |
828 | case IR_KBD_GET_KEY_PIXELVIEW: | |
829 | ir->get_key = get_key_pixelview; | |
830 | break; | |
1b6e59e3 AW |
831 | case IR_KBD_GET_KEY_HAUP: |
832 | ir->get_key = get_key_haup; | |
833 | break; | |
834 | case IR_KBD_GET_KEY_KNC1: | |
835 | ir->get_key = get_key_knc1; | |
836 | break; | |
837 | case IR_KBD_GET_KEY_FUSIONHDTV: | |
838 | ir->get_key = get_key_fusionhdtv; | |
839 | break; | |
840 | case IR_KBD_GET_KEY_HAUP_XVR: | |
841 | ir->get_key = get_key_haup_xvr; | |
842 | break; | |
843 | case IR_KBD_GET_KEY_AVERMEDIA_CARDBUS: | |
844 | ir->get_key = get_key_avermedia_cardbus; | |
845 | break; | |
846 | } | |
4d7a2d67 JD |
847 | } |
848 | ||
2eb25832 MCC |
849 | if (!rc) { |
850 | /* | |
39c1cb2b | 851 | * If platform_data doesn't specify rc_dev, initialize it |
2eb25832 MCC |
852 | * internally |
853 | */ | |
0f7499fd | 854 | rc = rc_allocate_device(RC_DRIVER_SCANCODE); |
c02b211d LP |
855 | if (!rc) |
856 | return -ENOMEM; | |
2eb25832 MCC |
857 | } |
858 | ir->rc = rc; | |
859 | ||
9a4cc5ac | 860 | /* Make sure we are all setup before going on */ |
6d741bfe | 861 | if (!name || !ir->get_key || !rc_proto || !ir_codes) { |
50a762b4 SY |
862 | dev_warn(&client->dev, "Unsupported device at address 0x%02x\n", |
863 | addr); | |
9a4cc5ac JD |
864 | err = -ENODEV; |
865 | goto err_out_free; | |
866 | } | |
867 | ||
b07b4783 | 868 | ir->ir_codes = ir_codes; |
d5e52653 | 869 | |
afc7f24c | 870 | snprintf(ir->phys, sizeof(ir->phys), "%s/%s", dev_name(&adap->dev), |
c668f32d | 871 | dev_name(&client->dev)); |
ac9cd976 | 872 | |
2eb25832 MCC |
873 | /* |
874 | * Initialize input_dev fields | |
875 | * It doesn't make sense to allow overriding them via platform_data | |
876 | */ | |
d8b4b582 | 877 | rc->input_id.bustype = BUS_I2C; |
d8b4b582 | 878 | rc->input_phys = ir->phys; |
afc7f24c SY |
879 | rc->device_name = name; |
880 | rc->dev.parent = &client->dev; | |
a6927d81 SY |
881 | rc->priv = ir; |
882 | rc->open = ir_open; | |
883 | rc->close = ir_close; | |
2eb25832 MCC |
884 | |
885 | /* | |
886 | * Initialize the other fields of rc_dev | |
887 | */ | |
888 | rc->map_name = ir->ir_codes; | |
6d741bfe | 889 | rc->allowed_protocols = rc_proto; |
2eb25832 | 890 | if (!rc->driver_name) |
50a762b4 | 891 | rc->driver_name = KBUILD_MODNAME; |
b7df3910 | 892 | |
acaa34bf SY |
893 | mutex_init(&ir->lock); |
894 | ||
a6927d81 SY |
895 | INIT_DELAYED_WORK(&ir->work, ir_work); |
896 | ||
7bc8a0de | 897 | if (probe_tx) { |
acaa34bf SY |
898 | ir->tx_c = i2c_new_dummy(client->adapter, 0x70); |
899 | if (!ir->tx_c) { | |
900 | dev_err(&client->dev, "failed to setup tx i2c address"); | |
901 | } else if (!zilog_init(ir)) { | |
902 | ir->carrier = 38000; | |
903 | ir->duty_cycle = 40; | |
904 | rc->tx_ir = zilog_tx; | |
905 | rc->s_tx_carrier = zilog_tx_carrier; | |
906 | rc->s_tx_duty_cycle = zilog_tx_duty_cycle; | |
907 | } | |
908 | } | |
909 | ||
d8b4b582 | 910 | err = rc_register_device(rc); |
b07b4783 | 911 | if (err) |
c668f32d | 912 | goto err_out_free; |
b07b4783 | 913 | |
1da177e4 | 914 | return 0; |
b07b4783 | 915 | |
b07b4783 | 916 | err_out_free: |
acaa34bf SY |
917 | if (ir->tx_c) |
918 | i2c_unregister_device(ir->tx_c); | |
919 | ||
2eb25832 | 920 | /* Only frees rc if it were allocated internally */ |
d8b4b582 | 921 | rc_free_device(rc); |
b07b4783 | 922 | return err; |
1da177e4 LT |
923 | } |
924 | ||
c668f32d | 925 | static int ir_remove(struct i2c_client *client) |
1da177e4 | 926 | { |
4ac97914 | 927 | struct IR_i2c *ir = i2c_get_clientdata(client); |
1da177e4 LT |
928 | |
929 | /* kill outstanding polls */ | |
c1089bdc | 930 | cancel_delayed_work_sync(&ir->work); |
1da177e4 | 931 | |
acaa34bf SY |
932 | if (ir->tx_c) |
933 | i2c_unregister_device(ir->tx_c); | |
934 | ||
c668f32d | 935 | /* unregister device */ |
8f1aeedf | 936 | rc_unregister_device(ir->rc); |
1da177e4 LT |
937 | |
938 | /* free memory */ | |
1da177e4 LT |
939 | return 0; |
940 | } | |
941 | ||
c668f32d JD |
942 | static const struct i2c_device_id ir_kbd_id[] = { |
943 | /* Generic entry for any IR receiver */ | |
944 | { "ir_video", 0 }, | |
f5d887ae | 945 | /* IR device specific entries should be added here */ |
acaa34bf SY |
946 | { "ir_z8f0811_haup", FLAG_TX }, |
947 | { "ir_z8f0811_hdpvr", FLAG_TX | FLAG_HDPVR }, | |
c668f32d JD |
948 | { } |
949 | }; | |
1cb26030 | 950 | MODULE_DEVICE_TABLE(i2c, ir_kbd_id); |
cb3bf504 | 951 | |
c6e8d86f | 952 | static struct i2c_driver ir_kbd_driver = { |
c668f32d JD |
953 | .driver = { |
954 | .name = "ir-kbd-i2c", | |
955 | }, | |
956 | .probe = ir_probe, | |
957 | .remove = ir_remove, | |
958 | .id_table = ir_kbd_id, | |
959 | }; | |
1da177e4 | 960 | |
c6e8d86f AL |
961 | module_i2c_driver(ir_kbd_driver); |
962 | ||
1da177e4 LT |
963 | /* ----------------------------------------------------------------------- */ |
964 | ||
965 | MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, Ulrich Mueller"); | |
966 | MODULE_DESCRIPTION("input driver for i2c IR remote controls"); | |
967 | MODULE_LICENSE("GPL"); |