ir-kbd-i2c.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. return get_key_haup_common (ir, ir_key, ir_raw, 6, 3);
  114. }
  115. static int get_key_pixelview(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  116. {
  117. unsigned char b;
  118. /* poll IR chip */
  119. if (1 != i2c_master_recv(ir->c, &b, 1)) {
  120. dprintk(1,"read error\n");
  121. return -EIO;
  122. }
  123. *ir_key = b;
  124. *ir_raw = b;
  125. return 1;
  126. }
  127. static int get_key_fusionhdtv(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  128. {
  129. unsigned char buf[4];
  130. /* poll IR chip */
  131. if (4 != i2c_master_recv(ir->c, buf, 4)) {
  132. dprintk(1,"read error\n");
  133. return -EIO;
  134. }
  135. if(buf[0] !=0 || buf[1] !=0 || buf[2] !=0 || buf[3] != 0)
  136. dprintk(2, "%s: 0x%2x 0x%2x 0x%2x 0x%2x\n", __func__,
  137. buf[0], buf[1], buf[2], buf[3]);
  138. /* no key pressed or signal from other ir remote */
  139. if(buf[0] != 0x1 || buf[1] != 0xfe)
  140. return 0;
  141. *ir_key = buf[2];
  142. *ir_raw = (buf[2] << 8) | buf[3];
  143. return 1;
  144. }
  145. static int get_key_knc1(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  146. {
  147. unsigned char b;
  148. /* poll IR chip */
  149. if (1 != i2c_master_recv(ir->c, &b, 1)) {
  150. dprintk(1,"read error\n");
  151. return -EIO;
  152. }
  153. /* it seems that 0xFE indicates that a button is still hold
  154. down, while 0xff indicates that no button is hold
  155. down. 0xfe sequences are sometimes interrupted by 0xFF */
  156. dprintk(2,"key %02x\n", b);
  157. if (b == 0xff)
  158. return 0;
  159. if (b == 0xfe)
  160. /* keep old data */
  161. return 1;
  162. *ir_key = b;
  163. *ir_raw = b;
  164. return 1;
  165. }
  166. static int get_key_avermedia_cardbus(struct IR_i2c *ir,
  167. u32 *ir_key, u32 *ir_raw)
  168. {
  169. unsigned char subaddr, key, keygroup;
  170. struct i2c_msg msg[] = { { .addr = ir->c->addr, .flags = 0,
  171. .buf = &subaddr, .len = 1},
  172. { .addr = ir->c->addr, .flags = I2C_M_RD,
  173. .buf = &key, .len = 1} };
  174. subaddr = 0x0d;
  175. if (2 != i2c_transfer(ir->c->adapter, msg, 2)) {
  176. dprintk(1, "read error\n");
  177. return -EIO;
  178. }
  179. if (key == 0xff)
  180. return 0;
  181. subaddr = 0x0b;
  182. msg[1].buf = &keygroup;
  183. if (2 != i2c_transfer(ir->c->adapter, msg, 2)) {
  184. dprintk(1, "read error\n");
  185. return -EIO;
  186. }
  187. if (keygroup == 0xff)
  188. return 0;
  189. dprintk(1, "read key 0x%02x/0x%02x\n", key, keygroup);
  190. if (keygroup < 2 || keygroup > 3) {
  191. /* Only a warning */
  192. dprintk(1, "warning: invalid key group 0x%02x for key 0x%02x\n",
  193. keygroup, key);
  194. }
  195. key |= (keygroup & 1) << 6;
  196. *ir_key = key;
  197. *ir_raw = key;
  198. return 1;
  199. }
  200. /* ----------------------------------------------------------------------- */
  201. static void ir_key_poll(struct IR_i2c *ir)
  202. {
  203. static u32 ir_key, ir_raw;
  204. int rc;
  205. dprintk(2,"ir_poll_key\n");
  206. rc = ir->get_key(ir, &ir_key, &ir_raw);
  207. if (rc < 0) {
  208. dprintk(2,"error\n");
  209. return;
  210. }
  211. if (rc)
  212. ir_keydown(ir->rc, ir_key, 0);
  213. }
  214. static void ir_work(struct work_struct *work)
  215. {
  216. struct IR_i2c *ir = container_of(work, struct IR_i2c, work.work);
  217. ir_key_poll(ir);
  218. schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling_interval));
  219. }
  220. /* ----------------------------------------------------------------------- */
  221. static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)
  222. {
  223. char *ir_codes = NULL;
  224. const char *name = NULL;
  225. u64 ir_type = IR_TYPE_UNKNOWN;
  226. struct IR_i2c *ir;
  227. struct rc_dev *rc = NULL;
  228. struct i2c_adapter *adap = client->adapter;
  229. unsigned short addr = client->addr;
  230. int err;
  231. ir = kzalloc(sizeof(struct IR_i2c), GFP_KERNEL);
  232. if (!ir)
  233. return -ENOMEM;
  234. ir->c = client;
  235. ir->polling_interval = DEFAULT_POLLING_INTERVAL;
  236. i2c_set_clientdata(client, ir);
  237. switch(addr) {
  238. case 0x64:
  239. name = "Pixelview";
  240. ir->get_key = get_key_pixelview;
  241. ir_type = IR_TYPE_OTHER;
  242. ir_codes = RC_MAP_EMPTY;
  243. break;
  244. case 0x18:
  245. case 0x1f:
  246. case 0x1a:
  247. name = "Hauppauge";
  248. ir->get_key = get_key_haup;
  249. ir_type = IR_TYPE_RC5;
  250. if (hauppauge == 1) {
  251. ir_codes = RC_MAP_HAUPPAUGE_NEW;
  252. } else {
  253. ir_codes = RC_MAP_RC5_TV;
  254. }
  255. break;
  256. case 0x30:
  257. name = "KNC One";
  258. ir->get_key = get_key_knc1;
  259. ir_type = IR_TYPE_OTHER;
  260. ir_codes = RC_MAP_EMPTY;
  261. break;
  262. case 0x6b:
  263. name = "FusionHDTV";
  264. ir->get_key = get_key_fusionhdtv;
  265. ir_type = IR_TYPE_RC5;
  266. ir_codes = RC_MAP_FUSIONHDTV_MCE;
  267. break;
  268. case 0x40:
  269. name = "AVerMedia Cardbus remote";
  270. ir->get_key = get_key_avermedia_cardbus;
  271. ir_type = IR_TYPE_OTHER;
  272. ir_codes = RC_MAP_AVERMEDIA_CARDBUS;
  273. break;
  274. }
  275. /* Let the caller override settings */
  276. if (client->dev.platform_data) {
  277. const struct IR_i2c_init_data *init_data =
  278. client->dev.platform_data;
  279. ir_codes = init_data->ir_codes;
  280. rc = init_data->rc_dev;
  281. name = init_data->name;
  282. if (init_data->type)
  283. ir_type = init_data->type;
  284. if (init_data->polling_interval)
  285. ir->polling_interval = init_data->polling_interval;
  286. switch (init_data->internal_get_key_func) {
  287. case IR_KBD_GET_KEY_CUSTOM:
  288. /* The bridge driver provided us its own function */
  289. ir->get_key = init_data->get_key;
  290. break;
  291. case IR_KBD_GET_KEY_PIXELVIEW:
  292. ir->get_key = get_key_pixelview;
  293. break;
  294. case IR_KBD_GET_KEY_HAUP:
  295. ir->get_key = get_key_haup;
  296. break;
  297. case IR_KBD_GET_KEY_KNC1:
  298. ir->get_key = get_key_knc1;
  299. break;
  300. case IR_KBD_GET_KEY_FUSIONHDTV:
  301. ir->get_key = get_key_fusionhdtv;
  302. break;
  303. case IR_KBD_GET_KEY_HAUP_XVR:
  304. ir->get_key = get_key_haup_xvr;
  305. break;
  306. case IR_KBD_GET_KEY_AVERMEDIA_CARDBUS:
  307. ir->get_key = get_key_avermedia_cardbus;
  308. break;
  309. }
  310. }
  311. if (!rc) {
  312. /*
  313. * If platform_data doesn't specify rc_dev, initilize it
  314. * internally
  315. */
  316. rc = rc_allocate_device();
  317. if (!rc) {
  318. err = -ENOMEM;
  319. goto err_out_free;
  320. }
  321. }
  322. ir->rc = rc;
  323. /* Make sure we are all setup before going on */
  324. if (!name || !ir->get_key || !ir_type || !ir_codes) {
  325. dprintk(1, ": Unsupported device at address 0x%02x\n",
  326. addr);
  327. err = -ENODEV;
  328. goto err_out_free;
  329. }
  330. /* Sets name */
  331. snprintf(ir->name, sizeof(ir->name), "i2c IR (%s)", name);
  332. ir->ir_codes = ir_codes;
  333. snprintf(ir->phys, sizeof(ir->phys), "%s/%s/ir0",
  334. dev_name(&adap->dev),
  335. dev_name(&client->dev));
  336. /*
  337. * Initialize input_dev fields
  338. * It doesn't make sense to allow overriding them via platform_data
  339. */
  340. rc->input_id.bustype = BUS_I2C;
  341. rc->input_phys = ir->phys;
  342. rc->input_name = ir->name;
  343. /*
  344. * Initialize the other fields of rc_dev
  345. */
  346. rc->map_name = ir->ir_codes;
  347. rc->allowed_protos = ir_type;
  348. if (!rc->driver_name)
  349. rc->driver_name = MODULE_NAME;
  350. err = rc_register_device(rc);
  351. if (err)
  352. goto err_out_free;
  353. printk(MODULE_NAME ": %s detected at %s [%s]\n",
  354. ir->name, ir->phys, adap->name);
  355. /* start polling via eventd */
  356. INIT_DELAYED_WORK(&ir->work, ir_work);
  357. schedule_delayed_work(&ir->work, 0);
  358. return 0;
  359. err_out_free:
  360. /* Only frees rc if it were allocated internally */
  361. rc_free_device(rc);
  362. kfree(ir);
  363. return err;
  364. }
  365. static int ir_remove(struct i2c_client *client)
  366. {
  367. struct IR_i2c *ir = i2c_get_clientdata(client);
  368. /* kill outstanding polls */
  369. cancel_delayed_work_sync(&ir->work);
  370. /* unregister device */
  371. rc_unregister_device(ir->rc);
  372. /* free memory */
  373. kfree(ir);
  374. return 0;
  375. }
  376. static const struct i2c_device_id ir_kbd_id[] = {
  377. /* Generic entry for any IR receiver */
  378. { "ir_video", 0 },
  379. /* IR device specific entries should be added here */
  380. { "ir_rx_z8f0811_haup", 0 },
  381. { }
  382. };
  383. static struct i2c_driver driver = {
  384. .driver = {
  385. .name = "ir-kbd-i2c",
  386. },
  387. .probe = ir_probe,
  388. .remove = ir_remove,
  389. .id_table = ir_kbd_id,
  390. };
  391. /* ----------------------------------------------------------------------- */
  392. MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, Ulrich Mueller");
  393. MODULE_DESCRIPTION("input driver for i2c IR remote controls");
  394. MODULE_LICENSE("GPL");
  395. static int __init ir_init(void)
  396. {
  397. return i2c_add_driver(&driver);
  398. }
  399. static void __exit ir_fini(void)
  400. {
  401. i2c_del_driver(&driver);
  402. }
  403. module_init(ir_init);
  404. module_exit(ir_fini);
  405. /*
  406. * Overrides for Emacs so that we follow Linus's tabbing style.
  407. * ---------------------------------------------------------------------------
  408. * Local variables:
  409. * c-basic-offset: 8
  410. * End:
  411. */