1
0

devfs.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Copyright (c) 2006-2021, 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. rt_exit_critical();
  115. root_dirent = (struct device_dirent *)rt_malloc(sizeof(struct device_dirent) +
  116. count * sizeof(rt_device_t));
  117. if (root_dirent != RT_NULL)
  118. {
  119. /* lock scheduler */
  120. rt_enter_critical();
  121. root_dirent->devices = (rt_device_t *)(root_dirent + 1);
  122. root_dirent->read_index = 0;
  123. root_dirent->device_count = count;
  124. count = 0;
  125. /* get all device node */
  126. for (node = information->object_list.next; node != &(information->object_list); node = node->next)
  127. {
  128. /* avoid memory write through */
  129. if (count == root_dirent->device_count)
  130. {
  131. rt_kprintf("warning: There are newly added devices that are not displayed!");
  132. break;
  133. }
  134. object = rt_list_entry(node, struct rt_object, list);
  135. root_dirent->devices[count] = (rt_device_t)object;
  136. count ++;
  137. }
  138. rt_exit_critical();
  139. }
  140. /* set data */
  141. file->data = root_dirent;
  142. return RT_EOK;
  143. }
  144. device = rt_device_find(&file->path[1]);
  145. if (device == RT_NULL)
  146. return -ENODEV;
  147. #ifdef RT_USING_POSIX_DEVIO
  148. if (device->fops)
  149. {
  150. /* use device fops */
  151. file->fops = device->fops;
  152. file->data = (void *)device;
  153. /* use fops */
  154. if (file->fops->open)
  155. {
  156. result = file->fops->open(file);
  157. if (result == RT_EOK || result == -RT_ENOSYS)
  158. {
  159. file->type = FT_DEVICE;
  160. return 0;
  161. }
  162. }
  163. }
  164. else
  165. #endif /* RT_USING_POSIX_DEVIO */
  166. {
  167. result = rt_device_open(device, RT_DEVICE_OFLAG_RDWR);
  168. if (result == RT_EOK || result == -RT_ENOSYS)
  169. {
  170. file->data = device;
  171. file->type = FT_DEVICE;
  172. return RT_EOK;
  173. }
  174. }
  175. file->data = RT_NULL;
  176. /* open device failed. */
  177. return -EIO;
  178. }
  179. int dfs_device_fs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
  180. {
  181. /* stat root directory */
  182. if ((path[0] == '/') && (path[1] == '\0'))
  183. {
  184. st->st_dev = 0;
  185. st->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH |
  186. S_IWUSR | S_IWGRP | S_IWOTH;
  187. st->st_mode &= ~S_IFREG;
  188. st->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
  189. st->st_size = 0;
  190. st->st_mtime = 0;
  191. return RT_EOK;
  192. }
  193. else
  194. {
  195. rt_device_t dev_id;
  196. dev_id = rt_device_find(&path[1]);
  197. if (dev_id != RT_NULL)
  198. {
  199. st->st_dev = 0;
  200. st->st_mode = S_IRUSR | S_IRGRP | S_IROTH |
  201. S_IWUSR | S_IWGRP | S_IWOTH;
  202. if (dev_id->type == RT_Device_Class_Char)
  203. st->st_mode |= S_IFCHR;
  204. else if (dev_id->type == RT_Device_Class_Block)
  205. st->st_mode |= S_IFBLK;
  206. else if (dev_id->type == RT_Device_Class_Pipe)
  207. st->st_mode |= S_IFIFO;
  208. else
  209. st->st_mode |= S_IFREG;
  210. st->st_size = 0;
  211. st->st_mtime = 0;
  212. return RT_EOK;
  213. }
  214. }
  215. return -ENOENT;
  216. }
  217. int dfs_device_fs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t count)
  218. {
  219. rt_uint32_t index;
  220. rt_object_t object;
  221. struct dirent *d;
  222. struct device_dirent *root_dirent;
  223. root_dirent = (struct device_dirent *)file->data;
  224. RT_ASSERT(root_dirent != RT_NULL);
  225. /* make integer count */
  226. count = (count / sizeof(struct dirent));
  227. if (count == 0)
  228. return -EINVAL;
  229. for (index = 0; index < count && index + root_dirent->read_index < root_dirent->device_count;
  230. index ++)
  231. {
  232. object = (rt_object_t)root_dirent->devices[root_dirent->read_index + index];
  233. d = dirp + index;
  234. d->d_type = DT_REG;
  235. d->d_namlen = RT_NAME_MAX;
  236. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  237. rt_strncpy(d->d_name, object->name, RT_NAME_MAX);
  238. }
  239. root_dirent->read_index += index;
  240. return index * sizeof(struct dirent);
  241. }
  242. static int dfs_device_fs_poll(struct dfs_fd *fd, struct rt_pollreq *req)
  243. {
  244. int mask = 0;
  245. return mask;
  246. }
  247. static const struct dfs_file_ops _device_fops =
  248. {
  249. dfs_device_fs_open,
  250. dfs_device_fs_close,
  251. dfs_device_fs_ioctl,
  252. dfs_device_fs_read,
  253. dfs_device_fs_write,
  254. RT_NULL, /* flush */
  255. RT_NULL, /* lseek */
  256. dfs_device_fs_getdents,
  257. dfs_device_fs_poll,
  258. };
  259. static const struct dfs_filesystem_ops _device_fs =
  260. {
  261. "devfs",
  262. DFS_FS_FLAG_DEFAULT,
  263. &_device_fops,
  264. dfs_device_fs_mount,
  265. RT_NULL, /*unmount*/
  266. RT_NULL, /*mkfs*/
  267. RT_NULL, /*statfs*/
  268. RT_NULL, /*unlink*/
  269. dfs_device_fs_stat,
  270. RT_NULL, /*rename*/
  271. };
  272. int devfs_init(void)
  273. {
  274. /* register device file system */
  275. dfs_register(&_device_fs);
  276. return 0;
  277. }