Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  *  @file
0005  *
0006  *  @brief RTEMS DeviceIO Support
0007  *  @ingroup Device
0008  */
0009 
0010 /*
0011  *  COPYRIGHT (c) 1989-2013.
0012  *  On-Line Applications Research Corporation (OAR).
0013  *
0014  * Redistribution and use in source and binary forms, with or without
0015  * modification, are permitted provided that the following conditions
0016  * are met:
0017  * 1. Redistributions of source code must retain the above copyright
0018  *    notice, this list of conditions and the following disclaimer.
0019  * 2. Redistributions in binary form must reproduce the above copyright
0020  *    notice, this list of conditions and the following disclaimer in the
0021  *    documentation and/or other materials provided with the distribution.
0022  *
0023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0024  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0026  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0027  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0028  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0029  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0032  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0033  * POSSIBILITY OF SUCH DAMAGE.
0034  */
0035 
0036 #ifdef HAVE_CONFIG_H
0037 #include "config.h"
0038 #endif
0039 
0040 #include <rtems/deviceio.h>
0041 #include <rtems/rtems/status.h>
0042 
0043 int rtems_deviceio_open(
0044   rtems_libio_t *iop,
0045   const char *path,
0046   int oflag,
0047   mode_t mode,
0048   rtems_device_major_number major,
0049   rtems_device_minor_number minor
0050 )
0051 {
0052   rtems_status_code status;
0053   rtems_libio_open_close_args_t args;
0054 
0055   args.iop = iop;
0056   args.flags = rtems_libio_iop_flags( iop );
0057   args.mode = mode;
0058 
0059   status = rtems_io_open( major, minor, &args );
0060 
0061   return rtems_status_code_to_errno( status );
0062 }
0063 
0064 int rtems_deviceio_close(
0065   rtems_libio_t *iop,
0066   rtems_device_major_number major,
0067   rtems_device_minor_number minor
0068 )
0069 {
0070   rtems_status_code status;
0071   rtems_libio_open_close_args_t args;
0072 
0073   args.iop = iop;
0074   args.flags = 0;
0075   args.mode = 0;
0076 
0077   status = rtems_io_close( major, minor, &args );
0078 
0079   return rtems_status_code_to_errno( status );
0080 }
0081 
0082 ssize_t rtems_deviceio_read(
0083   rtems_libio_t *iop,
0084   void *buf,
0085   size_t nbyte,
0086   rtems_device_major_number major,
0087   rtems_device_minor_number minor
0088 )
0089 {
0090   rtems_status_code status;
0091   rtems_libio_rw_args_t args;
0092 
0093   args.iop = iop;
0094   args.offset = iop->offset;
0095   args.buffer = buf;
0096   args.count = nbyte;
0097   args.flags = rtems_libio_iop_flags( iop );
0098   args.bytes_moved = 0;
0099 
0100   status = rtems_io_read( major, minor, &args );
0101   if ( status == RTEMS_SUCCESSFUL ) {
0102     iop->offset += args.bytes_moved;
0103 
0104     return (ssize_t) args.bytes_moved;
0105   } else {
0106     return rtems_status_code_to_errno( status );
0107   }
0108 }
0109 
0110 ssize_t rtems_deviceio_write(
0111   rtems_libio_t *iop,
0112   const void *buf,
0113   size_t nbyte,
0114   rtems_device_major_number major,
0115   rtems_device_minor_number minor
0116 )
0117 {
0118   rtems_status_code status;
0119   rtems_libio_rw_args_t args;
0120 
0121   args.iop = iop;
0122   args.offset = iop->offset;
0123   args.buffer = RTEMS_DECONST( void *, buf );
0124   args.count = nbyte;
0125   args.flags = rtems_libio_iop_flags( iop );
0126   args.bytes_moved = 0;
0127 
0128   status = rtems_io_write( major, minor, &args );
0129   if ( status == RTEMS_SUCCESSFUL ) {
0130     iop->offset += args.bytes_moved;
0131 
0132     return (ssize_t) args.bytes_moved;
0133   } else {
0134     return rtems_status_code_to_errno( status );
0135   }
0136 }
0137 
0138 int rtems_deviceio_control(
0139   rtems_libio_t *iop,
0140   ioctl_command_t command,
0141   void *buffer,
0142   rtems_device_major_number major,
0143   rtems_device_minor_number minor
0144 )
0145 {
0146   rtems_status_code status;
0147   rtems_libio_ioctl_args_t args;
0148 
0149   args.iop = iop;
0150   args.command = command;
0151   args.buffer = buffer;
0152 
0153   status = rtems_io_control( major, minor, &args );
0154   if ( status == RTEMS_SUCCESSFUL ) {
0155     return args.ioctl_return;
0156   } else {
0157     return rtems_status_code_to_errno(status);
0158   }
0159 }