ir-kbd-i2c.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  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>
  12. * modified for em2820 based USB TV tuners by
  13. * Markus Rechberger <mrechberger@gmail.com>
  14. * modified for DViCO Fusion HDTV 5 RT GOLD by
  15. * Chaogui Zhang <czhang1974@gmail.com>
  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>
  20. * modified for AVerMedia Cardbus by
  21. * Oldrich Jedlicka <oldium.pro@seznam.cz>
  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>
  36. */
  37. #include <linux/unaligned.h>
  38. #include <linux/module.h>
  39. #include <linux/init.h>
  40. #include <linux/kernel.h>
  41. #include <linux/string.h>
  42. #include <linux/timer.h>
  43. #include <linux/delay.h>
  44. #include <linux/errno.h>
  45. #include <linux/slab.h>
  46. #include <linux/i2c.h>
  47. #include <linux/workqueue.h>
  48. #include <media/rc-core.h>
  49. #include <media/i2c/ir-kbd-i2c.h>
  50. #define FLAG_TX 1
  51. #define FLAG_HDPVR 2
  52. static bool enable_hdpvr;
  53. module_param(enable_hdpvr, bool, 0644);
  54. static int get_key_haup_common(struct IR_i2c *ir, enum rc_proto *protocol,
  55. u32 *scancode, u8 *ptoggle, int size)
  56. {
  57. unsigned char buf[6];
  58. int start, range, toggle, dev, code, ircode, vendor;
  59. /* poll IR chip */
  60. if (size != i2c_master_recv(ir->c, buf, size))
  61. return -EIO;
  62. if (buf[0] & 0x80) {
  63. int offset = (size == 6) ? 3 : 0;
  64. /* split rc5 data block ... */
  65. start = (buf[offset] >> 7) & 1;
  66. range = (buf[offset] >> 6) & 1;
  67. toggle = (buf[offset] >> 5) & 1;
  68. dev = buf[offset] & 0x1f;
  69. code = (buf[offset+1] >> 2) & 0x3f;
  70. /* rc5 has two start bits
  71. * the first bit must be one
  72. * the second bit defines the command range:
  73. * 1 = 0-63, 0 = 64 - 127
  74. */
  75. if (!start)
  76. /* no key pressed */
  77. return 0;
  78. /* filter out invalid key presses */
  79. ircode = (start << 12) | (toggle << 11) | (dev << 6) | code;
  80. if ((ircode & 0x1fff) == 0x1fff)
  81. return 0;
  82. if (!range)
  83. code += 64;
  84. dev_dbg(&ir->rc->dev,
  85. "ir hauppauge (rc5): s%d r%d t%d dev=%d code=%d\n",
  86. start, range, toggle, dev, code);
  87. *protocol = RC_PROTO_RC5;
  88. *scancode = RC_SCANCODE_RC5(dev, code);
  89. *ptoggle = toggle;
  90. return 1;
  91. } else if (size == 6 && (buf[0] & 0x40)) {
  92. code = buf[4];
  93. dev = buf[3];
  94. vendor = get_unaligned_be16(buf + 1);
  95. if (vendor == 0x800f) {
  96. *ptoggle = (dev & 0x80) != 0;
  97. *protocol = RC_PROTO_RC6_MCE;
  98. dev &= 0x7f;
  99. dev_dbg(&ir->rc->dev,
  100. "ir hauppauge (rc6-mce): t%d vendor=%d dev=%d code=%d\n",
  101. *ptoggle, vendor, dev, code);
  102. } else {
  103. *ptoggle = 0;
  104. *protocol = RC_PROTO_RC6_6A_32;
  105. dev_dbg(&ir->rc->dev,
  106. "ir hauppauge (rc6-6a-32): vendor=%d dev=%d code=%d\n",
  107. vendor, dev, code);
  108. }
  109. *scancode = RC_SCANCODE_RC6_6A(vendor, dev, code);
  110. return 1;
  111. }
  112. return 0;
  113. }
  114. static int get_key_haup(struct IR_i2c *ir, enum rc_proto *protocol,
  115. u32 *scancode, u8 *toggle)
  116. {
  117. return get_key_haup_common(ir, protocol, scancode, toggle, 3);
  118. }
  119. static int get_key_haup_xvr(struct IR_i2c *ir, enum rc_proto *protocol,
  120. u32 *scancode, u8 *toggle)
  121. {
  122. int ret;
  123. unsigned char buf[1] = { 0 };
  124. /*
  125. * This is the same apparent "are you ready?" poll command observed
  126. * watching Windows driver traffic and implemented in lirc_zilog. With
  127. * this added, we get far saner remote behavior with z8 chips on usb
  128. * connected devices, even with the default polling interval of 100ms.
  129. */
  130. ret = i2c_master_send(ir->c, buf, 1);
  131. if (ret != 1)
  132. return (ret < 0) ? ret : -EINVAL;
  133. return get_key_haup_common(ir, protocol, scancode, toggle, 6);
  134. }
  135. static int get_key_pixelview(struct IR_i2c *ir, enum rc_proto *protocol,
  136. u32 *scancode, u8 *toggle)
  137. {
  138. int rc;
  139. unsigned char b;
  140. /* poll IR chip */
  141. rc = i2c_master_recv(ir->c, &b, 1);
  142. if (rc != 1) {
  143. dev_dbg(&ir->rc->dev, "read error\n");
  144. if (rc < 0)
  145. return rc;
  146. return -EIO;
  147. }
  148. *protocol = RC_PROTO_OTHER;
  149. *scancode = b;
  150. *toggle = 0;
  151. return 1;
  152. }
  153. static int get_key_fusionhdtv(struct IR_i2c *ir, enum rc_proto *protocol,
  154. u32 *scancode, u8 *toggle)
  155. {
  156. int rc;
  157. unsigned char buf[4];
  158. /* poll IR chip */
  159. rc = i2c_master_recv(ir->c, buf, 4);
  160. if (rc != 4) {
  161. dev_dbg(&ir->rc->dev, "read error\n");
  162. if (rc < 0)
  163. return rc;
  164. return -EIO;
  165. }
  166. if (buf[0] != 0 || buf[1] != 0 || buf[2] != 0 || buf[3] != 0)
  167. dev_dbg(&ir->rc->dev, "%s: %*ph\n", __func__, 4, buf);
  168. /* no key pressed or signal from other ir remote */
  169. if(buf[0] != 0x1 || buf[1] != 0xfe)
  170. return 0;
  171. *protocol = RC_PROTO_UNKNOWN;
  172. *scancode = buf[2];
  173. *toggle = 0;
  174. return 1;
  175. }
  176. static int get_key_knc1(struct IR_i2c *ir, enum rc_proto *protocol,
  177. u32 *scancode, u8 *toggle)
  178. {
  179. int rc;
  180. unsigned char b;
  181. /* poll IR chip */
  182. rc = i2c_master_recv(ir->c, &b, 1);
  183. if (rc != 1) {
  184. dev_dbg(&ir->rc->dev, "read error\n");
  185. if (rc < 0)
  186. return rc;
  187. return -EIO;
  188. }
  189. /* it seems that 0xFE indicates that a button is still hold
  190. down, while 0xff indicates that no button is hold
  191. down. 0xfe sequences are sometimes interrupted by 0xFF */
  192. dev_dbg(&ir->rc->dev, "key %02x\n", b);
  193. if (b == 0xff)
  194. return 0;
  195. if (b == 0xfe)
  196. /* keep old data */
  197. return 1;
  198. *protocol = RC_PROTO_UNKNOWN;
  199. *scancode = b;
  200. *toggle = 0;
  201. return 1;
  202. }
  203. static int get_key_geniatech(struct IR_i2c *ir, enum rc_proto *protocol,
  204. u32 *scancode, u8 *toggle)
  205. {
  206. int i, rc;
  207. unsigned char b;
  208. /* poll IR chip */
  209. for (i = 0; i < 4; i++) {
  210. rc = i2c_master_recv(ir->c, &b, 1);
  211. if (rc == 1)
  212. break;
  213. msleep(20);
  214. }
  215. if (rc != 1) {
  216. dev_dbg(&ir->rc->dev, "read error\n");
  217. if (rc < 0)
  218. return rc;
  219. return -EIO;
  220. }
  221. /* don't repeat the key */
  222. if (ir->old == b)
  223. return 0;
  224. ir->old = b;
  225. /* decode to RC5 */
  226. b &= 0x7f;
  227. b = (b - 1) / 2;
  228. dev_dbg(&ir->rc->dev, "key %02x\n", b);
  229. *protocol = RC_PROTO_RC5;
  230. *scancode = b;
  231. *toggle = ir->old >> 7;
  232. return 1;
  233. }
  234. static int get_key_avermedia_cardbus(struct IR_i2c *ir, enum rc_proto *protocol,
  235. u32 *scancode, u8 *toggle)
  236. {
  237. unsigned char subaddr, key, keygroup;
  238. struct i2c_msg msg[] = { { .addr = ir->c->addr, .flags = 0,
  239. .buf = &subaddr, .len = 1},
  240. { .addr = ir->c->addr, .flags = I2C_M_RD,
  241. .buf = &key, .len = 1} };
  242. subaddr = 0x0d;
  243. if (2 != i2c_transfer(ir->c->adapter, msg, 2)) {
  244. dev_dbg(&ir->rc->dev, "read error\n");
  245. return -EIO;
  246. }
  247. if (key == 0xff)
  248. return 0;
  249. subaddr = 0x0b;
  250. msg[1].buf = &keygroup;
  251. if (2 != i2c_transfer(ir->c->adapter, msg, 2)) {
  252. dev_dbg(&ir->rc->dev, "read error\n");
  253. return -EIO;
  254. }
  255. if (keygroup == 0xff)
  256. return 0;
  257. dev_dbg(&ir->rc->dev, "read key 0x%02x/0x%02x\n", key, keygroup);
  258. if (keygroup < 2 || keygroup > 4) {
  259. dev_warn(&ir->rc->dev, "warning: invalid key group 0x%02x for key 0x%02x\n",
  260. keygroup, key);
  261. }
  262. key |= (keygroup & 1) << 6;
  263. *protocol = RC_PROTO_UNKNOWN;
  264. *scancode = key;
  265. if (ir->c->addr == 0x41) /* AVerMedia EM78P153 */
  266. *scancode |= keygroup << 8;
  267. *toggle = 0;
  268. return 1;
  269. }
  270. /* ----------------------------------------------------------------------- */
  271. static int ir_key_poll(struct IR_i2c *ir)
  272. {
  273. enum rc_proto protocol = 0;
  274. u32 scancode = 0;
  275. u8 toggle = 0;
  276. int rc;
  277. dev_dbg(&ir->rc->dev, "%s\n", __func__);
  278. rc = ir->get_key(ir, &protocol, &scancode, &toggle);
  279. if (rc < 0) {
  280. dev_warn(&ir->rc->dev, "error %d\n", rc);
  281. return rc;
  282. }
  283. if (rc) {
  284. dev_dbg(&ir->rc->dev, "%s: proto = 0x%04x, scancode = 0x%08x\n",
  285. __func__, protocol, scancode);
  286. rc_keydown(ir->rc, protocol, scancode, toggle);
  287. }
  288. return 0;
  289. }
  290. static void ir_work(struct work_struct *work)
  291. {
  292. int rc;
  293. struct IR_i2c *ir = container_of(work, struct IR_i2c, work.work);
  294. /*
  295. * If the transmit code is holding the lock, skip polling for
  296. * IR, we'll get it to it next time round
  297. */
  298. if (mutex_trylock(&ir->lock)) {
  299. rc = ir_key_poll(ir);
  300. mutex_unlock(&ir->lock);
  301. if (rc == -ENODEV) {
  302. rc_unregister_device(ir->rc);
  303. ir->rc = NULL;
  304. return;
  305. }
  306. }
  307. schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling_interval));
  308. }
  309. static int ir_open(struct rc_dev *dev)
  310. {
  311. struct IR_i2c *ir = dev->priv;
  312. schedule_delayed_work(&ir->work, 0);
  313. return 0;
  314. }
  315. static void ir_close(struct rc_dev *dev)
  316. {
  317. struct IR_i2c *ir = dev->priv;
  318. cancel_delayed_work_sync(&ir->work);
  319. }
  320. /* Zilog Transmit Interface */
  321. #define XTAL_FREQ 18432000
  322. #define ZILOG_SEND 0x80
  323. #define ZILOG_UIR_END 0x40
  324. #define ZILOG_INIT_END 0x20
  325. #define ZILOG_LIR_END 0x10
  326. #define ZILOG_STATUS_OK 0x80
  327. #define ZILOG_STATUS_TX 0x40
  328. #define ZILOG_STATUS_SET 0x20
  329. /*
  330. * As you can see here, very few different lengths of pulse and space
  331. * can be encoded. This means that the hardware does not work well with
  332. * recorded IR. It's best to work with generated IR, like from ir-ctl or
  333. * the in-kernel encoders.
  334. */
  335. struct code_block {
  336. u8 length;
  337. u16 pulse[7]; /* not aligned */
  338. u8 carrier_pulse;
  339. u8 carrier_space;
  340. u16 space[8]; /* not aligned */
  341. u8 codes[61];
  342. u8 csum[2];
  343. } __packed;
  344. static int send_data_block(struct IR_i2c *ir, int cmd,
  345. struct code_block *code_block)
  346. {
  347. int i, j, ret;
  348. u8 buf[5], *p;
  349. p = &code_block->length;
  350. for (i = 0; p < code_block->csum; i++)
  351. code_block->csum[i & 1] ^= *p++;
  352. p = &code_block->length;
  353. for (i = 0; i < sizeof(*code_block);) {
  354. int tosend = sizeof(*code_block) - i;
  355. if (tosend > 4)
  356. tosend = 4;
  357. buf[0] = i + 1;
  358. for (j = 0; j < tosend; ++j)
  359. buf[1 + j] = p[i + j];
  360. dev_dbg(&ir->rc->dev, "%*ph", tosend + 1, buf);
  361. ret = i2c_master_send(ir->tx_c, buf, tosend + 1);
  362. if (ret != tosend + 1) {
  363. dev_dbg(&ir->rc->dev,
  364. "i2c_master_send failed with %d\n", ret);
  365. return ret < 0 ? ret : -EIO;
  366. }
  367. i += tosend;
  368. }
  369. buf[0] = 0;
  370. buf[1] = cmd;
  371. ret = i2c_master_send(ir->tx_c, buf, 2);
  372. if (ret != 2) {
  373. dev_err(&ir->rc->dev, "i2c_master_send failed with %d\n", ret);
  374. return ret < 0 ? ret : -EIO;
  375. }
  376. usleep_range(2000, 5000);
  377. ret = i2c_master_send(ir->tx_c, buf, 1);
  378. if (ret != 1) {
  379. dev_err(&ir->rc->dev, "i2c_master_send failed with %d\n", ret);
  380. return ret < 0 ? ret : -EIO;
  381. }
  382. return 0;
  383. }
  384. static int zilog_init(struct IR_i2c *ir)
  385. {
  386. struct code_block code_block = { .length = sizeof(code_block) };
  387. u8 buf[4];
  388. int ret;
  389. put_unaligned_be16(0x1000, &code_block.pulse[3]);
  390. ret = send_data_block(ir, ZILOG_INIT_END, &code_block);
  391. if (ret)
  392. return ret;
  393. ret = i2c_master_recv(ir->tx_c, buf, 4);
  394. if (ret != 4) {
  395. dev_err(&ir->c->dev, "failed to retrieve firmware version: %d\n",
  396. ret);
  397. return ret < 0 ? ret : -EIO;
  398. }
  399. dev_info(&ir->c->dev, "Zilog/Hauppauge IR blaster firmware version %d.%d.%d\n",
  400. buf[1], buf[2], buf[3]);
  401. return 0;
  402. }
  403. /*
  404. * If the last slot for pulse is the same as the current slot for pulse,
  405. * then use slot no 7.
  406. */
  407. static void copy_codes(u8 *dst, u8 *src, unsigned int count)
  408. {
  409. u8 c, last = 0xff;
  410. while (count--) {
  411. c = *src++;
  412. if ((c & 0xf0) == last) {
  413. *dst++ = 0x70 | (c & 0xf);
  414. } else {
  415. *dst++ = c;
  416. last = c & 0xf0;
  417. }
  418. }
  419. }
  420. /*
  421. * When looking for repeats, we don't care about the trailing space. This
  422. * is set to the shortest possible anyway.
  423. */
  424. static int cmp_no_trail(u8 *a, u8 *b, unsigned int count)
  425. {
  426. while (--count) {
  427. if (*a++ != *b++)
  428. return 1;
  429. }
  430. return (*a & 0xf0) - (*b & 0xf0);
  431. }
  432. static int find_slot(u16 *array, unsigned int size, u16 val)
  433. {
  434. int i;
  435. for (i = 0; i < size; i++) {
  436. if (get_unaligned_be16(&array[i]) == val) {
  437. return i;
  438. } else if (!array[i]) {
  439. put_unaligned_be16(val, &array[i]);
  440. return i;
  441. }
  442. }
  443. return -1;
  444. }
  445. static int zilog_ir_format(struct rc_dev *rcdev, unsigned int *txbuf,
  446. unsigned int count, struct code_block *code_block)
  447. {
  448. struct IR_i2c *ir = rcdev->priv;
  449. int rep, i, l, p = 0, s, c = 0;
  450. bool repeating;
  451. u8 codes[174];
  452. code_block->carrier_pulse = DIV_ROUND_CLOSEST(
  453. ir->duty_cycle * XTAL_FREQ / 1000, ir->carrier);
  454. code_block->carrier_space = DIV_ROUND_CLOSEST(
  455. (100 - ir->duty_cycle) * XTAL_FREQ / 1000, ir->carrier);
  456. for (i = 0; i < count; i++) {
  457. if (c >= ARRAY_SIZE(codes) - 1) {
  458. dev_warn(&rcdev->dev, "IR too long, cannot transmit\n");
  459. return -EINVAL;
  460. }
  461. /*
  462. * Lengths more than 142220us cannot be encoded; also
  463. * this checks for multiply overflow
  464. */
  465. if (txbuf[i] > 142220)
  466. return -EINVAL;
  467. l = DIV_ROUND_CLOSEST((XTAL_FREQ / 1000) * txbuf[i], 40000);
  468. if (i & 1) {
  469. s = find_slot(code_block->space,
  470. ARRAY_SIZE(code_block->space), l);
  471. if (s == -1) {
  472. dev_warn(&rcdev->dev, "Too many different lengths spaces, cannot transmit");
  473. return -EINVAL;
  474. }
  475. /* We have a pulse and space */
  476. codes[c++] = (p << 4) | s;
  477. } else {
  478. p = find_slot(code_block->pulse,
  479. ARRAY_SIZE(code_block->pulse), l);
  480. if (p == -1) {
  481. dev_warn(&rcdev->dev, "Too many different lengths pulses, cannot transmit");
  482. return -EINVAL;
  483. }
  484. }
  485. }
  486. /* We have to encode the trailing pulse. Find the shortest space */
  487. s = 0;
  488. for (i = 1; i < ARRAY_SIZE(code_block->space); i++) {
  489. u16 d = get_unaligned_be16(&code_block->space[i]);
  490. if (get_unaligned_be16(&code_block->space[s]) > d)
  491. s = i;
  492. }
  493. codes[c++] = (p << 4) | s;
  494. dev_dbg(&rcdev->dev, "generated %d codes\n", c);
  495. /*
  496. * Are the last N codes (so pulse + space) repeating 3 times?
  497. * if so we can shorten the codes list and use code 0xc0 to repeat
  498. * them.
  499. */
  500. repeating = false;
  501. for (rep = c / 3; rep >= 1; rep--) {
  502. if (!memcmp(&codes[c - rep * 3], &codes[c - rep * 2], rep) &&
  503. !cmp_no_trail(&codes[c - rep], &codes[c - rep * 2], rep)) {
  504. repeating = true;
  505. break;
  506. }
  507. }
  508. if (repeating) {
  509. /* first copy any leading non-repeating */
  510. int leading = c - rep * 3;
  511. if (leading >= ARRAY_SIZE(code_block->codes) - 3 - rep) {
  512. dev_warn(&rcdev->dev, "IR too long, cannot transmit\n");
  513. return -EINVAL;
  514. }
  515. dev_dbg(&rcdev->dev, "found trailing %d repeat\n", rep);
  516. copy_codes(code_block->codes, codes, leading);
  517. code_block->codes[leading] = 0x82;
  518. copy_codes(code_block->codes + leading + 1, codes + leading,
  519. rep);
  520. c = leading + 1 + rep;
  521. code_block->codes[c++] = 0xc0;
  522. } else {
  523. if (c >= ARRAY_SIZE(code_block->codes) - 3) {
  524. dev_warn(&rcdev->dev, "IR too long, cannot transmit\n");
  525. return -EINVAL;
  526. }
  527. dev_dbg(&rcdev->dev, "found no trailing repeat\n");
  528. code_block->codes[0] = 0x82;
  529. copy_codes(code_block->codes + 1, codes, c);
  530. c++;
  531. code_block->codes[c++] = 0xc4;
  532. }
  533. while (c < ARRAY_SIZE(code_block->codes))
  534. code_block->codes[c++] = 0x83;
  535. return 0;
  536. }
  537. static int zilog_tx(struct rc_dev *rcdev, unsigned int *txbuf,
  538. unsigned int count)
  539. {
  540. struct IR_i2c *ir = rcdev->priv;
  541. struct code_block code_block = { .length = sizeof(code_block) };
  542. u8 buf[2];
  543. int ret, i;
  544. ret = zilog_ir_format(rcdev, txbuf, count, &code_block);
  545. if (ret)
  546. return ret;
  547. ret = mutex_lock_interruptible(&ir->lock);
  548. if (ret)
  549. return ret;
  550. ret = send_data_block(ir, ZILOG_UIR_END, &code_block);
  551. if (ret)
  552. goto out_unlock;
  553. ret = i2c_master_recv(ir->tx_c, buf, 1);
  554. if (ret != 1) {
  555. dev_err(&ir->rc->dev, "i2c_master_recv failed with %d\n", ret);
  556. goto out_unlock;
  557. }
  558. dev_dbg(&ir->rc->dev, "code set status: %02x\n", buf[0]);
  559. if (buf[0] != (ZILOG_STATUS_OK | ZILOG_STATUS_SET)) {
  560. dev_err(&ir->rc->dev, "unexpected IR TX response %02x\n",
  561. buf[0]);
  562. ret = -EIO;
  563. goto out_unlock;
  564. }
  565. buf[0] = 0x00;
  566. buf[1] = ZILOG_SEND;
  567. ret = i2c_master_send(ir->tx_c, buf, 2);
  568. if (ret != 2) {
  569. dev_err(&ir->rc->dev, "i2c_master_send failed with %d\n", ret);
  570. if (ret >= 0)
  571. ret = -EIO;
  572. goto out_unlock;
  573. }
  574. dev_dbg(&ir->rc->dev, "send command sent\n");
  575. /*
  576. * This bit NAKs until the device is ready, so we retry it
  577. * sleeping a bit each time. This seems to be what the windows
  578. * driver does, approximately.
  579. * Try for up to 1s.
  580. */
  581. for (i = 0; i < 20; ++i) {
  582. set_current_state(TASK_UNINTERRUPTIBLE);
  583. schedule_timeout(msecs_to_jiffies(50));
  584. ret = i2c_master_send(ir->tx_c, buf, 1);
  585. if (ret == 1)
  586. break;
  587. dev_dbg(&ir->rc->dev,
  588. "NAK expected: i2c_master_send failed with %d (try %d)\n",
  589. ret, i + 1);
  590. }
  591. if (ret != 1) {
  592. dev_err(&ir->rc->dev,
  593. "IR TX chip never got ready: last i2c_master_send failed with %d\n",
  594. ret);
  595. if (ret >= 0)
  596. ret = -EIO;
  597. goto out_unlock;
  598. }
  599. ret = i2c_master_recv(ir->tx_c, buf, 1);
  600. if (ret != 1) {
  601. dev_err(&ir->rc->dev, "i2c_master_recv failed with %d\n", ret);
  602. ret = -EIO;
  603. goto out_unlock;
  604. } else if (buf[0] != ZILOG_STATUS_OK) {
  605. dev_err(&ir->rc->dev, "unexpected IR TX response #2: %02x\n",
  606. buf[0]);
  607. ret = -EIO;
  608. goto out_unlock;
  609. }
  610. dev_dbg(&ir->rc->dev, "transmit complete\n");
  611. /* Oh good, it worked */
  612. ret = count;
  613. out_unlock:
  614. mutex_unlock(&ir->lock);
  615. return ret;
  616. }
  617. static int zilog_tx_carrier(struct rc_dev *dev, u32 carrier)
  618. {
  619. struct IR_i2c *ir = dev->priv;
  620. if (carrier > 500000 || carrier < 20000)
  621. return -EINVAL;
  622. ir->carrier = carrier;
  623. return 0;
  624. }
  625. static int zilog_tx_duty_cycle(struct rc_dev *dev, u32 duty_cycle)
  626. {
  627. struct IR_i2c *ir = dev->priv;
  628. ir->duty_cycle = duty_cycle;
  629. return 0;
  630. }
  631. static int ir_probe(struct i2c_client *client)
  632. {
  633. const struct i2c_device_id *id = i2c_client_get_device_id(client);
  634. char *ir_codes = NULL;
  635. const char *name = NULL;
  636. u64 rc_proto = RC_PROTO_BIT_UNKNOWN;
  637. struct IR_i2c *ir;
  638. struct rc_dev *rc = NULL;
  639. struct i2c_adapter *adap = client->adapter;
  640. unsigned short addr = client->addr;
  641. bool probe_tx = (id->driver_data & FLAG_TX) != 0;
  642. int err;
  643. if ((id->driver_data & FLAG_HDPVR) && !enable_hdpvr) {
  644. dev_err(&client->dev, "IR for HDPVR is known to cause problems during recording, use enable_hdpvr modparam to enable\n");
  645. return -ENODEV;
  646. }
  647. ir = devm_kzalloc(&client->dev, sizeof(*ir), GFP_KERNEL);
  648. if (!ir)
  649. return -ENOMEM;
  650. ir->c = client;
  651. ir->polling_interval = DEFAULT_POLLING_INTERVAL;
  652. i2c_set_clientdata(client, ir);
  653. switch(addr) {
  654. case 0x64:
  655. name = "Pixelview";
  656. ir->get_key = get_key_pixelview;
  657. rc_proto = RC_PROTO_BIT_OTHER;
  658. ir_codes = RC_MAP_EMPTY;
  659. break;
  660. case 0x18:
  661. case 0x1f:
  662. case 0x1a:
  663. name = "Hauppauge";
  664. ir->get_key = get_key_haup;
  665. rc_proto = RC_PROTO_BIT_RC5;
  666. ir_codes = RC_MAP_HAUPPAUGE;
  667. break;
  668. case 0x30:
  669. name = "KNC One";
  670. ir->get_key = get_key_knc1;
  671. rc_proto = RC_PROTO_BIT_OTHER;
  672. ir_codes = RC_MAP_EMPTY;
  673. break;
  674. case 0x33:
  675. name = "Geniatech";
  676. ir->get_key = get_key_geniatech;
  677. rc_proto = RC_PROTO_BIT_RC5;
  678. ir_codes = RC_MAP_TOTAL_MEDIA_IN_HAND_02;
  679. ir->old = 0xfc;
  680. break;
  681. case 0x6b:
  682. name = "FusionHDTV";
  683. ir->get_key = get_key_fusionhdtv;
  684. rc_proto = RC_PROTO_BIT_UNKNOWN;
  685. ir_codes = RC_MAP_FUSIONHDTV_MCE;
  686. break;
  687. case 0x40:
  688. name = "AVerMedia Cardbus remote";
  689. ir->get_key = get_key_avermedia_cardbus;
  690. rc_proto = RC_PROTO_BIT_OTHER;
  691. ir_codes = RC_MAP_AVERMEDIA_CARDBUS;
  692. break;
  693. case 0x41:
  694. name = "AVerMedia EM78P153";
  695. ir->get_key = get_key_avermedia_cardbus;
  696. rc_proto = RC_PROTO_BIT_OTHER;
  697. /* RM-KV remote, seems to be same as RM-K6 */
  698. ir_codes = RC_MAP_AVERMEDIA_M733A_RM_K6;
  699. break;
  700. case 0x71:
  701. name = "Hauppauge/Zilog Z8";
  702. ir->get_key = get_key_haup_xvr;
  703. rc_proto = RC_PROTO_BIT_RC5 | RC_PROTO_BIT_RC6_MCE |
  704. RC_PROTO_BIT_RC6_6A_32;
  705. ir_codes = RC_MAP_HAUPPAUGE;
  706. ir->polling_interval = 125;
  707. probe_tx = true;
  708. break;
  709. }
  710. /* Let the caller override settings */
  711. if (client->dev.platform_data) {
  712. const struct IR_i2c_init_data *init_data =
  713. client->dev.platform_data;
  714. ir_codes = init_data->ir_codes;
  715. rc = init_data->rc_dev;
  716. name = init_data->name;
  717. if (init_data->type)
  718. rc_proto = init_data->type;
  719. if (init_data->polling_interval)
  720. ir->polling_interval = init_data->polling_interval;
  721. switch (init_data->internal_get_key_func) {
  722. case IR_KBD_GET_KEY_CUSTOM:
  723. /* The bridge driver provided us its own function */
  724. ir->get_key = init_data->get_key;
  725. break;
  726. case IR_KBD_GET_KEY_PIXELVIEW:
  727. ir->get_key = get_key_pixelview;
  728. break;
  729. case IR_KBD_GET_KEY_HAUP:
  730. ir->get_key = get_key_haup;
  731. break;
  732. case IR_KBD_GET_KEY_KNC1:
  733. ir->get_key = get_key_knc1;
  734. break;
  735. case IR_KBD_GET_KEY_GENIATECH:
  736. ir->get_key = get_key_geniatech;
  737. break;
  738. case IR_KBD_GET_KEY_FUSIONHDTV:
  739. ir->get_key = get_key_fusionhdtv;
  740. break;
  741. case IR_KBD_GET_KEY_HAUP_XVR:
  742. ir->get_key = get_key_haup_xvr;
  743. break;
  744. case IR_KBD_GET_KEY_AVERMEDIA_CARDBUS:
  745. ir->get_key = get_key_avermedia_cardbus;
  746. break;
  747. }
  748. }
  749. if (!rc) {
  750. /*
  751. * If platform_data doesn't specify rc_dev, initialize it
  752. * internally
  753. */
  754. rc = rc_allocate_device(RC_DRIVER_SCANCODE);
  755. if (!rc)
  756. return -ENOMEM;
  757. }
  758. ir->rc = rc;
  759. /* Make sure we are all setup before going on */
  760. if (!name || !ir->get_key || !rc_proto || !ir_codes) {
  761. dev_warn(&client->dev, "Unsupported device at address 0x%02x\n",
  762. addr);
  763. err = -ENODEV;
  764. goto err_out_free;
  765. }
  766. ir->ir_codes = ir_codes;
  767. snprintf(ir->phys, sizeof(ir->phys), "%s/%s", dev_name(&adap->dev),
  768. dev_name(&client->dev));
  769. /*
  770. * Initialize input_dev fields
  771. * It doesn't make sense to allow overriding them via platform_data
  772. */
  773. rc->input_id.bustype = BUS_I2C;
  774. rc->input_phys = ir->phys;
  775. rc->device_name = name;
  776. rc->dev.parent = &client->dev;
  777. rc->priv = ir;
  778. rc->open = ir_open;
  779. rc->close = ir_close;
  780. /*
  781. * Initialize the other fields of rc_dev
  782. */
  783. rc->map_name = ir->ir_codes;
  784. rc->allowed_protocols = rc_proto;
  785. if (!rc->driver_name)
  786. rc->driver_name = KBUILD_MODNAME;
  787. mutex_init(&ir->lock);
  788. INIT_DELAYED_WORK(&ir->work, ir_work);
  789. if (probe_tx) {
  790. ir->tx_c = i2c_new_dummy_device(client->adapter, 0x70);
  791. if (IS_ERR(ir->tx_c)) {
  792. dev_err(&client->dev, "failed to setup tx i2c address");
  793. err = PTR_ERR(ir->tx_c);
  794. goto err_out_free;
  795. } else if (!zilog_init(ir)) {
  796. ir->carrier = 38000;
  797. ir->duty_cycle = 40;
  798. rc->tx_ir = zilog_tx;
  799. rc->s_tx_carrier = zilog_tx_carrier;
  800. rc->s_tx_duty_cycle = zilog_tx_duty_cycle;
  801. }
  802. }
  803. err = rc_register_device(rc);
  804. if (err)
  805. goto err_out_free;
  806. return 0;
  807. err_out_free:
  808. if (!IS_ERR(ir->tx_c))
  809. i2c_unregister_device(ir->tx_c);
  810. /* Only frees rc if it were allocated internally */
  811. rc_free_device(rc);
  812. return err;
  813. }
  814. static void ir_remove(struct i2c_client *client)
  815. {
  816. struct IR_i2c *ir = i2c_get_clientdata(client);
  817. cancel_delayed_work_sync(&ir->work);
  818. i2c_unregister_device(ir->tx_c);
  819. rc_unregister_device(ir->rc);
  820. }
  821. static const struct i2c_device_id ir_kbd_id[] = {
  822. /* Generic entry for any IR receiver */
  823. { "ir_video", 0 },
  824. /* IR device specific entries should be added here */
  825. { "ir_z8f0811_haup", FLAG_TX },
  826. { "ir_z8f0811_hdpvr", FLAG_TX | FLAG_HDPVR },
  827. { }
  828. };
  829. MODULE_DEVICE_TABLE(i2c, ir_kbd_id);
  830. static struct i2c_driver ir_kbd_driver = {
  831. .driver = {
  832. .name = "ir-kbd-i2c",
  833. },
  834. .probe = ir_probe,
  835. .remove = ir_remove,
  836. .id_table = ir_kbd_id,
  837. };
  838. module_i2c_driver(ir_kbd_driver);
  839. /* ----------------------------------------------------------------------- */
  840. MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, Ulrich Mueller");
  841. MODULE_DESCRIPTION("input driver for i2c IR remote controls");
  842. MODULE_LICENSE("GPL");