bttv-input.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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/init.h>
  22. #include <linux/delay.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/input.h>
  25. #include "bttv.h"
  26. #include "bttvp.h"
  27. static int debug;
  28. module_param(debug, int, 0644); /* debug level (0,1,2) */
  29. static int repeat_delay = 500;
  30. module_param(repeat_delay, int, 0644);
  31. static int repeat_period = 33;
  32. module_param(repeat_period, int, 0644);
  33. static int ir_rc5_remote_gap = 885;
  34. module_param(ir_rc5_remote_gap, int, 0644);
  35. static int ir_rc5_key_timeout = 200;
  36. module_param(ir_rc5_key_timeout, int, 0644);
  37. #define DEVNAME "bttv-input"
  38. /* ---------------------------------------------------------------------- */
  39. static void ir_handle_key(struct bttv *btv)
  40. {
  41. struct card_ir *ir = btv->remote;
  42. u32 gpio,data;
  43. /* read gpio value */
  44. gpio = bttv_gpio_read(&btv->c);
  45. if (ir->polling) {
  46. if (ir->last_gpio == gpio)
  47. return;
  48. ir->last_gpio = gpio;
  49. }
  50. /* extract data */
  51. data = ir_extract_bits(gpio, ir->mask_keycode);
  52. dprintk(KERN_INFO DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",
  53. gpio, data,
  54. ir->polling ? "poll" : "irq",
  55. (gpio & ir->mask_keydown) ? " down" : "",
  56. (gpio & ir->mask_keyup) ? " up" : "");
  57. if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
  58. (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
  59. ir_input_keydown(ir->dev,&ir->ir,data,data);
  60. } else {
  61. /* HACK: Probably, ir->mask_keydown is missing
  62. for this board */
  63. if (btv->c.type == BTTV_BOARD_WINFAST2000)
  64. ir_input_keydown(ir->dev, &ir->ir, data, data);
  65. ir_input_nokey(ir->dev,&ir->ir);
  66. }
  67. }
  68. void bttv_input_irq(struct bttv *btv)
  69. {
  70. struct card_ir *ir = btv->remote;
  71. if (!ir->polling)
  72. ir_handle_key(btv);
  73. }
  74. static void bttv_input_timer(unsigned long data)
  75. {
  76. struct bttv *btv = (struct bttv*)data;
  77. struct card_ir *ir = btv->remote;
  78. ir_handle_key(btv);
  79. mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
  80. }
  81. /* ---------------------------------------------------------------*/
  82. static int bttv_rc5_irq(struct bttv *btv)
  83. {
  84. struct card_ir *ir = btv->remote;
  85. struct timeval tv;
  86. u32 gpio;
  87. u32 gap;
  88. unsigned long current_jiffies;
  89. /* read gpio port */
  90. gpio = bttv_gpio_read(&btv->c);
  91. /* remote IRQ? */
  92. if (!(gpio & 0x20))
  93. return 0;
  94. /* get time of bit */
  95. current_jiffies = jiffies;
  96. do_gettimeofday(&tv);
  97. /* avoid overflow with gap >1s */
  98. if (tv.tv_sec - ir->base_time.tv_sec > 1) {
  99. gap = 200000;
  100. } else {
  101. gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
  102. tv.tv_usec - ir->base_time.tv_usec;
  103. }
  104. /* active code => add bit */
  105. if (ir->active) {
  106. /* only if in the code (otherwise spurious IRQ or timer
  107. late) */
  108. if (ir->last_bit < 28) {
  109. ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
  110. ir_rc5_remote_gap;
  111. ir->code |= 1 << ir->last_bit;
  112. }
  113. /* starting new code */
  114. } else {
  115. ir->active = 1;
  116. ir->code = 0;
  117. ir->base_time = tv;
  118. ir->last_bit = 0;
  119. mod_timer(&ir->timer_end,
  120. current_jiffies + msecs_to_jiffies(30));
  121. }
  122. /* toggle GPIO pin 4 to reset the irq */
  123. bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
  124. bttv_gpio_write(&btv->c, gpio | (1 << 4));
  125. return 1;
  126. }
  127. /* ---------------------------------------------------------------------- */
  128. static void bttv_ir_start(struct bttv *btv, struct card_ir *ir)
  129. {
  130. if (ir->polling) {
  131. setup_timer(&ir->timer, bttv_input_timer, (unsigned long)btv);
  132. ir->timer.expires = jiffies + msecs_to_jiffies(1000);
  133. add_timer(&ir->timer);
  134. } else if (ir->rc5_gpio) {
  135. /* set timer_end for code completion */
  136. init_timer(&ir->timer_end);
  137. ir->timer_end.function = ir_rc5_timer_end;
  138. ir->timer_end.data = (unsigned long)ir;
  139. init_timer(&ir->timer_keyup);
  140. ir->timer_keyup.function = ir_rc5_timer_keyup;
  141. ir->timer_keyup.data = (unsigned long)ir;
  142. ir->shift_by = 1;
  143. ir->start = 3;
  144. ir->addr = 0x0;
  145. ir->rc5_key_timeout = ir_rc5_key_timeout;
  146. ir->rc5_remote_gap = ir_rc5_remote_gap;
  147. }
  148. }
  149. static void bttv_ir_stop(struct bttv *btv)
  150. {
  151. if (btv->remote->polling) {
  152. del_timer_sync(&btv->remote->timer);
  153. flush_scheduled_work();
  154. }
  155. if (btv->remote->rc5_gpio) {
  156. u32 gpio;
  157. del_timer_sync(&btv->remote->timer_end);
  158. flush_scheduled_work();
  159. gpio = bttv_gpio_read(&btv->c);
  160. bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
  161. }
  162. }
  163. int bttv_input_init(struct bttv *btv)
  164. {
  165. struct card_ir *ir;
  166. IR_KEYTAB_TYPE *ir_codes = NULL;
  167. struct input_dev *input_dev;
  168. int ir_type = IR_TYPE_OTHER;
  169. int err = -ENOMEM;
  170. if (!btv->has_remote)
  171. return -ENODEV;
  172. ir = kzalloc(sizeof(*ir),GFP_KERNEL);
  173. input_dev = input_allocate_device();
  174. if (!ir || !input_dev)
  175. goto err_out_free;
  176. /* detect & configure */
  177. switch (btv->c.type) {
  178. case BTTV_BOARD_AVERMEDIA:
  179. case BTTV_BOARD_AVPHONE98:
  180. case BTTV_BOARD_AVERMEDIA98:
  181. ir_codes = ir_codes_avermedia;
  182. ir->mask_keycode = 0xf88000;
  183. ir->mask_keydown = 0x010000;
  184. ir->polling = 50; // ms
  185. break;
  186. case BTTV_BOARD_AVDVBT_761:
  187. case BTTV_BOARD_AVDVBT_771:
  188. ir_codes = ir_codes_avermedia_dvbt;
  189. ir->mask_keycode = 0x0f00c0;
  190. ir->mask_keydown = 0x000020;
  191. ir->polling = 50; // ms
  192. break;
  193. case BTTV_BOARD_PXELVWPLTVPAK:
  194. ir_codes = ir_codes_pixelview;
  195. ir->mask_keycode = 0x003e00;
  196. ir->mask_keyup = 0x010000;
  197. ir->polling = 50; // ms
  198. break;
  199. case BTTV_BOARD_PV_M4900:
  200. case BTTV_BOARD_PV_BT878P_9B:
  201. case BTTV_BOARD_PV_BT878P_PLUS:
  202. ir_codes = ir_codes_pixelview;
  203. ir->mask_keycode = 0x001f00;
  204. ir->mask_keyup = 0x008000;
  205. ir->polling = 50; // ms
  206. break;
  207. case BTTV_BOARD_WINFAST2000:
  208. ir_codes = ir_codes_winfast;
  209. ir->mask_keycode = 0x1f8;
  210. break;
  211. case BTTV_BOARD_MAGICTVIEW061:
  212. case BTTV_BOARD_MAGICTVIEW063:
  213. ir_codes = ir_codes_winfast;
  214. ir->mask_keycode = 0x0008e000;
  215. ir->mask_keydown = 0x00200000;
  216. break;
  217. case BTTV_BOARD_APAC_VIEWCOMP:
  218. ir_codes = ir_codes_apac_viewcomp;
  219. ir->mask_keycode = 0x001f00;
  220. ir->mask_keyup = 0x008000;
  221. ir->polling = 50; // ms
  222. break;
  223. case BTTV_BOARD_CONCEPTRONIC_CTVFMI2:
  224. case BTTV_BOARD_CONTVFMI:
  225. ir_codes = ir_codes_pixelview;
  226. ir->mask_keycode = 0x001F00;
  227. ir->mask_keyup = 0x006000;
  228. ir->polling = 50; // ms
  229. break;
  230. case BTTV_BOARD_NEBULA_DIGITV:
  231. ir_codes = ir_codes_nebula;
  232. btv->custom_irq = bttv_rc5_irq;
  233. ir->rc5_gpio = 1;
  234. break;
  235. case BTTV_BOARD_MACHTV_MAGICTV:
  236. ir_codes = ir_codes_apac_viewcomp;
  237. ir->mask_keycode = 0x001F00;
  238. ir->mask_keyup = 0x004000;
  239. ir->polling = 50; /* ms */
  240. break;
  241. case BTTV_BOARD_KOZUMI_KTV_01C:
  242. ir_codes = ir_codes_pctv_sedna;
  243. ir->mask_keycode = 0x001f00;
  244. ir->mask_keyup = 0x006000;
  245. ir->polling = 50; /* ms */
  246. break;
  247. }
  248. if (NULL == ir_codes) {
  249. dprintk(KERN_INFO "Ooops: IR config error [card=%d]\n", btv->c.type);
  250. err = -ENODEV;
  251. goto err_out_free;
  252. }
  253. if (ir->rc5_gpio) {
  254. u32 gpio;
  255. /* enable remote irq */
  256. bttv_gpio_inout(&btv->c, (1 << 4), 1 << 4);
  257. gpio = bttv_gpio_read(&btv->c);
  258. bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
  259. bttv_gpio_write(&btv->c, gpio | (1 << 4));
  260. } else {
  261. /* init hardware-specific stuff */
  262. bttv_gpio_inout(&btv->c, ir->mask_keycode | ir->mask_keydown, 0);
  263. }
  264. /* init input device */
  265. ir->dev = input_dev;
  266. snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
  267. btv->c.type);
  268. snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
  269. pci_name(btv->c.pci));
  270. ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
  271. input_dev->name = ir->name;
  272. input_dev->phys = ir->phys;
  273. input_dev->id.bustype = BUS_PCI;
  274. input_dev->id.version = 1;
  275. if (btv->c.pci->subsystem_vendor) {
  276. input_dev->id.vendor = btv->c.pci->subsystem_vendor;
  277. input_dev->id.product = btv->c.pci->subsystem_device;
  278. } else {
  279. input_dev->id.vendor = btv->c.pci->vendor;
  280. input_dev->id.product = btv->c.pci->device;
  281. }
  282. input_dev->dev.parent = &btv->c.pci->dev;
  283. btv->remote = ir;
  284. bttv_ir_start(btv, ir);
  285. /* all done */
  286. err = input_register_device(btv->remote->dev);
  287. if (err)
  288. goto err_out_stop;
  289. /* the remote isn't as bouncy as a keyboard */
  290. ir->dev->rep[REP_DELAY] = repeat_delay;
  291. ir->dev->rep[REP_PERIOD] = repeat_period;
  292. return 0;
  293. err_out_stop:
  294. bttv_ir_stop(btv);
  295. btv->remote = NULL;
  296. err_out_free:
  297. input_free_device(input_dev);
  298. kfree(ir);
  299. return err;
  300. }
  301. void bttv_input_fini(struct bttv *btv)
  302. {
  303. if (btv->remote == NULL)
  304. return;
  305. bttv_ir_stop(btv);
  306. input_unregister_device(btv->remote->dev);
  307. kfree(btv->remote);
  308. btv->remote = NULL;
  309. }
  310. /*
  311. * Local variables:
  312. * c-basic-offset: 8
  313. * End:
  314. */