vfsFun.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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<linux/semaphore.h>
  14. #include<linux/slab.h>
  15. #include"TEEI.h"
  16. #include"teei_id.h"
  17. #define VFS_SIZE 0x80000
  18. #define MEM_CLEAR 0x1
  19. #define VFS_MAJOR 253
  20. static int vfs_major = VFS_MAJOR;
  21. static struct class *driver_class;
  22. static dev_t devno;
  23. struct vfs_dev {
  24. struct cdev cdev;
  25. unsigned char mem[VFS_SIZE];
  26. struct semaphore sem;
  27. };
  28. extern struct completion global_down_lock;
  29. #ifdef VFS_RDWR_SEM
  30. struct semaphore VFS_rd_sem;
  31. EXPORT_SYMBOL_GPL(VFS_rd_sem);
  32. struct semaphore VFS_wr_sem;
  33. EXPORT_SYMBOL_GPL(VFS_wr_sem);
  34. #else
  35. DECLARE_COMPLETION(VFS_rd_comp);
  36. EXPORT_SYMBOL_GPL(VFS_rd_comp);
  37. DECLARE_COMPLETION(VFS_wr_comp);
  38. EXPORT_SYMBOL_GPL(VFS_wr_comp);
  39. #endif
  40. struct vfs_dev *vfs_devp = NULL;
  41. int tz_vfs_open(struct inode *inode, struct file *filp)
  42. {
  43. filp->private_data = vfs_devp;
  44. return 0;
  45. }
  46. int tz_vfs_release(struct inode *inode, struct file *filp)
  47. {
  48. filp->private_data = NULL;
  49. return 0;
  50. }
  51. static long tz_vfs_ioctl(struct file *filp,
  52. unsigned int cmd, unsigned long arg)
  53. {
  54. struct vfs_dev *dev = filp->private_data;
  55. switch (cmd) {
  56. case MEM_CLEAR:
  57. if (down_interruptible(&dev->sem))
  58. return -ERESTARTSYS;
  59. memset(dev->mem, 0, VFS_SIZE);
  60. up(&dev->sem);
  61. printk(KERN_INFO "VFS is set to zero. \n");
  62. break;
  63. default:
  64. return -EINVAL;
  65. }
  66. return 0;
  67. }
  68. static ssize_t tz_vfs_read(struct file *filp, char __user *buf,
  69. size_t size, loff_t *ppos)
  70. {
  71. struct TEEI_vfs_command *vfs_p = NULL;
  72. int length = 0;
  73. int ret = 0;
  74. int cpu_id = raw_smp_processor_id();
  75. /*printk("read begin cpu[%d]\n",cpu_id);*/
  76. #ifdef VFS_RDWR_SEM
  77. down_interruptible(&VFS_rd_sem);
  78. #else
  79. ret = wait_for_completion_interruptible(&VFS_rd_comp);
  80. if (ret == -ERESTARTSYS) {
  81. printk("[%s][%d] ----------------wait_for_completion_interruptible_timeout interrupt----------------------- \n", __func__, __LINE__);
  82. complete(&global_down_lock);
  83. return ret;
  84. }
  85. #endif
  86. vfs_p = (struct TEEI_vfs_command *)daulOS_VFS_share_mem;
  87. #if 1
  88. if (vfs_p->cmd_size > size)
  89. length = size;
  90. else
  91. length = vfs_p->cmd_size;
  92. #endif
  93. length = size;
  94. if (copy_to_user(buf, (void *)vfs_p, length))
  95. ret = -EFAULT;
  96. else
  97. ret = length;
  98. cpu_id = raw_smp_processor_id();
  99. /*printk("read end cpu_id[%d]\n",cpu_id);*/
  100. return ret;
  101. }
  102. static ssize_t tz_vfs_write(struct file *filp, const char __user *buf,
  103. size_t size, loff_t *ppos)
  104. {
  105. int cpu_id = raw_smp_processor_id();
  106. /*printk("write begin cpu_id[%d]\n",cpu_id);*/
  107. if (copy_from_user((void *)daulOS_VFS_share_mem, buf, size))
  108. return -EFAULT;
  109. Flush_Dcache_By_Area((unsigned long)daulOS_VFS_share_mem, (unsigned long)daulOS_VFS_share_mem + size);
  110. #ifdef VFS_RDWR_SEM
  111. up(&VFS_wr_sem);
  112. #else
  113. complete(&VFS_wr_comp);
  114. #endif
  115. cpu_id = raw_smp_processor_id();
  116. /*printk("write end cpu_id[%d]\n",cpu_id);*/
  117. return 0;
  118. }
  119. static loff_t tz_vfs_llseek(struct file *filp, loff_t offset, int orig)
  120. {
  121. loff_t ret = 0;
  122. switch (orig) {
  123. case 0:
  124. if (offset < 0) {
  125. ret = -EINVAL;
  126. break;
  127. }
  128. if ((unsigned int)offset > VFS_SIZE) {
  129. ret = -EINVAL;
  130. break;
  131. }
  132. filp->f_pos = (unsigned int)offset;
  133. ret = filp->f_pos;
  134. break;
  135. case 1:
  136. if ((filp->f_pos + offset) > VFS_SIZE) {
  137. ret = -EINVAL;
  138. break;
  139. }
  140. if ((filp->f_pos + offset) < 0) {
  141. ret = -EINVAL;
  142. break;
  143. }
  144. filp->f_pos += offset;
  145. ret = filp->f_pos;
  146. break;
  147. default:
  148. ret = -EINVAL;
  149. break;
  150. }
  151. return ret;
  152. }
  153. static const struct file_operations vfs_fops = {
  154. .owner = THIS_MODULE,
  155. .llseek = tz_vfs_llseek,
  156. .read = tz_vfs_read,
  157. .write = tz_vfs_write,
  158. .unlocked_ioctl = tz_vfs_ioctl,
  159. #ifdef CONFIG_COMPAT
  160. .compat_ioctl = tz_vfs_ioctl,
  161. #endif
  162. .open = tz_vfs_open,
  163. .release = tz_vfs_release,
  164. };
  165. static void vfs_setup_cdev(struct vfs_dev *dev, int index)
  166. {
  167. int err = 0;
  168. int devno = MKDEV(vfs_major, index);
  169. cdev_init(&dev->cdev, &vfs_fops);
  170. dev->cdev.owner = vfs_fops.owner;
  171. err = cdev_add(&dev->cdev, devno, 1);
  172. if (err)
  173. printk("Error %d adding socket %d.\n", err, index);
  174. }
  175. int vfs_init(void)
  176. {
  177. int result = 0;
  178. struct device *class_dev = NULL;
  179. devno = MKDEV(vfs_major, 0);
  180. result = alloc_chrdev_region(&devno, 0, 1, "tz_vfs");
  181. vfs_major = MAJOR(devno);
  182. if (result < 0)
  183. return result;
  184. driver_class = class_create(THIS_MODULE, "tz_vfs");
  185. if (IS_ERR(driver_class)) {
  186. result = -ENOMEM;
  187. printk("class_create failed %d.\n", result);
  188. goto unregister_chrdev_region;
  189. }
  190. class_dev = device_create(driver_class, NULL, devno, NULL, "tz_vfs");
  191. if (!class_dev) {
  192. result = -ENOMEM;
  193. printk("class_device_create failed %d.\n", result);
  194. goto class_destroy;
  195. }
  196. vfs_devp = kmalloc(sizeof(struct vfs_dev), GFP_KERNEL);
  197. if (vfs_devp == NULL) {
  198. result = -ENOMEM;
  199. goto class_device_destroy;
  200. }
  201. memset(vfs_devp, 0, sizeof(struct vfs_dev));
  202. vfs_setup_cdev(vfs_devp, 0);
  203. sema_init(&vfs_devp->sem, 1);
  204. #ifdef VFS_RDWR_SEM
  205. sema_init(&VFS_rd_sem, 0);
  206. sema_init(&VFS_wr_sem, 0);
  207. #endif
  208. goto return_fn;
  209. class_device_destroy:
  210. device_destroy(driver_class, devno);
  211. class_destroy:
  212. class_destroy(driver_class);
  213. unregister_chrdev_region:
  214. unregister_chrdev_region(devno, 1);
  215. return_fn:
  216. return result;
  217. }
  218. void vfs_exit(void)
  219. {
  220. device_destroy(driver_class, devno);
  221. class_destroy(driver_class);
  222. cdev_del(&vfs_devp->cdev);
  223. kfree(vfs_devp);
  224. unregister_chrdev_region(MKDEV(vfs_major, 0), 1);
  225. }
  226. MODULE_AUTHOR("Neusoft");
  227. MODULE_LICENSE("Dual BSD/GPL");
  228. module_param(vfs_major, int, S_IRUGO);
  229. module_init(vfs_init);
  230. module_exit(vfs_exit);