bttv-input.c 8.7 KB

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