Exploring BeagleBone: LKMs (by Derek Molloy)  V1.0
This project describes how you can build loadable kernel modules (LKMs) on your BeagleBone platform
 All Files Functions Variables Enumerations Enumerator Macros
Macros | Functions | Variables
ebbchar.c File Reference

An introductory character driver to support the second article of my series on Linux loadable kernel module (LKM) development. This module maps to /dev/ebbchar and comes with a helper C program that can be run in Linux user space to communicate with this the LKM. More...

#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
Include dependency graph for ebbchar.c:

Macros

#define DEVICE_NAME   "ebbchar"
 The device will appear at /dev/ebbchar using this value. More...
 
#define CLASS_NAME   "ebb"
 The device class – this is a character device driver. More...
 

Functions

 MODULE_LICENSE ("GPL")
 The license type – this affects available functionality. More...
 
 MODULE_AUTHOR ("Derek Molloy")
 The author – visible when you use modinfo. More...
 
 MODULE_DESCRIPTION ("A simple Linux char driver for the BBB")
 The description – see modinfo. More...
 
 MODULE_VERSION ("0.1")
 A version number to inform users. More...
 
static int dev_open (struct inode *inodep, struct file *filep)
 The device open function that is called each time the device is opened This will only increment the numberOpens counter in this case. More...
 
static int dev_release (struct inode *inodep, struct file *filep)
 The device release function that is called whenever the device is closed/released by the userspace program. More...
 
static ssize_t dev_read (struct file *filep, char *buffer, size_t len, loff_t *offset)
 This function is called whenever device is being read from user space i.e. data is being sent from the device to the user. In this case is uses the copy_to_user() function to send the buffer string to the user and captures any errors. More...
 
static ssize_t dev_write (struct file *filep, const char *buffer, size_t len, loff_t *offset)
 This function is called whenever the device is being written to from user space i.e. data is sent to the device from the user. The data is copied to the message[] array in this LKM using the sprintf() function along with the length of the string. More...
 
static int __init ebbchar_init (void)
 The LKM initialization function The static keyword restricts the visibility of the function to within this C file. The __init macro means that for a built-in driver (not a LKM) the function is only used at initialization time and that it can be discarded and its memory freed up after that point. More...
 
static void __exit ebbchar_exit (void)
 The LKM cleanup function Similar to the initialization function, it is static. The __exit macro notifies that if this code is used for a built-in driver (not a LKM) that this function is not required. More...
 
 module_init (ebbchar_init)
 A module must use the module_init() module_exit() macros from linux/init.h, which identify the initialization function at insertion time and the cleanup function (as listed above) More...
 
 module_exit (ebbchar_exit)
 

Variables

static int majorNumber
 Stores the device number – determined automatically. More...
 
static char message [256] = {0}
 Memory for the string that is passed from userspace. More...
 
static short size_of_message
 Used to remember the size of the string stored. More...
 
static int numberOpens = 0
 Counts the number of times the device is opened. More...
 
static struct class * ebbcharClass = NULL
 The device-driver class struct pointer. More...
 
static struct device * ebbcharDevice = NULL
 The device-driver device struct pointer. More...
 
static struct file_operations fops
 Devices are represented as file structure in the kernel. The file_operations structure from /linux/fs.h lists the callback functions that you wish to associated with your file operations using a C99 syntax structure. char devices usually implement open, read, write and release calls. More...
 

Detailed Description

An introductory character driver to support the second article of my series on Linux loadable kernel module (LKM) development. This module maps to /dev/ebbchar and comes with a helper C program that can be run in Linux user space to communicate with this the LKM.

Author
Derek Molloy
Date
7 April 2015
Version
0.1
See also
http://www.derekmolloy.ie/ for a full description and follow-up descriptions.

Macro Definition Documentation

#define CLASS_NAME   "ebb"

The device class – this is a character device driver.

#define DEVICE_NAME   "ebbchar"

The device will appear at /dev/ebbchar using this value.

Function Documentation

static int dev_open ( struct inode *  inodep,
struct file *  filep 
)
static

The device open function that is called each time the device is opened This will only increment the numberOpens counter in this case.

Parameters
inodepA pointer to an inode object (defined in linux/fs.h)
filepA pointer to a file object (defined in linux/fs.h)
107  {
108  numberOpens++;
109  printk(KERN_INFO "EBBChar: Device has been opened %d time(s)\n", numberOpens);
110  return 0;
111 }
static int numberOpens
Counts the number of times the device is opened.
Definition: ebbchar.c:30
static ssize_t dev_read ( struct file *  filep,
char *  buffer,
size_t  len,
loff_t *  offset 
)
static

This function is called whenever device is being read from user space i.e. data is being sent from the device to the user. In this case is uses the copy_to_user() function to send the buffer string to the user and captures any errors.

Parameters
filepA pointer to a file object (defined in linux/fs.h)
bufferThe pointer to the buffer to which this function writes the data
lenThe length of the b
offsetThe offset if required
121  {
122  int error_count = 0;
123  // copy_to_user has the format ( * to, *from, size) and returns 0 on success
124  error_count = copy_to_user(buffer, message, size_of_message);
125 
126  if (error_count==0){ // if true then have success
127  printk(KERN_INFO "EBBChar: Sent %d characters to the user\n", size_of_message);
128  return (size_of_message=0); // clear the position to the start and return 0
129  }
130  else {
131  printk(KERN_INFO "EBBChar: Failed to send %d characters to the user\n", error_count);
132  return -EFAULT; // Failed -- return a bad address message (i.e. -14)
133  }
134 }
static short size_of_message
Used to remember the size of the string stored.
Definition: ebbchar.c:29
static char message[256]
Memory for the string that is passed from userspace.
Definition: ebbchar.c:28
static int dev_release ( struct inode *  inodep,
struct file *  filep 
)
static

The device release function that is called whenever the device is closed/released by the userspace program.

Parameters
inodepA pointer to an inode object (defined in linux/fs.h)
filepA pointer to a file object (defined in linux/fs.h)
156  {
157  printk(KERN_INFO "EBBChar: Device successfully closed\n");
158  return 0;
159 }
static ssize_t dev_write ( struct file *  filep,
const char *  buffer,
size_t  len,
loff_t *  offset 
)
static

This function is called whenever the device is being written to from user space i.e. data is sent to the device from the user. The data is copied to the message[] array in this LKM using the sprintf() function along with the length of the string.

Parameters
filepA pointer to a file object
bufferThe buffer to that contains the string to write to the device
lenThe length of the array of data that is being passed in the const char buffer
offsetThe offset if required
144  {
145  sprintf(message, "%s(%d letters)", buffer, len); // appending received string with its length
146  size_of_message = strlen(message); // store the length of the stored message
147  printk(KERN_INFO "EBBChar: Received %d characters from the user\n", len);
148  return len;
149 }
static short size_of_message
Used to remember the size of the string stored.
Definition: ebbchar.c:29
static char message[256]
Memory for the string that is passed from userspace.
Definition: ebbchar.c:28
static void __exit ebbchar_exit ( void  )
static

The LKM cleanup function Similar to the initialization function, it is static. The __exit macro notifies that if this code is used for a built-in driver (not a LKM) that this function is not required.

94  {
95  device_destroy(ebbcharClass, MKDEV(majorNumber, 0)); // remove the device
96  class_unregister(ebbcharClass); // unregister the device class
97  class_destroy(ebbcharClass); // remove the device class
98  unregister_chrdev(majorNumber, DEVICE_NAME); // unregister the major number
99  printk(KERN_INFO "EBBChar: Goodbye from the LKM!\n");
100 }
static struct class * ebbcharClass
The device-driver class struct pointer.
Definition: ebbchar.c:31
#define DEVICE_NAME
The device will appear at /dev/ebbchar using this value.
Definition: ebbchar.c:19
static int majorNumber
Stores the device number – determined automatically.
Definition: ebbchar.c:27
static int __init ebbchar_init ( void  )
static

The LKM initialization function The static keyword restricts the visibility of the function to within this C file. The __init macro means that for a built-in driver (not a LKM) the function is only used at initialization time and that it can be discarded and its memory freed up after that point.

Returns
returns 0 if successful
58  {
59  printk(KERN_INFO "EBBChar: Initializing the EBBChar LKM\n");
60 
61  // Try to dynamically allocate a major number for the device -- more difficult but worth it
62  majorNumber = register_chrdev(0, DEVICE_NAME, &fops);
63  if (majorNumber<0){
64  printk(KERN_ALERT "EBBChar failed to register a major number\n");
65  return majorNumber;
66  }
67  printk(KERN_INFO "EBBChar: registered correctly with major number %d\n", majorNumber);
68 
69  // Register the device class
70  ebbcharClass = class_create(THIS_MODULE, CLASS_NAME);
71  if (IS_ERR(ebbcharClass)){ // Check for error and clean up if there is
72  unregister_chrdev(majorNumber, DEVICE_NAME);
73  printk(KERN_ALERT "Failed to register device class\n");
74  return PTR_ERR(ebbcharClass); // Correct way to return an error on a pointer
75  }
76  printk(KERN_INFO "EBBChar: device class registered correctly\n");
77 
78  // Register the device driver
79  ebbcharDevice = device_create(ebbcharClass, NULL, MKDEV(majorNumber, 0), NULL, DEVICE_NAME);
80  if (IS_ERR(ebbcharDevice)){ // Clean up if there is an error
81  class_destroy(ebbcharClass); // Repeated code but the alternative is goto statements
82  unregister_chrdev(majorNumber, DEVICE_NAME);
83  printk(KERN_ALERT "Failed to create the device\n");
84  return PTR_ERR(ebbcharDevice);
85  }
86  printk(KERN_INFO "EBBChar: device class created correctly\n"); // Made it! device was initialized
87  return 0;
88 }
static struct class * ebbcharClass
The device-driver class struct pointer.
Definition: ebbchar.c:31
static struct file_operations fops
Devices are represented as file structure in the kernel. The file_operations structure from /linux/fs...
Definition: ebbchar.c:44
#define DEVICE_NAME
The device will appear at /dev/ebbchar using this value.
Definition: ebbchar.c:19
#define CLASS_NAME
The device class – this is a character device driver.
Definition: ebbchar.c:20
static struct device * ebbcharDevice
The device-driver device struct pointer.
Definition: ebbchar.c:32
static int majorNumber
Stores the device number – determined automatically.
Definition: ebbchar.c:27
MODULE_AUTHOR ( "Derek Molloy"  )

The author – visible when you use modinfo.

MODULE_DESCRIPTION ( "A simple Linux char driver for the BBB"  )

The description – see modinfo.

module_exit ( ebbchar_exit  )
module_init ( ebbchar_init  )

A module must use the module_init() module_exit() macros from linux/init.h, which identify the initialization function at insertion time and the cleanup function (as listed above)

MODULE_LICENSE ( "GPL"  )

The license type – this affects available functionality.

MODULE_VERSION ( "0.1"  )

A version number to inform users.

Variable Documentation

struct class* ebbcharClass = NULL
static

The device-driver class struct pointer.

struct device* ebbcharDevice = NULL
static

The device-driver device struct pointer.

struct file_operations fops
static
Initial value:
=
{
.open = dev_open,
.read = dev_read,
.write = dev_write,
.release = dev_release,
}
static int dev_open(struct inode *, struct file *)
The device open function that is called each time the device is opened This will only increment the n...
Definition: ebbchar.c:107
static ssize_t dev_write(struct file *, const char *, size_t, loff_t *)
This function is called whenever the device is being written to from user space i.e. data is sent to the device from the user. The data is copied to the message[] array in this LKM using the sprintf() function along with the length of the string.
Definition: ebbchar.c:144
static ssize_t dev_read(struct file *, char *, size_t, loff_t *)
This function is called whenever device is being read from user space i.e. data is being sent from th...
Definition: ebbchar.c:121
static int dev_release(struct inode *, struct file *)
The device release function that is called whenever the device is closed/released by the userspace pr...
Definition: ebbchar.c:156

Devices are represented as file structure in the kernel. The file_operations structure from /linux/fs.h lists the callback functions that you wish to associated with your file operations using a C99 syntax structure. char devices usually implement open, read, write and release calls.

int majorNumber
static

Stores the device number – determined automatically.

char message[256] = {0}
static

Memory for the string that is passed from userspace.

int numberOpens = 0
static

Counts the number of times the device is opened.

short size_of_message
static

Used to remember the size of the string stored.