saa7134-input.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  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->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. switch (dev->board) {
  85. case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
  86. if (data == ir->mask_keycode)
  87. ir_input_nokey(ir->dev, &ir->ir);
  88. else
  89. ir_input_keydown(ir->dev, &ir->ir, data, data);
  90. return 0;
  91. }
  92. if (ir->polling) {
  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. } else {
  97. ir_input_nokey(ir->dev, &ir->ir);
  98. }
  99. }
  100. else { /* IRQ driven mode - handle key press and release in one go */
  101. if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
  102. (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
  103. ir_input_keydown(ir->dev, &ir->ir, data, data);
  104. ir_input_nokey(ir->dev, &ir->ir);
  105. }
  106. }
  107. return 0;
  108. }
  109. /* --------------------- Chip specific I2C key builders ----------------- */
  110. static int get_key_msi_tvanywhere_plus(struct IR_i2c *ir, u32 *ir_key,
  111. u32 *ir_raw)
  112. {
  113. unsigned char b;
  114. int gpio;
  115. /* <dev> is needed to access GPIO. Used by the saa_readl macro. */
  116. struct saa7134_dev *dev = ir->c->adapter->algo_data;
  117. if (dev == NULL) {
  118. dprintk("get_key_msi_tvanywhere_plus: "
  119. "gir->c->adapter->algo_data is NULL!\n");
  120. return -EIO;
  121. }
  122. /* rising SAA7134_GPIO_GPRESCAN reads the status */
  123. saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
  124. saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
  125. gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
  126. /* GPIO&0x40 is pulsed low when a button is pressed. Don't do
  127. I2C receive if gpio&0x40 is not low. */
  128. if (gpio & 0x40)
  129. return 0; /* No button press */
  130. /* GPIO says there is a button press. Get it. */
  131. if (1 != i2c_master_recv(ir->c, &b, 1)) {
  132. i2cdprintk("read error\n");
  133. return -EIO;
  134. }
  135. /* No button press */
  136. if (b == 0xff)
  137. return 0;
  138. /* Button pressed */
  139. dprintk("get_key_msi_tvanywhere_plus: Key = 0x%02X\n", b);
  140. *ir_key = b;
  141. *ir_raw = b;
  142. return 1;
  143. }
  144. static int get_key_purpletv(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  145. {
  146. unsigned char b;
  147. /* poll IR chip */
  148. if (1 != i2c_master_recv(ir->c, &b, 1)) {
  149. i2cdprintk("read error\n");
  150. return -EIO;
  151. }
  152. /* no button press */
  153. if (b==0)
  154. return 0;
  155. /* repeating */
  156. if (b & 0x80)
  157. return 1;
  158. *ir_key = b;
  159. *ir_raw = b;
  160. return 1;
  161. }
  162. static int get_key_hvr1110(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  163. {
  164. unsigned char buf[5], cod4, code3, code4;
  165. /* poll IR chip */
  166. if (5 != i2c_master_recv(ir->c, buf, 5))
  167. return -EIO;
  168. cod4 = buf[4];
  169. code4 = (cod4 >> 2);
  170. code3 = buf[3];
  171. if (code3 == 0)
  172. /* no key pressed */
  173. return 0;
  174. /* return key */
  175. *ir_key = code4;
  176. *ir_raw = code4;
  177. return 1;
  178. }
  179. static int get_key_beholdm6xx(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  180. {
  181. unsigned char data[12];
  182. u32 gpio;
  183. struct saa7134_dev *dev = ir->c->adapter->algo_data;
  184. /* rising SAA7134_GPIO_GPRESCAN reads the status */
  185. saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
  186. saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
  187. gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
  188. if (0x400000 & ~gpio)
  189. return 0; /* No button press */
  190. ir->c->addr = 0x5a >> 1;
  191. if (12 != i2c_master_recv(ir->c, data, 12)) {
  192. i2cdprintk("read error\n");
  193. return -EIO;
  194. }
  195. /* IR of this card normally decode signals NEC-standard from
  196. * - Sven IHOO MT 5.1R remote. xxyye718
  197. * - Sven DVD HD-10xx remote. xxyyf708
  198. * - BBK ...
  199. * - mayby others
  200. * So, skip not our, if disable full codes mode.
  201. */
  202. if (data[10] != 0x6b && data[11] != 0x86 && disable_other_ir)
  203. return 0;
  204. *ir_key = data[9];
  205. *ir_raw = data[9];
  206. return 1;
  207. }
  208. /* Common (grey or coloured) pinnacle PCTV remote handling
  209. *
  210. */
  211. static int get_key_pinnacle(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw,
  212. int parity_offset, int marker, int code_modulo)
  213. {
  214. unsigned char b[4];
  215. unsigned int start = 0,parity = 0,code = 0;
  216. /* poll IR chip */
  217. if (4 != i2c_master_recv(ir->c, b, 4)) {
  218. i2cdprintk("read error\n");
  219. return -EIO;
  220. }
  221. for (start = 0; start < ARRAY_SIZE(b); start++) {
  222. if (b[start] == marker) {
  223. code=b[(start+parity_offset + 1) % 4];
  224. parity=b[(start+parity_offset) % 4];
  225. }
  226. }
  227. /* Empty Request */
  228. if (parity == 0)
  229. return 0;
  230. /* Repeating... */
  231. if (ir->old == parity)
  232. return 0;
  233. ir->old = parity;
  234. /* drop special codes when a key is held down a long time for the grey controller
  235. In this case, the second bit of the code is asserted */
  236. if (marker == 0xfe && (code & 0x40))
  237. return 0;
  238. code %= code_modulo;
  239. *ir_raw = code;
  240. *ir_key = code;
  241. i2cdprintk("Pinnacle PCTV key %02x\n", code);
  242. return 1;
  243. }
  244. /* The grey pinnacle PCTV remote
  245. *
  246. * There are one issue with this remote:
  247. * - I2c packet does not change when the same key is pressed quickly. The workaround
  248. * is to hold down each key for about half a second, so that another code is generated
  249. * in the i2c packet, and the function can distinguish key presses.
  250. *
  251. * Sylvain Pasche <sylvain.pasche@gmail.com>
  252. */
  253. static int get_key_pinnacle_grey(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  254. {
  255. return get_key_pinnacle(ir, ir_key, ir_raw, 1, 0xfe, 0xff);
  256. }
  257. /* The new pinnacle PCTV remote (with the colored buttons)
  258. *
  259. * Ricardo Cerqueira <v4l@cerqueira.org>
  260. */
  261. static int get_key_pinnacle_color(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  262. {
  263. /* code_modulo parameter (0x88) is used to reduce code value to fit inside IR_KEYTAB_SIZE
  264. *
  265. * this is the only value that results in 42 unique
  266. * codes < 128
  267. */
  268. return get_key_pinnacle(ir, ir_key, ir_raw, 2, 0x80, 0x88);
  269. }
  270. void saa7134_input_irq(struct saa7134_dev *dev)
  271. {
  272. struct card_ir *ir = dev->remote;
  273. if (ir->nec_gpio) {
  274. saa7134_nec_irq(dev);
  275. } else if (!ir->polling && !ir->rc5_gpio) {
  276. build_key(dev);
  277. } else if (ir->rc5_gpio) {
  278. saa7134_rc5_irq(dev);
  279. }
  280. }
  281. static void saa7134_input_timer(unsigned long data)
  282. {
  283. struct saa7134_dev *dev = (struct saa7134_dev *)data;
  284. struct card_ir *ir = dev->remote;
  285. build_key(dev);
  286. mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
  287. }
  288. void saa7134_ir_start(struct saa7134_dev *dev, struct card_ir *ir)
  289. {
  290. if (ir->polling) {
  291. setup_timer(&ir->timer, saa7134_input_timer,
  292. (unsigned long)dev);
  293. ir->timer.expires = jiffies + HZ;
  294. add_timer(&ir->timer);
  295. } else if (ir->rc5_gpio) {
  296. /* set timer_end for code completion */
  297. init_timer(&ir->timer_end);
  298. ir->timer_end.function = ir_rc5_timer_end;
  299. ir->timer_end.data = (unsigned long)ir;
  300. init_timer(&ir->timer_keyup);
  301. ir->timer_keyup.function = ir_rc5_timer_keyup;
  302. ir->timer_keyup.data = (unsigned long)ir;
  303. ir->shift_by = 2;
  304. ir->start = 0x2;
  305. ir->addr = 0x17;
  306. ir->rc5_key_timeout = ir_rc5_key_timeout;
  307. ir->rc5_remote_gap = ir_rc5_remote_gap;
  308. } else if (ir->nec_gpio) {
  309. setup_timer(&ir->timer_keyup, saa7134_nec_timer,
  310. (unsigned long)dev);
  311. tasklet_init(&ir->tlet, nec_task, (unsigned long)dev);
  312. }
  313. }
  314. void saa7134_ir_stop(struct saa7134_dev *dev)
  315. {
  316. if (dev->remote->polling)
  317. del_timer_sync(&dev->remote->timer);
  318. }
  319. int saa7134_input_init1(struct saa7134_dev *dev)
  320. {
  321. struct card_ir *ir;
  322. struct input_dev *input_dev;
  323. struct ir_scancode_table *ir_codes = NULL;
  324. u32 mask_keycode = 0;
  325. u32 mask_keydown = 0;
  326. u32 mask_keyup = 0;
  327. int polling = 0;
  328. int rc5_gpio = 0;
  329. int nec_gpio = 0;
  330. int ir_type = IR_TYPE_OTHER;
  331. int err;
  332. if (dev->has_remote != SAA7134_REMOTE_GPIO)
  333. return -ENODEV;
  334. if (disable_ir)
  335. return -ENODEV;
  336. /* detect & configure */
  337. switch (dev->board) {
  338. case SAA7134_BOARD_FLYVIDEO2000:
  339. case SAA7134_BOARD_FLYVIDEO3000:
  340. case SAA7134_BOARD_FLYTVPLATINUM_FM:
  341. case SAA7134_BOARD_FLYTVPLATINUM_MINI2:
  342. case SAA7134_BOARD_ROVERMEDIA_LINK_PRO_FM:
  343. ir_codes = &ir_codes_flyvideo_table;
  344. mask_keycode = 0xEC00000;
  345. mask_keydown = 0x0040000;
  346. break;
  347. case SAA7134_BOARD_CINERGY400:
  348. case SAA7134_BOARD_CINERGY600:
  349. case SAA7134_BOARD_CINERGY600_MK3:
  350. ir_codes = &ir_codes_cinergy_table;
  351. mask_keycode = 0x00003f;
  352. mask_keyup = 0x040000;
  353. break;
  354. case SAA7134_BOARD_ECS_TVP3XP:
  355. case SAA7134_BOARD_ECS_TVP3XP_4CB5:
  356. ir_codes = &ir_codes_eztv_table;
  357. mask_keycode = 0x00017c;
  358. mask_keyup = 0x000002;
  359. polling = 50; // ms
  360. break;
  361. case SAA7134_BOARD_KWORLD_XPERT:
  362. case SAA7134_BOARD_AVACSSMARTTV:
  363. ir_codes = &ir_codes_pixelview_table;
  364. mask_keycode = 0x00001F;
  365. mask_keyup = 0x000020;
  366. polling = 50; // ms
  367. break;
  368. case SAA7134_BOARD_MD2819:
  369. case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
  370. case SAA7134_BOARD_AVERMEDIA_305:
  371. case SAA7134_BOARD_AVERMEDIA_307:
  372. case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
  373. case SAA7134_BOARD_AVERMEDIA_STUDIO_505:
  374. case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
  375. case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
  376. case SAA7134_BOARD_AVERMEDIA_STUDIO_507UA:
  377. case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
  378. case SAA7134_BOARD_AVERMEDIA_M102:
  379. case SAA7134_BOARD_AVERMEDIA_GO_007_FM_PLUS:
  380. ir_codes = &ir_codes_avermedia_table;
  381. mask_keycode = 0x0007C8;
  382. mask_keydown = 0x000010;
  383. polling = 50; // ms
  384. /* Set GPIO pin2 to high to enable the IR controller */
  385. saa_setb(SAA7134_GPIO_GPMODE0, 0x4);
  386. saa_setb(SAA7134_GPIO_GPSTATUS0, 0x4);
  387. break;
  388. case SAA7134_BOARD_AVERMEDIA_M135A:
  389. ir_codes = &ir_codes_avermedia_m135a_table;
  390. mask_keydown = 0x0040000;
  391. mask_keycode = 0x00013f;
  392. nec_gpio = 1;
  393. break;
  394. case SAA7134_BOARD_AVERMEDIA_777:
  395. case SAA7134_BOARD_AVERMEDIA_A16AR:
  396. ir_codes = &ir_codes_avermedia_table;
  397. mask_keycode = 0x02F200;
  398. mask_keydown = 0x000400;
  399. polling = 50; // ms
  400. /* Without this we won't receive key up events */
  401. saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
  402. saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
  403. break;
  404. case SAA7134_BOARD_AVERMEDIA_A16D:
  405. ir_codes = &ir_codes_avermedia_a16d_table;
  406. mask_keycode = 0x02F200;
  407. mask_keydown = 0x000400;
  408. polling = 50; /* ms */
  409. /* Without this we won't receive key up events */
  410. saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
  411. saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
  412. break;
  413. case SAA7134_BOARD_KWORLD_TERMINATOR:
  414. ir_codes = &ir_codes_pixelview_table;
  415. mask_keycode = 0x00001f;
  416. mask_keyup = 0x000060;
  417. polling = 50; // ms
  418. break;
  419. case SAA7134_BOARD_MANLI_MTV001:
  420. case SAA7134_BOARD_MANLI_MTV002:
  421. ir_codes = &ir_codes_manli_table;
  422. mask_keycode = 0x001f00;
  423. mask_keyup = 0x004000;
  424. polling = 50; /* ms */
  425. break;
  426. case SAA7134_BOARD_BEHOLD_409FM:
  427. case SAA7134_BOARD_BEHOLD_401:
  428. case SAA7134_BOARD_BEHOLD_403:
  429. case SAA7134_BOARD_BEHOLD_403FM:
  430. case SAA7134_BOARD_BEHOLD_405:
  431. case SAA7134_BOARD_BEHOLD_405FM:
  432. case SAA7134_BOARD_BEHOLD_407:
  433. case SAA7134_BOARD_BEHOLD_407FM:
  434. case SAA7134_BOARD_BEHOLD_409:
  435. case SAA7134_BOARD_BEHOLD_505FM:
  436. case SAA7134_BOARD_BEHOLD_505RDS:
  437. case SAA7134_BOARD_BEHOLD_507_9FM:
  438. case SAA7134_BOARD_BEHOLD_507RDS_MK3:
  439. case SAA7134_BOARD_BEHOLD_507RDS_MK5:
  440. ir_codes = &ir_codes_manli_table;
  441. mask_keycode = 0x003f00;
  442. mask_keyup = 0x004000;
  443. polling = 50; /* ms */
  444. break;
  445. case SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM:
  446. ir_codes = &ir_codes_behold_columbus_table;
  447. mask_keycode = 0x003f00;
  448. mask_keyup = 0x004000;
  449. polling = 50; // ms
  450. break;
  451. case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
  452. ir_codes = &ir_codes_pctv_sedna_table;
  453. mask_keycode = 0x001f00;
  454. mask_keyup = 0x004000;
  455. polling = 50; // ms
  456. break;
  457. case SAA7134_BOARD_GOTVIEW_7135:
  458. ir_codes = &ir_codes_gotview7135_table;
  459. mask_keycode = 0x0003CC;
  460. mask_keydown = 0x000010;
  461. polling = 5; /* ms */
  462. saa_setb(SAA7134_GPIO_GPMODE1, 0x80);
  463. break;
  464. case SAA7134_BOARD_VIDEOMATE_TV_PVR:
  465. case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
  466. case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
  467. ir_codes = &ir_codes_videomate_tv_pvr_table;
  468. mask_keycode = 0x00003F;
  469. mask_keyup = 0x400000;
  470. polling = 50; // ms
  471. break;
  472. case SAA7134_BOARD_PROTEUS_2309:
  473. ir_codes = &ir_codes_proteus_2309_table;
  474. mask_keycode = 0x00007F;
  475. mask_keyup = 0x000080;
  476. polling = 50; // ms
  477. break;
  478. case SAA7134_BOARD_VIDEOMATE_DVBT_300:
  479. case SAA7134_BOARD_VIDEOMATE_DVBT_200:
  480. ir_codes = &ir_codes_videomate_tv_pvr_table;
  481. mask_keycode = 0x003F00;
  482. mask_keyup = 0x040000;
  483. break;
  484. case SAA7134_BOARD_FLYDVBS_LR300:
  485. case SAA7134_BOARD_FLYDVBT_LR301:
  486. case SAA7134_BOARD_FLYDVBTDUO:
  487. ir_codes = &ir_codes_flydvb_table;
  488. mask_keycode = 0x0001F00;
  489. mask_keydown = 0x0040000;
  490. break;
  491. case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
  492. case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
  493. case SAA7134_BOARD_ASUSTeK_P7131_ANALOG:
  494. ir_codes = &ir_codes_asus_pc39_table;
  495. mask_keydown = 0x0040000;
  496. rc5_gpio = 1;
  497. break;
  498. case SAA7134_BOARD_ENCORE_ENLTV:
  499. case SAA7134_BOARD_ENCORE_ENLTV_FM:
  500. ir_codes = &ir_codes_encore_enltv_table;
  501. mask_keycode = 0x00007f;
  502. mask_keyup = 0x040000;
  503. polling = 50; // ms
  504. break;
  505. case SAA7134_BOARD_ENCORE_ENLTV_FM53:
  506. ir_codes = &ir_codes_encore_enltv_fm53_table;
  507. mask_keydown = 0x0040000;
  508. mask_keycode = 0x00007f;
  509. nec_gpio = 1;
  510. break;
  511. case SAA7134_BOARD_10MOONSTVMASTER3:
  512. ir_codes = &ir_codes_encore_enltv_table;
  513. mask_keycode = 0x5f80000;
  514. mask_keyup = 0x8000000;
  515. polling = 50; //ms
  516. break;
  517. case SAA7134_BOARD_GENIUS_TVGO_A11MCE:
  518. ir_codes = &ir_codes_genius_tvgo_a11mce_table;
  519. mask_keycode = 0xff;
  520. mask_keydown = 0xf00000;
  521. polling = 50; /* ms */
  522. break;
  523. case SAA7134_BOARD_REAL_ANGEL_220:
  524. ir_codes = &ir_codes_real_audio_220_32_keys_table;
  525. mask_keycode = 0x3f00;
  526. mask_keyup = 0x4000;
  527. polling = 50; /* ms */
  528. break;
  529. case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
  530. ir_codes = &ir_codes_kworld_plus_tv_analog_table;
  531. mask_keycode = 0x7f;
  532. polling = 40; /* ms */
  533. break;
  534. case SAA7134_BOARD_VIDEOMATE_S350:
  535. ir_codes = &ir_codes_videomate_s350_table;
  536. mask_keycode = 0x003f00;
  537. mask_keydown = 0x040000;
  538. break;
  539. }
  540. if (NULL == ir_codes) {
  541. printk("%s: Oops: IR config error [card=%d]\n",
  542. dev->name, dev->board);
  543. return -ENODEV;
  544. }
  545. ir = kzalloc(sizeof(*ir), GFP_KERNEL);
  546. input_dev = input_allocate_device();
  547. if (!ir || !input_dev) {
  548. err = -ENOMEM;
  549. goto err_out_free;
  550. }
  551. ir->dev = input_dev;
  552. /* init hardware-specific stuff */
  553. ir->mask_keycode = mask_keycode;
  554. ir->mask_keydown = mask_keydown;
  555. ir->mask_keyup = mask_keyup;
  556. ir->polling = polling;
  557. ir->rc5_gpio = rc5_gpio;
  558. ir->nec_gpio = nec_gpio;
  559. /* init input device */
  560. snprintf(ir->name, sizeof(ir->name), "saa7134 IR (%s)",
  561. saa7134_boards[dev->board].name);
  562. snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
  563. pci_name(dev->pci));
  564. ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
  565. input_dev->name = ir->name;
  566. input_dev->phys = ir->phys;
  567. input_dev->id.bustype = BUS_PCI;
  568. input_dev->id.version = 1;
  569. if (dev->pci->subsystem_vendor) {
  570. input_dev->id.vendor = dev->pci->subsystem_vendor;
  571. input_dev->id.product = dev->pci->subsystem_device;
  572. } else {
  573. input_dev->id.vendor = dev->pci->vendor;
  574. input_dev->id.product = dev->pci->device;
  575. }
  576. input_dev->dev.parent = &dev->pci->dev;
  577. dev->remote = ir;
  578. saa7134_ir_start(dev, ir);
  579. err = input_register_device(ir->dev);
  580. if (err)
  581. goto err_out_stop;
  582. /* the remote isn't as bouncy as a keyboard */
  583. ir->dev->rep[REP_DELAY] = repeat_delay;
  584. ir->dev->rep[REP_PERIOD] = repeat_period;
  585. return 0;
  586. err_out_stop:
  587. saa7134_ir_stop(dev);
  588. dev->remote = NULL;
  589. err_out_free:
  590. input_free_device(input_dev);
  591. kfree(ir);
  592. return err;
  593. }
  594. void saa7134_input_fini(struct saa7134_dev *dev)
  595. {
  596. if (NULL == dev->remote)
  597. return;
  598. saa7134_ir_stop(dev);
  599. input_unregister_device(dev->remote->dev);
  600. kfree(dev->remote);
  601. dev->remote = NULL;
  602. }
  603. void saa7134_probe_i2c_ir(struct saa7134_dev *dev)
  604. {
  605. struct i2c_board_info info;
  606. struct IR_i2c_init_data init_data;
  607. const unsigned short addr_list[] = {
  608. 0x7a, 0x47, 0x71, 0x2d,
  609. I2C_CLIENT_END
  610. };
  611. struct i2c_msg msg_msi = {
  612. .addr = 0x50,
  613. .flags = I2C_M_RD,
  614. .len = 0,
  615. .buf = NULL,
  616. };
  617. int rc;
  618. if (disable_ir) {
  619. dprintk("IR has been disabled, not probing for i2c remote\n");
  620. return;
  621. }
  622. memset(&info, 0, sizeof(struct i2c_board_info));
  623. memset(&init_data, 0, sizeof(struct IR_i2c_init_data));
  624. strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
  625. switch (dev->board) {
  626. case SAA7134_BOARD_PINNACLE_PCTV_110i:
  627. case SAA7134_BOARD_PINNACLE_PCTV_310i:
  628. init_data.name = "Pinnacle PCTV";
  629. if (pinnacle_remote == 0) {
  630. init_data.get_key = get_key_pinnacle_color;
  631. init_data.ir_codes = &ir_codes_pinnacle_color_table;
  632. info.addr = 0x47;
  633. } else {
  634. init_data.get_key = get_key_pinnacle_grey;
  635. init_data.ir_codes = &ir_codes_pinnacle_grey_table;
  636. info.addr = 0x47;
  637. }
  638. break;
  639. case SAA7134_BOARD_UPMOST_PURPLE_TV:
  640. init_data.name = "Purple TV";
  641. init_data.get_key = get_key_purpletv;
  642. init_data.ir_codes = &ir_codes_purpletv_table;
  643. break;
  644. case SAA7134_BOARD_MSI_TVATANYWHERE_PLUS:
  645. init_data.name = "MSI TV@nywhere Plus";
  646. init_data.get_key = get_key_msi_tvanywhere_plus;
  647. init_data.ir_codes = &ir_codes_msi_tvanywhere_plus_table;
  648. info.addr = 0x30;
  649. /* MSI TV@nywhere Plus controller doesn't seem to
  650. respond to probes unless we read something from
  651. an existing device. Weird...
  652. REVISIT: might no longer be needed */
  653. rc = i2c_transfer(&dev->i2c_adap, &msg_msi, 1);
  654. dprintk(KERN_DEBUG "probe 0x%02x @ %s: %s\n",
  655. msg_msi.addr, dev->i2c_adap.name,
  656. (1 == rc) ? "yes" : "no");
  657. break;
  658. case SAA7134_BOARD_HAUPPAUGE_HVR1110:
  659. init_data.name = "HVR 1110";
  660. init_data.get_key = get_key_hvr1110;
  661. init_data.ir_codes = &ir_codes_hauppauge_new_table;
  662. break;
  663. case SAA7134_BOARD_BEHOLD_607FM_MK3:
  664. case SAA7134_BOARD_BEHOLD_607FM_MK5:
  665. case SAA7134_BOARD_BEHOLD_609FM_MK3:
  666. case SAA7134_BOARD_BEHOLD_609FM_MK5:
  667. case SAA7134_BOARD_BEHOLD_607RDS_MK3:
  668. case SAA7134_BOARD_BEHOLD_607RDS_MK5:
  669. case SAA7134_BOARD_BEHOLD_609RDS_MK3:
  670. case SAA7134_BOARD_BEHOLD_609RDS_MK5:
  671. case SAA7134_BOARD_BEHOLD_M6:
  672. case SAA7134_BOARD_BEHOLD_M63:
  673. case SAA7134_BOARD_BEHOLD_M6_EXTRA:
  674. case SAA7134_BOARD_BEHOLD_H6:
  675. case SAA7134_BOARD_BEHOLD_X7:
  676. init_data.name = "BeholdTV";
  677. init_data.get_key = get_key_beholdm6xx;
  678. init_data.ir_codes = &ir_codes_behold_table;
  679. break;
  680. case SAA7134_BOARD_AVERMEDIA_CARDBUS_501:
  681. case SAA7134_BOARD_AVERMEDIA_CARDBUS_506:
  682. info.addr = 0x40;
  683. break;
  684. }
  685. if (init_data.name)
  686. info.platform_data = &init_data;
  687. /* No need to probe if address is known */
  688. if (info.addr) {
  689. i2c_new_device(&dev->i2c_adap, &info);
  690. return;
  691. }
  692. /* Address not known, fallback to probing */
  693. i2c_new_probed_device(&dev->i2c_adap, &info, addr_list);
  694. }
  695. static int saa7134_rc5_irq(struct saa7134_dev *dev)
  696. {
  697. struct card_ir *ir = dev->remote;
  698. struct timeval tv;
  699. u32 gap;
  700. unsigned long current_jiffies, timeout;
  701. /* get time of bit */
  702. current_jiffies = jiffies;
  703. do_gettimeofday(&tv);
  704. /* avoid overflow with gap >1s */
  705. if (tv.tv_sec - ir->base_time.tv_sec > 1) {
  706. gap = 200000;
  707. } else {
  708. gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
  709. tv.tv_usec - ir->base_time.tv_usec;
  710. }
  711. /* active code => add bit */
  712. if (ir->active) {
  713. /* only if in the code (otherwise spurious IRQ or timer
  714. late) */
  715. if (ir->last_bit < 28) {
  716. ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
  717. ir_rc5_remote_gap;
  718. ir->code |= 1 << ir->last_bit;
  719. }
  720. /* starting new code */
  721. } else {
  722. ir->active = 1;
  723. ir->code = 0;
  724. ir->base_time = tv;
  725. ir->last_bit = 0;
  726. timeout = current_jiffies + (500 + 30 * HZ) / 1000;
  727. mod_timer(&ir->timer_end, timeout);
  728. }
  729. return 1;
  730. }
  731. /* On NEC protocol, One has 2.25 ms, and zero has 1.125 ms
  732. The first pulse (start) has 9 + 4.5 ms
  733. */
  734. static void saa7134_nec_timer(unsigned long data)
  735. {
  736. struct saa7134_dev *dev = (struct saa7134_dev *) data;
  737. struct card_ir *ir = dev->remote;
  738. dprintk("Cancel key repeat\n");
  739. ir_input_nokey(ir->dev, &ir->ir);
  740. }
  741. static void nec_task(unsigned long data)
  742. {
  743. struct saa7134_dev *dev = (struct saa7134_dev *) data;
  744. struct card_ir *ir;
  745. struct timeval tv;
  746. int count, pulse, oldpulse, gap;
  747. u32 ircode = 0, not_code = 0;
  748. int ngap = 0;
  749. if (!data) {
  750. printk(KERN_ERR "saa713x/ir: Can't recover dev struct\n");
  751. /* GPIO will be kept disabled */
  752. return;
  753. }
  754. ir = dev->remote;
  755. /* rising SAA7134_GPIO_GPRESCAN reads the status */
  756. saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
  757. saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
  758. oldpulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & ir->mask_keydown;
  759. pulse = oldpulse;
  760. do_gettimeofday(&tv);
  761. ir->base_time = tv;
  762. /* Decode NEC pulsecode. This code can take up to 76.5 ms to run.
  763. Unfortunately, using IRQ to decode pulse didn't work, since it uses
  764. a pulse train of 38KHz. This means one pulse on each 52 us
  765. */
  766. do {
  767. /* Wait until the end of pulse/space or 5 ms */
  768. for (count = 0; count < 500; count++) {
  769. udelay(10);
  770. /* rising SAA7134_GPIO_GPRESCAN reads the status */
  771. saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
  772. saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
  773. pulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2)
  774. & ir->mask_keydown;
  775. if (pulse != oldpulse)
  776. break;
  777. }
  778. do_gettimeofday(&tv);
  779. gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
  780. tv.tv_usec - ir->base_time.tv_usec;
  781. if (!pulse) {
  782. /* Bit 0 has 560 us, while bit 1 has 1120 us.
  783. Do something only if bit == 1
  784. */
  785. if (ngap && (gap > 560 + 280)) {
  786. unsigned int shift = ngap - 1;
  787. /* Address first, then command */
  788. if (shift < 8) {
  789. shift += 8;
  790. ircode |= 1 << shift;
  791. } else if (shift < 16) {
  792. not_code |= 1 << shift;
  793. } else if (shift < 24) {
  794. shift -= 16;
  795. ircode |= 1 << shift;
  796. } else {
  797. shift -= 24;
  798. not_code |= 1 << shift;
  799. }
  800. }
  801. ngap++;
  802. }
  803. ir->base_time = tv;
  804. /* TIMEOUT - Long pulse */
  805. if (gap >= 5000)
  806. break;
  807. oldpulse = pulse;
  808. } while (ngap < 32);
  809. if (ngap == 32) {
  810. /* FIXME: should check if not_code == ~ircode */
  811. ir->code = ir_extract_bits(ircode, ir->mask_keycode);
  812. dprintk("scancode = 0x%02x (code = 0x%02x, notcode= 0x%02x)\n",
  813. ir->code, ircode, not_code);
  814. ir_input_keydown(ir->dev, &ir->ir, ir->code, ir->code);
  815. } else
  816. dprintk("Repeat last key\n");
  817. /* Keep repeating the last key */
  818. mod_timer(&ir->timer_keyup, jiffies + msecs_to_jiffies(150));
  819. saa_setl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
  820. }
  821. static int saa7134_nec_irq(struct saa7134_dev *dev)
  822. {
  823. struct card_ir *ir = dev->remote;
  824. saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
  825. tasklet_schedule(&ir->tlet);
  826. return 1;
  827. }