saa7134-input.c 27 KB

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