em28xx-input.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /*
  2. handle em28xx IR remotes via linux kernel input layer.
  3. Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
  4. Markus Rechberger <mrechberger@gmail.com>
  5. Mauro Carvalho Chehab <mchehab@infradead.org>
  6. Sascha Sommer <saschasommer@freenet.de>
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/delay.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/usb.h>
  24. #include <linux/slab.h>
  25. #include "em28xx.h"
  26. #define EM28XX_SNAPSHOT_KEY KEY_CAMERA
  27. #define EM28XX_SBUTTON_QUERY_INTERVAL 500
  28. #define EM28XX_R0C_USBSUSP_SNAPSHOT 0x20
  29. static unsigned int ir_debug;
  30. module_param(ir_debug, int, 0644);
  31. MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
  32. #define MODULE_NAME "em28xx"
  33. #define dprintk(fmt, arg...) \
  34. if (ir_debug) { \
  35. printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
  36. }
  37. /**********************************************************
  38. Polling structure used by em28xx IR's
  39. **********************************************************/
  40. struct em28xx_ir_poll_result {
  41. unsigned int toggle_bit:1;
  42. unsigned int read_count:7;
  43. u32 scancode;
  44. };
  45. struct em28xx_IR {
  46. struct em28xx *dev;
  47. struct rc_dev *rc;
  48. char name[32];
  49. char phys[32];
  50. /* poll decoder */
  51. int polling;
  52. struct delayed_work work;
  53. unsigned int full_code:1;
  54. unsigned int last_readcount;
  55. u64 rc_type;
  56. /* i2c slave address of external device (if used) */
  57. u16 i2c_dev_addr;
  58. int (*get_key_i2c)(struct i2c_client *, u32 *);
  59. int (*get_key)(struct em28xx_IR *, struct em28xx_ir_poll_result *);
  60. };
  61. /**********************************************************
  62. I2C IR based get keycodes - should be used with ir-kbd-i2c
  63. **********************************************************/
  64. static int em28xx_get_key_terratec(struct i2c_client *i2c_dev, u32 *ir_key)
  65. {
  66. unsigned char b;
  67. /* poll IR chip */
  68. if (1 != i2c_master_recv(i2c_dev, &b, 1))
  69. return -EIO;
  70. /* it seems that 0xFE indicates that a button is still hold
  71. down, while 0xff indicates that no button is hold down. */
  72. if (b == 0xff)
  73. return 0;
  74. if (b == 0xfe)
  75. /* keep old data */
  76. return 1;
  77. *ir_key = b;
  78. return 1;
  79. }
  80. static int em28xx_get_key_em_haup(struct i2c_client *i2c_dev, u32 *ir_key)
  81. {
  82. unsigned char buf[2];
  83. u16 code;
  84. int size;
  85. /* poll IR chip */
  86. size = i2c_master_recv(i2c_dev, buf, sizeof(buf));
  87. if (size != 2)
  88. return -EIO;
  89. /* Does eliminate repeated parity code */
  90. if (buf[1] == 0xff)
  91. return 0;
  92. /*
  93. * Rearranges bits to the right order.
  94. * The bit order were determined experimentally by using
  95. * The original Hauppauge Grey IR and another RC5 that uses addr=0x08
  96. * The RC5 code has 14 bits, but we've experimentally determined
  97. * the meaning for only 11 bits.
  98. * So, the code translation is not complete. Yet, it is enough to
  99. * work with the provided RC5 IR.
  100. */
  101. code =
  102. ((buf[0] & 0x01) ? 0x0020 : 0) | /* 0010 0000 */
  103. ((buf[0] & 0x02) ? 0x0010 : 0) | /* 0001 0000 */
  104. ((buf[0] & 0x04) ? 0x0008 : 0) | /* 0000 1000 */
  105. ((buf[0] & 0x08) ? 0x0004 : 0) | /* 0000 0100 */
  106. ((buf[0] & 0x10) ? 0x0002 : 0) | /* 0000 0010 */
  107. ((buf[0] & 0x20) ? 0x0001 : 0) | /* 0000 0001 */
  108. ((buf[1] & 0x08) ? 0x1000 : 0) | /* 0001 0000 */
  109. ((buf[1] & 0x10) ? 0x0800 : 0) | /* 0000 1000 */
  110. ((buf[1] & 0x20) ? 0x0400 : 0) | /* 0000 0100 */
  111. ((buf[1] & 0x40) ? 0x0200 : 0) | /* 0000 0010 */
  112. ((buf[1] & 0x80) ? 0x0100 : 0); /* 0000 0001 */
  113. /* return key */
  114. *ir_key = code;
  115. return 1;
  116. }
  117. static int em28xx_get_key_pinnacle_usb_grey(struct i2c_client *i2c_dev,
  118. u32 *ir_key)
  119. {
  120. unsigned char buf[3];
  121. /* poll IR chip */
  122. if (3 != i2c_master_recv(i2c_dev, buf, 3))
  123. return -EIO;
  124. if (buf[0] != 0x00)
  125. return 0;
  126. *ir_key = buf[2]&0x3f;
  127. return 1;
  128. }
  129. static int em28xx_get_key_winfast_usbii_deluxe(struct i2c_client *i2c_dev,
  130. u32 *ir_key)
  131. {
  132. unsigned char subaddr, keydetect, key;
  133. struct i2c_msg msg[] = { { .addr = i2c_dev->addr, .flags = 0, .buf = &subaddr, .len = 1},
  134. { .addr = i2c_dev->addr, .flags = I2C_M_RD, .buf = &keydetect, .len = 1} };
  135. subaddr = 0x10;
  136. if (2 != i2c_transfer(i2c_dev->adapter, msg, 2))
  137. return -EIO;
  138. if (keydetect == 0x00)
  139. return 0;
  140. subaddr = 0x00;
  141. msg[1].buf = &key;
  142. if (2 != i2c_transfer(i2c_dev->adapter, msg, 2))
  143. return -EIO;
  144. if (key == 0x00)
  145. return 0;
  146. *ir_key = key;
  147. return 1;
  148. }
  149. /**********************************************************
  150. Poll based get keycode functions
  151. **********************************************************/
  152. /* This is for the em2860/em2880 */
  153. static int default_polling_getkey(struct em28xx_IR *ir,
  154. struct em28xx_ir_poll_result *poll_result)
  155. {
  156. struct em28xx *dev = ir->dev;
  157. int rc;
  158. u8 msg[3] = { 0, 0, 0 };
  159. /* Read key toggle, brand, and key code
  160. on registers 0x45, 0x46 and 0x47
  161. */
  162. rc = dev->em28xx_read_reg_req_len(dev, 0, EM28XX_R45_IR,
  163. msg, sizeof(msg));
  164. if (rc < 0)
  165. return rc;
  166. /* Infrared toggle (Reg 0x45[7]) */
  167. poll_result->toggle_bit = (msg[0] >> 7);
  168. /* Infrared read count (Reg 0x45[6:0] */
  169. poll_result->read_count = (msg[0] & 0x7f);
  170. /* Remote Control Address/Data (Regs 0x46/0x47) */
  171. poll_result->scancode = msg[1] << 8 | msg[2];
  172. return 0;
  173. }
  174. static int em2874_polling_getkey(struct em28xx_IR *ir,
  175. struct em28xx_ir_poll_result *poll_result)
  176. {
  177. struct em28xx *dev = ir->dev;
  178. int rc;
  179. u8 msg[5] = { 0, 0, 0, 0, 0 };
  180. /* Read key toggle, brand, and key code
  181. on registers 0x51-55
  182. */
  183. rc = dev->em28xx_read_reg_req_len(dev, 0, EM2874_R51_IR,
  184. msg, sizeof(msg));
  185. if (rc < 0)
  186. return rc;
  187. /* Infrared toggle (Reg 0x51[7]) */
  188. poll_result->toggle_bit = (msg[0] >> 7);
  189. /* Infrared read count (Reg 0x51[6:0] */
  190. poll_result->read_count = (msg[0] & 0x7f);
  191. /*
  192. * Remote Control Address (Reg 0x52)
  193. * Remote Control Data (Reg 0x53-0x55)
  194. */
  195. switch (ir->rc_type) {
  196. case RC_BIT_RC5:
  197. poll_result->scancode = msg[1] << 8 | msg[2];
  198. break;
  199. case RC_BIT_NEC:
  200. if ((msg[3] ^ msg[4]) != 0xff) /* 32 bits NEC */
  201. poll_result->scancode = (msg[1] << 24) |
  202. (msg[2] << 16) |
  203. (msg[3] << 8) |
  204. msg[4];
  205. else if ((msg[1] ^ msg[2]) != 0xff) /* 24 bits NEC */
  206. poll_result->scancode = (msg[1] << 16) |
  207. (msg[2] << 8) |
  208. msg[3];
  209. else /* Normal NEC */
  210. poll_result->scancode = msg[1] << 8 | msg[3];
  211. break;
  212. case RC_BIT_RC6_0:
  213. poll_result->scancode = msg[1] << 8 | msg[2];
  214. break;
  215. default:
  216. poll_result->scancode = (msg[1] << 24) | (msg[2] << 16) |
  217. (msg[3] << 8) | msg[4];
  218. break;
  219. }
  220. return 0;
  221. }
  222. /**********************************************************
  223. Polling code for em28xx
  224. **********************************************************/
  225. static int em28xx_i2c_ir_handle_key(struct em28xx_IR *ir)
  226. {
  227. struct em28xx *dev = ir->dev;
  228. static u32 ir_key;
  229. int rc;
  230. struct i2c_client client;
  231. client.adapter = &ir->dev->i2c_adap[dev->def_i2c_bus];
  232. client.addr = ir->i2c_dev_addr;
  233. rc = ir->get_key_i2c(&client, &ir_key);
  234. if (rc < 0) {
  235. dprintk("ir->get_key_i2c() failed: %d\n", rc);
  236. return rc;
  237. }
  238. if (rc) {
  239. dprintk("%s: keycode = 0x%04x\n", __func__, ir_key);
  240. rc_keydown(ir->rc, ir_key, 0);
  241. }
  242. return 0;
  243. }
  244. static void em28xx_ir_handle_key(struct em28xx_IR *ir)
  245. {
  246. int result;
  247. struct em28xx_ir_poll_result poll_result;
  248. /* read the registers containing the IR status */
  249. result = ir->get_key(ir, &poll_result);
  250. if (unlikely(result < 0)) {
  251. dprintk("ir->get_key() failed: %d\n", result);
  252. return;
  253. }
  254. if (unlikely(poll_result.read_count != ir->last_readcount)) {
  255. dprintk("%s: toggle: %d, count: %d, key 0x%04x\n", __func__,
  256. poll_result.toggle_bit, poll_result.read_count,
  257. poll_result.scancode);
  258. if (ir->full_code)
  259. rc_keydown(ir->rc,
  260. poll_result.scancode,
  261. poll_result.toggle_bit);
  262. else
  263. rc_keydown(ir->rc,
  264. poll_result.scancode & 0xff,
  265. poll_result.toggle_bit);
  266. if (ir->dev->chip_id == CHIP_ID_EM2874 ||
  267. ir->dev->chip_id == CHIP_ID_EM2884)
  268. /* The em2874 clears the readcount field every time the
  269. register is read. The em2860/2880 datasheet says that it
  270. is supposed to clear the readcount, but it doesn't. So with
  271. the em2874, we are looking for a non-zero read count as
  272. opposed to a readcount that is incrementing */
  273. ir->last_readcount = 0;
  274. else
  275. ir->last_readcount = poll_result.read_count;
  276. }
  277. }
  278. static void em28xx_ir_work(struct work_struct *work)
  279. {
  280. struct em28xx_IR *ir = container_of(work, struct em28xx_IR, work.work);
  281. if (ir->i2c_dev_addr) /* external i2c device */
  282. em28xx_i2c_ir_handle_key(ir);
  283. else /* internal device */
  284. em28xx_ir_handle_key(ir);
  285. schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
  286. }
  287. static int em28xx_ir_start(struct rc_dev *rc)
  288. {
  289. struct em28xx_IR *ir = rc->priv;
  290. INIT_DELAYED_WORK(&ir->work, em28xx_ir_work);
  291. schedule_delayed_work(&ir->work, 0);
  292. return 0;
  293. }
  294. static void em28xx_ir_stop(struct rc_dev *rc)
  295. {
  296. struct em28xx_IR *ir = rc->priv;
  297. cancel_delayed_work_sync(&ir->work);
  298. }
  299. static int em2860_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_type)
  300. {
  301. struct em28xx_IR *ir = rc_dev->priv;
  302. struct em28xx *dev = ir->dev;
  303. /* Adjust xclk based on IR table for RC5/NEC tables */
  304. if (*rc_type & RC_BIT_RC5) {
  305. dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
  306. ir->full_code = 1;
  307. *rc_type = RC_BIT_RC5;
  308. } else if (*rc_type & RC_BIT_NEC) {
  309. dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
  310. ir->full_code = 1;
  311. *rc_type = RC_BIT_NEC;
  312. } else if (*rc_type & RC_BIT_UNKNOWN) {
  313. *rc_type = RC_BIT_UNKNOWN;
  314. } else {
  315. *rc_type = ir->rc_type;
  316. return -EINVAL;
  317. }
  318. em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
  319. EM28XX_XCLK_IR_RC5_MODE);
  320. ir->rc_type = *rc_type;
  321. return 0;
  322. }
  323. static int em2874_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_type)
  324. {
  325. struct em28xx_IR *ir = rc_dev->priv;
  326. struct em28xx *dev = ir->dev;
  327. u8 ir_config = EM2874_IR_RC5;
  328. /* Adjust xclk and set type based on IR table for RC5/NEC/RC6 tables */
  329. if (*rc_type & RC_BIT_RC5) {
  330. dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
  331. ir->full_code = 1;
  332. *rc_type = RC_BIT_RC5;
  333. } else if (*rc_type & RC_BIT_NEC) {
  334. dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
  335. ir_config = EM2874_IR_NEC | EM2874_IR_NEC_NO_PARITY;
  336. ir->full_code = 1;
  337. *rc_type = RC_BIT_NEC;
  338. } else if (*rc_type & RC_BIT_RC6_0) {
  339. dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
  340. ir_config = EM2874_IR_RC6_MODE_0;
  341. ir->full_code = 1;
  342. *rc_type = RC_BIT_RC6_0;
  343. } else if (*rc_type & RC_BIT_UNKNOWN) {
  344. *rc_type = RC_BIT_UNKNOWN;
  345. } else {
  346. *rc_type = ir->rc_type;
  347. return -EINVAL;
  348. }
  349. em28xx_write_regs(dev, EM2874_R50_IR_CONFIG, &ir_config, 1);
  350. em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
  351. EM28XX_XCLK_IR_RC5_MODE);
  352. ir->rc_type = *rc_type;
  353. return 0;
  354. }
  355. static int em28xx_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_type)
  356. {
  357. struct em28xx_IR *ir = rc_dev->priv;
  358. struct em28xx *dev = ir->dev;
  359. /* Setup the proper handler based on the chip */
  360. switch (dev->chip_id) {
  361. case CHIP_ID_EM2860:
  362. case CHIP_ID_EM2883:
  363. return em2860_ir_change_protocol(rc_dev, rc_type);
  364. case CHIP_ID_EM2884:
  365. case CHIP_ID_EM2874:
  366. case CHIP_ID_EM28174:
  367. return em2874_ir_change_protocol(rc_dev, rc_type);
  368. default:
  369. printk("Unrecognized em28xx chip id 0x%02x: IR not supported\n",
  370. dev->chip_id);
  371. return -EINVAL;
  372. }
  373. }
  374. static int em28xx_probe_i2c_ir(struct em28xx *dev)
  375. {
  376. int i = 0;
  377. /* Leadtek winfast tv USBII deluxe can find a non working IR-device */
  378. /* at address 0x18, so if that address is needed for another board in */
  379. /* the future, please put it after 0x1f. */
  380. const unsigned short addr_list[] = {
  381. 0x1f, 0x30, 0x47, I2C_CLIENT_END
  382. };
  383. while (addr_list[i] != I2C_CLIENT_END) {
  384. if (i2c_probe_func_quick_read(&dev->i2c_adap[dev->def_i2c_bus], addr_list[i]) == 1)
  385. return addr_list[i];
  386. i++;
  387. }
  388. return -ENODEV;
  389. }
  390. /**********************************************************
  391. Handle Webcam snapshot button
  392. **********************************************************/
  393. static void em28xx_query_sbutton(struct work_struct *work)
  394. {
  395. /* Poll the register and see if the button is depressed */
  396. struct em28xx *dev =
  397. container_of(work, struct em28xx, sbutton_query_work.work);
  398. int ret;
  399. ret = em28xx_read_reg(dev, EM28XX_R0C_USBSUSP);
  400. if (ret & EM28XX_R0C_USBSUSP_SNAPSHOT) {
  401. u8 cleared;
  402. /* Button is depressed, clear the register */
  403. cleared = ((u8) ret) & ~EM28XX_R0C_USBSUSP_SNAPSHOT;
  404. em28xx_write_regs(dev, EM28XX_R0C_USBSUSP, &cleared, 1);
  405. /* Not emulate the keypress */
  406. input_report_key(dev->sbutton_input_dev, EM28XX_SNAPSHOT_KEY,
  407. 1);
  408. /* Now unpress the key */
  409. input_report_key(dev->sbutton_input_dev, EM28XX_SNAPSHOT_KEY,
  410. 0);
  411. }
  412. /* Schedule next poll */
  413. schedule_delayed_work(&dev->sbutton_query_work,
  414. msecs_to_jiffies(EM28XX_SBUTTON_QUERY_INTERVAL));
  415. }
  416. static void em28xx_register_snapshot_button(struct em28xx *dev)
  417. {
  418. struct input_dev *input_dev;
  419. int err;
  420. em28xx_info("Registering snapshot button...\n");
  421. input_dev = input_allocate_device();
  422. if (!input_dev) {
  423. em28xx_errdev("input_allocate_device failed\n");
  424. return;
  425. }
  426. usb_make_path(dev->udev, dev->snapshot_button_path,
  427. sizeof(dev->snapshot_button_path));
  428. strlcat(dev->snapshot_button_path, "/sbutton",
  429. sizeof(dev->snapshot_button_path));
  430. INIT_DELAYED_WORK(&dev->sbutton_query_work, em28xx_query_sbutton);
  431. input_dev->name = "em28xx snapshot button";
  432. input_dev->phys = dev->snapshot_button_path;
  433. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
  434. set_bit(EM28XX_SNAPSHOT_KEY, input_dev->keybit);
  435. input_dev->keycodesize = 0;
  436. input_dev->keycodemax = 0;
  437. input_dev->id.bustype = BUS_USB;
  438. input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
  439. input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
  440. input_dev->id.version = 1;
  441. input_dev->dev.parent = &dev->udev->dev;
  442. err = input_register_device(input_dev);
  443. if (err) {
  444. em28xx_errdev("input_register_device failed\n");
  445. input_free_device(input_dev);
  446. return;
  447. }
  448. dev->sbutton_input_dev = input_dev;
  449. schedule_delayed_work(&dev->sbutton_query_work,
  450. msecs_to_jiffies(EM28XX_SBUTTON_QUERY_INTERVAL));
  451. return;
  452. }
  453. static void em28xx_deregister_snapshot_button(struct em28xx *dev)
  454. {
  455. if (dev->sbutton_input_dev != NULL) {
  456. em28xx_info("Deregistering snapshot button\n");
  457. cancel_delayed_work_sync(&dev->sbutton_query_work);
  458. input_unregister_device(dev->sbutton_input_dev);
  459. dev->sbutton_input_dev = NULL;
  460. }
  461. return;
  462. }
  463. static int em28xx_ir_init(struct em28xx *dev)
  464. {
  465. struct em28xx_IR *ir;
  466. struct rc_dev *rc;
  467. int err = -ENOMEM;
  468. u64 rc_type;
  469. u16 i2c_rc_dev_addr = 0;
  470. if (dev->board.has_snapshot_button)
  471. em28xx_register_snapshot_button(dev);
  472. if (dev->board.has_ir_i2c) {
  473. i2c_rc_dev_addr = em28xx_probe_i2c_ir(dev);
  474. if (!i2c_rc_dev_addr) {
  475. dev->board.has_ir_i2c = 0;
  476. em28xx_warn("No i2c IR remote control device found.\n");
  477. return -ENODEV;
  478. }
  479. }
  480. if (dev->board.ir_codes == NULL && !dev->board.has_ir_i2c) {
  481. /* No remote control support */
  482. em28xx_warn("Remote control support is not available for "
  483. "this card.\n");
  484. return 0;
  485. }
  486. ir = kzalloc(sizeof(*ir), GFP_KERNEL);
  487. rc = rc_allocate_device();
  488. if (!ir || !rc)
  489. goto error;
  490. /* record handles to ourself */
  491. ir->dev = dev;
  492. dev->ir = ir;
  493. ir->rc = rc;
  494. rc->priv = ir;
  495. rc->open = em28xx_ir_start;
  496. rc->close = em28xx_ir_stop;
  497. if (dev->board.has_ir_i2c) { /* external i2c device */
  498. switch (dev->model) {
  499. case EM2800_BOARD_TERRATEC_CINERGY_200:
  500. case EM2820_BOARD_TERRATEC_CINERGY_250:
  501. rc->map_name = RC_MAP_EM_TERRATEC;
  502. ir->get_key_i2c = em28xx_get_key_terratec;
  503. break;
  504. case EM2820_BOARD_PINNACLE_USB_2:
  505. rc->map_name = RC_MAP_PINNACLE_GREY;
  506. ir->get_key_i2c = em28xx_get_key_pinnacle_usb_grey;
  507. break;
  508. case EM2820_BOARD_HAUPPAUGE_WINTV_USB_2:
  509. rc->map_name = RC_MAP_HAUPPAUGE;
  510. ir->get_key_i2c = em28xx_get_key_em_haup;
  511. rc->allowed_protos = RC_BIT_RC5;
  512. break;
  513. case EM2820_BOARD_LEADTEK_WINFAST_USBII_DELUXE:
  514. rc->map_name = RC_MAP_WINFAST_USBII_DELUXE;
  515. ir->get_key_i2c = em28xx_get_key_winfast_usbii_deluxe;
  516. break;
  517. default:
  518. err = -ENODEV;
  519. goto error;
  520. }
  521. ir->i2c_dev_addr = i2c_rc_dev_addr;
  522. } else { /* internal device */
  523. switch (dev->chip_id) {
  524. case CHIP_ID_EM2860:
  525. case CHIP_ID_EM2883:
  526. rc->allowed_protos = RC_BIT_RC5 | RC_BIT_NEC;
  527. ir->get_key = default_polling_getkey;
  528. break;
  529. case CHIP_ID_EM2884:
  530. case CHIP_ID_EM2874:
  531. case CHIP_ID_EM28174:
  532. ir->get_key = em2874_polling_getkey;
  533. rc->allowed_protos = RC_BIT_RC5 | RC_BIT_NEC |
  534. RC_BIT_RC6_0;
  535. break;
  536. default:
  537. err = -ENODEV;
  538. goto error;
  539. }
  540. rc->change_protocol = em28xx_ir_change_protocol;
  541. rc->map_name = dev->board.ir_codes;
  542. /* By default, keep protocol field untouched */
  543. rc_type = RC_BIT_UNKNOWN;
  544. err = em28xx_ir_change_protocol(rc, &rc_type);
  545. if (err)
  546. goto error;
  547. }
  548. /* This is how often we ask the chip for IR information */
  549. ir->polling = 100; /* ms */
  550. /* init input device */
  551. snprintf(ir->name, sizeof(ir->name), "em28xx IR (%s)", dev->name);
  552. usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
  553. strlcat(ir->phys, "/input0", sizeof(ir->phys));
  554. rc->input_name = ir->name;
  555. rc->input_phys = ir->phys;
  556. rc->input_id.bustype = BUS_USB;
  557. rc->input_id.version = 1;
  558. rc->input_id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
  559. rc->input_id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
  560. rc->dev.parent = &dev->udev->dev;
  561. rc->driver_name = MODULE_NAME;
  562. /* all done */
  563. err = rc_register_device(rc);
  564. if (err)
  565. goto error;
  566. return 0;
  567. error:
  568. dev->ir = NULL;
  569. rc_free_device(rc);
  570. kfree(ir);
  571. return err;
  572. }
  573. static int em28xx_ir_fini(struct em28xx *dev)
  574. {
  575. struct em28xx_IR *ir = dev->ir;
  576. em28xx_deregister_snapshot_button(dev);
  577. /* skip detach on non attached boards */
  578. if (!ir)
  579. return 0;
  580. if (ir->rc)
  581. rc_unregister_device(ir->rc);
  582. /* done */
  583. kfree(ir);
  584. dev->ir = NULL;
  585. return 0;
  586. }
  587. static struct em28xx_ops rc_ops = {
  588. .id = EM28XX_RC,
  589. .name = "Em28xx Input Extension",
  590. .init = em28xx_ir_init,
  591. .fini = em28xx_ir_fini,
  592. };
  593. static int __init em28xx_rc_register(void)
  594. {
  595. return em28xx_register_extension(&rc_ops);
  596. }
  597. static void __exit em28xx_rc_unregister(void)
  598. {
  599. em28xx_unregister_extension(&rc_ops);
  600. }
  601. MODULE_LICENSE("GPL");
  602. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  603. MODULE_DESCRIPTION("Em28xx Input driver");
  604. module_init(em28xx_rc_register);
  605. module_exit(em28xx_rc_unregister);