ir-kbd-i2c.c 12 KB

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