LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-03-2006, 02:03 PM   #1
nick021
Member
 
Registered: Jan 2005
Location: india
Distribution: slackware 10.0 (kernel 2.4.26)
Posts: 104

Rep: Reputation: 15
unable to include header file..


hi everybody.....
here is my code for printer driver...
i m compiling it with

bash-2.05b# gcc -O3 -I /usr/src/linux-2.4.26/include/linux/ -c -o myprint.o

Quote:

/*driver for printer on parallel port*/

#include <linux/module.h>
#include <linux/init.h>

#include <linux/config.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/fcntl.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <linux/console.h>

#include <linux/parport.h>
#undef LP_STATS


#include <asm/irq.h>
#include <asm/uaccess.h>
#include <asm/system.h>

#include <linux/saav.h>

/*defining a variable of struct printer*/
struct printer printer0;

#define P_PARPORT_CLAIMED 2

#define P_FLAGS() printer0.flags /*printer flags*/
#define P_TIME() printer0.time /* wait time */
#define P_CHAR() printer0.chars /* busy timeout */
#define P_WAIT() printer0.wait /* strobe wait */

#define read_data() (parport_read_data(printer0.dev->port))
#define read_status() (parport_read_status(printer0.dev->port))
#define write_control(var) do{ (parport_write_control(printer0.dev->port,var));} while(0)



int major;
static void lp_claim_parport_or_block(struct printer *this_lp)
{
if (!test_and_set_bit(P_PARPORT_CLAIMED, &this_lp->bits)) { parport_claim_or_block (this_lp->dev);
} }


void p_attach(struct parport *port)
{

printer0.dev = parport_register_device(port,"MyPrinter",NULL,NULL,NULL,0,NULL);

if(printer0.dev == NULL)
printk(KERN_ERR "Could not register driver with port");

else
printer0.flags |= F_EXIST;

}



void p_detach (struct parport *port)
{
}


/*to check if printer is out of power or not*/
int p_check()
{
unsigned int status1,status2,retval;
// int i=40;
lp_claim_parport_or_block(&printer0);
// parport_claim_or_block (printer0.dev);

status1 = read_status();
printk("\nstatus_before = %x",status1);


write_control(PC_STROBE | PC_SELECP);
udelay(P_DELAY);


status2 = read_status();
printk("\nstatus_after = %x",status2);

if(status1 == status2 && status2 != 0x67)
retval = -1;
else
retval = 0;

parport_release(printer0.dev);

return retval;
}

struct parport_driver printer_driver = {
"MyPrinter",
p_attach,
p_detach,
NULL
};


int saav_open(struct inode *inode,struct file *filp)
{

unsigned int status;
// printk("\nInside open\n");
if((P_FLAGS() & F_EXIST) == 0)
return -ENXIO;
// printk("\nhere 1");

status = read_status();
if(test_and_set_bit(PRINTER_BUSY_BIT_POS, &P_FLAGS()))
return -EBUSY;
// printk("\nhere 2status=%x",status);

/* if(p_check())
{
printk("\nPrinter not connected or offline\n");
P_FLAGS() &= ~F_BUSY;
return -EIO;
}*/

if((P_FLAGS() & F_ABORTOPEN) && !(filp->f_flags & O_NONBLOCK))
{
int status;
lp_claim_parport_or_block(&printer0);
// parport_claim_or_block(printer0.dev);
status = read_status();
parport_release(printer0.dev);

if(status & PS_OUT_PAPER)
{
printk(KERN_INFO "Printer is out of paper");
P_FLAGS() &= ~F_BUSY;
return -ENOSPC;
}

if(!(status & PS_SELECTED))
{
printk(KERN_INFO "printer is offline");
P_FLAGS() &= ~F_BUSY;
return -EIO;
}

if(!(status & PS_ERROR))
{
printk(KERN_INFO "printer Error");
P_FLAGS() &= ~F_BUSY;
return -EIO;
}

}

/* Implement Blocking Open here*/

/*to be Defined*/
printer0.p_buffer = (char *)kmalloc(P_BUFFER_SIZE,GFP_KERNEL);

if(!printer0.p_buffer)
{
P_FLAGS() &= ~F_BUSY;
return -ENOMEM;
}

lp_claim_parport_or_block(&printer0);
// parport_claim_or_block(printer0.dev);
parport_negotiate(printer0.dev->port,IEEE1284_MODE_COMPAT);
parport_release(printer0.dev);

printer0.current_mode = IEEE1284_MODE_COMPAT;

//testing
// printk("\nPrinter opened");
return 0;

}


int saav_release(struct inode * inode, struct file * filep)
{
lp_claim_parport_or_block(&printer0);
// parport_claim_or_block(printer0.dev);

parport_negotiate(printer0.dev->port, IEEE1284_MODE_COMPAT);
printer0.current_mode=IEEE1284_MODE_COMPAT;

parport_release(printer0.dev);


kfree(printer0.p_buffer);
printer0.p_buffer = NULL;
P_FLAGS() &= ~F_BUSY;

return 0;

}



/*function to check the status of printer through its status register*/
int check_printer_status()
{
int error = 0;
unsigned int last = printer0.last_error;
unsigned char status = read_status();
// int temp = (int) status;


if((P_FLAGS() & F_EXIST) == 0)
{
error = -ENXIO;
printk(KERN_INFO "Inside_check_status rinter busy");
}



/* to check if printer is out of paper*/
else if(status == 0x67)
{
if(last != PS_OUT_PAPER)
{ last = PS_OUT_PAPER;
printk("<0> Printer out of paper\n");
// printk("\ntemp=%X",temp);
}

error = -ENOSPC;
}


/*to check if printer is off-line*/
else if((status & PS_SELECTED))
{
if(last != PS_SELECTED)
{
last = PS_SELECTED;
printk("<0> Printer is off-line\n");
// printk("\nstatus = %x\n",status);
// printk("\ntemp=%X",temp);
}

error = -EIO;
}


else if(!(status & PS_ERROR))
{
if(last != PS_ERROR)
{
last = PS_ERROR;
printk("<0> Printer fault:check the printer\n");
}

error = -EIO;
}

else
{
printk("<0> Printer error\n");
error = -EIO;
}

return error;
}



ssize_t saav_write(struct file *filp,const char *buf, size_t count,loff_t *offp)
{

// printk("\nInside write\n");

struct parport *port = printer0.dev->port;
char *kbuf = printer0.p_buffer;
int bytes_written=0,copy_size;
ssize_t retv = 0;

/*setting copy_size*/
copy_size = count;
if(copy_size > P_BUFFER_SIZE)
copy_size = P_BUFFER_SIZE;


/*now taking semaphore*/
if(down_interruptible(&printer0.sem))
return -EINTR;


/*copy data from user space to kernel buffer*/
if(copy_from_user (kbuf, buf, copy_size))
return -EFAULT;


/* Claim Parport or sleep until it becomes available
*/
lp_claim_parport_or_block(&printer0);
// parport_claim_or_block (printer0.dev);

/*now setting the mode of data transfer with the port*/
parport_negotiate(printer0.dev->port,IEEE1284_MODE_COMPAT);

/*using while loop to transfer data to port*/
while(count > 0)
{

bytes_written=parport_write(port,kbuf,copy_size);
// printk("\nbytes written to port are=%d\n",bytes_written);
if(bytes_written >= 0)
{
buf += bytes_written;
count -= bytes_written;
copy_size -= bytes_written;
retv += bytes_written;
}

/*if there is some interrupt pending*/
if(signal_pending(current) == 1)
{
if(retv == 0)
retv = -EINTR;

break;
}

/*if entire data in kbuf has not been written to port*/
if(copy_size > 0)
{
int error;

error = check_printer_status();

if(error)
{
if(retv == 0)
retv = error;
break;
}


}


/*if data is left in user buffer*/
if(count > 0)
{
copy_size = count;
if(copy_size > P_BUFFER_SIZE)
copy_size = P_BUFFER_SIZE;

/*copy data from user space to kernel buffer*/ if(copy_from_user (kbuf, buf, copy_size))
{
if(retv == 0)
retv = -EFAULT;
}

break;
}

}/*end of while*/


/*releasing semaphore*/
up(&printer0.sem);

parport_release(printer0.dev);

/*returning value*/
return retv;

}/*end of write routine*/


int p_reset()
{
int retval=0,value1,value2;
printk("Check1");
value1 = read_status();
printk("\nCheck2:Inside p_reset():status1=%x\n",value1);

lp_claim_parport_or_block(&printer0);
// if (!test_and_set_bit( 2 , printer0.bits))
// parport_claim_or_block (printer0.dev);
printk("check3");
// write_control(PC_SELECP);
printk("check4");
udelay (P_DELAY);
printk("check5");
write_control(PC_SELECP | PC_INITP);
printk("check6");
value2 = read_status();
// printk("\nInside p_reset():status2=%x\n",value2);
parport_release(printer0.dev);

if(value1 != value2)
retval = 0;
else
retval = -1;


return retval;
}

int saav_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
{

int status;
int retval = 0;

// printk("\nInside ioctl with cmd =%x\n",cmd);

if ((P_FLAGS() & F_EXIST) == 0)
return -ENODEV;
switch ( cmd ) {

case PTIME:
P_TIME() = arg * HZ/100;
break;

case PCHAR:

P_CHAR() = arg;
break;

case PABORT:
if (arg)
P_FLAGS() |= F_ABORT;
else
P_FLAGS() &= ~F_ABORT;
break;

case PABORTOPEN:
if (arg)
P_FLAGS() |= F_ABORTOPEN;
else
P_FLAGS() &= ~F_ABORTOPEN;
break;

case PWAIT:
P_WAIT() = arg;
break;

case PGETSTATUS:
lp_claim_parport_or_block(&printer0);
// parport_claim_or_block(printer0.dev);
status = read_status();
parport_release(printer0.dev);

if (copy_to_user((int *) arg, &status, sizeof(int)))
return -EFAULT;
break;

case PRESET:
retval = p_reset();
// printk("after calling p_reset()");
break;

case PGETFLAGS:
status = P_FLAGS();
if (copy_to_user((int *) arg, &status, sizeof(int)))
return -EFAULT;
break;


default:
// printk("\nDefault in ioctl\n");
retval = -EINVAL;
}


return retval;
}



static struct file_operations p_fops = {
/* owner: THIS_MODULE,*/
write: saav_write,
ioctl: saav_ioctl,
open: saav_open,
release: saav_release,

/*#ifdef CONFIG_PARPORT_1284
read: pp_read,

#endif*/
};


int init_module(void)
{


/*initializing all data items of printer0*/
/*the printer0 is a variable of type 'struct printer'*/

printer0.dev = NULL;/*local variable */

printer0.flags = 0;/*flags defined in struct printer...*/

printer0.chars =P_INIT_CHAR;/*chars for busy time out*/


/* The parallel port specs apparently say that there needs to be
* a .5usec wait before and after the strobe. defined in lp.h
*/

printer0.time =P_INIT_TIME;/*time for wait time*/

printer0.wait =P_INIT_WAIT;/*wait for strobe wait*/



/* This is the amount of time that the driver waits for the printer to
* catch up when the printer's buffer appears to be filled. If you
* want to tune this and have a fast printer (i.e. HPIIIP), decrease
* this number, and if you have a slow printer, increase this number.
* This is in hundredths of a second, the default 2 being .05 second.
* Or use the tunelp(8) command, which is especially nice if you want
* change back and forth between character and graphics printing, which
* are wildly different...
*/




printer0.p_buffer = NULL;

printer0.last_error = 0;

init_waitqueue_head (&printer0.waitq);
init_waitqueue_head (&printer0.dataq);

printer0.timeout = 10 * HZ;

/*initializing the semaphore to a value of 1*/
sema_init(&printer0.sem,1);

major = register_chrdev(P_MAJOR,"MyPrinter",&p_fops);

/* requesting for major number */

if(major<0)
{
printk(KERN_WARNING " Error initializing printer module:can't get major %d \n",P_MAJOR);

return major;
}

if (parport_register_driver(&printer_driver))
{
printk("MyPrinter: unable to get register with parport");
return -EIO;
}


return 0;


}/*end of init_module*/



void cleanup_module(void)
{
/*unregistering the driver from the parport layer*/
parport_unregister_driver(&printer_driver);

/*unregistering the module*/
if(unregister_chrdev(P_MAJOR, "MyPrinter") == 0)
printk("removed");

/*unregistering the device*/
parport_unregister_device(printer0.dev);

}/*end of cleanup_module*/

MODULE_LICENSE("GPL");
EXPORT_NO_SYMBOLS;
 
Old 05-03-2006, 02:04 PM   #2
nick021
Member
 
Registered: Jan 2005
Location: india
Distribution: slackware 10.0 (kernel 2.4.26)
Posts: 104

Original Poster
Rep: Reputation: 15
these r the errors i m getting ...
how to correct them...

i m quoting few of them
Quote:

/root/Desktop/lodu/myprinte.c
In file included from /usr/src/linux-2.4.26/include/linux/sched.h:14,
from /root/Desktop/lodu/myprinte.c:10:
/usr/include/linux/timex.h:173: error: field `time' has incomplete type
In file included from /usr/src/linux-2.4.26/include/linux/sched.h:21,
from /root/Desktop/lodu/myprinte.c:10:
/usr/include/asm/mmu.h:12: error: field `sem' has incomplete type
In file included from /root/Desktop/lodu/myprinte.c:15:
/usr/include/linux/console.h:47: error: parse error before "u8"
/usr/include/linux/console.h:48: error: parse error before "u16"
/usr/include/linux/console.h:49: error: parse error before '*' token
/usr/include/linux/console.h:51: error: parse error before '}' token
/usr/include/linux/console.h:100: error: parse error before "kdev_t"
/usr/include/linux/console.h:107: error: parse error before '}' token
In file included from /root/Desktop/lodu/myprinte.c:22:
/usr/include/asm/uaccess.h: In function `verify_area':
/usr/include/asm/uaccess.h:106: error: `current' undeclared (first use in this function)
/usr/include/asm/uaccess.h:106: error: (Each undeclared identifier is reported only once
/usr/include/asm/uaccess.h:106: error: for each function it appears in.)
/usr/include/asm/uaccess.h: In function `__constant_copy_to_user':
/usr/include/asm/uaccess.h:669: error: `current' undeclared (first use in this function)
/usr/include/asm/uaccess.h: In function `__constant_copy_from_user':
/usr/include/asm/uaccess.h:677: error: `current' undeclared (first use in this function)
/root/Desktop/lodu/myprinte.c:25:24: linux/saav.h: No such file or directory
/root/Desktop/lodu/myprinte.c: In function `lp_claim_parport_or_block':
/root/Desktop/lodu/myprinte.c:46: error: dereferencing pointer to incomplete type
/root/Desktop/lodu/myprinte.c:46: error: dereferencing pointer to incomplete type

Last edited by nick021; 05-05-2006 at 03:21 PM.
 
Old 05-05-2006, 03:11 PM   #3
nick021
Member
 
Registered: Jan 2005
Location: india
Distribution: slackware 10.0 (kernel 2.4.26)
Posts: 104

Original Poster
Rep: Reputation: 15
hi all..

i have compiled my program with cmd:
Quote:
gcc -I /usr/src/linux/include/ -Wall -DMODULE -D__KERNEL__ -DLINUX -c usbd.c -o myusb.o
when i am inserting with
Quote:
insmod myusb.o
it gives following error:
Quote:
myusb.o: couldn't find the kernel version the module was compiled for
how to correct it..
 
  


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
Include Custom Header Kenji Miyamoto Programming 3 01-13-2005 08:46 PM
header include path KDE4me Linux - Newbie 6 01-06-2005 06:17 AM
Problem with C++ include/header files! Pisces107 Programming 12 12-23-2003 11:06 PM
Clarifiacation on header file "/usr/include/a.out.h" swathisat Linux - Software 0 09-05-2003 12:12 AM
need perl fuction like c #include statement for header file mrtwice Programming 5 06-19-2003 02:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:16 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
Open Source Consulting | Domain Registration