Specifications

For more information about open(), close(), read(), write(), lseek(), fstat(), and ioctl(), refer to
the "HAL API Reference" chapter of the Nios II Software Developer’s Handbook.
Related Information
HAL API Reference on page 14-1
Modifying the Global Error Status, errno
None of these functions directly modifies the global error status, errno. Instead, the return value is the
negation of the appropriate error code provided in errno.h.
For example, the ioctl() function returns -ENOTTY if it cannot handle a request rather than set errno to
ENOTTY directly. The HAL system routines that call these functions ensure that errno is set accordingly.
The function prototypes for these functions differ from their application level counterparts in that they
each take an input file descriptor argument of type alt_fd* rather than int.
A new alt_fd structure is created on a call to open(). This structure instance is then passed as an input
argument to all function calls made for the associated file descriptor.
The following code defines the alt_fd structure:
typedef struct
{
alt_dev* dev;
void* priv;
int fd_flags;
} alt_fd;
where:
dev is a pointer to the device structure for the device being used.
fd_flags is the value of flags passed to open().
priv is a reserved, implementation-dependent argument, defined by the driver. If the driver requires
any special, non-HAL-defined values to be maintained for each file or stream, you can store them in a
data structure, and use priv maintains a pointer to the structure. The HAL ignores priv.
Allocate storage for the data structure in your open() function (pointed to by the alt_dev structure).
Free the storage in your close() function.
Note:
To avoid memory leaks, ensure that the close() function is called when the file or stream is no
longer needed.
Default Behavior for Functions Defined in alt_dev
A driver is not required to provide all of the functions in the alt_dev structure. If a given function
pointer is set to NULL, a default action is used instead.
Table 7-1: Default Behavior for Functions Defined in alt_dev
Function Default Behavior
open Calls to open() for this device succeed, unless the device was
previously locked by a call to ioctl() with req = TIOCEXCL.
close Calls to close() for a valid file descriptor for this device always
succeed.
read Calls to read() for this device always fail.
7-6
Modifying the Global Error Status, errno
NII5V2
2015.05.14
Altera Corporation
Developing Device Drivers for the Hardware Abstraction Layer
Send Feedback