em28xx-input.c 24 KB

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