ir-kbd-i2c.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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 <[email protected]>
  7. * modified for PixelView (BT878P+W/FM) by
  8. * Michal Kochanowicz <[email protected]>
  9. * Christoph Bartelmus <[email protected]>
  10. * modified for KNC ONE TV Station/Anubis Typhoon TView Tuner by
  11. * Ulrich Mueller <[email protected]>
  12. * modified for em2820 based USB TV tuners by
  13. * Markus Rechberger <[email protected]>
  14. * modified for DViCO Fusion HDTV 5 RT GOLD by
  15. * Chaogui Zhang <[email protected]>
  16. * modified for MSI TV@nywhere Plus by
  17. * Henry Wong <[email protected]>
  18. * Mark Schultz <[email protected]>
  19. * Brian Rogers <[email protected]>
  20. * modified for AVerMedia Cardbus by
  21. * Oldrich Jedlicka <[email protected]>
  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 <[email protected]>
  27. * Michal Kochanowicz <[email protected]>
  28. * Christoph Bartelmus <[email protected]>
  29. * Ulrich Mueller <[email protected]>
  30. * Stefan Jahn <[email protected]>
  31. * Jerome Brock <[email protected]>
  32. * Thomas Reitmayr ([email protected])
  33. * Mark Weaver <[email protected]>
  34. * Jarod Wilson <[email protected]>
  35. * Copyright (C) 2011 Andy Walls <[email protected]>
  36. */
  37. #include <asm/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;
  274. u32 scancode;
  275. u8 toggle;
  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, const struct i2c_device_id *id)
  632. {
  633. char *ir_codes = NULL;
  634. const char *name = NULL;
  635. u64 rc_proto = RC_PROTO_BIT_UNKNOWN;
  636. struct IR_i2c *ir;
  637. struct rc_dev *rc = NULL;
  638. struct i2c_adapter *adap = client->adapter;
  639. unsigned short addr = client->addr;
  640. bool probe_tx = (id->driver_data & FLAG_TX) != 0;
  641. int err;
  642. if ((id->driver_data & FLAG_HDPVR) && !enable_hdpvr) {
  643. dev_err(&client->dev, "IR for HDPVR is known to cause problems during recording, use enable_hdpvr modparam to enable\n");
  644. return -ENODEV;
  645. }
  646. ir = devm_kzalloc(&client->dev, sizeof(*ir), GFP_KERNEL);
  647. if (!ir)
  648. return -ENOMEM;
  649. ir->c = client;
  650. ir->polling_interval = DEFAULT_POLLING_INTERVAL;
  651. i2c_set_clientdata(client, ir);
  652. switch(addr) {
  653. case 0x64:
  654. name = "Pixelview";
  655. ir->get_key = get_key_pixelview;
  656. rc_proto = RC_PROTO_BIT_OTHER;
  657. ir_codes = RC_MAP_EMPTY;
  658. break;
  659. case 0x18:
  660. case 0x1f:
  661. case 0x1a:
  662. name = "Hauppauge";
  663. ir->get_key = get_key_haup;
  664. rc_proto = RC_PROTO_BIT_RC5;
  665. ir_codes = RC_MAP_HAUPPAUGE;
  666. break;
  667. case 0x30:
  668. name = "KNC One";
  669. ir->get_key = get_key_knc1;
  670. rc_proto = RC_PROTO_BIT_OTHER;
  671. ir_codes = RC_MAP_EMPTY;
  672. break;
  673. case 0x33:
  674. name = "Geniatech";
  675. ir->get_key = get_key_geniatech;
  676. rc_proto = RC_PROTO_BIT_RC5;
  677. ir_codes = RC_MAP_TOTAL_MEDIA_IN_HAND_02;
  678. ir->old = 0xfc;
  679. break;
  680. case 0x6b:
  681. name = "FusionHDTV";
  682. ir->get_key = get_key_fusionhdtv;
  683. rc_proto = RC_PROTO_BIT_UNKNOWN;
  684. ir_codes = RC_MAP_FUSIONHDTV_MCE;
  685. break;
  686. case 0x40:
  687. name = "AVerMedia Cardbus remote";
  688. ir->get_key = get_key_avermedia_cardbus;
  689. rc_proto = RC_PROTO_BIT_OTHER;
  690. ir_codes = RC_MAP_AVERMEDIA_CARDBUS;
  691. break;
  692. case 0x41:
  693. name = "AVerMedia EM78P153";
  694. ir->get_key = get_key_avermedia_cardbus;
  695. rc_proto = RC_PROTO_BIT_OTHER;
  696. /* RM-KV remote, seems to be same as RM-K6 */
  697. ir_codes = RC_MAP_AVERMEDIA_M733A_RM_K6;
  698. break;
  699. case 0x71:
  700. name = "Hauppauge/Zilog Z8";
  701. ir->get_key = get_key_haup_xvr;
  702. rc_proto = RC_PROTO_BIT_RC5 | RC_PROTO_BIT_RC6_MCE |
  703. RC_PROTO_BIT_RC6_6A_32;
  704. ir_codes = RC_MAP_HAUPPAUGE;
  705. ir->polling_interval = 125;
  706. probe_tx = true;
  707. break;
  708. }
  709. /* Let the caller override settings */
  710. if (client->dev.platform_data) {
  711. const struct IR_i2c_init_data *init_data =
  712. client->dev.platform_data;
  713. ir_codes = init_data->ir_codes;
  714. rc = init_data->rc_dev;
  715. name = init_data->name;
  716. if (init_data->type)
  717. rc_proto = init_data->type;
  718. if (init_data->polling_interval)
  719. ir->polling_interval = init_data->polling_interval;
  720. switch (init_data->internal_get_key_func) {
  721. case IR_KBD_GET_KEY_CUSTOM:
  722. /* The bridge driver provided us its own function */
  723. ir->get_key = init_data->get_key;
  724. break;
  725. case IR_KBD_GET_KEY_PIXELVIEW:
  726. ir->get_key = get_key_pixelview;
  727. break;
  728. case IR_KBD_GET_KEY_HAUP:
  729. ir->get_key = get_key_haup;
  730. break;
  731. case IR_KBD_GET_KEY_KNC1:
  732. ir->get_key = get_key_knc1;
  733. break;
  734. case IR_KBD_GET_KEY_GENIATECH:
  735. ir->get_key = get_key_geniatech;
  736. break;
  737. case IR_KBD_GET_KEY_FUSIONHDTV:
  738. ir->get_key = get_key_fusionhdtv;
  739. break;
  740. case IR_KBD_GET_KEY_HAUP_XVR:
  741. ir->get_key = get_key_haup_xvr;
  742. break;
  743. case IR_KBD_GET_KEY_AVERMEDIA_CARDBUS:
  744. ir->get_key = get_key_avermedia_cardbus;
  745. break;
  746. }
  747. }
  748. if (!rc) {
  749. /*
  750. * If platform_data doesn't specify rc_dev, initialize it
  751. * internally
  752. */
  753. rc = rc_allocate_device(RC_DRIVER_SCANCODE);
  754. if (!rc)
  755. return -ENOMEM;
  756. }
  757. ir->rc = rc;
  758. /* Make sure we are all setup before going on */
  759. if (!name || !ir->get_key || !rc_proto || !ir_codes) {
  760. dev_warn(&client->dev, "Unsupported device at address 0x%02x\n",
  761. addr);
  762. err = -ENODEV;
  763. goto err_out_free;
  764. }
  765. ir->ir_codes = ir_codes;
  766. snprintf(ir->phys, sizeof(ir->phys), "%s/%s", dev_name(&adap->dev),
  767. dev_name(&client->dev));
  768. /*
  769. * Initialize input_dev fields
  770. * It doesn't make sense to allow overriding them via platform_data
  771. */
  772. rc->input_id.bustype = BUS_I2C;
  773. rc->input_phys = ir->phys;
  774. rc->device_name = name;
  775. rc->dev.parent = &client->dev;
  776. rc->priv = ir;
  777. rc->open = ir_open;
  778. rc->close = ir_close;
  779. /*
  780. * Initialize the other fields of rc_dev
  781. */
  782. rc->map_name = ir->ir_codes;
  783. rc->allowed_protocols = rc_proto;
  784. if (!rc->driver_name)
  785. rc->driver_name = KBUILD_MODNAME;
  786. mutex_init(&ir->lock);
  787. INIT_DELAYED_WORK(&ir->work, ir_work);
  788. if (probe_tx) {
  789. ir->tx_c = i2c_new_dummy_device(client->adapter, 0x70);
  790. if (IS_ERR(ir->tx_c)) {
  791. dev_err(&client->dev, "failed to setup tx i2c address");
  792. err = PTR_ERR(ir->tx_c);
  793. goto err_out_free;
  794. } else if (!zilog_init(ir)) {
  795. ir->carrier = 38000;
  796. ir->duty_cycle = 40;
  797. rc->tx_ir = zilog_tx;
  798. rc->s_tx_carrier = zilog_tx_carrier;
  799. rc->s_tx_duty_cycle = zilog_tx_duty_cycle;
  800. }
  801. }
  802. err = rc_register_device(rc);
  803. if (err)
  804. goto err_out_free;
  805. return 0;
  806. err_out_free:
  807. if (!IS_ERR(ir->tx_c))
  808. i2c_unregister_device(ir->tx_c);
  809. /* Only frees rc if it were allocated internally */
  810. rc_free_device(rc);
  811. return err;
  812. }
  813. static void ir_remove(struct i2c_client *client)
  814. {
  815. struct IR_i2c *ir = i2c_get_clientdata(client);
  816. cancel_delayed_work_sync(&ir->work);
  817. i2c_unregister_device(ir->tx_c);
  818. rc_unregister_device(ir->rc);
  819. }
  820. static const struct i2c_device_id ir_kbd_id[] = {
  821. /* Generic entry for any IR receiver */
  822. { "ir_video", 0 },
  823. /* IR device specific entries should be added here */
  824. { "ir_z8f0811_haup", FLAG_TX },
  825. { "ir_z8f0811_hdpvr", FLAG_TX | FLAG_HDPVR },
  826. { }
  827. };
  828. MODULE_DEVICE_TABLE(i2c, ir_kbd_id);
  829. static struct i2c_driver ir_kbd_driver = {
  830. .driver = {
  831. .name = "ir-kbd-i2c",
  832. },
  833. .probe = ir_probe,
  834. .remove = ir_remove,
  835. .id_table = ir_kbd_id,
  836. };
  837. module_i2c_driver(ir_kbd_driver);
  838. /* ----------------------------------------------------------------------- */
  839. MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, Ulrich Mueller");
  840. MODULE_DESCRIPTION("input driver for i2c IR remote controls");
  841. MODULE_LICENSE("GPL");