devfs.c 5.9 KB

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