ir-kbd-i2c.c 23 KB

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