fp_func.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #include<linux/kernel.h>
  2. #include <linux/platform_device.h>
  3. #include<linux/module.h>
  4. #include<linux/types.h>
  5. #include<linux/fs.h>
  6. #include<linux/errno.h>
  7. #include<linux/mm.h>
  8. #include<linux/sched.h>
  9. #include<linux/init.h>
  10. #include<linux/cdev.h>
  11. #include<asm/io.h>
  12. #include<asm/uaccess.h>
  13. #include <asm/cacheflush.h>
  14. #include<linux/semaphore.h>
  15. #include<linux/slab.h>
  16. #include "../tz_driver/teei_id.h"
  17. #include "../tz_driver/tz_service.h"
  18. #include "../tz_driver/nt_smc_call.h"
  19. #include "teei_fp.h"
  20. #define MICROTRUST_FP_SIZE (0x80000)
  21. #define FP_BUFFER_OFFSET (0x10)
  22. #define FP_LEN_MAX (MICROTRUST_FP_SIZE - FP_BUFFER_OFFSET)
  23. #define FP_LEN_MIN 0
  24. #define CMD_MEM_CLEAR _IO(0x775A777E, 0x1)
  25. #define CMD_FP_CMD _IO(0x775A777E, 0x2)
  26. #define FP_MAJOR 254
  27. #define SHMEM_ENABLE 0
  28. #define SHMEM_DISABLE 1
  29. #define DEV_NAME "teei_fp"
  30. #define FP_DRIVER_ID 100
  31. static int fp_major = FP_MAJOR;
  32. static struct class *driver_class;
  33. static dev_t devno;
  34. struct semaphore fp_api_lock;
  35. struct fp_dev {
  36. struct cdev cdev;
  37. unsigned char mem[MICROTRUST_FP_SIZE];
  38. struct semaphore sem;
  39. };
  40. struct semaphore daulOS_rd_sem;
  41. struct semaphore daulOS_wr_sem;
  42. EXPORT_SYMBOL_GPL(daulOS_rd_sem);
  43. EXPORT_SYMBOL_GPL(daulOS_wr_sem);
  44. /*#define FP_DEBUG*/
  45. extern char *fp_buff_addr;
  46. /*extern unsigned int daulOS_shmem_flags;*/
  47. struct fp_dev *fp_devp;
  48. int fp_open(struct inode *inode, struct file *filp)
  49. {
  50. #ifdef FP_DEBUG
  51. printk("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!say hello from fp!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  52. #endif
  53. filp->private_data = fp_devp;
  54. return 0;
  55. }
  56. int fp_release(struct inode *inode, struct file *filp)
  57. {
  58. filp->private_data = NULL;
  59. return 0;
  60. }
  61. static int fp_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  62. {
  63. down(&fp_api_lock);
  64. unsigned int args_len = 0;
  65. unsigned int fp_cid = 0xFF;
  66. unsigned int fp_fid = 0xFF;
  67. unsigned char args[16] = {0};
  68. unsigned int buff_len = 0;
  69. #ifdef FP_DEBUG
  70. printk("##################################\n");
  71. printk("fp ioctl received received cmd is: %x arg is %x\n", cmd, (unsigned int)arg);
  72. printk("CMD_MEM_CLEAR is: %x CMD_FP_CMD is %x \n", CMD_MEM_CLEAR, CMD_FP_CMD);
  73. #endif
  74. switch (cmd) {
  75. case CMD_MEM_CLEAR:
  76. pr_err("CMD MEM CLEAR.\n");
  77. break;
  78. case CMD_FP_CMD:
  79. if (copy_from_user((void *)args, (void *)arg, FP_BUFFER_OFFSET)) {
  80. pr_err("copy args from user failed.\n");
  81. up(&fp_api_lock);
  82. return -EFAULT;
  83. }
  84. /*TODO compute args length*/
  85. /*[11-15] is the length of data*/
  86. args_len = *((unsigned int *)(args + 12));
  87. if (args_len > FP_LEN_MAX || (args_len <= FP_LEN_MIN)) {
  88. pr_err("args_len is invalid %d !\n", args_len);
  89. up(&fp_api_lock);
  90. return -EFAULT;
  91. }
  92. buff_len = args_len + FP_BUFFER_OFFSET;
  93. /*[0-3] is cmd id*/
  94. fp_cid = *((unsigned int *)(args));
  95. /*[4-7] is fuction id*/
  96. fp_fid = *((unsigned int *)(args + 4));
  97. #ifdef FP_DEBUG
  98. pr_debug("invoke fp cmd CMD_FP_CMD: arg's address is %x, args's length %d\n",
  99. (unsigned int)arg, args_len);
  100. pr_debug("invoke fp cmd fp_cid is %d fp_fid is %d\n", fp_cid, fp_fid);
  101. #endif
  102. if (!fp_buff_addr) {
  103. pr_err("fp_buiff_addr is invalid!.\n");
  104. up(&fp_api_lock);
  105. return -EFAULT;
  106. }
  107. memset((void *)fp_buff_addr, 0, buff_len);
  108. if (copy_from_user((void *)fp_buff_addr, (void *)arg, buff_len)) {
  109. pr_err("copy from user failed.\n");
  110. up(&fp_api_lock);
  111. return -EFAULT;
  112. }
  113. Flush_Dcache_By_Area((unsigned long)fp_buff_addr,
  114. fp_buff_addr + MICROTRUST_FP_SIZE);
  115. /*send command data to TEEI*/
  116. send_fp_command(FP_DRIVER_ID);
  117. #ifdef FP_DEBUG
  118. printk("back from TEEI try copy share mem to user \n");
  119. printk("result in share memory %d \n", *((unsigned int *)fp_buff_addr));
  120. printk("[%s][%d] fp_buff_addr 88 - 91 = %d\n", __func__, args_len, *((unsigned int *)(fp_buff_addr + 88)));
  121. #endif
  122. if (copy_to_user((void *)arg, (void *)fp_buff_addr, buff_len)) {
  123. pr_err("copy from user failed.\n");
  124. up(&fp_api_lock);
  125. return -EFAULT;
  126. }
  127. #ifdef FP_DEBUG
  128. printk("result after copy %d \n", *((unsigned int *)arg));
  129. printk("invoke fp cmd end. \n");
  130. #endif
  131. break;
  132. default:
  133. up(&fp_api_lock);
  134. return -EINVAL;
  135. }
  136. up(&fp_api_lock);
  137. return 0;
  138. }
  139. static ssize_t fp_read(struct file *filp, char __user *buf,
  140. size_t size, loff_t *ppos)
  141. {
  142. int ret = 0;
  143. return ret;
  144. }
  145. static ssize_t fp_write(struct file *filp, const char __user *buf,
  146. size_t size, loff_t *ppos)
  147. {
  148. return 0;
  149. }
  150. static loff_t fp_llseek(struct file *filp, loff_t offset, int orig)
  151. {
  152. return 0;
  153. }
  154. static const struct file_operations fp_fops = {
  155. .owner = THIS_MODULE,
  156. .llseek = fp_llseek,
  157. .read = fp_read,
  158. .write = fp_write,
  159. .unlocked_ioctl = fp_ioctl,
  160. #ifdef CONFIG_COMPAT
  161. .compat_ioctl = fp_ioctl,
  162. #endif
  163. .open = fp_open,
  164. .release = fp_release
  165. };
  166. static void fp_setup_cdev(struct fp_dev *dev, int index)
  167. {
  168. int err = 0;
  169. int devno = MKDEV(fp_major, index);
  170. cdev_init(&dev->cdev, &fp_fops);
  171. dev->cdev.owner = fp_fops.owner;
  172. err = cdev_add(&dev->cdev, devno, 1);
  173. if (err) {
  174. printk(KERN_NOTICE "Error %d adding fp %d.\n", err, index);
  175. }
  176. }
  177. int fp_init(void)
  178. {
  179. int result = 0;
  180. struct device *class_dev = NULL;
  181. devno = MKDEV(fp_major, 0);
  182. result = alloc_chrdev_region(&devno, 0, 1, DEV_NAME);
  183. fp_major = MAJOR(devno);
  184. sema_init(&(fp_api_lock), 1);
  185. if (result < 0) {
  186. return result;
  187. }
  188. driver_class = NULL;
  189. driver_class = class_create(THIS_MODULE, DEV_NAME);
  190. if (IS_ERR(driver_class)) {
  191. result = -ENOMEM;
  192. printk("class_create failed %d.\n", result);
  193. goto unregister_chrdev_region;
  194. }
  195. class_dev = device_create(driver_class, NULL, devno, NULL, DEV_NAME);
  196. if (!class_dev) {
  197. result = -ENOMEM;
  198. printk("class_device_create failed %d.\n", result);
  199. goto class_destroy;
  200. }
  201. fp_devp = NULL;
  202. fp_devp = kmalloc(sizeof(struct fp_dev), GFP_KERNEL);
  203. if (fp_devp == NULL) {
  204. result = -ENOMEM;
  205. goto class_device_destroy;
  206. }
  207. memset(fp_devp, 0, sizeof(struct fp_dev));
  208. fp_setup_cdev(fp_devp, 0);
  209. sema_init(&fp_devp->sem, 1);
  210. sema_init(&daulOS_rd_sem, 0);
  211. sema_init(&daulOS_wr_sem, 0);
  212. printk("[%s][%d]create the teei_fp device node successfully!\n", __func__,
  213. __LINE__);
  214. goto return_fn;
  215. class_device_destroy: device_destroy(driver_class, devno);
  216. class_destroy: class_destroy(driver_class);
  217. unregister_chrdev_region: unregister_chrdev_region(devno, 1);
  218. return_fn: return result;
  219. }
  220. void fp_exit(void)
  221. {
  222. device_destroy(driver_class, devno);
  223. class_destroy(driver_class);
  224. cdev_del(&fp_devp->cdev);
  225. kfree(fp_devp);
  226. unregister_chrdev_region(MKDEV(fp_major, 0), 1);
  227. }
  228. MODULE_AUTHOR("Microtrust");
  229. MODULE_LICENSE("Dual BSD/GPL");
  230. module_param(fp_major, int, S_IRUGO);
  231. module_init(fp_init);
  232. module_exit(fp_exit);