saa7134-input.c 25 KB

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