2.4. Module Wrapper

Finally we add in the usual module wrapping and the driver is done.


#ifndef MODULE

static int io = 0x300;

#else

static int io = -1;


MODULE_AUTHOR("Alan Cox");
MODULE_DESCRIPTION("A driver for an imaginary radio card.");
MODULE_PARM(io, "i");
MODULE_PARM_DESC(io, "I/O address of the card.");

EXPORT_NO_SYMBOLS;

int init_module(void)
{
        if(io==-1)
        {
                printk(KERN_ERR 
         "You must set an I/O address with io=0x???\n");
                return -EINVAL;
        }
        return myradio_init(NULL);
}

void cleanup_module(void)
{
        video_unregister_device(&my_radio);
        release_region(io, MY_IO_SIZE);
}

#endif

  

In this example we set the IO base by default if the driver is compiled into the kernel where you cannot pass a parameter. For the module we require the user sets the parameter. We set io to a nonsense port (-1) so that we can tell if the user supplied an io parameter or not.

We use MODULE_ defines to give an author for the card driver and a description. We also use them to declare that io is an integer and it is the address of the card.

The clean-up routine unregisters the video_device we registered, and frees up the I/O space. Note that the unregister takes the actual video_device structure as its argument. Unlike the file operations structure which can be shared by all instances of a device a video_device structure as an actual instance of the device. If you are registering multiple radio devices you need to fill in one structure per device (most likely by setting up a template and copying it to each of the actual device structures).