LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Better way to load/unload a kernel module (http://www.linuxquestions.org/questions/linux-general-1/better-way-to-load-unload-a-kernel-module-457273/)

seran 06-22-2006 11:03 AM

Better way to load/unload a kernel module
 
Hello,
I have a custum made "USB device" (given by my friend) and I am trying to make this device work on linux. I have written a kernel module for this device.

To load and unload this module whenever I plug-in/unplug this "USB device" I did some changes in the "/sbin/hotplug" script.

This is the change I did ...

if [ "$1" = "usb" ]
then
if [ "$ACTION" = "add" ]
then
if [ "$PRODUCT" = "82d/7124/110" ]
then
/sbin/insmod /home/seran/controller.o
fi
fi

if [ "$ACTION" = "remove" ]
then
if [ "$PRODUCT" = "82d/7124/110" ]
then
/sbin/rmmod controller
fi
fi
fi

I added the above condition before hotplug calls "usb.agent" script.
If the ACTION is "add" and if the PRODUCT id matches, it does an insmod of my module.
If the ACTION is "remove" and if the PRODUCT id matches, it does an rmmod of the module.

And in the "usb.agent" script I added the following lines just before the "case" statement.

if [ "$PRODUCT" = "3eb/6124/110" ]
then
exit 2
fi

This will avoid "usb.agent" proceeding further, because "hotplug" already did the required work.

Everything looks fine. But I personally feel this looks sloppy.

Is there any better way I can load/unload my module whenever I plug/unplug this device.

I am using Red Hat Linux release 9 and kernel 2.4.20-8.

Please give me some suggesstions.

Thanks
Seran

binary_y2k2 06-22-2006 11:35 AM

If you just want it to look good then I would have done it more like:
Code:

if [ "$1" = "usb" ];then
 if [ "$PRODUCT" = "82d/7124/110" ];then
  if [ "$ACTION" = "add" ];then
  /sbin/insmod /home/seran/controller.o
  elif [ "$ACTION" = "remove" ];then
  /sbin/rmmod controller
  fi
 fi
fi


seran 06-23-2006 05:55 AM

Quote:

...feel this looks sloppy
What I ment was, is there any other way without touching hotplug script, can the module will be loaded/unloaded?

Whenever I get a new device I have to make it work in linux. So, everytime adding some conditions inside "hotplug" to load/unload a particular module won't be nice.

I hope there will be in a smarter way this can achieved.

binary_y2k2 06-23-2006 09:19 AM

you could put them all in a seperet file and add the line
Code:

. /etc/devices $@
Then the hotplug script would source that file and pass any options to it.


All times are GMT -5. The time now is 11:02 PM.