em28xx-input.c 23 KB

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