fp_func.c 6.0 KB

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