1
0

devfs.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-02-11 Bernard Ignore O_CREAT flag in open.
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <dfs.h>
  13. #include <dfs_fs.h>
  14. #include <dfs_file.h>
  15. #include "devfs.h"
  16. struct device_dirent
  17. {
  18. rt_device_t *devices;
  19. rt_uint16_t read_index;
  20. rt_uint16_t device_count;
  21. };
  22. int dfs_device_fs_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
  23. {
  24. return RT_EOK;
  25. }
  26. int dfs_device_fs_ioctl(struct dfs_fd *file, int cmd, void *args)
  27. {
  28. rt_err_t result;
  29. rt_device_t dev_id;
  30. RT_ASSERT(file != RT_NULL);
  31. /* get device handler */
  32. dev_id = (rt_device_t)file->data;
  33. RT_ASSERT(dev_id != RT_NULL);
  34. /* close device handler */
  35. result = rt_device_control(dev_id, cmd, args);
  36. if (result == RT_EOK)
  37. return RT_EOK;
  38. return result;
  39. }
  40. int dfs_device_fs_read(struct dfs_fd *file, void *buf, size_t count)
  41. {
  42. int result;
  43. rt_device_t dev_id;
  44. RT_ASSERT(file != RT_NULL);
  45. /* get device handler */
  46. dev_id = (rt_device_t)file->data;
  47. RT_ASSERT(dev_id != RT_NULL);
  48. /* read device data */
  49. result = rt_device_read(dev_id, file->pos, buf, count);
  50. file->pos += result;
  51. return result;
  52. }
  53. int dfs_device_fs_write(struct dfs_fd *file, const void *buf, size_t count)
  54. {
  55. int result;
  56. rt_device_t dev_id;
  57. RT_ASSERT(file != RT_NULL);
  58. /* get device handler */
  59. dev_id = (rt_device_t)file->data;
  60. RT_ASSERT(dev_id != RT_NULL);
  61. /* read device data */
  62. result = rt_device_write(dev_id, file->pos, buf, count);
  63. file->pos += result;
  64. return result;
  65. }
  66. int dfs_device_fs_close(struct dfs_fd *file)
  67. {
  68. rt_err_t result;
  69. rt_device_t dev_id;
  70. RT_ASSERT(file != RT_NULL);
  71. if (file->type == FT_DIRECTORY)
  72. {
  73. struct device_dirent *root_dirent;
  74. root_dirent = (struct device_dirent *)file->data;
  75. RT_ASSERT(root_dirent != RT_NULL);
  76. /* release dirent */
  77. rt_free(root_dirent);
  78. return RT_EOK;
  79. }
  80. /* get device handler */
  81. dev_id = (rt_device_t)file->data;
  82. RT_ASSERT(dev_id != RT_NULL);
  83. /* close device handler */
  84. result = rt_device_close(dev_id);
  85. if (result == RT_EOK)
  86. {
  87. file->data = RT_NULL;
  88. return RT_EOK;
  89. }
  90. return -EIO;
  91. }
  92. int dfs_device_fs_open(struct dfs_fd *file)
  93. {
  94. rt_err_t result;
  95. rt_device_t device;
  96. /* open root directory */
  97. if ((file->path[0] == '/') && (file->path[1] == '\0') &&
  98. (file->flags & O_DIRECTORY))
  99. {
  100. struct rt_object *object;
  101. struct rt_list_node *node;
  102. struct rt_object_information *information;
  103. struct device_dirent *root_dirent;
  104. rt_uint32_t count = 0;
  105. /* lock scheduler */
  106. rt_enter_critical();
  107. /* traverse device object */
  108. information = rt_object_get_information(RT_Object_Class_Device);
  109. RT_ASSERT(information != RT_NULL);
  110. for (node = information->object_list.next; node != &(information->object_list); node = node->next)
  111. {
  112. count ++;
  113. }
  114. root_dirent = (struct device_dirent *)rt_malloc(sizeof(struct device_dirent) +
  115. count * sizeof(rt_device_t));
  116. if (root_dirent != RT_NULL)
  117. {
  118. root_dirent->devices = (rt_device_t *)(root_dirent + 1);
  119. root_dirent->read_index = 0;
  120. root_dirent->device_count = count;
  121. count = 0;
  122. /* get all device node */
  123. for (node = information->object_list.next; node != &(information->object_list); node = node->next)
  124. {
  125. object = rt_list_entry(node, struct rt_object, list);
  126. root_dirent->devices[count] = (rt_device_t)object;
  127. count ++;
  128. }
  129. }
  130. rt_exit_critical();
  131. /* set data */
  132. file->data = root_dirent;
  133. return RT_EOK;
  134. }
  135. device = rt_device_find(&file->path[1]);
  136. if (device == RT_NULL)
  137. return -ENODEV;
  138. #ifdef RT_USING_POSIX
  139. if (device->fops)
  140. {
  141. /* use device fops */
  142. file->fops = device->fops;
  143. file->data = (void *)device;
  144. /* use fops */
  145. if (file->fops->open)
  146. {
  147. result = file->fops->open(file);
  148. if (result == RT_EOK || result == -RT_ENOSYS)
  149. {
  150. return 0;
  151. }
  152. }
  153. }
  154. else
  155. #endif
  156. {
  157. result = rt_device_open(device, RT_DEVICE_OFLAG_RDWR);
  158. if (result == RT_EOK || result == -RT_ENOSYS)
  159. {
  160. file->data = device;
  161. return RT_EOK;
  162. }
  163. }
  164. file->data = RT_NULL;
  165. /* open device failed. */
  166. return -EIO;
  167. }
  168. int dfs_device_fs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
  169. {
  170. /* stat root directory */
  171. if ((path[0] == '/') && (path[1] == '\0'))
  172. {
  173. st->st_dev = 0;
  174. st->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH |
  175. S_IWUSR | S_IWGRP | S_IWOTH;
  176. st->st_mode &= ~S_IFREG;
  177. st->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
  178. st->st_size = 0;
  179. st->st_mtime = 0;
  180. return RT_EOK;
  181. }
  182. else
  183. {
  184. rt_device_t dev_id;
  185. dev_id = rt_device_find(&path[1]);
  186. if (dev_id != RT_NULL)
  187. {
  188. st->st_dev = 0;
  189. st->st_mode = S_IRUSR | S_IRGRP | S_IROTH |
  190. S_IWUSR | S_IWGRP | S_IWOTH;
  191. if (dev_id->type == RT_Device_Class_Char)
  192. st->st_mode |= S_IFCHR;
  193. else if (dev_id->type == RT_Device_Class_Block)
  194. st->st_mode |= S_IFBLK;
  195. else if (dev_id->type == RT_Device_Class_Pipe)
  196. st->st_mode |= S_IFIFO;
  197. else
  198. st->st_mode |= S_IFREG;
  199. st->st_size = 0;
  200. st->st_mtime = 0;
  201. return RT_EOK;
  202. }
  203. }
  204. return -ENOENT;
  205. }
  206. int dfs_device_fs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t count)
  207. {
  208. rt_uint32_t index;
  209. rt_object_t object;
  210. struct dirent *d;
  211. struct device_dirent *root_dirent;
  212. root_dirent = (struct device_dirent *)file->data;
  213. RT_ASSERT(root_dirent != RT_NULL);
  214. /* make integer count */
  215. count = (count / sizeof(struct dirent));
  216. if (count == 0)
  217. return -EINVAL;
  218. for (index = 0; index < count && index + root_dirent->read_index < root_dirent->device_count;
  219. index ++)
  220. {
  221. object = (rt_object_t)root_dirent->devices[root_dirent->read_index + index];
  222. d = dirp + index;
  223. d->d_type = DT_REG;
  224. d->d_namlen = RT_NAME_MAX;
  225. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  226. rt_strncpy(d->d_name, object->name, RT_NAME_MAX);
  227. }
  228. root_dirent->read_index += index;
  229. return index * sizeof(struct dirent);
  230. }
  231. static int dfs_device_fs_poll(struct dfs_fd *fd, struct rt_pollreq *req)
  232. {
  233. int mask = 0;
  234. return mask;
  235. }
  236. static const struct dfs_file_ops _device_fops =
  237. {
  238. dfs_device_fs_open,
  239. dfs_device_fs_close,
  240. dfs_device_fs_ioctl,
  241. dfs_device_fs_read,
  242. dfs_device_fs_write,
  243. RT_NULL, /* flush */
  244. RT_NULL, /* lseek */
  245. dfs_device_fs_getdents,
  246. dfs_device_fs_poll,
  247. };
  248. static const struct dfs_filesystem_ops _device_fs =
  249. {
  250. "devfs",
  251. DFS_FS_FLAG_DEFAULT,
  252. &_device_fops,
  253. dfs_device_fs_mount,
  254. RT_NULL,
  255. RT_NULL,
  256. RT_NULL,
  257. RT_NULL,
  258. dfs_device_fs_stat,
  259. RT_NULL,
  260. };
  261. int devfs_init(void)
  262. {
  263. /* register rom file system */
  264. dfs_register(&_device_fs);
  265. return 0;
  266. }