em28xx-input.c 24 KB

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