em28xx-input.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // handle em28xx IR remotes via linux kernel input layer.
  4. //
  5. // Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
  6. // Markus Rechberger <mrechberger@gmail.com>
  7. // Mauro Carvalho Chehab <mchehab@kernel.org>
  8. // Sascha Sommer <saschasommer@freenet.de>
  9. //
  10. // This program is free software; you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation; either version 2 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. #include "em28xx.h"
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/delay.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/usb.h>
  25. #include <linux/slab.h>
  26. #include <linux/bitrev.h>
  27. #define EM28XX_SNAPSHOT_KEY KEY_CAMERA
  28. #define EM28XX_BUTTONS_DEBOUNCED_QUERY_INTERVAL 500 /* [ms] */
  29. #define EM28XX_BUTTONS_VOLATILE_QUERY_INTERVAL 100 /* [ms] */
  30. static unsigned int ir_debug;
  31. module_param(ir_debug, int, 0644);
  32. MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
  33. #define MODULE_NAME "em28xx"
  34. #define dprintk(fmt, arg...) do { \
  35. if (ir_debug) \
  36. dev_printk(KERN_DEBUG, &ir->dev->intf->dev, \
  37. "input: %s: " fmt, __func__, ## arg); \
  38. } while (0)
  39. /*
  40. * Polling structure used by em28xx IR's
  41. */
  42. struct em28xx_ir_poll_result {
  43. unsigned int toggle_bit:1;
  44. unsigned int read_count:7;
  45. enum rc_proto protocol;
  46. u32 scancode;
  47. };
  48. struct em28xx_IR {
  49. struct em28xx *dev;
  50. struct rc_dev *rc;
  51. char name[32];
  52. char phys[32];
  53. /* poll decoder */
  54. int polling;
  55. struct delayed_work work;
  56. unsigned int full_code:1;
  57. unsigned int last_readcount;
  58. u64 rc_proto;
  59. struct i2c_client *i2c_client;
  60. int (*get_key_i2c)(struct i2c_client *ir, enum rc_proto *protocol,
  61. u32 *scancode);
  62. int (*get_key)(struct em28xx_IR *ir, struct em28xx_ir_poll_result *r);
  63. };
  64. /*
  65. * I2C IR based get keycodes - should be used with ir-kbd-i2c
  66. */
  67. static int em28xx_get_key_terratec(struct i2c_client *i2c_dev,
  68. enum rc_proto *protocol, u32 *scancode)
  69. {
  70. int rc;
  71. unsigned char b;
  72. /* poll IR chip */
  73. rc = i2c_master_recv(i2c_dev, &b, 1);
  74. if (rc != 1) {
  75. if (rc < 0)
  76. return rc;
  77. return -EIO;
  78. }
  79. /*
  80. * it seems that 0xFE indicates that a button is still hold
  81. * down, while 0xff indicates that no button is hold down.
  82. */
  83. if (b == 0xff)
  84. return 0;
  85. if (b == 0xfe)
  86. /* keep old data */
  87. return 1;
  88. *protocol = RC_PROTO_UNKNOWN;
  89. *scancode = b;
  90. return 1;
  91. }
  92. static int em28xx_get_key_em_haup(struct i2c_client *i2c_dev,
  93. enum rc_proto *protocol, u32 *scancode)
  94. {
  95. unsigned char buf[2];
  96. int size;
  97. /* poll IR chip */
  98. size = i2c_master_recv(i2c_dev, buf, sizeof(buf));
  99. if (size != 2)
  100. return -EIO;
  101. /* Does eliminate repeated parity code */
  102. if (buf[1] == 0xff)
  103. return 0;
  104. /*
  105. * Rearranges bits to the right order.
  106. * The bit order were determined experimentally by using
  107. * The original Hauppauge Grey IR and another RC5 that uses addr=0x08
  108. * The RC5 code has 14 bits, but we've experimentally determined
  109. * the meaning for only 11 bits.
  110. * So, the code translation is not complete. Yet, it is enough to
  111. * work with the provided RC5 IR.
  112. */
  113. *protocol = RC_PROTO_RC5;
  114. *scancode = (bitrev8(buf[1]) & 0x1f) << 8 | bitrev8(buf[0]) >> 2;
  115. return 1;
  116. }
  117. static int em28xx_get_key_pinnacle_usb_grey(struct i2c_client *i2c_dev,
  118. enum rc_proto *protocol,
  119. u32 *scancode)
  120. {
  121. unsigned char buf[3];
  122. /* poll IR chip */
  123. if (i2c_master_recv(i2c_dev, buf, 3) != 3)
  124. return -EIO;
  125. if (buf[0] != 0x00)
  126. return 0;
  127. *protocol = RC_PROTO_UNKNOWN;
  128. *scancode = buf[2] & 0x3f;
  129. return 1;
  130. }
  131. static int em28xx_get_key_winfast_usbii_deluxe(struct i2c_client *i2c_dev,
  132. enum rc_proto *protocol,
  133. u32 *scancode)
  134. {
  135. unsigned char subaddr, keydetect, key;
  136. struct i2c_msg msg[] = {
  137. {
  138. .addr = i2c_dev->addr,
  139. .flags = 0,
  140. .buf = &subaddr, .len = 1
  141. }, {
  142. .addr = i2c_dev->addr,
  143. .flags = I2C_M_RD,
  144. .buf = &keydetect,
  145. .len = 1
  146. }
  147. };
  148. subaddr = 0x10;
  149. if (i2c_transfer(i2c_dev->adapter, msg, 2) != 2)
  150. return -EIO;
  151. if (keydetect == 0x00)
  152. return 0;
  153. subaddr = 0x00;
  154. msg[1].buf = &key;
  155. if (i2c_transfer(i2c_dev->adapter, msg, 2) != 2)
  156. return -EIO;
  157. if (key == 0x00)
  158. return 0;
  159. *protocol = RC_PROTO_UNKNOWN;
  160. *scancode = key;
  161. return 1;
  162. }
  163. /*
  164. * Poll based get keycode functions
  165. */
  166. /* This is for the em2860/em2880 */
  167. static int default_polling_getkey(struct em28xx_IR *ir,
  168. struct em28xx_ir_poll_result *poll_result)
  169. {
  170. struct em28xx *dev = ir->dev;
  171. int rc;
  172. u8 msg[3] = { 0, 0, 0 };
  173. /*
  174. * Read key toggle, brand, and key code
  175. * on registers 0x45, 0x46 and 0x47
  176. */
  177. rc = dev->em28xx_read_reg_req_len(dev, 0, EM28XX_R45_IR,
  178. msg, sizeof(msg));
  179. if (rc < 0)
  180. return rc;
  181. /* Infrared toggle (Reg 0x45[7]) */
  182. poll_result->toggle_bit = (msg[0] >> 7);
  183. /* Infrared read count (Reg 0x45[6:0] */
  184. poll_result->read_count = (msg[0] & 0x7f);
  185. /* Remote Control Address/Data (Regs 0x46/0x47) */
  186. switch (ir->rc_proto) {
  187. case RC_PROTO_BIT_RC5:
  188. poll_result->protocol = RC_PROTO_RC5;
  189. poll_result->scancode = RC_SCANCODE_RC5(msg[1], msg[2]);
  190. break;
  191. case RC_PROTO_BIT_NEC:
  192. poll_result->protocol = RC_PROTO_NEC;
  193. poll_result->scancode = RC_SCANCODE_NEC(msg[1], msg[2]);
  194. break;
  195. default:
  196. poll_result->protocol = RC_PROTO_UNKNOWN;
  197. poll_result->scancode = msg[1] << 8 | msg[2];
  198. break;
  199. }
  200. return 0;
  201. }
  202. static int em2874_polling_getkey(struct em28xx_IR *ir,
  203. struct em28xx_ir_poll_result *poll_result)
  204. {
  205. struct em28xx *dev = ir->dev;
  206. int rc;
  207. u8 msg[5] = { 0, 0, 0, 0, 0 };
  208. /*
  209. * Read key toggle, brand, and key code
  210. * on registers 0x51-55
  211. */
  212. rc = dev->em28xx_read_reg_req_len(dev, 0, EM2874_R51_IR,
  213. msg, sizeof(msg));
  214. if (rc < 0)
  215. return rc;
  216. /* Infrared toggle (Reg 0x51[7]) */
  217. poll_result->toggle_bit = (msg[0] >> 7);
  218. /* Infrared read count (Reg 0x51[6:0] */
  219. poll_result->read_count = (msg[0] & 0x7f);
  220. /*
  221. * Remote Control Address (Reg 0x52)
  222. * Remote Control Data (Reg 0x53-0x55)
  223. */
  224. switch (ir->rc_proto) {
  225. case RC_PROTO_BIT_RC5:
  226. poll_result->protocol = RC_PROTO_RC5;
  227. poll_result->scancode = RC_SCANCODE_RC5(msg[1], msg[2]);
  228. break;
  229. case RC_PROTO_BIT_NEC:
  230. poll_result->scancode = msg[1] << 8 | msg[2];
  231. if ((msg[3] ^ msg[4]) != 0xff) { /* 32 bits NEC */
  232. poll_result->protocol = RC_PROTO_NEC32;
  233. poll_result->scancode = RC_SCANCODE_NEC32((msg[1] << 24) |
  234. (msg[2] << 16) |
  235. (msg[3] << 8) |
  236. (msg[4]));
  237. } else if ((msg[1] ^ msg[2]) != 0xff) { /* 24 bits NEC */
  238. poll_result->protocol = RC_PROTO_NECX;
  239. poll_result->scancode = RC_SCANCODE_NECX(msg[1] << 8 |
  240. msg[2], msg[3]);
  241. } else { /* Normal NEC */
  242. poll_result->protocol = RC_PROTO_NEC;
  243. poll_result->scancode = RC_SCANCODE_NEC(msg[1], msg[3]);
  244. }
  245. break;
  246. case RC_PROTO_BIT_RC6_0:
  247. poll_result->protocol = RC_PROTO_RC6_0;
  248. poll_result->scancode = RC_SCANCODE_RC6_0(msg[1], msg[2]);
  249. break;
  250. default:
  251. poll_result->protocol = RC_PROTO_UNKNOWN;
  252. poll_result->scancode = (msg[1] << 24) | (msg[2] << 16) |
  253. (msg[3] << 8) | msg[4];
  254. break;
  255. }
  256. return 0;
  257. }
  258. /*
  259. * Polling code for em28xx
  260. */
  261. static int em28xx_i2c_ir_handle_key(struct em28xx_IR *ir)
  262. {
  263. static u32 scancode;
  264. enum rc_proto protocol;
  265. int rc;
  266. rc = ir->get_key_i2c(ir->i2c_client, &protocol, &scancode);
  267. if (rc < 0) {
  268. dprintk("ir->get_key_i2c() failed: %d\n", rc);
  269. return rc;
  270. }
  271. if (rc) {
  272. dprintk("%s: proto = 0x%04x, scancode = 0x%04x\n",
  273. __func__, protocol, scancode);
  274. rc_keydown(ir->rc, protocol, scancode, 0);
  275. }
  276. return 0;
  277. }
  278. static void em28xx_ir_handle_key(struct em28xx_IR *ir)
  279. {
  280. int result;
  281. struct em28xx_ir_poll_result poll_result;
  282. /* read the registers containing the IR status */
  283. result = ir->get_key(ir, &poll_result);
  284. if (unlikely(result < 0)) {
  285. dprintk("ir->get_key() failed: %d\n", result);
  286. return;
  287. }
  288. if (unlikely(poll_result.read_count != ir->last_readcount)) {
  289. dprintk("%s: toggle: %d, count: %d, key 0x%04x\n", __func__,
  290. poll_result.toggle_bit, poll_result.read_count,
  291. poll_result.scancode);
  292. if (ir->full_code)
  293. rc_keydown(ir->rc,
  294. poll_result.protocol,
  295. poll_result.scancode,
  296. poll_result.toggle_bit);
  297. else
  298. rc_keydown(ir->rc,
  299. RC_PROTO_UNKNOWN,
  300. poll_result.scancode & 0xff,
  301. poll_result.toggle_bit);
  302. if (ir->dev->chip_id == CHIP_ID_EM2874 ||
  303. ir->dev->chip_id == CHIP_ID_EM2884)
  304. /*
  305. * The em2874 clears the readcount field every time the
  306. * register is read. The em2860/2880 datasheet says
  307. * that it is supposed to clear the readcount, but it
  308. * doesn't. So with the em2874, we are looking for a
  309. * non-zero read count as opposed to a readcount
  310. * that is incrementing
  311. */
  312. ir->last_readcount = 0;
  313. else
  314. ir->last_readcount = poll_result.read_count;
  315. }
  316. }
  317. static void em28xx_ir_work(struct work_struct *work)
  318. {
  319. struct em28xx_IR *ir = container_of(work, struct em28xx_IR, work.work);
  320. if (ir->i2c_client) /* external i2c device */
  321. em28xx_i2c_ir_handle_key(ir);
  322. else /* internal device */
  323. em28xx_ir_handle_key(ir);
  324. schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
  325. }
  326. static int em28xx_ir_start(struct rc_dev *rc)
  327. {
  328. struct em28xx_IR *ir = rc->priv;
  329. INIT_DELAYED_WORK(&ir->work, em28xx_ir_work);
  330. schedule_delayed_work(&ir->work, 0);
  331. return 0;
  332. }
  333. static void em28xx_ir_stop(struct rc_dev *rc)
  334. {
  335. struct em28xx_IR *ir = rc->priv;
  336. cancel_delayed_work_sync(&ir->work);
  337. }
  338. static int em2860_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_proto)
  339. {
  340. struct em28xx_IR *ir = rc_dev->priv;
  341. struct em28xx *dev = ir->dev;
  342. /* Adjust xclk based on IR table for RC5/NEC tables */
  343. if (*rc_proto & RC_PROTO_BIT_RC5) {
  344. dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
  345. ir->full_code = 1;
  346. *rc_proto = RC_PROTO_BIT_RC5;
  347. } else if (*rc_proto & RC_PROTO_BIT_NEC) {
  348. dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
  349. ir->full_code = 1;
  350. *rc_proto = RC_PROTO_BIT_NEC;
  351. } else if (*rc_proto & RC_PROTO_BIT_UNKNOWN) {
  352. *rc_proto = RC_PROTO_BIT_UNKNOWN;
  353. } else {
  354. *rc_proto = ir->rc_proto;
  355. return -EINVAL;
  356. }
  357. em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
  358. EM28XX_XCLK_IR_RC5_MODE);
  359. ir->rc_proto = *rc_proto;
  360. return 0;
  361. }
  362. static int em2874_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_proto)
  363. {
  364. struct em28xx_IR *ir = rc_dev->priv;
  365. struct em28xx *dev = ir->dev;
  366. u8 ir_config = EM2874_IR_RC5;
  367. /* Adjust xclk and set type based on IR table for RC5/NEC/RC6 tables */
  368. if (*rc_proto & RC_PROTO_BIT_RC5) {
  369. dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
  370. ir->full_code = 1;
  371. *rc_proto = RC_PROTO_BIT_RC5;
  372. } else if (*rc_proto & RC_PROTO_BIT_NEC) {
  373. dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
  374. ir_config = EM2874_IR_NEC | EM2874_IR_NEC_NO_PARITY;
  375. ir->full_code = 1;
  376. *rc_proto = RC_PROTO_BIT_NEC;
  377. } else if (*rc_proto & RC_PROTO_BIT_RC6_0) {
  378. dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
  379. ir_config = EM2874_IR_RC6_MODE_0;
  380. ir->full_code = 1;
  381. *rc_proto = RC_PROTO_BIT_RC6_0;
  382. } else if (*rc_proto & RC_PROTO_BIT_UNKNOWN) {
  383. *rc_proto = RC_PROTO_BIT_UNKNOWN;
  384. } else {
  385. *rc_proto = ir->rc_proto;
  386. return -EINVAL;
  387. }
  388. em28xx_write_regs(dev, EM2874_R50_IR_CONFIG, &ir_config, 1);
  389. em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
  390. EM28XX_XCLK_IR_RC5_MODE);
  391. ir->rc_proto = *rc_proto;
  392. return 0;
  393. }
  394. static int em28xx_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_proto)
  395. {
  396. struct em28xx_IR *ir = rc_dev->priv;
  397. struct em28xx *dev = ir->dev;
  398. /* Setup the proper handler based on the chip */
  399. switch (dev->chip_id) {
  400. case CHIP_ID_EM2860:
  401. case CHIP_ID_EM2883:
  402. return em2860_ir_change_protocol(rc_dev, rc_proto);
  403. case CHIP_ID_EM2884:
  404. case CHIP_ID_EM2874:
  405. case CHIP_ID_EM28174:
  406. case CHIP_ID_EM28178:
  407. return em2874_ir_change_protocol(rc_dev, rc_proto);
  408. default:
  409. dev_err(&ir->dev->intf->dev,
  410. "Unrecognized em28xx chip id 0x%02x: IR not supported\n",
  411. dev->chip_id);
  412. return -EINVAL;
  413. }
  414. }
  415. static int em28xx_probe_i2c_ir(struct em28xx *dev)
  416. {
  417. int i = 0;
  418. /*
  419. * Leadtek winfast tv USBII deluxe can find a non working IR-device
  420. * at address 0x18, so if that address is needed for another board in
  421. * the future, please put it after 0x1f.
  422. */
  423. const unsigned short addr_list[] = {
  424. 0x1f, 0x30, 0x47, I2C_CLIENT_END
  425. };
  426. while (addr_list[i] != I2C_CLIENT_END) {
  427. if (i2c_probe_func_quick_read(&dev->i2c_adap[dev->def_i2c_bus],
  428. addr_list[i]) == 1)
  429. return addr_list[i];
  430. i++;
  431. }
  432. return -ENODEV;
  433. }
  434. /*
  435. * Handle buttons
  436. */
  437. static void em28xx_query_buttons(struct work_struct *work)
  438. {
  439. struct em28xx *dev =
  440. container_of(work, struct em28xx, buttons_query_work.work);
  441. u8 i, j;
  442. int regval;
  443. bool is_pressed, was_pressed;
  444. const struct em28xx_led *led;
  445. /* Poll and evaluate all addresses */
  446. for (i = 0; i < dev->num_button_polling_addresses; i++) {
  447. /* Read value from register */
  448. regval = em28xx_read_reg(dev, dev->button_polling_addresses[i]);
  449. if (regval < 0)
  450. continue;
  451. /* Check states of the buttons and act */
  452. j = 0;
  453. while (dev->board.buttons[j].role >= 0 &&
  454. dev->board.buttons[j].role < EM28XX_NUM_BUTTON_ROLES) {
  455. const struct em28xx_button *button;
  456. button = &dev->board.buttons[j];
  457. /* Check if button uses the current address */
  458. if (button->reg_r != dev->button_polling_addresses[i]) {
  459. j++;
  460. continue;
  461. }
  462. /* Determine if button is and was pressed last time */
  463. is_pressed = regval & button->mask;
  464. was_pressed = dev->button_polling_last_values[i]
  465. & button->mask;
  466. if (button->inverted) {
  467. is_pressed = !is_pressed;
  468. was_pressed = !was_pressed;
  469. }
  470. /* Clear button state (if needed) */
  471. if (is_pressed && button->reg_clearing)
  472. em28xx_write_reg(dev, button->reg_clearing,
  473. (~regval & button->mask)
  474. | (regval & ~button->mask));
  475. /* Handle button state */
  476. if (!is_pressed || was_pressed) {
  477. j++;
  478. continue;
  479. }
  480. switch (button->role) {
  481. case EM28XX_BUTTON_SNAPSHOT:
  482. /* Emulate the keypress */
  483. input_report_key(dev->sbutton_input_dev,
  484. EM28XX_SNAPSHOT_KEY, 1);
  485. /* Unpress the key */
  486. input_report_key(dev->sbutton_input_dev,
  487. EM28XX_SNAPSHOT_KEY, 0);
  488. break;
  489. case EM28XX_BUTTON_ILLUMINATION:
  490. led = em28xx_find_led(dev,
  491. EM28XX_LED_ILLUMINATION);
  492. /* Switch illumination LED on/off */
  493. if (led)
  494. em28xx_toggle_reg_bits(dev,
  495. led->gpio_reg,
  496. led->gpio_mask);
  497. break;
  498. default:
  499. WARN_ONCE(1, "BUG: unhandled button role.");
  500. }
  501. /* Next button */
  502. j++;
  503. }
  504. /* Save current value for comparison during the next polling */
  505. dev->button_polling_last_values[i] = regval;
  506. }
  507. /* Schedule next poll */
  508. schedule_delayed_work(&dev->buttons_query_work,
  509. msecs_to_jiffies(dev->button_polling_interval));
  510. }
  511. static int em28xx_register_snapshot_button(struct em28xx *dev)
  512. {
  513. struct usb_device *udev = interface_to_usbdev(dev->intf);
  514. struct input_dev *input_dev;
  515. int err;
  516. dev_info(&dev->intf->dev, "Registering snapshot button...\n");
  517. input_dev = input_allocate_device();
  518. if (!input_dev)
  519. return -ENOMEM;
  520. usb_make_path(udev, dev->snapshot_button_path,
  521. sizeof(dev->snapshot_button_path));
  522. strlcat(dev->snapshot_button_path, "/sbutton",
  523. sizeof(dev->snapshot_button_path));
  524. input_dev->name = "em28xx snapshot button";
  525. input_dev->phys = dev->snapshot_button_path;
  526. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
  527. set_bit(EM28XX_SNAPSHOT_KEY, input_dev->keybit);
  528. input_dev->keycodesize = 0;
  529. input_dev->keycodemax = 0;
  530. input_dev->id.bustype = BUS_USB;
  531. input_dev->id.vendor = le16_to_cpu(udev->descriptor.idVendor);
  532. input_dev->id.product = le16_to_cpu(udev->descriptor.idProduct);
  533. input_dev->id.version = 1;
  534. input_dev->dev.parent = &dev->intf->dev;
  535. err = input_register_device(input_dev);
  536. if (err) {
  537. dev_err(&dev->intf->dev, "input_register_device failed\n");
  538. input_free_device(input_dev);
  539. return err;
  540. }
  541. dev->sbutton_input_dev = input_dev;
  542. return 0;
  543. }
  544. static void em28xx_init_buttons(struct em28xx *dev)
  545. {
  546. u8 i = 0, j = 0;
  547. bool addr_new = false;
  548. dev->button_polling_interval = EM28XX_BUTTONS_DEBOUNCED_QUERY_INTERVAL;
  549. while (dev->board.buttons[i].role >= 0 &&
  550. dev->board.buttons[i].role < EM28XX_NUM_BUTTON_ROLES) {
  551. const struct em28xx_button *button = &dev->board.buttons[i];
  552. /* Check if polling address is already on the list */
  553. addr_new = true;
  554. for (j = 0; j < dev->num_button_polling_addresses; j++) {
  555. if (button->reg_r == dev->button_polling_addresses[j]) {
  556. addr_new = false;
  557. break;
  558. }
  559. }
  560. /* Check if max. number of polling addresses is exceeded */
  561. if (addr_new && dev->num_button_polling_addresses
  562. >= EM28XX_NUM_BUTTON_ADDRESSES_MAX) {
  563. WARN_ONCE(1, "BUG: maximum number of button polling addresses exceeded.");
  564. goto next_button;
  565. }
  566. /* Button role specific checks and actions */
  567. if (button->role == EM28XX_BUTTON_SNAPSHOT) {
  568. /* Register input device */
  569. if (em28xx_register_snapshot_button(dev) < 0)
  570. goto next_button;
  571. } else if (button->role == EM28XX_BUTTON_ILLUMINATION) {
  572. /* Check sanity */
  573. if (!em28xx_find_led(dev, EM28XX_LED_ILLUMINATION)) {
  574. dev_err(&dev->intf->dev,
  575. "BUG: illumination button defined, but no illumination LED.\n");
  576. goto next_button;
  577. }
  578. }
  579. /* Add read address to list of polling addresses */
  580. if (addr_new) {
  581. unsigned int index = dev->num_button_polling_addresses;
  582. dev->button_polling_addresses[index] = button->reg_r;
  583. dev->num_button_polling_addresses++;
  584. }
  585. /* Reduce polling interval if necessary */
  586. if (!button->reg_clearing)
  587. dev->button_polling_interval =
  588. EM28XX_BUTTONS_VOLATILE_QUERY_INTERVAL;
  589. next_button:
  590. /* Next button */
  591. i++;
  592. }
  593. /* Start polling */
  594. if (dev->num_button_polling_addresses) {
  595. memset(dev->button_polling_last_values, 0,
  596. EM28XX_NUM_BUTTON_ADDRESSES_MAX);
  597. schedule_delayed_work(&dev->buttons_query_work,
  598. msecs_to_jiffies(dev->button_polling_interval));
  599. }
  600. }
  601. static void em28xx_shutdown_buttons(struct em28xx *dev)
  602. {
  603. /* Cancel polling */
  604. cancel_delayed_work_sync(&dev->buttons_query_work);
  605. /* Clear polling addresses list */
  606. dev->num_button_polling_addresses = 0;
  607. /* Deregister input devices */
  608. if (dev->sbutton_input_dev) {
  609. dev_info(&dev->intf->dev, "Deregistering snapshot button\n");
  610. input_unregister_device(dev->sbutton_input_dev);
  611. dev->sbutton_input_dev = NULL;
  612. }
  613. }
  614. static int em28xx_ir_init(struct em28xx *dev)
  615. {
  616. struct usb_device *udev = interface_to_usbdev(dev->intf);
  617. struct em28xx_IR *ir;
  618. struct rc_dev *rc;
  619. int err = -ENOMEM;
  620. u64 rc_proto;
  621. u16 i2c_rc_dev_addr = 0;
  622. if (dev->is_audio_only) {
  623. /* Shouldn't initialize IR for this interface */
  624. return 0;
  625. }
  626. kref_get(&dev->ref);
  627. INIT_DELAYED_WORK(&dev->buttons_query_work, em28xx_query_buttons);
  628. if (dev->board.buttons)
  629. em28xx_init_buttons(dev);
  630. if (dev->board.has_ir_i2c) {
  631. i2c_rc_dev_addr = em28xx_probe_i2c_ir(dev);
  632. if (!i2c_rc_dev_addr) {
  633. dev->board.has_ir_i2c = 0;
  634. dev_warn(&dev->intf->dev,
  635. "No i2c IR remote control device found.\n");
  636. return -ENODEV;
  637. }
  638. }
  639. if (!dev->board.ir_codes && !dev->board.has_ir_i2c) {
  640. /* No remote control support */
  641. dev_warn(&dev->intf->dev,
  642. "Remote control support is not available for this card.\n");
  643. return 0;
  644. }
  645. dev_info(&dev->intf->dev, "Registering input extension\n");
  646. ir = kzalloc(sizeof(*ir), GFP_KERNEL);
  647. if (!ir)
  648. return -ENOMEM;
  649. rc = rc_allocate_device(RC_DRIVER_SCANCODE);
  650. if (!rc)
  651. goto error;
  652. /* record handles to ourself */
  653. ir->dev = dev;
  654. dev->ir = ir;
  655. ir->rc = rc;
  656. rc->priv = ir;
  657. rc->open = em28xx_ir_start;
  658. rc->close = em28xx_ir_stop;
  659. if (dev->board.has_ir_i2c) { /* external i2c device */
  660. switch (dev->model) {
  661. case EM2800_BOARD_TERRATEC_CINERGY_200:
  662. case EM2820_BOARD_TERRATEC_CINERGY_250:
  663. rc->map_name = RC_MAP_EM_TERRATEC;
  664. ir->get_key_i2c = em28xx_get_key_terratec;
  665. break;
  666. case EM2820_BOARD_PINNACLE_USB_2:
  667. rc->map_name = RC_MAP_PINNACLE_GREY;
  668. ir->get_key_i2c = em28xx_get_key_pinnacle_usb_grey;
  669. break;
  670. case EM2820_BOARD_HAUPPAUGE_WINTV_USB_2:
  671. rc->map_name = RC_MAP_HAUPPAUGE;
  672. ir->get_key_i2c = em28xx_get_key_em_haup;
  673. rc->allowed_protocols = RC_PROTO_BIT_RC5;
  674. break;
  675. case EM2820_BOARD_LEADTEK_WINFAST_USBII_DELUXE:
  676. rc->map_name = RC_MAP_WINFAST_USBII_DELUXE;
  677. ir->get_key_i2c = em28xx_get_key_winfast_usbii_deluxe;
  678. break;
  679. default:
  680. err = -ENODEV;
  681. goto error;
  682. }
  683. ir->i2c_client = kzalloc(sizeof(*ir->i2c_client), GFP_KERNEL);
  684. if (!ir->i2c_client)
  685. goto error;
  686. ir->i2c_client->adapter = &ir->dev->i2c_adap[dev->def_i2c_bus];
  687. ir->i2c_client->addr = i2c_rc_dev_addr;
  688. ir->i2c_client->flags = 0;
  689. /* NOTE: all other fields of i2c_client are unused */
  690. } else { /* internal device */
  691. switch (dev->chip_id) {
  692. case CHIP_ID_EM2860:
  693. case CHIP_ID_EM2883:
  694. rc->allowed_protocols = RC_PROTO_BIT_RC5 |
  695. RC_PROTO_BIT_NEC;
  696. ir->get_key = default_polling_getkey;
  697. break;
  698. case CHIP_ID_EM2884:
  699. case CHIP_ID_EM2874:
  700. case CHIP_ID_EM28174:
  701. case CHIP_ID_EM28178:
  702. ir->get_key = em2874_polling_getkey;
  703. rc->allowed_protocols = RC_PROTO_BIT_RC5 |
  704. RC_PROTO_BIT_NEC | RC_PROTO_BIT_NECX |
  705. RC_PROTO_BIT_NEC32 | RC_PROTO_BIT_RC6_0;
  706. break;
  707. default:
  708. err = -ENODEV;
  709. goto error;
  710. }
  711. rc->change_protocol = em28xx_ir_change_protocol;
  712. rc->map_name = dev->board.ir_codes;
  713. /* By default, keep protocol field untouched */
  714. rc_proto = RC_PROTO_BIT_UNKNOWN;
  715. err = em28xx_ir_change_protocol(rc, &rc_proto);
  716. if (err)
  717. goto error;
  718. }
  719. /* This is how often we ask the chip for IR information */
  720. ir->polling = 100; /* ms */
  721. /* init input device */
  722. snprintf(ir->name, sizeof(ir->name), "%s IR",
  723. dev_name(&dev->intf->dev));
  724. usb_make_path(udev, ir->phys, sizeof(ir->phys));
  725. strlcat(ir->phys, "/input0", sizeof(ir->phys));
  726. rc->device_name = ir->name;
  727. rc->input_phys = ir->phys;
  728. rc->input_id.bustype = BUS_USB;
  729. rc->input_id.version = 1;
  730. rc->input_id.vendor = le16_to_cpu(udev->descriptor.idVendor);
  731. rc->input_id.product = le16_to_cpu(udev->descriptor.idProduct);
  732. rc->dev.parent = &dev->intf->dev;
  733. rc->driver_name = MODULE_NAME;
  734. /* all done */
  735. err = rc_register_device(rc);
  736. if (err)
  737. goto error;
  738. dev_info(&dev->intf->dev, "Input extension successfully initialized\n");
  739. return 0;
  740. error:
  741. kfree(ir->i2c_client);
  742. dev->ir = NULL;
  743. rc_free_device(rc);
  744. kfree(ir);
  745. return err;
  746. }
  747. static int em28xx_ir_fini(struct em28xx *dev)
  748. {
  749. struct em28xx_IR *ir = dev->ir;
  750. if (dev->is_audio_only) {
  751. /* Shouldn't initialize IR for this interface */
  752. return 0;
  753. }
  754. dev_info(&dev->intf->dev, "Closing input extension\n");
  755. em28xx_shutdown_buttons(dev);
  756. /* skip detach on non attached boards */
  757. if (!ir)
  758. goto ref_put;
  759. rc_unregister_device(ir->rc);
  760. kfree(ir->i2c_client);
  761. /* done */
  762. kfree(ir);
  763. dev->ir = NULL;
  764. ref_put:
  765. kref_put(&dev->ref, em28xx_free_device);
  766. return 0;
  767. }
  768. static int em28xx_ir_suspend(struct em28xx *dev)
  769. {
  770. struct em28xx_IR *ir = dev->ir;
  771. if (dev->is_audio_only)
  772. return 0;
  773. dev_info(&dev->intf->dev, "Suspending input extension\n");
  774. if (ir)
  775. cancel_delayed_work_sync(&ir->work);
  776. cancel_delayed_work_sync(&dev->buttons_query_work);
  777. /*
  778. * is canceling delayed work sufficient or does the rc event
  779. * kthread needs stopping? kthread is stopped in
  780. * ir_raw_event_unregister()
  781. */
  782. return 0;
  783. }
  784. static int em28xx_ir_resume(struct em28xx *dev)
  785. {
  786. struct em28xx_IR *ir = dev->ir;
  787. if (dev->is_audio_only)
  788. return 0;
  789. dev_info(&dev->intf->dev, "Resuming input extension\n");
  790. /*
  791. * if suspend calls ir_raw_event_unregister(), the should call
  792. * ir_raw_event_register()
  793. */
  794. if (ir)
  795. schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
  796. if (dev->num_button_polling_addresses)
  797. schedule_delayed_work(&dev->buttons_query_work,
  798. msecs_to_jiffies(dev->button_polling_interval));
  799. return 0;
  800. }
  801. static struct em28xx_ops rc_ops = {
  802. .id = EM28XX_RC,
  803. .name = "Em28xx Input Extension",
  804. .init = em28xx_ir_init,
  805. .fini = em28xx_ir_fini,
  806. .suspend = em28xx_ir_suspend,
  807. .resume = em28xx_ir_resume,
  808. };
  809. static int __init em28xx_rc_register(void)
  810. {
  811. return em28xx_register_extension(&rc_ops);
  812. }
  813. static void __exit em28xx_rc_unregister(void)
  814. {
  815. em28xx_unregister_extension(&rc_ops);
  816. }
  817. MODULE_LICENSE("GPL v2");
  818. MODULE_AUTHOR("Mauro Carvalho Chehab");
  819. MODULE_DESCRIPTION(DRIVER_DESC " - input interface");
  820. MODULE_VERSION(EM28XX_VERSION);
  821. module_init(em28xx_rc_register);
  822. module_exit(em28xx_rc_unregister);