ir-kbd-i2c.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. *
  22. * This program is free software; you can redistribute it and/or modify
  23. * it under the terms of the GNU General Public License as published by
  24. * the Free Software Foundation; either version 2 of the License, or
  25. * (at your option) any later version.
  26. *
  27. * This program is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. * GNU General Public License for more details.
  31. *
  32. */
  33. #include <asm/unaligned.h>
  34. #include <linux/module.h>
  35. #include <linux/init.h>
  36. #include <linux/kernel.h>
  37. #include <linux/string.h>
  38. #include <linux/timer.h>
  39. #include <linux/delay.h>
  40. #include <linux/errno.h>
  41. #include <linux/slab.h>
  42. #include <linux/i2c.h>
  43. #include <linux/workqueue.h>
  44. #include <media/rc-core.h>
  45. #include <media/i2c/ir-kbd-i2c.h>
  46. /* ----------------------------------------------------------------------- */
  47. /* insmod parameters */
  48. static int debug;
  49. module_param(debug, int, 0644); /* debug level (0,1,2) */
  50. #define MODULE_NAME "ir-kbd-i2c"
  51. #define dprintk(level, fmt, arg...) if (debug >= level) \
  52. printk(KERN_DEBUG MODULE_NAME ": " fmt , ## arg)
  53. /* ----------------------------------------------------------------------- */
  54. static int get_key_haup_common(struct IR_i2c *ir, enum rc_type *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. dprintk(1, "ir hauppauge (rc5): s%d r%d t%d dev=%d code=%d\n",
  85. start, range, toggle, dev, code);
  86. *protocol = RC_TYPE_RC5;
  87. *scancode = RC_SCANCODE_RC5(dev, code);
  88. *ptoggle = toggle;
  89. return 1;
  90. } else if (size == 6 && (buf[0] & 0x40)) {
  91. code = buf[4];
  92. dev = buf[3];
  93. vendor = get_unaligned_be16(buf + 1);
  94. if (vendor == 0x800f) {
  95. *ptoggle = (dev & 0x80) != 0;
  96. *protocol = RC_TYPE_RC6_MCE;
  97. dev &= 0x7f;
  98. dprintk(1, "ir hauppauge (rc6-mce): t%d vendor=%d dev=%d code=%d\n",
  99. *ptoggle, vendor, dev, code);
  100. } else {
  101. *ptoggle = 0;
  102. *protocol = RC_TYPE_RC6_6A_32;
  103. dprintk(1, "ir hauppauge (rc6-6a-32): vendor=%d dev=%d code=%d\n",
  104. vendor, dev, code);
  105. }
  106. *scancode = RC_SCANCODE_RC6_6A(vendor, dev, code);
  107. return 1;
  108. }
  109. return 0;
  110. }
  111. static int get_key_haup(struct IR_i2c *ir, enum rc_type *protocol,
  112. u32 *scancode, u8 *toggle)
  113. {
  114. return get_key_haup_common(ir, protocol, scancode, toggle, 3);
  115. }
  116. static int get_key_haup_xvr(struct IR_i2c *ir, enum rc_type *protocol,
  117. u32 *scancode, u8 *toggle)
  118. {
  119. int ret;
  120. unsigned char buf[1] = { 0 };
  121. /*
  122. * This is the same apparent "are you ready?" poll command observed
  123. * watching Windows driver traffic and implemented in lirc_zilog. With
  124. * this added, we get far saner remote behavior with z8 chips on usb
  125. * connected devices, even with the default polling interval of 100ms.
  126. */
  127. ret = i2c_master_send(ir->c, buf, 1);
  128. if (ret != 1)
  129. return (ret < 0) ? ret : -EINVAL;
  130. return get_key_haup_common(ir, protocol, scancode, toggle, 6);
  131. }
  132. static int get_key_pixelview(struct IR_i2c *ir, enum rc_type *protocol,
  133. u32 *scancode, u8 *toggle)
  134. {
  135. unsigned char b;
  136. /* poll IR chip */
  137. if (1 != i2c_master_recv(ir->c, &b, 1)) {
  138. dprintk(1,"read error\n");
  139. return -EIO;
  140. }
  141. *protocol = RC_TYPE_OTHER;
  142. *scancode = b;
  143. *toggle = 0;
  144. return 1;
  145. }
  146. static int get_key_fusionhdtv(struct IR_i2c *ir, enum rc_type *protocol,
  147. u32 *scancode, u8 *toggle)
  148. {
  149. unsigned char buf[4];
  150. /* poll IR chip */
  151. if (4 != i2c_master_recv(ir->c, buf, 4)) {
  152. dprintk(1,"read error\n");
  153. return -EIO;
  154. }
  155. if(buf[0] !=0 || buf[1] !=0 || buf[2] !=0 || buf[3] != 0)
  156. dprintk(2, "%s: 0x%2x 0x%2x 0x%2x 0x%2x\n", __func__,
  157. buf[0], buf[1], buf[2], buf[3]);
  158. /* no key pressed or signal from other ir remote */
  159. if(buf[0] != 0x1 || buf[1] != 0xfe)
  160. return 0;
  161. *protocol = RC_TYPE_UNKNOWN;
  162. *scancode = buf[2];
  163. *toggle = 0;
  164. return 1;
  165. }
  166. static int get_key_knc1(struct IR_i2c *ir, enum rc_type *protocol,
  167. u32 *scancode, u8 *toggle)
  168. {
  169. unsigned char b;
  170. /* poll IR chip */
  171. if (1 != i2c_master_recv(ir->c, &b, 1)) {
  172. dprintk(1,"read error\n");
  173. return -EIO;
  174. }
  175. /* it seems that 0xFE indicates that a button is still hold
  176. down, while 0xff indicates that no button is hold
  177. down. 0xfe sequences are sometimes interrupted by 0xFF */
  178. dprintk(2,"key %02x\n", b);
  179. if (b == 0xff)
  180. return 0;
  181. if (b == 0xfe)
  182. /* keep old data */
  183. return 1;
  184. *protocol = RC_TYPE_UNKNOWN;
  185. *scancode = b;
  186. *toggle = 0;
  187. return 1;
  188. }
  189. static int get_key_avermedia_cardbus(struct IR_i2c *ir, enum rc_type *protocol,
  190. u32 *scancode, u8 *toggle)
  191. {
  192. unsigned char subaddr, key, keygroup;
  193. struct i2c_msg msg[] = { { .addr = ir->c->addr, .flags = 0,
  194. .buf = &subaddr, .len = 1},
  195. { .addr = ir->c->addr, .flags = I2C_M_RD,
  196. .buf = &key, .len = 1} };
  197. subaddr = 0x0d;
  198. if (2 != i2c_transfer(ir->c->adapter, msg, 2)) {
  199. dprintk(1, "read error\n");
  200. return -EIO;
  201. }
  202. if (key == 0xff)
  203. return 0;
  204. subaddr = 0x0b;
  205. msg[1].buf = &keygroup;
  206. if (2 != i2c_transfer(ir->c->adapter, msg, 2)) {
  207. dprintk(1, "read error\n");
  208. return -EIO;
  209. }
  210. if (keygroup == 0xff)
  211. return 0;
  212. dprintk(1, "read key 0x%02x/0x%02x\n", key, keygroup);
  213. if (keygroup < 2 || keygroup > 4) {
  214. /* Only a warning */
  215. dprintk(1, "warning: invalid key group 0x%02x for key 0x%02x\n",
  216. keygroup, key);
  217. }
  218. key |= (keygroup & 1) << 6;
  219. *protocol = RC_TYPE_UNKNOWN;
  220. *scancode = key;
  221. if (ir->c->addr == 0x41) /* AVerMedia EM78P153 */
  222. *scancode |= keygroup << 8;
  223. *toggle = 0;
  224. return 1;
  225. }
  226. /* ----------------------------------------------------------------------- */
  227. static int ir_key_poll(struct IR_i2c *ir)
  228. {
  229. enum rc_type protocol;
  230. u32 scancode;
  231. u8 toggle;
  232. int rc;
  233. dprintk(3, "%s\n", __func__);
  234. rc = ir->get_key(ir, &protocol, &scancode, &toggle);
  235. if (rc < 0) {
  236. dprintk(2,"error\n");
  237. return rc;
  238. }
  239. if (rc) {
  240. dprintk(1, "%s: proto = 0x%04x, scancode = 0x%08x\n",
  241. __func__, protocol, scancode);
  242. rc_keydown(ir->rc, protocol, scancode, toggle);
  243. }
  244. return 0;
  245. }
  246. static void ir_work(struct work_struct *work)
  247. {
  248. int rc;
  249. struct IR_i2c *ir = container_of(work, struct IR_i2c, work.work);
  250. rc = ir_key_poll(ir);
  251. if (rc == -ENODEV) {
  252. rc_unregister_device(ir->rc);
  253. ir->rc = NULL;
  254. return;
  255. }
  256. schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling_interval));
  257. }
  258. /* ----------------------------------------------------------------------- */
  259. static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)
  260. {
  261. char *ir_codes = NULL;
  262. const char *name = NULL;
  263. u64 rc_type = RC_BIT_UNKNOWN;
  264. struct IR_i2c *ir;
  265. struct rc_dev *rc = NULL;
  266. struct i2c_adapter *adap = client->adapter;
  267. unsigned short addr = client->addr;
  268. int err;
  269. ir = devm_kzalloc(&client->dev, sizeof(*ir), GFP_KERNEL);
  270. if (!ir)
  271. return -ENOMEM;
  272. ir->c = client;
  273. ir->polling_interval = DEFAULT_POLLING_INTERVAL;
  274. i2c_set_clientdata(client, ir);
  275. switch(addr) {
  276. case 0x64:
  277. name = "Pixelview";
  278. ir->get_key = get_key_pixelview;
  279. rc_type = RC_BIT_OTHER;
  280. ir_codes = RC_MAP_EMPTY;
  281. break;
  282. case 0x18:
  283. case 0x1f:
  284. case 0x1a:
  285. name = "Hauppauge";
  286. ir->get_key = get_key_haup;
  287. rc_type = RC_BIT_RC5;
  288. ir_codes = RC_MAP_HAUPPAUGE;
  289. break;
  290. case 0x30:
  291. name = "KNC One";
  292. ir->get_key = get_key_knc1;
  293. rc_type = RC_BIT_OTHER;
  294. ir_codes = RC_MAP_EMPTY;
  295. break;
  296. case 0x6b:
  297. name = "FusionHDTV";
  298. ir->get_key = get_key_fusionhdtv;
  299. rc_type = RC_BIT_UNKNOWN;
  300. ir_codes = RC_MAP_FUSIONHDTV_MCE;
  301. break;
  302. case 0x40:
  303. name = "AVerMedia Cardbus remote";
  304. ir->get_key = get_key_avermedia_cardbus;
  305. rc_type = RC_BIT_OTHER;
  306. ir_codes = RC_MAP_AVERMEDIA_CARDBUS;
  307. break;
  308. case 0x41:
  309. name = "AVerMedia EM78P153";
  310. ir->get_key = get_key_avermedia_cardbus;
  311. rc_type = RC_BIT_OTHER;
  312. /* RM-KV remote, seems to be same as RM-K6 */
  313. ir_codes = RC_MAP_AVERMEDIA_M733A_RM_K6;
  314. break;
  315. case 0x71:
  316. name = "Hauppauge/Zilog Z8";
  317. ir->get_key = get_key_haup_xvr;
  318. rc_type = RC_BIT_RC5 | RC_BIT_RC6_MCE | RC_BIT_RC6_6A_32;
  319. ir_codes = RC_MAP_HAUPPAUGE;
  320. break;
  321. }
  322. /* Let the caller override settings */
  323. if (client->dev.platform_data) {
  324. const struct IR_i2c_init_data *init_data =
  325. client->dev.platform_data;
  326. ir_codes = init_data->ir_codes;
  327. rc = init_data->rc_dev;
  328. name = init_data->name;
  329. if (init_data->type)
  330. rc_type = init_data->type;
  331. if (init_data->polling_interval)
  332. ir->polling_interval = init_data->polling_interval;
  333. switch (init_data->internal_get_key_func) {
  334. case IR_KBD_GET_KEY_CUSTOM:
  335. /* The bridge driver provided us its own function */
  336. ir->get_key = init_data->get_key;
  337. break;
  338. case IR_KBD_GET_KEY_PIXELVIEW:
  339. ir->get_key = get_key_pixelview;
  340. break;
  341. case IR_KBD_GET_KEY_HAUP:
  342. ir->get_key = get_key_haup;
  343. break;
  344. case IR_KBD_GET_KEY_KNC1:
  345. ir->get_key = get_key_knc1;
  346. break;
  347. case IR_KBD_GET_KEY_FUSIONHDTV:
  348. ir->get_key = get_key_fusionhdtv;
  349. break;
  350. case IR_KBD_GET_KEY_HAUP_XVR:
  351. ir->get_key = get_key_haup_xvr;
  352. break;
  353. case IR_KBD_GET_KEY_AVERMEDIA_CARDBUS:
  354. ir->get_key = get_key_avermedia_cardbus;
  355. break;
  356. }
  357. }
  358. if (!rc) {
  359. /*
  360. * If platform_data doesn't specify rc_dev, initialize it
  361. * internally
  362. */
  363. rc = rc_allocate_device(RC_DRIVER_SCANCODE);
  364. if (!rc)
  365. return -ENOMEM;
  366. }
  367. ir->rc = rc;
  368. /* Make sure we are all setup before going on */
  369. if (!name || !ir->get_key || !rc_type || !ir_codes) {
  370. dprintk(1, ": Unsupported device at address 0x%02x\n",
  371. addr);
  372. err = -ENODEV;
  373. goto err_out_free;
  374. }
  375. /* Sets name */
  376. snprintf(ir->name, sizeof(ir->name), "i2c IR (%s)", name);
  377. ir->ir_codes = ir_codes;
  378. snprintf(ir->phys, sizeof(ir->phys), "%s/%s/ir0",
  379. dev_name(&adap->dev),
  380. dev_name(&client->dev));
  381. /*
  382. * Initialize input_dev fields
  383. * It doesn't make sense to allow overriding them via platform_data
  384. */
  385. rc->input_id.bustype = BUS_I2C;
  386. rc->input_phys = ir->phys;
  387. rc->input_name = ir->name;
  388. /*
  389. * Initialize the other fields of rc_dev
  390. */
  391. rc->map_name = ir->ir_codes;
  392. rc->allowed_protocols = rc_type;
  393. rc->enabled_protocols = rc_type;
  394. if (!rc->driver_name)
  395. rc->driver_name = MODULE_NAME;
  396. err = rc_register_device(rc);
  397. if (err)
  398. goto err_out_free;
  399. printk(MODULE_NAME ": %s detected at %s [%s]\n",
  400. ir->name, ir->phys, adap->name);
  401. /* start polling via eventd */
  402. INIT_DELAYED_WORK(&ir->work, ir_work);
  403. schedule_delayed_work(&ir->work, 0);
  404. return 0;
  405. err_out_free:
  406. /* Only frees rc if it were allocated internally */
  407. rc_free_device(rc);
  408. return err;
  409. }
  410. static int ir_remove(struct i2c_client *client)
  411. {
  412. struct IR_i2c *ir = i2c_get_clientdata(client);
  413. /* kill outstanding polls */
  414. cancel_delayed_work_sync(&ir->work);
  415. /* unregister device */
  416. rc_unregister_device(ir->rc);
  417. /* free memory */
  418. return 0;
  419. }
  420. static const struct i2c_device_id ir_kbd_id[] = {
  421. /* Generic entry for any IR receiver */
  422. { "ir_video", 0 },
  423. /* IR device specific entries should be added here */
  424. { "ir_rx_z8f0811_haup", 0 },
  425. { "ir_rx_z8f0811_hdpvr", 0 },
  426. { }
  427. };
  428. static struct i2c_driver ir_kbd_driver = {
  429. .driver = {
  430. .name = "ir-kbd-i2c",
  431. },
  432. .probe = ir_probe,
  433. .remove = ir_remove,
  434. .id_table = ir_kbd_id,
  435. };
  436. module_i2c_driver(ir_kbd_driver);
  437. /* ----------------------------------------------------------------------- */
  438. MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, Ulrich Mueller");
  439. MODULE_DESCRIPTION("input driver for i2c IR remote controls");
  440. MODULE_LICENSE("GPL");