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