devfs.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. */
  23. #include <rtthread.h>
  24. #include <dfs.h>
  25. #include <dfs_fs.h>
  26. #include "devfs.h"
  27. struct device_dirent
  28. {
  29. rt_device_t *devices;
  30. rt_uint16_t read_index;
  31. rt_uint16_t device_count;
  32. };
  33. int dfs_device_fs_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
  34. {
  35. return DFS_STATUS_OK;
  36. }
  37. int dfs_device_fs_ioctl(struct dfs_fd *file, int cmd, void *args)
  38. {
  39. rt_err_t result;
  40. rt_device_t dev_id;
  41. RT_ASSERT(file != RT_NULL);
  42. /* get device handler */
  43. dev_id = (rt_device_t)file->data;
  44. RT_ASSERT(dev_id != RT_NULL);
  45. /* close device handler */
  46. result = rt_device_control(dev_id, cmd, args);
  47. if (result == RT_EOK)
  48. return DFS_STATUS_OK;
  49. return -DFS_STATUS_EIO;
  50. }
  51. int dfs_device_fs_read(struct dfs_fd *file, void *buf, rt_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->data;
  58. RT_ASSERT(dev_id != RT_NULL);
  59. /* read device data */
  60. result = rt_device_read(dev_id, file->pos, buf, count);
  61. file->pos += result;
  62. return result;
  63. }
  64. int dfs_device_fs_write(struct dfs_fd *file, const void *buf, rt_size_t count)
  65. {
  66. int result;
  67. rt_device_t dev_id;
  68. RT_ASSERT(file != RT_NULL);
  69. /* get device handler */
  70. dev_id = (rt_device_t)file->data;
  71. RT_ASSERT(dev_id != RT_NULL);
  72. /* read device data */
  73. result = rt_device_write(dev_id, file->pos, buf, count);
  74. file->pos += result;
  75. return result;
  76. }
  77. int dfs_device_fs_close(struct dfs_fd *file)
  78. {
  79. rt_err_t result;
  80. rt_device_t dev_id;
  81. RT_ASSERT(file != RT_NULL);
  82. if (file->type == FT_DIRECTORY)
  83. {
  84. struct device_dirent *root_dirent;
  85. root_dirent = (struct device_dirent *)file->data;
  86. RT_ASSERT(root_dirent != RT_NULL);
  87. /* release dirent */
  88. rt_free(root_dirent);
  89. return DFS_STATUS_OK;
  90. }
  91. /* get device handler */
  92. dev_id = (rt_device_t)file->data;
  93. RT_ASSERT(dev_id != RT_NULL);
  94. /* close device handler */
  95. result = rt_device_close(dev_id);
  96. if (result == RT_EOK)
  97. {
  98. file->data = RT_NULL;
  99. return DFS_STATUS_OK;
  100. }
  101. return -DFS_STATUS_EIO;
  102. }
  103. int dfs_device_fs_open(struct dfs_fd *file)
  104. {
  105. rt_device_t device;
  106. if (file->flags & DFS_O_CREAT)
  107. return -DFS_STATUS_EINVAL;
  108. /* open root directory */
  109. if ((file->path[0] == '/') && (file->path[1] == '\0') &&
  110. (file->flags & DFS_O_DIRECTORY))
  111. {
  112. struct rt_object *object;
  113. struct rt_list_node *node;
  114. struct rt_object_information *information;
  115. struct device_dirent *root_dirent;
  116. rt_uint32_t count = 0;
  117. extern struct rt_object_information rt_object_container[];
  118. /* lock scheduler */
  119. rt_enter_critical();
  120. /* traverse device object */
  121. information = &rt_object_container[RT_Object_Class_Device];
  122. for (node = information->object_list.next; node != &(information->object_list); node = node->next)
  123. {
  124. count ++;
  125. }
  126. root_dirent = (struct device_dirent *)rt_malloc(sizeof(struct device_dirent) +
  127. count * sizeof(rt_device_t));
  128. if (root_dirent != RT_NULL)
  129. {
  130. root_dirent->devices = (rt_device_t *)(root_dirent + 1);
  131. root_dirent->read_index = 0;
  132. root_dirent->device_count = count;
  133. count = 0;
  134. /* get all device node */
  135. for (node = information->object_list.next; node != &(information->object_list); node = node->next)
  136. {
  137. object = rt_list_entry(node, struct rt_object, list);
  138. root_dirent->devices[count] = (rt_device_t)object;
  139. count ++;
  140. }
  141. }
  142. rt_exit_critical();
  143. /* set data */
  144. file->data = root_dirent;
  145. return DFS_STATUS_OK;
  146. }
  147. device = rt_device_find(&file->path[1]);
  148. if (device == RT_NULL)
  149. return -DFS_STATUS_ENODEV;
  150. file->data = device;
  151. return DFS_STATUS_OK;
  152. }
  153. int dfs_device_fs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
  154. {
  155. /* stat root directory */
  156. if ((path[0] == '/') && (path[1] == '\0'))
  157. {
  158. st->st_dev = 0;
  159. st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
  160. DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
  161. st->st_mode &= ~DFS_S_IFREG;
  162. st->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
  163. st->st_size = 0;
  164. st->st_mtime = 0;
  165. st->st_blksize = 512;
  166. return DFS_STATUS_OK;
  167. }
  168. else
  169. {
  170. rt_device_t dev_id;
  171. dev_id = rt_device_find(&path[1]);
  172. if (dev_id != RT_NULL)
  173. {
  174. st->st_dev = 0;
  175. st->st_mode = DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
  176. DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
  177. if (dev_id->type == RT_Device_Class_Char)
  178. st->st_mode |= DFS_S_IFCHR;
  179. else if (dev_id->type == RT_Device_Class_Block)
  180. st->st_mode |= DFS_S_IFBLK;
  181. else
  182. st->st_mode |= DFS_S_IFREG;
  183. st->st_size = 0;
  184. st->st_mtime = 0;
  185. st->st_blksize = 512;
  186. return DFS_STATUS_OK;
  187. }
  188. }
  189. return -DFS_STATUS_ENOENT;
  190. }
  191. int dfs_device_fs_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t count)
  192. {
  193. rt_uint32_t index;
  194. rt_object_t object;
  195. struct dirent *d;
  196. struct device_dirent *root_dirent;
  197. root_dirent = (struct device_dirent *)file->data;
  198. RT_ASSERT(root_dirent != RT_NULL);
  199. /* make integer count */
  200. count = (count / sizeof(struct dirent));
  201. if (count == 0)
  202. return -DFS_STATUS_EINVAL;
  203. for (index = 0; index < count && index + root_dirent->read_index < root_dirent->device_count;
  204. index ++)
  205. {
  206. object = (rt_object_t)root_dirent->devices[root_dirent->read_index + index];
  207. d = dirp + index;
  208. d->d_type = DFS_DT_REG;
  209. d->d_namlen = RT_NAME_MAX;
  210. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  211. rt_strncpy(d->d_name, object->name, RT_NAME_MAX);
  212. }
  213. root_dirent->read_index += index;
  214. return index * sizeof(struct dirent);
  215. }
  216. static const struct dfs_filesystem_operation _device_fs =
  217. {
  218. "devfs",
  219. DFS_FS_FLAG_DEFAULT,
  220. dfs_device_fs_mount,
  221. RT_NULL,
  222. RT_NULL,
  223. RT_NULL,
  224. dfs_device_fs_open,
  225. dfs_device_fs_close,
  226. dfs_device_fs_ioctl,
  227. dfs_device_fs_read,
  228. dfs_device_fs_write,
  229. RT_NULL,
  230. RT_NULL,
  231. dfs_device_fs_getdents,
  232. RT_NULL,
  233. dfs_device_fs_stat,
  234. RT_NULL,
  235. };
  236. int devfs_init(void)
  237. {
  238. /* register rom file system */
  239. dfs_register(&_device_fs);
  240. return 0;
  241. }
  242. INIT_FS_EXPORT(devfs_init);