How to set errno in Linux device driver?
Nice question!
Ok, you could think of errno as global variable (to be honnest, it is an extern int). errno has plenty of pre-defined macros for errorcodes in the errno.h library. You can have a look here. It is very likely that some of these errorcodes describe what you want to show. Pick up the right one, set it like if it was a variable you defined, and (important!) exit immediately!
You may ask yourself though if setting errno is the right approach to your problem. You can always define an (*int) and develop your own error codes, and error handling mechanism. Errno's purpose is to show and explain system errors. Do u consider your code part of the "system" (as I can see you develop your own system call, so this might be the case) ? So go on and use errno to explain your "system error".
Edit (On question update): Ok more info. As i said errno is an extern int and is set by the kernel. The value at which errno is set is simply the return value of the system call. Linux kernel then interprets this negative value through the library errno.h. So an example error message is set simply by returning (EBUSY is just an example - you can use all of the predifined error types) the error message you want from your system call. Example:
return -EBUSY
Hope it helps
multiple definition error in linux device driver
There's 3 common options.
Don't define the firmware in a header file, define it in a .c file and create the
functions you need to work with that firmware within that .c file. Expose those functions in your header file.Make the array static, so it's not visible in other translation units:
like so:
static const unsigned char ucPICAppsectorFirmware[] = ....
Note that this will create a copy of the array in every .c file where you include this header file.
- Place the array in a .c file, and declare it instead of defining it in the header file. This way the array would not be duplicated for each file that includes it, as would be the case when defining the array in the header file.
i.e. the header file would look like.
extern const unsigned char ucPICAppsectorFirmware[];
extern const size_t ucPICAppsectorFirmwareLen;
and the .c file would look like
const unsigned char ucPICAppsectorFirmware[] = ...;
const size_t ucPICAppsectorFirmwareLen = sizeof ucPICAppsectorFirmware;
Is any errno a valid POSIX return value for read()?
The Linux manpages are generally pretty good, but they are not authoritative. For a question like this, you need to go to the official standards. You should only return one of the error codes listed in the POSIX.1-2008 specification for read
(scroll down to where it says ERRORS). If you return some other code, application software will not be prepared to handle it and may crash.
Be aware that many of those codes may only be returned under the conditions described for them. For instance, your device driver has no business returning EBADF
, EISDIR
, EOVERFLOW
, ESPIPE
, or anything with the word "socket" in its description, because all the circumstances where those are the appropriate failure codes are dealt with before control reaches you. This is probably also the case for EINTR
but I'm not sure.
You also should know that Linux doesn't implement some of the optional features of POSIX.1-2008 (most importantly here, pretend everything marked OB_XSR isn't there at all) and that some of the distinctions made by the standard are obsolescent (such as ENOMEM
versus ENOBUFS
).
Putting it all together, unless your device driver is very unusual, I expect you should only need EIO
, EAGAIN
, ENOMEM
, and maybe ENXIO
(if the EIO
/ENXIO
distinction is meaningful for your hardware).
Related Topics
Linux Telnet Vt100 Return Key Sends ^M
Ssh Command Output to Save in a Text File in Shell Script
Problem of Understanding Clock_Gettime
Dreaming of Making My Own Os- What Should I Use? (Suggestions)
Where The Structure "Struct Page" Is Stored on The Linux Kernel
How to Prevent Remote Branch Deletion in Git Without Using Gitolite
Avoid Daemon Running in Dedicated CPU Cores
Using Winscp to Grab a File Through a Tunnel
How to Make Linux Ignore a Keyboard While Keeping It Available for My Program to Read
How to Get The Output of at Command in Current or Another Terminal Window
How to Distinguish Between Different Operating System Distros in Node.Js
Getting "Permission Denied" on Dirname and Basename
Linux: How to Check The Largest Contiguous Address Range Available to a Process
How Event Packet Header Is Getiing in Hci_Send_Req API Implementation