Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * IEEE-1284 implementation for parport.
0003  *
0004  * Authors: Phil Blundell <philb@gnu.org>
0005  *          Carsten Gross <carsten@sol.wohnheim.uni-ulm.de>
0006  *      Jose Renau <renau@acm.org>
0007  *          Tim Waugh <tim@cyberelk.demon.co.uk> (largely rewritten)
0008  *
0009  * This file is responsible for IEEE 1284 negotiation, and for handing
0010  * read/write requests to low-level drivers.
0011  *
0012  * Any part of this program may be used in documents licensed under
0013  * the GNU Free Documentation License, Version 1.1 or any later version
0014  * published by the Free Software Foundation.
0015  *
0016  * Various hacks, Fred Barnes <frmb2@ukc.ac.uk>, 04/2000
0017  */
0018 
0019 #include <linux/module.h>
0020 #include <linux/threads.h>
0021 #include <linux/parport.h>
0022 #include <linux/delay.h>
0023 #include <linux/kernel.h>
0024 #include <linux/interrupt.h>
0025 #include <linux/timer.h>
0026 #include <linux/sched/signal.h>
0027 
0028 #undef DEBUG /* undef me for production */
0029 
0030 #ifdef CONFIG_LP_CONSOLE
0031 #undef DEBUG /* Don't want a garbled console */
0032 #endif
0033 
0034 /* Make parport_wait_peripheral wake up.
0035  * It will be useful to call this from an interrupt handler. */
0036 static void parport_ieee1284_wakeup (struct parport *port)
0037 {
0038     up (&port->physport->ieee1284.irq);
0039 }
0040 
0041 static void timeout_waiting_on_port (struct timer_list *t)
0042 {
0043     struct parport *port = from_timer(port, t, timer);
0044 
0045     parport_ieee1284_wakeup (port);
0046 }
0047 
0048 /**
0049  *  parport_wait_event - wait for an event on a parallel port
0050  *  @port: port to wait on
0051  *  @timeout: time to wait (in jiffies)
0052  *
0053  *  This function waits for up to @timeout jiffies for an
0054  *  interrupt to occur on a parallel port.  If the port timeout is
0055  *  set to zero, it returns immediately.
0056  *
0057  *  If an interrupt occurs before the timeout period elapses, this
0058  *  function returns zero immediately.  If it times out, it returns
0059  *  one.  An error code less than zero indicates an error (most
0060  *  likely a pending signal), and the calling code should finish
0061  *  what it's doing as soon as it can.
0062  */
0063 
0064 int parport_wait_event (struct parport *port, signed long timeout)
0065 {
0066     int ret;
0067 
0068     if (!port->physport->cad->timeout)
0069         /* Zero timeout is special, and we can't down() the
0070            semaphore. */
0071         return 1;
0072 
0073     timer_setup(&port->timer, timeout_waiting_on_port, 0);
0074     mod_timer(&port->timer, jiffies + timeout);
0075     ret = down_interruptible (&port->physport->ieee1284.irq);
0076     if (!del_timer_sync(&port->timer) && !ret)
0077         /* Timed out. */
0078         ret = 1;
0079 
0080     return ret;
0081 }
0082 
0083 /**
0084  *  parport_poll_peripheral - poll status lines
0085  *  @port: port to watch
0086  *  @mask: status lines to watch
0087  *  @result: desired values of chosen status lines
0088  *  @usec: timeout
0089  *
0090  *  This function busy-waits until the masked status lines have
0091  *  the desired values, or until the timeout period elapses.  The
0092  *  @mask and @result parameters are bitmasks, with the bits
0093  *  defined by the constants in parport.h: %PARPORT_STATUS_BUSY,
0094  *  and so on.
0095  *
0096  *  This function does not call schedule(); instead it busy-waits
0097  *  using udelay().  It currently has a resolution of 5usec.
0098  *
0099  *  If the status lines take on the desired values before the
0100  *  timeout period elapses, parport_poll_peripheral() returns zero
0101  *  immediately.  A return value greater than zero indicates
0102  *  a timeout.  An error code (less than zero) indicates an error,
0103  *  most likely a signal that arrived, and the caller should
0104  *  finish what it is doing as soon as possible.
0105 */
0106 
0107 int parport_poll_peripheral(struct parport *port,
0108                 unsigned char mask,
0109                 unsigned char result,
0110                 int usec)
0111 {
0112     /* Zero return code is success, >0 is timeout. */
0113     int count = usec / 5 + 2;
0114     int i;
0115     unsigned char status;
0116     for (i = 0; i < count; i++) {
0117         status = parport_read_status (port);
0118         if ((status & mask) == result)
0119             return 0;
0120         if (signal_pending (current))
0121             return -EINTR;
0122         if (need_resched())
0123             break;
0124         if (i >= 2)
0125             udelay (5);
0126     }
0127 
0128     return 1;
0129 }
0130 
0131 /**
0132  *  parport_wait_peripheral - wait for status lines to change in 35ms
0133  *  @port: port to watch
0134  *  @mask: status lines to watch
0135  *  @result: desired values of chosen status lines
0136  *
0137  *  This function waits until the masked status lines have the
0138  *  desired values, or until 35ms have elapsed (see IEEE 1284-1994
0139  *  page 24 to 25 for why this value in particular is hardcoded).
0140  *  The @mask and @result parameters are bitmasks, with the bits
0141  *  defined by the constants in parport.h: %PARPORT_STATUS_BUSY,
0142  *  and so on.
0143  *
0144  *  The port is polled quickly to start off with, in anticipation
0145  *  of a fast response from the peripheral.  This fast polling
0146  *  time is configurable (using /proc), and defaults to 500usec.
0147  *  If the timeout for this port (see parport_set_timeout()) is
0148  *  zero, the fast polling time is 35ms, and this function does
0149  *  not call schedule().
0150  *
0151  *  If the timeout for this port is non-zero, after the fast
0152  *  polling fails it uses parport_wait_event() to wait for up to
0153  *  10ms, waking up if an interrupt occurs.
0154  */
0155 
0156 int parport_wait_peripheral(struct parport *port,
0157                 unsigned char mask, 
0158                 unsigned char result)
0159 {
0160     int ret;
0161     int usec;
0162     unsigned long deadline;
0163     unsigned char status;
0164 
0165     usec = port->physport->spintime; /* usecs of fast polling */
0166     if (!port->physport->cad->timeout)
0167         /* A zero timeout is "special": busy wait for the
0168            entire 35ms. */
0169         usec = 35000;
0170 
0171     /* Fast polling.
0172      *
0173      * This should be adjustable.
0174      * How about making a note (in the device structure) of how long
0175      * it takes, so we know for next time?
0176      */
0177     ret = parport_poll_peripheral (port, mask, result, usec);
0178     if (ret != 1)
0179         return ret;
0180 
0181     if (!port->physport->cad->timeout)
0182         /* We may be in an interrupt handler, so we can't poll
0183          * slowly anyway. */
0184         return 1;
0185 
0186     /* 40ms of slow polling. */
0187     deadline = jiffies + msecs_to_jiffies(40);
0188     while (time_before (jiffies, deadline)) {
0189         if (signal_pending (current))
0190             return -EINTR;
0191 
0192         /* Wait for 10ms (or until an interrupt occurs if
0193          * the handler is set) */
0194         if ((ret = parport_wait_event (port, msecs_to_jiffies(10))) < 0)
0195             return ret;
0196 
0197         status = parport_read_status (port);
0198         if ((status & mask) == result)
0199             return 0;
0200 
0201         if (!ret) {
0202             /* parport_wait_event didn't time out, but the
0203              * peripheral wasn't actually ready either.
0204              * Wait for another 10ms. */
0205             schedule_timeout_interruptible(msecs_to_jiffies(10));
0206         }
0207     }
0208 
0209     return 1;
0210 }
0211 
0212 #ifdef CONFIG_PARPORT_1284
0213 /* Terminate a negotiated mode. */
0214 static void parport_ieee1284_terminate (struct parport *port)
0215 {
0216     int r;
0217     port = port->physport;
0218 
0219     /* EPP terminates differently. */
0220     switch (port->ieee1284.mode) {
0221     case IEEE1284_MODE_EPP:
0222     case IEEE1284_MODE_EPPSL:
0223     case IEEE1284_MODE_EPPSWE:
0224         /* Terminate from EPP mode. */
0225 
0226         /* Event 68: Set nInit low */
0227         parport_frob_control (port, PARPORT_CONTROL_INIT, 0);
0228         udelay (50);
0229 
0230         /* Event 69: Set nInit high, nSelectIn low */
0231         parport_frob_control (port,
0232                       PARPORT_CONTROL_SELECT
0233                       | PARPORT_CONTROL_INIT,
0234                       PARPORT_CONTROL_SELECT
0235                       | PARPORT_CONTROL_INIT);
0236         break;
0237 
0238     case IEEE1284_MODE_ECP:
0239     case IEEE1284_MODE_ECPRLE:
0240     case IEEE1284_MODE_ECPSWE:
0241         /* In ECP we can only terminate from fwd idle phase. */
0242         if (port->ieee1284.phase != IEEE1284_PH_FWD_IDLE) {
0243             /* Event 47: Set nInit high */
0244             parport_frob_control (port,
0245                           PARPORT_CONTROL_INIT
0246                           | PARPORT_CONTROL_AUTOFD,
0247                           PARPORT_CONTROL_INIT
0248                           | PARPORT_CONTROL_AUTOFD);
0249 
0250             /* Event 49: PError goes high */
0251             r = parport_wait_peripheral (port,
0252                              PARPORT_STATUS_PAPEROUT,
0253                              PARPORT_STATUS_PAPEROUT);
0254             if (r)
0255                 pr_debug("%s: Timeout at event 49\n",
0256                      port->name);
0257 
0258             parport_data_forward (port);
0259             pr_debug("%s: ECP direction: forward\n", port->name);
0260             port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
0261         }
0262 
0263         fallthrough;
0264 
0265     default:
0266         /* Terminate from all other modes. */
0267 
0268         /* Event 22: Set nSelectIn low, nAutoFd high */
0269         parport_frob_control (port,
0270                       PARPORT_CONTROL_SELECT
0271                       | PARPORT_CONTROL_AUTOFD,
0272                       PARPORT_CONTROL_SELECT);
0273 
0274         /* Event 24: nAck goes low */
0275         r = parport_wait_peripheral (port, PARPORT_STATUS_ACK, 0);
0276         if (r)
0277             pr_debug("%s: Timeout at event 24\n", port->name);
0278 
0279         /* Event 25: Set nAutoFd low */
0280         parport_frob_control (port,
0281                       PARPORT_CONTROL_AUTOFD,
0282                       PARPORT_CONTROL_AUTOFD);
0283 
0284         /* Event 27: nAck goes high */
0285         r = parport_wait_peripheral (port,
0286                          PARPORT_STATUS_ACK, 
0287                          PARPORT_STATUS_ACK);
0288         if (r)
0289             pr_debug("%s: Timeout at event 27\n", port->name);
0290 
0291         /* Event 29: Set nAutoFd high */
0292         parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
0293     }
0294 
0295     port->ieee1284.mode = IEEE1284_MODE_COMPAT;
0296     port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
0297 
0298     pr_debug("%s: In compatibility (forward idle) mode\n", port->name);
0299 }       
0300 #endif /* IEEE1284 support */
0301 
0302 /**
0303  *  parport_negotiate - negotiate an IEEE 1284 mode
0304  *  @port: port to use
0305  *  @mode: mode to negotiate to
0306  *
0307  *  Use this to negotiate to a particular IEEE 1284 transfer mode.
0308  *  The @mode parameter should be one of the constants in
0309  *  parport.h starting %IEEE1284_MODE_xxx.
0310  *
0311  *  The return value is 0 if the peripheral has accepted the
0312  *  negotiation to the mode specified, -1 if the peripheral is not
0313  *  IEEE 1284 compliant (or not present), or 1 if the peripheral
0314  *  has rejected the negotiation.
0315  */
0316 
0317 int parport_negotiate (struct parport *port, int mode)
0318 {
0319 #ifndef CONFIG_PARPORT_1284
0320     if (mode == IEEE1284_MODE_COMPAT)
0321         return 0;
0322     pr_err("parport: IEEE1284 not supported in this kernel\n");
0323     return -1;
0324 #else
0325     int m = mode & ~IEEE1284_ADDR;
0326     int r;
0327     unsigned char xflag;
0328 
0329     port = port->physport;
0330 
0331     /* Is there anything to do? */
0332     if (port->ieee1284.mode == mode)
0333         return 0;
0334 
0335     /* Is the difference just an address-or-not bit? */
0336     if ((port->ieee1284.mode & ~IEEE1284_ADDR) == (mode & ~IEEE1284_ADDR)){
0337         port->ieee1284.mode = mode;
0338         return 0;
0339     }
0340 
0341     /* Go to compatibility forward idle mode */
0342     if (port->ieee1284.mode != IEEE1284_MODE_COMPAT)
0343         parport_ieee1284_terminate (port);
0344 
0345     if (mode == IEEE1284_MODE_COMPAT)
0346         /* Compatibility mode: no negotiation. */
0347         return 0; 
0348 
0349     switch (mode) {
0350     case IEEE1284_MODE_ECPSWE:
0351         m = IEEE1284_MODE_ECP;
0352         break;
0353     case IEEE1284_MODE_EPPSL:
0354     case IEEE1284_MODE_EPPSWE:
0355         m = IEEE1284_MODE_EPP;
0356         break;
0357     case IEEE1284_MODE_BECP:
0358         return -ENOSYS; /* FIXME (implement BECP) */
0359     }
0360 
0361     if (mode & IEEE1284_EXT_LINK)
0362         m = 1<<7; /* request extensibility link */
0363 
0364     port->ieee1284.phase = IEEE1284_PH_NEGOTIATION;
0365 
0366     /* Start off with nStrobe and nAutoFd high, and nSelectIn low */
0367     parport_frob_control (port,
0368                   PARPORT_CONTROL_STROBE
0369                   | PARPORT_CONTROL_AUTOFD
0370                   | PARPORT_CONTROL_SELECT,
0371                   PARPORT_CONTROL_SELECT);
0372     udelay(1);
0373 
0374     /* Event 0: Set data */
0375     parport_data_forward (port);
0376     parport_write_data (port, m);
0377     udelay (400); /* Shouldn't need to wait this long. */
0378 
0379     /* Event 1: Set nSelectIn high, nAutoFd low */
0380     parport_frob_control (port,
0381                   PARPORT_CONTROL_SELECT
0382                   | PARPORT_CONTROL_AUTOFD,
0383                   PARPORT_CONTROL_AUTOFD);
0384 
0385     /* Event 2: PError, Select, nFault go high, nAck goes low */
0386     if (parport_wait_peripheral (port,
0387                      PARPORT_STATUS_ERROR
0388                      | PARPORT_STATUS_SELECT
0389                      | PARPORT_STATUS_PAPEROUT
0390                      | PARPORT_STATUS_ACK,
0391                      PARPORT_STATUS_ERROR
0392                      | PARPORT_STATUS_SELECT
0393                      | PARPORT_STATUS_PAPEROUT)) {
0394         /* Timeout */
0395         parport_frob_control (port,
0396                       PARPORT_CONTROL_SELECT
0397                       | PARPORT_CONTROL_AUTOFD,
0398                       PARPORT_CONTROL_SELECT);
0399         pr_debug("%s: Peripheral not IEEE1284 compliant (0x%02X)\n",
0400              port->name, parport_read_status (port));
0401         port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
0402         return -1; /* Not IEEE1284 compliant */
0403     }
0404 
0405     /* Event 3: Set nStrobe low */
0406     parport_frob_control (port,
0407                   PARPORT_CONTROL_STROBE,
0408                   PARPORT_CONTROL_STROBE);
0409 
0410     /* Event 4: Set nStrobe and nAutoFd high */
0411     udelay (5);
0412     parport_frob_control (port,
0413                   PARPORT_CONTROL_STROBE
0414                   | PARPORT_CONTROL_AUTOFD,
0415                   0);
0416 
0417     /* Event 6: nAck goes high */
0418     if (parport_wait_peripheral (port,
0419                      PARPORT_STATUS_ACK,
0420                      PARPORT_STATUS_ACK)) {
0421         /* This shouldn't really happen with a compliant device. */
0422         pr_debug("%s: Mode 0x%02x not supported? (0x%02x)\n",
0423              port->name, mode, port->ops->read_status (port));
0424         parport_ieee1284_terminate (port);
0425         return 1;
0426     }
0427 
0428     xflag = parport_read_status (port) & PARPORT_STATUS_SELECT;
0429 
0430     /* xflag should be high for all modes other than nibble (0). */
0431     if (mode && !xflag) {
0432         /* Mode not supported. */
0433         pr_debug("%s: Mode 0x%02x rejected by peripheral\n",
0434              port->name, mode);
0435         parport_ieee1284_terminate (port);
0436         return 1;
0437     }
0438 
0439     /* More to do if we've requested extensibility link. */
0440     if (mode & IEEE1284_EXT_LINK) {
0441         m = mode & 0x7f;
0442         udelay (1);
0443         parport_write_data (port, m);
0444         udelay (1);
0445 
0446         /* Event 51: Set nStrobe low */
0447         parport_frob_control (port,
0448                       PARPORT_CONTROL_STROBE,
0449                       PARPORT_CONTROL_STROBE);
0450 
0451         /* Event 52: nAck goes low */
0452         if (parport_wait_peripheral (port, PARPORT_STATUS_ACK, 0)) {
0453             /* This peripheral is _very_ slow. */
0454             pr_debug("%s: Event 52 didn't happen\n", port->name);
0455             parport_ieee1284_terminate (port);
0456             return 1;
0457         }
0458 
0459         /* Event 53: Set nStrobe high */
0460         parport_frob_control (port,
0461                       PARPORT_CONTROL_STROBE,
0462                       0);
0463 
0464         /* Event 55: nAck goes high */
0465         if (parport_wait_peripheral (port,
0466                          PARPORT_STATUS_ACK,
0467                          PARPORT_STATUS_ACK)) {
0468             /* This shouldn't really happen with a compliant
0469              * device. */
0470             pr_debug("%s: Mode 0x%02x not supported? (0x%02x)\n",
0471                  port->name, mode,
0472                  port->ops->read_status(port));
0473             parport_ieee1284_terminate (port);
0474             return 1;
0475         }
0476 
0477         /* Event 54: Peripheral sets XFlag to reflect support */
0478         xflag = parport_read_status (port) & PARPORT_STATUS_SELECT;
0479 
0480         /* xflag should be high. */
0481         if (!xflag) {
0482             /* Extended mode not supported. */
0483             pr_debug("%s: Extended mode 0x%02x not supported\n",
0484                  port->name, mode);
0485             parport_ieee1284_terminate (port);
0486             return 1;
0487         }
0488 
0489         /* Any further setup is left to the caller. */
0490     }
0491 
0492     /* Mode is supported */
0493     pr_debug("%s: In mode 0x%02x\n", port->name, mode);
0494     port->ieee1284.mode = mode;
0495 
0496     /* But ECP is special */
0497     if (!(mode & IEEE1284_EXT_LINK) && (m & IEEE1284_MODE_ECP)) {
0498         port->ieee1284.phase = IEEE1284_PH_ECP_SETUP;
0499 
0500         /* Event 30: Set nAutoFd low */
0501         parport_frob_control (port,
0502                       PARPORT_CONTROL_AUTOFD,
0503                       PARPORT_CONTROL_AUTOFD);
0504 
0505         /* Event 31: PError goes high. */
0506         r = parport_wait_peripheral (port,
0507                          PARPORT_STATUS_PAPEROUT,
0508                          PARPORT_STATUS_PAPEROUT);
0509         if (r) {
0510             pr_debug("%s: Timeout at event 31\n", port->name);
0511         }
0512 
0513         port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
0514         pr_debug("%s: ECP direction: forward\n", port->name);
0515     } else switch (mode) {
0516     case IEEE1284_MODE_NIBBLE:
0517     case IEEE1284_MODE_BYTE:
0518         port->ieee1284.phase = IEEE1284_PH_REV_IDLE;
0519         break;
0520     default:
0521         port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
0522     }
0523 
0524 
0525     return 0;
0526 #endif /* IEEE1284 support */
0527 }
0528 
0529 /* Acknowledge that the peripheral has data available.
0530  * Events 18-20, in order to get from Reverse Idle phase
0531  * to Host Busy Data Available.
0532  * This will most likely be called from an interrupt.
0533  * Returns zero if data was available.
0534  */
0535 #ifdef CONFIG_PARPORT_1284
0536 static int parport_ieee1284_ack_data_avail (struct parport *port)
0537 {
0538     if (parport_read_status (port) & PARPORT_STATUS_ERROR)
0539         /* Event 18 didn't happen. */
0540         return -1;
0541 
0542     /* Event 20: nAutoFd goes high. */
0543     port->ops->frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
0544     port->ieee1284.phase = IEEE1284_PH_HBUSY_DAVAIL;
0545     return 0;
0546 }
0547 #endif /* IEEE1284 support */
0548 
0549 /* Handle an interrupt. */
0550 void parport_ieee1284_interrupt (void *handle)
0551 {
0552     struct parport *port = handle;
0553     parport_ieee1284_wakeup (port);
0554 
0555 #ifdef CONFIG_PARPORT_1284
0556     if (port->ieee1284.phase == IEEE1284_PH_REV_IDLE) {
0557         /* An interrupt in this phase means that data
0558          * is now available. */
0559         pr_debug("%s: Data available\n", port->name);
0560         parport_ieee1284_ack_data_avail (port);
0561     }
0562 #endif /* IEEE1284 support */
0563 }
0564 
0565 /**
0566  *  parport_write - write a block of data to a parallel port
0567  *  @port: port to write to
0568  *  @buffer: data buffer (in kernel space)
0569  *  @len: number of bytes of data to transfer
0570  *
0571  *  This will write up to @len bytes of @buffer to the port
0572  *  specified, using the IEEE 1284 transfer mode most recently
0573  *  negotiated to (using parport_negotiate()), as long as that
0574  *  mode supports forward transfers (host to peripheral).
0575  *
0576  *  It is the caller's responsibility to ensure that the first
0577  *  @len bytes of @buffer are valid.
0578  *
0579  *  This function returns the number of bytes transferred (if zero
0580  *  or positive), or else an error code.
0581  */
0582 
0583 ssize_t parport_write (struct parport *port, const void *buffer, size_t len)
0584 {
0585 #ifndef CONFIG_PARPORT_1284
0586     return port->ops->compat_write_data (port, buffer, len, 0);
0587 #else
0588     ssize_t retval;
0589     int mode = port->ieee1284.mode;
0590     int addr = mode & IEEE1284_ADDR;
0591     size_t (*fn) (struct parport *, const void *, size_t, int);
0592 
0593     /* Ignore the device-ID-request bit and the address bit. */
0594     mode &= ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
0595 
0596     /* Use the mode we're in. */
0597     switch (mode) {
0598     case IEEE1284_MODE_NIBBLE:
0599     case IEEE1284_MODE_BYTE:
0600         parport_negotiate (port, IEEE1284_MODE_COMPAT);
0601         fallthrough;
0602     case IEEE1284_MODE_COMPAT:
0603         pr_debug("%s: Using compatibility mode\n", port->name);
0604         fn = port->ops->compat_write_data;
0605         break;
0606 
0607     case IEEE1284_MODE_EPP:
0608         pr_debug("%s: Using EPP mode\n", port->name);
0609         if (addr) {
0610             fn = port->ops->epp_write_addr;
0611         } else {
0612             fn = port->ops->epp_write_data;
0613         }
0614         break;
0615     case IEEE1284_MODE_EPPSWE:
0616         pr_debug("%s: Using software-emulated EPP mode\n", port->name);
0617         if (addr) {
0618             fn = parport_ieee1284_epp_write_addr;
0619         } else {
0620             fn = parport_ieee1284_epp_write_data;
0621         }
0622         break;
0623     case IEEE1284_MODE_ECP:
0624     case IEEE1284_MODE_ECPRLE:
0625         pr_debug("%s: Using ECP mode\n", port->name);
0626         if (addr) {
0627             fn = port->ops->ecp_write_addr;
0628         } else {
0629             fn = port->ops->ecp_write_data;
0630         }
0631         break;
0632 
0633     case IEEE1284_MODE_ECPSWE:
0634         pr_debug("%s: Using software-emulated ECP mode\n", port->name);
0635         /* The caller has specified that it must be emulated,
0636          * even if we have ECP hardware! */
0637         if (addr) {
0638             fn = parport_ieee1284_ecp_write_addr;
0639         } else {
0640             fn = parport_ieee1284_ecp_write_data;
0641         }
0642         break;
0643 
0644     default:
0645         pr_debug("%s: Unknown mode 0x%02x\n",
0646              port->name, port->ieee1284.mode);
0647         return -ENOSYS;
0648     }
0649 
0650     retval = (*fn) (port, buffer, len, 0);
0651     pr_debug("%s: wrote %zd/%zu bytes\n", port->name, retval, len);
0652     return retval;
0653 #endif /* IEEE1284 support */
0654 }
0655 
0656 /**
0657  *  parport_read - read a block of data from a parallel port
0658  *  @port: port to read from
0659  *  @buffer: data buffer (in kernel space)
0660  *  @len: number of bytes of data to transfer
0661  *
0662  *  This will read up to @len bytes of @buffer to the port
0663  *  specified, using the IEEE 1284 transfer mode most recently
0664  *  negotiated to (using parport_negotiate()), as long as that
0665  *  mode supports reverse transfers (peripheral to host).
0666  *
0667  *  It is the caller's responsibility to ensure that the first
0668  *  @len bytes of @buffer are available to write to.
0669  *
0670  *  This function returns the number of bytes transferred (if zero
0671  *  or positive), or else an error code.
0672  */
0673 
0674 ssize_t parport_read (struct parport *port, void *buffer, size_t len)
0675 {
0676 #ifndef CONFIG_PARPORT_1284
0677     pr_err("parport: IEEE1284 not supported in this kernel\n");
0678     return -ENODEV;
0679 #else
0680     int mode = port->physport->ieee1284.mode;
0681     int addr = mode & IEEE1284_ADDR;
0682     size_t (*fn) (struct parport *, void *, size_t, int);
0683 
0684     /* Ignore the device-ID-request bit and the address bit. */
0685     mode &= ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
0686 
0687     /* Use the mode we're in. */
0688     switch (mode) {
0689     case IEEE1284_MODE_COMPAT:
0690         /* if we can tri-state use BYTE mode instead of NIBBLE mode,
0691          * if that fails, revert to NIBBLE mode -- ought to store somewhere
0692          * the device's ability to do BYTE mode reverse transfers, so we don't
0693          * end up needlessly calling negotiate(BYTE) repeately..  (fb)
0694          */
0695         if ((port->physport->modes & PARPORT_MODE_TRISTATE) &&
0696             !parport_negotiate (port, IEEE1284_MODE_BYTE)) {
0697             /* got into BYTE mode OK */
0698             pr_debug("%s: Using byte mode\n", port->name);
0699             fn = port->ops->byte_read_data;
0700             break;
0701         }
0702         if (parport_negotiate (port, IEEE1284_MODE_NIBBLE)) {
0703             return -EIO;
0704         }
0705         fallthrough;    /* to NIBBLE */
0706     case IEEE1284_MODE_NIBBLE:
0707         pr_debug("%s: Using nibble mode\n", port->name);
0708         fn = port->ops->nibble_read_data;
0709         break;
0710 
0711     case IEEE1284_MODE_BYTE:
0712         pr_debug("%s: Using byte mode\n", port->name);
0713         fn = port->ops->byte_read_data;
0714         break;
0715 
0716     case IEEE1284_MODE_EPP:
0717         pr_debug("%s: Using EPP mode\n", port->name);
0718         if (addr) {
0719             fn = port->ops->epp_read_addr;
0720         } else {
0721             fn = port->ops->epp_read_data;
0722         }
0723         break;
0724     case IEEE1284_MODE_EPPSWE:
0725         pr_debug("%s: Using software-emulated EPP mode\n", port->name);
0726         if (addr) {
0727             fn = parport_ieee1284_epp_read_addr;
0728         } else {
0729             fn = parport_ieee1284_epp_read_data;
0730         }
0731         break;
0732     case IEEE1284_MODE_ECP:
0733     case IEEE1284_MODE_ECPRLE:
0734         pr_debug("%s: Using ECP mode\n", port->name);
0735         fn = port->ops->ecp_read_data;
0736         break;
0737 
0738     case IEEE1284_MODE_ECPSWE:
0739         pr_debug("%s: Using software-emulated ECP mode\n", port->name);
0740         fn = parport_ieee1284_ecp_read_data;
0741         break;
0742 
0743     default:
0744         pr_debug("%s: Unknown mode 0x%02x\n",
0745              port->name, port->physport->ieee1284.mode);
0746         return -ENOSYS;
0747     }
0748 
0749     return (*fn) (port, buffer, len, 0);
0750 #endif /* IEEE1284 support */
0751 }
0752 
0753 /**
0754  *  parport_set_timeout - set the inactivity timeout for a device
0755  *  @dev: device on a port
0756  *  @inactivity: inactivity timeout (in jiffies)
0757  *
0758  *  This sets the inactivity timeout for a particular device on a
0759  *  port.  This affects functions like parport_wait_peripheral().
0760  *  The special value 0 means not to call schedule() while dealing
0761  *  with this device.
0762  *
0763  *  The return value is the previous inactivity timeout.
0764  *
0765  *  Any callers of parport_wait_event() for this device are woken
0766  *  up.
0767  */
0768 
0769 long parport_set_timeout (struct pardevice *dev, long inactivity)
0770 {
0771     long int old = dev->timeout;
0772 
0773     dev->timeout = inactivity;
0774 
0775     if (dev->port->physport->cad == dev)
0776         parport_ieee1284_wakeup (dev->port);
0777 
0778     return old;
0779 }
0780 
0781 /* Exported symbols for modules. */
0782 
0783 EXPORT_SYMBOL(parport_negotiate);
0784 EXPORT_SYMBOL(parport_write);
0785 EXPORT_SYMBOL(parport_read);
0786 EXPORT_SYMBOL(parport_wait_peripheral);
0787 EXPORT_SYMBOL(parport_wait_event);
0788 EXPORT_SYMBOL(parport_set_timeout);
0789 EXPORT_SYMBOL(parport_ieee1284_interrupt);