提交 f95cca00 编写于 作者: F Fan Qian 提交者: Li Cheng

BugID:17711858: VFS API standardized rectification

Change-Id: Ic8f61a8241b949da60b46817f0e839c2cac22c60
上级 516d0d7b
......@@ -9,8 +9,7 @@
#include "aos/errno.h"
#include "vfs_inode.h"
#include "vfs_register.h"
#include "aos/vfs.h"
#include "ramfs_types.h"
#include "ramfs_api.h"
......
menuconfig AOS_COMP_VFS_DEVICE
bool "vfs_device"
default n
help
if AOS_COMP_VFS_DEVICE
# Configurations for component vfs_device
endif
NAME := vfs_device
$(NAME)_MBINS_TYPE := kernel
$(NAME)_VERSION := 0.0.1
$(NAME)_SUMMARY :=
$(NAME)_SOURCES += vfs_adc.c
$(NAME)_SOURCES += vfs_uart.c
$(NAME)_SOURCES += vfs_gpio.c
$(NAME)_SOURCES += vfs_spi.c
$(NAME)_SOURCES += vfs_pwm.c
$(NAME)_SOURCES += vfs_rtc.c
$(NAME)_SOURCES += vfs_wdg.c
$(NAME)_SOURCES += vfs_i2c.c
$(NAME)_INCLUDES += ../include/device/ \
../include/ \
../../hal/soc/
src = Split('''
vfs_adc.c
vfs_uart.c
vfs_gpio.c
vfs_spi.c
vfs_pwm.c
vfs_rtc.c
vfs_wdg.c
vfs_i2c.c
''')
component = aos_component('vfs_device', src)
component.add_global_includes('../include/device', '../include', '../../hal/soc/')
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef AOS_VFS_ADC_H
#define AOS_VFS_ADC_H
#ifdef __cplusplus
extern "C" {
#endif
#include "vfs_inode.h"
/* adc driver struct */
extern const struct file_ops adc_ops;
/**
* This function is used to open adc device.
*
* @param[in] fp device pointer.
*
* @return 0 on success, others on failure with errno set appropriately.
*/
int vfs_adc_open(inode_t *inode, file_t *fp);
/**
* This function is used to close adc device.
*
* @param[in] fp device pointer.
*
* @return 0 on success, others on failure with errno set appropriately.
*/
int vfs_adc_close(file_t *fp);
/**
* This function is used to complete the data sample and get the sampled value.
*
* @param[in] fp device pointer.
* @param[out] buf data buffer for sampled data.
* @param[in] nbytes the maximum size of the user-provided buffer.
*
* @return The positive non-zero number of bytes read on success,
* 0 on read nothing, or negative on failure with errno set appropriately.
*/
ssize_t vfs_adc_read(file_t *fp, void *buf, size_t nbytes);
#ifdef __cplusplus
}
#endif
#endif /* AOS_VFS_ADC_H */
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef AOS_VFS_DEVICE_H
#define AOS_VFS_DEVICE_H
#ifdef __cplusplus
extern "C" {
#endif
#include "vfs_adc.h"
#include "vfs_gpio.h"
#include "vfs_i2c.h"
#include "vfs_pwm.h"
#include "vfs_rtc.h"
#include "vfs_spi.h"
#include "vfs_uart.h"
#include "vfs_wdg.h"
#ifdef __cplusplus
}
#endif
#endif /* AOS_VFS_DEVICE_H */
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef AOS_VFS_GPIO_H
#define AOS_VFS_GPIO_H
#ifdef __cplusplus
extern "C" {
#endif
#include "vfs_inode.h"
/* cmd for ioctl */
#define IOCTL_GPIO_OUTPUT_HIGHT 1 /* output hight */
#define IOCTL_GPIO_OUTPUT_LOW 2 /* output low */
#define IOCTL_GPIO_OUTPUT_TOGGLE 3 /* toggle output */
/* gpio driver struct */
extern const struct file_ops gpio_ops;
/**
* This function is used to open gpio device.
*
* @param[in] fp device pointer.
*
* @return 0 on success, others on failure with errno set appropriately.
*/
int vfs_gpio_open(inode_t *inode, file_t *fp);
/**
* This function is used to close gpio device.
*
* @param[in] fp device pointer.
*
* @return 0 on success, others on failure with errno set appropriately.
*/
int vfs_gpio_close(file_t *fp);
/**
* This function is used to get data from gpio.
*
* @param[in] fp device pointer.
* @param[out] buf data buffer for data.
* @param[in] nbytes the maximum size of the user-provided buffer.
*
* @return The positive non-zero number of bytes read on success,
* 0 on read nothing, or negative on failure with errno set appropriately.
*/
ssize_t vfs_gpio_read(file_t *fp, void *buf, size_t nbytes);
/**
* This function performs device input and output operations.
*
* @param[in] fp device pointer.
* @param[in] cmd command of input and output operating.
* @param[in] arg argument of input and output operating.
*
* @return 0 on success, negative on failure with errno set appropriately.
*/
int vfs_gpio_ioctl(file_t *fp, int cmd, unsigned long arg);
#ifdef __cplusplus
}
#endif
#endif /* AOS_VFS_GPIO_H */
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef AOS_VFS_I2C_H
#define AOS_VFS_I2C_H
#ifdef __cplusplus
extern "C" {
#endif
#include "vfs_inode.h"
/* i2c driver struct */
extern const struct file_ops i2c_ops;
/**
* This function is used to open i2c device.
*
* @param[in] fp device pointer.
*
* @return 0 on success, others on failure with errno set appropriately.
*/
int vfs_i2c_open(inode_t *inode, file_t *fp);
/**
* This function is used to close i2c device.
*
* @param[in] fp device pointer.
*
* @return 0 on success, others on failure with errno set appropriately.
*/
int vfs_i2c_close(file_t *fp);
/**
* This function is used to get data from i2c.
*
* @param[in] fp device pointer.
* @param[out] buf data buffer for data.
* @param[in] nbytes the maximum size of the user-provided buffer.
*
* @return The positive non-zero number of bytes read on success,
* 0 on read nothing, or negative on failure with errno set appropriately.
*/
ssize_t vfs_i2c_read(file_t *fp, void *buf, size_t nbytes);
/**
* This function is used to send data through i2c.
*
* @param[in] fp device pointer.
* @param[out] buf data buffer for data.
* @param[in] nbytes the maximum size of the user-provided buffer.
*
* @return The positive non-zero number of bytes write on success,
* 0 on write nothing, or negative on failure with errno set appropriately.
*/
ssize_t vfs_i2c_write(file_t *fp, const void *buf, size_t nbytes);
#ifdef __cplusplus
}
#endif
#endif /* AOS_VFS_I2C_H */
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef AOS_VFS_PWM_H
#define AOS_VFS_PWM_H
#ifdef __cplusplus
extern "C" {
#endif
#include "vfs_inode.h"
/* cmd for ioctl */
#define IOCTL_PWM_OUTPUT_START 1 /* start output pwm */
#define IOCTL_PWM_OUTPUT_STOP 2 /* stop output pwm */
/* pwm driver struct */
extern const struct file_ops pwm_ops;
/**
* This function is used to open pwm device.
*
* @param[in] fp device pointer.
*
* @return 0 on success, others on failure with errno set appropriately.
*/
int vfs_pwm_open(inode_t *inode, file_t *fp);
/**
* This function is used to close pwm device.
*
* @param[in] fp device pointer.
*
* @return 0 on success, others on failure with errno set appropriately.
*/
int vfs_pwm_close(file_t *fp);
/**
* This function performs device input and output operations.
*
* @param[in] fp device pointer.
* @param[in] cmd command of input and output operating.
* @param[in] arg argument of input and output operating.
*
* @return 0 on success, negative on failure with errno set appropriately.
*/
int vfs_pwm_ioctl(file_t *fp, int cmd, unsigned long arg);
#ifdef __cplusplus
}
#endif
#endif /* AOS_VFS_PWM_H */
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef AOS_VFS_RTC_H
#define AOS_VFS_RTC_H
#ifdef __cplusplus
extern "C" {
#endif
#include "vfs_inode.h"
/* rtc driver struct */
extern const struct file_ops rtc_ops;
/**
* This function is used to open rtc device.
*
* @param[in] fp device pointer.
*
* @return 0 on success, others on failure with errno set appropriately.
*/
int vfs_rtc_open(inode_t *inode, file_t *fp);
/**
* This function is used to close rtc device.
*
* @param[in] fp device pointer.
*
* @return 0 on success, others on failure with errno set appropriately.
*/
int vfs_rtc_close(file_t *fp);
/**
* This function is used to get Real-Time Clock.
*
* @param[in] fp device pointer.
* @param[out] buf data buffer for data.
* @param[in] nbytes the maximum size of the user-provided buffer.
*
* @return The positive non-zero number of bytes read on success,
* 0 on read nothing, or negative on failure with errno set appropriately.
*/
ssize_t vfs_rtc_read(file_t *fp, void *buf, size_t nbytes);
/**
* This function is used to set Real-Time Clock.
*
* @param[in] fp device pointer.
* @param[out] buf data buffer for data.
* @param[in] nbytes the maximum size of the user-provided buffer.
*
* @return The positive non-zero number of bytes write on success,
* 0 on write nothing, or negative on failure with errno set appropriately.
*/
ssize_t vfs_rtc_write(file_t *fp, const void *buf, size_t nbytes);
#ifdef __cplusplus
}
#endif
#endif /* AOS_VFS_RTC_H */
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef AOS_VFS_SPI_H
#define AOS_VFS_SPI_H
#ifdef __cplusplus
extern "C" {
#endif
#include "vfs_inode.h"
/* spi driver struct */
extern const struct file_ops spi_ops;
/**
* This function is used to open spi device.
*
* @param[in] fp device pointer.
*
* @return 0 on success, others on failure with errno set appropriately.
*/
int vfs_spi_open(inode_t *inode, file_t *fp);
/**
* This function is used to close spi device.
*
* @param[in] fp device pointer.
*
* @return 0 on success, others on failure with errno set appropriately.
*/
int vfs_spi_close(file_t *fp);
/**
* This function is used to get data from spi.
*
* @param[in] fp device pointer.
* @param[out] buf data buffer for data.
* @param[in] nbytes the maximum size of the user-provided buffer.
*
* @return The positive non-zero number of bytes read on success,
* 0 on read nothing, or negative on failure with errno set appropriately.
*/
ssize_t vfs_spi_read(file_t *fp, void *buf, size_t nbytes);
/**
* This function is used to send data through spi.
*
* @param[in] fp device pointer.
* @param[out] buf data buffer for data.
* @param[in] nbytes the maximum size of the user-provided buffer.
*
* @return The positive non-zero number of bytes write on success,
* 0 on write nothing, or negative on failure with errno set appropriately.
*/
ssize_t vfs_spi_write(file_t *fp, const void *buf, size_t nbytes);
#ifdef __cplusplus
}
#endif
#endif /* AOS_VFS_SPI_H */
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef AOS_VFS_UART_H
#define AOS_VFS_UART_H
#ifdef __cplusplus
extern "C" {
#endif
#include "vfs_inode.h"
/* uart driver struct */
extern const struct file_ops uart_ops;
/**
* This function is used to open uart device.
*
* @param[in] fp device pointer.
*
* @return 0 on success, others on failure with errno set appropriately.
*/
int vfs_uart_open(inode_t *inode, file_t *fp);
/**
* This function is used to close uart device.
*
* @param[in] fp device pointer.
*
* @return 0 on success, others on failure with errno set appropriately.
*/
int vfs_uart_close(file_t *fp);
/**
* This function is used to get data from uart.
*
* @param[in] fp device pointer.
* @param[out] buf data buffer for data.
* @param[in] nbytes the maximum size of the user-provided buffer.
*
* @return The positive non-zero number of bytes read on success,
* 0 on read nothing, or negative on failure with errno set appropriately.
*/
ssize_t vfs_uart_read(file_t *fp, void *buf, size_t nbytes);
/**
* This function is used to send data through uart.
*
* @param[in] fp device pointer.
* @param[out] buf data buffer for data.
* @param[in] nbytes the maximum size of the user-provided buffer.
*
* @return The positive non-zero number of bytes write on success,
* 0 on write nothing, or negative on failure with errno set appropriately.
*/
ssize_t vfs_uart_write(file_t *fp, const void *buf, size_t nbytes);
#ifdef __cplusplus
}
#endif
#endif /* AOS_VFS_UART_H */
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef AOS_VFS_WDG_H
#define AOS_VFS_WDG_H
#ifdef __cplusplus
extern "C" {
#endif
#include "vfs_inode.h"
/* cmd for ioctl */
#define IOCTL_WDG_RELOAD 1 /* reload watchdog */
/* wdg driver struct */
extern const struct file_ops wdg_ops;
/**
* This function is used to open wdg device.
*
* @param[in] fp device pointer.
*
* @return 0 on success, others on failure with errno set appropriately.
*/
int vfs_wdg_open(inode_t *inode, file_t *fp);
/**
* This function is used to close wdg device.
*
* @param[in] fp device pointer.
*
* @return 0 on success, others on failure with errno set appropriately.
*/
int vfs_wdg_close(file_t *fp);
/**
* This function performs device input and output operations.
*
* @param[in] fp device pointer.
* @param[in] cmd command of input and output operating.
* @param[in] arg argument of input and output operating.
*
* @return 0 on success, negative on failure with errno set appropriately.
*/
int vfs_wdg_ioctl(file_t *fp, int cmd, unsigned long arg);
#ifdef __cplusplus
}
#endif
#endif /* AOS_VFS_WDG_H */
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef AOS_VFS_H
#define AOS_VFS_H
#include <sys/types.h>
#include <sys/stat.h>
#include <aos/types.h>
#ifdef __cplusplus
extern "C" {
#endif
struct statfs {
long f_type; /* fs type */
long f_bsize; /* optimized transport block size */
long f_blocks; /* total blocks */
long f_bfree; /* available blocks */
long f_bavail; /* number of blocks that non-super users can acquire */
long f_files; /* total number of file nodes */
long f_ffree; /* available file nodes */
long f_fsid; /* fs id */
long f_namelen; /* max file name length */
};
typedef struct {
int d_ino; /* file number */
uint8_t d_type; /* type of file */
char d_name[]; /* file name */
} aos_dirent_t;
typedef struct {
int dd_vfs_fd;
int dd_rsv;
} aos_dir_t;
/**
* Init vfs.
*
* @param[in] NULL
*
* @return 0 on success, negative error on failure.
*/
int vfs_init(void);
/**
* Open the file or device by its path.
*
* @param[in] path the path of the file or device to open.
* @param[in] flags the mode of open operation.
*
* @return the new file descriptor, negative error on failure.
*/
int aos_open(const char *path, int flags);
/**
* Close the file or device by its file descriptor.
*
* @param[in] fd the file descriptor of the file or device.
*
* @return 0 on success, negative error on failure.
*/
int aos_close(int fd);
/**
* Read the contents of a file or device into a buffer.
*
* @param[in] fd the file descriptor of the file or device.
* @param[in] nbytes the number of bytes to read.
* @param[out] buf the buffer to read in to.
*
* @return The number of bytes read, 0 at end of file, negative error on failure.
*/
ssize_t aos_read(int fd, void *buf, size_t nbytes);
/**
* Write the contents of a buffer to file or device.
*
* @param[in] fd the file descriptor of the file or device.
* @param[in] nbytes the number of bytes to write.
* @param[in] buf the buffer to write from.
*
* @return The number of bytes written, negative error on failure.
*/
ssize_t aos_write(int fd, const void *buf, size_t nbytes);
/**
* This is a wildcard API for sending controller specific commands.
*
* @param[in] fd the file descriptior of the file or device.
* @param[in] cmd A controller specific command.
* @param[in] arg Argument to the command, interpreted according to the command.
*
* @return any return from the command.
*/
int aos_ioctl(int fd, int cmd, unsigned long arg);
/**
* A mechanism to multiplex input/output over a set of file descriptors.
* For every file descriptor provided, poll() examines it for any events registered for that particular file descriptor.
*
* @param[in] fds a point to the array of pollfd struct carrying a file descriptor and bitmasks of events.
* @param[in] nfhs number of file descriptors.
* @param[in] timeout timer value to timeout or -1 for loop forever.
*
* @return number of file descriptors selected (for which revents is non-zero). 0 if timed out with nothing selected. -1 for error.
*/
int aos_poll(struct pollfd *fds, int nfds, int timeout);
/**
* Performs one of the operations described below on the open file descriptor, The operation is determined by cmd.
*
* @param[in] fd the file descriptor of the file or device.
* @param[in] cmd the operation of the file or device.
* @param[in] val it depends on whether cmd need params.
*
* @return 0 on success, negative error on failure.
*/
int aos_fcntl(int fd, int cmd, int val);
/**
* Move the file position to a given offset from a given location.
*
* @param[in] fd the file descriptor of the file.
* @param[in] offset The offset from whence to move to.
* @param[in] whence The start of where to seek.
* SEEK_SET to start from beginning of file.
* SEEK_CUR to start from current position in file.
* SEEK_END to start from end of file.
*
* @return The new offset of the file.
*/
off_t aos_lseek(int fd, off_t offset, int whence);
/**
* Flush any buffers associated with the file.
*
* @param[in] fd the file descriptor of the file.
*
* @return 0 on success, negative error code on failure.
*/
int aos_sync(int fd);
/**
* Store information about the file in a stat structure.
*
* @param[in] path The path of the file to find information about.
* @param[out] st The stat buffer to write to.
*
* @return 0 on success, negative error code on failure.
*/
int aos_stat(const char *path, struct stat *st);
/**
* Remove a file from the filesystem.
*
* @param[in] path The path of the file to remove.
*
* @return 0 on success, negative error code on failure.
*/
int aos_unlink(const char *path);
/**
* Rename a file in the filesystem.
*
* @param[in] oldpath The path of the file to rename.
* @param[in] newpath The path to rename it to.
*
* @return 0 on success, negative error code on failure.
*/
int aos_rename(const char *oldpath, const char *newpath);
/**
* Open a directory on the filesystem.
*
* @param[in] path the path of the directory to open.
*
* @return a point of directory stream on success, NULL on failure.
*/
aos_dir_t *aos_opendir(const char *path);
/**
* Close a directory.
*
* @param[in] dir the handle of the directory to close.
*
* @return 0 on success, negative error code on failure.
*/
int aos_closedir(aos_dir_t *dir);
/**
* Read the next directory entry.
*
* @param[in] dir the handle of the directory to read.
*
* @return a pointer to a dirent structure.
*/
aos_dirent_t *aos_readdir(aos_dir_t *dir);
/**
* Create the directory, if they do not already exist.
*
* @param[in] path the path of the directory.
*
* @return 0 on success, negative error code on failure.
*/
int aos_mkdir(const char *path);
/**
* Remove a directory.
*
* @param[in] path the path of the directory.
*
* @return 0 on success, negative error code on failure.
*/
int aos_rmdir(const char *path);
/**
* Reset the position of a directory stream to the beginning of a directory.
*
* @param[in] dir the handle of the directory.
*
* @return none.
*/
void aos_rewinddir(aos_dir_t *dir);
/**
* Obtain the current location associated with the directory stream specified by dirp.
*
* @param[in] dir the handle of the directory.
*
* @return 0 on success, negative error code on failure.
*/
long aos_telldir(aos_dir_t *dir);
/**
* Reset the position of a directory stream to the beginning of a directory.
*
* @param[in] dir the handle of the directory.
* @param[in] loc the position of the directory.
*
* @return none.
*/
void aos_seekdir(aos_dir_t *dir, long loc);
/**
* Store information about the file system in a statfs structure.
*
* @param[in] path The path of the file system to find information about.
* @param[out] buf The statfs buffer to write to.
*
* @return 0 on success, negative error code on failure.
*/
int aos_statfs(const char *path, struct statfs *buf);
/**
* get access info.
*
* @param path The path of the file.
* @param mode the info to get.
*
* @return 0 on success, negative error code on failure.
*/
int aos_access(const char *path, int amode);
#ifdef __cplusplus
}
#endif
#endif /* AOS_VFS_H */
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef VFS_ADAPT_H
#define VFS_ADAPT_H
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Create OS lock
*
* @return the pointer of the lock
*
*/
void *vfs_lock_create(void);
/**
* @brief Free OS lock
*
* @param[in] lock pointer to the os lock
*
* @return 0 on success, negative error on failure
*
*/
int32_t vfs_lock_free(void *lock);
/**
* @brief Lock the os lock
*
* @param[in] lock pointer to the os lock
*
* @return 0 on success, negative error on failure
*
*/
int32_t vfs_lock(void *lock);
/**
* @brief Unlock the os lock
*
* @param[in] lock pointer to the os lock
*
* @return 0 on success, negative error on failure
*
*/
int32_t vfs_unlock(void *lock);
/**
* @brief wrapper of MM allocation
*
* @param[in] size size of the mem to alloc
*
* @return NULL is error, other is memory address
*
*/
void *vfs_malloc(uint32_t size);
/**
* @brief wrapper of MM free
*
* @param[in] ptr address point of the mem
*
*/
void vfs_free(void *ptr);
#ifdef __cplusplus
}
#endif
#endif /* VFS_ADAPT_H */
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef VFS_API_H
#define VFS_API_H
#include "vfs_types.h"
#include "vfs_err.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Initialize the vfs module
*
* @return 0 on success, negative error on failure
*
*/
int32_t vfs_init(void);
/**
* @brief Open the file or device by path
*
* @param[in] path the path of the file or device to open
* @param[in] flags the mode of the open operation
*
* @return the new file descriptor, negative error on failure
*
*/
int32_t vfs_open(const char *path, int32_t flags);
/**
* @brief Close the file or device by file descriptor
*
* @param[in] fd the file descriptor of the file ot device
*
* @return 0 on success, negative error on failure
*
*/
int32_t vfs_close(int32_t fd);
/**
* @brief Read the contents of a file or device into a buffer
*
* @param[in] fd the file descriptor of the file or device
* @param[out] buf the buffer to read into
* @param[in] nbytes the number of bytes to read
*
* @return the number of bytes read, 0 at end of file, negative error on failure
*
*/
int32_t vfs_read(int32_t fd, void *buf, uint32_t nbytes);
/**
* @brief Write the contents of a buffer to file or device
*
* @param[in] fd the file descriptor of the file or device
* @param[in] buf the buffer to write from
* @param[in] nbytes the number of bytes to write
*
* @return the number of bytes written, negative error on failure
*
*/
int32_t vfs_write(int32_t fd, const void *buf, uint32_t nbytes);
/**
* @brief This is a wildcard API for sending specific commands
*
* @param[in] fd the file descriptor of the file or device
* @param[in] cmd a specific command
* @param[in] arg argument to the command, interpreted according to the cmd
*
* @return any return from the command
*
*/
int32_t vfs_ioctl(int32_t fd, int32_t cmd, uint32_t arg);
/**
* @brief This is a wildcard API for executing the particular poll by fd
*
* @param[in] fd the file descriptor of the file or device
* @param[in] flag the flag of the polling
* @param[in] notify the polling notify callback
* @param[in] fds a pointer to the array of pollfd
* @param[in] arg the arguments of the polling
*
* @return 0 on success, negative error on failure
*
*/
int32_t vfs_do_pollfd(int32_t fd, int32_t flag, vfs_poll_notify_t notify,
void *fds, void *arg);
/**
* @brief Move the file position to a given offset from a given location
*
* @param[in] fd the file descriptor of the file
* @param[in] offset the offset from whence to move to
* @param[in] whence the start of where to seek
* SEEK_SET to start from beginning of the file
* SEEK_CUR to start from current position in file
* SEEK_END to start from end of file
*
* @return the new offset of the file
*
*/
uint32_t vfs_lseek(int32_t fd, uint32_t offset, int32_t whence);
/**
* @brief Flush any buffers associated with the file
*
* @param[in] fd the file descriptor of the file
*
* @return 0 on success, negative error on failure
*
*/
int32_t vfs_sync(int32_t fd);
/**
* Store information about the file in a vfs_stat structure
*
* @param[in] path the path of the file to find information about
* @param[out] st the vfs_stat buffer to write to
*
* @return 0 on success, negative error on failure
*
*/
int32_t vfs_stat(const char *path, vfs_stat_t *st);
/**
* @brief Remove a file from the filesystem
*
* @param[in] path the path of the file to remove
*
* @return 0 on success, negative error on failure
*
*/
int32_t vfs_unlink(const char *path);
/**
* @brief Rename a file in the filesystem
*
* @param[in] oldpath the path of the file to rename
* @param[in] newpath the path to rename it to
*
* @return 0 on success, negative error on failure
*
*/
int32_t vfs_rename(const char *oldpath, const char *newpath);
/**
* @brief Open a directory on the filesystem
*
* @param[in] path the path of the directory to open
*
* @return a pointer of directory stream on success, NULL on failure
*
*/
vfs_dir_t *vfs_opendir(const char *path);
/**
* @brief Close a directory
*
* @param[in] dir the pointer of the directory to close
*
* @return 0 on success, negative error on failure
*
*/
int32_t vfs_closedir(vfs_dir_t *dir);
/**
* @brief Read the next directory entry
*
* @param[in] dir the pointer of the directory to read
*
* @return a pointer to a vfs_dirent structure
*
*/
vfs_dirent_t *vfs_readdir(vfs_dir_t *dir);
/**
* @brief Create the directory, if ther do not already exist
*
* @param[in] path the path of the directory to create
*
* @return 0 on success, negative error on failure
*
*/
int32_t vfs_mkdir(const char *path);
/**
* @brief Remove a directory
*
* @param[in] path the path of the directory to remove
*
* @return 0 on success, negative error on failure
*
*/
int32_t vfs_rmdir(const char *path);
/**
* @brief Reset the position of a directory stream to the beginning of a directory
*
* @param[in] dir the pointer of the directory to rewind
*
* @return none
*
*/
void vfs_rewinddir(vfs_dir_t *dir);
/**
* @brief Obtain the current location associated with the directory
*
* @param[in] dir the pointer of the directory to tell
*
* @return the current location of the directory, negative error on failure
*
*/
int32_t vfs_telldir(vfs_dir_t *dir);
/**
* @brief Move the directory position to a given location
*
* @param[in] dir the pointer of the directory to seek
* @param[in] loc the location of the directory
*
* @return none
*/
void vfs_seekdir(vfs_dir_t *dir, int32_t loc);
/**
* @brief Store information about the filesystem in a vfs_statfs structure
*
* @param[in] path the path of the filesystem to find information about
* @param[out] buf the vfs_statfs buffer to write to
*
* @return 0 on success, negative error on failure
*
*/
int32_t vfs_statfs(const char *path, vfs_statfs_t *buf);
/**
* @brief Get access information
*
* @param[in] path the path of the file to access
* @param[in] amode the access information to get
*
* @return 0 on success, negative error on failure
*
*/
int32_t vfs_access(const char *path, int32_t amode);
/**
* @brief Get file descriptor offset
*
* @return the vfs file descriptor offset
*
*/
int32_t vfs_fd_offset_get(void);
/**
* @brief Bind driver to the file or device
*
* @param[in] path the path of the file or device
* @param[in] ops the driver operations to bind
* @param[in] arg the arguments of the driver operations
*
* @return 0 on success, negative error on failure
*
*/
int32_t vfs_register_driver(const char *path, vfs_file_ops_t *ops, void *arg);
/**
* @brief Unbind driver from the file or device
*
* @param[in] path the path of the file or device
*
* @return 0 on success, negative error on failure
*
*/
int32_t vfs_unregister_driver(const char *path);
/**
* @brief Mount filesystem to the path
*
* @param[in] path the mount point path
* @param[in] ops the filesystem operations
* @param[in] arg the arguments of the filesystem operations
*
* @return 0 on success, negative error on failure
*
*/
int32_t vfs_register_fs(const char *path, vfs_filesystem_ops_t* ops, void *arg);
/**
* @brief Unmount the filesystem
*
* @param[in] path the mount point path
*
* @return 0 on success, negative error on failure
*
*/
int32_t vfs_unregister_fs(const char *path);
#ifdef __cplusplus
}
#endif
#endif /* VFS_API_H */
......@@ -2,32 +2,43 @@
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef AOS_VFS_CONFIG_H
#define AOS_VFS_CONFIG_H
#ifndef VFS_CONF_H
#define VFS_CONF_H
#ifdef __cplusplus
extern "C"
{
#ifndef CONFIG_VFS_DEVICE_NODES
#define VFS_DEVICE_NODES 25
#else
#define VFS_DEVICE_NODES CONFIG_VFS_DEVICE_NODES
#endif
#define VFS_FALSE 0u
#define VFS_TRUE 1u
#define AOS_CONFIG_VFS_DEV_NODES 25
/*mem 1000 byte*/
#define AOS_CONFIG_VFS_DEV_MEM 2000
#define AOS_CONFIG_VFS_POLL_SUPPORT 1
#define AOS_CONFIG_VFS_FD_OFFSET 512
#ifndef CONFIG_VFS_DEVICE_MEM_SIZE
#define VFS_DEVICE_MEM_SIZE 2000
#else
#define VFS_DEVICE_MEM_SIZE CONFIG_VFS_DEVICE_MEM_SIZE
#endif
#define MAX_FILE_NUM (AOS_CONFIG_VFS_DEV_NODES * 2)
#ifndef CONFIG_VFS_FD_OFFSET
#define VFS_FD_OFFSET 512
#else
#define VFS_FD_OFFSET CONFIG_VFS_FD_OFFSET
#endif
#ifdef _WIN32
#define CHECK_IF_NON_VFS_FD(_SOCKET) \
((_SOCKET < AOS_CONFIG_VFS_FD_OFFSET) || \
(_SOCKET > (AOS_CONFIG_VFS_FD_OFFSET + AOS_CONFIG_VFS_DEV_NODES)))
#ifndef CONFIG_VFS_PATH_MAX
#define VFS_PATH_MAX 256
#else
#define VFS_PATH_MAX CONFIG_VFS_PATH_MAX
#endif
#ifdef __cplusplus
}
#ifndef CONFIG_VFS_MAX_FILE_NUM
#define VFS_MAX_FILE_NUM (VFS_DEVICE_NODES * 2)
#else
#define VFS_MAX_FILE_NUM CONFIG_VFS_MAX_FILE_NUM
#endif
#ifndef CONFIG_VFS_STAT_INCLUDE_SIZE
#define VFS_STAT_INCLUDE_SIZE 1
#else
#define VFS_STAT_INCLUDE_SIZE CONFIG_VFS_STAT_INCLUDE_SIZE
#endif
#endif /* VFS_CONF_H */
......@@ -2,20 +2,18 @@
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef VFS_ERRNO_H
#define VFS_ERRNO_H
#ifndef VFS_ERR_H
#define VFS_ERR_H
#ifdef __cplusplus
extern "C" {
#endif
#include <errno.h>
#define VFS_SUCCESS 0u
#ifdef __cplusplus
}
#endif
#endif /* VFS_ERRNO_H */
#define VFS_OK 0
#define VFS_ERR_NOMEM -10000
#define VFS_ERR_INVAL -10001
#define VFS_ERR_NOENT -10002
#define VFS_ERR_NAMETOOLONG -10003
#define VFS_ERR_NOSYS -10004
#define VFS_ERR_ENFILE -10005
#define VFS_ERR_NODEV -10006
#define VFS_ERR_LOCK -10007
#define VFS_ERR_BUSY -10008
#endif /* VFS_ERR_H */
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef AOS_VFS_FILE_H
#define AOS_VFS_FILE_H
#ifdef __cplusplus
extern "C" {
#endif
int get_fd(file_t *file);
file_t *get_file(int fd);
file_t *new_file(inode_t *node);
void del_file(file_t *file);
#ifdef __cplusplus
}
#endif
#endif
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef VFS_INODE_H
#define VFS_INODE_H
#include <stdbool.h>
#include <stdint.h>
#include <sys/stat.h>
#include <vfs_conf.h>
#include <k_api.h>
#include <aos/errno.h>
#include <vfs.h>
#ifdef __cplusplus
extern "C" {
#endif
enum {
VFS_TYPE_NOT_INIT,
VFS_TYPE_CHAR_DEV,
VFS_TYPE_BLOCK_DEV,
VFS_TYPE_FS_DEV
};
#define INODE_IS_TYPE(i,t) \
((i)->type == (t))
#define INODE_IS_CHAR(i) INODE_IS_TYPE(i, VFS_TYPE_CHAR_DEV)
#define INODE_IS_BLOCK(i) INODE_IS_TYPE(i, VFS_TYPE_BLOCK_DEV)
#define INODE_IS_FS(i) INODE_IS_TYPE(i, VFS_TYPE_FS_DEV)
#define INODE_GET_TYPE(i) ((i)->type)
#define INODE_SET_TYPE(i,t) \
do \
{ \
(i)->type = (t); \
} \
while(0)
#define INODE_SET_CHAR(i) INODE_SET_TYPE(i, VFS_TYPE_CHAR_DEV)
#define INODE_SET_BLOCK(i) INODE_SET_TYPE(i, VFS_TYPE_BLOCK_DEV)
#define INODE_SET_FS(i) INODE_SET_TYPE(i, VFS_TYPE_FS_DEV)
typedef const struct file_ops file_ops_t;
typedef const struct fs_ops fs_ops_t;
union inode_ops_t {
const file_ops_t *i_ops; /* char driver operations */
const fs_ops_t *i_fops; /* FS operations */
};
/* this structure represents inode for driver and fs*/
typedef struct {
union inode_ops_t ops; /* inode operations */
void *i_arg; /* per inode private data */
char *i_name; /* name of inode */
int i_flags; /* flags for inode */
uint8_t type; /* type for inode */
uint8_t refs; /* refs for inode */
} inode_t;
typedef struct {
inode_t *node; /* node for file */
void *f_arg; /* f_arg for file */
size_t offset; /* offset for file */
} file_t;
struct pollfd;
typedef void (*poll_notify_t)(struct pollfd *fd, void *arg);
struct file_ops {
int (*open) (inode_t *node, file_t *fp);
int (*close) (file_t *fp);
ssize_t (*read) (file_t *fp, void *buf, size_t nbytes);
ssize_t (*write) (file_t *fp, const void *buf, size_t nbytes);
int (*ioctl) (file_t *fp, int cmd, unsigned long arg);
#ifdef AOS_CONFIG_VFS_POLL_SUPPORT
int (*poll) (file_t *fp, bool flag, poll_notify_t notify, struct pollfd *fd, void *arg);
#endif
};
struct fs_ops {
int (*open) (file_t *fp, const char *path, int flags);
int (*close) (file_t *fp);
ssize_t (*read) (file_t *fp, char *buf, size_t len);
ssize_t (*write) (file_t *fp, const char *buf, size_t len);
off_t (*lseek) (file_t *fp, off_t off, int whence);
int (*sync) (file_t *fp);
int (*stat) (file_t *fp, const char *path, struct stat *st);
int (*unlink) (file_t *fp, const char *path);
int (*rename) (file_t *fp, const char *oldpath, const char *newpath);
aos_dir_t *(*opendir) (file_t *fp, const char *path);
aos_dirent_t *(*readdir) (file_t *fp, aos_dir_t *dir);
int (*closedir) (file_t *fp, aos_dir_t *dir);
int (*mkdir) (file_t *fp, const char *path);
int (*rmdir) (file_t *fp, const char *path);
void (*rewinddir)(file_t *fp, aos_dir_t *dir);
long (*telldir) (file_t *fp, aos_dir_t *dir);
void (*seekdir) (file_t *fp, aos_dir_t *dir, long loc);
int (*ioctl) (file_t *fp, int cmd, unsigned long arg);
int (*statfs) (file_t *fp, const char *path, struct statfs *suf);
int (*access) (file_t *fp, const char *path, int amode);
};
int inode_init(void);
int inode_alloc(void);
int inode_del(inode_t *node);
inode_t *inode_open(const char *path);
int inode_ptr_get(int fd, inode_t **node);
int inode_avail_count(void);
void inode_ref(inode_t *);
void inode_unref(inode_t *);
int inode_busy(inode_t *);
int inode_reserve(const char *path, inode_t **inode);
int inode_release(const char *path);
#ifdef __cplusplus
}
#endif
#endif /*VFS_INODE_H*/
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef VFS_DRIVER_H
#define VFS_DRIVER_H
#include <vfs_inode.h>
#ifdef __cplusplus
extern "C" {
#endif
int aos_register_driver(const char *path, file_ops_t *fops, void *arg);
int aos_unregister_driver(const char *path);
int aos_register_fs(const char *path, fs_ops_t *fops, void *arg);
int aos_unregister_fs(const char *path);
#ifdef __cplusplus
}
#endif
#endif /*VFS_DRIVER_H*/
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef VFS_TYPES_H
#define VFS_TYPES_H
typedef struct {
uint16_t st_mode;
uint32_t st_size;
} vfs_stat_t;
typedef struct {
int32_t d_ino; /* file number */
uint8_t d_type; /* type of file */
char *d_name; /* file name */
} vfs_dirent_t;
typedef struct {
int32_t dd_vfs_fd;
int32_t dd_rsv;
} vfs_dir_t;
typedef struct {
int32_t f_type; /* fs type */
int32_t f_bsize; /* optimized transport block size */
int32_t f_blocks; /* total blocks */
int32_t f_bfree; /* available blocks */
int32_t f_bavail; /* number of blocks that non-super users can acquire */
int32_t f_files; /* total number of file nodes */
int32_t f_ffree; /* available file nodes */
int32_t f_fsid; /* fs id */
int32_t f_namelen; /* max file name length */
} vfs_statfs_t;
typedef void (*vfs_poll_notify_t)(void *fds, void *arg);
typedef struct vfs_file_ops vfs_file_ops_t;
typedef struct vfs_filesystem_ops vfs_filesystem_ops_t;
union vfs_inode_ops_t {
const vfs_file_ops_t *i_ops;
const vfs_filesystem_ops_t *i_fops;
};
typedef struct {
union vfs_inode_ops_t ops; /* inode operations */
void *i_arg; /* per inode private data */
char *i_name; /* name of inode */
int32_t i_flags; /* flags for inode */
uint8_t type; /* type for inode */
uint8_t refs; /* refs for inode */
} vfs_inode_t;
typedef struct {
vfs_inode_t *node; /* node for file or device */
void *f_arg; /* arguments for file or device */
uint32_t offset; /* offset of the file */
} vfs_file_t;
struct vfs_file_ops {
int32_t (*open) (vfs_inode_t *node, vfs_file_t *fp);
int32_t (*close) (vfs_file_t *fp);
int32_t (*read) (vfs_file_t *fp, void *buf, uint32_t nbytes);
int32_t (*write) (vfs_file_t *fp, const void *buf, uint32_t nbytes);
int32_t (*ioctl) (vfs_file_t *fp, int32_t cmd, uint32_t arg);
int32_t (*poll) (vfs_file_t *fp, int32_t flag, vfs_poll_notify_t notify, void *fds, void *arg);
};
struct vfs_filesystem_ops {
int32_t (*open) (vfs_file_t *fp, const char *path, int32_t flags);
int32_t (*close) (vfs_file_t *fp);
int32_t (*read) (vfs_file_t *fp, char *buf, uint32_t len);
int32_t (*write) (vfs_file_t *fp, const char *buf, uint32_t len);
uint32_t (*lseek) (vfs_file_t *fp, uint32_t off, int32_t whence);
int32_t (*sync) (vfs_file_t *fp);
int32_t (*stat) (vfs_file_t *fp, const char *path, vfs_stat_t *st);
int32_t (*unlink) (vfs_file_t *fp, const char *path);
int32_t (*rename) (vfs_file_t *fp, const char *oldpath, const char *newpath);
vfs_dir_t *(*opendir) (vfs_file_t *fp, const char *path);
vfs_dirent_t *(*readdir) (vfs_file_t *fp, vfs_dir_t *dir);
int32_t (*closedir) (vfs_file_t *fp, vfs_dir_t *dir);
int32_t (*mkdir) (vfs_file_t *fp, const char *path);
int32_t (*rmdir) (vfs_file_t *fp, const char *path);
void (*rewinddir) (vfs_file_t *fp, vfs_dir_t *dir);
int32_t (*telldir) (vfs_file_t *fp, vfs_dir_t *dir);
void (*seekdir) (vfs_file_t *fp, vfs_dir_t *dir, int32_t loc);
int32_t (*ioctl) (vfs_file_t *fp, int32_t cmd, uint32_t arg);
int32_t (*statfs) (vfs_file_t *fp, const char *path, vfs_statfs_t *suf);
int32_t (*access) (vfs_file_t *fp, const char *path, int32_t amode);
};
#endif /* VFS_TYPES_H */
此差异已折叠。
NAME := vfs
$(NAME)_MBINS_TYPE := kernel
$(NAME)_VERSION := 0.0.1
$(NAME)_SUMMARY :=
$(NAME)_VERSION := 0.0.1
$(NAME)_SUMMARY := Virtual File System
$(NAME)_SOURCES := vfs.c
$(NAME)_SOURCES += vfs_file.c
$(NAME)_SOURCES += vfs_inode.c
$(NAME)_SOURCES += vfs_register.c
$(NAME)_SOURCES += vfs_adapt.c
$(NAME)_SOURCES += vfs_device_adc.c \
vfs_device_gpio.c \
vfs_device_i2c.c \
vfs_device_pwm.c \
vfs_device_rtc.c \
vfs_device_spi.c \
vfs_device_uart.c \
vfs_device_wdg.c
ifeq ($(HOST_ARCH),linux)
$(NAME)_DEFINES += IO_NEED_TRAP
......@@ -24,7 +33,7 @@ else ifeq ($(COMPILER),rvct)
GLOBAL_DEFINES += __BSD_VISIBLE
endif
GLOBAL_INCLUDES += include
GLOBAL_INCLUDES += . include
GLOBAL_DEFINES += AOS_VFS
$(NAME)_COMPONENTS += kernel.fs.vfs.device
AOS_VFS ?= 1
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <stdint.h>
#include "k_api.h"
#include "vfs_adapt.h"
void *vfs_lock_create(void)
{
int32_t ret;
kmutex_t *m;
m = krhino_mm_alloc(sizeof(kmutex_t));
if (m == NULL) {
return NULL;
}
ret = krhino_mutex_create(m, "VFS");
if (ret != RHINO_SUCCESS) {
krhino_mm_free(m);
return NULL;
}
return (void *)m;
}
int32_t vfs_lock_free(void *lock)
{
int32_t ret;
kmutex_t *m = (kmutex_t *)lock;
if (m == NULL) {
return -1;
}
ret = krhino_mutex_del(m);
if (ret != RHINO_SUCCESS) {
return ret;
}
krhino_mm_free(m);
return ret;
}
int32_t vfs_lock(void *lock)
{
return krhino_mutex_lock((kmutex_t *)lock, RHINO_WAIT_FOREVER);
}
int32_t vfs_unlock(void *lock)
{
return krhino_mutex_unlock((kmutex_t *)lock);
}
void *vfs_malloc(uint32_t size)
{
return krhino_mm_alloc(size);
}
void vfs_free(void *ptr)
{
krhino_mm_free(ptr);
}
......@@ -2,48 +2,54 @@
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include "device/vfs_adc.h"
#include <stdint.h>
#include "hal/soc/soc.h"
#include "vfs_err.h"
#include "vfs_api.h"
static int32_t vfs_adc_open(vfs_inode_t *node, vfs_file_t *fp);
static int32_t vfs_adc_close(vfs_file_t *fp);
static int32_t vfs_adc_read(vfs_file_t *fp, void *buf, uint32_t nbytes);
/* adc driver struct */
const struct file_ops adc_ops =
const vfs_file_ops_t adc_ops =
{
.open = vfs_adc_open,
.read = vfs_adc_read,
.open = vfs_adc_open,
.read = vfs_adc_read,
.close = vfs_adc_close
};
int vfs_adc_open(inode_t *inode, file_t *fp)
static int32_t vfs_adc_open(vfs_inode_t *node, vfs_file_t *fp)
{
int ret = -1; /* return value */
adc_dev_t *adc_dev = NULL; /* adc device pointer */
int32_t ret = VFS_ERR_NOSYS; /* return value */
adc_dev_t *pdev = NULL; /* adc device pointer */
/* check empty pointer. */
/* check empty pointer */
if ((fp != NULL) && (fp->node != NULL)) {
/* Initialize if the device is first opened. */
/* Initialize if the device is first opened */
if (fp->node->refs == 1) {
/* get the device pointer. */
adc_dev = (adc_dev_t *)(fp->node->i_arg);
/* get the device pointer */
pdev = (adc_dev_t *)(fp->node->i_arg);
/* init adc device. */
ret = hal_adc_init(adc_dev);
/* init adc device */
ret = hal_adc_init(pdev);
} else {
ret = VFS_SUCCESS;
ret = VFS_OK;
}
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
return ret;
}
int vfs_adc_close(file_t *fp)
static int32_t vfs_adc_close(vfs_file_t *fp)
{
int ret = -1; /* return value */
adc_dev_t *adc_dev = NULL; /* adc device pointer */
int32_t ret = VFS_ERR_NOSYS; /* return value */
adc_dev_t *pdev = NULL; /* adc device pointer */
/* check empty pointer. */
if ((fp != NULL) && (fp->node != NULL)) {
......@@ -52,52 +58,53 @@ int vfs_adc_close(file_t *fp)
if (fp->node->refs == 1) {
/* get the device pointer. */
adc_dev = (adc_dev_t *)(fp->node->i_arg);
pdev = (adc_dev_t *)(fp->node->i_arg);
if (adc_dev != NULL) {
if (pdev != NULL) {
/* turns off an ADC hardwar. */
ret = hal_adc_finalize(adc_dev);
ret = hal_adc_finalize(pdev);
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
} else {
ret = VFS_SUCCESS;
ret = VFS_OK;
}
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
return ret;
}
ssize_t vfs_adc_read(file_t *fp, void *buf, size_t nbytes)
static int32_t vfs_adc_read(vfs_file_t *fp, void *buf, uint32_t nbytes)
{
int ret = -1; /* return value */
adc_dev_t *adc_dev = NULL; /* adc device pointer */
int32_t ret = VFS_ERR_NOSYS; /* return value */
adc_dev_t *pdev = NULL; /* adc device pointer */
/* check empty pointer. */
if ((fp != NULL) && (fp->node != NULL)) {
/* get the device pointer. */
adc_dev = (adc_dev_t *)(fp->node->i_arg);
/* get the device pointer. */
pdev = (adc_dev_t *)(fp->node->i_arg);
if (adc_dev != NULL) {
if (pdev != NULL) {
/* get adc sampled value. */
ret = hal_adc_value_get(adc_dev, buf, HAL_WAIT_FOREVER);
/* get adc sampled value. */
ret = hal_adc_value_get(pdev, buf, HAL_WAIT_FOREVER);
/* If the data is got successfully, set the return
/* If the data is got successfully, set the return
value to nbytes. */
if (ret == 0) {
ret = nbytes;
}
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
return ret;
}
......@@ -2,23 +2,35 @@
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include "device/vfs_gpio.h"
#include <stdint.h>
#include "hal/soc/soc.h"
#include "vfs_err.h"
#include "vfs_api.h"
/* cmd for ioctl */
#define IOCTL_GPIO_OUTPUT_HIGHT 1 /* output hight */
#define IOCTL_GPIO_OUTPUT_LOW 2 /* output low */
#define IOCTL_GPIO_OUTPUT_TOGGLE 3 /* toggle output */
static int32_t vfs_gpio_open(vfs_inode_t *node, vfs_file_t *fp);
static int32_t vfs_gpio_close(vfs_file_t *fp);
static int32_t vfs_gpio_read(vfs_file_t *fp, void *buf, uint32_t nbytes);
static int32_t vfs_gpio_ioctl(vfs_file_t *fp, int32_t cmd, uint32_t arg);
/* gpio driver struct */
const struct file_ops gpio_ops =
const vfs_file_ops_t gpio_ops =
{
.open = vfs_gpio_open,
.open = vfs_gpio_open,
.read = vfs_gpio_read,
.close = vfs_gpio_close,
.read = vfs_gpio_read,
.ioctl = vfs_gpio_ioctl
};
int vfs_gpio_open(inode_t *inode, file_t *fp)
static int32_t vfs_gpio_open(vfs_inode_t *node, vfs_file_t *fp)
{
int ret = -1; /* return value */
gpio_dev_t *gpio_dev = NULL; /* device pointer */
int32_t ret = VFS_ERR_NOSYS; /* return value */
gpio_dev_t *pdev = NULL; /* device pointer */
/* check empty pointer. */
if ((fp != NULL) && (fp->node != NULL)) {
......@@ -27,24 +39,24 @@ int vfs_gpio_open(inode_t *inode, file_t *fp)
if (fp->node->refs == 1) {
/* get the device pointer. */
gpio_dev = (gpio_dev_t *)(fp->node->i_arg);
pdev = (gpio_dev_t *)(fp->node->i_arg);
/* init gpio device. */
ret = hal_gpio_init(gpio_dev);
ret = hal_gpio_init(pdev);
} else {
ret = VFS_SUCCESS;
ret = VFS_OK;
}
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
return ret;
}
int vfs_gpio_close(file_t *fp)
static int32_t vfs_gpio_close(vfs_file_t *fp)
{
int ret = -1; /* return value */
gpio_dev_t *gpio_dev = NULL; /* device pointer */
int32_t ret = VFS_ERR_NOSYS; /* return value */
gpio_dev_t *pdev = NULL; /* device pointer */
/* check empty pointer. */
if ((fp != NULL) && (fp->node != NULL)) {
......@@ -53,87 +65,89 @@ int vfs_gpio_close(file_t *fp)
if (fp->node->refs == 1) {
/* get the device pointer. */
gpio_dev = (gpio_dev_t *)(fp->node->i_arg);
pdev = (gpio_dev_t *)(fp->node->i_arg);
if (gpio_dev != NULL) {
if (pdev != NULL) {
/* turns off hardware. */
ret = hal_gpio_finalize(gpio_dev);
ret = hal_gpio_finalize(pdev);
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
} else {
ret = VFS_SUCCESS;
ret = VFS_OK;
}
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
return ret;
}
ssize_t vfs_gpio_read(file_t *fp, void *buf, size_t nbytes)
static int32_t vfs_gpio_read(vfs_file_t *fp, void *buf, uint32_t nbytes)
{
int ret = -1; /* return value */
gpio_dev_t *gpio_dev = NULL; /* device pointer */
int32_t ret = VFS_ERR_NOSYS; /* return value */
gpio_dev_t *pdev = NULL; /* device pointer */
/* check empty pointer. */
if ((fp != NULL) && (fp->node != NULL)) {
/* get the device pointer. */
gpio_dev = (gpio_dev_t *)(fp->node->i_arg);
pdev = (gpio_dev_t *)(fp->node->i_arg);
if (gpio_dev != NULL) {
if (pdev != NULL) {
/* get data from gpio. */
ret = hal_gpio_input_get(gpio_dev, (uint32_t *)buf);
/* get data from gpio. */
ret = hal_gpio_input_get(pdev, (uint32_t *)buf);
/* If the data is read correctly and the number of read data
/* If the data is read correctly and the number of read data
bytes is not negative, the return value is set to read bytes. */
if (ret == 0) {
ret = sizeof(uint32_t);
}
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
} else {
ret = -EINVAL;
}
ret = VFS_ERR_INVAL;
}
return ret;
}
int vfs_gpio_ioctl(file_t *fp, int cmd, unsigned long arg)
static int32_t vfs_gpio_ioctl(vfs_file_t *fp, int32_t cmd, uint32_t arg)
{
int ret = -1; /* return value */
gpio_dev_t *gpio_dev = NULL; /* device pointer */
int32_t ret = VFS_ERR_NOSYS; /* return value */
gpio_dev_t *pdev = NULL; /* device pointer */
/* check empty pointer. */
if ((fp == NULL) || (fp->node == NULL)) {
return -EINVAL;
return VFS_ERR_INVAL;
}
/* get the device pointer. */
gpio_dev = (gpio_dev_t *)(fp->node->i_arg);
/* get the device pointer. */
pdev = (gpio_dev_t *)(fp->node->i_arg);
if (gpio_dev == NULL) {
return -EINVAL;
if (pdev == NULL) {
return VFS_ERR_INVAL;
}
switch(cmd) {
case IOCTL_GPIO_OUTPUT_HIGHT:
ret = hal_gpio_output_high(gpio_dev); /* output hight */
ret = hal_gpio_output_high(pdev); /* output hight */
break;
case IOCTL_GPIO_OUTPUT_LOW:
ret = hal_gpio_output_low(gpio_dev); /* output low */
ret = hal_gpio_output_low(pdev); /* output low */
break;
case IOCTL_GPIO_OUTPUT_TOGGLE:
ret = hal_gpio_output_toggle(gpio_dev); /* toggle output */
break;
ret = hal_gpio_output_toggle(pdev); /* toggle output */
break;
default:
ret = -EINVAL;
ret = VFS_ERR_INVAL;
break;
}
return ret;
}
......@@ -2,23 +2,30 @@
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include "device/vfs_i2c.h"
#include <stdint.h>
#include "hal/soc/soc.h"
#include "vfs_err.h"
#include "vfs_api.h"
static int32_t vfs_i2c_open(vfs_inode_t *node, vfs_file_t *fp);
static int32_t vfs_i2c_close(vfs_file_t *fp);
static int32_t vfs_i2c_read(vfs_file_t *fp, void *buf, uint32_t nbytes);
static int32_t vfs_i2c_write(vfs_file_t *fp, const void *buf, uint32_t nbytes);
/* i2c driver struct */
const struct file_ops i2c_ops =
const vfs_file_ops_t i2c_ops =
{
.open = vfs_i2c_open,
.open = vfs_i2c_open,
.read = vfs_i2c_read,
.close = vfs_i2c_close,
.read = vfs_i2c_read,
.write = vfs_i2c_write
};
int vfs_i2c_open(inode_t *inode, file_t *fp)
static int32_t vfs_i2c_open(vfs_inode_t *node, vfs_file_t *fp)
{
int ret = -1; /* return value */
i2c_dev_t *i2c_dev = NULL; /* device pointer */
int32_t ret = VFS_ERR_NOSYS; /* return value */
i2c_dev_t *pdev = NULL; /* device pointer */
/* check empty pointer. */
if ((fp != NULL) && (fp->node != NULL)) {
......@@ -27,24 +34,24 @@ int vfs_i2c_open(inode_t *inode, file_t *fp)
if (fp->node->refs == 1) {
/* get the device pointer. */
i2c_dev = (i2c_dev_t *)(fp->node->i_arg);
pdev = (i2c_dev_t *)(fp->node->i_arg);
/* init i2c device. */
ret = hal_i2c_init(i2c_dev);
ret = hal_i2c_init(pdev);
} else {
ret = VFS_SUCCESS;
ret = VFS_OK;
}
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
return ret;
}
int vfs_i2c_close(file_t *fp)
static int32_t vfs_i2c_close(vfs_file_t *fp)
{
int ret = -1; /* return value */
i2c_dev_t *i2c_dev = NULL; /* device pointer */
int32_t ret = VFS_ERR_NOSYS; /* return value */
i2c_dev_t *pdev = NULL; /* device pointer */
/* check empty pointer. */
if ((fp != NULL) && (fp->node != NULL)) {
......@@ -53,90 +60,91 @@ int vfs_i2c_close(file_t *fp)
if (fp->node->refs == 1) {
/* get the device pointer. */
i2c_dev = (i2c_dev_t *)(fp->node->i_arg);
pdev = (i2c_dev_t *)(fp->node->i_arg);
if (i2c_dev != NULL) {
if (pdev != NULL) {
/* turns off hardware. */
ret = hal_i2c_finalize(i2c_dev);
ret = hal_i2c_finalize(pdev);
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
} else {
ret = VFS_SUCCESS;
ret = VFS_OK;
}
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
return ret;
}
ssize_t vfs_i2c_read(file_t *fp, void *buf, size_t nbytes)
static int32_t vfs_i2c_read(vfs_file_t *fp, void *buf, uint32_t nbytes)
{
int ret = -1; /* return value */
i2c_dev_t *i2c_dev = NULL; /* device pointer */
int32_t ret = VFS_ERR_NOSYS; /* return value */
i2c_dev_t *pdev = NULL; /* device pointer */
uint16_t dev_addr = 0; /* dev address */
/* check empty pointer. */
if ((fp != NULL) && (fp->node != NULL)) {
/* get the device pointer. */
i2c_dev = (i2c_dev_t *)(fp->node->i_arg);
pdev = (i2c_dev_t *)(fp->node->i_arg);
if (i2c_dev != NULL) {
if (pdev != NULL) {
/* get the device address. */
dev_addr = i2c_dev->config.dev_addr;
dev_addr = pdev->config.dev_addr;
/* get data from i2c. */
ret = hal_i2c_master_recv(i2c_dev, dev_addr, (uint8_t *)buf, nbytes, HAL_WAIT_FOREVER);
ret = hal_i2c_master_recv(pdev, dev_addr, (uint8_t *)buf, nbytes, HAL_WAIT_FOREVER);
/* If the data is read correctly, the return
value is set to read bytes. */
/* If the data is read correctly, the return value is set to read bytes. */
if (ret == 0) {
ret = nbytes;
}
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
} else {
ret = -EINVAL;
}
ret = VFS_ERR_INVAL;
}
return ret;
}
ssize_t vfs_i2c_write(file_t *fp, const void *buf, size_t nbytes)
static int32_t vfs_i2c_write(vfs_file_t *fp, const void *buf, uint32_t nbytes)
{
int ret = -1; /* return value */
i2c_dev_t *i2c_dev = NULL; /* device pointer */
int32_t ret = VFS_ERR_NOSYS; /* return value */
i2c_dev_t *pdev = NULL; /* device pointer */
uint16_t dev_addr = 0; /* dev address */
/* check empty pointer. */
if ((fp != NULL) && (fp->node != NULL)) {
/* get the device pointer. */
i2c_dev = (i2c_dev_t *)(fp->node->i_arg);
/* get the device pointer. */
pdev = (i2c_dev_t *)(fp->node->i_arg);
if (i2c_dev != NULL) {
if (pdev != NULL) {
/* get the device address. */
dev_addr = i2c_dev->config.dev_addr;
dev_addr = pdev->config.dev_addr;
/* send data from i2c. */
ret = hal_i2c_master_send(i2c_dev, dev_addr, (const uint8_t *)buf, nbytes, HAL_WAIT_FOREVER);
/* send data from i2c. */
ret = hal_i2c_master_send(pdev, dev_addr, (const uint8_t *)buf, nbytes, HAL_WAIT_FOREVER);
/* If the data is sent successfully, set the return
/* If the data is sent successfully, set the return
value to nbytes. */
if (ret == 0) {
ret = nbytes;
}
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
return ret;
......
......@@ -2,22 +2,32 @@
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include "device/vfs_pwm.h"
#include <stdint.h>
#include "hal/soc/soc.h"
#include "vfs_err.h"
#include "vfs_api.h"
/* cmd for ioctl */
#define IOCTL_PWM_OUTPUT_START 1 /* start output pwm */
#define IOCTL_PWM_OUTPUT_STOP 2 /* stop output pwm */
static int32_t vfs_pwm_open(vfs_inode_t *node, vfs_file_t *fp);
static int32_t vfs_pwm_close(vfs_file_t *fp);
static int32_t vfs_pwm_ioctl(vfs_file_t *fp, int32_t cmd, uint32_t arg);
/* pwm driver struct */
const struct file_ops pwm_ops =
const vfs_file_ops_t pwm_ops =
{
.open = vfs_pwm_open,
.open = vfs_pwm_open,
.close = vfs_pwm_close,
.ioctl = vfs_pwm_ioctl
};
int vfs_pwm_open(inode_t *inode, file_t *fp)
static int32_t vfs_pwm_open(vfs_inode_t *node, vfs_file_t *fp)
{
int ret = -1; /* return value */
pwm_dev_t *pwm_dev = NULL; /* device pointer */
int32_t ret = VFS_ERR_NOSYS; /* return value */
pwm_dev_t *pdev = NULL; /* device pointer */
/* check empty pointer. */
if ((fp != NULL) && (fp->node != NULL)) {
......@@ -26,24 +36,24 @@ int vfs_pwm_open(inode_t *inode, file_t *fp)
if (fp->node->refs == 1) {
/* get the device pointer. */
pwm_dev = (pwm_dev_t *)(fp->node->i_arg);
pdev = (pwm_dev_t *)(fp->node->i_arg);
/* init pwm device. */
ret = hal_pwm_init(pwm_dev);
ret = hal_pwm_init(pdev);
} else {
ret = VFS_SUCCESS;
ret = VFS_OK;
}
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
return ret;
}
int vfs_pwm_close(file_t *fp)
static int32_t vfs_pwm_close(vfs_file_t *fp)
{
int ret = -1; /* return value */
pwm_dev_t *pwm_dev = NULL; /* device pointer */
int32_t ret = VFS_ERR_NOSYS; /* return value */
pwm_dev_t *pdev = NULL; /* device pointer */
/* check empty pointer. */
if ((fp != NULL) && (fp->node != NULL)) {
......@@ -52,53 +62,54 @@ int vfs_pwm_close(file_t *fp)
if (fp->node->refs == 1) {
/* get the device pointer. */
pwm_dev = (pwm_dev_t *)(fp->node->i_arg);
pdev = (pwm_dev_t *)(fp->node->i_arg);
if (pwm_dev != NULL) {
if (pdev != NULL) {
/* turns off hardware. */
ret = hal_pwm_finalize(pwm_dev);
ret = hal_pwm_finalize(pdev);
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
} else {
ret = VFS_SUCCESS;
ret = VFS_OK;
}
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
return ret;
}
int vfs_pwm_ioctl(file_t *fp, int cmd, unsigned long arg)
static int32_t vfs_pwm_ioctl(vfs_file_t *fp, int32_t cmd, uint32_t arg)
{
int ret = -1; /* return value */
pwm_dev_t *pwm_dev = NULL; /* device pointer */
int32_t ret = VFS_ERR_NOSYS; /* return value */
pwm_dev_t *pdev = NULL; /* device pointer */
/* check empty pointer. */
if ((fp == NULL) || (fp->node == NULL)) {
return -EINVAL;
return VFS_ERR_INVAL;
}
/* get the device pointer. */
pwm_dev = (pwm_dev_t *)(fp->node->i_arg);
pdev = (pwm_dev_t *)(fp->node->i_arg);
if (pwm_dev == NULL) {
return -EINVAL;
if (pdev == NULL) {
return VFS_ERR_INVAL;
}
switch(cmd) {
case IOCTL_PWM_OUTPUT_START:
ret = hal_pwm_start(pwm_dev);
ret = hal_pwm_start(pdev);
break;
case IOCTL_PWM_OUTPUT_STOP:
ret = hal_pwm_stop(pwm_dev);
ret = hal_pwm_stop(pdev);
break;
default:
ret = -EINVAL;
ret = VFS_ERR_INVAL;
break;
}
return ret;
}
......@@ -2,23 +2,30 @@
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include "device/vfs_rtc.h"
#include <stdint.h>
#include "hal/soc/soc.h"
#include "vfs_err.h"
#include "vfs_api.h"
static int32_t vfs_rtc_open(vfs_inode_t *node, vfs_file_t *fp);
static int32_t vfs_rtc_close(vfs_file_t *fp);
static int32_t vfs_rtc_read(vfs_file_t *fp, void *buf, uint32_t nbytes);
static int32_t vfs_rtc_write(vfs_file_t *fp, const void *buf, uint32_t nbytes);
/* rtc driver struct */
const struct file_ops rtc_ops =
const vfs_file_ops_t rtc_ops =
{
.open = vfs_rtc_open,
.open = vfs_rtc_open,
.read = vfs_rtc_read,
.close = vfs_rtc_close,
.read = vfs_rtc_read,
.write = vfs_rtc_write
};
int vfs_rtc_open(inode_t *inode, file_t *fp)
static int32_t vfs_rtc_open(vfs_inode_t *node, vfs_file_t *fp)
{
int ret = -1; /* return value */
rtc_dev_t *rtc_dev = NULL; /* device pointer */
int32_t ret = VFS_ERR_NOSYS; /* return value */
rtc_dev_t *pdev = NULL; /* device pointer */
/* check empty pointer. */
if ((fp != NULL) && (fp->node != NULL)) {
......@@ -27,24 +34,24 @@ int vfs_rtc_open(inode_t *inode, file_t *fp)
if (fp->node->refs == 1) {
/* get the device pointer. */
rtc_dev = (rtc_dev_t *)(fp->node->i_arg);
pdev = (rtc_dev_t *)(fp->node->i_arg);
/* init rtc device. */
ret = hal_rtc_init(rtc_dev);
ret = hal_rtc_init(pdev);
} else {
ret = VFS_SUCCESS;
ret = VFS_OK;
}
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
return ret;
}
int vfs_rtc_close(file_t *fp)
static int32_t vfs_rtc_close(vfs_file_t *fp)
{
int ret = -1; /* return value */
rtc_dev_t *rtc_dev = NULL; /* device pointer */
int32_t ret = VFS_ERR_NOSYS; /* return value */
rtc_dev_t *pdev = NULL; /* device pointer */
/* check empty pointer. */
if ((fp != NULL) && (fp->node != NULL)) {
......@@ -53,82 +60,83 @@ int vfs_rtc_close(file_t *fp)
if (fp->node->refs == 1) {
/* get the device pointer. */
rtc_dev = (rtc_dev_t *)(fp->node->i_arg);
pdev = (rtc_dev_t *)(fp->node->i_arg);
if (pdev != NULL) {
if (rtc_dev != NULL) {
/* turns off hardware. */
ret = hal_rtc_finalize(rtc_dev);
ret = hal_rtc_finalize(pdev);
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
} else {
ret = VFS_SUCCESS;
ret = VFS_OK;
}
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
return ret;
}
ssize_t vfs_rtc_read(file_t *fp, void *buf, size_t nbytes)
static int32_t vfs_rtc_read(vfs_file_t *fp, void *buf, uint32_t nbytes)
{
int ret = -1; /* return value */
rtc_dev_t *rtc_dev = NULL; /* device pointer */
int32_t ret = VFS_ERR_NOSYS; /* return value */
rtc_dev_t *pdev = NULL; /* device pointer */
/* check empty pointer. */
if ((fp != NULL) && (fp->node != NULL) && (nbytes >= sizeof(rtc_time_t))) {
/* get the device pointer. */
rtc_dev = (rtc_dev_t *)(fp->node->i_arg);
pdev = (rtc_dev_t *)(fp->node->i_arg);
if (rtc_dev != NULL) {
if (pdev != NULL) {
/* get data from rtc. */
ret = hal_rtc_get_time(rtc_dev, (rtc_time_t *)buf);
/* set rtc time. */
ret = hal_rtc_get_time(pdev, (rtc_time_t *)buf);
/* If the data is read correctly the return value is set to nbytes. */
/* If the time is set successfully, set the return
value to nbytes. */
if (ret == 0) {
ret = nbytes;
}
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
return ret;
}
ssize_t vfs_rtc_write(file_t *fp, const void *buf, size_t nbytes)
static int32_t vfs_rtc_write(vfs_file_t *fp, const void *buf, uint32_t nbytes)
{
int ret = -1; /* return value */
rtc_dev_t *rtc_dev = NULL; /* device pointer */
int32_t ret = VFS_ERR_NOSYS; /* return value */
rtc_dev_t *pdev = NULL; /* device pointer */
/* check empty pointer. */
if ((fp != NULL) && (fp->node != NULL) && (nbytes >= sizeof(rtc_time_t))) {
/* get the device pointer. */
rtc_dev = (rtc_dev_t *)(fp->node->i_arg);
pdev = (rtc_dev_t *)(fp->node->i_arg);
if (rtc_dev != NULL) {
if (pdev != NULL) {
/* set rtc time. */
ret = hal_rtc_set_time(rtc_dev, (const rtc_time_t *)buf);
ret = hal_rtc_set_time(pdev, (const rtc_time_t *)buf);
/* If the time is set successfully, set the return
/* If the time is set successfully, set the return
value to nbytes. */
if (ret == 0) {
ret = nbytes;
}
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
} else {
ret = -EINVAL;
}
ret = VFS_ERR_INVAL;
}
return ret;
}
......@@ -2,23 +2,30 @@
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include "device/vfs_spi.h"
#include <stdint.h>
#include "hal/soc/soc.h"
#include "vfs_err.h"
#include "vfs_api.h"
static int32_t vfs_spi_open(vfs_inode_t *node, vfs_file_t *fp);
static int32_t vfs_spi_close(vfs_file_t *fp);
static int32_t vfs_spi_read(vfs_file_t *fp, void *buf, uint32_t nbytes);
static int32_t vfs_spi_write(vfs_file_t *fp, const void *buf, uint32_t nbytes);
/* spi driver struct */
const struct file_ops spi_ops =
const vfs_file_ops_t spi_ops =
{
.open = vfs_spi_open,
.open = vfs_spi_open,
.read = vfs_spi_read,
.close = vfs_spi_close,
.read = vfs_spi_read,
.write = vfs_spi_write
};
int vfs_spi_open(inode_t *inode, file_t *fp)
static int32_t vfs_spi_open(vfs_inode_t *node, vfs_file_t *fp)
{
int ret = -1; /* return value */
spi_dev_t *spi_dev = NULL; /* device pointer */
int32_t ret = VFS_ERR_NOSYS; /* return value */
spi_dev_t *pdev = NULL; /* device pointer */
/* check empty pointer. */
if ((fp != NULL) && (fp->node != NULL)) {
......@@ -27,24 +34,24 @@ int vfs_spi_open(inode_t *inode, file_t *fp)
if (fp->node->refs == 1) {
/* get the device pointer. */
spi_dev = (spi_dev_t *)(fp->node->i_arg);
pdev = (spi_dev_t *)(fp->node->i_arg);
/* init spi device. */
ret = hal_spi_init(spi_dev);
ret = hal_spi_init(pdev);
} else {
ret = VFS_SUCCESS;
ret = VFS_OK;
}
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
return ret;
}
int vfs_spi_close(file_t *fp)
static int32_t vfs_spi_close(vfs_file_t *fp)
{
int ret = -1; /* return value */
spi_dev_t *spi_dev = NULL; /* device pointer */
int32_t ret = VFS_ERR_NOSYS; /* return value */
spi_dev_t *pdev = NULL; /* device pointer */
/* check empty pointer. */
if ((fp != NULL) && (fp->node != NULL)) {
......@@ -53,82 +60,82 @@ int vfs_spi_close(file_t *fp)
if (fp->node->refs == 1) {
/* get the device pointer. */
spi_dev = (spi_dev_t *)(fp->node->i_arg);
pdev = (spi_dev_t *)(fp->node->i_arg);
if (spi_dev != NULL) {
if (pdev != NULL) {
/* turns off hardware. */
ret = hal_spi_finalize(spi_dev);
ret = hal_spi_finalize(pdev);
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
} else {
ret = VFS_SUCCESS;
ret = VFS_OK;
}
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
return ret;
}
ssize_t vfs_spi_read(file_t *fp, void *buf, size_t nbytes)
static int32_t vfs_spi_read(vfs_file_t *fp, void *buf, uint32_t nbytes)
{
int ret = -1; /* return value */
spi_dev_t *spi_dev = NULL; /* device pointer */
int32_t ret = VFS_ERR_NOSYS; /* return value */
spi_dev_t *pdev = NULL; /* device pointer */
/* check empty pointer. */
if ((fp != NULL) && (fp->node != NULL)) {
/* get the device pointer. */
spi_dev = (spi_dev_t *)(fp->node->i_arg);
pdev = (spi_dev_t *)(fp->node->i_arg);
if (spi_dev != NULL) {
if (pdev != NULL) {
/* get data from spi. */
ret = hal_spi_recv(spi_dev, (unsigned char *)buf, nbytes, HAL_WAIT_FOREVER);
ret = hal_spi_recv(pdev, (unsigned char *)buf, nbytes, HAL_WAIT_FOREVER);
/* If the data is read correctly and the number of read data
/* If the data is read correctly and the number of read data
bytes is not negative, the return value is set to read bytes. */
if (ret == 0) {
ret = nbytes;
}
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
} else {
ret = -EINVAL;
}
ret = VFS_ERR_INVAL;
}
return ret;
}
ssize_t vfs_spi_write(file_t *fp, const void *buf, size_t nbytes)
static int32_t vfs_spi_write(vfs_file_t *fp, const void *buf, uint32_t nbytes)
{
int ret = -1; /* return value */
spi_dev_t *spi_dev = NULL; /* device pointer */
int32_t ret = VFS_ERR_NOSYS; /* return value */
spi_dev_t *pdev = NULL; /* device pointer */
/* check empty pointer. */
if ((fp != NULL) && (fp->node != NULL)) {
/* get the device pointer. */
spi_dev = (spi_dev_t *)(fp->node->i_arg);
pdev = (spi_dev_t *)(fp->node->i_arg);
if (spi_dev != NULL) {
if (pdev != NULL) {
/* send data from spi. */
ret = hal_spi_send(spi_dev, (const uint8_t *)buf, nbytes, HAL_WAIT_FOREVER);
ret = hal_spi_send(pdev, (const uint8_t *)buf, nbytes, HAL_WAIT_FOREVER);
/* If the data is sent successfully, set the return
/* If the data is sent successfully, set the return
value to nbytes. */
if (ret == 0) {
ret = nbytes;
}
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
return ret;
......
......@@ -2,23 +2,30 @@
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include "device/vfs_uart.h"
#include <stdint.h>
#include "hal/soc/soc.h"
#include "vfs_err.h"
#include "vfs_api.h"
static int32_t vfs_uart_open(vfs_inode_t *node, vfs_file_t *fp);
static int32_t vfs_uart_close(vfs_file_t *fp);
static int32_t vfs_uart_read(vfs_file_t *fp, void *buf, uint32_t nbytes);
static int32_t vfs_uart_write(vfs_file_t *fp, const void *buf, uint32_t nbytes);
/* uart driver struct */
const struct file_ops uart_ops =
const vfs_file_ops_t uart_ops =
{
.open = vfs_uart_open,
.open = vfs_uart_open,
.read = vfs_uart_read,
.close = vfs_uart_close,
.read = vfs_uart_read,
.write = vfs_uart_write
};
int vfs_uart_open(inode_t *inode, file_t *fp)
static int32_t vfs_uart_open(vfs_inode_t *node, vfs_file_t *fp)
{
int ret = -1; /* return value */
uart_dev_t *uart_dev = NULL; /* device pointer */
int32_t ret = VFS_ERR_NOSYS; /* return value */
uart_dev_t *pdev = NULL; /* device pointer */
/* check empty pointer. */
if ((fp != NULL) && (fp->node != NULL)) {
......@@ -27,24 +34,24 @@ int vfs_uart_open(inode_t *inode, file_t *fp)
if (fp->node->refs == 1) {
/* get the device pointer. */
uart_dev = (uart_dev_t *)(fp->node->i_arg);
pdev = (uart_dev_t *)(fp->node->i_arg);
/* init uart device. */
ret = hal_uart_init(uart_dev);
ret = hal_uart_init(pdev);
} else {
ret = VFS_SUCCESS;
ret = VFS_OK;
}
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
return ret;
}
int vfs_uart_close(file_t *fp)
static int32_t vfs_uart_close(vfs_file_t *fp)
{
int ret = -1; /* return value */
uart_dev_t *uart_dev = NULL; /* device pointer */
int32_t ret = VFS_ERR_NOSYS; /* return value */
uart_dev_t *pdev = NULL; /* device pointer */
/* check empty pointer. */
if ((fp != NULL) && (fp->node != NULL)) {
......@@ -53,82 +60,83 @@ int vfs_uart_close(file_t *fp)
if (fp->node->refs == 1) {
/* get the device pointer. */
uart_dev = (uart_dev_t *)(fp->node->i_arg);
pdev = (uart_dev_t *)(fp->node->i_arg);
if (uart_dev != NULL) {
if (pdev != NULL) {
/* turns off hardware. */
ret = hal_uart_finalize(uart_dev);
ret = hal_uart_finalize(pdev);
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
} else {
ret = VFS_SUCCESS;
ret = VFS_OK;
}
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
return ret;
}
ssize_t vfs_uart_read(file_t *fp, void *buf, size_t nbytes)
static int32_t vfs_uart_read(vfs_file_t *fp, void *buf, uint32_t nbytes)
{
int ret = -1; /* return value */
uart_dev_t *uart_dev = NULL; /* device pointer */
int32_t ret = VFS_ERR_NOSYS; /* return value */
uart_dev_t *pdev = NULL; /* device pointer */
uint32_t recv_bytes = 0; /* number of bytes received */
/* check empty pointer. */
if ((fp != NULL) && (fp->node != NULL)) {
/* get the device pointer. */
uart_dev = (uart_dev_t *)(fp->node->i_arg);
pdev = (uart_dev_t *)(fp->node->i_arg);
if (uart_dev != NULL) {
if (pdev != NULL) {
/* get data from uart. */
ret = hal_uart_recv_II(uart_dev, buf, nbytes, &recv_bytes, HAL_WAIT_FOREVER);
ret = hal_uart_recv_II(pdev, buf, nbytes, &recv_bytes, HAL_WAIT_FOREVER);
/* If the data is read correctly the return value is set to read bytes. */
/* If the data is read correctly the return value is set to read bytes. */
if (ret == 0) {
ret = recv_bytes;
}
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
} else {
ret = -EINVAL;
}
ret = VFS_ERR_INVAL;
}
return ret;
}
ssize_t vfs_uart_write(file_t *fp, const void *buf, size_t nbytes)
static int32_t vfs_uart_write(vfs_file_t *fp, const void *buf, uint32_t nbytes)
{
int ret = -1; /* return value */
uart_dev_t *uart_dev = NULL; /* device pointer */
int32_t ret = VFS_ERR_NOSYS; /* return value */
uart_dev_t *pdev = NULL; /* device pointer */
/* check empty pointer. */
if ((fp != NULL) && (fp->node != NULL)) {
/* get the device pointer. */
uart_dev = (uart_dev_t *)(fp->node->i_arg);
pdev = (uart_dev_t *)(fp->node->i_arg);
if (uart_dev != NULL) {
if (pdev != NULL) {
/* send data from uart. */
ret = hal_uart_send(uart_dev, buf, nbytes, HAL_WAIT_FOREVER);
/* send data from uart. */
ret = hal_uart_send(pdev, buf, nbytes, HAL_WAIT_FOREVER);
/* If the data is sent successfully, set the return
/* If the data is sent successfully, set the return
value to nbytes. */
if (ret == 0) {
ret = nbytes;
}
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
return ret;
......
......@@ -2,22 +2,31 @@
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include "device/vfs_wdg.h"
#include <stdint.h>
#include "hal/soc/soc.h"
#include "vfs_err.h"
/* wdg driver struct */
const struct file_ops wdg_ops =
#include "vfs_api.h"
/* cmd for ioctl */
#define IOCTL_WDG_RELOAD 1 /* reload watchdog */
static int32_t vfs_wdg_open(vfs_inode_t *node, vfs_file_t *fp);
static int32_t vfs_wdg_close(vfs_file_t *fp);
static int32_t vfs_wdg_ioctl(vfs_file_t *fp, int32_t cmd, uint32_t arg);
/* wgd driver struct */
const vfs_file_ops_t wdg_ops =
{
.open = vfs_wdg_open,
.open = vfs_wdg_open,
.close = vfs_wdg_close,
.ioctl = vfs_wdg_ioctl
};
int vfs_wdg_open(inode_t *inode, file_t *fp)
static int32_t vfs_wdg_open(vfs_inode_t *node, vfs_file_t *fp)
{
int ret = -1; /* return value */
wdg_dev_t *wdg_dev = NULL; /* device pointer */
int32_t ret = VFS_ERR_NOSYS; /* return value */
wdg_dev_t *pdev = NULL; /* adc device pointer */
/* check empty pointer. */
if ((fp != NULL) && (fp->node != NULL)) {
......@@ -26,24 +35,25 @@ int vfs_wdg_open(inode_t *inode, file_t *fp)
if (fp->node->refs == 1) {
/* get the device pointer. */
wdg_dev = (wdg_dev_t *)(fp->node->i_arg);
pdev = (wdg_dev_t *)(fp->node->i_arg);
/* init wdg device. */
ret = hal_wdg_init(wdg_dev);
ret = hal_wdg_init(pdev);
} else {
ret = VFS_SUCCESS;
ret = VFS_OK;
}
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
return ret;
}
int vfs_wdg_close(file_t *fp)
static int32_t vfs_wdg_close(vfs_file_t *fp)
{
int ret = -1; /* return value */
wdg_dev_t *wdg_dev = NULL; /* device pointer */
int32_t ret = VFS_ERR_NOSYS; /* return value */
wdg_dev_t *pdev = NULL; /* adc device pointer */
/* check empty pointer. */
if ((fp != NULL) && (fp->node != NULL)) {
......@@ -52,49 +62,49 @@ int vfs_wdg_close(file_t *fp)
if (fp->node->refs == 1) {
/* get the device pointer. */
wdg_dev = (wdg_dev_t *)(fp->node->i_arg);
pdev = (wdg_dev_t *)(fp->node->i_arg);
if (wdg_dev != NULL) {
if (pdev != NULL) {
/* turns off hardware. */
ret = hal_wdg_finalize(wdg_dev);
ret = hal_wdg_finalize(pdev);
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
} else {
ret = VFS_SUCCESS;
ret = VFS_OK;
}
} else {
ret = -EINVAL;
ret = VFS_ERR_INVAL;
}
return ret;
}
int vfs_wdg_ioctl(file_t *fp, int cmd, unsigned long arg)
static int32_t vfs_wdg_ioctl(vfs_file_t *fp, int32_t cmd, uint32_t arg)
{
int ret = -1; /* return value */
wdg_dev_t *wdg_dev = NULL; /* device pointer */
int32_t ret = VFS_ERR_NOSYS; /* return value */
wdg_dev_t *pdev = NULL; /* adc device pointer */
/* check empty pointer. */
if ((fp == NULL) || (fp->node == NULL)) {
return -EINVAL;
return VFS_ERR_INVAL;
}
/* get the device pointer. */
wdg_dev = (wdg_dev_t *)(fp->node->i_arg);
pdev = (wdg_dev_t *)(fp->node->i_arg);
if (wdg_dev == NULL) {
return -EINVAL;
if (pdev == NULL) {
return VFS_ERR_INVAL;
}
switch(cmd) {
case IOCTL_WDG_RELOAD:
hal_wdg_reload(wdg_dev);
ret = VFS_SUCCESS;
hal_wdg_reload(pdev);
ret = VFS_OK;
break;
default:
ret = -EINVAL;
ret = VFS_ERR_INVAL;
break;
}
......
......@@ -2,20 +2,48 @@
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <vfs_conf.h>
#include <vfs_err.h>
#include <vfs_inode.h>
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
static file_t files[MAX_FILE_NUM];
#include "vfs_conf.h"
#include "vfs_types.h"
#include "vfs_file.h"
file_t *new_file(inode_t *node)
static vfs_file_t g_files[VFS_MAX_FILE_NUM];
extern void vfs_inode_ref(vfs_inode_t *node);
extern void vfs_inode_unref(vfs_inode_t *node);
int32_t vfs_fd_get(vfs_file_t *file)
{
return (file - g_files) + VFS_FD_OFFSET;
}
vfs_file_t *vfs_file_get(int32_t fd)
{
vfs_file_t *f;
fd -= VFS_FD_OFFSET;
if (fd < 0) {
return NULL;
}
if (fd >= VFS_MAX_FILE_NUM) {
return NULL;
}
f = &g_files[fd];
return f->node ? f : NULL;
}
vfs_file_t *vfs_file_new(vfs_inode_t *node)
{
file_t *f;
int idx;
int32_t idx;
vfs_file_t *f;
for (idx = 0; idx < MAX_FILE_NUM; idx++) {
f = &files[idx];
for (idx = 0; idx < VFS_MAX_FILE_NUM; idx++) {
f = &g_files[idx];
if (f->node == NULL) {
goto got_file;
......@@ -25,38 +53,17 @@ file_t *new_file(inode_t *node)
return NULL;
got_file:
f->node = node;
f->f_arg = NULL;
f->node = node;
f->f_arg = NULL;
f->offset = 0;
inode_ref(node);
return f;
}
vfs_inode_ref(node);
void del_file(file_t *file)
{
inode_unref(file->node);
file->node = NULL;
}
int get_fd(file_t *file)
{
return (file - files) + AOS_CONFIG_VFS_FD_OFFSET;
return f;
}
file_t *get_file(int fd)
void vfs_file_del(vfs_file_t *file)
{
file_t *f;
fd -= AOS_CONFIG_VFS_FD_OFFSET;
vfs_inode_unref(file->node);
if (fd < 0) {
return NULL;
}
if (fd >= MAX_FILE_NUM) {
return NULL;
}
f = &files[fd];
return f->node ? f : NULL;
file->node = NULL;
}
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef VFS_FILE_H
#define VFS_FILE_H
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Get the file descriptor by file structure
*
* @param[in] file pointer the file structure
*
* @return the file descriptor
*
*/
int32_t vfs_fd_get(vfs_file_t *file);
/**
* @brief Get the file structure by file descriptor
*
* @param[in] fd the file descriptor
*
* @return the pointer of the file structure
*
*/
vfs_file_t *vfs_file_get(int32_t fd);
/**
* @brief Create a new file structure
*
* @param[in] node pointer to the inode
*
* @return the pointer of the file structure
*
*/
vfs_file_t *vfs_file_new(vfs_inode_t *node);
/**
* @brief Delete the file structure
*
* @param[in] file pointer to the file structure
*
* @return none
*/
void vfs_file_del(vfs_file_t *file);
#ifdef __cplusplus
}
#endif
#endif /* VFS_FILE_H */
......@@ -2,71 +2,96 @@
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <vfs_conf.h>
#include <vfs_err.h>
#include <vfs_inode.h>
#define VFS_NULL_PARA_CHK(para) do { if (!(para)) return -EINVAL; } while(0)
#include "vfs_conf.h"
#include "vfs_err.h"
#include "vfs_types.h"
#include "vfs_inode.h"
#include "vfs_adapt.h"
static inode_t g_vfs_dev_nodes[AOS_CONFIG_VFS_DEV_NODES];
static vfs_inode_t g_vfs_nodes[VFS_DEVICE_NODES];
int inode_init()
static int32_t vfs_inode_set_name(const char *path, vfs_inode_t **p_node)
{
memset(g_vfs_dev_nodes, 0, sizeof(inode_t) * AOS_CONFIG_VFS_DEV_NODES);
return 0;
int32_t len;
void *mem;
len = strlen(path);
mem = (void *)vfs_malloc(len + 1);
if (mem == NULL) {
return VFS_ERR_NOMEM;
}
memcpy(mem, (const void *)path, len);
(*p_node)->i_name = (char *)mem;
(*p_node)->i_name[len] = '\0';
return VFS_OK;
}
int32_t vfs_inode_init(void)
{
memset(g_vfs_nodes, 0, sizeof(vfs_inode_t) * VFS_DEVICE_NODES);
return VFS_OK;
}
int inode_alloc()
int32_t vfs_inode_alloc(void)
{
int e = 0;
int32_t idx;
for (; e < AOS_CONFIG_VFS_DEV_NODES; e++) {
if (g_vfs_dev_nodes[e].type == VFS_TYPE_NOT_INIT) {
return e;
for (idx = 0; idx < VFS_DEVICE_NODES; idx++) {
if (g_vfs_nodes[idx].type == VFS_TYPE_NOT_INIT) {
return idx;
}
}
return -ENOMEM;
return VFS_ERR_NOMEM;
}
int inode_del(inode_t *node)
int32_t vfs_inode_del(vfs_inode_t *node)
{
if (node->refs > 0) {
return -EBUSY;
return VFS_ERR_BUSY;
}
if (node->refs == 0) {
if (node->i_name != NULL) {
krhino_mm_free(node->i_name);
vfs_free(node->i_name);
}
node->i_name = NULL;
node->i_arg = NULL;
node->i_name = NULL;
node->i_arg = NULL;
node->i_flags = 0;
node->type = VFS_TYPE_NOT_INIT;
node->type = VFS_TYPE_NOT_INIT;
}
return VFS_SUCCESS;
return VFS_OK;
}
inode_t *inode_open(const char *path)
vfs_inode_t *vfs_inode_open(const char *path)
{
int e = 0;
inode_t *node;
int32_t idx;
vfs_inode_t *node;
for (e = 0; e < AOS_CONFIG_VFS_DEV_NODES; e++) {
node = &g_vfs_dev_nodes[e];
for (idx = 0; idx < VFS_DEVICE_NODES; idx++) {
node = &g_vfs_nodes[idx];
if (node->i_name == NULL) {
continue;
}
if (INODE_IS_TYPE(node, VFS_TYPE_FS_DEV)) {
if ((strncmp(node->i_name, path, strlen(node->i_name)) == 0) &&
if ((strncmp(node->i_name, path, strlen(node->i_name)) == 0) &&
(*(path + strlen(node->i_name)) == '/')) {
return node;
}
}
if (strcmp(node->i_name, path) == 0) {
return node;
}
......@@ -75,116 +100,103 @@ inode_t *inode_open(const char *path)
return NULL;
}
int inode_ptr_get(int fd, inode_t **node)
int32_t vfs_inode_ptr_get(int32_t fd, vfs_inode_t **p_node)
{
if (fd < 0 || fd >= AOS_CONFIG_VFS_DEV_NODES) {
return -EINVAL;
if (fd < 0 || fd >= VFS_DEVICE_NODES) {
return VFS_ERR_INVAL;
}
*node = &g_vfs_dev_nodes[fd];
*p_node = &g_vfs_nodes[fd];
return VFS_OK;
}
int32_t vfs_inode_avail_count(void)
{
int32_t idx, count = 0;
return VFS_SUCCESS;
for (idx = 0; idx < VFS_DEVICE_NODES; idx++){
if (g_vfs_nodes[count].type == VFS_TYPE_NOT_INIT) {
count++;
}
}
return count;
}
void inode_ref(inode_t *node)
void vfs_inode_ref(vfs_inode_t *node)
{
node->refs++;
}
void inode_unref(inode_t *node)
void vfs_inode_unref(vfs_inode_t *node)
{
if (node->refs > 0) {
node->refs--;
}
}
int inode_busy(inode_t *node)
int32_t vfs_inode_busy(vfs_inode_t *node)
{
return node->refs > 0;
return (node->refs > 0);
}
int inode_avail_count(void)
int32_t vfs_inode_reserve(const char *path, vfs_inode_t **p_node)
{
int count = 0;
int e = 0;
int32_t ret;
for (; e < AOS_CONFIG_VFS_DEV_NODES; e++) {
if (g_vfs_dev_nodes[count].type == VFS_TYPE_NOT_INIT) {
count++;
}
}
return count;
}
vfs_inode_t *node = NULL;
static int inode_set_name(const char *path, inode_t **inode)
{
size_t len;
void *mem;
len = strlen(path);
mem = (void *)krhino_mm_alloc(len + 1);
if (!mem) {
return -ENOMEM;
if ((path == NULL) || (p_node == NULL)) {
return VFS_ERR_INVAL;
}
memcpy(mem, (const void *)path, len);
(*inode)->i_name = (char *)mem;
(*inode)->i_name[len] = '\0';
return VFS_SUCCESS;
}
int inode_reserve(const char *path, inode_t **inode)
{
int ret;
inode_t *node = NULL;
VFS_NULL_PARA_CHK(path != NULL && inode != NULL);
*inode = NULL;
*p_node = NULL;
/* Handle paths that are interpreted as the root directory */
#ifdef _WIN32
if (path[0] == '\0' || path[1] != ':') {
if ((path[0] == '\0') || (path[1] != ':')) {
#else
if (path[0] == '\0' || path[0] != '/') {
if ((path[0] == '\0') || (path[0] != '/')) {
#endif
return -EINVAL;
return VFS_ERR_INVAL;
}
ret = inode_alloc();
if (ret < 0) {
ret = vfs_inode_alloc();
if (ret != VFS_OK) {
return ret;
}
inode_ptr_get(ret, &node);
vfs_inode_ptr_get(ret, &node);
ret = inode_set_name(path, &node);
if (ret < 0) {
ret = vfs_inode_set_name(path, &node);
if (ret != VFS_OK) {
return ret;
}
*inode = node;
return VFS_SUCCESS;
*p_node = node;
return VFS_OK;
}
int inode_release(const char *path)
int32_t vfs_inode_release(const char *path)
{
int ret;
inode_t *node;
int32_t ret;
vfs_inode_t *node;
VFS_NULL_PARA_CHK(path != NULL);
if (path == NULL) {
return VFS_ERR_INVAL;
}
node = inode_open(path);
node = vfs_inode_open(path);
if (node == NULL) {
return -ENODEV;
return VFS_ERR_NODEV;
}
ret = inode_del(node);
if (ret < 0) {
return ret;
}
ret = vfs_inode_del(node);
return VFS_SUCCESS;
return ret;
}
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef VFS_INODE_H
#define VFS_INODE_H
#ifdef __cplusplus
extern "C" {
#endif
enum {
VFS_TYPE_NOT_INIT,
VFS_TYPE_CHAR_DEV,
VFS_TYPE_BLOCK_DEV,
VFS_TYPE_FS_DEV
};
#define INODE_IS_TYPE(node, t) ((node)->type == (t))
#define INODE_IS_CHAR(node) INODE_IS_TYPE(node, VFS_TYPE_CHAR_DEV)
#define INODE_IS_BLOCK(node) INODE_IS_TYPE(node, VFS_TYPE_BLOCK_DEV)
#define INODE_IS_FS(node) INODE_IS_TYPE(node, VFS_TYPE_FS_DEV)
#define INODE_GET_TYPE(node) ((node)->type)
#define INODE_SET_TYPE(node, t) do { (node)->type = (t); } while(0)
#define INODE_SET_CHAR(node) INODE_SET_TYPE(node, VFS_TYPE_CHAR_DEV)
#define INODE_SET_BLOCK(node) INODE_SET_TYPE(node, VFS_TYPE_BLOCK_DEV)
#define INODE_SET_FS(node) INODE_SET_TYPE(node, VFS_TYPE_FS_DEV)
/**
* @brief Initialize inode
*
* @return 0 on success, negative error on failure
*
*/
int32_t vfs_inode_init(void);
/**
* @brief Alloc a free inode
*
* @return 0 on success, negative error on failure
*
*/
int32_t vfs_inode_alloc(void);
/**
* @brief Delete a inode
*
* @param[in] node pointer to the inode to delete
*
* @return 0 on success, negative error on failure
*
*/
int32_t vfs_inode_del(vfs_inode_t *node);
/**
* @brief Open the inode by path
*
* @param[in] path the path of the inode reference
*
* @return the pointer of the inode, NULL on failure or not found
*
*/
vfs_inode_t *vfs_inode_open(const char *path);
/**
* @brief Get the inode pointer by fd
*
* @param[in] fd the file descriptor
* @param[out] p_node the pointer of the inode pointer
*
* @return 0 on success, negative error on failure
*
*/
int32_t vfs_inode_ptr_get(int32_t fd, vfs_inode_t **p_node);
/**
* @brief Get the available inode count
*
* @return the count of the available inodes
*
*/
int32_t vfs_inode_avail_count(void);
/**
* @brief Add the inode refence count
*
* @param[in] node the pointer of the inode
*
* @return none
*
*/
void vfs_inode_ref(vfs_inode_t *node);
/**
* @brief Dec the inode refence count
*
* @param[in] node the pointer of the inode
*
* @return none
*
*/
void vfs_inode_unref(vfs_inode_t *node);
/**
* @brief Check whether the inode is busy or not
*
* @param[in] node the pointer of the inode
*
* @return 1 on busy, 0 on free
*
*/
int32_t vfs_inode_busy(vfs_inode_t *node);
/**
* @brief Reserve an inode
*
* @param[in] path the path of the inode reference
* @param[in] p_node pointer to the inode pointer
*
* @return 0 on success, negative error on failure
*
*/
int32_t vfs_inode_reserve(const char *path, vfs_inode_t **p_node);
/**
* @brief Release an inode
*
* @param[in] path the path of the inode reference
*
* @return 0 on success, negative error on failure
*
*/
int32_t vfs_inode_release(const char *path);
#ifdef __cplusplus
}
#endif
#endif /* VFS_INODE_H */
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <string.h>
#include <vfs_conf.h>
#include <vfs_err.h>
#include <vfs_register.h>
extern kmutex_t g_vfs_mutex;
int aos_register_driver(const char *path, file_ops_t *ops, void *arg)
{
inode_t *node = NULL;
int err, ret;
err = krhino_mutex_lock(&g_vfs_mutex, RHINO_WAIT_FOREVER);
if (err != 0) {
return err;
}
ret = inode_reserve(path, &node);
if (ret == VFS_SUCCESS) {
/* now populate it with char specific information */
INODE_SET_CHAR(node);
node->ops.i_ops = ops;
node->i_arg = arg;
}
/* step out critical area for type is allocated */
err = krhino_mutex_unlock(&g_vfs_mutex);
if (err != 0) {
if (node->i_name != NULL) {
krhino_mm_free(node->i_name);
}
memset(node, 0, sizeof(inode_t));
return err;
}
return ret;
}
int aos_unregister_driver(const char *path)
{
int err, ret;
err = krhino_mutex_lock(&g_vfs_mutex, RHINO_WAIT_FOREVER);
if (err != 0) {
return err;
}
ret = inode_release(path);
err = krhino_mutex_unlock(&g_vfs_mutex);
if (err != 0) {
return err;
}
return ret;
}
int aos_register_fs(const char *path, fs_ops_t *ops, void *arg)
{
inode_t *node = NULL;
int err, ret;
err = krhino_mutex_lock(&g_vfs_mutex, RHINO_WAIT_FOREVER);
if (err != 0) {
return err;
}
ret = inode_reserve(path, &node);
if (ret == VFS_SUCCESS) {
INODE_SET_FS(node);
node->ops.i_fops = ops;
node->i_arg = arg;
}
err = krhino_mutex_unlock(&g_vfs_mutex);
if (err != 0) {
if (node->i_name != NULL) {
krhino_mm_free(node->i_name);
}
memset(node, 0, sizeof(inode_t));
return err;
}
return ret;
}
int aos_unregister_fs(const char *path)
{
int err, ret;
err = krhino_mutex_lock(&g_vfs_mutex, RHINO_WAIT_FOREVER);
if (err != 0) {
return err;
}
ret = inode_release(path);
err = krhino_mutex_unlock(&g_vfs_mutex);
if (err != 0) {
return err;
}
return ret;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册