blob: 1d11aab1817a446b928c7ad68b440c4fad5a3321 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
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>
Markus Rechbergerc3658642005-11-08 21:37:21 -080011 * modified for em2820 based USB TV tuners by
12 * Markus Rechberger <mrechberger@gmail.com>
Chaogui Zhangd54d6982007-08-24 01:02:32 -030013 * modified for DViCO Fusion HDTV 5 RT GOLD by
14 * Chaogui Zhang <czhang1974@gmail.com>
Brian Rogersba340b42008-10-13 08:37:06 -030015 * 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>
Oldřich Jedličkacb3bf502009-02-12 03:43:11 -030019 * modified for AVerMedia Cardbus by
20 * Oldrich Jedlicka <oldium.pro@seznam.cz>
Sean Youngacaa34b2017-10-21 08:16:47 -040021 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 *
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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 */
47
Sean Young00bb8202016-09-19 19:21:23 -030048#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/init.h>
51#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#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>
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -080059
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -030060#include <media/rc-core.h>
Mauro Carvalho Chehabb5dcee22015-11-10 12:01:44 -020061#include <media/i2c/ir-kbd-i2c.h>
Markus Rechbergerc3658642005-11-08 21:37:21 -080062
Sean Youngacaa34b2017-10-21 08:16:47 -040063#define FLAG_TX 1
64#define FLAG_HDPVR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Sean Young1cb26032017-10-24 17:04:16 -040066static bool enable_hdpvr;
67module_param(enable_hdpvr, bool, 0644);
68
Sean Young6d741bf2017-08-07 16:20:58 -040069static int get_key_haup_common(struct IR_i2c *ir, enum rc_proto *protocol,
70 u32 *scancode, u8 *ptoggle, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Jan Frey39cf1e82007-06-25 14:34:06 -030072 unsigned char buf[6];
Sean Young00bb8202016-09-19 19:21:23 -030073 int start, range, toggle, dev, code, ircode, vendor;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 /* poll IR chip */
Jean Delvarec668f322009-05-13 16:48:50 -030076 if (size != i2c_master_recv(ir->c, buf, size))
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return -EIO;
78
Sean Young00bb8202016-09-19 19:21:23 -030079 if (buf[0] & 0x80) {
80 int offset = (size == 6) ? 3 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Sean Young00bb8202016-09-19 19:21:23 -030082 /* 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;
David Härdeman4dd9bb92014-04-03 20:31:25 -030088
Sean Young00bb8202016-09-19 19:21:23 -030089 /* 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;
Darron Broad34c08022008-09-22 00:54:59 -030097
Sean Young00bb8202016-09-19 19:21:23 -030098 /* filter out invalid key presses */
99 ircode = (start << 12) | (toggle << 11) | (dev << 6) | code;
100 if ((ircode & 0x1fff) == 0x1fff)
101 return 0;
David Hardemancc7093d2006-09-26 16:39:00 -0300102
Sean Young00bb8202016-09-19 19:21:23 -0300103 if (!range)
104 code += 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Sean Young50a762b2017-10-18 10:00:50 -0400106 dev_dbg(&ir->rc->dev,
107 "ir hauppauge (rc5): s%d r%d t%d dev=%d code=%d\n",
Sean Young00bb8202016-09-19 19:21:23 -0300108 start, range, toggle, dev, code);
109
Sean Young6d741bf2017-08-07 16:20:58 -0400110 *protocol = RC_PROTO_RC5;
Sean Young00bb8202016-09-19 19:21:23 -0300111 *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;
Sean Young6d741bf2017-08-07 16:20:58 -0400122 *protocol = RC_PROTO_RC6_MCE;
Sean Young00bb8202016-09-19 19:21:23 -0300123 dev &= 0x7f;
Sean Young50a762b2017-10-18 10:00:50 -0400124 dev_dbg(&ir->rc->dev,
125 "ir hauppauge (rc6-mce): t%d vendor=%d dev=%d code=%d\n",
126 *ptoggle, vendor, dev, code);
Sean Young00bb8202016-09-19 19:21:23 -0300127 } else {
128 *ptoggle = 0;
Sean Young6d741bf2017-08-07 16:20:58 -0400129 *protocol = RC_PROTO_RC6_6A_32;
Sean Young50a762b2017-10-18 10:00:50 -0400130 dev_dbg(&ir->rc->dev,
131 "ir hauppauge (rc6-6a-32): vendor=%d dev=%d code=%d\n",
132 vendor, dev, code);
Sean Young00bb8202016-09-19 19:21:23 -0300133 }
134
135 *scancode = RC_SCANCODE_RC6_6A(vendor, dev, code);
136
137 return 1;
138 }
139
140 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Sean Young6d741bf2017-08-07 16:20:58 -0400143static int get_key_haup(struct IR_i2c *ir, enum rc_proto *protocol,
David Härdeman4dd9bb92014-04-03 20:31:25 -0300144 u32 *scancode, u8 *toggle)
Jan Frey39cf1e82007-06-25 14:34:06 -0300145{
Sean Young00bb8202016-09-19 19:21:23 -0300146 return get_key_haup_common(ir, protocol, scancode, toggle, 3);
Jan Frey39cf1e82007-06-25 14:34:06 -0300147}
148
Sean Young6d741bf2017-08-07 16:20:58 -0400149static int get_key_haup_xvr(struct IR_i2c *ir, enum rc_proto *protocol,
David Härdeman4dd9bb92014-04-03 20:31:25 -0300150 u32 *scancode, u8 *toggle)
Jan Frey39cf1e82007-06-25 14:34:06 -0300151{
Jarod Wilson8df59912011-01-20 18:31:18 -0300152 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
Sean Young00bb8202016-09-19 19:21:23 -0300165 return get_key_haup_common(ir, protocol, scancode, toggle, 6);
Jan Frey39cf1e82007-06-25 14:34:06 -0300166}
167
Sean Young6d741bf2017-08-07 16:20:58 -0400168static int get_key_pixelview(struct IR_i2c *ir, enum rc_proto *protocol,
David Härdeman4dd9bb92014-04-03 20:31:25 -0300169 u32 *scancode, u8 *toggle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Mauro Carvalho Chehabc3902da2018-03-23 07:10:12 -0400171 int rc;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800172 unsigned char b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 /* poll IR chip */
Mauro Carvalho Chehabc3902da2018-03-23 07:10:12 -0400175 rc = i2c_master_recv(ir->c, &b, 1);
176 if (rc != 1) {
Sean Young50a762b2017-10-18 10:00:50 -0400177 dev_dbg(&ir->rc->dev, "read error\n");
Mauro Carvalho Chehabc3902da2018-03-23 07:10:12 -0400178 if (rc < 0)
179 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return -EIO;
181 }
David Härdeman4dd9bb92014-04-03 20:31:25 -0300182
Sean Young6d741bf2017-08-07 16:20:58 -0400183 *protocol = RC_PROTO_OTHER;
David Härdeman4dd9bb92014-04-03 20:31:25 -0300184 *scancode = b;
185 *toggle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return 1;
187}
188
Sean Young6d741bf2017-08-07 16:20:58 -0400189static int get_key_fusionhdtv(struct IR_i2c *ir, enum rc_proto *protocol,
David Härdeman4dd9bb92014-04-03 20:31:25 -0300190 u32 *scancode, u8 *toggle)
Chaogui Zhangd54d6982007-08-24 01:02:32 -0300191{
Mauro Carvalho Chehabc3902da2018-03-23 07:10:12 -0400192 int rc;
Chaogui Zhangd54d6982007-08-24 01:02:32 -0300193 unsigned char buf[4];
194
195 /* poll IR chip */
Mauro Carvalho Chehabc3902da2018-03-23 07:10:12 -0400196 rc = i2c_master_recv(ir->c, buf, 4);
197 if (rc != 4) {
Sean Young50a762b2017-10-18 10:00:50 -0400198 dev_dbg(&ir->rc->dev, "read error\n");
Mauro Carvalho Chehabc3902da2018-03-23 07:10:12 -0400199 if (rc < 0)
200 return rc;
Chaogui Zhangd54d6982007-08-24 01:02:32 -0300201 return -EIO;
202 }
203
Sean Young50a762b2017-10-18 10:00:50 -0400204 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);
Chaogui Zhangd54d6982007-08-24 01:02:32 -0300206
207 /* no key pressed or signal from other ir remote */
208 if(buf[0] != 0x1 || buf[1] != 0xfe)
209 return 0;
210
Sean Young6d741bf2017-08-07 16:20:58 -0400211 *protocol = RC_PROTO_UNKNOWN;
David Härdeman4dd9bb92014-04-03 20:31:25 -0300212 *scancode = buf[2];
213 *toggle = 0;
Chaogui Zhangd54d6982007-08-24 01:02:32 -0300214 return 1;
Chaogui Zhangd54d6982007-08-24 01:02:32 -0300215}
216
Sean Young6d741bf2017-08-07 16:20:58 -0400217static int get_key_knc1(struct IR_i2c *ir, enum rc_proto *protocol,
David Härdeman4dd9bb92014-04-03 20:31:25 -0300218 u32 *scancode, u8 *toggle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Mauro Carvalho Chehabc3902da2018-03-23 07:10:12 -0400220 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 unsigned char b;
222
223 /* poll IR chip */
Mauro Carvalho Chehabc3902da2018-03-23 07:10:12 -0400224 rc = i2c_master_recv(ir->c, &b, 1);
225 if (rc != 1) {
Sean Young50a762b2017-10-18 10:00:50 -0400226 dev_dbg(&ir->rc->dev, "read error\n");
Mauro Carvalho Chehabc3902da2018-03-23 07:10:12 -0400227 if (rc < 0)
228 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return -EIO;
230 }
231
232 /* it seems that 0xFE indicates that a button is still hold
Mauro Carvalho Chehab4f9c05a2005-11-08 21:37:36 -0800233 down, while 0xff indicates that no button is hold
234 down. 0xfe sequences are sometimes interrupted by 0xFF */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Sean Young50a762b2017-10-18 10:00:50 -0400236 dev_dbg(&ir->rc->dev, "key %02x\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Mauro Carvalho Chehab4f9c05a2005-11-08 21:37:36 -0800238 if (b == 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return 0;
240
Mauro Carvalho Chehab4f9c05a2005-11-08 21:37:36 -0800241 if (b == 0xfe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 /* keep old data */
243 return 1;
244
Sean Young6d741bf2017-08-07 16:20:58 -0400245 *protocol = RC_PROTO_UNKNOWN;
David Härdeman4dd9bb92014-04-03 20:31:25 -0300246 *scancode = b;
247 *toggle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return 1;
249}
250
Sean Young6d741bf2017-08-07 16:20:58 -0400251static int get_key_avermedia_cardbus(struct IR_i2c *ir, enum rc_proto *protocol,
David Härdeman4dd9bb92014-04-03 20:31:25 -0300252 u32 *scancode, u8 *toggle)
Oldřich Jedličkacb3bf502009-02-12 03:43:11 -0300253{
254 unsigned char subaddr, key, keygroup;
Jean Delvarec668f322009-05-13 16:48:50 -0300255 struct i2c_msg msg[] = { { .addr = ir->c->addr, .flags = 0,
Oldřich Jedličkacb3bf502009-02-12 03:43:11 -0300256 .buf = &subaddr, .len = 1},
Jean Delvarec668f322009-05-13 16:48:50 -0300257 { .addr = ir->c->addr, .flags = I2C_M_RD,
Oldřich Jedličkacb3bf502009-02-12 03:43:11 -0300258 .buf = &key, .len = 1} };
259 subaddr = 0x0d;
Jean Delvarec668f322009-05-13 16:48:50 -0300260 if (2 != i2c_transfer(ir->c->adapter, msg, 2)) {
Sean Young50a762b2017-10-18 10:00:50 -0400261 dev_dbg(&ir->rc->dev, "read error\n");
Oldřich Jedličkacb3bf502009-02-12 03:43:11 -0300262 return -EIO;
263 }
264
265 if (key == 0xff)
266 return 0;
267
268 subaddr = 0x0b;
269 msg[1].buf = &keygroup;
Jean Delvarec668f322009-05-13 16:48:50 -0300270 if (2 != i2c_transfer(ir->c->adapter, msg, 2)) {
Sean Young50a762b2017-10-18 10:00:50 -0400271 dev_dbg(&ir->rc->dev, "read error\n");
Oldřich Jedličkacb3bf502009-02-12 03:43:11 -0300272 return -EIO;
273 }
274
275 if (keygroup == 0xff)
276 return 0;
277
Sean Young50a762b2017-10-18 10:00:50 -0400278 dev_dbg(&ir->rc->dev, "read key 0x%02x/0x%02x\n", key, keygroup);
Ondrej Zary34fe2782013-04-06 14:28:16 -0300279 if (keygroup < 2 || keygroup > 4) {
Sean Young50a762b2017-10-18 10:00:50 -0400280 dev_warn(&ir->rc->dev, "warning: invalid key group 0x%02x for key 0x%02x\n",
281 keygroup, key);
Oldřich Jedličkacb3bf502009-02-12 03:43:11 -0300282 }
283 key |= (keygroup & 1) << 6;
284
Sean Young6d741bf2017-08-07 16:20:58 -0400285 *protocol = RC_PROTO_UNKNOWN;
David Härdeman4dd9bb92014-04-03 20:31:25 -0300286 *scancode = key;
287 if (ir->c->addr == 0x41) /* AVerMedia EM78P153 */
288 *scancode |= keygroup << 8;
289 *toggle = 0;
Oldřich Jedličkacb3bf502009-02-12 03:43:11 -0300290 return 1;
291}
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293/* ----------------------------------------------------------------------- */
294
Mauro Carvalho Chehab90bf3aa2012-01-09 22:28:13 -0200295static int ir_key_poll(struct IR_i2c *ir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Sean Young6d741bf2017-08-07 16:20:58 -0400297 enum rc_proto protocol;
David Härdeman4dd9bb92014-04-03 20:31:25 -0300298 u32 scancode;
299 u8 toggle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 int rc;
301
Sean Young50a762b2017-10-18 10:00:50 -0400302 dev_dbg(&ir->rc->dev, "%s\n", __func__);
David Härdeman4dd9bb92014-04-03 20:31:25 -0300303 rc = ir->get_key(ir, &protocol, &scancode, &toggle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (rc < 0) {
Sean Young50a762b2017-10-18 10:00:50 -0400305 dev_warn(&ir->rc->dev, "error %d\n", rc);
Mauro Carvalho Chehab90bf3aa2012-01-09 22:28:13 -0200306 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308
Mauro Carvalho Chehabe6bcb2f2011-01-12 13:50:01 -0300309 if (rc) {
Sean Young50a762b2017-10-18 10:00:50 -0400310 dev_dbg(&ir->rc->dev, "%s: proto = 0x%04x, scancode = 0x%08x\n",
David Härdeman120703f2014-04-03 20:31:30 -0300311 __func__, protocol, scancode);
312 rc_keydown(ir->rc, protocol, scancode, toggle);
Mauro Carvalho Chehabe6bcb2f2011-01-12 13:50:01 -0300313 }
Mauro Carvalho Chehab90bf3aa2012-01-09 22:28:13 -0200314 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
David Howellsc4028952006-11-22 14:57:56 +0000317static void ir_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Mauro Carvalho Chehab90bf3aa2012-01-09 22:28:13 -0200319 int rc;
Jean Delvarec1089bd2009-03-07 07:43:43 -0300320 struct IR_i2c *ir = container_of(work, struct IR_i2c, work.work);
Dmitry Torokhov8083c522007-05-21 11:51:11 -0300321
Sean Youngacaa34b2017-10-21 08:16:47 -0400322 /*
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 }
Mauro Carvalho Chehab90bf3aa2012-01-09 22:28:13 -0200334 }
335
Mauro Carvalho Chehabc72ba8e2010-09-23 01:23:10 -0300336 schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling_interval));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Sean Younga6927d82017-10-17 16:31:20 -0400339static 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
348static 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
Sean Youngacaa34b2017-10-21 08:16:47 -0400355/* 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 */
373struct 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
383static 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
432static 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 */
461static 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 */
480static 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
490static 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
506static 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
586 if (leading + rep >= ARRAY_SIZE(code_block->codes) - 3) {
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
617static 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;
706out_unlock:
707 mutex_unlock(&ir->lock);
708
709 return ret;
710}
711
712static 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
724static 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}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Jean Delvarec668f322009-05-13 16:48:50 -0300733static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300735 char *ir_codes = NULL;
Jean Delvare9a4cc5a2009-05-13 16:50:11 -0300736 const char *name = NULL;
Sean Young6d741bf2017-08-07 16:20:58 -0400737 u64 rc_proto = RC_PROTO_BIT_UNKNOWN;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800738 struct IR_i2c *ir;
Mauro Carvalho Chehab2eb25832010-11-12 09:02:40 -0300739 struct rc_dev *rc = NULL;
Jean Delvarec668f322009-05-13 16:48:50 -0300740 struct i2c_adapter *adap = client->adapter;
741 unsigned short addr = client->addr;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300742 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Sean Young1cb26032017-10-24 17:04:16 -0400744 if ((id->driver_data & FLAG_HDPVR) && !enable_hdpvr) {
745 dev_err(&client->dev, "IR for HDPVR is known to cause problems during recording, use enable_hdpvr modparam to enable\n");
746 return -ENODEV;
747 }
748
Laurent Pinchartc02b2112013-05-02 08:29:43 -0300749 ir = devm_kzalloc(&client->dev, sizeof(*ir), GFP_KERNEL);
Mauro Carvalho Chehab2eb25832010-11-12 09:02:40 -0300750 if (!ir)
751 return -ENOMEM;
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500752
Jean Delvarec668f322009-05-13 16:48:50 -0300753 ir->c = client;
Mauro Carvalho Chehabc72ba8e2010-09-23 01:23:10 -0300754 ir->polling_interval = DEFAULT_POLLING_INTERVAL;
Jean Delvarec668f322009-05-13 16:48:50 -0300755 i2c_set_clientdata(client, ir);
Mauro Carvalho Chehabd21838d2006-01-09 15:25:21 -0200756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 switch(addr) {
758 case 0x64:
759 name = "Pixelview";
760 ir->get_key = get_key_pixelview;
Sean Young6d741bf2017-08-07 16:20:58 -0400761 rc_proto = RC_PROTO_BIT_OTHER;
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300762 ir_codes = RC_MAP_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 case 0x18:
Magnus Almca39d842009-11-13 05:48:24 -0300765 case 0x1f:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 case 0x1a:
767 name = "Hauppauge";
768 ir->get_key = get_key_haup;
Sean Young6d741bf2017-08-07 16:20:58 -0400769 rc_proto = RC_PROTO_BIT_RC5;
Mauro Carvalho Chehab20624102011-01-24 22:23:08 -0300770 ir_codes = RC_MAP_HAUPPAUGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 break;
772 case 0x30:
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -0800773 name = "KNC One";
774 ir->get_key = get_key_knc1;
Sean Young6d741bf2017-08-07 16:20:58 -0400775 rc_proto = RC_PROTO_BIT_OTHER;
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300776 ir_codes = RC_MAP_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 break;
Chaogui Zhangd54d6982007-08-24 01:02:32 -0300778 case 0x6b:
Michael Krufky3c443582007-08-24 01:07:12 -0300779 name = "FusionHDTV";
780 ir->get_key = get_key_fusionhdtv;
Sean Young6d741bf2017-08-07 16:20:58 -0400781 rc_proto = RC_PROTO_BIT_UNKNOWN;
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300782 ir_codes = RC_MAP_FUSIONHDTV_MCE;
Chaogui Zhangd54d6982007-08-24 01:02:32 -0300783 break;
Oldřich Jedličkacb3bf502009-02-12 03:43:11 -0300784 case 0x40:
785 name = "AVerMedia Cardbus remote";
786 ir->get_key = get_key_avermedia_cardbus;
Sean Young6d741bf2017-08-07 16:20:58 -0400787 rc_proto = RC_PROTO_BIT_OTHER;
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300788 ir_codes = RC_MAP_AVERMEDIA_CARDBUS;
Oldřich Jedličkacb3bf502009-02-12 03:43:11 -0300789 break;
Ondrej Zary34fe2782013-04-06 14:28:16 -0300790 case 0x41:
791 name = "AVerMedia EM78P153";
792 ir->get_key = get_key_avermedia_cardbus;
Sean Young6d741bf2017-08-07 16:20:58 -0400793 rc_proto = RC_PROTO_BIT_OTHER;
Ondrej Zary34fe2782013-04-06 14:28:16 -0300794 /* RM-KV remote, seems to be same as RM-K6 */
795 ir_codes = RC_MAP_AVERMEDIA_M733A_RM_K6;
796 break;
Andy Wallsc69a4af2011-01-16 15:45:32 -0300797 case 0x71:
798 name = "Hauppauge/Zilog Z8";
799 ir->get_key = get_key_haup_xvr;
Sean Young6d741bf2017-08-07 16:20:58 -0400800 rc_proto = RC_PROTO_BIT_RC5 | RC_PROTO_BIT_RC6_MCE |
801 RC_PROTO_BIT_RC6_6A_32;
Mauro Carvalho Chehab20624102011-01-24 22:23:08 -0300802 ir_codes = RC_MAP_HAUPPAUGE;
Andy Wallsc69a4af2011-01-16 15:45:32 -0300803 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
805
Jean Delvare4d7a2d62009-05-13 16:49:32 -0300806 /* Let the caller override settings */
807 if (client->dev.platform_data) {
808 const struct IR_i2c_init_data *init_data =
809 client->dev.platform_data;
810
811 ir_codes = init_data->ir_codes;
Mauro Carvalho Chehab2eb25832010-11-12 09:02:40 -0300812 rc = init_data->rc_dev;
813
Jean Delvare4d7a2d62009-05-13 16:49:32 -0300814 name = init_data->name;
Andy Walls1b6e59e32009-07-28 11:44:05 -0300815 if (init_data->type)
Sean Young6d741bf2017-08-07 16:20:58 -0400816 rc_proto = init_data->type;
Andy Walls1b6e59e32009-07-28 11:44:05 -0300817
Mauro Carvalho Chehabc72ba8e2010-09-23 01:23:10 -0300818 if (init_data->polling_interval)
819 ir->polling_interval = init_data->polling_interval;
820
Andy Walls1b6e59e32009-07-28 11:44:05 -0300821 switch (init_data->internal_get_key_func) {
822 case IR_KBD_GET_KEY_CUSTOM:
823 /* The bridge driver provided us its own function */
824 ir->get_key = init_data->get_key;
825 break;
826 case IR_KBD_GET_KEY_PIXELVIEW:
827 ir->get_key = get_key_pixelview;
828 break;
Andy Walls1b6e59e32009-07-28 11:44:05 -0300829 case IR_KBD_GET_KEY_HAUP:
830 ir->get_key = get_key_haup;
831 break;
832 case IR_KBD_GET_KEY_KNC1:
833 ir->get_key = get_key_knc1;
834 break;
835 case IR_KBD_GET_KEY_FUSIONHDTV:
836 ir->get_key = get_key_fusionhdtv;
837 break;
838 case IR_KBD_GET_KEY_HAUP_XVR:
839 ir->get_key = get_key_haup_xvr;
840 break;
841 case IR_KBD_GET_KEY_AVERMEDIA_CARDBUS:
842 ir->get_key = get_key_avermedia_cardbus;
843 break;
844 }
Jean Delvare4d7a2d62009-05-13 16:49:32 -0300845 }
846
Mauro Carvalho Chehab2eb25832010-11-12 09:02:40 -0300847 if (!rc) {
848 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -0300849 * If platform_data doesn't specify rc_dev, initialize it
Mauro Carvalho Chehab2eb25832010-11-12 09:02:40 -0300850 * internally
851 */
Andi Shyti0f7499f2016-12-16 06:50:58 -0200852 rc = rc_allocate_device(RC_DRIVER_SCANCODE);
Laurent Pinchartc02b2112013-05-02 08:29:43 -0300853 if (!rc)
854 return -ENOMEM;
Mauro Carvalho Chehab2eb25832010-11-12 09:02:40 -0300855 }
856 ir->rc = rc;
857
Jean Delvare9a4cc5a2009-05-13 16:50:11 -0300858 /* Make sure we are all setup before going on */
Sean Young6d741bf2017-08-07 16:20:58 -0400859 if (!name || !ir->get_key || !rc_proto || !ir_codes) {
Sean Young50a762b2017-10-18 10:00:50 -0400860 dev_warn(&client->dev, "Unsupported device at address 0x%02x\n",
861 addr);
Jean Delvare9a4cc5a2009-05-13 16:50:11 -0300862 err = -ENODEV;
863 goto err_out_free;
864 }
865
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300866 ir->ir_codes = ir_codes;
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -0800867
Sean Youngafc7f242017-10-18 09:39:12 -0400868 snprintf(ir->phys, sizeof(ir->phys), "%s/%s", dev_name(&adap->dev),
Jean Delvarec668f322009-05-13 16:48:50 -0300869 dev_name(&client->dev));
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800870
Mauro Carvalho Chehab2eb25832010-11-12 09:02:40 -0300871 /*
872 * Initialize input_dev fields
873 * It doesn't make sense to allow overriding them via platform_data
874 */
David Härdemand8b4b582010-10-29 16:08:23 -0300875 rc->input_id.bustype = BUS_I2C;
David Härdemand8b4b582010-10-29 16:08:23 -0300876 rc->input_phys = ir->phys;
Sean Youngafc7f242017-10-18 09:39:12 -0400877 rc->device_name = name;
878 rc->dev.parent = &client->dev;
Sean Younga6927d82017-10-17 16:31:20 -0400879 rc->priv = ir;
880 rc->open = ir_open;
881 rc->close = ir_close;
Mauro Carvalho Chehab2eb25832010-11-12 09:02:40 -0300882
883 /*
884 * Initialize the other fields of rc_dev
885 */
886 rc->map_name = ir->ir_codes;
Sean Young6d741bf2017-08-07 16:20:58 -0400887 rc->allowed_protocols = rc_proto;
Mauro Carvalho Chehab2eb25832010-11-12 09:02:40 -0300888 if (!rc->driver_name)
Sean Young50a762b2017-10-18 10:00:50 -0400889 rc->driver_name = KBUILD_MODNAME;
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500890
Sean Youngacaa34b2017-10-21 08:16:47 -0400891 mutex_init(&ir->lock);
892
Sean Younga6927d82017-10-17 16:31:20 -0400893 INIT_DELAYED_WORK(&ir->work, ir_work);
894
Sean Youngacaa34b2017-10-21 08:16:47 -0400895 if (id->driver_data & FLAG_TX) {
896 ir->tx_c = i2c_new_dummy(client->adapter, 0x70);
897 if (!ir->tx_c) {
898 dev_err(&client->dev, "failed to setup tx i2c address");
899 } else if (!zilog_init(ir)) {
900 ir->carrier = 38000;
901 ir->duty_cycle = 40;
902 rc->tx_ir = zilog_tx;
903 rc->s_tx_carrier = zilog_tx_carrier;
904 rc->s_tx_duty_cycle = zilog_tx_duty_cycle;
905 }
906 }
907
David Härdemand8b4b582010-10-29 16:08:23 -0300908 err = rc_register_device(rc);
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300909 if (err)
Jean Delvarec668f322009-05-13 16:48:50 -0300910 goto err_out_free;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return 0;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300913
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300914 err_out_free:
Sean Youngacaa34b2017-10-21 08:16:47 -0400915 if (ir->tx_c)
916 i2c_unregister_device(ir->tx_c);
917
Mauro Carvalho Chehab2eb25832010-11-12 09:02:40 -0300918 /* Only frees rc if it were allocated internally */
David Härdemand8b4b582010-10-29 16:08:23 -0300919 rc_free_device(rc);
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300920 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
Jean Delvarec668f322009-05-13 16:48:50 -0300923static int ir_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800925 struct IR_i2c *ir = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 /* kill outstanding polls */
Jean Delvarec1089bd2009-03-07 07:43:43 -0300928 cancel_delayed_work_sync(&ir->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Sean Youngacaa34b2017-10-21 08:16:47 -0400930 if (ir->tx_c)
931 i2c_unregister_device(ir->tx_c);
932
Jean Delvarec668f322009-05-13 16:48:50 -0300933 /* unregister device */
Markus Elfring8f1aeed2014-11-20 07:13:16 -0300934 rc_unregister_device(ir->rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 /* free memory */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return 0;
938}
939
Jean Delvarec668f322009-05-13 16:48:50 -0300940static const struct i2c_device_id ir_kbd_id[] = {
941 /* Generic entry for any IR receiver */
942 { "ir_video", 0 },
Andy Wallsf5d887a2009-07-28 11:50:14 -0300943 /* IR device specific entries should be added here */
Sean Youngacaa34b2017-10-21 08:16:47 -0400944 { "ir_z8f0811_haup", FLAG_TX },
945 { "ir_z8f0811_hdpvr", FLAG_TX | FLAG_HDPVR },
Jean Delvarec668f322009-05-13 16:48:50 -0300946 { }
947};
Sean Young1cb26032017-10-24 17:04:16 -0400948MODULE_DEVICE_TABLE(i2c, ir_kbd_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Axel Linc6e8d862012-02-12 06:56:32 -0300950static struct i2c_driver ir_kbd_driver = {
Jean Delvarec668f322009-05-13 16:48:50 -0300951 .driver = {
952 .name = "ir-kbd-i2c",
953 },
954 .probe = ir_probe,
955 .remove = ir_remove,
956 .id_table = ir_kbd_id,
957};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Axel Linc6e8d862012-02-12 06:56:32 -0300959module_i2c_driver(ir_kbd_driver);
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961/* ----------------------------------------------------------------------- */
962
963MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, Ulrich Mueller");
964MODULE_DESCRIPTION("input driver for i2c IR remote controls");
965MODULE_LICENSE("GPL");