virtual_remote.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * linux/drivers/input/irremote/virtual_remote.c
  3. *
  4. * Virtual Keypad Driver
  5. *
  6. * Copyright (C) 2010 Amlogic Corporation
  7. *
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. * author : jianfeng_wang
  23. */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/types.h>
  28. #include <linux/input.h>
  29. #include <linux/kernel.h>
  30. #include <linux/delay.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/mutex.h>
  33. #include <linux/errno.h>
  34. #include <asm/irq.h>
  35. #include <asm/io.h>
  36. #include <mach/am_regs.h>
  37. #include <linux/slab.h>
  38. #include <linux/major.h>
  39. #include <asm/uaccess.h>
  40. struct input_struct {
  41. struct input_dev *input;
  42. struct timer_list timer;
  43. unsigned int scancode;
  44. int config_major;
  45. char config_name[20];
  46. struct class *config_class;
  47. struct device *config_dev;
  48. };
  49. static struct input_struct *gp_input_device = NULL;
  50. static void timer_sr(unsigned long data)
  51. {
  52. struct input_struct *kp = (struct input_struct *)data;
  53. input_report_key(kp->input, (kp->scancode) & 0xff, 0);
  54. }
  55. static int virtual_remote_open(struct inode *inode, struct file *file)
  56. {
  57. file->private_data = gp_input_device;
  58. return 0;
  59. }
  60. static int
  61. virtual_remote_ioctl(struct inode *inode, struct file *filp,
  62. unsigned int cmd, unsigned long args)
  63. {
  64. unsigned int scan_code;
  65. struct input_struct *kp = (struct input_struct *)filp->private_data;
  66. switch (cmd) {
  67. case 2:
  68. kp->timer.data = (unsigned long)kp;
  69. mod_timer(&kp->timer, jiffies + msecs_to_jiffies(120));
  70. case 1:
  71. copy_from_user(&scan_code, (unsigned int *)args,
  72. sizeof(unsigned int));
  73. kp->scancode = scan_code;
  74. input_report_key(kp->input, scan_code & 0xff, 1);
  75. break;
  76. case 0:
  77. copy_from_user(&scan_code, (unsigned int *)args,
  78. sizeof(unsigned int));
  79. input_report_key(kp->input, scan_code & 0xff, 0);
  80. break;
  81. default:
  82. printk("%s : Wrong command code\n", __func__);
  83. return -EACCES;
  84. }
  85. return 0;
  86. }
  87. static int virtual_remote_release(struct inode *inode, struct file *file)
  88. {
  89. file->private_data = NULL;
  90. return 0;
  91. }
  92. static const struct file_operations virtual_remote_fops = {
  93. .owner = THIS_MODULE,
  94. .open = virtual_remote_open,
  95. .ioctl = virtual_remote_ioctl,
  96. .release = virtual_remote_release,
  97. };
  98. static void register_remote_dev(struct input_struct *kp)
  99. {
  100. int ret = 0;
  101. strcpy(kp->config_name, "virtualremote");
  102. ret = register_chrdev(0, kp->config_name, &virtual_remote_fops);
  103. if (ret <= 0) {
  104. printk("register char dev (VirtualRemote) error\r\n");
  105. return;
  106. }
  107. kp->config_major = ret;
  108. kp->config_class = class_create(THIS_MODULE, kp->config_name);
  109. kp->config_dev =
  110. device_create(kp->config_class, NULL, MKDEV(kp->config_major, 0),
  111. NULL, kp->config_name);
  112. return;
  113. }
  114. static int __init virtual_remote_probe(struct platform_device *pdev)
  115. {
  116. struct input_struct *apollo_kp;
  117. struct input_dev *input_dev;
  118. int i, ret;
  119. apollo_kp = kzalloc(sizeof(struct input_struct), GFP_KERNEL);
  120. input_dev = input_allocate_device();
  121. if (!apollo_kp || !input_dev) {
  122. kfree(apollo_kp);
  123. input_free_device(input_dev);
  124. printk("creat device failed.\n");
  125. return -ENOMEM;
  126. }
  127. gp_input_device = apollo_kp;
  128. platform_set_drvdata(pdev, apollo_kp);
  129. apollo_kp->input = input_dev;
  130. setup_timer(&apollo_kp->timer, timer_sr, 0);
  131. /* setup input device */
  132. set_bit(EV_KEY, input_dev->evbit);
  133. set_bit(EV_REP, input_dev->evbit);
  134. for (i = 0; i < KEY_MAX; i++) {
  135. set_bit(i, input_dev->keybit);
  136. }
  137. input_dev->name = "Virtual-Remote";
  138. input_dev->phys = "Virtual-Remote/input0";
  139. input_dev->dev.parent = &pdev->dev;
  140. input_dev->id.bustype = BUS_ISA;
  141. input_dev->id.vendor = 0x0001;
  142. input_dev->id.product = 0x0001;
  143. input_dev->id.version = 0x0100;
  144. input_dev->rep[REP_DELAY] = 0xffffffff;
  145. input_dev->rep[REP_PERIOD] = 0xffffffff;
  146. input_dev->keycodesize = sizeof(unsigned short);
  147. input_dev->keycodemax = 0x1ff;
  148. ret = input_register_device(input_dev);
  149. if (ret < 0) {
  150. printk(KERN_ERR
  151. "Unable to register Virtual Remote input device\n");
  152. kfree(apollo_kp);
  153. input_free_device(input_dev);
  154. gp_input_device = NULL;
  155. return -EINVAL;
  156. }
  157. printk("input_register_device completed \r\n");
  158. register_remote_dev(apollo_kp);
  159. return 0;
  160. }
  161. static int virtual_remote_remove(struct platform_device *pdev)
  162. {
  163. struct input_struct *apollo_kp = platform_get_drvdata(pdev);
  164. /* disable keypad interrupt handling */
  165. printk("remove Virtual Remote. \r\n");
  166. /* unregister everything */
  167. input_unregister_device(apollo_kp->input);
  168. input_free_device(apollo_kp->input);
  169. unregister_chrdev(apollo_kp->config_major, apollo_kp->config_name);
  170. if (apollo_kp->config_class) {
  171. if (apollo_kp->config_dev)
  172. device_destroy(apollo_kp->config_class,
  173. MKDEV(apollo_kp->config_major, 0));
  174. class_destroy(apollo_kp->config_class);
  175. }
  176. kfree(apollo_kp);
  177. gp_input_device = NULL;
  178. return 0;
  179. }
  180. static struct platform_driver virtual_remote_driver = {
  181. .probe = virtual_remote_probe,
  182. .remove = virtual_remote_remove,
  183. .suspend = NULL,
  184. .resume = NULL,
  185. .driver = {
  186. .name = "apollo-kp",
  187. },
  188. };
  189. static int __devinit virtual_remote_init(void)
  190. {
  191. printk(KERN_INFO "Virtual Remote Driver for QA testing\n");
  192. return platform_driver_register(&virtual_remote_driver);
  193. }
  194. static void __exit virtual_remote_exit(void)
  195. {
  196. printk(KERN_INFO "Virtual Remote exit \n");
  197. platform_driver_unregister(&virtual_remote_driver);
  198. }
  199. module_init(virtual_remote_init);
  200. module_exit(virtual_remote_exit);
  201. MODULE_AUTHOR("geng.li");
  202. MODULE_DESCRIPTION("Virtual Remote Driver");
  203. MODULE_LICENSE("GPL");