bttv-input.c 8.7 KB

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