devfs.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #include <rtthread.h>
  2. #include <dfs.h>
  3. #include <dfs_fs.h>
  4. #include "devfs.h"
  5. struct device_dirent
  6. {
  7. rt_device_t *devices;
  8. rt_uint16_t read_index;
  9. rt_uint16_t device_count;
  10. };
  11. int dfs_device_fs_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
  12. {
  13. return DFS_STATUS_OK;
  14. }
  15. int dfs_device_fs_ioctl(struct dfs_fd *file, int cmd, void *args)
  16. {
  17. rt_err_t result;
  18. rt_device_t dev_id;
  19. RT_ASSERT(file != RT_NULL);
  20. /* get device handler */
  21. dev_id = (rt_device_t)file->data;
  22. RT_ASSERT(dev_id != RT_NULL);
  23. /* close device handler */
  24. result = rt_device_control(dev_id, cmd, args);
  25. if (result == RT_EOK)
  26. return DFS_STATUS_OK;
  27. return -DFS_STATUS_EIO;
  28. }
  29. int dfs_device_fs_read(struct dfs_fd *file, void *buf, rt_size_t count)
  30. {
  31. int result;
  32. rt_device_t dev_id;
  33. RT_ASSERT(file != RT_NULL);
  34. /* get device handler */
  35. dev_id = (rt_device_t)file->data;
  36. RT_ASSERT(dev_id != RT_NULL);
  37. /* read device data */
  38. result = rt_device_read(dev_id, file->pos, buf, count);
  39. file->pos += result;
  40. return result;
  41. }
  42. int dfs_device_fs_write(struct dfs_fd *file, const void *buf, rt_size_t count)
  43. {
  44. int result;
  45. rt_device_t dev_id;
  46. RT_ASSERT(file != RT_NULL);
  47. /* get device handler */
  48. dev_id = (rt_device_t)file->data;
  49. RT_ASSERT(dev_id != RT_NULL);
  50. /* read device data */
  51. result = rt_device_write(dev_id, file->pos, buf, count);
  52. file->pos += result;
  53. return result;
  54. }
  55. int dfs_device_fs_close(struct dfs_fd *file)
  56. {
  57. rt_err_t result;
  58. rt_device_t dev_id;
  59. RT_ASSERT(file != RT_NULL);
  60. if (file->type == FT_DIRECTORY)
  61. {
  62. struct device_dirent *root_dirent;
  63. root_dirent = (struct device_dirent *)file->data;
  64. RT_ASSERT(root_dirent != RT_NULL);
  65. /* release dirent */
  66. rt_free(root_dirent);
  67. return DFS_STATUS_OK;
  68. }
  69. /* get device handler */
  70. dev_id = (rt_device_t)file->data;
  71. RT_ASSERT(dev_id != RT_NULL);
  72. /* close device handler */
  73. result = rt_device_close(dev_id);
  74. if (result == RT_EOK)
  75. {
  76. file->data = RT_NULL;
  77. return DFS_STATUS_OK;
  78. }
  79. return -DFS_STATUS_EIO;
  80. }
  81. int dfs_device_fs_open(struct dfs_fd *file)
  82. {
  83. rt_device_t device;
  84. if (file->flags & DFS_O_CREAT)
  85. return -DFS_STATUS_EINVAL;
  86. /* open root directory */
  87. if ((file->path[0] == '/') && (file->path[1] == '\0') &&
  88. (file->flags & DFS_O_DIRECTORY))
  89. {
  90. struct rt_object *object;
  91. struct rt_list_node *node;
  92. struct rt_object_information *information;
  93. struct device_dirent *root_dirent;
  94. rt_uint32_t count = 0;
  95. extern struct rt_object_information rt_object_container[];
  96. /* lock scheduler */
  97. rt_enter_critical();
  98. /* traverse device object */
  99. information = &rt_object_container[RT_Object_Class_Device];
  100. for (node = information->object_list.next; node != &(information->object_list); node = node->next)
  101. {
  102. count ++;
  103. }
  104. root_dirent = (struct device_dirent *)rt_malloc(sizeof(struct device_dirent) +
  105. count * sizeof(rt_device_t));
  106. if (root_dirent != RT_NULL)
  107. {
  108. root_dirent->devices = (rt_device_t *)(root_dirent + 1);
  109. root_dirent->read_index = 0;
  110. root_dirent->device_count = count;
  111. count = 0;
  112. /* get all device node */
  113. for (node = information->object_list.next; node != &(information->object_list); node = node->next)
  114. {
  115. object = rt_list_entry(node, struct rt_object, list);
  116. root_dirent->devices[count] = (rt_device_t)object;
  117. count ++;
  118. }
  119. }
  120. rt_exit_critical();
  121. /* set data */
  122. file->data = root_dirent;
  123. return DFS_STATUS_OK;
  124. }
  125. device = rt_device_find(&file->path[1]);
  126. if (device == RT_NULL)
  127. return -DFS_STATUS_ENODEV;
  128. file->data = device;
  129. return DFS_STATUS_OK;
  130. }
  131. int dfs_device_fs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
  132. {
  133. /* stat root directory */
  134. if ((path[0] == '/') && (path[1] == '\0'))
  135. {
  136. st->st_dev = 0;
  137. st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
  138. DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
  139. st->st_mode &= ~DFS_S_IFREG;
  140. st->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
  141. st->st_size = 0;
  142. st->st_mtime = 0;
  143. st->st_blksize = 512;
  144. return DFS_STATUS_OK;
  145. }
  146. else
  147. {
  148. rt_device_t dev_id;
  149. dev_id = rt_device_find(&path[1]);
  150. if (dev_id != RT_NULL)
  151. {
  152. st->st_dev = 0;
  153. st->st_mode = DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
  154. DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
  155. if (dev_id->type == RT_Device_Class_Char)
  156. st->st_mode |= DFS_S_IFCHR;
  157. else if (dev_id->type == RT_Device_Class_Block)
  158. st->st_mode |= DFS_S_IFBLK;
  159. else
  160. st->st_mode |= DFS_S_IFREG;
  161. st->st_size = 0;
  162. st->st_mtime = 0;
  163. st->st_blksize = 512;
  164. return DFS_STATUS_OK;
  165. }
  166. }
  167. return -DFS_STATUS_ENOENT;
  168. }
  169. int dfs_device_fs_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t count)
  170. {
  171. rt_uint32_t index;
  172. rt_object_t object;
  173. struct dirent *d;
  174. struct device_dirent *root_dirent;
  175. root_dirent = (struct device_dirent *)file->data;
  176. RT_ASSERT(root_dirent != RT_NULL);
  177. /* make integer count */
  178. count = (count / sizeof(struct dirent));
  179. if (count == 0)
  180. return -DFS_STATUS_EINVAL;
  181. for (index = 0; index < count && index + root_dirent->read_index < root_dirent->device_count;
  182. index ++)
  183. {
  184. object = (rt_object_t)root_dirent->devices[root_dirent->read_index + index];
  185. d = dirp + index;
  186. d->d_type = DFS_DT_REG;
  187. d->d_namlen = RT_NAME_MAX;
  188. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  189. rt_strncpy(d->d_name, object->name, RT_NAME_MAX);
  190. }
  191. root_dirent->read_index += index;
  192. return index * sizeof(struct dirent);
  193. }
  194. static const struct dfs_filesystem_operation _device_fs =
  195. {
  196. "devfs",
  197. DFS_FS_FLAG_DEFAULT,
  198. dfs_device_fs_mount,
  199. RT_NULL,
  200. RT_NULL,
  201. RT_NULL,
  202. dfs_device_fs_open,
  203. dfs_device_fs_close,
  204. dfs_device_fs_ioctl,
  205. dfs_device_fs_read,
  206. dfs_device_fs_write,
  207. RT_NULL,
  208. RT_NULL,
  209. dfs_device_fs_getdents,
  210. RT_NULL,
  211. dfs_device_fs_stat,
  212. RT_NULL,
  213. };
  214. int devfs_init(void)
  215. {
  216. /* register rom file system */
  217. dfs_register(&_device_fs);
  218. return 0;
  219. }