ir-kbd-i2c.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. *
  20. * This program is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation; either version 2 of the License, or
  23. * (at your option) any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with this program; if not, write to the Free Software
  32. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  33. *
  34. */
  35. #include <linux/module.h>
  36. #include <linux/init.h>
  37. #include <linux/kernel.h>
  38. #include <linux/string.h>
  39. #include <linux/timer.h>
  40. #include <linux/delay.h>
  41. #include <linux/errno.h>
  42. #include <linux/slab.h>
  43. #include <linux/i2c.h>
  44. #include <linux/i2c-id.h>
  45. #include <linux/workqueue.h>
  46. #include <media/ir-common.h>
  47. #include <media/ir-kbd-i2c.h>
  48. /* ----------------------------------------------------------------------- */
  49. /* insmod parameters */
  50. static int debug;
  51. module_param(debug, int, 0644); /* debug level (0,1,2) */
  52. static int hauppauge;
  53. module_param(hauppauge, int, 0644); /* Choose Hauppauge remote */
  54. MODULE_PARM_DESC(hauppauge, "Specify Hauppauge remote: 0=black, 1=grey (defaults to 0)");
  55. #define DEVNAME "ir-kbd-i2c"
  56. #define dprintk(level, fmt, arg...) if (debug >= level) \
  57. printk(KERN_DEBUG DEVNAME ": " fmt , ## arg)
  58. /* ----------------------------------------------------------------------- */
  59. static int get_key_haup_common(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw,
  60. int size, int offset)
  61. {
  62. unsigned char buf[6];
  63. int start, range, toggle, dev, code, ircode;
  64. /* poll IR chip */
  65. if (size != i2c_master_recv(&ir->c,buf,size))
  66. return -EIO;
  67. /* split rc5 data block ... */
  68. start = (buf[offset] >> 7) & 1;
  69. range = (buf[offset] >> 6) & 1;
  70. toggle = (buf[offset] >> 5) & 1;
  71. dev = buf[offset] & 0x1f;
  72. code = (buf[offset+1] >> 2) & 0x3f;
  73. /* rc5 has two start bits
  74. * the first bit must be one
  75. * the second bit defines the command range (1 = 0-63, 0 = 64 - 127)
  76. */
  77. if (!start)
  78. /* no key pressed */
  79. return 0;
  80. /*
  81. * Hauppauge remotes (black/silver) always use
  82. * specific device ids. If we do not filter the
  83. * device ids then messages destined for devices
  84. * such as TVs (id=0) will get through causing
  85. * mis-fired events.
  86. *
  87. * We also filter out invalid key presses which
  88. * produce annoying debug log entries.
  89. */
  90. ircode= (start << 12) | (toggle << 11) | (dev << 6) | code;
  91. if ((ircode & 0x1fff)==0x1fff)
  92. /* invalid key press */
  93. return 0;
  94. if (dev!=0x1e && dev!=0x1f)
  95. /* not a hauppauge remote */
  96. return 0;
  97. if (!range)
  98. code += 64;
  99. dprintk(1,"ir hauppauge (rc5): s%d r%d t%d dev=%d code=%d\n",
  100. start, range, toggle, dev, code);
  101. /* return key */
  102. *ir_key = code;
  103. *ir_raw = ircode;
  104. return 1;
  105. }
  106. static inline int get_key_haup(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  107. {
  108. return get_key_haup_common (ir, ir_key, ir_raw, 3, 0);
  109. }
  110. static inline int get_key_haup_xvr(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  111. {
  112. return get_key_haup_common (ir, ir_key, ir_raw, 6, 3);
  113. }
  114. static int get_key_pixelview(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  115. {
  116. unsigned char b;
  117. /* poll IR chip */
  118. if (1 != i2c_master_recv(&ir->c,&b,1)) {
  119. dprintk(1,"read error\n");
  120. return -EIO;
  121. }
  122. *ir_key = b;
  123. *ir_raw = b;
  124. return 1;
  125. }
  126. static int get_key_pv951(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. /* ignore 0xaa */
  135. if (b==0xaa)
  136. return 0;
  137. dprintk(2,"key %02x\n", b);
  138. *ir_key = b;
  139. *ir_raw = b;
  140. return 1;
  141. }
  142. static int get_key_fusionhdtv(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  143. {
  144. unsigned char buf[4];
  145. /* poll IR chip */
  146. if (4 != i2c_master_recv(&ir->c,buf,4)) {
  147. dprintk(1,"read error\n");
  148. return -EIO;
  149. }
  150. if(buf[0] !=0 || buf[1] !=0 || buf[2] !=0 || buf[3] != 0)
  151. dprintk(2, "%s: 0x%2x 0x%2x 0x%2x 0x%2x\n", __func__,
  152. buf[0], buf[1], buf[2], buf[3]);
  153. /* no key pressed or signal from other ir remote */
  154. if(buf[0] != 0x1 || buf[1] != 0xfe)
  155. return 0;
  156. *ir_key = buf[2];
  157. *ir_raw = (buf[2] << 8) | buf[3];
  158. return 1;
  159. }
  160. static int get_key_knc1(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  161. {
  162. unsigned char b;
  163. /* poll IR chip */
  164. if (1 != i2c_master_recv(&ir->c,&b,1)) {
  165. dprintk(1,"read error\n");
  166. return -EIO;
  167. }
  168. /* it seems that 0xFE indicates that a button is still hold
  169. down, while 0xff indicates that no button is hold
  170. down. 0xfe sequences are sometimes interrupted by 0xFF */
  171. dprintk(2,"key %02x\n", b);
  172. if (b == 0xff)
  173. return 0;
  174. if (b == 0xfe)
  175. /* keep old data */
  176. return 1;
  177. *ir_key = b;
  178. *ir_raw = b;
  179. return 1;
  180. }
  181. /* ----------------------------------------------------------------------- */
  182. static void ir_key_poll(struct IR_i2c *ir)
  183. {
  184. static u32 ir_key, ir_raw;
  185. int rc;
  186. dprintk(2,"ir_poll_key\n");
  187. rc = ir->get_key(ir, &ir_key, &ir_raw);
  188. if (rc < 0) {
  189. dprintk(2,"error\n");
  190. return;
  191. }
  192. if (0 == rc) {
  193. ir_input_nokey(ir->input, &ir->ir);
  194. } else {
  195. ir_input_keydown(ir->input, &ir->ir, ir_key, ir_raw);
  196. }
  197. }
  198. static void ir_timer(unsigned long data)
  199. {
  200. struct IR_i2c *ir = (struct IR_i2c*)data;
  201. schedule_work(&ir->work);
  202. }
  203. static void ir_work(struct work_struct *work)
  204. {
  205. struct IR_i2c *ir = container_of(work, struct IR_i2c, work);
  206. int polling_interval = 100;
  207. /* MSI TV@nywhere Plus requires more frequent polling
  208. otherwise it will miss some keypresses */
  209. if (ir->c.adapter->id == I2C_HW_SAA7134 && ir->c.addr == 0x30)
  210. polling_interval = 50;
  211. ir_key_poll(ir);
  212. mod_timer(&ir->timer, jiffies + msecs_to_jiffies(polling_interval));
  213. }
  214. /* ----------------------------------------------------------------------- */
  215. static int ir_attach(struct i2c_adapter *adap, int addr,
  216. unsigned short flags, int kind);
  217. static int ir_detach(struct i2c_client *client);
  218. static int ir_probe(struct i2c_adapter *adap);
  219. static struct i2c_driver driver = {
  220. .driver = {
  221. .name = "ir-kbd-i2c",
  222. },
  223. .id = I2C_DRIVERID_INFRARED,
  224. .attach_adapter = ir_probe,
  225. .detach_client = ir_detach,
  226. };
  227. static struct i2c_client client_template =
  228. {
  229. .name = "unset",
  230. .driver = &driver
  231. };
  232. static int ir_attach(struct i2c_adapter *adap, int addr,
  233. unsigned short flags, int kind)
  234. {
  235. IR_KEYTAB_TYPE *ir_codes = NULL;
  236. char *name;
  237. int ir_type;
  238. struct IR_i2c *ir;
  239. struct input_dev *input_dev;
  240. int err;
  241. ir = kzalloc(sizeof(struct IR_i2c),GFP_KERNEL);
  242. input_dev = input_allocate_device();
  243. if (!ir || !input_dev) {
  244. err = -ENOMEM;
  245. goto err_out_free;
  246. }
  247. ir->c = client_template;
  248. ir->input = input_dev;
  249. ir->c.adapter = adap;
  250. ir->c.addr = addr;
  251. i2c_set_clientdata(&ir->c, ir);
  252. switch(addr) {
  253. case 0x64:
  254. name = "Pixelview";
  255. ir->get_key = get_key_pixelview;
  256. ir_type = IR_TYPE_OTHER;
  257. ir_codes = ir_codes_empty;
  258. break;
  259. case 0x4b:
  260. name = "PV951";
  261. ir->get_key = get_key_pv951;
  262. ir_type = IR_TYPE_OTHER;
  263. ir_codes = ir_codes_pv951;
  264. break;
  265. case 0x18:
  266. case 0x1a:
  267. name = "Hauppauge";
  268. ir->get_key = get_key_haup;
  269. ir_type = IR_TYPE_RC5;
  270. if (hauppauge == 1) {
  271. ir_codes = ir_codes_hauppauge_new;
  272. } else {
  273. ir_codes = ir_codes_rc5_tv;
  274. }
  275. break;
  276. case 0x30:
  277. name = "KNC One";
  278. ir->get_key = get_key_knc1;
  279. ir_type = IR_TYPE_OTHER;
  280. ir_codes = ir_codes_empty;
  281. break;
  282. case 0x6b:
  283. name = "FusionHDTV";
  284. ir->get_key = get_key_fusionhdtv;
  285. ir_type = IR_TYPE_RC5;
  286. ir_codes = ir_codes_fusionhdtv_mce;
  287. break;
  288. case 0x7a:
  289. case 0x47:
  290. case 0x71:
  291. case 0x2d:
  292. if (adap->id == I2C_HW_B_CX2388x) {
  293. /* Handled by cx88-input */
  294. name = "CX2388x remote";
  295. ir_type = IR_TYPE_RC5;
  296. ir->get_key = get_key_haup_xvr;
  297. if (hauppauge == 1) {
  298. ir_codes = ir_codes_hauppauge_new;
  299. } else {
  300. ir_codes = ir_codes_rc5_tv;
  301. }
  302. } else {
  303. /* Handled by saa7134-input */
  304. name = "SAA713x remote";
  305. ir_type = IR_TYPE_OTHER;
  306. }
  307. break;
  308. default:
  309. /* shouldn't happen */
  310. printk(DEVNAME ": Huh? unknown i2c address (0x%02x)?\n", addr);
  311. err = -ENODEV;
  312. goto err_out_free;
  313. }
  314. /* Sets name */
  315. snprintf(ir->c.name, sizeof(ir->c.name), "i2c IR (%s)", name);
  316. ir->ir_codes = ir_codes;
  317. /* register i2c device
  318. * At device register, IR codes may be changed to be
  319. * board dependent.
  320. */
  321. err = i2c_attach_client(&ir->c);
  322. if (err)
  323. goto err_out_free;
  324. /* If IR not supported or disabled, unregisters driver */
  325. if (ir->get_key == NULL) {
  326. err = -ENODEV;
  327. goto err_out_detach;
  328. }
  329. /* Phys addr can only be set after attaching (for ir->c.dev) */
  330. snprintf(ir->phys, sizeof(ir->phys), "%s/%s/ir0",
  331. dev_name(&ir->c.adapter->dev),
  332. dev_name(&ir->c.dev));
  333. /* init + register input device */
  334. ir_input_init(input_dev, &ir->ir, ir_type, ir->ir_codes);
  335. input_dev->id.bustype = BUS_I2C;
  336. input_dev->name = ir->c.name;
  337. input_dev->phys = ir->phys;
  338. err = input_register_device(ir->input);
  339. if (err)
  340. goto err_out_detach;
  341. printk(DEVNAME ": %s detected at %s [%s]\n",
  342. ir->input->name, ir->input->phys, adap->name);
  343. /* start polling via eventd */
  344. INIT_WORK(&ir->work, ir_work);
  345. init_timer(&ir->timer);
  346. ir->timer.function = ir_timer;
  347. ir->timer.data = (unsigned long)ir;
  348. schedule_work(&ir->work);
  349. return 0;
  350. err_out_detach:
  351. i2c_detach_client(&ir->c);
  352. err_out_free:
  353. input_free_device(input_dev);
  354. kfree(ir);
  355. return err;
  356. }
  357. static int ir_detach(struct i2c_client *client)
  358. {
  359. struct IR_i2c *ir = i2c_get_clientdata(client);
  360. /* kill outstanding polls */
  361. del_timer_sync(&ir->timer);
  362. flush_scheduled_work();
  363. /* unregister devices */
  364. input_unregister_device(ir->input);
  365. i2c_detach_client(&ir->c);
  366. /* free memory */
  367. kfree(ir);
  368. return 0;
  369. }
  370. static int ir_probe(struct i2c_adapter *adap)
  371. {
  372. /* The external IR receiver is at i2c address 0x34 (0x35 for
  373. reads). Future Hauppauge cards will have an internal
  374. receiver at 0x30 (0x31 for reads). In theory, both can be
  375. fitted, and Hauppauge suggest an external overrides an
  376. internal.
  377. That's why we probe 0x1a (~0x34) first. CB
  378. */
  379. static const int probe_bttv[] = { 0x1a, 0x18, 0x4b, 0x64, 0x30, -1};
  380. static const int probe_saa7134[] = { 0x7a, 0x47, 0x71, 0x2d, -1 };
  381. static const int probe_em28XX[] = { 0x30, 0x47, -1 };
  382. static const int probe_cx88[] = { 0x18, 0x6b, 0x71, -1 };
  383. static const int probe_cx23885[] = { 0x6b, -1 };
  384. const int *probe;
  385. struct i2c_msg msg = {
  386. .flags = I2C_M_RD,
  387. .len = 0,
  388. .buf = NULL,
  389. };
  390. int i, rc;
  391. switch (adap->id) {
  392. case I2C_HW_B_BT848:
  393. probe = probe_bttv;
  394. break;
  395. case I2C_HW_B_CX2341X:
  396. probe = probe_bttv;
  397. break;
  398. case I2C_HW_SAA7134:
  399. probe = probe_saa7134;
  400. break;
  401. case I2C_HW_B_EM28XX:
  402. probe = probe_em28XX;
  403. break;
  404. case I2C_HW_B_CX2388x:
  405. probe = probe_cx88;
  406. break;
  407. case I2C_HW_B_CX23885:
  408. probe = probe_cx23885;
  409. break;
  410. default:
  411. return 0;
  412. }
  413. for (i = 0; -1 != probe[i]; i++) {
  414. msg.addr = probe[i];
  415. rc = i2c_transfer(adap, &msg, 1);
  416. dprintk(1,"probe 0x%02x @ %s: %s\n",
  417. probe[i], adap->name,
  418. (1 == rc) ? "yes" : "no");
  419. if (1 == rc) {
  420. ir_attach(adap, probe[i], 0, 0);
  421. return 0;
  422. }
  423. }
  424. /* Special case for MSI TV@nywhere Plus remote */
  425. if (adap->id == I2C_HW_SAA7134) {
  426. u8 temp;
  427. /* MSI TV@nywhere Plus controller doesn't seem to
  428. respond to probes unless we read something from
  429. an existing device. Weird... */
  430. msg.addr = 0x50;
  431. rc = i2c_transfer(adap, &msg, 1);
  432. dprintk(1, "probe 0x%02x @ %s: %s\n",
  433. msg.addr, adap->name,
  434. (1 == rc) ? "yes" : "no");
  435. /* Now do the probe. The controller does not respond
  436. to 0-byte reads, so we use a 1-byte read instead. */
  437. msg.addr = 0x30;
  438. msg.len = 1;
  439. msg.buf = &temp;
  440. rc = i2c_transfer(adap, &msg, 1);
  441. dprintk(1, "probe 0x%02x @ %s: %s\n",
  442. msg.addr, adap->name,
  443. (1 == rc) ? "yes" : "no");
  444. if (1 == rc)
  445. ir_attach(adap, msg.addr, 0, 0);
  446. }
  447. return 0;
  448. }
  449. /* ----------------------------------------------------------------------- */
  450. MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, Ulrich Mueller");
  451. MODULE_DESCRIPTION("input driver for i2c IR remote controls");
  452. MODULE_LICENSE("GPL");
  453. static int __init ir_init(void)
  454. {
  455. return i2c_add_driver(&driver);
  456. }
  457. static void __exit ir_fini(void)
  458. {
  459. i2c_del_driver(&driver);
  460. }
  461. module_init(ir_init);
  462. module_exit(ir_fini);
  463. /*
  464. * Overrides for Emacs so that we follow Linus's tabbing style.
  465. * ---------------------------------------------------------------------------
  466. * Local variables:
  467. * c-basic-offset: 8
  468. * End:
  469. */