Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

developerWorks Community:

  • Close [x]

IBM AIX device driver development

A tutorial on AIX device driver framework and related APIs

Gautam Raut (gautraut@in.ibm.com), Senior Software Engineer, IBM
Gautam
Gautam Raut works as a Senior Software Engineer for the Andrew File System (AFS) Team at the IBM Software Labs in Pune, India. He works with kernel and user-level debugging of dumps and crashes, as well as reported bugs on the Linux and AIX platforms. He has also worked on various AFS-specific AIX kernel components. Gautam holds M.S. in Computer Science from the University of Pune. He is a Linux enthusiast who spends his spare time exploring the Linux kernel on his Fedora 8 box.

Summary:  This tutorial illustrates the IBM® AIX® device driver framework and related application programming interfaces (APIs). Here, we shall discuss about the device switch structure, API to register device switch methods, major and minor number management, and moving data from kernel to user space.

Date:  28 Mar 2013
Level:  Intermediate PDF:  A4 and Letter (244 KB | 15 pages)Get Adobe® Reader®

Activity:  20933 views
Comments:  

Example of a device driver

sample_driver.c

#include <stdio.h>
#include <syslog.h>
#include <sys/types.h> /* for dev_t and other types */
#include <sys/errno.h> /* for errno declarations */
#include <sys/sysconfig.h> /* for sysconfig() */
#include <sys/device.h> /* for devsw */
#include <sys/uio.h> /* for uiomove */
#include <sys/sysmacros.h>

struct dr_data
{
  char buffer[1024];
}dr_data[5];

int dr_open(dev_t devno, ulong devflag, chan_t chan, int ext)
{
   bsdlog(LOG_KERN|LOG_DEBUG,"Inside dr_open\n");
   return 0;
}


int dr_close (dev_t devno, chan_t chan)
{
   bsdlog(LOG_KERN|LOG_DEBUG,"Inside dr_close \n");
   return 0;
}

int dr_read (dev_t devno, struct uio *uiop, chan_t chan, int ext)
{
   uint min;
   int rc;

   min = minor_num(devno);
   rc = uiomove(dr_data[min].buffer, 1024, UIO_READ, uiop);
   bsdlog(LOG_KERN | LOG_DEBUG, "Inside dr_read min: 
   %d, buffer: %s \n", min, dr_data[min].buffer);
   return rc;
}

int dr_write (dev_t devno, struct uio *uiop, chan_t chan, int ext)
{
   uint min;
   int rc;

   min = minor_num(devno);
   rc = uiomove(dr_data[min].buffer, 1024, UIO_WRITE, uiop);
   bsdlog(LOG_KERN | LOG_DEBUG,"Inside dr_write min: 
   %d, buffer: %s \n", min, dr_data[min].buffer);
   return rc;
}

int driverdd_config (dev_t devno, int cmd, struct uio *uiop)
{
   struct devsw dswp;
   int rc = 0;

   switch (cmd)
   {
       case CFG_INIT:
            dswp.d_open     = dr_open;
            dswp.d_close    = dr_close;
            dswp.d_read     = dr_read;
            dswp.d_write    = dr_write;
            dswp.d_ioctl    = nodev;
            dswp.d_strategy = nodev;
            dswp.d_ttys     = NULL;
            dswp.d_select   = nodev;
            dswp.d_config   = driverdd_config;
            dswp.d_print    = nodev;
            dswp.d_dump     = nodev;
            dswp.d_mpx      = nodev;
            dswp.d_revoke   = nodev;
            dswp.d_dsdptr   = NULL;
            dswp.d_selptr   = NULL;
            dswp.d_opts     = DEV_MPSAFE|DEV_64BIT;



            if((rc = devswadd(devno, &dswp)) != 0)
            {
                rc = major_num(devno);
                printf("Error in devswadd: %d\n", rc);
                return rc;
            }
            break;

        case CFG_TERM:
            if((rc = devswdel(devno)) != 0)
            {
                printf("Error in devswdel: %d\n", rc);
                return rc;
            }
            break;
        default:
             printf("Invalid command \n");
            return EINVAL;
    }
    return 0;
}

10 of 15 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=AIX and UNIX, Linux
ArticleID=861832
TutorialTitle= IBM AIX device driver development
publish-date=03282013
author1-email=gautraut@in.ibm.com
author1-email-cc=