saa7134-input.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. /*
  2. *
  3. * handle saa7134 IR remotes via linux kernel input layer.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  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. */
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/delay.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/input.h>
  25. #include "saa7134-reg.h"
  26. #include "saa7134.h"
  27. static unsigned int disable_ir;
  28. module_param(disable_ir, int, 0444);
  29. MODULE_PARM_DESC(disable_ir,"disable infrared remote support");
  30. static unsigned int ir_debug;
  31. module_param(ir_debug, int, 0644);
  32. MODULE_PARM_DESC(ir_debug,"enable debug messages [IR]");
  33. static int pinnacle_remote;
  34. module_param(pinnacle_remote, int, 0644); /* Choose Pinnacle PCTV remote */
  35. MODULE_PARM_DESC(pinnacle_remote, "Specify Pinnacle PCTV remote: 0=coloured, 1=grey (defaults to 0)");
  36. static int ir_rc5_remote_gap = 885;
  37. module_param(ir_rc5_remote_gap, int, 0644);
  38. static int ir_rc5_key_timeout = 115;
  39. module_param(ir_rc5_key_timeout, int, 0644);
  40. static int repeat_delay = 500;
  41. module_param(repeat_delay, int, 0644);
  42. MODULE_PARM_DESC(repeat_delay, "delay before key repeat started");
  43. static int repeat_period = 33;
  44. module_param(repeat_period, int, 0644);
  45. MODULE_PARM_DESC(repeat_period, "repeat period between "
  46. "keypresses when key is down");
  47. static unsigned int disable_other_ir;
  48. module_param(disable_other_ir, int, 0644);
  49. MODULE_PARM_DESC(disable_other_ir, "disable full codes of "
  50. "alternative remotes from other manufacturers");
  51. #define dprintk(fmt, arg...) if (ir_debug) \
  52. printk(KERN_DEBUG "%s/ir: " fmt, dev->name , ## arg)
  53. #define i2cdprintk(fmt, arg...) if (ir_debug) \
  54. printk(KERN_DEBUG "%s/ir: " fmt, ir->c.name , ## arg)
  55. /* Helper functions for RC5 and NEC decoding at GPIO16 or GPIO18 */
  56. static int saa7134_rc5_irq(struct saa7134_dev *dev);
  57. static int saa7134_nec_irq(struct saa7134_dev *dev);
  58. static void nec_task(unsigned long data);
  59. static void saa7134_nec_timer(unsigned long data);
  60. /* -------------------- GPIO generic keycode builder -------------------- */
  61. static int build_key(struct saa7134_dev *dev)
  62. {
  63. struct card_ir *ir = dev->remote;
  64. u32 gpio, data;
  65. /* here comes the additional handshake steps for some cards */
  66. switch (dev->board) {
  67. case SAA7134_BOARD_GOTVIEW_7135:
  68. saa_setb(SAA7134_GPIO_GPSTATUS1, 0x80);
  69. saa_clearb(SAA7134_GPIO_GPSTATUS1, 0x80);
  70. break;
  71. }
  72. /* rising SAA7134_GPIO_GPRESCAN reads the status */
  73. saa_clearb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
  74. saa_setb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
  75. gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
  76. if (ir->polling) {
  77. if (ir->last_gpio == gpio)
  78. return 0;
  79. ir->last_gpio = gpio;
  80. }
  81. data = ir_extract_bits(gpio, ir->mask_keycode);
  82. dprintk("build_key gpio=0x%x mask=0x%x data=%d\n",
  83. gpio, ir->mask_keycode, data);
  84. if (ir->polling) {
  85. if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
  86. (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
  87. ir_input_keydown(ir->dev, &ir->ir, data, data);
  88. } else {
  89. ir_input_nokey(ir->dev, &ir->ir);
  90. }
  91. }
  92. else { /* IRQ driven mode - handle key press and release in one go */
  93. if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
  94. (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
  95. ir_input_keydown(ir->dev, &ir->ir, data, data);
  96. ir_input_nokey(ir->dev, &ir->ir);
  97. }
  98. }
  99. return 0;
  100. }
  101. /* --------------------- Chip specific I2C key builders ----------------- */
  102. static int get_key_purpletv(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  103. {
  104. unsigned char b;
  105. /* poll IR chip */
  106. if (1 != i2c_master_recv(&ir->c,&b,1)) {
  107. i2cdprintk("read error\n");
  108. return -EIO;
  109. }
  110. /* no button press */
  111. if (b==0)
  112. return 0;
  113. /* repeating */
  114. if (b & 0x80)
  115. return 1;
  116. *ir_key = b;
  117. *ir_raw = b;
  118. return 1;
  119. }
  120. static int get_key_hvr1110(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  121. {
  122. unsigned char buf[5], cod4, code3, code4;
  123. /* poll IR chip */
  124. if (5 != i2c_master_recv(&ir->c,buf,5))
  125. return -EIO;
  126. cod4 = buf[4];
  127. code4 = (cod4 >> 2);
  128. code3 = buf[3];
  129. if (code3 == 0)
  130. /* no key pressed */
  131. return 0;
  132. /* return key */
  133. *ir_key = code4;
  134. *ir_raw = code4;
  135. return 1;
  136. }
  137. static int get_key_beholdm6xx(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  138. {
  139. unsigned char data[12];
  140. u32 gpio;
  141. struct saa7134_dev *dev = ir->c.adapter->algo_data;
  142. /* rising SAA7134_GPIO_GPRESCAN reads the status */
  143. saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
  144. saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
  145. gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
  146. if (0x400000 & ~gpio)
  147. return 0; /* No button press */
  148. ir->c.addr = 0x5a >> 1;
  149. if (12 != i2c_master_recv(&ir->c, data, 12)) {
  150. i2cdprintk("read error\n");
  151. return -EIO;
  152. }
  153. /* IR of this card normally decode signals NEC-standard from
  154. * - Sven IHOO MT 5.1R remote. xxyye718
  155. * - Sven DVD HD-10xx remote. xxyyf708
  156. * - BBK ...
  157. * - mayby others
  158. * So, skip not our, if disable full codes mode.
  159. */
  160. if (data[10] != 0x6b && data[11] != 0x86 && disable_other_ir)
  161. return 0;
  162. *ir_key = data[9];
  163. *ir_raw = data[9];
  164. return 1;
  165. }
  166. /* Common (grey or coloured) pinnacle PCTV remote handling
  167. *
  168. */
  169. static int get_key_pinnacle(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw,
  170. int parity_offset, int marker, int code_modulo)
  171. {
  172. unsigned char b[4];
  173. unsigned int start = 0,parity = 0,code = 0;
  174. /* poll IR chip */
  175. if (4 != i2c_master_recv(&ir->c, b, 4)) {
  176. i2cdprintk("read error\n");
  177. return -EIO;
  178. }
  179. for (start = 0; start < ARRAY_SIZE(b); start++) {
  180. if (b[start] == marker) {
  181. code=b[(start+parity_offset + 1) % 4];
  182. parity=b[(start+parity_offset) % 4];
  183. }
  184. }
  185. /* Empty Request */
  186. if (parity == 0)
  187. return 0;
  188. /* Repeating... */
  189. if (ir->old == parity)
  190. return 0;
  191. ir->old = parity;
  192. /* drop special codes when a key is held down a long time for the grey controller
  193. In this case, the second bit of the code is asserted */
  194. if (marker == 0xfe && (code & 0x40))
  195. return 0;
  196. code %= code_modulo;
  197. *ir_raw = code;
  198. *ir_key = code;
  199. i2cdprintk("Pinnacle PCTV key %02x\n", code);
  200. return 1;
  201. }
  202. /* The grey pinnacle PCTV remote
  203. *
  204. * There are one issue with this remote:
  205. * - I2c packet does not change when the same key is pressed quickly. The workaround
  206. * is to hold down each key for about half a second, so that another code is generated
  207. * in the i2c packet, and the function can distinguish key presses.
  208. *
  209. * Sylvain Pasche <sylvain.pasche@gmail.com>
  210. */
  211. static int get_key_pinnacle_grey(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  212. {
  213. return get_key_pinnacle(ir, ir_key, ir_raw, 1, 0xfe, 0xff);
  214. }
  215. /* The new pinnacle PCTV remote (with the colored buttons)
  216. *
  217. * Ricardo Cerqueira <v4l@cerqueira.org>
  218. */
  219. static int get_key_pinnacle_color(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  220. {
  221. /* code_modulo parameter (0x88) is used to reduce code value to fit inside IR_KEYTAB_SIZE
  222. *
  223. * this is the only value that results in 42 unique
  224. * codes < 128
  225. */
  226. return get_key_pinnacle(ir, ir_key, ir_raw, 2, 0x80, 0x88);
  227. }
  228. void saa7134_input_irq(struct saa7134_dev *dev)
  229. {
  230. struct card_ir *ir = dev->remote;
  231. if (ir->nec_gpio) {
  232. saa7134_nec_irq(dev);
  233. } else if (!ir->polling && !ir->rc5_gpio) {
  234. build_key(dev);
  235. } else if (ir->rc5_gpio) {
  236. saa7134_rc5_irq(dev);
  237. }
  238. }
  239. static void saa7134_input_timer(unsigned long data)
  240. {
  241. struct saa7134_dev *dev = (struct saa7134_dev *)data;
  242. struct card_ir *ir = dev->remote;
  243. build_key(dev);
  244. mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
  245. }
  246. void saa7134_ir_start(struct saa7134_dev *dev, struct card_ir *ir)
  247. {
  248. if (ir->polling) {
  249. setup_timer(&ir->timer, saa7134_input_timer,
  250. (unsigned long)dev);
  251. ir->timer.expires = jiffies + HZ;
  252. add_timer(&ir->timer);
  253. } else if (ir->rc5_gpio) {
  254. /* set timer_end for code completion */
  255. init_timer(&ir->timer_end);
  256. ir->timer_end.function = ir_rc5_timer_end;
  257. ir->timer_end.data = (unsigned long)ir;
  258. init_timer(&ir->timer_keyup);
  259. ir->timer_keyup.function = ir_rc5_timer_keyup;
  260. ir->timer_keyup.data = (unsigned long)ir;
  261. ir->shift_by = 2;
  262. ir->start = 0x2;
  263. ir->addr = 0x17;
  264. ir->rc5_key_timeout = ir_rc5_key_timeout;
  265. ir->rc5_remote_gap = ir_rc5_remote_gap;
  266. } else if (ir->nec_gpio) {
  267. setup_timer(&ir->timer_keyup, saa7134_nec_timer,
  268. (unsigned long)dev);
  269. tasklet_init(&ir->tlet, nec_task, (unsigned long)dev);
  270. }
  271. }
  272. void saa7134_ir_stop(struct saa7134_dev *dev)
  273. {
  274. if (dev->remote->polling)
  275. del_timer_sync(&dev->remote->timer);
  276. }
  277. int saa7134_input_init1(struct saa7134_dev *dev)
  278. {
  279. struct card_ir *ir;
  280. struct input_dev *input_dev;
  281. IR_KEYTAB_TYPE *ir_codes = NULL;
  282. u32 mask_keycode = 0;
  283. u32 mask_keydown = 0;
  284. u32 mask_keyup = 0;
  285. int polling = 0;
  286. int rc5_gpio = 0;
  287. int nec_gpio = 0;
  288. int ir_type = IR_TYPE_OTHER;
  289. int err;
  290. if (dev->has_remote != SAA7134_REMOTE_GPIO)
  291. return -ENODEV;
  292. if (disable_ir)
  293. return -ENODEV;
  294. /* detect & configure */
  295. switch (dev->board) {
  296. case SAA7134_BOARD_FLYVIDEO2000:
  297. case SAA7134_BOARD_FLYVIDEO3000:
  298. case SAA7134_BOARD_FLYTVPLATINUM_FM:
  299. case SAA7134_BOARD_FLYTVPLATINUM_MINI2:
  300. ir_codes = ir_codes_flyvideo;
  301. mask_keycode = 0xEC00000;
  302. mask_keydown = 0x0040000;
  303. break;
  304. case SAA7134_BOARD_CINERGY400:
  305. case SAA7134_BOARD_CINERGY600:
  306. case SAA7134_BOARD_CINERGY600_MK3:
  307. ir_codes = ir_codes_cinergy;
  308. mask_keycode = 0x00003f;
  309. mask_keyup = 0x040000;
  310. break;
  311. case SAA7134_BOARD_ECS_TVP3XP:
  312. case SAA7134_BOARD_ECS_TVP3XP_4CB5:
  313. ir_codes = ir_codes_eztv;
  314. mask_keycode = 0x00017c;
  315. mask_keyup = 0x000002;
  316. polling = 50; // ms
  317. break;
  318. case SAA7134_BOARD_KWORLD_XPERT:
  319. case SAA7134_BOARD_AVACSSMARTTV:
  320. ir_codes = ir_codes_pixelview;
  321. mask_keycode = 0x00001F;
  322. mask_keyup = 0x000020;
  323. polling = 50; // ms
  324. break;
  325. case SAA7134_BOARD_MD2819:
  326. case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
  327. case SAA7134_BOARD_AVERMEDIA_305:
  328. case SAA7134_BOARD_AVERMEDIA_307:
  329. case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
  330. case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
  331. case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
  332. case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
  333. case SAA7134_BOARD_AVERMEDIA_M102:
  334. ir_codes = ir_codes_avermedia;
  335. mask_keycode = 0x0007C8;
  336. mask_keydown = 0x000010;
  337. polling = 50; // ms
  338. /* Set GPIO pin2 to high to enable the IR controller */
  339. saa_setb(SAA7134_GPIO_GPMODE0, 0x4);
  340. saa_setb(SAA7134_GPIO_GPSTATUS0, 0x4);
  341. break;
  342. case SAA7134_BOARD_AVERMEDIA_M135A:
  343. ir_codes = ir_codes_avermedia_m135a;
  344. mask_keydown = 0x0040000;
  345. mask_keycode = 0x00013f;
  346. nec_gpio = 1;
  347. break;
  348. case SAA7134_BOARD_AVERMEDIA_777:
  349. case SAA7134_BOARD_AVERMEDIA_A16AR:
  350. ir_codes = ir_codes_avermedia;
  351. mask_keycode = 0x02F200;
  352. mask_keydown = 0x000400;
  353. polling = 50; // ms
  354. /* Without this we won't receive key up events */
  355. saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
  356. saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
  357. break;
  358. case SAA7134_BOARD_AVERMEDIA_A16D:
  359. ir_codes = ir_codes_avermedia_a16d;
  360. mask_keycode = 0x02F200;
  361. mask_keydown = 0x000400;
  362. polling = 50; /* ms */
  363. /* Without this we won't receive key up events */
  364. saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
  365. saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
  366. break;
  367. case SAA7134_BOARD_KWORLD_TERMINATOR:
  368. ir_codes = ir_codes_pixelview;
  369. mask_keycode = 0x00001f;
  370. mask_keyup = 0x000060;
  371. polling = 50; // ms
  372. break;
  373. case SAA7134_BOARD_MANLI_MTV001:
  374. case SAA7134_BOARD_MANLI_MTV002:
  375. ir_codes = ir_codes_manli;
  376. mask_keycode = 0x001f00;
  377. mask_keyup = 0x004000;
  378. polling = 50; /* ms */
  379. break;
  380. case SAA7134_BOARD_BEHOLD_409FM:
  381. case SAA7134_BOARD_BEHOLD_401:
  382. case SAA7134_BOARD_BEHOLD_403:
  383. case SAA7134_BOARD_BEHOLD_403FM:
  384. case SAA7134_BOARD_BEHOLD_405:
  385. case SAA7134_BOARD_BEHOLD_405FM:
  386. case SAA7134_BOARD_BEHOLD_407:
  387. case SAA7134_BOARD_BEHOLD_407FM:
  388. case SAA7134_BOARD_BEHOLD_409:
  389. case SAA7134_BOARD_BEHOLD_505FM:
  390. case SAA7134_BOARD_BEHOLD_507_9FM:
  391. ir_codes = ir_codes_manli;
  392. mask_keycode = 0x003f00;
  393. mask_keyup = 0x004000;
  394. polling = 50; /* ms */
  395. break;
  396. case SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM:
  397. ir_codes = ir_codes_behold_columbus;
  398. mask_keycode = 0x003f00;
  399. mask_keyup = 0x004000;
  400. polling = 50; // ms
  401. break;
  402. case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
  403. ir_codes = ir_codes_pctv_sedna;
  404. mask_keycode = 0x001f00;
  405. mask_keyup = 0x004000;
  406. polling = 50; // ms
  407. break;
  408. case SAA7134_BOARD_GOTVIEW_7135:
  409. ir_codes = ir_codes_gotview7135;
  410. mask_keycode = 0x0003CC;
  411. mask_keydown = 0x000010;
  412. polling = 5; /* ms */
  413. saa_setb(SAA7134_GPIO_GPMODE1, 0x80);
  414. break;
  415. case SAA7134_BOARD_VIDEOMATE_TV_PVR:
  416. case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
  417. case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
  418. ir_codes = ir_codes_videomate_tv_pvr;
  419. mask_keycode = 0x00003F;
  420. mask_keyup = 0x400000;
  421. polling = 50; // ms
  422. break;
  423. case SAA7134_BOARD_PROTEUS_2309:
  424. ir_codes = ir_codes_proteus_2309;
  425. mask_keycode = 0x00007F;
  426. mask_keyup = 0x000080;
  427. polling = 50; // ms
  428. break;
  429. case SAA7134_BOARD_VIDEOMATE_DVBT_300:
  430. case SAA7134_BOARD_VIDEOMATE_DVBT_200:
  431. ir_codes = ir_codes_videomate_tv_pvr;
  432. mask_keycode = 0x003F00;
  433. mask_keyup = 0x040000;
  434. break;
  435. case SAA7134_BOARD_FLYDVBS_LR300:
  436. case SAA7134_BOARD_FLYDVBT_LR301:
  437. case SAA7134_BOARD_FLYDVBTDUO:
  438. ir_codes = ir_codes_flydvb;
  439. mask_keycode = 0x0001F00;
  440. mask_keydown = 0x0040000;
  441. break;
  442. case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
  443. case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
  444. case SAA7134_BOARD_ASUSTeK_P7131_ANALOG:
  445. ir_codes = ir_codes_asus_pc39;
  446. mask_keydown = 0x0040000;
  447. rc5_gpio = 1;
  448. break;
  449. case SAA7134_BOARD_ENCORE_ENLTV:
  450. case SAA7134_BOARD_ENCORE_ENLTV_FM:
  451. ir_codes = ir_codes_encore_enltv;
  452. mask_keycode = 0x00007f;
  453. mask_keyup = 0x040000;
  454. polling = 50; // ms
  455. break;
  456. case SAA7134_BOARD_ENCORE_ENLTV_FM53:
  457. ir_codes = ir_codes_encore_enltv_fm53;
  458. mask_keydown = 0x0040000;
  459. mask_keycode = 0x00007f;
  460. nec_gpio = 1;
  461. break;
  462. case SAA7134_BOARD_10MOONSTVMASTER3:
  463. ir_codes = ir_codes_encore_enltv;
  464. mask_keycode = 0x5f80000;
  465. mask_keyup = 0x8000000;
  466. polling = 50; //ms
  467. break;
  468. case SAA7134_BOARD_GENIUS_TVGO_A11MCE:
  469. ir_codes = ir_codes_genius_tvgo_a11mce;
  470. mask_keycode = 0xff;
  471. mask_keydown = 0xf00000;
  472. polling = 50; /* ms */
  473. break;
  474. case SAA7134_BOARD_REAL_ANGEL_220:
  475. ir_codes = ir_codes_real_audio_220_32_keys;
  476. mask_keycode = 0x3f00;
  477. mask_keyup = 0x4000;
  478. polling = 50; /* ms */
  479. break;
  480. }
  481. if (NULL == ir_codes) {
  482. printk("%s: Oops: IR config error [card=%d]\n",
  483. dev->name, dev->board);
  484. return -ENODEV;
  485. }
  486. ir = kzalloc(sizeof(*ir), GFP_KERNEL);
  487. input_dev = input_allocate_device();
  488. if (!ir || !input_dev) {
  489. err = -ENOMEM;
  490. goto err_out_free;
  491. }
  492. ir->dev = input_dev;
  493. /* init hardware-specific stuff */
  494. ir->mask_keycode = mask_keycode;
  495. ir->mask_keydown = mask_keydown;
  496. ir->mask_keyup = mask_keyup;
  497. ir->polling = polling;
  498. ir->rc5_gpio = rc5_gpio;
  499. ir->nec_gpio = nec_gpio;
  500. /* init input device */
  501. snprintf(ir->name, sizeof(ir->name), "saa7134 IR (%s)",
  502. saa7134_boards[dev->board].name);
  503. snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
  504. pci_name(dev->pci));
  505. ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
  506. input_dev->name = ir->name;
  507. input_dev->phys = ir->phys;
  508. input_dev->id.bustype = BUS_PCI;
  509. input_dev->id.version = 1;
  510. if (dev->pci->subsystem_vendor) {
  511. input_dev->id.vendor = dev->pci->subsystem_vendor;
  512. input_dev->id.product = dev->pci->subsystem_device;
  513. } else {
  514. input_dev->id.vendor = dev->pci->vendor;
  515. input_dev->id.product = dev->pci->device;
  516. }
  517. input_dev->dev.parent = &dev->pci->dev;
  518. dev->remote = ir;
  519. saa7134_ir_start(dev, ir);
  520. err = input_register_device(ir->dev);
  521. if (err)
  522. goto err_out_stop;
  523. /* the remote isn't as bouncy as a keyboard */
  524. ir->dev->rep[REP_DELAY] = repeat_delay;
  525. ir->dev->rep[REP_PERIOD] = repeat_period;
  526. return 0;
  527. err_out_stop:
  528. saa7134_ir_stop(dev);
  529. dev->remote = NULL;
  530. err_out_free:
  531. input_free_device(input_dev);
  532. kfree(ir);
  533. return err;
  534. }
  535. void saa7134_input_fini(struct saa7134_dev *dev)
  536. {
  537. if (NULL == dev->remote)
  538. return;
  539. saa7134_ir_stop(dev);
  540. input_unregister_device(dev->remote->dev);
  541. kfree(dev->remote);
  542. dev->remote = NULL;
  543. }
  544. void saa7134_set_i2c_ir(struct saa7134_dev *dev, struct IR_i2c *ir)
  545. {
  546. if (disable_ir) {
  547. dprintk("Found supported i2c remote, but IR has been disabled\n");
  548. ir->get_key=NULL;
  549. return;
  550. }
  551. switch (dev->board) {
  552. case SAA7134_BOARD_PINNACLE_PCTV_110i:
  553. case SAA7134_BOARD_PINNACLE_PCTV_310i:
  554. snprintf(ir->c.name, sizeof(ir->c.name), "Pinnacle PCTV");
  555. if (pinnacle_remote == 0) {
  556. ir->get_key = get_key_pinnacle_color;
  557. ir->ir_codes = ir_codes_pinnacle_color;
  558. } else {
  559. ir->get_key = get_key_pinnacle_grey;
  560. ir->ir_codes = ir_codes_pinnacle_grey;
  561. }
  562. break;
  563. case SAA7134_BOARD_UPMOST_PURPLE_TV:
  564. snprintf(ir->c.name, sizeof(ir->c.name), "Purple TV");
  565. ir->get_key = get_key_purpletv;
  566. ir->ir_codes = ir_codes_purpletv;
  567. break;
  568. case SAA7134_BOARD_HAUPPAUGE_HVR1110:
  569. snprintf(ir->c.name, sizeof(ir->c.name), "HVR 1110");
  570. ir->get_key = get_key_hvr1110;
  571. ir->ir_codes = ir_codes_hauppauge_new;
  572. break;
  573. case SAA7134_BOARD_BEHOLD_607_9FM:
  574. case SAA7134_BOARD_BEHOLD_M6:
  575. case SAA7134_BOARD_BEHOLD_M63:
  576. case SAA7134_BOARD_BEHOLD_M6_EXTRA:
  577. case SAA7134_BOARD_BEHOLD_H6:
  578. snprintf(ir->c.name, sizeof(ir->c.name), "BeholdTV");
  579. ir->get_key = get_key_beholdm6xx;
  580. ir->ir_codes = ir_codes_behold;
  581. break;
  582. default:
  583. dprintk("Shouldn't get here: Unknown board %x for I2C IR?\n",dev->board);
  584. break;
  585. }
  586. }
  587. static int saa7134_rc5_irq(struct saa7134_dev *dev)
  588. {
  589. struct card_ir *ir = dev->remote;
  590. struct timeval tv;
  591. u32 gap;
  592. unsigned long current_jiffies, timeout;
  593. /* get time of bit */
  594. current_jiffies = jiffies;
  595. do_gettimeofday(&tv);
  596. /* avoid overflow with gap >1s */
  597. if (tv.tv_sec - ir->base_time.tv_sec > 1) {
  598. gap = 200000;
  599. } else {
  600. gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
  601. tv.tv_usec - ir->base_time.tv_usec;
  602. }
  603. /* active code => add bit */
  604. if (ir->active) {
  605. /* only if in the code (otherwise spurious IRQ or timer
  606. late) */
  607. if (ir->last_bit < 28) {
  608. ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
  609. ir_rc5_remote_gap;
  610. ir->code |= 1 << ir->last_bit;
  611. }
  612. /* starting new code */
  613. } else {
  614. ir->active = 1;
  615. ir->code = 0;
  616. ir->base_time = tv;
  617. ir->last_bit = 0;
  618. timeout = current_jiffies + (500 + 30 * HZ) / 1000;
  619. mod_timer(&ir->timer_end, timeout);
  620. }
  621. return 1;
  622. }
  623. /* On NEC protocol, One has 2.25 ms, and zero has 1.125 ms
  624. The first pulse (start) has 9 + 4.5 ms
  625. */
  626. static void saa7134_nec_timer(unsigned long data)
  627. {
  628. struct saa7134_dev *dev = (struct saa7134_dev *) data;
  629. struct card_ir *ir = dev->remote;
  630. dprintk("Cancel key repeat\n");
  631. ir_input_nokey(ir->dev, &ir->ir);
  632. }
  633. static void nec_task(unsigned long data)
  634. {
  635. struct saa7134_dev *dev = (struct saa7134_dev *) data;
  636. struct card_ir *ir;
  637. struct timeval tv;
  638. int count, pulse, oldpulse, gap;
  639. u32 ircode = 0, not_code = 0;
  640. int ngap = 0;
  641. if (!data) {
  642. printk(KERN_ERR "saa713x/ir: Can't recover dev struct\n");
  643. /* GPIO will be kept disabled */
  644. return;
  645. }
  646. ir = dev->remote;
  647. /* rising SAA7134_GPIO_GPRESCAN reads the status */
  648. saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
  649. saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
  650. oldpulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & ir->mask_keydown;
  651. pulse = oldpulse;
  652. do_gettimeofday(&tv);
  653. ir->base_time = tv;
  654. /* Decode NEC pulsecode. This code can take up to 76.5 ms to run.
  655. Unfortunately, using IRQ to decode pulse didn't work, since it uses
  656. a pulse train of 38KHz. This means one pulse on each 52 us
  657. */
  658. do {
  659. /* Wait until the end of pulse/space or 5 ms */
  660. for (count = 0; count < 500; count++) {
  661. udelay(10);
  662. /* rising SAA7134_GPIO_GPRESCAN reads the status */
  663. saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
  664. saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
  665. pulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2)
  666. & ir->mask_keydown;
  667. if (pulse != oldpulse)
  668. break;
  669. }
  670. do_gettimeofday(&tv);
  671. gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
  672. tv.tv_usec - ir->base_time.tv_usec;
  673. if (!pulse) {
  674. /* Bit 0 has 560 us, while bit 1 has 1120 us.
  675. Do something only if bit == 1
  676. */
  677. if (ngap && (gap > 560 + 280)) {
  678. unsigned int shift = ngap - 1;
  679. /* Address first, then command */
  680. if (shift < 8) {
  681. shift += 8;
  682. ircode |= 1 << shift;
  683. } else if (shift < 16) {
  684. not_code |= 1 << shift;
  685. } else if (shift < 24) {
  686. shift -= 16;
  687. ircode |= 1 << shift;
  688. } else {
  689. shift -= 24;
  690. not_code |= 1 << shift;
  691. }
  692. }
  693. ngap++;
  694. }
  695. ir->base_time = tv;
  696. /* TIMEOUT - Long pulse */
  697. if (gap >= 5000)
  698. break;
  699. oldpulse = pulse;
  700. } while (ngap < 32);
  701. if (ngap == 32) {
  702. /* FIXME: should check if not_code == ~ircode */
  703. ir->code = ir_extract_bits(ircode, ir->mask_keycode);
  704. dprintk("scancode = 0x%02x (code = 0x%02x, notcode= 0x%02x)\n",
  705. ir->code, ircode, not_code);
  706. ir_input_keydown(ir->dev, &ir->ir, ir->code, ir->code);
  707. } else
  708. dprintk("Repeat last key\n");
  709. /* Keep repeating the last key */
  710. mod_timer(&ir->timer_keyup, jiffies + msecs_to_jiffies(150));
  711. saa_setl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
  712. }
  713. static int saa7134_nec_irq(struct saa7134_dev *dev)
  714. {
  715. struct card_ir *ir = dev->remote;
  716. saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
  717. tasklet_schedule(&ir->tlet);
  718. return 1;
  719. }