OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [rc203soc/] [sw/] [uClinux/] [arch/] [armnommu/] [drivers/] [char/] [lp.c] - Diff between revs 1765 and 1782

Only display areas with differences | Details | Blame | View Log

Rev 1765 Rev 1782
/*
/*
 * Copyright (C) 1992 by Jim Weigand and Linus Torvalds
 * Copyright (C) 1992 by Jim Weigand and Linus Torvalds
 * Copyright (C) 1992,1993 by Michael K. Johnson
 * Copyright (C) 1992,1993 by Michael K. Johnson
 * - Thanks much to Gunter Windau for pointing out to me where the error
 * - Thanks much to Gunter Windau for pointing out to me where the error
 *   checking ought to be.
 *   checking ought to be.
 * Copyright (C) 1993 by Nigel Gamble (added interrupt code)
 * Copyright (C) 1993 by Nigel Gamble (added interrupt code)
 * Copyright (C) 1994 by Alan Cox (Modularised it)
 * Copyright (C) 1994 by Alan Cox (Modularised it)
 * LPCAREFUL, LPABORT, LPGETSTATUS added by Chris Metcalf, metcalf@lcs.mit.edu
 * LPCAREFUL, LPABORT, LPGETSTATUS added by Chris Metcalf, metcalf@lcs.mit.edu
 * Statistics and support for slow printers by Rob Janssen, rob@knoware.nl
 * Statistics and support for slow printers by Rob Janssen, rob@knoware.nl
 * "lp=" command line parameters added by Grant Guenther, grant@torque.net
 * "lp=" command line parameters added by Grant Guenther, grant@torque.net
 */
 */
 
 
#include <linux/module.h>
#include <linux/module.h>
 
 
#include <linux/errno.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/major.h>
#include <linux/sched.h>
#include <linux/sched.h>
#include <linux/lp.h>
#include <linux/lp.h>
#include <linux/malloc.h>
#include <linux/malloc.h>
#include <linux/ioport.h>
#include <linux/ioport.h>
#include <linux/fcntl.h>
#include <linux/fcntl.h>
#include <linux/delay.h>
#include <linux/delay.h>
 
 
#include <asm/io.h>
#include <asm/io.h>
#include <asm/segment.h>
#include <asm/segment.h>
#include <asm/system.h>
#include <asm/system.h>
 
 
#define NO_IRQ (-1)
#define NO_IRQ (-1)
 
 
/* the BIOS manuals say there can be up to 4 lpt devices
/* the BIOS manuals say there can be up to 4 lpt devices
 * but I have not seen a board where the 4th address is listed
 * but I have not seen a board where the 4th address is listed
 * if you have different hardware change the table below
 * if you have different hardware change the table below
 * please let me know if you have different equipment
 * please let me know if you have different equipment
 * if you have more than 3 printers, remember to increase LP_NO
 * if you have more than 3 printers, remember to increase LP_NO
 */
 */
struct lp_struct lp_table[] = {
struct lp_struct lp_table[] = {
        { 0x3bc, NO_IRQ, 0, LP_INIT_CHAR, LP_INIT_TIME, LP_INIT_WAIT, NULL, NULL, 0, 0, 0, {0} },
        { 0x3bc, NO_IRQ, 0, LP_INIT_CHAR, LP_INIT_TIME, LP_INIT_WAIT, NULL, NULL, 0, 0, 0, {0} },
        { 0x378, NO_IRQ, 0, LP_INIT_CHAR, LP_INIT_TIME, LP_INIT_WAIT, NULL, NULL, 0, 0, 0, {0} },
        { 0x378, NO_IRQ, 0, LP_INIT_CHAR, LP_INIT_TIME, LP_INIT_WAIT, NULL, NULL, 0, 0, 0, {0} },
        { 0x278, NO_IRQ, 0, LP_INIT_CHAR, LP_INIT_TIME, LP_INIT_WAIT, NULL, NULL, 0, 0, 0, {0} },
        { 0x278, NO_IRQ, 0, LP_INIT_CHAR, LP_INIT_TIME, LP_INIT_WAIT, NULL, NULL, 0, 0, 0, {0} },
};
};
#define LP_NO 3
#define LP_NO 3
 
 
/* Test if printer is ready (and optionally has no error conditions) */
/* Test if printer is ready (and optionally has no error conditions) */
#define LP_READY(minor, status) \
#define LP_READY(minor, status) \
  ((LP_F(minor) & LP_CAREFUL) ? _LP_CAREFUL_READY(status) : (status & LP_PBUSY))
  ((LP_F(minor) & LP_CAREFUL) ? _LP_CAREFUL_READY(status) : (status & LP_PBUSY))
#define LP_CAREFUL_READY(minor, status) \
#define LP_CAREFUL_READY(minor, status) \
  ((LP_F(minor) & LP_CAREFUL) ? _LP_CAREFUL_READY(status) : 1)
  ((LP_F(minor) & LP_CAREFUL) ? _LP_CAREFUL_READY(status) : 1)
#define _LP_CAREFUL_READY(status) \
#define _LP_CAREFUL_READY(status) \
   (status & (LP_PBUSY|LP_POUTPA|LP_PSELECD|LP_PERRORP)) == \
   (status & (LP_PBUSY|LP_POUTPA|LP_PSELECD|LP_PERRORP)) == \
      (LP_PBUSY|LP_PSELECD|LP_PERRORP)
      (LP_PBUSY|LP_PSELECD|LP_PERRORP)
 
 
/*
/*
 * All my debugging code assumes that you debug with only one printer at
 * All my debugging code assumes that you debug with only one printer at
 * a time. RWWH
 * a time. RWWH
 * Debug info moved into stats area, so this is no longer true (Rob Janssen)
 * Debug info moved into stats area, so this is no longer true (Rob Janssen)
 */
 */
 
 
#undef LP_DEBUG
#undef LP_DEBUG
 
 
static int lp_reset(int minor)
static int lp_reset(int minor)
{
{
        outb_p(LP_PSELECP, LP_C(minor));
        outb_p(LP_PSELECP, LP_C(minor));
        udelay(LP_DELAY);
        udelay(LP_DELAY);
        outb_p(LP_PSELECP | LP_PINITP, LP_C(minor));
        outb_p(LP_PSELECP | LP_PINITP, LP_C(minor));
        return LP_S(minor);
        return LP_S(minor);
}
}
 
 
static inline int lp_char_polled(char lpchar, int minor)
static inline int lp_char_polled(char lpchar, int minor)
{
{
        int status, wait = 0;
        int status, wait = 0;
        unsigned long count  = 0;
        unsigned long count  = 0;
        struct lp_stats *stats;
        struct lp_stats *stats;
 
 
        do {
        do {
                status = LP_S(minor);
                status = LP_S(minor);
                count ++;
                count ++;
                if(need_resched)
                if(need_resched)
                        schedule();
                        schedule();
        } while(!LP_READY(minor,status) && count < LP_CHAR(minor));
        } while(!LP_READY(minor,status) && count < LP_CHAR(minor));
 
 
        if (count == LP_CHAR(minor)) {
        if (count == LP_CHAR(minor)) {
                return 0;
                return 0;
                /* we timed out, and the character was /not/ printed */
                /* we timed out, and the character was /not/ printed */
        }
        }
        outb_p(lpchar, LP_B(minor));
        outb_p(lpchar, LP_B(minor));
        stats = &LP_STAT(minor);
        stats = &LP_STAT(minor);
        stats->chars++;
        stats->chars++;
        /* must wait before taking strobe high, and after taking strobe
        /* must wait before taking strobe high, and after taking strobe
           low, according spec.  Some printers need it, others don't. */
           low, according spec.  Some printers need it, others don't. */
        while(wait != LP_WAIT(minor)) wait++;
        while(wait != LP_WAIT(minor)) wait++;
        /* control port takes strobe high */
        /* control port takes strobe high */
        outb_p(( LP_PSELECP | LP_PINITP | LP_PSTROBE ), ( LP_C( minor )));
        outb_p(( LP_PSELECP | LP_PINITP | LP_PSTROBE ), ( LP_C( minor )));
        /* Wait until NBUSY line goes high */
        /* Wait until NBUSY line goes high */
        count = 0;
        count = 0;
        do {
        do {
                status = LP_S(minor);
                status = LP_S(minor);
                count++;
                count++;
                if (need_resched)
                if (need_resched)
                        schedule();
                        schedule();
        } while (LP_READY(minor, status) && (count<LP_CHAR(minor)));
        } while (LP_READY(minor, status) && (count<LP_CHAR(minor)));
        /* take strobe low */
        /* take strobe low */
        outb_p(( LP_PSELECP | LP_PINITP ), ( LP_C( minor )));
        outb_p(( LP_PSELECP | LP_PINITP ), ( LP_C( minor )));
        /* update waittime statistics */
        /* update waittime statistics */
        if (count > stats->maxwait) {
        if (count > stats->maxwait) {
#ifdef LP_DEBUG
#ifdef LP_DEBUG
            printk(KERN_DEBUG "lp%d success after %d counts.\n",minor,count);
            printk(KERN_DEBUG "lp%d success after %d counts.\n",minor,count);
#endif
#endif
            stats->maxwait = count;
            stats->maxwait = count;
        }
        }
        count *= 256;
        count *= 256;
        wait = (count > stats->meanwait)? count - stats->meanwait :
        wait = (count > stats->meanwait)? count - stats->meanwait :
                                          stats->meanwait - count;
                                          stats->meanwait - count;
        stats->meanwait = (255*stats->meanwait + count + 128) / 256;
        stats->meanwait = (255*stats->meanwait + count + 128) / 256;
        stats->mdev = ((127 * stats->mdev) + wait + 64) / 128;
        stats->mdev = ((127 * stats->mdev) + wait + 64) / 128;
 
 
        return 1;
        return 1;
}
}
 
 
static inline int lp_char_interrupt(char lpchar, int minor)
static inline int lp_char_interrupt(char lpchar, int minor)
{
{
        int wait;
        int wait;
        unsigned long count = 0;
        unsigned long count = 0;
        unsigned char status;
        unsigned char status;
        struct lp_stats *stats;
        struct lp_stats *stats;
 
 
        do {
        do {
            if(need_resched)
            if(need_resched)
                schedule();
                schedule();
            if ((status = LP_S(minor)) & LP_PBUSY) {
            if ((status = LP_S(minor)) & LP_PBUSY) {
                if (!LP_CAREFUL_READY(minor, status))
                if (!LP_CAREFUL_READY(minor, status))
                        return 0;
                        return 0;
                outb_p(lpchar, LP_B(minor));
                outb_p(lpchar, LP_B(minor));
                stats = &LP_STAT(minor);
                stats = &LP_STAT(minor);
                stats->chars++;
                stats->chars++;
                /* must wait before taking strobe high, and after taking strobe
                /* must wait before taking strobe high, and after taking strobe
                   low, according spec.  Some printers need it, others don't. */
                   low, according spec.  Some printers need it, others don't. */
                wait = 0;
                wait = 0;
                while(wait != LP_WAIT(minor)) wait++;
                while(wait != LP_WAIT(minor)) wait++;
                /* control port takes strobe high */
                /* control port takes strobe high */
                outb_p(( LP_PSELECP | LP_PINITP | LP_PSTROBE ), ( LP_C( minor )));
                outb_p(( LP_PSELECP | LP_PINITP | LP_PSTROBE ), ( LP_C( minor )));
                while(wait) wait--;
                while(wait) wait--;
                /* take strobe low */
                /* take strobe low */
                outb_p(( LP_PSELECP | LP_PINITP ), ( LP_C( minor )));
                outb_p(( LP_PSELECP | LP_PINITP ), ( LP_C( minor )));
                /* update waittime statistics */
                /* update waittime statistics */
                if (count) {
                if (count) {
                    if (count > stats->maxwait)
                    if (count > stats->maxwait)
                        stats->maxwait = count;
                        stats->maxwait = count;
                    count *= 256;
                    count *= 256;
                    wait = (count > stats->meanwait)? count - stats->meanwait :
                    wait = (count > stats->meanwait)? count - stats->meanwait :
                                                      stats->meanwait - count;
                                                      stats->meanwait - count;
                    stats->meanwait = (255*stats->meanwait + count + 128) / 256;
                    stats->meanwait = (255*stats->meanwait + count + 128) / 256;
                    stats->mdev = ((127 * stats->mdev) + wait + 64) / 128;
                    stats->mdev = ((127 * stats->mdev) + wait + 64) / 128;
                }
                }
                return 1;
                return 1;
            }
            }
        } while (count++ < LP_CHAR(minor));
        } while (count++ < LP_CHAR(minor));
 
 
        return 0;
        return 0;
}
}
 
 
static void lp_interrupt(int irq, void *dev_id, struct pt_regs *regs)
static void lp_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
{
        struct lp_struct *lp = &lp_table[0];
        struct lp_struct *lp = &lp_table[0];
 
 
        while (irq != lp->irq) {
        while (irq != lp->irq) {
                if (++lp >= &lp_table[LP_NO])
                if (++lp >= &lp_table[LP_NO])
                        return;
                        return;
        }
        }
 
 
        wake_up(&lp->lp_wait_q);
        wake_up(&lp->lp_wait_q);
}
}
 
 
static inline int lp_write_interrupt(unsigned int minor, const char * buf, int count)
static inline int lp_write_interrupt(unsigned int minor, const char * buf, int count)
{
{
        unsigned long copy_size;
        unsigned long copy_size;
        unsigned long total_bytes_written = 0;
        unsigned long total_bytes_written = 0;
        unsigned long bytes_written;
        unsigned long bytes_written;
        struct lp_struct *lp = &lp_table[minor];
        struct lp_struct *lp = &lp_table[minor];
        unsigned char status;
        unsigned char status;
 
 
        do {
        do {
                bytes_written = 0;
                bytes_written = 0;
                copy_size = (count <= LP_BUFFER_SIZE ? count : LP_BUFFER_SIZE);
                copy_size = (count <= LP_BUFFER_SIZE ? count : LP_BUFFER_SIZE);
                memcpy_fromfs(lp->lp_buffer, buf, copy_size);
                memcpy_fromfs(lp->lp_buffer, buf, copy_size);
 
 
                while (copy_size) {
                while (copy_size) {
                        if (lp_char_interrupt(lp->lp_buffer[bytes_written], minor)) {
                        if (lp_char_interrupt(lp->lp_buffer[bytes_written], minor)) {
                                --copy_size;
                                --copy_size;
                                ++bytes_written;
                                ++bytes_written;
                                lp_table[minor].runchars++;
                                lp_table[minor].runchars++;
                        } else {
                        } else {
                                int rc = total_bytes_written + bytes_written;
                                int rc = total_bytes_written + bytes_written;
                                if (lp_table[minor].runchars > LP_STAT(minor).maxrun)
                                if (lp_table[minor].runchars > LP_STAT(minor).maxrun)
                                         LP_STAT(minor).maxrun = lp_table[minor].runchars;
                                         LP_STAT(minor).maxrun = lp_table[minor].runchars;
                                status = LP_S(minor);
                                status = LP_S(minor);
                                if ((status & LP_POUTPA)) {
                                if ((status & LP_POUTPA)) {
                                        printk(KERN_INFO "lp%d out of paper\n", minor);
                                        printk(KERN_INFO "lp%d out of paper\n", minor);
                                        if (LP_F(minor) & LP_ABORT)
                                        if (LP_F(minor) & LP_ABORT)
                                                return rc?rc:-ENOSPC;
                                                return rc?rc:-ENOSPC;
                                } else if (!(status & LP_PSELECD)) {
                                } else if (!(status & LP_PSELECD)) {
                                        printk(KERN_INFO "lp%d off-line\n", minor);
                                        printk(KERN_INFO "lp%d off-line\n", minor);
                                        if (LP_F(minor) & LP_ABORT)
                                        if (LP_F(minor) & LP_ABORT)
                                                return rc?rc:-EIO;
                                                return rc?rc:-EIO;
                                } else if (!(status & LP_PERRORP)) {
                                } else if (!(status & LP_PERRORP)) {
                                        printk(KERN_ERR "lp%d printer error\n", minor);
                                        printk(KERN_ERR "lp%d printer error\n", minor);
                                        if (LP_F(minor) & LP_ABORT)
                                        if (LP_F(minor) & LP_ABORT)
                                                return rc?rc:-EIO;
                                                return rc?rc:-EIO;
                                }
                                }
                                LP_STAT(minor).sleeps++;
                                LP_STAT(minor).sleeps++;
                                cli();
                                cli();
                                outb_p((LP_PSELECP|LP_PINITP|LP_PINTEN), (LP_C(minor)));
                                outb_p((LP_PSELECP|LP_PINITP|LP_PINTEN), (LP_C(minor)));
                                status = LP_S(minor);
                                status = LP_S(minor);
                                if ((!(status & LP_PACK) || (status & LP_PBUSY))
                                if ((!(status & LP_PACK) || (status & LP_PBUSY))
                                  && LP_CAREFUL_READY(minor, status)) {
                                  && LP_CAREFUL_READY(minor, status)) {
                                        outb_p((LP_PSELECP|LP_PINITP), (LP_C(minor)));
                                        outb_p((LP_PSELECP|LP_PINITP), (LP_C(minor)));
                                        sti();
                                        sti();
                                        continue;
                                        continue;
                                }
                                }
                                lp_table[minor].runchars=0;
                                lp_table[minor].runchars=0;
                                current->timeout = jiffies + LP_TIMEOUT_INTERRUPT;
                                current->timeout = jiffies + LP_TIMEOUT_INTERRUPT;
                                interruptible_sleep_on(&lp->lp_wait_q);
                                interruptible_sleep_on(&lp->lp_wait_q);
                                outb_p((LP_PSELECP|LP_PINITP), (LP_C(minor)));
                                outb_p((LP_PSELECP|LP_PINITP), (LP_C(minor)));
                                sti();
                                sti();
                                if (current->signal & ~current->blocked) {
                                if (current->signal & ~current->blocked) {
                                        if (total_bytes_written + bytes_written)
                                        if (total_bytes_written + bytes_written)
                                                return total_bytes_written + bytes_written;
                                                return total_bytes_written + bytes_written;
                                        else
                                        else
                                                return -EINTR;
                                                return -EINTR;
                                }
                                }
                        }
                        }
                }
                }
 
 
                total_bytes_written += bytes_written;
                total_bytes_written += bytes_written;
                buf += bytes_written;
                buf += bytes_written;
                count -= bytes_written;
                count -= bytes_written;
 
 
        } while (count > 0);
        } while (count > 0);
 
 
        return total_bytes_written;
        return total_bytes_written;
}
}
 
 
static inline int lp_write_polled(unsigned int minor, const char * buf, int count)
static inline int lp_write_polled(unsigned int minor, const char * buf, int count)
{
{
        int  retval,status;
        int  retval,status;
        char c;
        char c;
        const char *temp;
        const char *temp;
 
 
        temp = buf;
        temp = buf;
        while (count > 0) {
        while (count > 0) {
                c = get_user(temp);
                c = get_user(temp);
                retval = lp_char_polled(c, minor);
                retval = lp_char_polled(c, minor);
                /* only update counting vars if character was printed */
                /* only update counting vars if character was printed */
                if (retval) {
                if (retval) {
                        count--; temp++;
                        count--; temp++;
                        lp_table[minor].runchars++;
                        lp_table[minor].runchars++;
                } else { /* if printer timed out */
                } else { /* if printer timed out */
                        if (lp_table[minor].runchars > LP_STAT(minor).maxrun)
                        if (lp_table[minor].runchars > LP_STAT(minor).maxrun)
                                 LP_STAT(minor).maxrun = lp_table[minor].runchars;
                                 LP_STAT(minor).maxrun = lp_table[minor].runchars;
                        status = LP_S(minor);
                        status = LP_S(minor);
 
 
                        if (status & LP_POUTPA) {
                        if (status & LP_POUTPA) {
                                printk(KERN_INFO "lp%d out of paper\n", minor);
                                printk(KERN_INFO "lp%d out of paper\n", minor);
                                if(LP_F(minor) & LP_ABORT)
                                if(LP_F(minor) & LP_ABORT)
                                        return temp-buf?temp-buf:-ENOSPC;
                                        return temp-buf?temp-buf:-ENOSPC;
                                current->state = TASK_INTERRUPTIBLE;
                                current->state = TASK_INTERRUPTIBLE;
                                current->timeout = jiffies + LP_TIMEOUT_POLLED;
                                current->timeout = jiffies + LP_TIMEOUT_POLLED;
                                schedule();
                                schedule();
                        } else
                        } else
                        if (!(status & LP_PSELECD)) {
                        if (!(status & LP_PSELECD)) {
                                printk(KERN_INFO "lp%d off-line\n", minor);
                                printk(KERN_INFO "lp%d off-line\n", minor);
                                if(LP_F(minor) & LP_ABORT)
                                if(LP_F(minor) & LP_ABORT)
                                        return temp-buf?temp-buf:-EIO;
                                        return temp-buf?temp-buf:-EIO;
                                current->state = TASK_INTERRUPTIBLE;
                                current->state = TASK_INTERRUPTIBLE;
                                current->timeout = jiffies + LP_TIMEOUT_POLLED;
                                current->timeout = jiffies + LP_TIMEOUT_POLLED;
                                schedule();
                                schedule();
                        } else
                        } else
                        /* not offline or out of paper. on fire? */
                        /* not offline or out of paper. on fire? */
                        if (!(status & LP_PERRORP)) {
                        if (!(status & LP_PERRORP)) {
                                printk(KERN_ERR "lp%d reported invalid error status (on fire, eh?)\n", minor);
                                printk(KERN_ERR "lp%d reported invalid error status (on fire, eh?)\n", minor);
                                if(LP_F(minor) & LP_ABORT)
                                if(LP_F(minor) & LP_ABORT)
                                        return temp-buf?temp-buf:-EIO;
                                        return temp-buf?temp-buf:-EIO;
                                current->state = TASK_INTERRUPTIBLE;
                                current->state = TASK_INTERRUPTIBLE;
                                current->timeout = jiffies + LP_TIMEOUT_POLLED;
                                current->timeout = jiffies + LP_TIMEOUT_POLLED;
                                schedule();
                                schedule();
                        }
                        }
 
 
                        /* check for signals before going to sleep */
                        /* check for signals before going to sleep */
                        if (current->signal & ~current->blocked) {
                        if (current->signal & ~current->blocked) {
                                if (temp != buf)
                                if (temp != buf)
                                        return temp-buf;
                                        return temp-buf;
                                else
                                else
                                        return -EINTR;
                                        return -EINTR;
                        }
                        }
                        LP_STAT(minor).sleeps++;
                        LP_STAT(minor).sleeps++;
#ifdef LP_DEBUG
#ifdef LP_DEBUG
                        printk(KERN_DEBUG "lp%d sleeping at %d characters for %d jiffies\n",
                        printk(KERN_DEBUG "lp%d sleeping at %d characters for %d jiffies\n",
                                minor,lp_table[minor].runchars, LP_TIME(minor));
                                minor,lp_table[minor].runchars, LP_TIME(minor));
#endif
#endif
                        lp_table[minor].runchars=0;
                        lp_table[minor].runchars=0;
                        current->state = TASK_INTERRUPTIBLE;
                        current->state = TASK_INTERRUPTIBLE;
                        current->timeout = jiffies + LP_TIME(minor);
                        current->timeout = jiffies + LP_TIME(minor);
                        schedule();
                        schedule();
                }
                }
        }
        }
        return temp-buf;
        return temp-buf;
}
}
 
 
static int lp_write(struct inode * inode, struct file * file, const char * buf, int count)
static int lp_write(struct inode * inode, struct file * file, const char * buf, int count)
{
{
        unsigned int minor = MINOR(inode->i_rdev);
        unsigned int minor = MINOR(inode->i_rdev);
 
 
        if (jiffies-lp_table[minor].lastcall > LP_TIME(minor))
        if (jiffies-lp_table[minor].lastcall > LP_TIME(minor))
                lp_table[minor].runchars = 0;
                lp_table[minor].runchars = 0;
        lp_table[minor].lastcall = jiffies;
        lp_table[minor].lastcall = jiffies;
 
 
        if (LP_IRQ(minor) != NO_IRQ)
        if (LP_IRQ(minor) != NO_IRQ)
                return lp_write_interrupt(minor, buf, count);
                return lp_write_interrupt(minor, buf, count);
        else
        else
                return lp_write_polled(minor, buf, count);
                return lp_write_polled(minor, buf, count);
}
}
 
 
static int lp_lseek(struct inode * inode, struct file * file,
static int lp_lseek(struct inode * inode, struct file * file,
                    off_t offset, int origin)
                    off_t offset, int origin)
{
{
        return -ESPIPE;
        return -ESPIPE;
}
}
 
 
static int lp_open(struct inode * inode, struct file * file)
static int lp_open(struct inode * inode, struct file * file)
{
{
        unsigned int minor = MINOR(inode->i_rdev);
        unsigned int minor = MINOR(inode->i_rdev);
        int ret;
        int ret;
        unsigned int irq;
        unsigned int irq;
 
 
        if (minor >= LP_NO)
        if (minor >= LP_NO)
                return -ENXIO;
                return -ENXIO;
        if ((LP_F(minor) & LP_EXIST) == 0)
        if ((LP_F(minor) & LP_EXIST) == 0)
                return -ENXIO;
                return -ENXIO;
        if (LP_F(minor) & LP_BUSY)
        if (LP_F(minor) & LP_BUSY)
                return -EBUSY;
                return -EBUSY;
 
 
        MOD_INC_USE_COUNT;
        MOD_INC_USE_COUNT;
 
 
        /* If ABORTOPEN is set and the printer is offline or out of paper,
        /* If ABORTOPEN is set and the printer is offline or out of paper,
           we may still want to open it to perform ioctl()s.  Therefore we
           we may still want to open it to perform ioctl()s.  Therefore we
           have commandeered O_NONBLOCK, even though it is being used in
           have commandeered O_NONBLOCK, even though it is being used in
           a non-standard manner.  This is strictly a Linux hack, and
           a non-standard manner.  This is strictly a Linux hack, and
           should most likely only ever be used by the tunelp application. */
           should most likely only ever be used by the tunelp application. */
        if ((LP_F(minor) & LP_ABORTOPEN) && !(file->f_flags & O_NONBLOCK)) {
        if ((LP_F(minor) & LP_ABORTOPEN) && !(file->f_flags & O_NONBLOCK)) {
                int status = LP_S(minor);
                int status = LP_S(minor);
                if (status & LP_POUTPA) {
                if (status & LP_POUTPA) {
                        printk(KERN_INFO "lp%d out of paper\n", minor);
                        printk(KERN_INFO "lp%d out of paper\n", minor);
                        MOD_DEC_USE_COUNT;
                        MOD_DEC_USE_COUNT;
                        return -ENOSPC;
                        return -ENOSPC;
                } else if (!(status & LP_PSELECD)) {
                } else if (!(status & LP_PSELECD)) {
                        printk(KERN_INFO "lp%d off-line\n", minor);
                        printk(KERN_INFO "lp%d off-line\n", minor);
                        MOD_DEC_USE_COUNT;
                        MOD_DEC_USE_COUNT;
                        return -EIO;
                        return -EIO;
                } else if (!(status & LP_PERRORP)) {
                } else if (!(status & LP_PERRORP)) {
                        printk(KERN_ERR "lp%d printer error\n", minor);
                        printk(KERN_ERR "lp%d printer error\n", minor);
                        MOD_DEC_USE_COUNT;
                        MOD_DEC_USE_COUNT;
                        return -EIO;
                        return -EIO;
                }
                }
        }
        }
 
 
        if ((irq = LP_IRQ(minor)) != NO_IRQ) {
        if ((irq = LP_IRQ(minor)) != NO_IRQ) {
                lp_table[minor].lp_buffer = (char *) kmalloc(LP_BUFFER_SIZE, GFP_KERNEL);
                lp_table[minor].lp_buffer = (char *) kmalloc(LP_BUFFER_SIZE, GFP_KERNEL);
                if (!lp_table[minor].lp_buffer) {
                if (!lp_table[minor].lp_buffer) {
                        MOD_DEC_USE_COUNT;
                        MOD_DEC_USE_COUNT;
                        return -ENOMEM;
                        return -ENOMEM;
                }
                }
 
 
                ret = request_irq(irq, lp_interrupt, SA_INTERRUPT, "printer", NULL);
                ret = request_irq(irq, lp_interrupt, SA_INTERRUPT, "printer", NULL);
                if (ret) {
                if (ret) {
                        kfree_s(lp_table[minor].lp_buffer, LP_BUFFER_SIZE);
                        kfree_s(lp_table[minor].lp_buffer, LP_BUFFER_SIZE);
                        lp_table[minor].lp_buffer = NULL;
                        lp_table[minor].lp_buffer = NULL;
                        printk("lp%d unable to use interrupt %d, error %d\n", minor, irq, ret);
                        printk("lp%d unable to use interrupt %d, error %d\n", minor, irq, ret);
                        MOD_DEC_USE_COUNT;
                        MOD_DEC_USE_COUNT;
                        return ret;
                        return ret;
                }
                }
        }
        }
 
 
        LP_F(minor) |= LP_BUSY;
        LP_F(minor) |= LP_BUSY;
        return 0;
        return 0;
}
}
 
 
static void lp_release(struct inode * inode, struct file * file)
static void lp_release(struct inode * inode, struct file * file)
{
{
        unsigned int minor = MINOR(inode->i_rdev);
        unsigned int minor = MINOR(inode->i_rdev);
        unsigned int irq;
        unsigned int irq;
 
 
        if ((irq = LP_IRQ(minor)) != NO_IRQ) {
        if ((irq = LP_IRQ(minor)) != NO_IRQ) {
                free_irq(irq, NULL);
                free_irq(irq, NULL);
                kfree_s(lp_table[minor].lp_buffer, LP_BUFFER_SIZE);
                kfree_s(lp_table[minor].lp_buffer, LP_BUFFER_SIZE);
                lp_table[minor].lp_buffer = NULL;
                lp_table[minor].lp_buffer = NULL;
        }
        }
 
 
        LP_F(minor) &= ~LP_BUSY;
        LP_F(minor) &= ~LP_BUSY;
        MOD_DEC_USE_COUNT;
        MOD_DEC_USE_COUNT;
}
}
 
 
 
 
static int lp_ioctl(struct inode *inode, struct file *file,
static int lp_ioctl(struct inode *inode, struct file *file,
                    unsigned int cmd, unsigned long arg)
                    unsigned int cmd, unsigned long arg)
{
{
        unsigned int minor = MINOR(inode->i_rdev);
        unsigned int minor = MINOR(inode->i_rdev);
        int retval = 0;
        int retval = 0;
 
 
#ifdef LP_DEBUG
#ifdef LP_DEBUG
        printk(KERN_DEBUG "lp%d ioctl, cmd: 0x%x, arg: 0x%x\n", minor, cmd, arg);
        printk(KERN_DEBUG "lp%d ioctl, cmd: 0x%x, arg: 0x%x\n", minor, cmd, arg);
#endif
#endif
        if (minor >= LP_NO)
        if (minor >= LP_NO)
                return -ENODEV;
                return -ENODEV;
        if ((LP_F(minor) & LP_EXIST) == 0)
        if ((LP_F(minor) & LP_EXIST) == 0)
                return -ENODEV;
                return -ENODEV;
        switch ( cmd ) {
        switch ( cmd ) {
                case LPTIME:
                case LPTIME:
                        LP_TIME(minor) = arg * HZ/100;
                        LP_TIME(minor) = arg * HZ/100;
                        break;
                        break;
                case LPCHAR:
                case LPCHAR:
                        LP_CHAR(minor) = arg;
                        LP_CHAR(minor) = arg;
                        break;
                        break;
                case LPABORT:
                case LPABORT:
                        if (arg)
                        if (arg)
                                LP_F(minor) |= LP_ABORT;
                                LP_F(minor) |= LP_ABORT;
                        else
                        else
                                LP_F(minor) &= ~LP_ABORT;
                                LP_F(minor) &= ~LP_ABORT;
                        break;
                        break;
                case LPABORTOPEN:
                case LPABORTOPEN:
                        if (arg)
                        if (arg)
                                LP_F(minor) |= LP_ABORTOPEN;
                                LP_F(minor) |= LP_ABORTOPEN;
                        else
                        else
                                LP_F(minor) &= ~LP_ABORTOPEN;
                                LP_F(minor) &= ~LP_ABORTOPEN;
                        break;
                        break;
                case LPCAREFUL:
                case LPCAREFUL:
                        if (arg)
                        if (arg)
                                LP_F(minor) |= LP_CAREFUL;
                                LP_F(minor) |= LP_CAREFUL;
                        else
                        else
                                LP_F(minor) &= ~LP_CAREFUL;
                                LP_F(minor) &= ~LP_CAREFUL;
                        break;
                        break;
                case LPWAIT:
                case LPWAIT:
                        LP_WAIT(minor) = arg;
                        LP_WAIT(minor) = arg;
                        break;
                        break;
                case LPSETIRQ: {
                case LPSETIRQ: {
                        int oldirq;
                        int oldirq;
                        int newirq = arg;
                        int newirq = arg;
                        struct lp_struct *lp = &lp_table[minor];
                        struct lp_struct *lp = &lp_table[minor];
 
 
                        if (!suser())
                        if (!suser())
                                return -EPERM;
                                return -EPERM;
 
 
                        oldirq = LP_IRQ(minor);
                        oldirq = LP_IRQ(minor);
 
 
                        /* Allocate buffer now if we are going to need it */
                        /* Allocate buffer now if we are going to need it */
                        if (oldirq == NO_IRQ && newirq != NO_IRQ) {
                        if (oldirq == NO_IRQ && newirq != NO_IRQ) {
                                lp->lp_buffer = (char *) kmalloc(LP_BUFFER_SIZE, GFP_KERNEL);
                                lp->lp_buffer = (char *) kmalloc(LP_BUFFER_SIZE, GFP_KERNEL);
                                if (!lp->lp_buffer)
                                if (!lp->lp_buffer)
                                        return -ENOMEM;
                                        return -ENOMEM;
                        }
                        }
 
 
                        if (oldirq != NO_IRQ) {
                        if (oldirq != NO_IRQ) {
                                free_irq(oldirq, NULL);
                                free_irq(oldirq, NULL);
                        }
                        }
                        if (newirq != NO_IRQ) {
                        if (newirq != NO_IRQ) {
                                /* Install new irq */
                                /* Install new irq */
                                if ((retval = request_irq(newirq, lp_interrupt, SA_INTERRUPT, "printer", NULL))) {
                                if ((retval = request_irq(newirq, lp_interrupt, SA_INTERRUPT, "printer", NULL))) {
                                        if (oldirq != NO_IRQ) {
                                        if (oldirq != NO_IRQ) {
                                                /* restore old irq */
                                                /* restore old irq */
                                                request_irq(oldirq, lp_interrupt, SA_INTERRUPT, "printer", NULL);
                                                request_irq(oldirq, lp_interrupt, SA_INTERRUPT, "printer", NULL);
                                        } else {
                                        } else {
                                                /* We don't need the buffer */
                                                /* We don't need the buffer */
                                                kfree_s(lp->lp_buffer, LP_BUFFER_SIZE);
                                                kfree_s(lp->lp_buffer, LP_BUFFER_SIZE);
                                                lp->lp_buffer = NULL;
                                                lp->lp_buffer = NULL;
                                        }
                                        }
                                        return retval;
                                        return retval;
                                }
                                }
                        }
                        }
                        if (oldirq != NO_IRQ && newirq == NO_IRQ) {
                        if (oldirq != NO_IRQ && newirq == NO_IRQ) {
                                /* We don't need the buffer */
                                /* We don't need the buffer */
                                kfree_s(lp->lp_buffer, LP_BUFFER_SIZE);
                                kfree_s(lp->lp_buffer, LP_BUFFER_SIZE);
                                lp->lp_buffer = NULL;
                                lp->lp_buffer = NULL;
                        }
                        }
                        LP_IRQ(minor) = newirq;
                        LP_IRQ(minor) = newirq;
                        lp_reset(minor);
                        lp_reset(minor);
                        break;
                        break;
                }
                }
                case LPGETIRQ:
                case LPGETIRQ:
                        retval = verify_area(VERIFY_WRITE, (void *) arg,
                        retval = verify_area(VERIFY_WRITE, (void *) arg,
                            sizeof(int));
                            sizeof(int));
                        if (retval)
                        if (retval)
                                return retval;
                                return retval;
                        memcpy_tofs((int *) arg, &LP_IRQ(minor), sizeof(int));
                        memcpy_tofs((int *) arg, &LP_IRQ(minor), sizeof(int));
                        break;
                        break;
                case LPGETSTATUS:
                case LPGETSTATUS:
                        retval = verify_area(VERIFY_WRITE, (void *) arg,
                        retval = verify_area(VERIFY_WRITE, (void *) arg,
                            sizeof(int));
                            sizeof(int));
                        if (retval)
                        if (retval)
                                return retval;
                                return retval;
                        else {
                        else {
                                int status = LP_S(minor);
                                int status = LP_S(minor);
                                memcpy_tofs((int *) arg, &status, sizeof(int));
                                memcpy_tofs((int *) arg, &status, sizeof(int));
                        }
                        }
                        break;
                        break;
                case LPRESET:
                case LPRESET:
                        lp_reset(minor);
                        lp_reset(minor);
                        break;
                        break;
                case LPGETSTATS:
                case LPGETSTATS:
                        retval = verify_area(VERIFY_WRITE, (void *) arg,
                        retval = verify_area(VERIFY_WRITE, (void *) arg,
                            sizeof(struct lp_stats));
                            sizeof(struct lp_stats));
                        if (retval)
                        if (retval)
                                return retval;
                                return retval;
                        else {
                        else {
                                memcpy_tofs((int *) arg, &LP_STAT(minor), sizeof(struct lp_stats));
                                memcpy_tofs((int *) arg, &LP_STAT(minor), sizeof(struct lp_stats));
                                if (suser())
                                if (suser())
                                        memset(&LP_STAT(minor), 0, sizeof(struct lp_stats));
                                        memset(&LP_STAT(minor), 0, sizeof(struct lp_stats));
                        }
                        }
                        break;
                        break;
                case LPGETFLAGS:
                case LPGETFLAGS:
                        retval = verify_area(VERIFY_WRITE, (void *) arg,
                        retval = verify_area(VERIFY_WRITE, (void *) arg,
                            sizeof(int));
                            sizeof(int));
                        if (retval)
                        if (retval)
                                return retval;
                                return retval;
                        else {
                        else {
                                int status = LP_F(minor);
                                int status = LP_F(minor);
                                memcpy_tofs((int *) arg, &status, sizeof(int));
                                memcpy_tofs((int *) arg, &status, sizeof(int));
                        }
                        }
                        break;
                        break;
                default:
                default:
                        retval = -EINVAL;
                        retval = -EINVAL;
        }
        }
        return retval;
        return retval;
}
}
 
 
 
 
static struct file_operations lp_fops = {
static struct file_operations lp_fops = {
        lp_lseek,
        lp_lseek,
        NULL,           /* lp_read */
        NULL,           /* lp_read */
        lp_write,
        lp_write,
        NULL,           /* lp_readdir */
        NULL,           /* lp_readdir */
        NULL,           /* lp_select */
        NULL,           /* lp_select */
        lp_ioctl,
        lp_ioctl,
        NULL,           /* lp_mmap */
        NULL,           /* lp_mmap */
        lp_open,
        lp_open,
        lp_release
        lp_release
};
};
 
 
static int lp_probe(int offset)
static int lp_probe(int offset)
{
{
        int base, size;
        int base, size;
        unsigned int testvalue;
        unsigned int testvalue;
 
 
        base = LP_B(offset);
        base = LP_B(offset);
        if (base == 0)
        if (base == 0)
                return -1;              /* de-configured by command line */
                return -1;              /* de-configured by command line */
        if (LP_IRQ(offset) != NO_IRQ && LP_IRQ(offset) > 15)
        if (LP_IRQ(offset) != NO_IRQ && LP_IRQ(offset) > 15)
                return -1;              /* bogus interrupt value */
                return -1;              /* bogus interrupt value */
        size = (base == 0x3bc)? 3 : 8;
        size = (base == 0x3bc)? 3 : 8;
        if (check_region(base, size) < 0)
        if (check_region(base, size) < 0)
                return -1;
                return -1;
        /* write to port & read back to check */
        /* write to port & read back to check */
        outb_p(LP_DUMMY, base);
        outb_p(LP_DUMMY, base);
        udelay(LP_DELAY);
        udelay(LP_DELAY);
        testvalue = inb_p(base);
        testvalue = inb_p(base);
        if (testvalue == LP_DUMMY) {
        if (testvalue == LP_DUMMY) {
                LP_F(offset) |= LP_EXIST;
                LP_F(offset) |= LP_EXIST;
                lp_reset(offset);
                lp_reset(offset);
                printk(KERN_INFO "lp%d at 0x%04x, ", offset, base);
                printk(KERN_INFO "lp%d at 0x%04x, ", offset, base);
                request_region(base, size, "lp");
                request_region(base, size, "lp");
                if (LP_IRQ(offset) != NO_IRQ)
                if (LP_IRQ(offset) != NO_IRQ)
                        printk("(irq = %d)\n", LP_IRQ(offset));
                        printk("(irq = %d)\n", LP_IRQ(offset));
                else
                else
                        printk("(polling)\n");
                        printk("(polling)\n");
                return 1;
                return 1;
        } else
        } else
                return 0;
                return 0;
}
}
 
 
/* Command line parameters:
/* Command line parameters:
 
 
   When the lp driver is built in to the kernel, you may use the
   When the lp driver is built in to the kernel, you may use the
   LILO/LOADLIN command line to set the port addresses and interrupts
   LILO/LOADLIN command line to set the port addresses and interrupts
   that the driver will use.
   that the driver will use.
 
 
   Syntax:      lp=port0[,irq0[,port1[,irq1[,port2[,irq2]]]]]
   Syntax:      lp=port0[,irq0[,port1[,irq1[,port2[,irq2]]]]]
 
 
   For example:   lp=0x378,0   or   lp=0x278,5,0x378,7
   For example:   lp=0x378,0   or   lp=0x278,5,0x378,7
 
 
   Note that if this feature is used, you must specify *all* the ports
   Note that if this feature is used, you must specify *all* the ports
   you want considered, there are no defaults.  You can disable a
   you want considered, there are no defaults.  You can disable a
   built-in driver with lp=0 .
   built-in driver with lp=0 .
 
 
*/
*/
 
 
void    lp_setup(char *str, int *ints)
void    lp_setup(char *str, int *ints)
 
 
{
{
        LP_B(0)   = ((ints[0] > 0) ? ints[1] : 0 );
        LP_B(0)   = ((ints[0] > 0) ? ints[1] : 0 );
        LP_IRQ(0) = ((ints[0] > 1) ? ints[2] : NO_IRQ );
        LP_IRQ(0) = ((ints[0] > 1) ? ints[2] : NO_IRQ );
        LP_B(1)   = ((ints[0] > 2) ? ints[3] : 0 );
        LP_B(1)   = ((ints[0] > 2) ? ints[3] : 0 );
        LP_IRQ(1) = ((ints[0] > 3) ? ints[4] : NO_IRQ );
        LP_IRQ(1) = ((ints[0] > 3) ? ints[4] : NO_IRQ );
        LP_B(2)   = ((ints[0] > 4) ? ints[5] : 0 );
        LP_B(2)   = ((ints[0] > 4) ? ints[5] : 0 );
        LP_IRQ(2) = ((ints[0] > 5) ? ints[6] : NO_IRQ );
        LP_IRQ(2) = ((ints[0] > 5) ? ints[6] : NO_IRQ );
}
}
 
 
#ifdef MODULE
#ifdef MODULE
static int io[] = {0, 0, 0};
static int io[] = {0, 0, 0};
static int irq[] = {NO_IRQ, NO_IRQ, NO_IRQ};
static int irq[] = {NO_IRQ, NO_IRQ, NO_IRQ};
 
 
#define lp_init init_module
#define lp_init init_module
#endif
#endif
 
 
int lp_init(void)
int lp_init(void)
{
{
        int offset = 0;
        int offset = 0;
        int count = 0;
        int count = 0;
#ifdef MODULE
#ifdef MODULE
        int failed = 0;
        int failed = 0;
#endif
#endif
 
 
        if (register_chrdev(LP_MAJOR,"lp",&lp_fops)) {
        if (register_chrdev(LP_MAJOR,"lp",&lp_fops)) {
                printk("lp: unable to get major %d\n", LP_MAJOR);
                printk("lp: unable to get major %d\n", LP_MAJOR);
                return -EIO;
                return -EIO;
        }
        }
#ifdef MODULE
#ifdef MODULE
        /* When user feeds parameters, use them */
        /* When user feeds parameters, use them */
        for (offset=0; offset < LP_NO; offset++) {
        for (offset=0; offset < LP_NO; offset++) {
                int specified=0;
                int specified=0;
 
 
                if (io[offset] != 0) {
                if (io[offset] != 0) {
                        LP_B(offset) = io[offset];
                        LP_B(offset) = io[offset];
                        specified++;
                        specified++;
                }
                }
                if (irq[offset] != NO_IRQ) {
                if (irq[offset] != NO_IRQ) {
                        LP_IRQ(offset) = irq[offset];
                        LP_IRQ(offset) = irq[offset];
                        specified++;
                        specified++;
                }
                }
                if (specified) {
                if (specified) {
                        if (lp_probe(offset) <= 0) {
                        if (lp_probe(offset) <= 0) {
                                printk(KERN_INFO "lp%d: Not found\n", offset);
                                printk(KERN_INFO "lp%d: Not found\n", offset);
                                failed++;
                                failed++;
                        } else
                        } else
                                count++;
                                count++;
                }
                }
        }
        }
        /* Successful specified devices increase count
        /* Successful specified devices increase count
         * Unsuccessful specified devices increase failed
         * Unsuccessful specified devices increase failed
         */
         */
        if (count)
        if (count)
                return 0;
                return 0;
        if (failed) {
        if (failed) {
                printk(KERN_INFO "lp: No override devices found.\n");
                printk(KERN_INFO "lp: No override devices found.\n");
                unregister_chrdev(LP_MAJOR,"lp");
                unregister_chrdev(LP_MAJOR,"lp");
                return -EIO;
                return -EIO;
        }
        }
        /* Only get here if there were no specified devices. To continue
        /* Only get here if there were no specified devices. To continue
         * would be silly since the above code has scribbled all over the
         * would be silly since the above code has scribbled all over the
         * probe list.
         * probe list.
         */
         */
#endif
#endif
        /* take on all known port values */
        /* take on all known port values */
        for (offset = 0; offset < LP_NO; offset++) {
        for (offset = 0; offset < LP_NO; offset++) {
                int ret = lp_probe(offset);
                int ret = lp_probe(offset);
                if (ret < 0)
                if (ret < 0)
                        continue;
                        continue;
                count += ret;
                count += ret;
        }
        }
        if (count == 0)
        if (count == 0)
                printk("lp: Driver configured but no interfaces found.\n");
                printk("lp: Driver configured but no interfaces found.\n");
 
 
        return 0;
        return 0;
}
}
 
 
#ifdef MODULE
#ifdef MODULE
void cleanup_module(void)
void cleanup_module(void)
{
{
        int offset;
        int offset;
 
 
        unregister_chrdev(LP_MAJOR,"lp");
        unregister_chrdev(LP_MAJOR,"lp");
        for (offset = 0; offset < LP_NO; offset++) {
        for (offset = 0; offset < LP_NO; offset++) {
                int base, size;
                int base, size;
                base = LP_B(offset);
                base = LP_B(offset);
                size = (base == 0x3bc)? 3 : 8;
                size = (base == 0x3bc)? 3 : 8;
                if (LP_F(offset) & LP_EXIST)
                if (LP_F(offset) & LP_EXIST)
                        release_region(LP_B(offset),size);
                        release_region(LP_B(offset),size);
        }
        }
}
}
#endif
#endif
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.