ir-kbd-i2c.c 13 KB

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