hanvon_virt_pen.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include <linux/module.h>
  2. #include <linux/init.h>
  3. #include <linux/fs.h>
  4. #include <linux/interrupt.h>
  5. #include <linux/irq.h>
  6. #include <linux/cdev.h>
  7. #include <linux/sched.h>
  8. #include <linux/pm.h>
  9. #include <linux/slab.h>
  10. #include <linux/sysctl.h>
  11. #include <linux/proc_fs.h>
  12. #include <linux/delay.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/input.h>
  15. #include <linux/workqueue.h>
  16. #include <linux/gpio.h>
  17. #include <linux/device.h>
  18. #include <linux/version.h>
  19. #include <asm/uaccess.h>
  20. struct hanvon_char_dev
  21. {
  22. struct cdev cdev;
  23. };
  24. struct hanvon_pen_data
  25. {
  26. u16 x;
  27. u16 y;
  28. u16 pressure;
  29. bool isLeave;
  30. };
  31. //#define HANVON_PEN_DEBUG
  32. #define MAX_EVENTS 600
  33. int isPenOn = 0;
  34. static int global_major = 0;
  35. static int global_minor = 0;
  36. static struct input_dev* pen_idev = NULL;
  37. static struct hanvon_char_dev *p_char_dev = NULL;
  38. static struct class *hanvon_class;
  39. static int hanvon_cdev_open (struct inode* inode, struct file* filp)
  40. {
  41. return 0;
  42. }
  43. static int hanvon_cdev_release (struct inode* inode, struct file* filp)
  44. {
  45. return 0;
  46. }
  47. static ssize_t hanvon_cdev_read (struct file *filp, char __user *buf, size_t count, loff_t *f_ops)
  48. {
  49. printk (KERN_INFO "%s\n",__func__);
  50. return count;
  51. }
  52. static ssize_t hanvon_cdev_write (struct file *filp, const char __user *buf, size_t count, loff_t *offset)
  53. {
  54. #ifdef HANVON_PEN_DEBUG
  55. printk (KERN_INFO "%s\n",__func__);
  56. #endif
  57. struct hanvon_pen_data data = {0};
  58. if (copy_from_user(&data,buf,sizeof(struct hanvon_pen_data)))
  59. {
  60. return -EFAULT;
  61. }
  62. do {
  63. isPenOn = 1; // indicate that pen is on.
  64. input_report_abs(pen_idev, ABS_MT_TRACKING_ID, 0);
  65. input_report_abs(pen_idev, ABS_MT_TOUCH_MAJOR, data.pressure);
  66. input_report_abs(pen_idev, ABS_MT_POSITION_X, data.x);
  67. input_report_abs(pen_idev, ABS_MT_POSITION_Y, data.y);
  68. input_report_abs(pen_idev, ABS_MT_WIDTH_MAJOR, 0);
  69. input_mt_sync(pen_idev);
  70. input_sync(pen_idev);
  71. }while(false);
  72. if (data.isLeave == true)
  73. isPenOn = 0;
  74. #ifdef HANVON_PEN_DEBUG
  75. printk(KERN_INFO "(pressure)=%d,(x)=%d,(y)=%d\n",data.pressure,data.x,data.y);
  76. #endif
  77. return count;
  78. }
  79. static const struct file_operations hanvon_cdev_fops = {
  80. .owner = THIS_MODULE,
  81. .open = hanvon_cdev_open,
  82. .read = hanvon_cdev_read,
  83. .write = hanvon_cdev_write,
  84. .release = hanvon_cdev_release,
  85. };
  86. static struct hanvon_char_dev *hanvon_setup_cdev (dev_t dev)
  87. {
  88. int result;
  89. struct hanvon_char_dev *pcdev = NULL;
  90. pcdev = kmalloc(1*sizeof(struct hanvon_char_dev),GFP_KERNEL);
  91. if (pcdev == NULL)
  92. printk(KERN_NOTICE "%s: malloc memmory failed!\n",__func__);
  93. memset(pcdev, 0, sizeof(struct hanvon_char_dev));
  94. cdev_init(&pcdev->cdev, &hanvon_cdev_fops);
  95. pcdev->cdev.owner = THIS_MODULE;
  96. result = cdev_add(&pcdev->cdev, dev, 1);
  97. if (result)
  98. {
  99. printk(KERN_NOTICE "%s: add cdev failed. \n", __func__);
  100. }
  101. return pcdev;
  102. }
  103. static struct input_dev * allocate_Input_Dev(void)
  104. {
  105. int ret;
  106. struct input_dev *pInputDev=NULL;
  107. pInputDev = input_allocate_device();
  108. if(pInputDev == NULL)
  109. {
  110. printk(KERN_NOTICE, " Failed to allocate input device\n");
  111. return NULL;//-ENOMEM;
  112. }
  113. pInputDev->name = "Hanvon-Pen";
  114. pInputDev->phys = "UART";
  115. //pInputDev->id.bustype = BUS_I2C;
  116. pInputDev->id.vendor = 0x0F0F;
  117. pInputDev->id.product = 0x0099;
  118. pInputDev->id.version = 0x001;
  119. set_bit(EV_ABS, pInputDev->evbit);
  120. #if 0
  121. set_bit(EV_KEY, pInputDev->evbit);
  122. __set_bit(BTN_LEFT, pInputDev->keybit);
  123. input_set_abs_params(pInputDev, ABS_X, 0, 2047, 0, 0);
  124. input_set_abs_params(pInputDev, ABS_Y, 0, 2047, 0, 0);
  125. #endif
  126. input_set_abs_params(pInputDev, ABS_MT_POSITION_X, 0, 2047, 0, 0);
  127. input_set_abs_params(pInputDev, ABS_MT_POSITION_Y, 0, 2047, 0, 0);
  128. input_set_abs_params(pInputDev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
  129. input_set_abs_params(pInputDev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
  130. input_set_abs_params(pInputDev, ABS_MT_TRACKING_ID, 0, 1, 0, 0);
  131. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
  132. input_set_events_per_packet(pInputDev, MAX_EVENTS);
  133. #endif
  134. ret = input_register_device(pInputDev);
  135. if(ret)
  136. {
  137. printk(KERN_NOTICE, " Unable to register input device.\n");
  138. input_free_device(pInputDev);
  139. pInputDev = NULL;
  140. }
  141. return pInputDev;
  142. }
  143. static int hanvon_pen_init (void)
  144. {
  145. int result;
  146. dev_t devno = 0;
  147. if (global_major)
  148. {
  149. devno = MKDEV(global_major,global_minor);
  150. result = register_chrdev_region (devno,1,"hanvon_pen");
  151. }
  152. else
  153. {
  154. result = alloc_chrdev_region (&devno, global_minor, 1, "hanvon_pen");
  155. global_major = MAJOR(devno);
  156. }
  157. if (result < 0)
  158. {
  159. return 0;
  160. }
  161. p_char_dev = hanvon_setup_cdev (devno);
  162. if (!p_char_dev)
  163. {
  164. printk(KERN_NOTICE "%s: hanvon_setup_cdev failed .\n",__func__);
  165. result = -ENOMEM;
  166. }
  167. hanvon_class = class_create(THIS_MODULE, "hanvon_pen");
  168. if (IS_ERR(hanvon_class))
  169. {
  170. printk(KERN_NOTICE "%s: class_create failed .\n",__func__);
  171. return -EFAULT;
  172. }
  173. device_create(hanvon_class, NULL, devno, NULL, "hanvon_pen");
  174. printk(KERN_INFO "Registor hanvon pen char device done, major: %d\n",global_major);
  175. // Register hanvon input device
  176. pen_idev = allocate_Input_Dev();
  177. if (!pen_idev)
  178. {
  179. printk(KERN_NOTICE, "allocate input device failed.\n");
  180. return -EINVAL;
  181. }
  182. printk(KERN_INFO "Registor hanvon input device done.\n");
  183. return 0;
  184. }
  185. static void hanvon_pen_exit (void)
  186. {
  187. dev_t devno = MKDEV(global_major, global_minor);
  188. if (p_char_dev)
  189. {
  190. cdev_del(&p_char_dev->cdev);
  191. kfree(p_char_dev);
  192. p_char_dev = NULL;
  193. }
  194. unregister_chrdev_region (devno, 1);
  195. if (!IS_ERR(hanvon_class))
  196. {
  197. device_destroy(hanvon_class, devno);
  198. class_destroy(hanvon_class);
  199. }
  200. printk (KERN_NOTICE "hanvon pen exit. \n");
  201. }
  202. module_init(hanvon_pen_init);
  203. module_exit(hanvon_pen_exit);
  204. MODULE_LICENSE("GPL");
  205. MODULE_AUTHOR("Hanvon Tec.");
  206. MODULE_DESCRIPTION("pen driver for Hanvon");
  207. MODULE_ALIAS("platform:virtual");