You are here:   Home | Device Driver Resources | Character device driver for the Delcom 7-segment display

Character device driver for the Delcom 7-segment display

Simple char device driver for the Delcom USB seven-segment display for Linux.

 Download Code Here...

 

Building and testing driver:

To build the module, a copy of the Linux kernel source tree for the running kernel needs to be installed.  This is usually located at /usr/src/linux.  From your favorite terminal, change to the directory containing the module source and make files and simply type 'make'.  This will create a module file called usbseven.ko.   

To load the module,  run as root.the load script by typing './usbseven_load'.  This script will call insmod to load the driver module.  After insmod has finished loading the driver module the script looks in the /proc/devices file for the major and minor device numbers of the newly loaded module.  It will then call mknod to create the special device file, usbseven, in /dev.  The mknod command takes the device major and minor numbers as arguments to specify which device is to be made accessable via the new device special file created in /dev. By default, during the module initialization, the driver will be automatically assigned a major device number.  To override this run the load script and pass the desired major number as a parameter: './usbseven_load usbseven_major=xxx' 

To unload the module run the script usbseven_unload as root.  This will unload the module and clean up the /dev directory. 

To test the driver run the python script test.py (type ./test.py or python test.py).  This will display the current time on the 7-segment display.  Alternatively try typing: './test 12.3456' to run the simple test.c program included.  This will display the number argument.  

 

 

Overview of char device driver code 

 

 

Loading and unloading kernel modules

Two macros must be defined within the module code (usbseven.c): 

  • module_init(usb_seven_init);
  • module_exit(usb_seven_exit); 

The usb_seven_init function will be called during the module loading procedure.  This function will call all the necessary initialization code and register the capabilities it can provide with the kernel. 

The usb_seven_exit function is called when the utility rmmod is run to unload the module from the kernel.  This function performs any clean up required, such as freeing memory. 

 

 

Kernel module initialization

The usb_seven_init function does the following:            

  •  Register the USB functionality with the USB core           
  •  Obtain the major and minor numbers for the device           
  • Register the functionality provided by the device driver with the kernel 

If any errors occur during the initialization of these three items then we must reverse any initialization done up to the point of failure, hence the use of gotos in the initialization code. 

To register with the USB subsystem a call to usb_register() is performed passing to it a pointer to a struct usb_driver.  The usb_driver struct contains pointers the probe and disconnect callback functions.   The probe function is called when the device is plugged in and the release function is called when the device is unplugged. 

If no major number is specified in the command line of the usbseven_load script then one will be assigned using the alloc_chrdev_region() function, otherwise register_chrdev_region() is called to register the desired major number with the kernel. 

The standard char device operations, open, read, write and close have corresponding functions in the device driver.  The open, read, write and release (close) functions are registered with the kernel by passing a file operations struct to the cdev_add() function.  As soon as this call returns the device driver is live and user space applications can perform file operations on the /dev/usbseven device file.  

 

 

References 

1.     Jonathon Corbet, Alessandro Rubini and Greg Kroah-Hartman. Linux Device Drivers, 3rd Edition, O'Reilly Media Inc. 2005.  See http://lwn.net/Kernel/LDD3/ for the online version.

2.    Delcom USB Numeric Display Manual. http://www.delcomproducts.com/ 

3.     Greg Kroah-Hartman has written a number of good articles about device drivers see: http://www.linuxjournal.com/user/800887/track and http://www.linuxjournal.com/article/7353  

 

User resources

Support center