LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-26-2009, 08:42 AM   #1
LEICIF
LQ Newbie
 
Registered: Nov 2009
Posts: 11

Rep: Reputation: 0
Catch LPT interrupt in user space.


Hello,

Is it possible to catch LPT interrupt [IRQ7] in user space???

I have connected D0 to ACK signal and I use this code to generate interrupt:

Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/io.h>
#include <termios.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <sys/types.h>

#define BASEPORT 0x378 /* lp1 */

void signal_handler_IO (int status);

int main()
{
int i;

  /* Get access to the ports */
  if (ioperm(BASEPORT, 3, 1)) {perror("ioperm"); return(1);}

  //set port to interrupt mode; pins are output
  outb(0x00, BASEPORT + 2);
  outb(0x10, BASEPORT + 2);
  
  for (i = 0 ; i < 10 ; i++ )
  {
	  usleep(500000);
	  outb(0, BASEPORT);
	  usleep(500000);
	  outb(15, BASEPORT);
  }

  outb(0, BASEPORT);

  /* Read from the status port (BASE+1) and display the result */
  printf("status: %d\n", inb(BASEPORT + 1));

  /* We don't need the ports anymore */
  if (ioperm(BASEPORT, 3, 0)) {perror("ioperm"); return(1);}

  return 0;
}
It works, because after execute program, I make "cat /proc/interrupt" and show we:

7: 20 XT-PIC-XT parport0.

The problem is I don't known how to catch this interrupt in user space, perhaps like signal........

Could some body tell more or less if I can do it??
Best reagards.
 
Old 11-26-2009, 06:00 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650
I don't think that's possible, to catch an interrupt you would need to replace the interrupt handler that is already present, this would require a kernel module/driver
 
Old 11-27-2009, 04:02 AM   #3
LEICIF
LQ Newbie
 
Registered: Nov 2009
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by kbp View Post
I don't think that's possible, to catch an interrupt you would need to replace the interrupt handler that is already present, this would require a kernel module/driver
Hello kbp,

Ok, no problem. I made a module/driver and it works fine.

Code:
#include <linux/slab.h>
#include <linux/sched.h>
#include <linux/module.h>
#include <linux/interrupt.h>
#include <asm/io.h>
#include <linux/time.h>
#include <linux/ioport.h>

#define BASEPORT 0x378
#define LPT_INTERRUPT 7

unsigned int contador;

irq_handler_t handler(void)
{
        // do stuff
	outb_p(15, BASEPORT);
        printk(">>> PARALLEL PORT INT HANDLED %d\n", contador++);

	return 0;
}

int xinit_module(void)
{
int ret;
struct resource *lpt_res;

	contador = 0;
	// Request IRQ 7
        ret = request_irq(LPT_INTERRUPT, (irq_handler_t) handler, IRQF_DISABLED , "LEICIF", NULL);
	if (ret < 0)
	{
		printk("Error, can not request irq 7");	
		return (-1);
	}
	// Request access io region LPT
	lpt_res = request_region(BASEPORT, 3, "BASEPORT");
	if (lpt_res == NULL)
	{
		printk("Error en request_region.\n");
		return(-1);
	}

	// ENCIENDO UN LED
	outb_p(0x01, BASEPORT);

        //set port to interrupt mode; pins are output
        outb_p(0x00, BASEPORT + 2);
        outb_p(0x10, BASEPORT + 2);

        printk("wait for interrupt\n");

        // Here generate interrupt D0 signal connect to ACK signal
        outb(0, BASEPORT);
        outb(1, BASEPORT);

        printk("return code %d \n", ret);
        return 0;
}

void xcleanup_module(void)
{
        disable_irq(LPT_INTERRUPT);
        free_irq(LPT_INTERRUPT, NULL);
        printk("closed\n");
}

module_init(xinit_module);
module_exit(xcleanup_module);
MODULE_LICENSE("GPL");
This code works fine.
Code:
#insmod mydriver.ko
wait for interrupt
>>> PARALLEL PORT INT HANDLED 1
return code 0
#
And now, the big question, Could module driver send something (signal, etc) to user space program??

Best regards.
 
Old 11-29-2009, 04:35 AM   #4
webquinty
Member
 
Registered: Apr 2008
Location: Espaņa
Distribution: Suse
Posts: 218

Rep: Reputation: 32
OK,

Perhaps you can use this code.

In kernel module, inside interrupt function handler:

Code:
 
struct task_struct *p;
p = find_task_by_pid( PID );
send_sig(SIGNAL,p,0);
And in user space program:

Code:
struct sigaction a_action;

	a_action.sa_handler = handler; 
	sigaction(SIGNAL,&a_action,NULL);

Last edited by webquinty; 11-29-2009 at 04:39 AM.
 
Old 11-29-2009, 04:44 AM   #5
LEICIF
LQ Newbie
 
Registered: Nov 2009
Posts: 11

Original Poster
Rep: Reputation: 0
Thanks a lot,

It works fine.

But I have a doubt, what signal number ca I use to send signal??? Can I use 0,1,2, or there are a free range of signals to use???

Best regards
 
Old 03-17-2010, 04:03 PM   #6
10110111
Member
 
Registered: Jun 2008
Location: St.-Petersburg, Russia
Distribution: (B)LFS, Ubuntu, SliTaz
Posts: 402

Rep: Reputation: 51
You can use USR1 and USR2
 
Old 02-27-2011, 06:56 AM   #7
granaos
LQ Newbie
 
Registered: Feb 2011
Posts: 1

Rep: Reputation: 0
can't catch irq

I have the same program but nothing happens.
It doesn't work. No problems at compiling time, but when I try to insert the module via : "insmod catch.ko" i got this when i ran "dmesg":
...
Error, can not request irq 7

This is what I have before try to insert the module.

#lsmod | more
...
parport_pc 15799 0
...
parport 22554 1 parport_pc
...

#cat /proc/interrupts
...
7: 1 IO-APIC-edge parport0
...

#cat /proc/ioports
...
0378-037a : parport0
...

And the lpt registers have this values:
baseport =0x04
baseport+1=0x7f
baseport+2=0x00


Inserting the module catch.ko was done when I removed parport_pc via "rmmod parport_pc", but registers values got wrong values; all them where 0xff. And got this output.

When I ran "dmesg"
[11549.797978] wait for interrupt
[11549.797987] return code 0


#lsmod | more
...
catch 973 0
parport 22554 1 parport_pc
...
#cat /proc/interrupts
...
7: 1 IO-APIC-edge LEICIF
...
#cat /proc/ioports
...
0378-037a : BASEPORT
...
And the lpt registers have this values:
baseport =0xff
baseport+1=0xff
baseport+2=0xff

So, i dont know what to do, please If someone can help I'll appreciate.
Also i can give remote access to my box in order to help insert that module into the kernel.

Granaos
 
Old 06-24-2011, 08:17 AM   #8
caco3
LQ Newbie
 
Registered: Jun 2011
Posts: 4

Rep: Reputation: Disabled
Does anybody have a working example for a kernel 3.6.37 (or another newer one)?
I tried several examples, but with none of them I can generate an interrupt
They compile and can get loaded.
Most of them generate a test interrupt at initialisation, but I can not even get that one.

I shorted pin 2 and 10, also forced once all other pins to GND (pin 18) as well as +5V.

I tried IRQ 5 as well as 7, also port 0x378 as well as 0x3BC.
Can load it under all conditions, but never receive an interrupt.
cat /proc/interrupts shows always 0 for IRQ 5 or 7.
with cat /proc/ioports I can see that the module uses the right address.

I had this once working with kernel 2.4 on an embedded PC, but now I would like to use it on my old Thinkpad R40.

Thank you for your advice
 
Old 06-24-2011, 04:25 PM   #9
caco3
LQ Newbie
 
Registered: Jun 2011
Posts: 4

Rep: Reputation: Disabled
All right

After many more hours of tracing, I figured out that I have to set the parallel port into ECP-Mode in the BIOS.
After that, it works.
 
Old 06-25-2011, 12:15 PM   #10
caco3
LQ Newbie
 
Registered: Jun 2011
Posts: 4

Rep: Reputation: Disabled
Update:
Sadly it still doesn't work as a kernel module
I tried EPP as well as ECP mode, but no difference.

I got it working as a user space program, as explained here: http://xenoscope.net/weblog/2008/09/...pace-in-linux/
How ever for time critical application this is not usable.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Mechanism for interrupt report to user space zvivered Linux - Embedded & Single-board computer 0 08-11-2009 04:59 PM
interrupt with push buttons through Linux - driver or user space? lila780 Linux - Newbie 1 12-30-2008 06:31 AM
how to trensfer data from user space to interrupt handler? rose_hatami Programming 4 12-19-2007 06:17 AM
how to catch USB Interrupt in 'c' zoobave Programming 1 06-15-2007 04:35 AM
can interrupt handler call user space function? pot_po Programming 0 11-07-2002 10:32 PM


All times are GMT -5. The time now is 05:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration