em28xx-input.c 23 KB

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