bttv-input.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. #undef dprintk
  37. #define dprintk(arg...) do { \
  38. if (ir_debug >= 1) \
  39. printk(arg); \
  40. } while (0)
  41. #define DEVNAME "bttv-input"
  42. #define MODULE_NAME "bttv"
  43. /* ---------------------------------------------------------------------- */
  44. static void ir_handle_key(struct bttv *btv)
  45. {
  46. struct card_ir *ir = btv->remote;
  47. u32 gpio,data;
  48. /* read gpio value */
  49. gpio = bttv_gpio_read(&btv->c);
  50. if (ir->polling) {
  51. if (ir->last_gpio == gpio)
  52. return;
  53. ir->last_gpio = gpio;
  54. }
  55. /* extract data */
  56. data = ir_extract_bits(gpio, ir->mask_keycode);
  57. dprintk(KERN_INFO DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",
  58. gpio, data,
  59. ir->polling ? "poll" : "irq",
  60. (gpio & ir->mask_keydown) ? " down" : "",
  61. (gpio & ir->mask_keyup) ? " up" : "");
  62. if ((ir->mask_keydown && (gpio & ir->mask_keydown)) ||
  63. (ir->mask_keyup && !(gpio & ir->mask_keyup))) {
  64. ir_keydown_notimeout(ir->dev, data, 0);
  65. } else {
  66. /* HACK: Probably, ir->mask_keydown is missing
  67. for this board */
  68. if (btv->c.type == BTTV_BOARD_WINFAST2000)
  69. ir_keydown_notimeout(ir->dev, data, 0);
  70. ir_keyup(ir->dev);
  71. }
  72. }
  73. static void ir_enltv_handle_key(struct bttv *btv)
  74. {
  75. struct card_ir *ir = btv->remote;
  76. u32 gpio, data, keyup;
  77. /* read gpio value */
  78. gpio = bttv_gpio_read(&btv->c);
  79. /* extract data */
  80. data = ir_extract_bits(gpio, ir->mask_keycode);
  81. /* Check if it is keyup */
  82. keyup = (gpio & ir->mask_keyup) ? 1 << 31 : 0;
  83. if ((ir->last_gpio & 0x7f) != data) {
  84. dprintk(KERN_INFO DEVNAME ": gpio=0x%x code=%d | %s\n",
  85. gpio, data,
  86. (gpio & ir->mask_keyup) ? " up" : "up/down");
  87. ir_keydown_notimeout(ir->dev, data, 0);
  88. if (keyup)
  89. ir_keyup(ir->dev);
  90. } else {
  91. if ((ir->last_gpio & 1 << 31) == keyup)
  92. return;
  93. dprintk(KERN_INFO DEVNAME ":(cnt) gpio=0x%x code=%d | %s\n",
  94. gpio, data,
  95. (gpio & ir->mask_keyup) ? " up" : "down");
  96. if (keyup)
  97. ir_keyup(ir->dev);
  98. else
  99. ir_keydown_notimeout(ir->dev, data, 0);
  100. }
  101. ir->last_gpio = data | keyup;
  102. }
  103. void bttv_input_irq(struct bttv *btv)
  104. {
  105. struct card_ir *ir = btv->remote;
  106. if (!ir->polling)
  107. ir_handle_key(btv);
  108. }
  109. static void bttv_input_timer(unsigned long data)
  110. {
  111. struct bttv *btv = (struct bttv*)data;
  112. struct card_ir *ir = btv->remote;
  113. if (btv->c.type == BTTV_BOARD_ENLTV_FM_2)
  114. ir_enltv_handle_key(btv);
  115. else
  116. ir_handle_key(btv);
  117. mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
  118. }
  119. /* ---------------------------------------------------------------*/
  120. static int bttv_rc5_irq(struct bttv *btv)
  121. {
  122. struct card_ir *ir = btv->remote;
  123. struct timeval tv;
  124. u32 gpio;
  125. u32 gap;
  126. unsigned long current_jiffies;
  127. /* read gpio port */
  128. gpio = bttv_gpio_read(&btv->c);
  129. /* remote IRQ? */
  130. if (!(gpio & 0x20))
  131. return 0;
  132. /* get time of bit */
  133. current_jiffies = jiffies;
  134. do_gettimeofday(&tv);
  135. /* avoid overflow with gap >1s */
  136. if (tv.tv_sec - ir->base_time.tv_sec > 1) {
  137. gap = 200000;
  138. } else {
  139. gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
  140. tv.tv_usec - ir->base_time.tv_usec;
  141. }
  142. /* active code => add bit */
  143. if (ir->active) {
  144. /* only if in the code (otherwise spurious IRQ or timer
  145. late) */
  146. if (ir->last_bit < 28) {
  147. ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
  148. ir_rc5_remote_gap;
  149. ir->code |= 1 << ir->last_bit;
  150. }
  151. /* starting new code */
  152. } else {
  153. ir->active = 1;
  154. ir->code = 0;
  155. ir->base_time = tv;
  156. ir->last_bit = 0;
  157. mod_timer(&ir->timer_end,
  158. current_jiffies + msecs_to_jiffies(30));
  159. }
  160. /* toggle GPIO pin 4 to reset the irq */
  161. bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
  162. bttv_gpio_write(&btv->c, gpio | (1 << 4));
  163. return 1;
  164. }
  165. /* ---------------------------------------------------------------------- */
  166. static void bttv_ir_start(struct bttv *btv, struct card_ir *ir)
  167. {
  168. if (ir->polling) {
  169. setup_timer(&ir->timer, bttv_input_timer, (unsigned long)btv);
  170. ir->timer.expires = jiffies + msecs_to_jiffies(1000);
  171. add_timer(&ir->timer);
  172. } else if (ir->rc5_gpio) {
  173. /* set timer_end for code completion */
  174. init_timer(&ir->timer_end);
  175. ir->timer_end.function = ir_rc5_timer_end;
  176. ir->timer_end.data = (unsigned long)ir;
  177. ir->shift_by = 1;
  178. ir->start = 3;
  179. ir->addr = 0x0;
  180. ir->rc5_remote_gap = ir_rc5_remote_gap;
  181. }
  182. }
  183. static void bttv_ir_stop(struct bttv *btv)
  184. {
  185. if (btv->remote->polling) {
  186. del_timer_sync(&btv->remote->timer);
  187. flush_scheduled_work();
  188. }
  189. if (btv->remote->rc5_gpio) {
  190. u32 gpio;
  191. del_timer_sync(&btv->remote->timer_end);
  192. flush_scheduled_work();
  193. gpio = bttv_gpio_read(&btv->c);
  194. bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
  195. }
  196. }
  197. /*
  198. * Get_key functions used by I2C remotes
  199. */
  200. static int get_key_pv951(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  201. {
  202. unsigned char b;
  203. /* poll IR chip */
  204. if (1 != i2c_master_recv(ir->c, &b, 1)) {
  205. dprintk(KERN_INFO DEVNAME ": read error\n");
  206. return -EIO;
  207. }
  208. /* ignore 0xaa */
  209. if (b==0xaa)
  210. return 0;
  211. dprintk(KERN_INFO DEVNAME ": key %02x\n", b);
  212. *ir_key = b;
  213. *ir_raw = b;
  214. return 1;
  215. }
  216. /* Instantiate the I2C IR receiver device, if present */
  217. void __devinit init_bttv_i2c_ir(struct bttv *btv)
  218. {
  219. const unsigned short addr_list[] = {
  220. 0x1a, 0x18, 0x64, 0x30, 0x71,
  221. I2C_CLIENT_END
  222. };
  223. struct i2c_board_info info;
  224. if (0 != btv->i2c_rc)
  225. return;
  226. memset(&info, 0, sizeof(struct i2c_board_info));
  227. memset(&btv->init_data, 0, sizeof(btv->init_data));
  228. strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
  229. switch (btv->c.type) {
  230. case BTTV_BOARD_PV951:
  231. btv->init_data.name = "PV951";
  232. btv->init_data.get_key = get_key_pv951;
  233. btv->init_data.ir_codes = RC_MAP_PV951;
  234. info.addr = 0x4b;
  235. break;
  236. default:
  237. /*
  238. * The external IR receiver is at i2c address 0x34 (0x35 for
  239. * reads). Future Hauppauge cards will have an internal
  240. * receiver at 0x30 (0x31 for reads). In theory, both can be
  241. * fitted, and Hauppauge suggest an external overrides an
  242. * internal.
  243. * That's why we probe 0x1a (~0x34) first. CB
  244. */
  245. i2c_new_probed_device(&btv->c.i2c_adap, &info, addr_list, NULL);
  246. return;
  247. }
  248. if (btv->init_data.name)
  249. info.platform_data = &btv->init_data;
  250. i2c_new_device(&btv->c.i2c_adap, &info);
  251. return;
  252. }
  253. int __devexit fini_bttv_i2c(struct bttv *btv)
  254. {
  255. if (0 != btv->i2c_rc)
  256. return 0;
  257. return i2c_del_adapter(&btv->c.i2c_adap);
  258. }
  259. int bttv_input_init(struct bttv *btv)
  260. {
  261. struct card_ir *ir;
  262. char *ir_codes = NULL;
  263. struct input_dev *input_dev;
  264. int err = -ENOMEM;
  265. if (!btv->has_remote)
  266. return -ENODEV;
  267. ir = kzalloc(sizeof(*ir),GFP_KERNEL);
  268. input_dev = input_allocate_device();
  269. if (!ir || !input_dev)
  270. goto err_out_free;
  271. /* detect & configure */
  272. switch (btv->c.type) {
  273. case BTTV_BOARD_AVERMEDIA:
  274. case BTTV_BOARD_AVPHONE98:
  275. case BTTV_BOARD_AVERMEDIA98:
  276. ir_codes = RC_MAP_AVERMEDIA;
  277. ir->mask_keycode = 0xf88000;
  278. ir->mask_keydown = 0x010000;
  279. ir->polling = 50; // ms
  280. break;
  281. case BTTV_BOARD_AVDVBT_761:
  282. case BTTV_BOARD_AVDVBT_771:
  283. ir_codes = RC_MAP_AVERMEDIA_DVBT;
  284. ir->mask_keycode = 0x0f00c0;
  285. ir->mask_keydown = 0x000020;
  286. ir->polling = 50; // ms
  287. break;
  288. case BTTV_BOARD_PXELVWPLTVPAK:
  289. ir_codes = RC_MAP_PIXELVIEW;
  290. ir->mask_keycode = 0x003e00;
  291. ir->mask_keyup = 0x010000;
  292. ir->polling = 50; // ms
  293. break;
  294. case BTTV_BOARD_PV_M4900:
  295. case BTTV_BOARD_PV_BT878P_9B:
  296. case BTTV_BOARD_PV_BT878P_PLUS:
  297. ir_codes = RC_MAP_PIXELVIEW;
  298. ir->mask_keycode = 0x001f00;
  299. ir->mask_keyup = 0x008000;
  300. ir->polling = 50; // ms
  301. break;
  302. case BTTV_BOARD_WINFAST2000:
  303. ir_codes = RC_MAP_WINFAST;
  304. ir->mask_keycode = 0x1f8;
  305. break;
  306. case BTTV_BOARD_MAGICTVIEW061:
  307. case BTTV_BOARD_MAGICTVIEW063:
  308. ir_codes = RC_MAP_WINFAST;
  309. ir->mask_keycode = 0x0008e000;
  310. ir->mask_keydown = 0x00200000;
  311. break;
  312. case BTTV_BOARD_APAC_VIEWCOMP:
  313. ir_codes = RC_MAP_APAC_VIEWCOMP;
  314. ir->mask_keycode = 0x001f00;
  315. ir->mask_keyup = 0x008000;
  316. ir->polling = 50; // ms
  317. break;
  318. case BTTV_BOARD_ASKEY_CPH03X:
  319. case BTTV_BOARD_CONCEPTRONIC_CTVFMI2:
  320. case BTTV_BOARD_CONTVFMI:
  321. ir_codes = RC_MAP_PIXELVIEW;
  322. ir->mask_keycode = 0x001F00;
  323. ir->mask_keyup = 0x006000;
  324. ir->polling = 50; // ms
  325. break;
  326. case BTTV_BOARD_NEBULA_DIGITV:
  327. ir_codes = RC_MAP_NEBULA;
  328. btv->custom_irq = bttv_rc5_irq;
  329. ir->rc5_gpio = 1;
  330. break;
  331. case BTTV_BOARD_MACHTV_MAGICTV:
  332. ir_codes = RC_MAP_APAC_VIEWCOMP;
  333. ir->mask_keycode = 0x001F00;
  334. ir->mask_keyup = 0x004000;
  335. ir->polling = 50; /* ms */
  336. break;
  337. case BTTV_BOARD_KOZUMI_KTV_01C:
  338. ir_codes = RC_MAP_PCTV_SEDNA;
  339. ir->mask_keycode = 0x001f00;
  340. ir->mask_keyup = 0x006000;
  341. ir->polling = 50; /* ms */
  342. break;
  343. case BTTV_BOARD_ENLTV_FM_2:
  344. ir_codes = RC_MAP_ENCORE_ENLTV2;
  345. ir->mask_keycode = 0x00fd00;
  346. ir->mask_keyup = 0x000080;
  347. ir->polling = 1; /* ms */
  348. ir->last_gpio = ir_extract_bits(bttv_gpio_read(&btv->c),
  349. ir->mask_keycode);
  350. break;
  351. }
  352. if (NULL == ir_codes) {
  353. dprintk(KERN_INFO "Ooops: IR config error [card=%d]\n", btv->c.type);
  354. err = -ENODEV;
  355. goto err_out_free;
  356. }
  357. if (ir->rc5_gpio) {
  358. u32 gpio;
  359. /* enable remote irq */
  360. bttv_gpio_inout(&btv->c, (1 << 4), 1 << 4);
  361. gpio = bttv_gpio_read(&btv->c);
  362. bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
  363. bttv_gpio_write(&btv->c, gpio | (1 << 4));
  364. } else {
  365. /* init hardware-specific stuff */
  366. bttv_gpio_inout(&btv->c, ir->mask_keycode | ir->mask_keydown, 0);
  367. }
  368. /* init input device */
  369. ir->dev = input_dev;
  370. snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
  371. btv->c.type);
  372. snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
  373. pci_name(btv->c.pci));
  374. input_dev->name = ir->name;
  375. input_dev->phys = ir->phys;
  376. input_dev->id.bustype = BUS_PCI;
  377. input_dev->id.version = 1;
  378. if (btv->c.pci->subsystem_vendor) {
  379. input_dev->id.vendor = btv->c.pci->subsystem_vendor;
  380. input_dev->id.product = btv->c.pci->subsystem_device;
  381. } else {
  382. input_dev->id.vendor = btv->c.pci->vendor;
  383. input_dev->id.product = btv->c.pci->device;
  384. }
  385. input_dev->dev.parent = &btv->c.pci->dev;
  386. btv->remote = ir;
  387. bttv_ir_start(btv, ir);
  388. /* all done */
  389. err = ir_input_register(btv->remote->dev, ir_codes, NULL, MODULE_NAME);
  390. if (err)
  391. goto err_out_stop;
  392. /* the remote isn't as bouncy as a keyboard */
  393. ir->dev->rep[REP_DELAY] = repeat_delay;
  394. ir->dev->rep[REP_PERIOD] = repeat_period;
  395. return 0;
  396. err_out_stop:
  397. bttv_ir_stop(btv);
  398. btv->remote = NULL;
  399. err_out_free:
  400. kfree(ir);
  401. return err;
  402. }
  403. void bttv_input_fini(struct bttv *btv)
  404. {
  405. if (btv->remote == NULL)
  406. return;
  407. bttv_ir_stop(btv);
  408. ir_input_unregister(btv->remote->dev);
  409. kfree(btv->remote);
  410. btv->remote = NULL;
  411. }