Find the answer to your Linux question:
Results 1 to 6 of 6
Hi, I've compiled a paralel port character device sample , which i taken from a web-site. It's compiled but it doesnt give the result that it must give. Code: Code: ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2010
    Posts
    6

    [SOLVED] Parallel Port , inb() Error


    Hi,
    I've compiled a paralel port character device sample , which i taken from a web-site.
    It's compiled but it doesnt give the result that it must give.

    Code:
    Code:
    /* Necessary includes for drivers */
    #include <linux/init.h>
    #include <linux/autoconf.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 */
    #include <linux/device.h>  // class ve device structlari ve methodlari icin gerekli 
    
    #define DEVICE_NAME "parlelport"
    #define ERROR  -1
    
    MODULE_LICENSE("Dual BSD/GPL");
    MODULE_AUTHOR("Nazmi ALTUN");
    MODULE_DESCRIPTION("HOMEWORK PARALEL-PORT CHARACTER DEVICE");
    
    /* 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);
    static int mountDevice(void); 
    static void unmountDevice(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;
    
    static char parlelport_buffer  ;
    static struct class *dev_Class =NULL; // Bu struct yapisini ekledik , bu device'a ait bir class yaratiyor 
    
    module_init(parlelport_init);
    module_exit(parlelport_exit);
    
    int parlelport_init(void) {
      int result;
      /* Registering device */
      result = register_chrdev(parlelport_major, DEVICE_NAME,
          &parlelport_fops);
      if (result < 0) {
        printk(
          "<1>parlelport: cannot obtain major number %d\n",
          parlelport_major);
        return result;
      }
      /* Registering port */
      port = check_region(0x378, 1);
      if (port) {
      printk("<1>parlelport: cannot reserve 0x378\n");
      result = port;
      goto fail;
      }
      request_region(0x378, 1, DEVICE_NAME);
     
      result = mountDevice();
      if( result )
    	return ERROR;
    
      printk("<1>Inserting parlelport module\n");
      return 0;
      fail:
      parlelport_exit();
      return result;
    }
    int mountDevice(void)
    {
      struct device *chr_dev =NULL ;  // Device structi , yeni olusan device'i atiyacagiz bu pointera
      dev_Class = class_create(THIS_MODULE,DEVICE_NAME);  // yeni class olusturduk 
      if( dev_Class == NULL)
      {
    	printk( KERN_ALERT "Device ait olan class olusturulurken hata olustu\n" );
    	return 1 ;
      }
      printk( KERN_INFO "Device'a ait olan class olusturuldu\n" );
    	/*
    	device_create = creates a device and registers it with sysfs 
    	MKDEV( Major_No ,0 ) --> Burdaki 2. parametre , minor nodur.
    	*/
      chr_dev = device_create( dev_Class , NULL , MKDEV(parlelport_major,0),NULL,DEVICE_NAME);
    	
      if( chr_dev == NULL )
      {
    	printk( KERN_ALERT "Device  dev dizininde olusturulurken hata meydana geldi!\n" );
    	return 1;
      }
      printk( KERN_INFO "Device mount edildi! \\dev dizininde ulasilabilir.\n" );
      return 0 ;	
    }
    void unmountDevice()
    {
        device_destroy(dev_Class,MKDEV(parlelport_major,0));
        class_destroy(dev_Class); 
    }
    void parlelport_exit(void) {
       unmountDevice() ;  // <-- once mount et , 
       /* Make major number free! */
       unregister_chrdev(parlelport_major, DEVICE_NAME);
      /* Make port free! */
       if (!port) {
    	release_region(0x378,1);
       }
       printk("<1>Removing parlelport module\n");
    }
    int parlelport_open(struct inode *inode, struct file *filp) {
       /* Success */
      return 0;
    }
    int parlelport_release(struct inode *inode, struct file *filp) {
      /* Success */
      return 0;
    }
    ssize_t parlelport_read(struct file *filp, char *buf,
      size_t count, loff_t *f_pos) {
    
      /* Reading port */
      parlelport_buffer = inb(0x378);
    
      /* 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;
      }
    }
    ssize_t parlelport_write( struct file *filp, char *buf,
      size_t count, loff_t *f_pos) {
      char *tmp;
      /* Buffer writing to the device */
      tmp=buf+count-1;
      copy_from_user(&parlelport_buffer,tmp,1);
    
      /* Writing to the port */
      outb(parlelport_buffer,0x378);
    
      return 1;
    }
    After inserting modüle , when i sent parameter with echo such as
    Code:
    echo -n A > /dev/parlelport
    and read the value
    Code:
    cat /dev/parlelport
    It's output should be A but it writes '?' to command line.

    What can be the problem ?

  2. #2
    Just Joined!
    Join Date
    Mar 2010
    Posts
    6
    By the way ,
    When i print the value ,that inb() read ,to syslog with ;
    Code:
    printk( KERN_INFO "%d\n",(int)parlelport_buffer )
    The output on systemlog is -1.

    I guess , inb returns error while it reads.
    Why inb fails ? What can be the reason?

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    11,174
    Is this a real parallel port, or a parallel port over USB? In any case, what status comes back in errno?
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  4. #4
    Just Joined!
    Join Date
    Mar 2010
    Posts
    6
    Quote Originally Posted by Rubberman View Post
    Is this a real parallel port, or a parallel port over USB? In any case, what status comes back in errno?
    This is a real parallel port,i use linux on my physical machine,378 is parallel port number of LPT.
    inb() function returns error. It returns value of -1 but it shall returns a value between 0-255 .

    I found out what error is.( I guess )
    Problem is with my LPT port on my Computer.

  5. #5
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    11,174
    Well, it is returning a byte, and 255 (0xff hex) in a byte (char) is -1 as an int...
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  6. #6
    Linux Newbie unlimitedscolobb's Avatar
    Join Date
    Jan 2008
    Posts
    120
    Quote Originally Posted by Dufresne View Post
    Hi,
    After inserting modüle , when i sent parameter with echo such as
    Code:
    echo -n A > /dev/parlelport
    and read the value
    Code:
    cat /dev/parlelport
    It's output should be A but it writes '?' to command line.
    I might be wrong, but I don't remember the parallel port to be doing any buffering. That is, if you have nothing connected to your (real) parallel port or what you have connected has nothing to tell you, inb has really nothing to read and return -1, regardless of what you have just written. However, I've never used inb, so you'd better check.

    In other words, don't try to see the parallel port as a location where you can store and retrieve something.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •