1/*  *********************************************************************
2    *  Broadcom Common Firmware Environment (CFE)
3    *
4    *  Device Function stubs			File: cfe_devfuncs.c
5    *
6    *  This module contains device function stubs (small routines to
7    *  call the standard "iocb" interface entry point to CFE).
8    *  There should be one routine here per iocb function call.
9    *
10    *  Author:  Mitch Lichtenberg (mpl@broadcom.com)
11    *
12    *********************************************************************
13    *
14    *  Copyright 2000,2001,2002,2003
15    *  Broadcom Corporation. All rights reserved.
16    *
17    *  This software is furnished under license and may be used and
18    *  copied only in accordance with the following terms and
19    *  conditions.  Subject to these conditions, you may download,
20    *  copy, install, use, modify and distribute modified or unmodified
21    *  copies of this software in source and/or binary form.  No title
22    *  or ownership is transferred hereby.
23    *
24    *  1) Any source code used, modified or distributed must reproduce
25    *     and retain this copyright notice and list of conditions
26    *     as they appear in the source file.
27    *
28    *  2) No right is granted to use any trade name, trademark, or
29    *     logo of Broadcom Corporation.  The "Broadcom Corporation"
30    *     name may not be used to endorse or promote products derived
31    *     from this software without the prior written permission of
32    *     Broadcom Corporation.
33    *
34    *  3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
35    *     IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
36    *     WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
37    *     PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT
38    *     SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN
39    *     PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
40    *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
41    *     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
42    *     GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43    *     BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
44    *     OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
45    *     TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF
46    *     THE POSSIBILITY OF SUCH DAMAGE.
47    ********************************************************************* */
48
49
50#include "lib_types.h"
51#include "lib_string.h"
52#include "cfe_iocb.h"
53#include "cfe_devfuncs.h"
54
55extern int cfe_iocb_dispatch(cfe_iocb_t *iocb);
56
57static int cfe_strlen(char *name)
58{
59    int count = 0;
60
61    while (*name) {
62	count++;
63	name++;
64	}
65
66    return count;
67}
68
69int cfe_open(char *name)
70{
71    cfe_iocb_t iocb;
72
73    iocb.iocb_fcode = CFE_CMD_DEV_OPEN;
74    iocb.iocb_status = 0;
75    iocb.iocb_handle = 0;
76    iocb.iocb_flags = 0;
77    iocb.iocb_psize = sizeof(iocb_buffer_t);
78    iocb.plist.iocb_buffer.buf_offset = 0;
79    iocb.plist.iocb_buffer.buf_ptr = name;
80    iocb.plist.iocb_buffer.buf_length = cfe_strlen(name);
81
82    cfe_iocb_dispatch(&iocb);
83
84    return (iocb.iocb_status < 0) ? iocb.iocb_status : iocb.iocb_handle;
85}
86
87int cfe_close(int handle)
88{
89    cfe_iocb_t iocb;
90
91    iocb.iocb_fcode = CFE_CMD_DEV_CLOSE;
92    iocb.iocb_status = 0;
93    iocb.iocb_handle = handle;
94    iocb.iocb_flags = 0;
95    iocb.iocb_psize = 0;
96
97    cfe_iocb_dispatch(&iocb);
98
99    return (iocb.iocb_status);
100
101}
102
103int cfe_readblk(int handle,cfe_offset_t offset,unsigned char *buffer,int length)
104{
105    cfe_iocb_t iocb;
106
107    iocb.iocb_fcode = CFE_CMD_DEV_READ;
108    iocb.iocb_status = 0;
109    iocb.iocb_handle = handle;
110    iocb.iocb_flags = 0;
111    iocb.iocb_psize = sizeof(iocb_buffer_t);
112    iocb.plist.iocb_buffer.buf_offset = offset;
113    iocb.plist.iocb_buffer.buf_ptr = buffer;
114    iocb.plist.iocb_buffer.buf_length = length;
115
116    cfe_iocb_dispatch(&iocb);
117
118    return (iocb.iocb_status < 0) ? iocb.iocb_status : iocb.plist.iocb_buffer.buf_retlen;
119}
120
121int cfe_read(int handle,unsigned char *buffer,int length)
122{
123    return cfe_readblk(handle,0,buffer,length);
124}
125
126
127int cfe_writeblk(int handle,cfe_offset_t offset,unsigned char *buffer,int length)
128{
129    cfe_iocb_t iocb;
130
131    iocb.iocb_fcode = CFE_CMD_DEV_WRITE;
132    iocb.iocb_status = 0;
133    iocb.iocb_handle = handle;
134    iocb.iocb_flags = 0;
135    iocb.iocb_psize = sizeof(iocb_buffer_t);
136    iocb.plist.iocb_buffer.buf_offset = offset;
137    iocb.plist.iocb_buffer.buf_ptr = buffer;
138    iocb.plist.iocb_buffer.buf_length = length;
139
140    cfe_iocb_dispatch(&iocb);
141
142    return (iocb.iocb_status < 0) ? iocb.iocb_status : iocb.plist.iocb_buffer.buf_retlen;
143}
144
145int cfe_write(int handle,unsigned char *buffer,int length)
146{
147    return cfe_writeblk(handle,0,buffer,length);
148}
149
150
151int cfe_ioctl(int handle,unsigned int ioctlnum,
152	      unsigned char *buffer,int length,int *retlen,
153	      cfe_offset_t offset)
154{
155    cfe_iocb_t iocb;
156
157    iocb.iocb_fcode = CFE_CMD_DEV_IOCTL;
158    iocb.iocb_status = 0;
159    iocb.iocb_handle = handle;
160    iocb.iocb_flags = 0;
161    iocb.iocb_psize = sizeof(iocb_buffer_t);
162    iocb.plist.iocb_buffer.buf_offset = offset;
163    iocb.plist.iocb_buffer.buf_ioctlcmd = (cfe_offset_t) ioctlnum;
164    iocb.plist.iocb_buffer.buf_ptr = buffer;
165    iocb.plist.iocb_buffer.buf_length = length;
166
167    cfe_iocb_dispatch(&iocb);
168
169    if (retlen) *retlen = iocb.plist.iocb_buffer.buf_retlen;
170    return iocb.iocb_status;
171}
172
173int cfe_inpstat(int handle)
174{
175    cfe_iocb_t iocb;
176
177    iocb.iocb_fcode = CFE_CMD_DEV_INPSTAT;
178    iocb.iocb_status = 0;
179    iocb.iocb_handle = handle;
180    iocb.iocb_flags = 0;
181    iocb.iocb_psize = sizeof(iocb_inpstat_t);
182    iocb.plist.iocb_inpstat.inp_status = 0;
183
184    cfe_iocb_dispatch(&iocb);
185
186    if (iocb.iocb_status < 0) return iocb.iocb_status;
187
188    return iocb.plist.iocb_inpstat.inp_status;
189
190}
191
192long long cfe_getticks(void)
193{
194    cfe_iocb_t iocb;
195
196    iocb.iocb_fcode = CFE_CMD_FW_GETTIME;
197    iocb.iocb_status = 0;
198    iocb.iocb_handle = 0;
199    iocb.iocb_flags = 0;
200    iocb.iocb_psize = sizeof(iocb_time_t);
201    iocb.plist.iocb_time.ticks = 0;
202
203    cfe_iocb_dispatch(&iocb);
204
205    return iocb.plist.iocb_time.ticks;
206
207}
208
209int cfe_getenv(char *name,char *dest,int destlen)
210{
211    cfe_iocb_t iocb;
212
213    *dest = NULL;
214
215    iocb.iocb_fcode = CFE_CMD_ENV_GET;
216    iocb.iocb_status = 0;
217    iocb.iocb_handle = 0;
218    iocb.iocb_flags = 0;
219    iocb.iocb_psize = sizeof(iocb_envbuf_t);
220    iocb.plist.iocb_envbuf.enum_idx = 0;
221    iocb.plist.iocb_envbuf.name_ptr = name;
222    iocb.plist.iocb_envbuf.name_length = strlen(name)+1;
223    iocb.plist.iocb_envbuf.val_ptr = dest;
224    iocb.plist.iocb_envbuf.val_length = destlen;
225
226    cfe_iocb_dispatch(&iocb);
227
228    return iocb.iocb_status;
229}
230
231int cfe_exit(int warm,int code)
232{
233    cfe_iocb_t iocb;
234
235    iocb.iocb_fcode = CFE_CMD_FW_RESTART;
236    iocb.iocb_status = 0;
237    iocb.iocb_handle = 0;
238    iocb.iocb_flags = warm ? CFE_FLG_WARMSTART : 0;
239    iocb.iocb_psize = sizeof(iocb_exitstat_t);
240    iocb.plist.iocb_exitstat.status = code;
241
242    cfe_iocb_dispatch(&iocb);
243
244    return (iocb.iocb_status);
245
246}
247
248int cfe_flushcache(int flg)
249{
250    cfe_iocb_t iocb;
251
252    iocb.iocb_fcode = CFE_CMD_FW_FLUSHCACHE;
253    iocb.iocb_status = 0;
254    iocb.iocb_handle = 0;
255    iocb.iocb_flags = flg;
256    iocb.iocb_psize = 0;
257
258    cfe_iocb_dispatch(&iocb);
259
260    return iocb.iocb_status;
261}
262
263int cfe_getdevinfo(char *name)
264{
265    cfe_iocb_t iocb;
266
267    iocb.iocb_fcode = CFE_CMD_DEV_GETINFO;
268    iocb.iocb_status = 0;
269    iocb.iocb_handle = 0;
270    iocb.iocb_flags = 0;
271    iocb.iocb_psize = sizeof(iocb_buffer_t);
272    iocb.plist.iocb_buffer.buf_offset = 0;
273    iocb.plist.iocb_buffer.buf_ptr = name;
274    iocb.plist.iocb_buffer.buf_length = cfe_strlen(name);
275
276    cfe_iocb_dispatch(&iocb);
277
278    return (iocb.iocb_status < 0) ? iocb.iocb_status : (int)iocb.plist.iocb_buffer.buf_devflags;
279}
280