Tuesday 23 July 2013

The real “parlelport” driver



The real “parlelport” driver: description of the parallel port
I’ll now proceed by modifying the driver that I just created to develop one that does a real task on a real device. I’ll use the simple and ubiquitous computer parallel port and the driver will be called parlelport.
The parallel port is effectively a device that allows the input and output of digital information. More specifically it has a female D-25 connector with twenty-five pins. Internally, from the point of view of the CPU, it uses three bytes of memory. In a PC, the base address (the one from the first byte of the device) is usually 0x378. In this basic example, I’ll use just the first byte, which consists entirely of digital outputs.
The connection of the above-mentioned byte with the external connector pins is shown in figure 2.
Figure 2: The first byte of the parallel port and its pin connections with the external female D-25 connectorFigure 2: The first byte of the parallel port and its pin connections with the external female D-25 connector
The “parlelport” driver: initializing the module
The previous memory_init function needs modification—changing the RAM memory allocation for the reservation of the memory address of the parallel port (0x378). To achieve this, use the function for checking the availability of a memory region (check_region), and the function to reserve the memory region for this device (request_region). Both have as arguments the base address of the memory region and its length. The request_region function also accepts a string which defines the module.
<parlelport modified init module> =
  /* Registering port */
  port = check_region(0x378, 1);
  if (port) {
    printk("<1>parlelport: cannot reserve 0x378\n");
    result = port;
    goto fail;
  }
  request_region(0x378, 1, "parlelport");
The “parlelport” driver: removing the module
It will be very similar to the memory module but substituting the freeing of memory with the removal of the reserved memory of the parallel port. This is done by the release_region function, which has the same arguments as check_region.
<parlelport modified exit module> =
  /* Make port free! */
  if (!port) {
    release_region(0x378,1);
  }
The “parlelport” driver: reading the device
In this case, a real device reading action needs to be added to allow the transfer of this information to user space. The inb function achieves this; its arguments are the address of the parallel port and it returns the content of the port.
<parlelport inport> =
/* Reading port */
parlelport_buffer = inb(0x378);
Table 9 (the equivalent of Table 2) shows this new function.


Events
Kernel functions
Read data
inb
Write data

Device driver events and their associated functions between kernel space and the hardware device.
The “parlelport” driver: writing to the device
Again, you have to add the “writing to the device” function to be able to transfer later this data to user space. The function outb accomplishes this; it takes as arguments the content to write in the port and its address.
<parlelport outport> =
/* Writing to the port */
outb(parlelport_buffer,0x378);
Table 10 summarizes this new function.


Events
Kernel functions
Read data
inb
Write data
outb
Device driver events and their associated functions between kernel space and the hardware device.
The complete “parlelport” driver
I’ll proceed by looking at the whole code of the parlelport module. You have to replace the word memory for the word parlelport throughout the code for the memory module. The final result is shown below:
<parlelport.c> =
<parlelport initial>
<parlelport init module>
<parlelport exit module>
<parlelport open>
<parlelport release>
<parlelport read>
<parlelport write>
Initial section
In the initial section of the driver a different major number is used (61). Also, the global variable memory_buffer is changed to port and two more #include lines are added: ioport.h and io.h.
<parlelport initial> =
/* Necessary includes for drivers */
#include <linux/init.h>
#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h> /* printk() */
#include <linux/slab.h> /* kmalloc() */
#include <linux/fs.h> /* everything... */
#include <linux/errno.h> /* error codes */
#include <linux/types.h> /* size_t */
#include <linux/proc_fs.h>
#include <linux/fcntl.h> /* O_ACCMODE */
#include <linux/ioport.h>
#include <asm/system.h> /* cli(), *_flags */
#include <asm/uaccess.h> /* copy_from/to_user */
#include <asm/io.h> /* inb, outb */

MODULE_LICENSE("Dual BSD/GPL");

/* Function declaration of parlelport.c */
int parlelport_open(struct inode *inode, struct file *filp);
int parlelport_release(struct inode *inode, struct file *filp);
ssize_t parlelport_read(struct file *filp, char *buf,
                       size_t count, loff_t *f_pos);
ssize_t parlelport_write(struct file *filp, char *buf,
                       size_t count, loff_t *f_pos);
void parlelport_exit(void);
int parlelport_init(void);

/* Structure that declares the common */
/* file access fcuntions */
struct file_operations parlelport_fops = {
  read: parlelport_read,
  write: parlelport_write,
  open: parlelport_open,
  release: parlelport_release
};

/* Driver global variables */
/* Major number */
int parlelport_major = 61;

/* Control variable for memory */
/* reservation of the parallel port*/
int port;

module_init(parlelport_init);
module_exit(parlelport_exit);
Module init
In this module-initializing-routine I’ll introduce the memory reserve of the parallel port as was described before.
<parlelport init module> =
int parlelport_init(void) {
  int result;

  /* Registering device */
  result = register_chrdev(parlelport_major, "parlelport",
      &parlelport_fops);
  if (result < 0) {
    printk(
      "<1>parlelport: cannot obtain major number %d\n",
      parlelport_major);
    return result;
  }
  
  <parlelport modified init module>

  printk("<1>Inserting parlelport module\n");
  return 0;

  fail:
    parlelport_exit();
    return result;
}
Removing the module
This routine will include the modifications previously mentioned.
<parlelport exit module> =
void parlelport_exit(void) {

  /* Make major number free! */
  unregister_chrdev(parlelport_major, "parlelport");

  <parlelport modified exit module>

  printk("<1>Removing parlelport module\n");
}
Opening the device as a file
This routine is identical to the memory driver.
<parlelport open> =
int parlelport_open(struct inode *inode, struct file *filp) {

  /* Success */
  return 0;

}
Closing the device as a file
Again, the match is perfect.
<parlelport release> =
int parlelport_release(struct inode *inode, struct file *filp) {

  /* Success */
  return 0;
}
Reading the device
The reading function is similar to the memory one with the corresponding modifications to read from the port of a device.
<parlelport read> =
ssize_t parlelport_read(struct file *filp, char *buf,
  size_t count, loff_t *f_pos) {

  /* Buffer to read the device */
  char parlelport_buffer;

  <parlelport inport>

  /* We transfer data to user space */
  copy_to_user(buf,&parlelport_buffer,1);
 
  /* We change the reading position as best suits */
  if (*f_pos == 0) {
    *f_pos+=1;
    return 1;
  } else {
    return 0;
  }
}
Writing to the device
It is analogous to the memory one except for writing to a device.
<parlelport write> =
ssize_t parlelport_write( struct file *filp, char *buf,
  size_t count, loff_t *f_pos) {

  char *tmp;

  /* Buffer writing to the device */
  char parlelport_buffer;

  tmp=buf+count-1;
  copy_from_user(&parlelport_buffer,tmp,1);

  <parlelport outport>
 
  return 1; 
}

No comments:

Post a Comment