ir-kbd-i2c.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. * You should have received a copy of the GNU General Public License
  33. * along with this program; if not, write to the Free Software
  34. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  35. *
  36. */
  37. #include <linux/module.h>
  38. #include <linux/init.h>
  39. #include <linux/kernel.h>
  40. #include <linux/string.h>
  41. #include <linux/timer.h>
  42. #include <linux/delay.h>
  43. #include <linux/errno.h>
  44. #include <linux/slab.h>
  45. #include <linux/i2c.h>
  46. #include <linux/workqueue.h>
  47. #include <media/rc-core.h>
  48. #include <media/ir-kbd-i2c.h>
  49. /* ----------------------------------------------------------------------- */
  50. /* insmod parameters */
  51. static int debug;
  52. module_param(debug, int, 0644); /* debug level (0,1,2) */
  53. static int hauppauge;
  54. module_param(hauppauge, int, 0644); /* Choose Hauppauge remote */
  55. MODULE_PARM_DESC(hauppauge, "Specify Hauppauge remote: 0=black, 1=grey (defaults to 0)");
  56. #define MODULE_NAME "ir-kbd-i2c"
  57. #define dprintk(level, fmt, arg...) if (debug >= level) \
  58. printk(KERN_DEBUG MODULE_NAME ": " fmt , ## arg)
  59. /* ----------------------------------------------------------------------- */
  60. static int get_key_haup_common(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw,
  61. int size, int offset)
  62. {
  63. unsigned char buf[6];
  64. int start, range, toggle, dev, code, ircode;
  65. /* poll IR chip */
  66. if (size != i2c_master_recv(ir->c, buf, size))
  67. return -EIO;
  68. /* split rc5 data block ... */
  69. start = (buf[offset] >> 7) & 1;
  70. range = (buf[offset] >> 6) & 1;
  71. toggle = (buf[offset] >> 5) & 1;
  72. dev = buf[offset] & 0x1f;
  73. code = (buf[offset+1] >> 2) & 0x3f;
  74. /* rc5 has two start bits
  75. * the first bit must be one
  76. * the second bit defines the command range (1 = 0-63, 0 = 64 - 127)
  77. */
  78. if (!start)
  79. /* no key pressed */
  80. return 0;
  81. /*
  82. * Hauppauge remotes (black/silver) always use
  83. * specific device ids. If we do not filter the
  84. * device ids then messages destined for devices
  85. * such as TVs (id=0) will get through causing
  86. * mis-fired events.
  87. *
  88. * We also filter out invalid key presses which
  89. * produce annoying debug log entries.
  90. */
  91. ircode= (start << 12) | (toggle << 11) | (dev << 6) | code;
  92. if ((ircode & 0x1fff)==0x1fff)
  93. /* invalid key press */
  94. return 0;
  95. if (dev!=0x1e && dev!=0x1f)
  96. /* not a hauppauge remote */
  97. return 0;
  98. if (!range)
  99. code += 64;
  100. dprintk(1,"ir hauppauge (rc5): s%d r%d t%d dev=%d code=%d\n",
  101. start, range, toggle, dev, code);
  102. /* return key */
  103. *ir_key = code;
  104. *ir_raw = ircode;
  105. return 1;
  106. }
  107. static int get_key_haup(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  108. {
  109. return get_key_haup_common (ir, ir_key, ir_raw, 3, 0);
  110. }
  111. static int get_key_haup_xvr(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  112. {
  113. int ret;
  114. unsigned char buf[1] = { 0 };
  115. /*
  116. * This is the same apparent "are you ready?" poll command observed
  117. * watching Windows driver traffic and implemented in lirc_zilog. With
  118. * this added, we get far saner remote behavior with z8 chips on usb
  119. * connected devices, even with the default polling interval of 100ms.
  120. */
  121. ret = i2c_master_send(ir->c, buf, 1);
  122. if (ret != 1)
  123. return (ret < 0) ? ret : -EINVAL;
  124. return get_key_haup_common (ir, ir_key, ir_raw, 6, 3);
  125. }
  126. static int get_key_pixelview(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  127. {
  128. unsigned char b;
  129. /* poll IR chip */
  130. if (1 != i2c_master_recv(ir->c, &b, 1)) {
  131. dprintk(1,"read error\n");
  132. return -EIO;
  133. }
  134. *ir_key = b;
  135. *ir_raw = b;
  136. return 1;
  137. }
  138. static int get_key_fusionhdtv(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  139. {
  140. unsigned char buf[4];
  141. /* poll IR chip */
  142. if (4 != i2c_master_recv(ir->c, buf, 4)) {
  143. dprintk(1,"read error\n");
  144. return -EIO;
  145. }
  146. if(buf[0] !=0 || buf[1] !=0 || buf[2] !=0 || buf[3] != 0)
  147. dprintk(2, "%s: 0x%2x 0x%2x 0x%2x 0x%2x\n", __func__,
  148. buf[0], buf[1], buf[2], buf[3]);
  149. /* no key pressed or signal from other ir remote */
  150. if(buf[0] != 0x1 || buf[1] != 0xfe)
  151. return 0;
  152. *ir_key = buf[2];
  153. *ir_raw = (buf[2] << 8) | buf[3];
  154. return 1;
  155. }
  156. static int get_key_knc1(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  157. {
  158. unsigned char b;
  159. /* poll IR chip */
  160. if (1 != i2c_master_recv(ir->c, &b, 1)) {
  161. dprintk(1,"read error\n");
  162. return -EIO;
  163. }
  164. /* it seems that 0xFE indicates that a button is still hold
  165. down, while 0xff indicates that no button is hold
  166. down. 0xfe sequences are sometimes interrupted by 0xFF */
  167. dprintk(2,"key %02x\n", b);
  168. if (b == 0xff)
  169. return 0;
  170. if (b == 0xfe)
  171. /* keep old data */
  172. return 1;
  173. *ir_key = b;
  174. *ir_raw = b;
  175. return 1;
  176. }
  177. static int get_key_avermedia_cardbus(struct IR_i2c *ir,
  178. u32 *ir_key, u32 *ir_raw)
  179. {
  180. unsigned char subaddr, key, keygroup;
  181. struct i2c_msg msg[] = { { .addr = ir->c->addr, .flags = 0,
  182. .buf = &subaddr, .len = 1},
  183. { .addr = ir->c->addr, .flags = I2C_M_RD,
  184. .buf = &key, .len = 1} };
  185. subaddr = 0x0d;
  186. if (2 != i2c_transfer(ir->c->adapter, msg, 2)) {
  187. dprintk(1, "read error\n");
  188. return -EIO;
  189. }
  190. if (key == 0xff)
  191. return 0;
  192. subaddr = 0x0b;
  193. msg[1].buf = &keygroup;
  194. if (2 != i2c_transfer(ir->c->adapter, msg, 2)) {
  195. dprintk(1, "read error\n");
  196. return -EIO;
  197. }
  198. if (keygroup == 0xff)
  199. return 0;
  200. dprintk(1, "read key 0x%02x/0x%02x\n", key, keygroup);
  201. if (keygroup < 2 || keygroup > 3) {
  202. /* Only a warning */
  203. dprintk(1, "warning: invalid key group 0x%02x for key 0x%02x\n",
  204. keygroup, key);
  205. }
  206. key |= (keygroup & 1) << 6;
  207. *ir_key = key;
  208. *ir_raw = key;
  209. return 1;
  210. }
  211. /* ----------------------------------------------------------------------- */
  212. static void ir_key_poll(struct IR_i2c *ir)
  213. {
  214. static u32 ir_key, ir_raw;
  215. int rc;
  216. dprintk(3, "%s\n", __func__);
  217. rc = ir->get_key(ir, &ir_key, &ir_raw);
  218. if (rc < 0) {
  219. dprintk(2,"error\n");
  220. return;
  221. }
  222. if (rc) {
  223. dprintk(1, "%s: keycode = 0x%04x\n", __func__, ir_key);
  224. rc_keydown(ir->rc, ir_key, 0);
  225. }
  226. }
  227. static void ir_work(struct work_struct *work)
  228. {
  229. struct IR_i2c *ir = container_of(work, struct IR_i2c, work.work);
  230. ir_key_poll(ir);
  231. schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling_interval));
  232. }
  233. /* ----------------------------------------------------------------------- */
  234. static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)
  235. {
  236. char *ir_codes = NULL;
  237. const char *name = NULL;
  238. u64 rc_type = RC_TYPE_UNKNOWN;
  239. struct IR_i2c *ir;
  240. struct rc_dev *rc = NULL;
  241. struct i2c_adapter *adap = client->adapter;
  242. unsigned short addr = client->addr;
  243. int err;
  244. ir = kzalloc(sizeof(struct IR_i2c), GFP_KERNEL);
  245. if (!ir)
  246. return -ENOMEM;
  247. ir->c = client;
  248. ir->polling_interval = DEFAULT_POLLING_INTERVAL;
  249. i2c_set_clientdata(client, ir);
  250. switch(addr) {
  251. case 0x64:
  252. name = "Pixelview";
  253. ir->get_key = get_key_pixelview;
  254. rc_type = RC_TYPE_OTHER;
  255. ir_codes = RC_MAP_EMPTY;
  256. break;
  257. case 0x18:
  258. case 0x1f:
  259. case 0x1a:
  260. name = "Hauppauge";
  261. ir->get_key = get_key_haup;
  262. rc_type = RC_TYPE_RC5;
  263. if (hauppauge == 1) {
  264. ir_codes = RC_MAP_HAUPPAUGE_NEW;
  265. } else {
  266. ir_codes = RC_MAP_RC5_TV;
  267. }
  268. break;
  269. case 0x30:
  270. name = "KNC One";
  271. ir->get_key = get_key_knc1;
  272. rc_type = RC_TYPE_OTHER;
  273. ir_codes = RC_MAP_EMPTY;
  274. break;
  275. case 0x6b:
  276. name = "FusionHDTV";
  277. ir->get_key = get_key_fusionhdtv;
  278. rc_type = RC_TYPE_RC5;
  279. ir_codes = RC_MAP_FUSIONHDTV_MCE;
  280. break;
  281. case 0x40:
  282. name = "AVerMedia Cardbus remote";
  283. ir->get_key = get_key_avermedia_cardbus;
  284. rc_type = RC_TYPE_OTHER;
  285. ir_codes = RC_MAP_AVERMEDIA_CARDBUS;
  286. break;
  287. case 0x71:
  288. name = "Hauppauge/Zilog Z8";
  289. ir->get_key = get_key_haup_xvr;
  290. rc_type = RC_TYPE_RC5;
  291. ir_codes = hauppauge ? RC_MAP_HAUPPAUGE_NEW : RC_MAP_RC5_TV;
  292. break;
  293. }
  294. /* Let the caller override settings */
  295. if (client->dev.platform_data) {
  296. const struct IR_i2c_init_data *init_data =
  297. client->dev.platform_data;
  298. ir_codes = init_data->ir_codes;
  299. rc = init_data->rc_dev;
  300. name = init_data->name;
  301. if (init_data->type)
  302. rc_type = init_data->type;
  303. if (init_data->polling_interval)
  304. ir->polling_interval = init_data->polling_interval;
  305. switch (init_data->internal_get_key_func) {
  306. case IR_KBD_GET_KEY_CUSTOM:
  307. /* The bridge driver provided us its own function */
  308. ir->get_key = init_data->get_key;
  309. break;
  310. case IR_KBD_GET_KEY_PIXELVIEW:
  311. ir->get_key = get_key_pixelview;
  312. break;
  313. case IR_KBD_GET_KEY_HAUP:
  314. ir->get_key = get_key_haup;
  315. break;
  316. case IR_KBD_GET_KEY_KNC1:
  317. ir->get_key = get_key_knc1;
  318. break;
  319. case IR_KBD_GET_KEY_FUSIONHDTV:
  320. ir->get_key = get_key_fusionhdtv;
  321. break;
  322. case IR_KBD_GET_KEY_HAUP_XVR:
  323. ir->get_key = get_key_haup_xvr;
  324. break;
  325. case IR_KBD_GET_KEY_AVERMEDIA_CARDBUS:
  326. ir->get_key = get_key_avermedia_cardbus;
  327. break;
  328. }
  329. }
  330. if (!rc) {
  331. /*
  332. * If platform_data doesn't specify rc_dev, initilize it
  333. * internally
  334. */
  335. rc = rc_allocate_device();
  336. if (!rc) {
  337. err = -ENOMEM;
  338. goto err_out_free;
  339. }
  340. }
  341. ir->rc = rc;
  342. /* Make sure we are all setup before going on */
  343. if (!name || !ir->get_key || !rc_type || !ir_codes) {
  344. dprintk(1, ": Unsupported device at address 0x%02x\n",
  345. addr);
  346. err = -ENODEV;
  347. goto err_out_free;
  348. }
  349. /* Sets name */
  350. snprintf(ir->name, sizeof(ir->name), "i2c IR (%s)", name);
  351. ir->ir_codes = ir_codes;
  352. snprintf(ir->phys, sizeof(ir->phys), "%s/%s/ir0",
  353. dev_name(&adap->dev),
  354. dev_name(&client->dev));
  355. /*
  356. * Initialize input_dev fields
  357. * It doesn't make sense to allow overriding them via platform_data
  358. */
  359. rc->input_id.bustype = BUS_I2C;
  360. rc->input_phys = ir->phys;
  361. rc->input_name = ir->name;
  362. /*
  363. * Initialize the other fields of rc_dev
  364. */
  365. rc->map_name = ir->ir_codes;
  366. rc->allowed_protos = rc_type;
  367. if (!rc->driver_name)
  368. rc->driver_name = MODULE_NAME;
  369. err = rc_register_device(rc);
  370. if (err)
  371. goto err_out_free;
  372. printk(MODULE_NAME ": %s detected at %s [%s]\n",
  373. ir->name, ir->phys, adap->name);
  374. /* start polling via eventd */
  375. INIT_DELAYED_WORK(&ir->work, ir_work);
  376. schedule_delayed_work(&ir->work, 0);
  377. return 0;
  378. err_out_free:
  379. /* Only frees rc if it were allocated internally */
  380. rc_free_device(rc);
  381. kfree(ir);
  382. return err;
  383. }
  384. static int ir_remove(struct i2c_client *client)
  385. {
  386. struct IR_i2c *ir = i2c_get_clientdata(client);
  387. /* kill outstanding polls */
  388. cancel_delayed_work_sync(&ir->work);
  389. /* unregister device */
  390. rc_unregister_device(ir->rc);
  391. /* free memory */
  392. kfree(ir);
  393. return 0;
  394. }
  395. static const struct i2c_device_id ir_kbd_id[] = {
  396. /* Generic entry for any IR receiver */
  397. { "ir_video", 0 },
  398. /* IR device specific entries should be added here */
  399. { "ir_rx_z8f0811_haup", 0 },
  400. { "ir_rx_z8f0811_hdpvr", 0 },
  401. { }
  402. };
  403. static struct i2c_driver driver = {
  404. .driver = {
  405. .name = "ir-kbd-i2c",
  406. },
  407. .probe = ir_probe,
  408. .remove = ir_remove,
  409. .id_table = ir_kbd_id,
  410. };
  411. /* ----------------------------------------------------------------------- */
  412. MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, Ulrich Mueller");
  413. MODULE_DESCRIPTION("input driver for i2c IR remote controls");
  414. MODULE_LICENSE("GPL");
  415. static int __init ir_init(void)
  416. {
  417. return i2c_add_driver(&driver);
  418. }
  419. static void __exit ir_fini(void)
  420. {
  421. i2c_del_driver(&driver);
  422. }
  423. module_init(ir_init);
  424. module_exit(ir_fini);
  425. /*
  426. * Overrides for Emacs so that we follow Linus's tabbing style.
  427. * ---------------------------------------------------------------------------
  428. * Local variables:
  429. * c-basic-offset: 8
  430. * End:
  431. */