ir-kbd-gpio.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. /*
  2. *
  3. * Copyright (c) 2003 Gerd Knorr
  4. * Copyright (c) 2003 Pavel Machek
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/module.h>
  21. #include <linux/moduleparam.h>
  22. #include <linux/init.h>
  23. #include <linux/delay.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/input.h>
  26. #include <linux/pci.h>
  27. #include <media/ir-common.h>
  28. #include "bttv.h"
  29. /* ---------------------------------------------------------------------- */
  30. static IR_KEYTAB_TYPE ir_codes_avermedia[IR_KEYTAB_SIZE] = {
  31. [ 34 ] = KEY_KP0,
  32. [ 40 ] = KEY_KP1,
  33. [ 24 ] = KEY_KP2,
  34. [ 56 ] = KEY_KP3,
  35. [ 36 ] = KEY_KP4,
  36. [ 20 ] = KEY_KP5,
  37. [ 52 ] = KEY_KP6,
  38. [ 44 ] = KEY_KP7,
  39. [ 28 ] = KEY_KP8,
  40. [ 60 ] = KEY_KP9,
  41. [ 48 ] = KEY_EJECTCD, // Unmarked on my controller
  42. [ 0 ] = KEY_POWER,
  43. [ 18 ] = BTN_LEFT, // DISPLAY/L
  44. [ 50 ] = BTN_RIGHT, // LOOP/R
  45. [ 10 ] = KEY_MUTE,
  46. [ 38 ] = KEY_RECORD,
  47. [ 22 ] = KEY_PAUSE,
  48. [ 54 ] = KEY_STOP,
  49. [ 30 ] = KEY_VOLUMEDOWN,
  50. [ 62 ] = KEY_VOLUMEUP,
  51. [ 32 ] = KEY_TUNER, // TV/FM
  52. [ 16 ] = KEY_CD,
  53. [ 8 ] = KEY_VIDEO,
  54. [ 4 ] = KEY_AUDIO,
  55. [ 12 ] = KEY_ZOOM, // full screen
  56. [ 2 ] = KEY_INFO, // preview
  57. [ 42 ] = KEY_SEARCH, // autoscan
  58. [ 26 ] = KEY_STOP, // freeze
  59. [ 58 ] = KEY_RECORD, // capture
  60. [ 6 ] = KEY_PLAY, // unmarked
  61. [ 46 ] = KEY_RED, // unmarked
  62. [ 14 ] = KEY_GREEN, // unmarked
  63. [ 33 ] = KEY_YELLOW, // unmarked
  64. [ 17 ] = KEY_CHANNELDOWN,
  65. [ 49 ] = KEY_CHANNELUP,
  66. [ 1 ] = KEY_BLUE, // unmarked
  67. };
  68. /* Matt Jesson <dvb@jesson.eclipse.co.uk */
  69. static IR_KEYTAB_TYPE ir_codes_avermedia_dvbt[IR_KEYTAB_SIZE] = {
  70. [ 0x28 ] = KEY_KP0, //'0' / 'enter'
  71. [ 0x22 ] = KEY_KP1, //'1'
  72. [ 0x12 ] = KEY_KP2, //'2' / 'up arrow'
  73. [ 0x32 ] = KEY_KP3, //'3'
  74. [ 0x24 ] = KEY_KP4, //'4' / 'left arrow'
  75. [ 0x14 ] = KEY_KP5, //'5'
  76. [ 0x34 ] = KEY_KP6, //'6' / 'right arrow'
  77. [ 0x26 ] = KEY_KP7, //'7'
  78. [ 0x16 ] = KEY_KP8, //'8' / 'down arrow'
  79. [ 0x36 ] = KEY_KP9, //'9'
  80. [ 0x20 ] = KEY_LIST, // 'source'
  81. [ 0x10 ] = KEY_TEXT, // 'teletext'
  82. [ 0x00 ] = KEY_POWER, // 'power'
  83. [ 0x04 ] = KEY_AUDIO, // 'audio'
  84. [ 0x06 ] = KEY_ZOOM, // 'full screen'
  85. [ 0x18 ] = KEY_VIDEO, // 'display'
  86. [ 0x38 ] = KEY_SEARCH, // 'loop'
  87. [ 0x08 ] = KEY_INFO, // 'preview'
  88. [ 0x2a ] = KEY_REWIND, // 'backward <<'
  89. [ 0x1a ] = KEY_FASTFORWARD, // 'forward >>'
  90. [ 0x3a ] = KEY_RECORD, // 'capture'
  91. [ 0x0a ] = KEY_MUTE, // 'mute'
  92. [ 0x2c ] = KEY_RECORD, // 'record'
  93. [ 0x1c ] = KEY_PAUSE, // 'pause'
  94. [ 0x3c ] = KEY_STOP, // 'stop'
  95. [ 0x0c ] = KEY_PLAY, // 'play'
  96. [ 0x2e ] = KEY_RED, // 'red'
  97. [ 0x01 ] = KEY_BLUE, // 'blue' / 'cancel'
  98. [ 0x0e ] = KEY_YELLOW, // 'yellow' / 'ok'
  99. [ 0x21 ] = KEY_GREEN, // 'green'
  100. [ 0x11 ] = KEY_CHANNELDOWN, // 'channel -'
  101. [ 0x31 ] = KEY_CHANNELUP, // 'channel +'
  102. [ 0x1e ] = KEY_VOLUMEDOWN, // 'volume -'
  103. [ 0x3e ] = KEY_VOLUMEUP, // 'volume +'
  104. };
  105. /* Attila Kondoros <attila.kondoros@chello.hu> */
  106. static IR_KEYTAB_TYPE ir_codes_apac_viewcomp[IR_KEYTAB_SIZE] = {
  107. [ 1 ] = KEY_KP1,
  108. [ 2 ] = KEY_KP2,
  109. [ 3 ] = KEY_KP3,
  110. [ 4 ] = KEY_KP4,
  111. [ 5 ] = KEY_KP5,
  112. [ 6 ] = KEY_KP6,
  113. [ 7 ] = KEY_KP7,
  114. [ 8 ] = KEY_KP8,
  115. [ 9 ] = KEY_KP9,
  116. [ 0 ] = KEY_KP0,
  117. [ 23 ] = KEY_LAST, // +100
  118. [ 10 ] = KEY_LIST, // recall
  119. [ 28 ] = KEY_TUNER, // TV/FM
  120. [ 21 ] = KEY_SEARCH, // scan
  121. [ 18 ] = KEY_POWER, // power
  122. [ 31 ] = KEY_VOLUMEDOWN, // vol up
  123. [ 27 ] = KEY_VOLUMEUP, // vol down
  124. [ 30 ] = KEY_CHANNELDOWN, // chn up
  125. [ 26 ] = KEY_CHANNELUP, // chn down
  126. [ 17 ] = KEY_VIDEO, // video
  127. [ 15 ] = KEY_ZOOM, // full screen
  128. [ 19 ] = KEY_MUTE, // mute/unmute
  129. [ 16 ] = KEY_TEXT, // min
  130. [ 13 ] = KEY_STOP, // freeze
  131. [ 14 ] = KEY_RECORD, // record
  132. [ 29 ] = KEY_PLAYPAUSE, // stop
  133. [ 25 ] = KEY_PLAY, // play
  134. [ 22 ] = KEY_GOTO, // osd
  135. [ 20 ] = KEY_REFRESH, // default
  136. [ 12 ] = KEY_KPPLUS, // fine tune >>>>
  137. [ 24 ] = KEY_KPMINUS // fine tune <<<<
  138. };
  139. /* ---------------------------------------------------------------------- */
  140. /* Ricardo Cerqueira <v4l@cerqueira.org> */
  141. /* Weird matching, since the remote has "uncommon" keys */
  142. static IR_KEYTAB_TYPE ir_codes_conceptronic[IR_KEYTAB_SIZE] = {
  143. [ 30 ] = KEY_POWER, // power
  144. [ 7 ] = KEY_MEDIA, // source
  145. [ 28 ] = KEY_SEARCH, // scan
  146. /* FIXME: duplicate keycodes?
  147. *
  148. * These four keys seem to share the same GPIO as CH+, CH-, <<< and >>>
  149. * The GPIO values are
  150. * 6397fb for both "Scan <" and "CH -",
  151. * 639ffb for "Scan >" and "CH+",
  152. * 6384fb for "Tune <" and "<<<",
  153. * 638cfb for "Tune >" and ">>>", regardless of the mask.
  154. *
  155. * [ 23 ] = KEY_BACK, // fm scan <<
  156. * [ 31 ] = KEY_FORWARD, // fm scan >>
  157. *
  158. * [ 4 ] = KEY_LEFT, // fm tuning <
  159. * [ 12 ] = KEY_RIGHT, // fm tuning >
  160. *
  161. * For now, these four keys are disabled. Pressing them will generate
  162. * the CH+/CH-/<<</>>> events
  163. */
  164. [ 3 ] = KEY_TUNER, // TV/FM
  165. [ 0 ] = KEY_RECORD,
  166. [ 8 ] = KEY_STOP,
  167. [ 17 ] = KEY_PLAY,
  168. [ 26 ] = KEY_PLAYPAUSE, // freeze
  169. [ 25 ] = KEY_ZOOM, // zoom
  170. [ 15 ] = KEY_TEXT, // min
  171. [ 1 ] = KEY_KP1,
  172. [ 11 ] = KEY_KP2,
  173. [ 27 ] = KEY_KP3,
  174. [ 5 ] = KEY_KP4,
  175. [ 9 ] = KEY_KP5,
  176. [ 21 ] = KEY_KP6,
  177. [ 6 ] = KEY_KP7,
  178. [ 10 ] = KEY_KP8,
  179. [ 18 ] = KEY_KP9,
  180. [ 2 ] = KEY_KP0,
  181. [ 16 ] = KEY_LAST, // +100
  182. [ 19 ] = KEY_LIST, // recall
  183. [ 31 ] = KEY_CHANNELUP, // chn down
  184. [ 23 ] = KEY_CHANNELDOWN, // chn up
  185. [ 22 ] = KEY_VOLUMEUP, // vol down
  186. [ 20 ] = KEY_VOLUMEDOWN, // vol up
  187. [ 4 ] = KEY_KPMINUS, // <<<
  188. [ 14 ] = KEY_SETUP, // function
  189. [ 12 ] = KEY_KPPLUS, // >>>
  190. [ 13 ] = KEY_GOTO, // mts
  191. [ 29 ] = KEY_REFRESH, // reset
  192. [ 24 ] = KEY_MUTE // mute/unmute
  193. };
  194. static IR_KEYTAB_TYPE ir_codes_nebula[IR_KEYTAB_SIZE] = {
  195. [0x00] = KEY_KP0,
  196. [0x01] = KEY_KP1,
  197. [0x02] = KEY_KP2,
  198. [0x03] = KEY_KP3,
  199. [0x04] = KEY_KP4,
  200. [0x05] = KEY_KP5,
  201. [0x06] = KEY_KP6,
  202. [0x07] = KEY_KP7,
  203. [0x08] = KEY_KP8,
  204. [0x09] = KEY_KP9,
  205. [0x0a] = KEY_TV,
  206. [0x0b] = KEY_AUX,
  207. [0x0c] = KEY_DVD,
  208. [0x0d] = KEY_POWER,
  209. [0x0e] = KEY_MHP, /* labelled 'Picture' */
  210. [0x0f] = KEY_AUDIO,
  211. [0x10] = KEY_INFO,
  212. [0x11] = KEY_F13, /* 16:9 */
  213. [0x12] = KEY_F14, /* 14:9 */
  214. [0x13] = KEY_EPG,
  215. [0x14] = KEY_EXIT,
  216. [0x15] = KEY_MENU,
  217. [0x16] = KEY_UP,
  218. [0x17] = KEY_DOWN,
  219. [0x18] = KEY_LEFT,
  220. [0x19] = KEY_RIGHT,
  221. [0x1a] = KEY_ENTER,
  222. [0x1b] = KEY_CHANNELUP,
  223. [0x1c] = KEY_CHANNELDOWN,
  224. [0x1d] = KEY_VOLUMEUP,
  225. [0x1e] = KEY_VOLUMEDOWN,
  226. [0x1f] = KEY_RED,
  227. [0x20] = KEY_GREEN,
  228. [0x21] = KEY_YELLOW,
  229. [0x22] = KEY_BLUE,
  230. [0x23] = KEY_SUBTITLE,
  231. [0x24] = KEY_F15, /* AD */
  232. [0x25] = KEY_TEXT,
  233. [0x26] = KEY_MUTE,
  234. [0x27] = KEY_REWIND,
  235. [0x28] = KEY_STOP,
  236. [0x29] = KEY_PLAY,
  237. [0x2a] = KEY_FASTFORWARD,
  238. [0x2b] = KEY_F16, /* chapter */
  239. [0x2c] = KEY_PAUSE,
  240. [0x2d] = KEY_PLAY,
  241. [0x2e] = KEY_RECORD,
  242. [0x2f] = KEY_F17, /* picture in picture */
  243. [0x30] = KEY_KPPLUS, /* zoom in */
  244. [0x31] = KEY_KPMINUS, /* zoom out */
  245. [0x32] = KEY_F18, /* capture */
  246. [0x33] = KEY_F19, /* web */
  247. [0x34] = KEY_EMAIL,
  248. [0x35] = KEY_PHONE,
  249. [0x36] = KEY_PC
  250. };
  251. struct IR {
  252. struct bttv_sub_device *sub;
  253. struct input_dev *input;
  254. struct ir_input_state ir;
  255. char name[32];
  256. char phys[32];
  257. /* Usual gpio signalling */
  258. u32 mask_keycode;
  259. u32 mask_keydown;
  260. u32 mask_keyup;
  261. u32 polling;
  262. u32 last_gpio;
  263. struct work_struct work;
  264. struct timer_list timer;
  265. /* RC5 gpio */
  266. u32 rc5_gpio;
  267. struct timer_list timer_end; /* timer_end for code completion */
  268. struct timer_list timer_keyup; /* timer_end for key release */
  269. u32 last_rc5; /* last good rc5 code */
  270. u32 last_bit; /* last raw bit seen */
  271. u32 code; /* raw code under construction */
  272. struct timeval base_time; /* time of last seen code */
  273. int active; /* building raw code */
  274. };
  275. static int debug;
  276. module_param(debug, int, 0644); /* debug level (0,1,2) */
  277. static int repeat_delay = 500;
  278. module_param(repeat_delay, int, 0644);
  279. static int repeat_period = 33;
  280. module_param(repeat_period, int, 0644);
  281. #define DEVNAME "ir-kbd-gpio"
  282. #define dprintk(fmt, arg...) if (debug) \
  283. printk(KERN_DEBUG DEVNAME ": " fmt , ## arg)
  284. static void ir_irq(struct bttv_sub_device *sub);
  285. static int ir_probe(struct device *dev);
  286. static int ir_remove(struct device *dev);
  287. static struct bttv_sub_driver driver = {
  288. .drv = {
  289. .name = DEVNAME,
  290. .probe = ir_probe,
  291. .remove = ir_remove,
  292. },
  293. .gpio_irq = ir_irq,
  294. };
  295. /* ---------------------------------------------------------------------- */
  296. static void ir_handle_key(struct IR *ir)
  297. {
  298. u32 gpio,data;
  299. /* read gpio value */
  300. gpio = bttv_gpio_read(ir->sub->core);
  301. if (ir->polling) {
  302. if (ir->last_gpio == gpio)
  303. return;
  304. ir->last_gpio = gpio;
  305. }
  306. /* extract data */
  307. data = ir_extract_bits(gpio, ir->mask_keycode);
  308. dprintk(DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",
  309. gpio, data,
  310. ir->polling ? "poll" : "irq",
  311. (gpio & ir->mask_keydown) ? " down" : "",
  312. (gpio & ir->mask_keyup) ? " up" : "");
  313. if (ir->mask_keydown) {
  314. /* bit set on keydown */
  315. if (gpio & ir->mask_keydown) {
  316. ir_input_keydown(ir->input, &ir->ir, data, data);
  317. } else {
  318. ir_input_nokey(ir->input, &ir->ir);
  319. }
  320. } else if (ir->mask_keyup) {
  321. /* bit cleared on keydown */
  322. if (0 == (gpio & ir->mask_keyup)) {
  323. ir_input_keydown(ir->input, &ir->ir, data, data);
  324. } else {
  325. ir_input_nokey(ir->input, &ir->ir);
  326. }
  327. } else {
  328. /* can't disturgissh keydown/up :-/ */
  329. ir_input_keydown(ir->input, &ir->ir, data, data);
  330. ir_input_nokey(ir->input, &ir->ir);
  331. }
  332. }
  333. static void ir_irq(struct bttv_sub_device *sub)
  334. {
  335. struct IR *ir = dev_get_drvdata(&sub->dev);
  336. if (!ir->polling)
  337. ir_handle_key(ir);
  338. }
  339. static void ir_timer(unsigned long data)
  340. {
  341. struct IR *ir = (struct IR*)data;
  342. schedule_work(&ir->work);
  343. }
  344. static void ir_work(void *data)
  345. {
  346. struct IR *ir = data;
  347. unsigned long timeout;
  348. ir_handle_key(ir);
  349. timeout = jiffies + (ir->polling * HZ / 1000);
  350. mod_timer(&ir->timer, timeout);
  351. }
  352. /* ---------------------------------------------------------------*/
  353. static int rc5_remote_gap = 885;
  354. module_param(rc5_remote_gap, int, 0644);
  355. static int rc5_key_timeout = 200;
  356. module_param(rc5_key_timeout, int, 0644);
  357. #define RC5_START(x) (((x)>>12)&3)
  358. #define RC5_TOGGLE(x) (((x)>>11)&1)
  359. #define RC5_ADDR(x) (((x)>>6)&31)
  360. #define RC5_INSTR(x) ((x)&63)
  361. /* decode raw bit pattern to RC5 code */
  362. static u32 rc5_decode(unsigned int code)
  363. {
  364. unsigned int org_code = code;
  365. unsigned int pair;
  366. unsigned int rc5 = 0;
  367. int i;
  368. code = (code << 1) | 1;
  369. for (i = 0; i < 14; ++i) {
  370. pair = code & 0x3;
  371. code >>= 2;
  372. rc5 <<= 1;
  373. switch (pair) {
  374. case 0:
  375. case 2:
  376. break;
  377. case 1:
  378. rc5 |= 1;
  379. break;
  380. case 3:
  381. dprintk("bad code: %x\n", org_code);
  382. return 0;
  383. }
  384. }
  385. dprintk("code=%x, rc5=%x, start=%x, toggle=%x, address=%x, "
  386. "instr=%x\n", rc5, org_code, RC5_START(rc5),
  387. RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));
  388. return rc5;
  389. }
  390. static int ir_rc5_irq(struct bttv_sub_device *sub)
  391. {
  392. struct IR *ir = dev_get_drvdata(&sub->dev);
  393. struct timeval tv;
  394. u32 gpio;
  395. u32 gap;
  396. unsigned long current_jiffies, timeout;
  397. /* read gpio port */
  398. gpio = bttv_gpio_read(ir->sub->core);
  399. /* remote IRQ? */
  400. if (!(gpio & 0x20))
  401. return 0;
  402. /* get time of bit */
  403. current_jiffies = jiffies;
  404. do_gettimeofday(&tv);
  405. /* avoid overflow with gap >1s */
  406. if (tv.tv_sec - ir->base_time.tv_sec > 1) {
  407. gap = 200000;
  408. } else {
  409. gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
  410. tv.tv_usec - ir->base_time.tv_usec;
  411. }
  412. /* active code => add bit */
  413. if (ir->active) {
  414. /* only if in the code (otherwise spurious IRQ or timer
  415. late) */
  416. if (ir->last_bit < 28) {
  417. ir->last_bit = (gap - rc5_remote_gap / 2) /
  418. rc5_remote_gap;
  419. ir->code |= 1 << ir->last_bit;
  420. }
  421. /* starting new code */
  422. } else {
  423. ir->active = 1;
  424. ir->code = 0;
  425. ir->base_time = tv;
  426. ir->last_bit = 0;
  427. timeout = current_jiffies + (500 + 30 * HZ) / 1000;
  428. mod_timer(&ir->timer_end, timeout);
  429. }
  430. /* toggle GPIO pin 4 to reset the irq */
  431. bttv_gpio_write(ir->sub->core, gpio & ~(1 << 4));
  432. bttv_gpio_write(ir->sub->core, gpio | (1 << 4));
  433. return 1;
  434. }
  435. static void ir_rc5_timer_end(unsigned long data)
  436. {
  437. struct IR *ir = (struct IR *)data;
  438. struct timeval tv;
  439. unsigned long current_jiffies, timeout;
  440. u32 gap;
  441. /* get time */
  442. current_jiffies = jiffies;
  443. do_gettimeofday(&tv);
  444. /* avoid overflow with gap >1s */
  445. if (tv.tv_sec - ir->base_time.tv_sec > 1) {
  446. gap = 200000;
  447. } else {
  448. gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
  449. tv.tv_usec - ir->base_time.tv_usec;
  450. }
  451. /* Allow some timmer jitter (RC5 is ~24ms anyway so this is ok) */
  452. if (gap < 28000) {
  453. dprintk("spurious timer_end\n");
  454. return;
  455. }
  456. ir->active = 0;
  457. if (ir->last_bit < 20) {
  458. /* ignore spurious codes (caused by light/other remotes) */
  459. dprintk("short code: %x\n", ir->code);
  460. } else {
  461. u32 rc5 = rc5_decode(ir->code);
  462. /* two start bits? */
  463. if (RC5_START(rc5) != 3) {
  464. dprintk("rc5 start bits invalid: %u\n", RC5_START(rc5));
  465. /* right address? */
  466. } else if (RC5_ADDR(rc5) == 0x0) {
  467. u32 toggle = RC5_TOGGLE(rc5);
  468. u32 instr = RC5_INSTR(rc5);
  469. /* Good code, decide if repeat/repress */
  470. if (toggle != RC5_TOGGLE(ir->last_rc5) ||
  471. instr != RC5_INSTR(ir->last_rc5)) {
  472. dprintk("instruction %x, toggle %x\n", instr,
  473. toggle);
  474. ir_input_nokey(ir->input, &ir->ir);
  475. ir_input_keydown(ir->input, &ir->ir, instr,
  476. instr);
  477. }
  478. /* Set/reset key-up timer */
  479. timeout = current_jiffies + (500 + rc5_key_timeout
  480. * HZ) / 1000;
  481. mod_timer(&ir->timer_keyup, timeout);
  482. /* Save code for repeat test */
  483. ir->last_rc5 = rc5;
  484. }
  485. }
  486. }
  487. static void ir_rc5_timer_keyup(unsigned long data)
  488. {
  489. struct IR *ir = (struct IR *)data;
  490. dprintk("key released\n");
  491. ir_input_nokey(ir->input, &ir->ir);
  492. }
  493. /* ---------------------------------------------------------------------- */
  494. static int ir_probe(struct device *dev)
  495. {
  496. struct bttv_sub_device *sub = to_bttv_sub_dev(dev);
  497. struct IR *ir;
  498. struct input_dev *input_dev;
  499. IR_KEYTAB_TYPE *ir_codes = NULL;
  500. int ir_type = IR_TYPE_OTHER;
  501. ir = kzalloc(sizeof(*ir), GFP_KERNEL);
  502. input_dev = input_allocate_device();
  503. if (!ir || !input_dev) {
  504. kfree(ir);
  505. input_free_device(input_dev);
  506. return -ENOMEM;
  507. }
  508. /* detect & configure */
  509. switch (sub->core->type) {
  510. case BTTV_BOARD_AVERMEDIA:
  511. case BTTV_BOARD_AVPHONE98:
  512. case BTTV_BOARD_AVERMEDIA98:
  513. ir_codes = ir_codes_avermedia;
  514. ir->mask_keycode = 0xf88000;
  515. ir->mask_keydown = 0x010000;
  516. ir->polling = 50; // ms
  517. break;
  518. case BTTV_BOARD_AVDVBT_761:
  519. case BTTV_BOARD_AVDVBT_771:
  520. ir_codes = ir_codes_avermedia_dvbt;
  521. ir->mask_keycode = 0x0f00c0;
  522. ir->mask_keydown = 0x000020;
  523. ir->polling = 50; // ms
  524. break;
  525. case BTTV_BOARD_PXELVWPLTVPAK:
  526. ir_codes = ir_codes_pixelview;
  527. ir->mask_keycode = 0x003e00;
  528. ir->mask_keyup = 0x010000;
  529. ir->polling = 50; // ms
  530. break;
  531. case BTTV_BOARD_PV_BT878P_9B:
  532. case BTTV_BOARD_PV_BT878P_PLUS:
  533. ir_codes = ir_codes_pixelview;
  534. ir->mask_keycode = 0x001f00;
  535. ir->mask_keyup = 0x008000;
  536. ir->polling = 50; // ms
  537. break;
  538. case BTTV_BOARD_WINFAST2000:
  539. ir_codes = ir_codes_winfast;
  540. ir->mask_keycode = 0x1f8;
  541. break;
  542. case BTTV_BOARD_MAGICTVIEW061:
  543. case BTTV_BOARD_MAGICTVIEW063:
  544. ir_codes = ir_codes_winfast;
  545. ir->mask_keycode = 0x0008e000;
  546. ir->mask_keydown = 0x00200000;
  547. break;
  548. case BTTV_BOARD_APAC_VIEWCOMP:
  549. ir_codes = ir_codes_apac_viewcomp;
  550. ir->mask_keycode = 0x001f00;
  551. ir->mask_keyup = 0x008000;
  552. ir->polling = 50; // ms
  553. break;
  554. case BTTV_BOARD_CONCEPTRONIC_CTVFMI2:
  555. ir_codes = ir_codes_conceptronic;
  556. ir->mask_keycode = 0x001F00;
  557. ir->mask_keyup = 0x006000;
  558. ir->polling = 50; // ms
  559. break;
  560. case BTTV_BOARD_NEBULA_DIGITV:
  561. ir_codes = ir_codes_nebula;
  562. driver.any_irq = ir_rc5_irq;
  563. driver.gpio_irq = NULL;
  564. ir->rc5_gpio = 1;
  565. break;
  566. }
  567. if (NULL == ir_codes) {
  568. kfree(ir);
  569. input_free_device(input_dev);
  570. return -ENODEV;
  571. }
  572. if (ir->rc5_gpio) {
  573. u32 gpio;
  574. /* enable remote irq */
  575. bttv_gpio_inout(sub->core, (1 << 4), 1 << 4);
  576. gpio = bttv_gpio_read(sub->core);
  577. bttv_gpio_write(sub->core, gpio & ~(1 << 4));
  578. bttv_gpio_write(sub->core, gpio | (1 << 4));
  579. } else {
  580. /* init hardware-specific stuff */
  581. bttv_gpio_inout(sub->core, ir->mask_keycode | ir->mask_keydown, 0);
  582. }
  583. /* init input device */
  584. snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
  585. sub->core->type);
  586. snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
  587. pci_name(sub->core->pci));
  588. ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
  589. input_dev->name = ir->name;
  590. input_dev->phys = ir->phys;
  591. input_dev->id.bustype = BUS_PCI;
  592. input_dev->id.version = 1;
  593. if (sub->core->pci->subsystem_vendor) {
  594. input_dev->id.vendor = sub->core->pci->subsystem_vendor;
  595. input_dev->id.product = sub->core->pci->subsystem_device;
  596. } else {
  597. input_dev->id.vendor = sub->core->pci->vendor;
  598. input_dev->id.product = sub->core->pci->device;
  599. }
  600. input_dev->cdev.dev = &sub->core->pci->dev;
  601. ir->input = input_dev;
  602. ir->sub = sub;
  603. if (ir->polling) {
  604. INIT_WORK(&ir->work, ir_work, ir);
  605. init_timer(&ir->timer);
  606. ir->timer.function = ir_timer;
  607. ir->timer.data = (unsigned long)ir;
  608. schedule_work(&ir->work);
  609. } else if (ir->rc5_gpio) {
  610. /* set timer_end for code completion */
  611. init_timer(&ir->timer_end);
  612. ir->timer_end.function = ir_rc5_timer_end;
  613. ir->timer_end.data = (unsigned long)ir;
  614. init_timer(&ir->timer_keyup);
  615. ir->timer_keyup.function = ir_rc5_timer_keyup;
  616. ir->timer_keyup.data = (unsigned long)ir;
  617. }
  618. /* all done */
  619. dev_set_drvdata(dev, ir);
  620. input_register_device(ir->input);
  621. /* the remote isn't as bouncy as a keyboard */
  622. ir->input->rep[REP_DELAY] = repeat_delay;
  623. ir->input->rep[REP_PERIOD] = repeat_period;
  624. return 0;
  625. }
  626. static int ir_remove(struct device *dev)
  627. {
  628. struct IR *ir = dev_get_drvdata(dev);
  629. if (ir->polling) {
  630. del_timer(&ir->timer);
  631. flush_scheduled_work();
  632. }
  633. if (ir->rc5_gpio) {
  634. u32 gpio;
  635. del_timer(&ir->timer_end);
  636. flush_scheduled_work();
  637. gpio = bttv_gpio_read(ir->sub->core);
  638. bttv_gpio_write(ir->sub->core, gpio & ~(1 << 4));
  639. }
  640. input_unregister_device(ir->input);
  641. kfree(ir);
  642. return 0;
  643. }
  644. /* ---------------------------------------------------------------------- */
  645. MODULE_AUTHOR("Gerd Knorr, Pavel Machek");
  646. MODULE_DESCRIPTION("input driver for bt8x8 gpio IR remote controls");
  647. MODULE_LICENSE("GPL");
  648. static int ir_init(void)
  649. {
  650. return bttv_sub_register(&driver, "remote");
  651. }
  652. static void ir_fini(void)
  653. {
  654. bttv_sub_unregister(&driver);
  655. }
  656. module_init(ir_init);
  657. module_exit(ir_fini);
  658. /*
  659. * Local variables:
  660. * c-basic-offset: 8
  661. * End:
  662. */