devfs.c 10 KB

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