devfs.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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_err_t result;
  106. rt_device_t device;
  107. if (file->flags & DFS_O_CREAT)
  108. return -DFS_STATUS_EINVAL;
  109. /* open root directory */
  110. if ((file->path[0] == '/') && (file->path[1] == '\0') &&
  111. (file->flags & DFS_O_DIRECTORY))
  112. {
  113. struct rt_object *object;
  114. struct rt_list_node *node;
  115. struct rt_object_information *information;
  116. struct device_dirent *root_dirent;
  117. rt_uint32_t count = 0;
  118. extern struct rt_object_information rt_object_container[];
  119. /* lock scheduler */
  120. rt_enter_critical();
  121. /* traverse device object */
  122. information = &rt_object_container[RT_Object_Class_Device];
  123. for (node = information->object_list.next; node != &(information->object_list); node = node->next)
  124. {
  125. count ++;
  126. }
  127. root_dirent = (struct device_dirent *)rt_malloc(sizeof(struct device_dirent) +
  128. count * sizeof(rt_device_t));
  129. if (root_dirent != RT_NULL)
  130. {
  131. root_dirent->devices = (rt_device_t *)(root_dirent + 1);
  132. root_dirent->read_index = 0;
  133. root_dirent->device_count = count;
  134. count = 0;
  135. /* get all device node */
  136. for (node = information->object_list.next; node != &(information->object_list); node = node->next)
  137. {
  138. object = rt_list_entry(node, struct rt_object, list);
  139. root_dirent->devices[count] = (rt_device_t)object;
  140. count ++;
  141. }
  142. }
  143. rt_exit_critical();
  144. /* set data */
  145. file->data = root_dirent;
  146. return DFS_STATUS_OK;
  147. }
  148. device = rt_device_find(&file->path[1]);
  149. if (device == RT_NULL)
  150. return -DFS_STATUS_ENODEV;
  151. /* to open device */
  152. result = rt_device_open(device, RT_DEVICE_OFLAG_RDWR);
  153. if (result == RT_EOK || result == -RT_ENOSYS)
  154. {
  155. file->data = device;
  156. return DFS_STATUS_OK;
  157. }
  158. /* open device failed. */
  159. return -DFS_STATUS_EIO;
  160. }
  161. int dfs_device_fs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
  162. {
  163. /* stat root directory */
  164. if ((path[0] == '/') && (path[1] == '\0'))
  165. {
  166. st->st_dev = 0;
  167. st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
  168. DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
  169. st->st_mode &= ~DFS_S_IFREG;
  170. st->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
  171. st->st_size = 0;
  172. st->st_mtime = 0;
  173. return DFS_STATUS_OK;
  174. }
  175. else
  176. {
  177. rt_device_t dev_id;
  178. dev_id = rt_device_find(&path[1]);
  179. if (dev_id != RT_NULL)
  180. {
  181. st->st_dev = 0;
  182. st->st_mode = DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
  183. DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
  184. if (dev_id->type == RT_Device_Class_Char)
  185. st->st_mode |= DFS_S_IFCHR;
  186. else if (dev_id->type == RT_Device_Class_Block)
  187. st->st_mode |= DFS_S_IFBLK;
  188. else
  189. st->st_mode |= DFS_S_IFREG;
  190. st->st_size = 0;
  191. st->st_mtime = 0;
  192. return DFS_STATUS_OK;
  193. }
  194. }
  195. return -DFS_STATUS_ENOENT;
  196. }
  197. int dfs_device_fs_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t count)
  198. {
  199. rt_uint32_t index;
  200. rt_object_t object;
  201. struct dirent *d;
  202. struct device_dirent *root_dirent;
  203. root_dirent = (struct device_dirent *)file->data;
  204. RT_ASSERT(root_dirent != RT_NULL);
  205. /* make integer count */
  206. count = (count / sizeof(struct dirent));
  207. if (count == 0)
  208. return -DFS_STATUS_EINVAL;
  209. for (index = 0; index < count && index + root_dirent->read_index < root_dirent->device_count;
  210. index ++)
  211. {
  212. object = (rt_object_t)root_dirent->devices[root_dirent->read_index + index];
  213. d = dirp + index;
  214. d->d_type = DFS_DT_REG;
  215. d->d_namlen = RT_NAME_MAX;
  216. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  217. rt_strncpy(d->d_name, object->name, RT_NAME_MAX);
  218. }
  219. root_dirent->read_index += index;
  220. return index * sizeof(struct dirent);
  221. }
  222. static const struct dfs_filesystem_operation _device_fs =
  223. {
  224. "devfs",
  225. DFS_FS_FLAG_DEFAULT,
  226. dfs_device_fs_mount,
  227. RT_NULL,
  228. RT_NULL,
  229. RT_NULL,
  230. dfs_device_fs_open,
  231. dfs_device_fs_close,
  232. dfs_device_fs_ioctl,
  233. dfs_device_fs_read,
  234. dfs_device_fs_write,
  235. RT_NULL,
  236. RT_NULL,
  237. dfs_device_fs_getdents,
  238. RT_NULL,
  239. dfs_device_fs_stat,
  240. RT_NULL,
  241. };
  242. int devfs_init(void)
  243. {
  244. /* register rom file system */
  245. dfs_register(&_device_fs);
  246. return 0;
  247. }
  248. INIT_FS_EXPORT(devfs_init);