em28xx-input.c 24 KB

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