| 1 |
2 |
alfik |
/******************************************************************************
|
| 2 |
|
|
* *
|
| 3 |
|
|
* License Agreement *
|
| 4 |
|
|
* *
|
| 5 |
|
|
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
|
| 6 |
|
|
* All rights reserved. *
|
| 7 |
|
|
* *
|
| 8 |
|
|
* Permission is hereby granted, free of charge, to any person obtaining a *
|
| 9 |
|
|
* copy of this software and associated documentation files (the "Software"), *
|
| 10 |
|
|
* to deal in the Software without restriction, including without limitation *
|
| 11 |
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
|
| 12 |
|
|
* and/or sell copies of the Software, and to permit persons to whom the *
|
| 13 |
|
|
* Software is furnished to do so, subject to the following conditions: *
|
| 14 |
|
|
* *
|
| 15 |
|
|
* The above copyright notice and this permission notice shall be included in *
|
| 16 |
|
|
* all copies or substantial portions of the Software. *
|
| 17 |
|
|
* *
|
| 18 |
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
|
| 19 |
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
|
| 20 |
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
|
| 21 |
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
|
| 22 |
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
|
| 23 |
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
|
| 24 |
|
|
* DEALINGS IN THE SOFTWARE. *
|
| 25 |
|
|
* *
|
| 26 |
|
|
* This agreement shall be governed in all respects by the laws of the State *
|
| 27 |
|
|
* of California and by the laws of the United States of America. *
|
| 28 |
|
|
* *
|
| 29 |
|
|
* Altera does not recommend, suggest or require that this reference design *
|
| 30 |
|
|
* file be used in conjunction or combination with any other product. *
|
| 31 |
|
|
******************************************************************************/
|
| 32 |
|
|
|
| 33 |
|
|
#include <stddef.h>
|
| 34 |
|
|
#include <stdio.h>
|
| 35 |
|
|
#include <unistd.h>
|
| 36 |
|
|
#include <fcntl.h>
|
| 37 |
|
|
#include <errno.h>
|
| 38 |
|
|
|
| 39 |
|
|
#include "sys/alt_dev.h"
|
| 40 |
|
|
#include "priv/alt_file.h"
|
| 41 |
|
|
|
| 42 |
|
|
#include "alt_types.h"
|
| 43 |
|
|
|
| 44 |
|
|
#include "system.h"
|
| 45 |
|
|
|
| 46 |
|
|
/*
|
| 47 |
|
|
* This file contains the data constructs used to control access to device and
|
| 48 |
|
|
* filesytems.
|
| 49 |
|
|
*/
|
| 50 |
|
|
|
| 51 |
|
|
/*
|
| 52 |
|
|
* "alt_fs_list" is the head of a linked list of registered filesystems. It is
|
| 53 |
|
|
* initialised as an empty list. New entries can be added using the
|
| 54 |
|
|
* alt_fs_reg() function.
|
| 55 |
|
|
*/
|
| 56 |
|
|
|
| 57 |
|
|
ALT_LLIST_HEAD(alt_fs_list);
|
| 58 |
|
|
|
| 59 |
|
|
|
| 60 |
|
|
/*
|
| 61 |
|
|
* "alt_dev_list" is the head of a linked list of registered devices. It is
|
| 62 |
|
|
* configured at startup to include a single device, "alt_dev_null". This
|
| 63 |
|
|
* device is discussed below.
|
| 64 |
|
|
*/
|
| 65 |
|
|
|
| 66 |
|
|
extern alt_dev alt_dev_null; /* forward declaration */
|
| 67 |
|
|
|
| 68 |
|
|
alt_llist alt_dev_list = {&alt_dev_null.llist, &alt_dev_null.llist};
|
| 69 |
|
|
|
| 70 |
|
|
/*
|
| 71 |
|
|
* alt_dev_null_write() is the implementation of the write() function used
|
| 72 |
|
|
* by the alt_dev_null device. It simple discards all data passed to it, and
|
| 73 |
|
|
* indicates that the data has been successfully transmitted.
|
| 74 |
|
|
*/
|
| 75 |
|
|
|
| 76 |
|
|
static int alt_dev_null_write (alt_fd* fd, const char* ptr, int len)
|
| 77 |
|
|
{
|
| 78 |
|
|
return len;
|
| 79 |
|
|
}
|
| 80 |
|
|
|
| 81 |
|
|
/*
|
| 82 |
|
|
* "alt_dev_null" is used to allow output to be redirected to nowhere. It is
|
| 83 |
|
|
* the only device registered before the call to alt_sys_init(). At startup
|
| 84 |
|
|
* stin, stdout & stderr are all directed towards this device so that library
|
| 85 |
|
|
* calls like printf() will be safe but inefectual.
|
| 86 |
|
|
*/
|
| 87 |
|
|
|
| 88 |
|
|
alt_dev alt_dev_null = {
|
| 89 |
|
|
{
|
| 90 |
|
|
&alt_dev_list,
|
| 91 |
|
|
&alt_dev_list
|
| 92 |
|
|
},
|
| 93 |
|
|
"/dev/null",
|
| 94 |
|
|
NULL, /* open */
|
| 95 |
|
|
NULL, /* close */
|
| 96 |
|
|
NULL, /* write */
|
| 97 |
|
|
alt_dev_null_write, /* write */
|
| 98 |
|
|
NULL, /* lseek */
|
| 99 |
|
|
NULL, /* fstat */
|
| 100 |
|
|
NULL /* ioctl */
|
| 101 |
|
|
};
|
| 102 |
|
|
|
| 103 |
|
|
/*
|
| 104 |
|
|
* "alt_fd_list_lock" is a semaphore used to control access to the file
|
| 105 |
|
|
* descriptor list. This is used to ensure that access to the list is thread
|
| 106 |
|
|
* safe.
|
| 107 |
|
|
*/
|
| 108 |
|
|
|
| 109 |
|
|
ALT_SEM(alt_fd_list_lock)
|
| 110 |
|
|
|
| 111 |
|
|
/*
|
| 112 |
|
|
* "alt_max_fd" is used to make access to the file descriptor list more
|
| 113 |
|
|
* efficent. It is set to be the value of the highest allocated file
|
| 114 |
|
|
* descriptor. This saves having to search the entire pool of unallocated
|
| 115 |
|
|
* file descriptors when looking for a match.
|
| 116 |
|
|
*/
|
| 117 |
|
|
|
| 118 |
|
|
alt_32 alt_max_fd = -1;
|
| 119 |
|
|
|
| 120 |
|
|
/*
|
| 121 |
|
|
* "alt_fd_list" is the file descriptor pool. The first three entries in the
|
| 122 |
|
|
* array are configured as standard in, standard out, and standard error. These
|
| 123 |
|
|
* are all initialised so that accesses are directed to the alt_dev_null
|
| 124 |
|
|
* device. The remaining file descriptors are initialised as unallocated.
|
| 125 |
|
|
*
|
| 126 |
|
|
* The maximum number of file descriptors within the system is specified by the
|
| 127 |
|
|
* user defined macro "ALT_MAX_FD". This is defined in "system.h", which is
|
| 128 |
|
|
* auto-genereated using the projects PTF and STF files.
|
| 129 |
|
|
*/
|
| 130 |
|
|
|
| 131 |
|
|
alt_fd alt_fd_list[ALT_MAX_FD] =
|
| 132 |
|
|
{
|
| 133 |
|
|
{
|
| 134 |
|
|
&alt_dev_null, /* standard in */
|
| 135 |
|
|
0,
|
| 136 |
|
|
|
| 137 |
|
|
},
|
| 138 |
|
|
{
|
| 139 |
|
|
&alt_dev_null, /* standard out */
|
| 140 |
|
|
0,
|
| 141 |
|
|
|
| 142 |
|
|
},
|
| 143 |
|
|
{
|
| 144 |
|
|
&alt_dev_null, /* standard error */
|
| 145 |
|
|
0,
|
| 146 |
|
|
|
| 147 |
|
|
}
|
| 148 |
|
|
/* all other elements are set to zero */
|
| 149 |
|
|
};
|