bttv-input.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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 "bttv.h"
  27. #include "bttvp.h"
  28. static int debug;
  29. module_param(debug, int, 0644); /* debug level (0,1,2) */
  30. static int repeat_delay = 500;
  31. module_param(repeat_delay, int, 0644);
  32. static int repeat_period = 33;
  33. module_param(repeat_period, int, 0644);
  34. #define DEVNAME "bttv-input"
  35. /* ---------------------------------------------------------------------- */
  36. static void ir_handle_key(struct bttv *btv)
  37. {
  38. struct bttv_ir *ir = btv->remote;
  39. u32 gpio,data;
  40. /* read gpio value */
  41. gpio = bttv_gpio_read(&btv->c);
  42. if (ir->polling) {
  43. if (ir->last_gpio == gpio)
  44. return;
  45. ir->last_gpio = gpio;
  46. }
  47. /* extract data */
  48. data = ir_extract_bits(gpio, ir->mask_keycode);
  49. dprintk(KERN_INFO DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",
  50. gpio, data,
  51. ir->polling ? "poll" : "irq",
  52. (gpio & ir->mask_keydown) ? " down" : "",
  53. (gpio & ir->mask_keyup) ? " up" : "");
  54. if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
  55. (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
  56. ir_input_keydown(ir->dev,&ir->ir,data,data);
  57. } else {
  58. ir_input_nokey(ir->dev,&ir->ir);
  59. }
  60. }
  61. void bttv_input_irq(struct bttv *btv)
  62. {
  63. struct bttv_ir *ir = btv->remote;
  64. if (!ir->polling)
  65. ir_handle_key(btv);
  66. }
  67. static void bttv_input_timer(unsigned long data)
  68. {
  69. struct bttv *btv = (struct bttv*)data;
  70. struct bttv_ir *ir = btv->remote;
  71. unsigned long timeout;
  72. ir_handle_key(btv);
  73. timeout = jiffies + (ir->polling * HZ / 1000);
  74. mod_timer(&ir->timer, timeout);
  75. }
  76. /* ---------------------------------------------------------------*/
  77. static int rc5_remote_gap = 885;
  78. module_param(rc5_remote_gap, int, 0644);
  79. static int rc5_key_timeout = 200;
  80. module_param(rc5_key_timeout, int, 0644);
  81. #define RC5_START(x) (((x)>>12)&3)
  82. #define RC5_TOGGLE(x) (((x)>>11)&1)
  83. #define RC5_ADDR(x) (((x)>>6)&31)
  84. #define RC5_INSTR(x) ((x)&63)
  85. /* decode raw bit pattern to RC5 code */
  86. static u32 rc5_decode(unsigned int code)
  87. {
  88. unsigned int org_code = code;
  89. unsigned int pair;
  90. unsigned int rc5 = 0;
  91. int i;
  92. code = (code << 1) | 1;
  93. for (i = 0; i < 14; ++i) {
  94. pair = code & 0x3;
  95. code >>= 2;
  96. rc5 <<= 1;
  97. switch (pair) {
  98. case 0:
  99. case 2:
  100. break;
  101. case 1:
  102. rc5 |= 1;
  103. break;
  104. case 3:
  105. dprintk(KERN_WARNING "bad code: %x\n", org_code);
  106. return 0;
  107. }
  108. }
  109. dprintk(KERN_WARNING "code=%x, rc5=%x, start=%x, toggle=%x, address=%x, "
  110. "instr=%x\n", rc5, org_code, RC5_START(rc5),
  111. RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));
  112. return rc5;
  113. }
  114. static int bttv_rc5_irq(struct bttv *btv)
  115. {
  116. struct bttv_ir *ir = btv->remote;
  117. struct timeval tv;
  118. u32 gpio;
  119. u32 gap;
  120. unsigned long current_jiffies, timeout;
  121. /* read gpio port */
  122. gpio = bttv_gpio_read(&btv->c);
  123. /* remote IRQ? */
  124. if (!(gpio & 0x20))
  125. return 0;
  126. /* get time of bit */
  127. current_jiffies = jiffies;
  128. do_gettimeofday(&tv);
  129. /* avoid overflow with gap >1s */
  130. if (tv.tv_sec - ir->base_time.tv_sec > 1) {
  131. gap = 200000;
  132. } else {
  133. gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
  134. tv.tv_usec - ir->base_time.tv_usec;
  135. }
  136. /* active code => add bit */
  137. if (ir->active) {
  138. /* only if in the code (otherwise spurious IRQ or timer
  139. late) */
  140. if (ir->last_bit < 28) {
  141. ir->last_bit = (gap - rc5_remote_gap / 2) /
  142. rc5_remote_gap;
  143. ir->code |= 1 << ir->last_bit;
  144. }
  145. /* starting new code */
  146. } else {
  147. ir->active = 1;
  148. ir->code = 0;
  149. ir->base_time = tv;
  150. ir->last_bit = 0;
  151. timeout = current_jiffies + (500 + 30 * HZ) / 1000;
  152. mod_timer(&ir->timer_end, timeout);
  153. }
  154. /* toggle GPIO pin 4 to reset the irq */
  155. bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
  156. bttv_gpio_write(&btv->c, gpio | (1 << 4));
  157. return 1;
  158. }
  159. static void bttv_rc5_timer_end(unsigned long data)
  160. {
  161. struct bttv_ir *ir = (struct bttv_ir *)data;
  162. struct timeval tv;
  163. unsigned long current_jiffies, timeout;
  164. u32 gap;
  165. /* get time */
  166. current_jiffies = jiffies;
  167. do_gettimeofday(&tv);
  168. /* avoid overflow with gap >1s */
  169. if (tv.tv_sec - ir->base_time.tv_sec > 1) {
  170. gap = 200000;
  171. } else {
  172. gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
  173. tv.tv_usec - ir->base_time.tv_usec;
  174. }
  175. /* Allow some timmer jitter (RC5 is ~24ms anyway so this is ok) */
  176. if (gap < 28000) {
  177. dprintk(KERN_WARNING "spurious timer_end\n");
  178. return;
  179. }
  180. ir->active = 0;
  181. if (ir->last_bit < 20) {
  182. /* ignore spurious codes (caused by light/other remotes) */
  183. dprintk(KERN_WARNING "short code: %x\n", ir->code);
  184. } else {
  185. u32 rc5 = rc5_decode(ir->code);
  186. /* two start bits? */
  187. if (RC5_START(rc5) != 3) {
  188. dprintk(KERN_WARNING "rc5 start bits invalid: %u\n", RC5_START(rc5));
  189. /* right address? */
  190. } else if (RC5_ADDR(rc5) == 0x0) {
  191. u32 toggle = RC5_TOGGLE(rc5);
  192. u32 instr = RC5_INSTR(rc5);
  193. /* Good code, decide if repeat/repress */
  194. if (toggle != RC5_TOGGLE(ir->last_rc5) ||
  195. instr != RC5_INSTR(ir->last_rc5)) {
  196. dprintk(KERN_WARNING "instruction %x, toggle %x\n", instr,
  197. toggle);
  198. ir_input_nokey(ir->dev, &ir->ir);
  199. ir_input_keydown(ir->dev, &ir->ir, instr,
  200. instr);
  201. }
  202. /* Set/reset key-up timer */
  203. timeout = current_jiffies + (500 + rc5_key_timeout
  204. * HZ) / 1000;
  205. mod_timer(&ir->timer_keyup, timeout);
  206. /* Save code for repeat test */
  207. ir->last_rc5 = rc5;
  208. }
  209. }
  210. }
  211. static void bttv_rc5_timer_keyup(unsigned long data)
  212. {
  213. struct bttv_ir *ir = (struct bttv_ir *)data;
  214. dprintk(KERN_DEBUG "key released\n");
  215. ir_input_nokey(ir->dev, &ir->ir);
  216. }
  217. /* ---------------------------------------------------------------------- */
  218. int bttv_input_init(struct bttv *btv)
  219. {
  220. struct bttv_ir *ir;
  221. IR_KEYTAB_TYPE *ir_codes = NULL;
  222. struct input_dev *input_dev;
  223. int ir_type = IR_TYPE_OTHER;
  224. if (!btv->has_remote)
  225. return -ENODEV;
  226. ir = kzalloc(sizeof(*ir),GFP_KERNEL);
  227. input_dev = input_allocate_device();
  228. if (!ir || !input_dev) {
  229. kfree(ir);
  230. input_free_device(input_dev);
  231. return -ENOMEM;
  232. }
  233. memset(ir,0,sizeof(*ir));
  234. /* detect & configure */
  235. switch (btv->c.type) {
  236. case BTTV_BOARD_AVERMEDIA:
  237. case BTTV_BOARD_AVPHONE98:
  238. case BTTV_BOARD_AVERMEDIA98:
  239. ir_codes = ir_codes_avermedia;
  240. ir->mask_keycode = 0xf88000;
  241. ir->mask_keydown = 0x010000;
  242. ir->polling = 50; // ms
  243. break;
  244. case BTTV_BOARD_AVDVBT_761:
  245. case BTTV_BOARD_AVDVBT_771:
  246. ir_codes = ir_codes_avermedia_dvbt;
  247. ir->mask_keycode = 0x0f00c0;
  248. ir->mask_keydown = 0x000020;
  249. ir->polling = 50; // ms
  250. break;
  251. case BTTV_BOARD_PXELVWPLTVPAK:
  252. ir_codes = ir_codes_pixelview;
  253. ir->mask_keycode = 0x003e00;
  254. ir->mask_keyup = 0x010000;
  255. ir->polling = 50; // ms
  256. break;
  257. case BTTV_BOARD_PV_M4900:
  258. case BTTV_BOARD_PV_BT878P_9B:
  259. case BTTV_BOARD_PV_BT878P_PLUS:
  260. ir_codes = ir_codes_pixelview;
  261. ir->mask_keycode = 0x001f00;
  262. ir->mask_keyup = 0x008000;
  263. ir->polling = 50; // ms
  264. break;
  265. case BTTV_BOARD_WINFAST2000:
  266. ir_codes = ir_codes_winfast;
  267. ir->mask_keycode = 0x1f8;
  268. break;
  269. case BTTV_BOARD_MAGICTVIEW061:
  270. case BTTV_BOARD_MAGICTVIEW063:
  271. ir_codes = ir_codes_winfast;
  272. ir->mask_keycode = 0x0008e000;
  273. ir->mask_keydown = 0x00200000;
  274. break;
  275. case BTTV_BOARD_APAC_VIEWCOMP:
  276. ir_codes = ir_codes_apac_viewcomp;
  277. ir->mask_keycode = 0x001f00;
  278. ir->mask_keyup = 0x008000;
  279. ir->polling = 50; // ms
  280. break;
  281. case BTTV_BOARD_CONCEPTRONIC_CTVFMI2:
  282. case BTTV_BOARD_CONTVFMI:
  283. ir_codes = ir_codes_pixelview;
  284. ir->mask_keycode = 0x001F00;
  285. ir->mask_keyup = 0x006000;
  286. ir->polling = 50; // ms
  287. break;
  288. case BTTV_BOARD_NEBULA_DIGITV:
  289. ir_codes = ir_codes_nebula;
  290. btv->custom_irq = bttv_rc5_irq;
  291. ir->rc5_gpio = 1;
  292. break;
  293. case BTTV_BOARD_MACHTV_MAGICTV:
  294. ir_codes = ir_codes_apac_viewcomp;
  295. ir->mask_keycode = 0x001F00;
  296. ir->mask_keyup = 0x004000;
  297. ir->polling = 50; /* ms */
  298. break;
  299. }
  300. if (NULL == ir_codes) {
  301. dprintk(KERN_INFO "Ooops: IR config error [card=%d]\n",btv->c.type);
  302. kfree(ir);
  303. input_free_device(input_dev);
  304. return -ENODEV;
  305. }
  306. if (ir->rc5_gpio) {
  307. u32 gpio;
  308. /* enable remote irq */
  309. bttv_gpio_inout(&btv->c, (1 << 4), 1 << 4);
  310. gpio = bttv_gpio_read(&btv->c);
  311. bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
  312. bttv_gpio_write(&btv->c, gpio | (1 << 4));
  313. } else {
  314. /* init hardware-specific stuff */
  315. bttv_gpio_inout(&btv->c, ir->mask_keycode | ir->mask_keydown, 0);
  316. }
  317. /* init input device */
  318. ir->dev = input_dev;
  319. snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
  320. btv->c.type);
  321. snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
  322. pci_name(btv->c.pci));
  323. ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
  324. input_dev->name = ir->name;
  325. input_dev->phys = ir->phys;
  326. input_dev->id.bustype = BUS_PCI;
  327. input_dev->id.version = 1;
  328. if (btv->c.pci->subsystem_vendor) {
  329. input_dev->id.vendor = btv->c.pci->subsystem_vendor;
  330. input_dev->id.product = btv->c.pci->subsystem_device;
  331. } else {
  332. input_dev->id.vendor = btv->c.pci->vendor;
  333. input_dev->id.product = btv->c.pci->device;
  334. }
  335. input_dev->cdev.dev = &btv->c.pci->dev;
  336. btv->remote = ir;
  337. if (ir->polling) {
  338. init_timer(&ir->timer);
  339. ir->timer.function = bttv_input_timer;
  340. ir->timer.data = (unsigned long)btv;
  341. ir->timer.expires = jiffies + HZ;
  342. add_timer(&ir->timer);
  343. } else if (ir->rc5_gpio) {
  344. /* set timer_end for code completion */
  345. init_timer(&ir->timer_end);
  346. ir->timer_end.function = bttv_rc5_timer_end;
  347. ir->timer_end.data = (unsigned long)ir;
  348. init_timer(&ir->timer_keyup);
  349. ir->timer_keyup.function = bttv_rc5_timer_keyup;
  350. ir->timer_keyup.data = (unsigned long)ir;
  351. }
  352. /* all done */
  353. input_register_device(btv->remote->dev);
  354. printk(DEVNAME ": %s detected at %s\n",ir->name,ir->phys);
  355. /* the remote isn't as bouncy as a keyboard */
  356. ir->dev->rep[REP_DELAY] = repeat_delay;
  357. ir->dev->rep[REP_PERIOD] = repeat_period;
  358. return 0;
  359. }
  360. void bttv_input_fini(struct bttv *btv)
  361. {
  362. if (btv->remote == NULL)
  363. return;
  364. if (btv->remote->polling) {
  365. del_timer_sync(&btv->remote->timer);
  366. flush_scheduled_work();
  367. }
  368. if (btv->remote->rc5_gpio) {
  369. u32 gpio;
  370. del_timer_sync(&btv->remote->timer_end);
  371. flush_scheduled_work();
  372. gpio = bttv_gpio_read(&btv->c);
  373. bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
  374. }
  375. input_unregister_device(btv->remote->dev);
  376. kfree(btv->remote);
  377. btv->remote = NULL;
  378. }
  379. /*
  380. * Local variables:
  381. * c-basic-offset: 8
  382. * End:
  383. */