saa7134-input.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  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->running)
  341. return;
  342. ir->running = 1;
  343. if (ir->polling) {
  344. setup_timer(&ir->timer, saa7134_input_timer,
  345. (unsigned long)dev);
  346. ir->timer.expires = jiffies + HZ;
  347. add_timer(&ir->timer);
  348. } else if (ir->rc5_gpio) {
  349. /* set timer_end for code completion */
  350. init_timer(&ir->timer_end);
  351. ir->timer_end.function = ir_rc5_timer_end;
  352. ir->timer_end.data = (unsigned long)ir;
  353. init_timer(&ir->timer_keyup);
  354. ir->timer_keyup.function = ir_rc5_timer_keyup;
  355. ir->timer_keyup.data = (unsigned long)ir;
  356. ir->shift_by = 2;
  357. ir->start = 0x2;
  358. ir->addr = 0x17;
  359. ir->rc5_key_timeout = ir_rc5_key_timeout;
  360. ir->rc5_remote_gap = ir_rc5_remote_gap;
  361. } else if (ir->nec_gpio) {
  362. setup_timer(&ir->timer_keyup, saa7134_nec_timer,
  363. (unsigned long)dev);
  364. tasklet_init(&ir->tlet, nec_task, (unsigned long)dev);
  365. }
  366. }
  367. void saa7134_ir_stop(struct saa7134_dev *dev)
  368. {
  369. struct card_ir *ir = dev->remote;
  370. if (!ir->running)
  371. return;
  372. if (dev->remote->polling)
  373. del_timer_sync(&dev->remote->timer);
  374. else if (ir->rc5_gpio)
  375. del_timer_sync(&ir->timer_end);
  376. else if (ir->nec_gpio)
  377. tasklet_kill(&ir->tlet);
  378. ir->running = 0;
  379. }
  380. int saa7134_ir_change_protocol(void *priv, u64 ir_type)
  381. {
  382. struct saa7134_dev *dev = priv;
  383. struct card_ir *ir = dev->remote;
  384. u32 nec_gpio, rc5_gpio;
  385. if (ir_type == IR_TYPE_RC5) {
  386. dprintk("Changing protocol to RC5\n");
  387. nec_gpio = 0;
  388. rc5_gpio = 1;
  389. } else if (ir_type == IR_TYPE_NEC) {
  390. dprintk("Changing protocol to NEC\n");
  391. nec_gpio = 1;
  392. rc5_gpio = 0;
  393. } else {
  394. dprintk("IR protocol type %ud is not supported\n",
  395. (unsigned)ir_type);
  396. return -EINVAL;
  397. }
  398. if (ir->running) {
  399. saa7134_ir_stop(dev);
  400. ir->nec_gpio = nec_gpio;
  401. ir->rc5_gpio = rc5_gpio;
  402. saa7134_ir_start(dev, ir);
  403. } else {
  404. ir->nec_gpio = nec_gpio;
  405. ir->rc5_gpio = rc5_gpio;
  406. }
  407. return 0;
  408. }
  409. int saa7134_input_init1(struct saa7134_dev *dev)
  410. {
  411. struct card_ir *ir;
  412. struct input_dev *input_dev;
  413. struct ir_scancode_table *ir_codes = NULL;
  414. u32 mask_keycode = 0;
  415. u32 mask_keydown = 0;
  416. u32 mask_keyup = 0;
  417. int polling = 0;
  418. int rc5_gpio = 0;
  419. int nec_gpio = 0;
  420. u64 ir_type = IR_TYPE_OTHER;
  421. int err;
  422. if (dev->has_remote != SAA7134_REMOTE_GPIO)
  423. return -ENODEV;
  424. if (disable_ir)
  425. return -ENODEV;
  426. /* detect & configure */
  427. switch (dev->board) {
  428. case SAA7134_BOARD_FLYVIDEO2000:
  429. case SAA7134_BOARD_FLYVIDEO3000:
  430. case SAA7134_BOARD_HAWELL_HW_404M7:
  431. case SAA7134_BOARD_FLYTVPLATINUM_FM:
  432. case SAA7134_BOARD_FLYTVPLATINUM_MINI2:
  433. case SAA7134_BOARD_ROVERMEDIA_LINK_PRO_FM:
  434. ir_codes = &ir_codes_flyvideo_table;
  435. mask_keycode = 0xEC00000;
  436. mask_keydown = 0x0040000;
  437. break;
  438. case SAA7134_BOARD_CINERGY400:
  439. case SAA7134_BOARD_CINERGY600:
  440. case SAA7134_BOARD_CINERGY600_MK3:
  441. ir_codes = &ir_codes_cinergy_table;
  442. mask_keycode = 0x00003f;
  443. mask_keyup = 0x040000;
  444. break;
  445. case SAA7134_BOARD_ECS_TVP3XP:
  446. case SAA7134_BOARD_ECS_TVP3XP_4CB5:
  447. ir_codes = &ir_codes_eztv_table;
  448. mask_keycode = 0x00017c;
  449. mask_keyup = 0x000002;
  450. polling = 50; // ms
  451. break;
  452. case SAA7134_BOARD_KWORLD_XPERT:
  453. case SAA7134_BOARD_AVACSSMARTTV:
  454. ir_codes = &ir_codes_pixelview_table;
  455. mask_keycode = 0x00001F;
  456. mask_keyup = 0x000020;
  457. polling = 50; // ms
  458. break;
  459. case SAA7134_BOARD_MD2819:
  460. case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
  461. case SAA7134_BOARD_AVERMEDIA_305:
  462. case SAA7134_BOARD_AVERMEDIA_307:
  463. case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
  464. case SAA7134_BOARD_AVERMEDIA_STUDIO_505:
  465. case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
  466. case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
  467. case SAA7134_BOARD_AVERMEDIA_STUDIO_507UA:
  468. case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
  469. case SAA7134_BOARD_AVERMEDIA_M102:
  470. case SAA7134_BOARD_AVERMEDIA_GO_007_FM_PLUS:
  471. ir_codes = &ir_codes_avermedia_table;
  472. mask_keycode = 0x0007C8;
  473. mask_keydown = 0x000010;
  474. polling = 50; // ms
  475. /* Set GPIO pin2 to high to enable the IR controller */
  476. saa_setb(SAA7134_GPIO_GPMODE0, 0x4);
  477. saa_setb(SAA7134_GPIO_GPSTATUS0, 0x4);
  478. break;
  479. case SAA7134_BOARD_AVERMEDIA_M135A:
  480. ir_codes = &ir_codes_avermedia_m135a_rm_jx_table;
  481. mask_keydown = 0x0040000;
  482. mask_keycode = 0xffff;
  483. nec_gpio = 1;
  484. break;
  485. case SAA7134_BOARD_AVERMEDIA_777:
  486. case SAA7134_BOARD_AVERMEDIA_A16AR:
  487. ir_codes = &ir_codes_avermedia_table;
  488. mask_keycode = 0x02F200;
  489. mask_keydown = 0x000400;
  490. polling = 50; // ms
  491. /* Without this we won't receive key up events */
  492. saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
  493. saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
  494. break;
  495. case SAA7134_BOARD_AVERMEDIA_A16D:
  496. ir_codes = &ir_codes_avermedia_a16d_table;
  497. mask_keycode = 0x02F200;
  498. mask_keydown = 0x000400;
  499. polling = 50; /* ms */
  500. /* Without this we won't receive key up events */
  501. saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
  502. saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
  503. break;
  504. case SAA7134_BOARD_KWORLD_TERMINATOR:
  505. ir_codes = &ir_codes_pixelview_table;
  506. mask_keycode = 0x00001f;
  507. mask_keyup = 0x000060;
  508. polling = 50; // ms
  509. break;
  510. case SAA7134_BOARD_MANLI_MTV001:
  511. case SAA7134_BOARD_MANLI_MTV002:
  512. ir_codes = &ir_codes_manli_table;
  513. mask_keycode = 0x001f00;
  514. mask_keyup = 0x004000;
  515. polling = 50; /* ms */
  516. break;
  517. case SAA7134_BOARD_BEHOLD_409FM:
  518. case SAA7134_BOARD_BEHOLD_401:
  519. case SAA7134_BOARD_BEHOLD_403:
  520. case SAA7134_BOARD_BEHOLD_403FM:
  521. case SAA7134_BOARD_BEHOLD_405:
  522. case SAA7134_BOARD_BEHOLD_405FM:
  523. case SAA7134_BOARD_BEHOLD_407:
  524. case SAA7134_BOARD_BEHOLD_407FM:
  525. case SAA7134_BOARD_BEHOLD_409:
  526. case SAA7134_BOARD_BEHOLD_505FM:
  527. case SAA7134_BOARD_BEHOLD_505RDS_MK5:
  528. case SAA7134_BOARD_BEHOLD_505RDS_MK3:
  529. case SAA7134_BOARD_BEHOLD_507_9FM:
  530. case SAA7134_BOARD_BEHOLD_507RDS_MK3:
  531. case SAA7134_BOARD_BEHOLD_507RDS_MK5:
  532. ir_codes = &ir_codes_manli_table;
  533. mask_keycode = 0x003f00;
  534. mask_keyup = 0x004000;
  535. polling = 50; /* ms */
  536. break;
  537. case SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM:
  538. ir_codes = &ir_codes_behold_columbus_table;
  539. mask_keycode = 0x003f00;
  540. mask_keyup = 0x004000;
  541. polling = 50; // ms
  542. break;
  543. case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
  544. ir_codes = &ir_codes_pctv_sedna_table;
  545. mask_keycode = 0x001f00;
  546. mask_keyup = 0x004000;
  547. polling = 50; // ms
  548. break;
  549. case SAA7134_BOARD_GOTVIEW_7135:
  550. ir_codes = &ir_codes_gotview7135_table;
  551. mask_keycode = 0x0003CC;
  552. mask_keydown = 0x000010;
  553. polling = 5; /* ms */
  554. saa_setb(SAA7134_GPIO_GPMODE1, 0x80);
  555. break;
  556. case SAA7134_BOARD_VIDEOMATE_TV_PVR:
  557. case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
  558. case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
  559. ir_codes = &ir_codes_videomate_tv_pvr_table;
  560. mask_keycode = 0x00003F;
  561. mask_keyup = 0x400000;
  562. polling = 50; // ms
  563. break;
  564. case SAA7134_BOARD_PROTEUS_2309:
  565. ir_codes = &ir_codes_proteus_2309_table;
  566. mask_keycode = 0x00007F;
  567. mask_keyup = 0x000080;
  568. polling = 50; // ms
  569. break;
  570. case SAA7134_BOARD_VIDEOMATE_DVBT_300:
  571. case SAA7134_BOARD_VIDEOMATE_DVBT_200:
  572. ir_codes = &ir_codes_videomate_tv_pvr_table;
  573. mask_keycode = 0x003F00;
  574. mask_keyup = 0x040000;
  575. break;
  576. case SAA7134_BOARD_FLYDVBS_LR300:
  577. case SAA7134_BOARD_FLYDVBT_LR301:
  578. case SAA7134_BOARD_FLYDVBTDUO:
  579. ir_codes = &ir_codes_flydvb_table;
  580. mask_keycode = 0x0001F00;
  581. mask_keydown = 0x0040000;
  582. break;
  583. case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
  584. case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
  585. case SAA7134_BOARD_ASUSTeK_P7131_ANALOG:
  586. ir_codes = &ir_codes_asus_pc39_table;
  587. mask_keydown = 0x0040000;
  588. rc5_gpio = 1;
  589. break;
  590. case SAA7134_BOARD_ENCORE_ENLTV:
  591. case SAA7134_BOARD_ENCORE_ENLTV_FM:
  592. ir_codes = &ir_codes_encore_enltv_table;
  593. mask_keycode = 0x00007f;
  594. mask_keyup = 0x040000;
  595. polling = 50; // ms
  596. break;
  597. case SAA7134_BOARD_ENCORE_ENLTV_FM53:
  598. ir_codes = &ir_codes_encore_enltv_fm53_table;
  599. mask_keydown = 0x0040000;
  600. mask_keycode = 0x00007f;
  601. nec_gpio = 1;
  602. break;
  603. case SAA7134_BOARD_10MOONSTVMASTER3:
  604. ir_codes = &ir_codes_encore_enltv_table;
  605. mask_keycode = 0x5f80000;
  606. mask_keyup = 0x8000000;
  607. polling = 50; //ms
  608. break;
  609. case SAA7134_BOARD_GENIUS_TVGO_A11MCE:
  610. ir_codes = &ir_codes_genius_tvgo_a11mce_table;
  611. mask_keycode = 0xff;
  612. mask_keydown = 0xf00000;
  613. polling = 50; /* ms */
  614. break;
  615. case SAA7134_BOARD_REAL_ANGEL_220:
  616. ir_codes = &ir_codes_real_audio_220_32_keys_table;
  617. mask_keycode = 0x3f00;
  618. mask_keyup = 0x4000;
  619. polling = 50; /* ms */
  620. break;
  621. case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
  622. ir_codes = &ir_codes_kworld_plus_tv_analog_table;
  623. mask_keycode = 0x7f;
  624. polling = 40; /* ms */
  625. break;
  626. case SAA7134_BOARD_VIDEOMATE_S350:
  627. ir_codes = &ir_codes_videomate_s350_table;
  628. mask_keycode = 0x003f00;
  629. mask_keydown = 0x040000;
  630. break;
  631. case SAA7134_BOARD_LEADTEK_WINFAST_DTV1000S:
  632. ir_codes = &ir_codes_winfast_table;
  633. mask_keycode = 0x5f00;
  634. mask_keyup = 0x020000;
  635. polling = 50; /* ms */
  636. break;
  637. break;
  638. }
  639. if (NULL == ir_codes) {
  640. printk("%s: Oops: IR config error [card=%d]\n",
  641. dev->name, dev->board);
  642. return -ENODEV;
  643. }
  644. ir = kzalloc(sizeof(*ir), GFP_KERNEL);
  645. input_dev = input_allocate_device();
  646. if (!ir || !input_dev) {
  647. err = -ENOMEM;
  648. goto err_out_free;
  649. }
  650. ir->dev = input_dev;
  651. dev->remote = ir;
  652. ir->running = 0;
  653. /* init hardware-specific stuff */
  654. ir->mask_keycode = mask_keycode;
  655. ir->mask_keydown = mask_keydown;
  656. ir->mask_keyup = mask_keyup;
  657. ir->polling = polling;
  658. ir->rc5_gpio = rc5_gpio;
  659. ir->nec_gpio = nec_gpio;
  660. /* init input device */
  661. snprintf(ir->name, sizeof(ir->name), "saa7134 IR (%s)",
  662. saa7134_boards[dev->board].name);
  663. snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
  664. pci_name(dev->pci));
  665. if (ir_codes->ir_type != IR_TYPE_OTHER) {
  666. ir->props.allowed_protos = IR_TYPE_RC5 | IR_TYPE_NEC;
  667. ir->props.priv = dev;
  668. ir->props.change_protocol = saa7134_ir_change_protocol;
  669. /* Set IR protocol */
  670. saa7134_ir_change_protocol(ir->props.priv, ir_codes->ir_type);
  671. }
  672. err = ir_input_init(input_dev, &ir->ir, ir_type);
  673. if (err < 0)
  674. goto err_out_free;
  675. input_dev->name = ir->name;
  676. input_dev->phys = ir->phys;
  677. input_dev->id.bustype = BUS_PCI;
  678. input_dev->id.version = 1;
  679. if (dev->pci->subsystem_vendor) {
  680. input_dev->id.vendor = dev->pci->subsystem_vendor;
  681. input_dev->id.product = dev->pci->subsystem_device;
  682. } else {
  683. input_dev->id.vendor = dev->pci->vendor;
  684. input_dev->id.product = dev->pci->device;
  685. }
  686. input_dev->dev.parent = &dev->pci->dev;
  687. err = ir_input_register(ir->dev, ir_codes, &ir->props, MODULE_NAME);
  688. if (err)
  689. goto err_out_stop;
  690. saa7134_ir_start(dev, ir);
  691. /* the remote isn't as bouncy as a keyboard */
  692. ir->dev->rep[REP_DELAY] = repeat_delay;
  693. ir->dev->rep[REP_PERIOD] = repeat_period;
  694. return 0;
  695. err_out_stop:
  696. saa7134_ir_stop(dev);
  697. dev->remote = NULL;
  698. err_out_free:
  699. kfree(ir);
  700. return err;
  701. }
  702. void saa7134_input_fini(struct saa7134_dev *dev)
  703. {
  704. if (NULL == dev->remote)
  705. return;
  706. saa7134_ir_stop(dev);
  707. ir_input_unregister(dev->remote->dev);
  708. kfree(dev->remote);
  709. dev->remote = NULL;
  710. }
  711. void saa7134_probe_i2c_ir(struct saa7134_dev *dev)
  712. {
  713. struct i2c_board_info info;
  714. struct i2c_msg msg_msi = {
  715. .addr = 0x50,
  716. .flags = I2C_M_RD,
  717. .len = 0,
  718. .buf = NULL,
  719. };
  720. int rc;
  721. if (disable_ir) {
  722. dprintk("IR has been disabled, not probing for i2c remote\n");
  723. return;
  724. }
  725. memset(&info, 0, sizeof(struct i2c_board_info));
  726. memset(&dev->init_data, 0, sizeof(dev->init_data));
  727. strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
  728. switch (dev->board) {
  729. case SAA7134_BOARD_PINNACLE_PCTV_110i:
  730. case SAA7134_BOARD_PINNACLE_PCTV_310i:
  731. dev->init_data.name = "Pinnacle PCTV";
  732. if (pinnacle_remote == 0) {
  733. dev->init_data.get_key = get_key_pinnacle_color;
  734. dev->init_data.ir_codes = &ir_codes_pinnacle_color_table;
  735. info.addr = 0x47;
  736. } else {
  737. dev->init_data.get_key = get_key_pinnacle_grey;
  738. dev->init_data.ir_codes = &ir_codes_pinnacle_grey_table;
  739. info.addr = 0x47;
  740. }
  741. break;
  742. case SAA7134_BOARD_UPMOST_PURPLE_TV:
  743. dev->init_data.name = "Purple TV";
  744. dev->init_data.get_key = get_key_purpletv;
  745. dev->init_data.ir_codes = &ir_codes_purpletv_table;
  746. info.addr = 0x7a;
  747. break;
  748. case SAA7134_BOARD_MSI_TVATANYWHERE_PLUS:
  749. dev->init_data.name = "MSI TV@nywhere Plus";
  750. dev->init_data.get_key = get_key_msi_tvanywhere_plus;
  751. dev->init_data.ir_codes = &ir_codes_msi_tvanywhere_plus_table;
  752. info.addr = 0x30;
  753. /* MSI TV@nywhere Plus controller doesn't seem to
  754. respond to probes unless we read something from
  755. an existing device. Weird...
  756. REVISIT: might no longer be needed */
  757. rc = i2c_transfer(&dev->i2c_adap, &msg_msi, 1);
  758. dprintk(KERN_DEBUG "probe 0x%02x @ %s: %s\n",
  759. msg_msi.addr, dev->i2c_adap.name,
  760. (1 == rc) ? "yes" : "no");
  761. break;
  762. case SAA7134_BOARD_HAUPPAUGE_HVR1110:
  763. dev->init_data.name = "HVR 1110";
  764. dev->init_data.get_key = get_key_hvr1110;
  765. dev->init_data.ir_codes = &ir_codes_hauppauge_new_table;
  766. info.addr = 0x71;
  767. break;
  768. case SAA7134_BOARD_BEHOLD_607FM_MK3:
  769. case SAA7134_BOARD_BEHOLD_607FM_MK5:
  770. case SAA7134_BOARD_BEHOLD_609FM_MK3:
  771. case SAA7134_BOARD_BEHOLD_609FM_MK5:
  772. case SAA7134_BOARD_BEHOLD_607RDS_MK3:
  773. case SAA7134_BOARD_BEHOLD_607RDS_MK5:
  774. case SAA7134_BOARD_BEHOLD_609RDS_MK3:
  775. case SAA7134_BOARD_BEHOLD_609RDS_MK5:
  776. case SAA7134_BOARD_BEHOLD_M6:
  777. case SAA7134_BOARD_BEHOLD_M63:
  778. case SAA7134_BOARD_BEHOLD_M6_EXTRA:
  779. case SAA7134_BOARD_BEHOLD_H6:
  780. case SAA7134_BOARD_BEHOLD_X7:
  781. dev->init_data.name = "BeholdTV";
  782. dev->init_data.get_key = get_key_beholdm6xx;
  783. dev->init_data.ir_codes = &ir_codes_behold_table;
  784. dev->init_data.type = IR_TYPE_NEC;
  785. info.addr = 0x2d;
  786. break;
  787. case SAA7134_BOARD_AVERMEDIA_CARDBUS_501:
  788. case SAA7134_BOARD_AVERMEDIA_CARDBUS_506:
  789. info.addr = 0x40;
  790. break;
  791. case SAA7134_BOARD_FLYDVB_TRIO:
  792. dev->init_data.name = "FlyDVB Trio";
  793. dev->init_data.get_key = get_key_flydvb_trio;
  794. dev->init_data.ir_codes = &ir_codes_flydvb_table;
  795. info.addr = 0x0b;
  796. break;
  797. default:
  798. dprintk("No I2C IR support for board %x\n", dev->board);
  799. return;
  800. }
  801. if (dev->init_data.name)
  802. info.platform_data = &dev->init_data;
  803. i2c_new_device(&dev->i2c_adap, &info);
  804. }
  805. static int saa7134_rc5_irq(struct saa7134_dev *dev)
  806. {
  807. struct card_ir *ir = dev->remote;
  808. struct timeval tv;
  809. u32 gap;
  810. unsigned long current_jiffies, timeout;
  811. /* get time of bit */
  812. current_jiffies = jiffies;
  813. do_gettimeofday(&tv);
  814. /* avoid overflow with gap >1s */
  815. if (tv.tv_sec - ir->base_time.tv_sec > 1) {
  816. gap = 200000;
  817. } else {
  818. gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
  819. tv.tv_usec - ir->base_time.tv_usec;
  820. }
  821. /* active code => add bit */
  822. if (ir->active) {
  823. /* only if in the code (otherwise spurious IRQ or timer
  824. late) */
  825. if (ir->last_bit < 28) {
  826. ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
  827. ir_rc5_remote_gap;
  828. ir->code |= 1 << ir->last_bit;
  829. }
  830. /* starting new code */
  831. } else {
  832. ir->active = 1;
  833. ir->code = 0;
  834. ir->base_time = tv;
  835. ir->last_bit = 0;
  836. timeout = current_jiffies + (500 + 30 * HZ) / 1000;
  837. mod_timer(&ir->timer_end, timeout);
  838. }
  839. return 1;
  840. }
  841. /* On NEC protocol, One has 2.25 ms, and zero has 1.125 ms
  842. The first pulse (start) has 9 + 4.5 ms
  843. */
  844. static void saa7134_nec_timer(unsigned long data)
  845. {
  846. struct saa7134_dev *dev = (struct saa7134_dev *) data;
  847. struct card_ir *ir = dev->remote;
  848. dprintk("Cancel key repeat\n");
  849. ir_input_nokey(ir->dev, &ir->ir);
  850. }
  851. static void nec_task(unsigned long data)
  852. {
  853. struct saa7134_dev *dev = (struct saa7134_dev *) data;
  854. struct card_ir *ir;
  855. struct timeval tv;
  856. int count, pulse, oldpulse, gap;
  857. u32 ircode = 0, not_code = 0;
  858. int ngap = 0;
  859. if (!data) {
  860. printk(KERN_ERR "saa713x/ir: Can't recover dev struct\n");
  861. /* GPIO will be kept disabled */
  862. return;
  863. }
  864. ir = dev->remote;
  865. /* rising SAA7134_GPIO_GPRESCAN reads the status */
  866. saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
  867. saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
  868. oldpulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & ir->mask_keydown;
  869. pulse = oldpulse;
  870. do_gettimeofday(&tv);
  871. ir->base_time = tv;
  872. /* Decode NEC pulsecode. This code can take up to 76.5 ms to run.
  873. Unfortunately, using IRQ to decode pulse didn't work, since it uses
  874. a pulse train of 38KHz. This means one pulse on each 52 us
  875. */
  876. do {
  877. /* Wait until the end of pulse/space or 5 ms */
  878. for (count = 0; count < 500; count++) {
  879. udelay(10);
  880. /* rising SAA7134_GPIO_GPRESCAN reads the status */
  881. saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
  882. saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
  883. pulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2)
  884. & ir->mask_keydown;
  885. if (pulse != oldpulse)
  886. break;
  887. }
  888. do_gettimeofday(&tv);
  889. gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
  890. tv.tv_usec - ir->base_time.tv_usec;
  891. if (!pulse) {
  892. /* Bit 0 has 560 us, while bit 1 has 1120 us.
  893. Do something only if bit == 1
  894. */
  895. if (ngap && (gap > 560 + 280)) {
  896. unsigned int shift = ngap - 1;
  897. /* Address first, then command */
  898. if (shift < 8) {
  899. shift += 8;
  900. ircode |= 1 << shift;
  901. } else if (shift < 16) {
  902. not_code |= 1 << shift;
  903. } else if (shift < 24) {
  904. shift -= 16;
  905. ircode |= 1 << shift;
  906. } else {
  907. shift -= 24;
  908. not_code |= 1 << shift;
  909. }
  910. }
  911. ngap++;
  912. }
  913. ir->base_time = tv;
  914. /* TIMEOUT - Long pulse */
  915. if (gap >= 5000)
  916. break;
  917. oldpulse = pulse;
  918. } while (ngap < 32);
  919. if (ngap == 32) {
  920. /* FIXME: should check if not_code == ~ircode */
  921. ir->code = ir_extract_bits(ircode, ir->mask_keycode);
  922. dprintk("scancode = 0x%02x (code = 0x%02x, notcode= 0x%02x)\n",
  923. ir->code, ircode, not_code);
  924. ir_input_keydown(ir->dev, &ir->ir, ir->code);
  925. } else
  926. dprintk("Repeat last key\n");
  927. /* Keep repeating the last key */
  928. mod_timer(&ir->timer_keyup, jiffies + msecs_to_jiffies(150));
  929. saa_setl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
  930. }
  931. static int saa7134_nec_irq(struct saa7134_dev *dev)
  932. {
  933. struct card_ir *ir = dev->remote;
  934. saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
  935. tasklet_schedule(&ir->tlet);
  936. return 1;
  937. }