ir-kbd-i2c.c 13 KB

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