user-sp-rc-input.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* Copyright (c) 2012, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/ioctl.h>
  16. #include <linux/fs.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/device.h>
  20. #include <linux/cdev.h>
  21. #include <linux/slab.h>
  22. #include <media/rc-core.h>
  23. #include <media/user-rc-input.h>
  24. #define MAX_SP_DEVICES 1
  25. #define USER_SP_INPUT_DEV_NAME "user-sp-input"
  26. #define USER_SP_INPUT_DRV_NAME "sp-user-input"
  27. struct user_sp_input_dev {
  28. struct cdev sp_input_cdev;
  29. struct class *sp_input_class;
  30. struct device *sp_input_dev;
  31. struct rc_dev *spdev;
  32. dev_t sp_input_base_dev;
  33. struct device *dev;
  34. int in_use;
  35. };
  36. static int user_sp_input_open(struct inode *inode, struct file *file)
  37. {
  38. struct cdev *input_cdev = inode->i_cdev;
  39. struct user_sp_input_dev *input_dev =
  40. container_of(input_cdev, struct user_sp_input_dev, sp_input_cdev);
  41. if (input_dev->in_use) {
  42. dev_err(input_dev->dev,
  43. "Device is already open..only one instance is allowed\n");
  44. return -EBUSY;
  45. }
  46. input_dev->in_use++;
  47. file->private_data = input_dev;
  48. return 0;
  49. }
  50. static int user_sp_input_release(struct inode *inode, struct file *file)
  51. {
  52. struct user_sp_input_dev *input_dev = file->private_data;
  53. input_dev->in_use--;
  54. return 0;
  55. }
  56. static ssize_t user_sp_input_write(struct file *file,
  57. const char __user *buffer, size_t count, loff_t *ppos)
  58. {
  59. int ret = count;
  60. struct user_sp_input_dev *input_dev = file->private_data;
  61. unsigned char cmd = 0;
  62. int scancode = 0;
  63. if (copy_from_user(&cmd, buffer, 1)) {
  64. dev_err(input_dev->dev, "Copy from user failed\n");
  65. ret = -EFAULT;
  66. goto out_free;
  67. }
  68. if (copy_from_user(&scancode, &buffer[1], 4)) {
  69. dev_err(input_dev->dev, "Copy from user failed\n");
  70. ret = -EFAULT;
  71. goto out_free;
  72. }
  73. switch (cmd) {
  74. case USER_CONTROL_PRESSED:
  75. dev_dbg(input_dev->dev, "user controlled pressed 0x%x\n",
  76. scancode);
  77. rc_keydown(input_dev->spdev, scancode, 0);
  78. break;
  79. case USER_CONTROL_REPEATED:
  80. dev_dbg(input_dev->dev, "user controlled repeated 0x%x\n",
  81. scancode);
  82. rc_repeat(input_dev->spdev);
  83. break;
  84. case USER_CONTROL_RELEASED:
  85. dev_dbg(input_dev->dev, "user controlled released 0x%x\n",
  86. scancode);
  87. rc_keyup(input_dev->spdev);
  88. break;
  89. }
  90. out_free:
  91. return ret;
  92. }
  93. const struct file_operations sp_fops = {
  94. .owner = THIS_MODULE,
  95. .open = user_sp_input_open,
  96. .write = user_sp_input_write,
  97. .release = user_sp_input_release,
  98. };
  99. static int __devinit user_sp_input_probe(struct platform_device *pdev)
  100. {
  101. struct user_sp_input_dev *user_sp_dev;
  102. struct rc_dev *spdev;
  103. int retval;
  104. user_sp_dev = kzalloc(sizeof(struct user_sp_input_dev), GFP_KERNEL);
  105. if (!user_sp_dev)
  106. return -ENOMEM;
  107. user_sp_dev->sp_input_class = class_create(THIS_MODULE,
  108. "user-sp-input-loopback");
  109. if (IS_ERR(user_sp_dev->sp_input_class)) {
  110. retval = PTR_ERR(user_sp_dev->sp_input_class);
  111. goto err;
  112. }
  113. retval = alloc_chrdev_region(&user_sp_dev->sp_input_base_dev, 0,
  114. MAX_SP_DEVICES, USER_SP_INPUT_DEV_NAME);
  115. if (retval) {
  116. dev_err(&pdev->dev,
  117. "alloc_chrdev_region failed\n");
  118. goto alloc_chrdev_err;
  119. }
  120. dev_info(&pdev->dev, "User space report standby key event input" \
  121. " driver registered, major %d\n",
  122. MAJOR(user_sp_dev->sp_input_base_dev));
  123. cdev_init(&user_sp_dev->sp_input_cdev, &sp_fops);
  124. retval = cdev_add(&user_sp_dev->sp_input_cdev,
  125. user_sp_dev->sp_input_base_dev, MAX_SP_DEVICES);
  126. if (retval) {
  127. dev_err(&pdev->dev, "cdev_add failed\n");
  128. goto cdev_add_err;
  129. }
  130. user_sp_dev->sp_input_dev = device_create(user_sp_dev->sp_input_class,
  131. NULL, MKDEV(MAJOR(user_sp_dev->sp_input_base_dev), 0), NULL,
  132. "user-sp-input-dev%d", 0);
  133. if (IS_ERR(user_sp_dev->sp_input_dev)) {
  134. retval = PTR_ERR(user_sp_dev->sp_input_dev);
  135. dev_err(&pdev->dev, "device_create failed\n");
  136. goto device_create_err;
  137. }
  138. spdev = rc_allocate_device();
  139. if (!spdev) {
  140. dev_err(&pdev->dev, "failed to allocate rc device");
  141. retval = -ENOMEM;
  142. goto err_allocate_device;
  143. }
  144. spdev->driver_type = RC_DRIVER_SCANCODE;
  145. spdev->allowed_protos = RC_TYPE_OTHER;
  146. spdev->input_name = USER_SP_INPUT_DEV_NAME;
  147. spdev->input_id.bustype = BUS_HOST;
  148. spdev->driver_name = USER_SP_INPUT_DRV_NAME;
  149. spdev->map_name = RC_MAP_RC6_PHILIPS;
  150. retval = rc_register_device(spdev);
  151. if (retval < 0) {
  152. dev_err(&pdev->dev, "failed to register rc device\n");
  153. goto rc_register_err;
  154. }
  155. user_sp_dev->spdev = spdev;
  156. user_sp_dev->dev = &pdev->dev;
  157. platform_set_drvdata(pdev, user_sp_dev);
  158. user_sp_dev->in_use = 0;
  159. return 0;
  160. rc_register_err:
  161. rc_free_device(spdev);
  162. err_allocate_device:
  163. device_destroy(user_sp_dev->sp_input_class,
  164. MKDEV(MAJOR(user_sp_dev->sp_input_base_dev), 0));
  165. cdev_add_err:
  166. unregister_chrdev_region(user_sp_dev->sp_input_base_dev,
  167. MAX_SP_DEVICES);
  168. device_create_err:
  169. cdev_del(&user_sp_dev->sp_input_cdev);
  170. alloc_chrdev_err:
  171. class_destroy(user_sp_dev->sp_input_class);
  172. err:
  173. kfree(user_sp_dev);
  174. return retval;
  175. }
  176. static int __devexit user_sp_input_remove(struct platform_device *pdev)
  177. {
  178. struct user_sp_input_dev *user_sp_dev = platform_get_drvdata(pdev);
  179. platform_set_drvdata(pdev, NULL);
  180. rc_free_device(user_sp_dev->spdev);
  181. device_destroy(user_sp_dev->sp_input_class,
  182. MKDEV(MAJOR(user_sp_dev->sp_input_base_dev), 0));
  183. unregister_chrdev_region(user_sp_dev->sp_input_base_dev,
  184. MAX_SP_DEVICES);
  185. cdev_del(&user_sp_dev->sp_input_cdev);
  186. class_destroy(user_sp_dev->sp_input_class);
  187. kfree(user_sp_dev);
  188. return 0;
  189. }
  190. static struct platform_driver user_sp_input_driver = {
  191. .probe = user_sp_input_probe,
  192. .remove = __devexit_p(user_sp_input_remove),
  193. .driver = {
  194. .name = USER_SP_INPUT_DRV_NAME,
  195. .owner = THIS_MODULE,
  196. },
  197. };
  198. static int __init user_sp_input_init(void)
  199. {
  200. return platform_driver_register(&user_sp_input_driver);
  201. }
  202. module_init(user_sp_input_init);
  203. static void __exit user_sp_input_exit(void)
  204. {
  205. platform_driver_unregister(&user_sp_input_driver);
  206. }
  207. module_exit(user_sp_input_exit);
  208. MODULE_DESCRIPTION("User SP RC Input driver");
  209. MODULE_LICENSE("GPL v2");