1 #ifndef __ALT_DRIVER_H__
2 #define __ALT_DRIVER_H__
3 
4 /******************************************************************************
5 *                                                                             *
6 * License Agreement                                                           *
7 *                                                                             *
8 * Copyright (c) 2006 Altera Corporation, San Jose, California, USA.           *
9 * All rights reserved.                                                        *
10 *                                                                             *
11 * Permission is hereby granted, free of charge, to any person obtaining a     *
12 * copy of this software and associated documentation files (the "Software"),  *
13 * to deal in the Software without restriction, including without limitation   *
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,    *
15 * and/or sell copies of the Software, and to permit persons to whom the       *
16 * Software is furnished to do so, subject to the following conditions:        *
17 *                                                                             *
18 * The above copyright notice and this permission notice shall be included in  *
19 * all copies or substantial portions of the Software.                         *
20 *                                                                             *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  *
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,    *
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      *
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING     *
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER         *
27 * DEALINGS IN THE SOFTWARE.                                                   *
28 *                                                                             *
29 *                                                                             *
30 * Altera does not recommend, suggest or require that this reference design    *
31 * file be used in conjunction or combination with any other product.          *
32 ******************************************************************************/
33 
34 /*
35  * Macros used to access a driver without HAL file descriptors.
36  */
37 
38 /*
39  * ALT_MODULE_CLASS
40  *
41  * This macro returns the module class name for the specified module instance.
42  * It uses information in the system.h file.
43  * Neither the instance name or class name are quoted (so that they can
44  * be used with other pre-processor macros).
45  *
46  * Example:
47  *   Assume the design has an instance of an altera_avalon_uart called uart1.
48  *   Calling ALT_MODULE_CLASS(uart1) returns altera_avalon_uart.
49  */
50 
51 #define ALT_MODULE_CLASS(instance) ALT_MODULE_CLASS_ ## instance
52 
53 
54 /*
55  * ALT_DRIVER_FUNC_NAME
56  *
57  *   --> instance               Instance name.
58  *   --> func                   Function name.
59  *
60  * This macro returns the device driver function name of the specified
61  * module instance for the specified function name.
62  *
63  * Example:
64  *   Assume the design has an instance of an altera_avalon_uart called uart1.
65  *   Calling ALT_DRIVER_FUNC_NAME(uart1, write) returns
66  *   altera_avalon_uart_write.
67  */
68 
69 #define ALT_DRIVER_FUNC_NAME(instance, func) \
70   ALT_DRIVER_FUNC_NAME1(ALT_MODULE_CLASS(instance), func)
71 #define ALT_DRIVER_FUNC_NAME1(module_class, func) \
72   ALT_DRIVER_FUNC_NAME2(module_class, func)
73 #define ALT_DRIVER_FUNC_NAME2(module_class, func) \
74   module_class ## _ ## func
75 
76 /*
77  * ALT_DRIVER_STATE_STRUCT
78  *
79  *   --> instance               Instance name.
80  *
81  * This macro returns the device driver state type name of the specified
82  * module instance.
83  *
84  * Example:
85  *   Assume the design has an instance of an altera_avalon_uart called uart1.
86  *   Calling ALT_DRIVER_STATE_STRUCT(uart1) returns:
87  *     struct altera_avalon_uart_state_s
88  *
89  * Note that the ALT_DRIVER_FUNC_NAME macro is used even though "state" isn't
90  * really a function but it does match the required naming convention.
91  */
92 #define ALT_DRIVER_STATE_STRUCT(instance) \
93     struct ALT_DRIVER_FUNC_NAME(instance, state_s)
94 
95 /*
96  * ALT_DRIVER_STATE
97  *
98  *   --> instance               Instance name.
99  *
100  * This macro returns the device driver state name of the specified
101  * module instance.
102  *
103  * Example:
104  *   Assume the design has an instance of an altera_avalon_uart called uart1.
105  *   Calling ALT_DRIVER_STATE(uart1) returns uart1.
106  */
107 #define ALT_DRIVER_STATE(instance) instance
108 
109 /*
110  * ALT_DRIVER_WRITE
111  *
112  *   --> instance               Instance name.
113  *   --> buffer                 Write buffer.
114  *   --> len                    Length of write buffer data.
115  *   --> flags                  Control flags (e.g. O_NONBLOCK)
116  *
117  * This macro calls the "write" function of the specified driver instance.
118  */
119 
120 #define ALT_DRIVER_WRITE_EXTERNS(instance)                                  \
121     extern ALT_DRIVER_STATE_STRUCT(instance) ALT_DRIVER_STATE(instance);    \
122     extern int ALT_DRIVER_FUNC_NAME(instance, write)                        \
123       (ALT_DRIVER_STATE_STRUCT(instance) *, const char *, int, int);
124 
125 #define ALT_DRIVER_WRITE(instance, buffer, len, flags)                      \
126     ALT_DRIVER_FUNC_NAME(instance, write)(&ALT_DRIVER_STATE(instance), buffer, len, flags)
127 
128 
129 /*
130  * ALT_DRIVER_READ
131  *
132  *   --> instance               Instance name.
133  *   <-- buffer                 Read buffer.
134  *   --> len                    Length of read buffer.
135  *   --> flags                  Control flags (e.g. O_NONBLOCK)
136  *
137  * This macro calls the "read" function of the specified driver instance.
138  */
139 
140 #define ALT_DRIVER_READ_EXTERNS(instance)                                   \
141     extern ALT_DRIVER_STATE_STRUCT(instance) ALT_DRIVER_STATE(instance);    \
142     extern int ALT_DRIVER_FUNC_NAME(instance, read)                         \
143       (ALT_DRIVER_STATE_STRUCT(instance) *, const char *, int, int);
144 
145 #define ALT_DRIVER_READ(instance, buffer, len, flags)                       \
146     ALT_DRIVER_FUNC_NAME(instance, read)(&ALT_DRIVER_STATE(instance), buffer, len, flags)
147 
148 /*
149  * ALT_DRIVER_IOCTL
150  *
151  *   --> instance               Instance name.
152  *   --> req                    ioctl request (e.g. TIOCSTIMEOUT)
153  *   --> arg                    Optional argument (void*)
154  *
155  * This macro calls the "ioctl" function of the specified driver instance
156  */
157 
158 #define ALT_DRIVER_IOCTL_EXTERNS(instance)                                  \
159     extern ALT_DRIVER_STATE_STRUCT(instance) ALT_DRIVER_STATE(instance);    \
160     extern int ALT_DRIVER_FUNC_NAME(instance, ioctl)                        \
161       (ALT_DRIVER_STATE_STRUCT(instance) *, int, void*);
162 
163 #define ALT_DRIVER_IOCTL(instance, req, arg)                                \
164     ALT_DRIVER_FUNC_NAME(instance, ioctl)(&ALT_DRIVER_STATE(instance), req, arg)
165 
166 #endif /* __ALT_DRIVER_H__ */
167