While getting to know how to write device drivers I came across IS_ERR() macro. But I couldn't find how is it working. I have include the code below:
majorNumber = register_chrdev(0, DEVICE_NAME, &fops);
if (majorNumber<0)
{
printk(KERN_ALERT "Failed to register a major number\n");
return majorNumber;
}
printk(KERN_INFO "Registered correctly with major number %d\n", majorNumber);
// Register the device class
ebbcharClass = class_create(THIS_MODULE, CLASS_NAME);
if (IS_ERR(ebbcharClass))
{
unregister_chrdev(majorNumber, DEVICE_NAME);
printk(KERN_ALERT "Failed to register device class\n");
return PTR_ERR(ebbcharClass);
}
So what does the IS_ERR() macro expand to and how it gets executed.
Tests if the supplied pointer should be considered an error value.
It does not check if the pointer is valid.
In your code IS_ERR is used to check if class_create
succeded creating ebbcharClass
. If an error occurs unregister the char driver and signal the error.
You can find MACROs and inline funcs in err.h
Be careful for pitfall:
#define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)
#define MAX_ERRNO 4095
This covers -1 to -4095, which represents error code, not number below 4096, nor NULL (0). Every value from 0 to 4294963201 (0xfffff001) is considered no error. Do not use it to cover NULL checking.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With