1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup ClassicIO |
---|
5 | * |
---|
6 | * @brief Classic Input/Output Manager implementation. |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * COPYRIGHT (c) 1989-2009. |
---|
11 | * On-Line Applications Research Corporation (OAR). |
---|
12 | * |
---|
13 | * Copyright (c) 2009 embedded brains GmbH. |
---|
14 | * |
---|
15 | * The license and distribution terms for this file may be |
---|
16 | * found in the file LICENSE in this distribution or at |
---|
17 | * http://www.rtems.org/license/LICENSE. |
---|
18 | */ |
---|
19 | |
---|
20 | #if HAVE_CONFIG_H |
---|
21 | #include "config.h" |
---|
22 | #endif |
---|
23 | |
---|
24 | #include <rtems/ioimpl.h> |
---|
25 | #include <rtems/rtems/intr.h> |
---|
26 | |
---|
27 | ISR_LOCK_DEFINE( , _IO_Driver_registration_lock, "IO Driver Registration" ) |
---|
28 | |
---|
29 | static inline bool rtems_io_is_empty_table( |
---|
30 | const rtems_driver_address_table *table |
---|
31 | ) |
---|
32 | { |
---|
33 | return table->initialization_entry == NULL && table->open_entry == NULL; |
---|
34 | } |
---|
35 | |
---|
36 | static rtems_status_code rtems_io_obtain_major_number( |
---|
37 | rtems_device_major_number *major |
---|
38 | ) |
---|
39 | { |
---|
40 | rtems_device_major_number n = _IO_Number_of_drivers; |
---|
41 | rtems_device_major_number m = 0; |
---|
42 | |
---|
43 | /* major is error checked by caller */ |
---|
44 | |
---|
45 | for ( m = 0; m < n; ++m ) { |
---|
46 | rtems_driver_address_table *const table = _IO_Driver_address_table + m; |
---|
47 | |
---|
48 | if ( rtems_io_is_empty_table( table ) ) |
---|
49 | break; |
---|
50 | } |
---|
51 | |
---|
52 | /* Assigns invalid value in case of failure */ |
---|
53 | *major = m; |
---|
54 | |
---|
55 | if ( m != n ) |
---|
56 | return RTEMS_SUCCESSFUL; |
---|
57 | |
---|
58 | return RTEMS_TOO_MANY; |
---|
59 | } |
---|
60 | |
---|
61 | rtems_status_code rtems_io_register_driver( |
---|
62 | rtems_device_major_number major, |
---|
63 | const rtems_driver_address_table *driver_table, |
---|
64 | rtems_device_major_number *registered_major |
---|
65 | ) |
---|
66 | { |
---|
67 | rtems_device_major_number major_limit = _IO_Number_of_drivers; |
---|
68 | ISR_lock_Context lock_context; |
---|
69 | |
---|
70 | if ( rtems_interrupt_is_in_progress() ) |
---|
71 | return RTEMS_CALLED_FROM_ISR; |
---|
72 | |
---|
73 | if ( registered_major == NULL ) |
---|
74 | return RTEMS_INVALID_ADDRESS; |
---|
75 | |
---|
76 | /* Set it to an invalid value */ |
---|
77 | *registered_major = major_limit; |
---|
78 | |
---|
79 | if ( driver_table == NULL ) |
---|
80 | return RTEMS_INVALID_ADDRESS; |
---|
81 | |
---|
82 | if ( rtems_io_is_empty_table( driver_table ) ) |
---|
83 | return RTEMS_INVALID_ADDRESS; |
---|
84 | |
---|
85 | if ( major >= major_limit ) |
---|
86 | return RTEMS_INVALID_NUMBER; |
---|
87 | |
---|
88 | _IO_Driver_registration_acquire( &lock_context ); |
---|
89 | |
---|
90 | if ( major == 0 ) { |
---|
91 | rtems_status_code sc = rtems_io_obtain_major_number( registered_major ); |
---|
92 | |
---|
93 | if ( sc != RTEMS_SUCCESSFUL ) { |
---|
94 | _IO_Driver_registration_release( &lock_context ); |
---|
95 | return sc; |
---|
96 | } |
---|
97 | major = *registered_major; |
---|
98 | } else { |
---|
99 | rtems_driver_address_table *const table = _IO_Driver_address_table + major; |
---|
100 | |
---|
101 | if ( !rtems_io_is_empty_table( table ) ) { |
---|
102 | _IO_Driver_registration_release( &lock_context ); |
---|
103 | return RTEMS_RESOURCE_IN_USE; |
---|
104 | } |
---|
105 | |
---|
106 | *registered_major = major; |
---|
107 | } |
---|
108 | |
---|
109 | _IO_Driver_address_table [major] = *driver_table; |
---|
110 | |
---|
111 | _IO_Driver_registration_release( &lock_context ); |
---|
112 | |
---|
113 | if ( _IO_All_drivers_initialized ) { |
---|
114 | /* Other drivers have already been initialized, we initialize |
---|
115 | * the driver directly. |
---|
116 | */ |
---|
117 | return rtems_io_initialize( major, 0, NULL ); |
---|
118 | } else { |
---|
119 | /* The driver will be initialized together with all other drivers |
---|
120 | * in a later stage by _IO_Initialize_all_drivers(). |
---|
121 | */ |
---|
122 | return RTEMS_SUCCESSFUL; |
---|
123 | } |
---|
124 | } |
---|