devfs.c 8.3 KB

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