| 1 |
2 |
alfik |
/******************************************************************************
|
| 2 |
|
|
* *
|
| 3 |
|
|
* License Agreement *
|
| 4 |
|
|
* *
|
| 5 |
|
|
* Copyright (c) 2006 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 |
|
|
|
| 35 |
|
|
#include "sys/ioctl.h"
|
| 36 |
|
|
#include "sys/alt_errno.h"
|
| 37 |
|
|
#include "sys/alt_warning.h"
|
| 38 |
|
|
#include "priv/alt_file.h"
|
| 39 |
|
|
#include "os/alt_syscall.h"
|
| 40 |
|
|
|
| 41 |
|
|
/*
|
| 42 |
|
|
* The ioctl() system call is provided so that application code can manipulate
|
| 43 |
|
|
* the i/o capabilities of a device in device specific ways. This is identical
|
| 44 |
|
|
* to the standard posix ioctl() function.
|
| 45 |
|
|
*
|
| 46 |
|
|
* In general this implementation simply vectors ioctl requests to the
|
| 47 |
|
|
* apropriate drivers ioctl function (as registered in the drivers alt_dev
|
| 48 |
|
|
* structure).
|
| 49 |
|
|
*
|
| 50 |
|
|
* However in the case of devices (as oposed to filesystem), the TIOCEXCL and
|
| 51 |
|
|
* TIOCNXCL requests are handled without reference to the driver. These
|
| 52 |
|
|
* requests are used to lock/release a device for exclusive access.
|
| 53 |
|
|
*
|
| 54 |
|
|
* Handling these requests centrally eases the task of device driver
|
| 55 |
|
|
* development.
|
| 56 |
|
|
*
|
| 57 |
|
|
* ALT_IOCTL is mapped onto the ioctl() system call in alt_syscall.h
|
| 58 |
|
|
*/
|
| 59 |
|
|
|
| 60 |
|
|
#ifdef ALT_USE_DIRECT_DRIVERS
|
| 61 |
|
|
|
| 62 |
|
|
#include "system.h"
|
| 63 |
|
|
#include "sys/alt_driver.h"
|
| 64 |
|
|
|
| 65 |
|
|
/*
|
| 66 |
|
|
* Provide minimal version that calls ioctl routine of provided stdio devices.
|
| 67 |
|
|
*/
|
| 68 |
|
|
int ALT_IOCTL (int file, int req, void* arg)
|
| 69 |
|
|
{
|
| 70 |
|
|
#ifdef ALT_STDIN_PRESENT
|
| 71 |
|
|
ALT_DRIVER_IOCTL_EXTERNS(ALT_STDIN_DEV);
|
| 72 |
|
|
#endif
|
| 73 |
|
|
#ifdef ALT_STDOUT_PRESENT
|
| 74 |
|
|
ALT_DRIVER_IOCTL_EXTERNS(ALT_STDOUT_DEV);
|
| 75 |
|
|
#endif
|
| 76 |
|
|
#ifdef ALT_STDERR_PRESENT
|
| 77 |
|
|
ALT_DRIVER_IOCTL_EXTERNS(ALT_STDERR_DEV);
|
| 78 |
|
|
#endif
|
| 79 |
|
|
|
| 80 |
|
|
#if !defined(ALT_STDIN_PRESENT) && !defined(ALT_STDOUT_PRESENT) && !defined(ALT_STDERR_PRESENT)
|
| 81 |
|
|
/* Generate a link time warning, should this function ever be called. */
|
| 82 |
|
|
ALT_STUB_WARNING(ioctl);
|
| 83 |
|
|
#endif
|
| 84 |
|
|
|
| 85 |
|
|
switch (file) {
|
| 86 |
|
|
#ifdef ALT_STDIN_PRESENT
|
| 87 |
|
|
case 0: /* stdin file descriptor */
|
| 88 |
|
|
return ALT_DRIVER_IOCTL(ALT_STDIN_DEV, req, arg);
|
| 89 |
|
|
#endif /* ALT_STDIN_PRESENT */
|
| 90 |
|
|
#ifdef ALT_STDOUT_PRESENT
|
| 91 |
|
|
case 1: /* stdout file descriptor */
|
| 92 |
|
|
return ALT_DRIVER_IOCTL(ALT_STDOUT_DEV, req, arg);
|
| 93 |
|
|
#endif /* ALT_STDOUT_PRESENT */
|
| 94 |
|
|
#ifdef ALT_STDERR_PRESENT
|
| 95 |
|
|
case 2: /* stderr file descriptor */
|
| 96 |
|
|
return ALT_DRIVER_IOCTL(ALT_STDERR_DEV, req, arg);
|
| 97 |
|
|
#endif /* ALT_STDERR_PRESENT */
|
| 98 |
|
|
default:
|
| 99 |
|
|
ALT_ERRNO = EBADFD;
|
| 100 |
|
|
return -1;
|
| 101 |
|
|
}
|
| 102 |
|
|
}
|
| 103 |
|
|
|
| 104 |
|
|
#else /* !ALT_USE_DIRECT_DRIVERS */
|
| 105 |
|
|
|
| 106 |
|
|
int ALT_IOCTL (int file, int req, void* arg)
|
| 107 |
|
|
{
|
| 108 |
|
|
alt_fd* fd;
|
| 109 |
|
|
int rc;
|
| 110 |
|
|
|
| 111 |
|
|
/*
|
| 112 |
|
|
* A common error case is that when the file descriptor was created, the call
|
| 113 |
|
|
* to open() failed resulting in a negative file descriptor. This is trapped
|
| 114 |
|
|
* below so that we don't try and process an invalid file descriptor.
|
| 115 |
|
|
*/
|
| 116 |
|
|
|
| 117 |
|
|
fd = (file < 0) ? NULL : &alt_fd_list[file];
|
| 118 |
|
|
|
| 119 |
|
|
if (fd)
|
| 120 |
|
|
{
|
| 121 |
|
|
|
| 122 |
|
|
/*
|
| 123 |
|
|
* In the case of device drivers (not file systems) handle the TIOCEXCL
|
| 124 |
|
|
* and TIOCNXCL requests as special cases.
|
| 125 |
|
|
*/
|
| 126 |
|
|
|
| 127 |
|
|
if (fd->fd_flags & ALT_FD_DEV)
|
| 128 |
|
|
{
|
| 129 |
|
|
if (req == TIOCEXCL)
|
| 130 |
|
|
{
|
| 131 |
|
|
rc = alt_fd_lock (fd);
|
| 132 |
|
|
goto ioctl_done;
|
| 133 |
|
|
}
|
| 134 |
|
|
else if (req == TIOCNXCL)
|
| 135 |
|
|
{
|
| 136 |
|
|
rc = alt_fd_unlock (fd);
|
| 137 |
|
|
goto ioctl_done;
|
| 138 |
|
|
}
|
| 139 |
|
|
}
|
| 140 |
|
|
|
| 141 |
|
|
/*
|
| 142 |
|
|
* If the driver provides an ioctl() function, call that to handle the
|
| 143 |
|
|
* request, otherwise set the return code to indicate that the request
|
| 144 |
|
|
* could not be processed.
|
| 145 |
|
|
*/
|
| 146 |
|
|
|
| 147 |
|
|
if (fd->dev->ioctl)
|
| 148 |
|
|
{
|
| 149 |
|
|
rc = fd->dev->ioctl(fd, req, arg);
|
| 150 |
|
|
}
|
| 151 |
|
|
else
|
| 152 |
|
|
{
|
| 153 |
|
|
rc = -ENOTTY;
|
| 154 |
|
|
}
|
| 155 |
|
|
}
|
| 156 |
|
|
else
|
| 157 |
|
|
{
|
| 158 |
|
|
rc = -EBADFD;
|
| 159 |
|
|
}
|
| 160 |
|
|
|
| 161 |
|
|
ioctl_done:
|
| 162 |
|
|
|
| 163 |
|
|
if (rc < 0)
|
| 164 |
|
|
{
|
| 165 |
|
|
ALT_ERRNO = -rc;
|
| 166 |
|
|
}
|
| 167 |
|
|
return rc;
|
| 168 |
|
|
}
|
| 169 |
|
|
|
| 170 |
|
|
#endif /* ALT_USE_DIRECT_DRIVERS */
|