Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:18

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup rtems_rfs
0007  *
0008  * @brief RTEMS RFS Device Interface
0009  * 
0010  * This file contains the set of handlers used to map operations on RFS device
0011  * nodes onto calls to the RTEMS Classic API IO Manager.
0012  */
0013 
0014 /*
0015  *  COPYRIGHT (c) 2010 Chris Johns <chrisj@rtems.org>
0016  *
0017  * Redistribution and use in source and binary forms, with or without
0018  * modification, are permitted provided that the following conditions
0019  * are met:
0020  * 1. Redistributions of source code must retain the above copyright
0021  *    notice, this list of conditions and the following disclaimer.
0022  * 2. Redistributions in binary form must reproduce the above copyright
0023  *    notice, this list of conditions and the following disclaimer in the
0024  *    documentation and/or other materials provided with the distribution.
0025  *
0026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0027  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0028  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0029  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0030  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0031  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0032  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0033  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0034  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0035  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0036  * POSSIBILITY OF SUCH DAMAGE.
0037  */
0038 
0039 #ifdef HAVE_CONFIG_H
0040 #include "config.h"
0041 #endif
0042 
0043 #include "rtems-rfs-rtems.h"
0044 
0045 #include <rtems/deviceio.h>
0046 
0047 static void
0048 rtems_rfs_rtems_device_get_major_and_minor ( const rtems_libio_t       *iop,
0049                                              rtems_device_major_number *major,
0050                                              rtems_device_minor_number *minor)
0051 {
0052   *major = iop->data0;
0053   *minor = (rtems_device_minor_number) (uintptr_t) iop->data1;
0054 }
0055 
0056 /**
0057  * This handler maps an open() operation onto rtems_io_open().
0058  *
0059  * @param iop
0060  * @param pathname
0061  * @param flag
0062  * @param mode
0063  * @return int
0064  */
0065 static int
0066 rtems_rfs_rtems_device_open ( rtems_libio_t *iop,
0067                               const char    *pathname,
0068                               int            oflag,
0069                               mode_t         mode)
0070 {
0071   rtems_rfs_file_system*        fs = rtems_rfs_rtems_pathloc_dev (&iop->pathinfo);
0072   rtems_rfs_ino                 ino = rtems_rfs_rtems_get_iop_ino (iop);
0073   rtems_rfs_inode_handle        inode;
0074   rtems_device_major_number     major;
0075   rtems_device_minor_number     minor;
0076   int                           rc;
0077 
0078   rtems_rfs_rtems_lock (fs);
0079 
0080   rc = rtems_rfs_inode_open (fs, ino, &inode, true);
0081   if (rc > 0)
0082   {
0083     rtems_rfs_rtems_unlock (fs);
0084     return rtems_rfs_rtems_error ("device_open: opening inode", rc);
0085   }
0086 
0087   major = rtems_rfs_inode_get_block (&inode, 0);
0088   minor = rtems_rfs_inode_get_block (&inode, 1);
0089 
0090   rc = rtems_rfs_inode_close (fs, &inode);
0091   if (rc > 0)
0092   {
0093     rtems_rfs_rtems_unlock (fs);
0094     return rtems_rfs_rtems_error ("device_open: closing inode", rc);
0095   }
0096 
0097   rtems_rfs_rtems_unlock (fs);
0098 
0099   iop->data0 = major;
0100   iop->data1 = (void *) (uintptr_t) minor;
0101 
0102   return rtems_deviceio_open (iop, pathname, oflag, mode, minor, major);
0103 }
0104 
0105 /**
0106  * This handler maps a close() operation onto rtems_io_close().
0107  *
0108  * @param iop
0109  * @return int
0110  */
0111 
0112 static int
0113 rtems_rfs_rtems_device_close (rtems_libio_t* iop)
0114 {
0115   rtems_device_major_number     major;
0116   rtems_device_minor_number     minor;
0117 
0118   rtems_rfs_rtems_device_get_major_and_minor (iop, &major, &minor);
0119 
0120   return rtems_deviceio_close (iop, major, minor);
0121 }
0122 
0123 /**
0124  * This handler maps a read() operation onto rtems_io_read().
0125  *
0126  * @param iop
0127  * @param buffer
0128  * @param count
0129  * @return ssize_t
0130  */
0131 
0132 static ssize_t
0133 rtems_rfs_rtems_device_read (rtems_libio_t* iop, void* buffer, size_t count)
0134 {
0135   rtems_device_major_number major;
0136   rtems_device_minor_number minor;
0137 
0138   rtems_rfs_rtems_device_get_major_and_minor (iop, &major, &minor);
0139 
0140   return rtems_deviceio_read (iop, buffer, count, major, minor);
0141 }
0142 
0143 /*
0144  * This handler maps a write() operation onto rtems_io_write().
0145  *
0146  * @param iop
0147  * @param buffer
0148  * @param count
0149  * @return ssize_t
0150  */
0151 
0152 static ssize_t
0153 rtems_rfs_rtems_device_write (rtems_libio_t* iop,
0154                               const void*    buffer,
0155                               size_t         count)
0156 {
0157   rtems_device_major_number major;
0158   rtems_device_minor_number minor;
0159 
0160   rtems_rfs_rtems_device_get_major_and_minor (iop, &major, &minor);
0161 
0162   return rtems_deviceio_write (iop, buffer, count, major, minor);
0163 }
0164 
0165 /**
0166  * This handler maps an ioctl() operation onto rtems_io_ioctl().
0167  *
0168  * @param iop
0169  * @param command
0170  * @param buffer
0171  * @return int
0172  */
0173 
0174 static int
0175 rtems_rfs_rtems_device_ioctl (rtems_libio_t*  iop,
0176                               ioctl_command_t command,
0177                               void*           buffer)
0178 {
0179   rtems_device_major_number major;
0180   rtems_device_minor_number minor;
0181 
0182   rtems_rfs_rtems_device_get_major_and_minor (iop, &major, &minor);
0183 
0184   return rtems_deviceio_control (iop, command, buffer, major, minor);
0185 }
0186 
0187 /**
0188  * The consumes the truncate call. You cannot truncate device files.
0189  *
0190  * @param iop
0191  * @param length
0192  * @return int
0193  */
0194 
0195 static int
0196 rtems_rfs_rtems_device_ftruncate (rtems_libio_t* iop, off_t length)
0197 {
0198   return 0;
0199 }
0200 
0201 /*
0202  *  Handler table for RFS device nodes
0203  */
0204 
0205 const rtems_filesystem_file_handlers_r rtems_rfs_rtems_device_handlers = {
0206   .open_h      = rtems_rfs_rtems_device_open,
0207   .close_h     = rtems_rfs_rtems_device_close,
0208   .read_h      = rtems_rfs_rtems_device_read,
0209   .write_h     = rtems_rfs_rtems_device_write,
0210   .ioctl_h     = rtems_rfs_rtems_device_ioctl,
0211   .lseek_h     = rtems_filesystem_default_lseek_file,
0212   .fstat_h     = rtems_rfs_rtems_fstat,
0213   .ftruncate_h = rtems_rfs_rtems_device_ftruncate,
0214   .fsync_h     = rtems_filesystem_default_fsync_or_fdatasync,
0215   .fdatasync_h = rtems_filesystem_default_fsync_or_fdatasync,
0216   .fcntl_h     = rtems_filesystem_default_fcntl,
0217   .kqfilter_h  = rtems_filesystem_default_kqfilter,
0218   .mmap_h      = rtems_filesystem_default_mmap,
0219   .poll_h      = rtems_filesystem_default_poll,
0220   .readv_h     = rtems_filesystem_default_readv,
0221   .writev_h    = rtems_filesystem_default_writev
0222 };