bttv-input.c 10 KB

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