My first device driver => "No such device" :(

My first device driver => "No such device" :(

Post by Espen San » Thu, 20 Nov 1997 04:00:00



Hello all. Finally I have decided to make a device driver. It is a
character based driver and it will be quite simple (I hope). It shall
be used in an embedded system that uses an i960 processor on the PCI
bus and my first goal is to allocate some (kernel) memory that both the
device and the user program can access.

So, I have made a minimalistic driver that shall be loaded with
"insmod". But I am not able to "open()" the device from a user
program. I get the error "No such device". Here is what I do:

1) Compile driver : gcc -O -DMODULE -D__KERNEL__ -c i960.c  
2) [ROOT]> mknod i960 c 150 0
3) [ROOT]> chmod a+rw /dev/i960      (as root)
4) [ROOT]> insmod i960.o
5) I then get
[ROOT] beren:/home/espen/r10/source/dev>lsmod
Module:        #pages:  Used by:
i960               1            0
ppp                5            1 (autoclean)
slhc               2    [ppp]   1 (autoclean)
3c59x              3            1 (autoclean)
3c509              2            1 (autoclean)
ipx                3            11 (autoclean)
st                 6            0
vfat               3            1 (autoclean)
BusLogic           6            2

Quote:>uname -a

Linux> beren.neo.no 2.0.27 #1 Sat Dec 21 23:44:11 EST 1996 i686
Quote:>gcc --version

2.7.2.1

Below is the driver + user program . Can anybody tell me what I am doing
wrong.

[USER PROGRAM]

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define PORT "/dev/i960"

// Works well with this variant
//#define PORT "/dev/zero"

int main( void )
{
  int fd;

  fd = open( PORT, O_RDWR );
  if( fd == -1 )
  {
    printf("Can't open \"%s\": %s\n", PORT, strerror( errno ));
    exit(1);
  }

  if( 1 )
  {
    char buf[10];
    int errCode, i;

    memset( buf, 1, sizeof(buf) );    
    errCode = read( fd, buf, sizeof( buf ) );
    if( errCode != sizeof(buf) )
    {
      printf(" Can not read \"%s\": %s\n", PORT, strerror( errno ));
      exit(2);
    }

    for( i=0; i<sizeof(buf); i++ )
    {
      printf("buf[%d]=%d\n", i, buf[i] );
    }

  }

  close( fd );
  exit( 0 );

Quote:}

[DRIVER]

//
// Compile with "gcc -O -DMODULE -D__KERNEL__ -c i960rp.c"
//

#include <linux/config.h>
#ifdef MODULE
#include <linux/module.h>
#include <linux/version.h>
#else
#define MOD_INC_USE_COUNT
#define MOD_DEC_USE_COUNT
#endif

#include <linux/types.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/errno.h>
#include <asm/segment.h>

#include <linux/bios32.h>
#include <linux/pci.h>

int i960_detect_pci( void );

//
// global data
//
static int i960_major;

static int i960_read( struct inode * node, struct file * file, char *
buf,
                      int count )
{
  int left;

  if( verify_area( VERIFY_WRITE, buf, count ) == -EFAULT )
  {
    return( -EFAULT );
  }
  for( left = count; left > 0; left-- )
  {
    //put_user_byte( 0, buf );
    buf++;
  }
  return( count );

Quote:}

static int i960_write( struct inode * inode, struct file * file,
                       const char * buf, int count )
{
  return( count );

Quote:}

static int i960_lseek( struct inode * inode, struct file * file,
                       off_t offset, int orig)
{
  return( file->f_pos );

Quote:}

static int i960_open( struct inode *inode, struct file * file )
{
  MOD_INC_USE_COUNT;
  return( 0 );

Quote:}

static void i960_release( struct inode *inode, struct file * file )
{
  MOD_DEC_USE_COUNT;

Quote:}

static struct file_operations i960_fops =
{
  i960_lseek,
  i960_read,
  i960_write,
  NULL,         // no special i960_readdir
  NULL,         // no special i960_select
  NULL,         // no special i960_ioctl
  NULL,         // no special i960_mmap
  i960_open,
  i960_release,
  NULL,         // no special i960_fsync
  NULL,         // no special i960_fasync
  NULL,         // no special i960_check_media_change
  NULL,         // no special i960_revalidate

Quote:};

#ifndef MODULE
long i960_init( long mem_start, long mem_end )
{
  if ( i960_major = register_chrdev(0, "i960", &i960_fops) )
  {
    printk("Unable to get major for i960 device\n");
    return( -EIO );
  }
  i960_detect_pci();
  return( mem_start );
Quote:}

#else
int init_module( void )
{
  if( (i960_major = register_chrdev( 150, "i960", &i960_fops)) == -EBUSY
)
  {
    printk("Unable to get major for i960 device\n");
    return( -EIO );
  }
  i960_detect_pci();
  return( 0 );
Quote:}

void cleanup_module( void )
{
  unregister_chrdev( i960_major, "i960" );
Quote:}

#endif

int i960_detect_pci( void )
{
  // Removed PCI detection code for simplicity
  return( 0 );

Quote:}

----
Espen Sand, Research Scientist at Norsk Elektro Optikk A/S


      Address: Solheimveien 62A, P.O.Box 384, N-1471 Skarer, Norway.

      Address: Evje terrasse 3B, N-1300 Sandvika, Norway.
----

 
 
 

My first device driver => "No such device" :(

Post by David Sye » Thu, 20 Nov 1997 04:00:00



> int init_module( void )
> {
>   if( (i960_major = register_chrdev( 150, "i960", &i960_fops)) == -EBUSY
> )
> .......
> void cleanup_module( void )
> {
>   unregister_chrdev( i960_major, "i960" );
> }

This isn't going to help I don't think, but register_chrdev returns 0 on
success when called with a non-zero first argument, so cleanup_module is
doomed to failure.

Dave.

 
 
 

1. PATCH: eisa reports "0 device" not "0 devices"

Since it gets 1 device right it wasnt hard to fix 8)

diff -u --new-file --recursive --exclude-from /usr/src/exclude linux-2.5.65-bk3/drivers/eisa/eisa-bus.c linux-2.5.65-ac3/drivers/eisa/eisa-bus.c
--- linux-2.5.65-bk3/drivers/eisa/eisa-bus.c    2003-03-22 19:33:35.000000000 +0000

                        eisa_register_device (root, str, i);
                 }
         }
-        printk (KERN_INFO "EISA: Detected %d card%s.\n", c, c < 2 ? "" : "s");
+        printk (KERN_INFO "EISA: Detected %d card%s.\n", c, c == 1 ? "" : "s");

        return 0;
 }
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in

More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

2. JServDefaultHost name can't be resolved

3. Error Message "No space on device on device 31/50" on Free SCO

4. Space Station uses 95/NT, disaster imminent (no joke)

5. Installation program terminates "NOTICE: ram: No space on device on device 31/50"

6. setting shell to none

7. Q: solaris2.4: "no such device" where device has previously been used

8. cannot start X

9. Q: 2.4: "no such device" messages when accessing a Magneto-Optical device

10. GETSERVBYNAME()????????????????????"""""""""""""

11. "attempt to access beyond end of device" (but there is no such device!)

12. Weird :- "show-devs" OK but no" /devices" entry ??

13. How do i configure my "boot-device" and "boot-file" (LinuxPPC)?