av7110_ir.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * Driver for the remote control of SAA7146 based AV7110 cards
  3. *
  4. * Copyright (C) 1999-2003 Holger Waechtler <holger@convergence.de>
  5. * Copyright (C) 2003-2007 Oliver Endriss <o.endriss@gmx.de>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
  21. *
  22. */
  23. #include <linux/types.h>
  24. #include <linux/init.h>
  25. #include <linux/module.h>
  26. #include <linux/proc_fs.h>
  27. #include <linux/kernel.h>
  28. #include <linux/bitops.h>
  29. #include "av7110.h"
  30. #include "av7110_hw.h"
  31. #define AV_CNT 4
  32. #define IR_RC5 0
  33. #define IR_RCMM 1
  34. #define IR_RC5_EXT 2 /* internal only */
  35. #define IR_ALL 0xffffffff
  36. #define UP_TIMEOUT (HZ*7/25)
  37. /* Note: enable ir debugging by or'ing debug with 16 */
  38. static int ir_protocol[AV_CNT] = { IR_RCMM, IR_RCMM, IR_RCMM, IR_RCMM};
  39. module_param_array(ir_protocol, int, NULL, 0644);
  40. MODULE_PARM_DESC(ir_protocol, "Infrared protocol: 0 RC5, 1 RCMM (default)");
  41. static int ir_inversion[AV_CNT];
  42. module_param_array(ir_inversion, int, NULL, 0644);
  43. MODULE_PARM_DESC(ir_inversion, "Inversion of infrared signal: 0 not inverted (default), 1 inverted");
  44. static uint ir_device_mask[AV_CNT] = { IR_ALL, IR_ALL, IR_ALL, IR_ALL };
  45. module_param_array(ir_device_mask, uint, NULL, 0644);
  46. MODULE_PARM_DESC(ir_device_mask, "Bitmask of infrared devices: bit 0..31 = device 0..31 (default: all)");
  47. static int av_cnt;
  48. static struct av7110 *av_list[AV_CNT];
  49. static u16 default_key_map [256] = {
  50. KEY_0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7,
  51. KEY_8, KEY_9, KEY_BACK, 0, KEY_POWER, KEY_MUTE, 0, KEY_INFO,
  52. KEY_VOLUMEUP, KEY_VOLUMEDOWN, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  53. KEY_CHANNELUP, KEY_CHANNELDOWN, 0, 0, 0, 0, 0, 0,
  54. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  55. 0, 0, 0, 0, KEY_TEXT, 0, 0, KEY_TV, 0, 0, 0, 0, 0, KEY_SETUP, 0, 0,
  56. 0, 0, 0, KEY_SUBTITLE, 0, 0, KEY_LANGUAGE, 0,
  57. KEY_RADIO, 0, 0, 0, 0, KEY_EXIT, 0, 0,
  58. KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_OK, 0, 0, 0,
  59. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_RED, KEY_GREEN, KEY_YELLOW,
  60. KEY_BLUE, 0, 0, 0, 0, 0, 0, 0, KEY_MENU, KEY_LIST, 0, 0, 0, 0, 0, 0,
  61. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  62. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  63. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  64. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  65. 0, 0, 0, 0, KEY_UP, KEY_UP, KEY_DOWN, KEY_DOWN,
  66. 0, 0, 0, 0, KEY_EPG, 0, 0, 0,
  67. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  68. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  69. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_VCR
  70. };
  71. /* key-up timer */
  72. static void av7110_emit_keyup(unsigned long parm)
  73. {
  74. struct infrared *ir = (struct infrared *) parm;
  75. if (!ir || !test_bit(ir->last_key, ir->input_dev->key))
  76. return;
  77. input_report_key(ir->input_dev, ir->last_key, 0);
  78. input_sync(ir->input_dev);
  79. }
  80. /* tasklet */
  81. static void av7110_emit_key(unsigned long parm)
  82. {
  83. struct infrared *ir = (struct infrared *) parm;
  84. u32 ircom = ir->ir_command;
  85. u8 data;
  86. u8 addr;
  87. u16 toggle;
  88. u16 keycode;
  89. /* extract device address and data */
  90. switch (ir->protocol) {
  91. case IR_RC5: /* RC5: 5 bits device address, 6 bits data */
  92. data = ircom & 0x3f;
  93. addr = (ircom >> 6) & 0x1f;
  94. toggle = ircom & 0x0800;
  95. break;
  96. case IR_RCMM: /* RCMM: ? bits device address, ? bits data */
  97. data = ircom & 0xff;
  98. addr = (ircom >> 8) & 0x1f;
  99. toggle = ircom & 0x8000;
  100. break;
  101. case IR_RC5_EXT: /* extended RC5: 5 bits device address, 7 bits data */
  102. data = ircom & 0x3f;
  103. addr = (ircom >> 6) & 0x1f;
  104. /* invert 7th data bit for backward compatibility with RC5 keymaps */
  105. if (!(ircom & 0x1000))
  106. data |= 0x40;
  107. toggle = ircom & 0x0800;
  108. break;
  109. default:
  110. printk("%s invalid protocol %x\n", __func__, ir->protocol);
  111. return;
  112. }
  113. input_event(ir->input_dev, EV_MSC, MSC_RAW, (addr << 16) | data);
  114. input_event(ir->input_dev, EV_MSC, MSC_SCAN, data);
  115. keycode = ir->key_map[data];
  116. dprintk(16, "%s: code %08x -> addr %i data 0x%02x -> keycode %i\n",
  117. __func__, ircom, addr, data, keycode);
  118. /* check device address */
  119. if (!(ir->device_mask & (1 << addr)))
  120. return;
  121. if (!keycode) {
  122. printk ("%s: code %08x -> addr %i data 0x%02x -> unknown key!\n",
  123. __func__, ircom, addr, data);
  124. return;
  125. }
  126. if (timer_pending(&ir->keyup_timer)) {
  127. del_timer(&ir->keyup_timer);
  128. if (ir->last_key != keycode || toggle != ir->last_toggle) {
  129. ir->delay_timer_finished = 0;
  130. input_event(ir->input_dev, EV_KEY, ir->last_key, 0);
  131. input_event(ir->input_dev, EV_KEY, keycode, 1);
  132. input_sync(ir->input_dev);
  133. } else if (ir->delay_timer_finished) {
  134. input_event(ir->input_dev, EV_KEY, keycode, 2);
  135. input_sync(ir->input_dev);
  136. }
  137. } else {
  138. ir->delay_timer_finished = 0;
  139. input_event(ir->input_dev, EV_KEY, keycode, 1);
  140. input_sync(ir->input_dev);
  141. }
  142. ir->last_key = keycode;
  143. ir->last_toggle = toggle;
  144. ir->keyup_timer.expires = jiffies + UP_TIMEOUT;
  145. add_timer(&ir->keyup_timer);
  146. }
  147. /* register with input layer */
  148. static void input_register_keys(struct infrared *ir)
  149. {
  150. int i;
  151. set_bit(EV_KEY, ir->input_dev->evbit);
  152. set_bit(EV_REP, ir->input_dev->evbit);
  153. set_bit(EV_MSC, ir->input_dev->evbit);
  154. set_bit(MSC_RAW, ir->input_dev->mscbit);
  155. set_bit(MSC_SCAN, ir->input_dev->mscbit);
  156. memset(ir->input_dev->keybit, 0, sizeof(ir->input_dev->keybit));
  157. for (i = 0; i < ARRAY_SIZE(ir->key_map); i++) {
  158. if (ir->key_map[i] > KEY_MAX)
  159. ir->key_map[i] = 0;
  160. else if (ir->key_map[i] > KEY_RESERVED)
  161. set_bit(ir->key_map[i], ir->input_dev->keybit);
  162. }
  163. ir->input_dev->keycode = ir->key_map;
  164. ir->input_dev->keycodesize = sizeof(ir->key_map[0]);
  165. ir->input_dev->keycodemax = ARRAY_SIZE(ir->key_map);
  166. }
  167. /* called by the input driver after rep[REP_DELAY] ms */
  168. static void input_repeat_key(unsigned long parm)
  169. {
  170. struct infrared *ir = (struct infrared *) parm;
  171. ir->delay_timer_finished = 1;
  172. }
  173. /* check for configuration changes */
  174. int av7110_check_ir_config(struct av7110 *av7110, int force)
  175. {
  176. int i;
  177. int modified = force;
  178. int ret = -ENODEV;
  179. for (i = 0; i < av_cnt; i++)
  180. if (av7110 == av_list[i])
  181. break;
  182. if (i < av_cnt && av7110) {
  183. if ((av7110->ir.protocol & 1) != ir_protocol[i] ||
  184. av7110->ir.inversion != ir_inversion[i])
  185. modified = true;
  186. if (modified) {
  187. /* protocol */
  188. if (ir_protocol[i]) {
  189. ir_protocol[i] = 1;
  190. av7110->ir.protocol = IR_RCMM;
  191. av7110->ir.ir_config = 0x0001;
  192. } else if (FW_VERSION(av7110->arm_app) >= 0x2620) {
  193. av7110->ir.protocol = IR_RC5_EXT;
  194. av7110->ir.ir_config = 0x0002;
  195. } else {
  196. av7110->ir.protocol = IR_RC5;
  197. av7110->ir.ir_config = 0x0000;
  198. }
  199. /* inversion */
  200. if (ir_inversion[i]) {
  201. ir_inversion[i] = 1;
  202. av7110->ir.ir_config |= 0x8000;
  203. }
  204. av7110->ir.inversion = ir_inversion[i];
  205. /* update ARM */
  206. ret = av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, SetIR, 1,
  207. av7110->ir.ir_config);
  208. } else
  209. ret = 0;
  210. /* address */
  211. if (av7110->ir.device_mask != ir_device_mask[i])
  212. av7110->ir.device_mask = ir_device_mask[i];
  213. }
  214. return ret;
  215. }
  216. /* /proc/av7110_ir interface */
  217. static ssize_t av7110_ir_proc_write(struct file *file, const char __user *buffer,
  218. size_t count, loff_t *pos)
  219. {
  220. char *page;
  221. u32 ir_config;
  222. int size = sizeof ir_config + sizeof av_list[0]->ir.key_map;
  223. int i;
  224. if (count < size)
  225. return -EINVAL;
  226. page = vmalloc(size);
  227. if (!page)
  228. return -ENOMEM;
  229. if (copy_from_user(page, buffer, size)) {
  230. vfree(page);
  231. return -EFAULT;
  232. }
  233. memcpy(&ir_config, page, sizeof ir_config);
  234. for (i = 0; i < av_cnt; i++) {
  235. /* keymap */
  236. memcpy(av_list[i]->ir.key_map, page + sizeof ir_config,
  237. sizeof(av_list[i]->ir.key_map));
  238. /* protocol, inversion, address */
  239. ir_protocol[i] = ir_config & 0x0001;
  240. ir_inversion[i] = ir_config & 0x8000 ? 1 : 0;
  241. if (ir_config & 0x4000)
  242. ir_device_mask[i] = 1 << ((ir_config >> 16) & 0x1f);
  243. else
  244. ir_device_mask[i] = IR_ALL;
  245. /* update configuration */
  246. av7110_check_ir_config(av_list[i], false);
  247. input_register_keys(&av_list[i]->ir);
  248. }
  249. vfree(page);
  250. return count;
  251. }
  252. static const struct file_operations av7110_ir_proc_fops = {
  253. .owner = THIS_MODULE,
  254. .write = av7110_ir_proc_write,
  255. .llseek = noop_llseek,
  256. };
  257. /* interrupt handler */
  258. static void ir_handler(struct av7110 *av7110, u32 ircom)
  259. {
  260. dprintk(4, "ir command = %08x\n", ircom);
  261. av7110->ir.ir_command = ircom;
  262. tasklet_schedule(&av7110->ir.ir_tasklet);
  263. }
  264. int __devinit av7110_ir_init(struct av7110 *av7110)
  265. {
  266. struct input_dev *input_dev;
  267. static struct proc_dir_entry *e;
  268. int err;
  269. if (av_cnt >= ARRAY_SIZE(av_list))
  270. return -ENOSPC;
  271. av_list[av_cnt++] = av7110;
  272. av7110_check_ir_config(av7110, true);
  273. init_timer(&av7110->ir.keyup_timer);
  274. av7110->ir.keyup_timer.function = av7110_emit_keyup;
  275. av7110->ir.keyup_timer.data = (unsigned long) &av7110->ir;
  276. input_dev = input_allocate_device();
  277. if (!input_dev)
  278. return -ENOMEM;
  279. av7110->ir.input_dev = input_dev;
  280. snprintf(av7110->ir.input_phys, sizeof(av7110->ir.input_phys),
  281. "pci-%s/ir0", pci_name(av7110->dev->pci));
  282. input_dev->name = "DVB on-card IR receiver";
  283. input_dev->phys = av7110->ir.input_phys;
  284. input_dev->id.bustype = BUS_PCI;
  285. input_dev->id.version = 2;
  286. if (av7110->dev->pci->subsystem_vendor) {
  287. input_dev->id.vendor = av7110->dev->pci->subsystem_vendor;
  288. input_dev->id.product = av7110->dev->pci->subsystem_device;
  289. } else {
  290. input_dev->id.vendor = av7110->dev->pci->vendor;
  291. input_dev->id.product = av7110->dev->pci->device;
  292. }
  293. input_dev->dev.parent = &av7110->dev->pci->dev;
  294. /* initial keymap */
  295. memcpy(av7110->ir.key_map, default_key_map, sizeof av7110->ir.key_map);
  296. input_register_keys(&av7110->ir);
  297. err = input_register_device(input_dev);
  298. if (err) {
  299. input_free_device(input_dev);
  300. return err;
  301. }
  302. input_dev->timer.function = input_repeat_key;
  303. input_dev->timer.data = (unsigned long) &av7110->ir;
  304. if (av_cnt == 1) {
  305. e = proc_create("av7110_ir", S_IWUSR, NULL, &av7110_ir_proc_fops);
  306. if (e)
  307. e->size = 4 + 256 * sizeof(u16);
  308. }
  309. tasklet_init(&av7110->ir.ir_tasklet, av7110_emit_key, (unsigned long) &av7110->ir);
  310. av7110->ir.ir_handler = ir_handler;
  311. return 0;
  312. }
  313. void __devexit av7110_ir_exit(struct av7110 *av7110)
  314. {
  315. int i;
  316. if (av_cnt == 0)
  317. return;
  318. del_timer_sync(&av7110->ir.keyup_timer);
  319. av7110->ir.ir_handler = NULL;
  320. tasklet_kill(&av7110->ir.ir_tasklet);
  321. for (i = 0; i < av_cnt; i++)
  322. if (av_list[i] == av7110) {
  323. av_list[i] = av_list[av_cnt-1];
  324. av_list[av_cnt-1] = NULL;
  325. break;
  326. }
  327. if (av_cnt == 1)
  328. remove_proc_entry("av7110_ir", NULL);
  329. input_unregister_device(av7110->ir.input_dev);
  330. av_cnt--;
  331. }
  332. //MODULE_AUTHOR("Holger Waechtler <holger@convergence.de>, Oliver Endriss <o.endriss@gmx.de>");
  333. //MODULE_LICENSE("GPL");