devfs.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 <rthw.h>
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include <dfs.h>
  14. #include <dfs_fs.h>
  15. #include <dfs_file.h>
  16. #include "devfs.h"
  17. struct device_dirent
  18. {
  19. rt_device_t *devices;
  20. rt_uint16_t read_index;
  21. rt_uint16_t device_count;
  22. };
  23. int dfs_device_fs_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
  24. {
  25. return RT_EOK;
  26. }
  27. int dfs_device_fs_ioctl(struct dfs_fd *file, int cmd, void *args)
  28. {
  29. rt_err_t result;
  30. rt_device_t dev_id;
  31. RT_ASSERT(file != RT_NULL);
  32. /* get device handler */
  33. dev_id = (rt_device_t)file->vnode->data;
  34. RT_ASSERT(dev_id != RT_NULL);
  35. if ((file->vnode->path[0] == '/') && (file->vnode->path[1] == '\0'))
  36. return -RT_ENOSYS;
  37. /* close device handler */
  38. result = rt_device_control(dev_id, cmd, args);
  39. if (result == RT_EOK)
  40. return RT_EOK;
  41. return result;
  42. }
  43. int dfs_device_fs_read(struct dfs_fd *file, void *buf, size_t count)
  44. {
  45. int result;
  46. rt_device_t dev_id;
  47. RT_ASSERT(file != RT_NULL);
  48. /* get device handler */
  49. dev_id = (rt_device_t)file->vnode->data;
  50. RT_ASSERT(dev_id != RT_NULL);
  51. if ((file->vnode->path[0] == '/') && (file->vnode->path[1] == '\0'))
  52. return -RT_ENOSYS;
  53. /* read device data */
  54. result = rt_device_read(dev_id, file->pos, buf, count);
  55. file->pos += result;
  56. return result;
  57. }
  58. int dfs_device_fs_write(struct dfs_fd *file, const void *buf, size_t count)
  59. {
  60. int result;
  61. rt_device_t dev_id;
  62. RT_ASSERT(file != RT_NULL);
  63. /* get device handler */
  64. dev_id = (rt_device_t)file->vnode->data;
  65. RT_ASSERT(dev_id != RT_NULL);
  66. if ((file->vnode->path[0] == '/') && (file->vnode->path[1] == '\0'))
  67. return -RT_ENOSYS;
  68. /* read device data */
  69. result = rt_device_write(dev_id, file->pos, buf, count);
  70. file->pos += result;
  71. return result;
  72. }
  73. int dfs_device_fs_close(struct dfs_fd *file)
  74. {
  75. rt_err_t result;
  76. rt_device_t dev_id;
  77. RT_ASSERT(file != RT_NULL);
  78. RT_ASSERT(file->vnode->ref_count > 0);
  79. if (file->vnode->ref_count > 1)
  80. {
  81. return 0;
  82. }
  83. if (file->vnode->type == FT_DIRECTORY && (file->vnode->path[0] == '/') && (file->vnode->path[1] == '\0'))
  84. {
  85. struct device_dirent *root_dirent;
  86. root_dirent = (struct device_dirent *)file->vnode->data;
  87. RT_ASSERT(root_dirent != RT_NULL);
  88. /* release dirent */
  89. rt_free(root_dirent);
  90. return RT_EOK;
  91. }
  92. /* get device handler */
  93. dev_id = (rt_device_t)file->vnode->data;
  94. RT_ASSERT(dev_id != RT_NULL);
  95. /* close device handler */
  96. result = rt_device_close(dev_id);
  97. if (result == RT_EOK)
  98. {
  99. file->vnode->data = RT_NULL;
  100. return RT_EOK;
  101. }
  102. return -EIO;
  103. }
  104. int dfs_device_fs_open(struct dfs_fd *file)
  105. {
  106. rt_err_t result;
  107. rt_device_t device;
  108. RT_ASSERT(file->vnode->ref_count > 0);
  109. if (file->vnode->ref_count > 1)
  110. {
  111. file->pos = 0;
  112. return 0;
  113. }
  114. /* open root directory */
  115. if ((file->vnode->path[0] == '/') && (file->vnode->path[1] == '\0') &&
  116. (file->flags & O_DIRECTORY))
  117. {
  118. struct rt_object *object;
  119. struct rt_list_node *node;
  120. struct rt_object_information *information;
  121. struct device_dirent *root_dirent;
  122. rt_uint32_t count = 0;
  123. /* lock scheduler */
  124. rt_enter_critical();
  125. /* traverse device object */
  126. information = rt_object_get_information(RT_Object_Class_Device);
  127. RT_ASSERT(information != RT_NULL);
  128. for (node = information->object_list.next; node != &(information->object_list); node = node->next)
  129. {
  130. count ++;
  131. }
  132. rt_exit_critical();
  133. root_dirent = (struct device_dirent *)rt_malloc(sizeof(struct device_dirent) +
  134. count * sizeof(rt_device_t));
  135. if (root_dirent != RT_NULL)
  136. {
  137. /* lock scheduler */
  138. rt_enter_critical();
  139. root_dirent->devices = (rt_device_t *)(root_dirent + 1);
  140. root_dirent->read_index = 0;
  141. root_dirent->device_count = count;
  142. count = 0;
  143. /* get all device node */
  144. for (node = information->object_list.next; node != &(information->object_list); node = node->next)
  145. {
  146. /* avoid memory write through */
  147. if (count == root_dirent->device_count)
  148. {
  149. rt_kprintf("warning: There are newly added devices that are not displayed!");
  150. break;
  151. }
  152. object = rt_list_entry(node, struct rt_object, list);
  153. root_dirent->devices[count] = (rt_device_t)object;
  154. count ++;
  155. }
  156. rt_exit_critical();
  157. }
  158. /* set data */
  159. file->vnode->data = root_dirent;
  160. return RT_EOK;
  161. }
  162. #ifdef RT_USING_DEV_BUS
  163. else if (file->flags & O_CREAT)
  164. {
  165. if (!(file->flags & O_DIRECTORY))
  166. {
  167. return -ENOSYS;
  168. }
  169. /* regester bus device */
  170. if (rt_device_bus_create(&file->vnode->path[1], 0) == RT_NULL)
  171. {
  172. return -EEXIST;
  173. }
  174. }
  175. #endif
  176. device = rt_device_find(&file->vnode->path[1]);
  177. if (device == RT_NULL)
  178. {
  179. return -ENODEV;
  180. }
  181. #ifdef RT_USING_POSIX_DEVIO
  182. if (device->fops)
  183. {
  184. /* use device fops */
  185. file->vnode->fops = device->fops;
  186. file->vnode->data = (void *)device;
  187. /* use fops */
  188. if (file->vnode->fops->open)
  189. {
  190. result = file->vnode->fops->open(file);
  191. if (result == RT_EOK || result == -RT_ENOSYS)
  192. {
  193. file->vnode->type = FT_DEVICE;
  194. return 0;
  195. }
  196. }
  197. }
  198. else
  199. #endif /* RT_USING_POSIX_DEVIO */
  200. {
  201. result = rt_device_open(device, RT_DEVICE_OFLAG_RDWR);
  202. if (result == RT_EOK || result == -RT_ENOSYS)
  203. {
  204. file->vnode->data = device;
  205. file->vnode->type = FT_DEVICE;
  206. return RT_EOK;
  207. }
  208. }
  209. file->vnode->data = RT_NULL;
  210. /* open device failed. */
  211. return -EIO;
  212. }
  213. int dfs_device_fs_unlink(struct dfs_filesystem *fs, const char *path)
  214. {
  215. #ifdef RT_USING_DEV_BUS
  216. rt_device_t dev_id;
  217. dev_id = rt_device_find(&path[1]);
  218. if (dev_id == RT_NULL)
  219. {
  220. return -1;
  221. }
  222. if (dev_id->type != RT_Device_Class_Bus)
  223. {
  224. return -1;
  225. }
  226. rt_device_bus_destroy(dev_id);
  227. #endif
  228. return RT_EOK;
  229. }
  230. int dfs_device_fs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
  231. {
  232. /* stat root directory */
  233. if ((path[0] == '/') && (path[1] == '\0'))
  234. {
  235. st->st_dev = 0;
  236. st->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH |
  237. S_IWUSR | S_IWGRP | S_IWOTH;
  238. st->st_mode &= ~S_IFREG;
  239. st->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
  240. st->st_size = 0;
  241. st->st_mtime = 0;
  242. return RT_EOK;
  243. }
  244. else
  245. {
  246. rt_device_t dev_id;
  247. dev_id = rt_device_find(&path[1]);
  248. if (dev_id != RT_NULL)
  249. {
  250. st->st_dev = 0;
  251. st->st_mode = S_IRUSR | S_IRGRP | S_IROTH |
  252. S_IWUSR | S_IWGRP | S_IWOTH;
  253. if (dev_id->type == RT_Device_Class_Char)
  254. st->st_mode |= S_IFCHR;
  255. else if (dev_id->type == RT_Device_Class_Block)
  256. st->st_mode |= S_IFBLK;
  257. else if (dev_id->type == RT_Device_Class_Pipe)
  258. st->st_mode |= S_IFIFO;
  259. else if (dev_id->type == RT_Device_Class_Bus)
  260. st->st_mode |= S_IFDIR;
  261. else
  262. st->st_mode |= S_IFREG;
  263. st->st_size = 0;
  264. st->st_mtime = 0;
  265. return RT_EOK;
  266. }
  267. }
  268. return -ENOENT;
  269. }
  270. int dfs_device_fs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t count)
  271. {
  272. rt_uint32_t index;
  273. rt_object_t object;
  274. struct dirent *d;
  275. struct device_dirent *root_dirent;
  276. root_dirent = (struct device_dirent *)file->vnode->data;
  277. RT_ASSERT(root_dirent != RT_NULL);
  278. /* make integer count */
  279. count = (count / sizeof(struct dirent));
  280. if (count == 0)
  281. return -EINVAL;
  282. for (index = 0; index < count && index + root_dirent->read_index < root_dirent->device_count;
  283. index ++)
  284. {
  285. object = (rt_object_t)root_dirent->devices[root_dirent->read_index + index];
  286. d = dirp + index;
  287. if ((((rt_device_t)object)->type) == RT_Device_Class_Bus)
  288. {
  289. d->d_type = DT_DIR;
  290. }
  291. else
  292. {
  293. d->d_type = DT_REG;
  294. }
  295. d->d_namlen = RT_NAME_MAX;
  296. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  297. rt_strncpy(d->d_name, object->name, RT_NAME_MAX);
  298. }
  299. root_dirent->read_index += index;
  300. return index * sizeof(struct dirent);
  301. }
  302. static int dfs_device_fs_poll(struct dfs_fd *fd, struct rt_pollreq *req)
  303. {
  304. int mask = 0;
  305. return mask;
  306. }
  307. static const struct dfs_file_ops _device_fops =
  308. {
  309. dfs_device_fs_open,
  310. dfs_device_fs_close,
  311. dfs_device_fs_ioctl,
  312. dfs_device_fs_read,
  313. dfs_device_fs_write,
  314. RT_NULL, /* flush */
  315. RT_NULL, /* lseek */
  316. dfs_device_fs_getdents,
  317. dfs_device_fs_poll,
  318. };
  319. static const struct dfs_filesystem_ops _device_fs =
  320. {
  321. "devfs",
  322. DFS_FS_FLAG_DEFAULT,
  323. &_device_fops,
  324. dfs_device_fs_mount,
  325. RT_NULL, /*unmount*/
  326. RT_NULL, /*mkfs*/
  327. RT_NULL, /*statfs*/
  328. dfs_device_fs_unlink,
  329. dfs_device_fs_stat,
  330. RT_NULL, /*rename*/
  331. };
  332. int devfs_init(void)
  333. {
  334. /* register device file system */
  335. dfs_register(&_device_fs);
  336. return 0;
  337. }