devfs.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 <rtdevice.h>
  25. #include <dfs.h>
  26. #include <dfs_fs.h>
  27. #include <dfs_file.h>
  28. #include "devfs.h"
  29. struct device_dirent
  30. {
  31. rt_device_t *devices;
  32. rt_uint16_t read_index;
  33. rt_uint16_t device_count;
  34. };
  35. int dfs_device_fs_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
  36. {
  37. return RT_EOK;
  38. }
  39. int dfs_device_fs_ioctl(struct dfs_fd *file, int cmd, void *args)
  40. {
  41. rt_err_t result;
  42. rt_device_t dev_id;
  43. RT_ASSERT(file != RT_NULL);
  44. /* get device handler */
  45. dev_id = (rt_device_t)file->data;
  46. RT_ASSERT(dev_id != RT_NULL);
  47. /* close device handler */
  48. result = rt_device_control(dev_id, cmd, args);
  49. if (result == RT_EOK)
  50. return RT_EOK;
  51. return result;
  52. }
  53. int dfs_device_fs_read(struct dfs_fd *file, void *buf, size_t count)
  54. {
  55. int result;
  56. rt_device_t dev_id;
  57. RT_ASSERT(file != RT_NULL);
  58. /* get device handler */
  59. dev_id = (rt_device_t)file->data;
  60. RT_ASSERT(dev_id != RT_NULL);
  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_fd *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->data;
  73. RT_ASSERT(dev_id != RT_NULL);
  74. /* read device data */
  75. result = rt_device_write(dev_id, file->pos, buf, count);
  76. file->pos += result;
  77. return result;
  78. }
  79. int dfs_device_fs_close(struct dfs_fd *file)
  80. {
  81. rt_err_t result;
  82. rt_device_t dev_id;
  83. RT_ASSERT(file != RT_NULL);
  84. if (file->type == FT_DIRECTORY)
  85. {
  86. struct device_dirent *root_dirent;
  87. root_dirent = (struct device_dirent *)file->data;
  88. RT_ASSERT(root_dirent != RT_NULL);
  89. /* release dirent */
  90. rt_free(root_dirent);
  91. return RT_EOK;
  92. }
  93. /* get device handler */
  94. dev_id = (rt_device_t)file->data;
  95. RT_ASSERT(dev_id != RT_NULL);
  96. /* close device handler */
  97. result = rt_device_close(dev_id);
  98. if (result == RT_EOK)
  99. {
  100. file->data = RT_NULL;
  101. return RT_EOK;
  102. }
  103. return -EIO;
  104. }
  105. int dfs_device_fs_open(struct dfs_fd *file)
  106. {
  107. rt_err_t result;
  108. rt_device_t device;
  109. if (file->flags & O_CREAT)
  110. return -EINVAL;
  111. /* open root directory */
  112. if ((file->path[0] == '/') && (file->path[1] == '\0') &&
  113. (file->flags & O_DIRECTORY))
  114. {
  115. struct rt_object *object;
  116. struct rt_list_node *node;
  117. struct rt_object_information *information;
  118. struct device_dirent *root_dirent;
  119. rt_uint32_t count = 0;
  120. extern struct rt_object_information rt_object_container[];
  121. /* lock scheduler */
  122. rt_enter_critical();
  123. /* traverse device object */
  124. information = &rt_object_container[RT_Object_Class_Device];
  125. for (node = information->object_list.next; node != &(information->object_list); node = node->next)
  126. {
  127. count ++;
  128. }
  129. root_dirent = (struct device_dirent *)rt_malloc(sizeof(struct device_dirent) +
  130. count * sizeof(rt_device_t));
  131. if (root_dirent != RT_NULL)
  132. {
  133. root_dirent->devices = (rt_device_t *)(root_dirent + 1);
  134. root_dirent->read_index = 0;
  135. root_dirent->device_count = count;
  136. count = 0;
  137. /* get all device node */
  138. for (node = information->object_list.next; node != &(information->object_list); node = node->next)
  139. {
  140. object = rt_list_entry(node, struct rt_object, list);
  141. root_dirent->devices[count] = (rt_device_t)object;
  142. count ++;
  143. }
  144. }
  145. rt_exit_critical();
  146. /* set data */
  147. file->data = root_dirent;
  148. return RT_EOK;
  149. }
  150. device = rt_device_find(&file->path[1]);
  151. if (device == RT_NULL)
  152. return -ENODEV;
  153. #ifdef RT_USING_POSIX
  154. if (device->fops)
  155. {
  156. /* use device fops */
  157. file->fops = device->fops;
  158. file->data = (void*)device;
  159. /* use fops */
  160. if (file->fops->open)
  161. {
  162. result = file->fops->open(file);
  163. if (result == RT_EOK || result == -RT_ENOSYS)
  164. {
  165. return 0;
  166. }
  167. }
  168. }
  169. else
  170. #endif
  171. {
  172. result = rt_device_open(device, RT_DEVICE_OFLAG_RDWR);
  173. if (result == RT_EOK || result == -RT_ENOSYS)
  174. {
  175. file->data = device;
  176. return RT_EOK;
  177. }
  178. }
  179. file->data = RT_NULL;
  180. /* open device failed. */
  181. return -EIO;
  182. }
  183. int dfs_device_fs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
  184. {
  185. /* stat root directory */
  186. if ((path[0] == '/') && (path[1] == '\0'))
  187. {
  188. st->st_dev = 0;
  189. st->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH |
  190. S_IWUSR | S_IWGRP | S_IWOTH;
  191. st->st_mode &= ~S_IFREG;
  192. st->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
  193. st->st_size = 0;
  194. st->st_mtime = 0;
  195. return RT_EOK;
  196. }
  197. else
  198. {
  199. rt_device_t dev_id;
  200. dev_id = rt_device_find(&path[1]);
  201. if (dev_id != RT_NULL)
  202. {
  203. st->st_dev = 0;
  204. st->st_mode = S_IRUSR | S_IRGRP | S_IROTH |
  205. S_IWUSR | S_IWGRP | S_IWOTH;
  206. if (dev_id->type == RT_Device_Class_Char)
  207. st->st_mode |= S_IFCHR;
  208. else if (dev_id->type == RT_Device_Class_Block)
  209. st->st_mode |= S_IFBLK;
  210. else if (dev_id->type == RT_Device_Class_Pipe)
  211. st->st_mode |= S_IFIFO;
  212. else
  213. st->st_mode |= S_IFREG;
  214. st->st_size = 0;
  215. st->st_mtime = 0;
  216. return RT_EOK;
  217. }
  218. }
  219. return -ENOENT;
  220. }
  221. int dfs_device_fs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t count)
  222. {
  223. rt_uint32_t index;
  224. rt_object_t object;
  225. struct dirent *d;
  226. struct device_dirent *root_dirent;
  227. root_dirent = (struct device_dirent *)file->data;
  228. RT_ASSERT(root_dirent != RT_NULL);
  229. /* make integer count */
  230. count = (count / sizeof(struct dirent));
  231. if (count == 0)
  232. return -EINVAL;
  233. for (index = 0; index < count && index + root_dirent->read_index < root_dirent->device_count;
  234. index ++)
  235. {
  236. object = (rt_object_t)root_dirent->devices[root_dirent->read_index + index];
  237. d = dirp + index;
  238. d->d_type = DT_REG;
  239. d->d_namlen = RT_NAME_MAX;
  240. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  241. rt_strncpy(d->d_name, object->name, RT_NAME_MAX);
  242. }
  243. root_dirent->read_index += index;
  244. return index * sizeof(struct dirent);
  245. }
  246. static int dfs_device_fs_poll(struct dfs_fd *fd, struct rt_pollreq *req)
  247. {
  248. int mask = 0;
  249. return mask;
  250. }
  251. static const struct dfs_file_ops _device_fops =
  252. {
  253. dfs_device_fs_open,
  254. dfs_device_fs_close,
  255. dfs_device_fs_ioctl,
  256. dfs_device_fs_read,
  257. dfs_device_fs_write,
  258. RT_NULL, /* flush */
  259. RT_NULL, /* lseek */
  260. dfs_device_fs_getdents,
  261. dfs_device_fs_poll,
  262. };
  263. static const struct dfs_filesystem_ops _device_fs =
  264. {
  265. "devfs",
  266. DFS_FS_FLAG_DEFAULT,
  267. &_device_fops,
  268. dfs_device_fs_mount,
  269. RT_NULL,
  270. RT_NULL,
  271. RT_NULL,
  272. RT_NULL,
  273. dfs_device_fs_stat,
  274. RT_NULL,
  275. };
  276. int devfs_init(void)
  277. {
  278. /* register rom file system */
  279. dfs_register(&_device_fs);
  280. return 0;
  281. }