Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup RTEMSImplClassicIO
0007  *
0008  * @brief This source file contains the implementation of
0009  *   rtems_io_register_driver().
0010  */
0011 
0012 /*
0013  *  COPYRIGHT (c) 1989-2009.
0014  *  On-Line Applications Research Corporation (OAR).
0015  *
0016  *  Copyright (c) 2009 embedded brains GmbH & Co. KG
0017  *
0018  * Redistribution and use in source and binary forms, with or without
0019  * modification, are permitted provided that the following conditions
0020  * are met:
0021  * 1. Redistributions of source code must retain the above copyright
0022  *    notice, this list of conditions and the following disclaimer.
0023  * 2. Redistributions in binary form must reproduce the above copyright
0024  *    notice, this list of conditions and the following disclaimer in the
0025  *    documentation and/or other materials provided with the distribution.
0026  *
0027  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0028  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0029  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0030  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0031  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0032  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0033  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0034  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0035  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0036  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0037  * POSSIBILITY OF SUCH DAMAGE.
0038  */
0039 
0040 #ifdef HAVE_CONFIG_H
0041 #include "config.h"
0042 #endif
0043 
0044 #include <rtems/ioimpl.h>
0045 #include <rtems/rtems/intr.h>
0046 
0047 #if ISR_LOCK_NEEDS_OBJECT
0048 ISR_lock_Control _IO_Driver_registration_lock =
0049   ISR_LOCK_INITIALIZER( "IO Driver Registration" );
0050 #endif
0051 
0052 static inline bool rtems_io_is_empty_table(
0053   const rtems_driver_address_table *table
0054 )
0055 {
0056   return table->initialization_entry == NULL && table->open_entry == NULL;
0057 }
0058 
0059 static rtems_status_code rtems_io_obtain_major_number(
0060   rtems_device_major_number *major
0061 )
0062 {
0063   rtems_device_major_number n = _IO_Number_of_drivers;
0064   rtems_device_major_number m = 0;
0065 
0066   /* major is error checked by caller */
0067 
0068   for ( m = 0; m < n; ++m ) {
0069     rtems_driver_address_table *const table = _IO_Driver_address_table + m;
0070 
0071     if ( rtems_io_is_empty_table( table ) )
0072       break;
0073   }
0074 
0075   /* Assigns invalid value in case of failure */
0076   *major = m;
0077 
0078   if ( m != n )
0079     return RTEMS_SUCCESSFUL;
0080 
0081   return RTEMS_TOO_MANY;
0082 }
0083 
0084 rtems_status_code rtems_io_register_driver(
0085   rtems_device_major_number         major,
0086   const rtems_driver_address_table *driver_table,
0087   rtems_device_major_number        *registered_major
0088 )
0089 {
0090   rtems_device_major_number major_limit = _IO_Number_of_drivers;
0091   ISR_lock_Context lock_context;
0092 
0093   if ( rtems_interrupt_is_in_progress() )
0094     return RTEMS_CALLED_FROM_ISR;
0095 
0096   if ( registered_major == NULL )
0097     return RTEMS_INVALID_ADDRESS;
0098 
0099   /* Set it to an invalid value */
0100   *registered_major = major_limit;
0101 
0102   if ( driver_table == NULL )
0103     return RTEMS_INVALID_ADDRESS;
0104 
0105   if ( rtems_io_is_empty_table( driver_table ) )
0106     return RTEMS_INVALID_ADDRESS;
0107 
0108   if ( major >= major_limit )
0109     return RTEMS_INVALID_NUMBER;
0110 
0111   _IO_Driver_registration_acquire( &lock_context );
0112 
0113   if ( major == 0 ) {
0114     rtems_status_code sc = rtems_io_obtain_major_number( registered_major );
0115 
0116     if ( sc != RTEMS_SUCCESSFUL ) {
0117       _IO_Driver_registration_release( &lock_context );
0118       return sc;
0119     }
0120     major = *registered_major;
0121   } else {
0122     rtems_driver_address_table *const table = _IO_Driver_address_table + major;
0123 
0124     if ( !rtems_io_is_empty_table( table ) ) {
0125       _IO_Driver_registration_release( &lock_context );
0126       return RTEMS_RESOURCE_IN_USE;
0127     }
0128 
0129     *registered_major = major;
0130   }
0131 
0132   _IO_Driver_address_table [major] = *driver_table;
0133 
0134   _IO_Driver_registration_release( &lock_context );
0135 
0136   if ( _IO_All_drivers_initialized ) {
0137     /* Other drivers have already been initialized, we initialize
0138      * the driver directly.
0139      */
0140     return rtems_io_initialize( major, 0, NULL );
0141   } else {
0142     /* The driver will be initialized together with all other drivers
0143      * in a later stage by _IO_Initialize_all_drivers().
0144      */
0145     return RTEMS_SUCCESSFUL;
0146   }
0147 }