home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / KERNEL-S / V1.2 / LINUX-1.2 / LINUX-1 / linux / drivers / net / plip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-20  |  26.8 KB  |  1,116 lines

  1. /* $Id: plip.c,v 1.12 1995/02/11 10:26:05 gniibe Exp $ */
  2. /* PLIP: A parallel port "network" driver for Linux. */
  3. /* This driver is for parallel port with 5-bit cable (LapLink (R) cable). */
  4. /*
  5.  * Authors:    Donald Becker,  <becker@super.org>
  6.  *        Tommy Thorn, <thorn@daimi.aau.dk>
  7.  *        Tanabe Hiroyasu, <hiro@sanpo.t.u-tokyo.ac.jp>
  8.  *        Alan Cox, <gw4pts@gw4pts.ampr.org>
  9.  *        Peter Bauer, <100136.3530@compuserve.com>
  10.  *        Niibe Yutaka, <gniibe@mri.co.jp>
  11.  *
  12.  *        Modularization and ifreq/ifmap support by Alan Cox.
  13.  *        Rewritten by Niibe Yutaka.
  14.  *
  15.  *        This program is free software; you can redistribute it and/or
  16.  *        modify it under the terms of the GNU General Public License
  17.  *        as published by the Free Software Foundation; either version
  18.  *        2 of the License, or (at your option) any later version.
  19.  */
  20.  
  21. /*
  22.  * Original version and the name 'PLIP' from Donald Becker <becker@super.org>
  23.  * inspired by Russ Nelson's parallel port packet driver.
  24.  *
  25.  * NOTE:
  26.  *     Tanabe Hiroyasu had changed the protocol, and it was in Linux v1.0.
  27.  *     Because of the necessity to communicate to DOS machines with the
  28.  *     Crynwr packet driver, Peter Bauer changed the protocol again
  29.  *     back to original protocol.
  30.  *
  31.  *     This version follows original PLIP protocol. 
  32.  *     So, this PLIP can't communicate the PLIP of Linux v1.0.
  33.  */
  34.  
  35. static char *version = "NET3 PLIP version 2.0 gniibe@mri.co.jp\n";
  36.  
  37. /*
  38.   Sources:
  39.     Ideas and protocols came from Russ Nelson's <nelson@crynwr.com>
  40.     "parallel.asm" parallel port packet driver.
  41.  
  42.   The "Crynwr" parallel port standard specifies the following protocol:
  43.     Trigger by sending '0x08' (this cause interrupt on other end)
  44.     count-low octet
  45.     count-high octet
  46.     ... data octets
  47.     checksum octet
  48.   Each octet is sent as <wait for rx. '0x1?'> <send 0x10+(octet&0x0F)>
  49.             <wait for rx. '0x0?'> <send 0x00+((octet>>4)&0x0F)>
  50.  
  51.   The packet is encapsulated as if it were ethernet.
  52.  
  53.   The cable used is a de facto standard parallel null cable -- sold as
  54.   a "LapLink" cable by various places.  You'll need a 12-conductor cable to
  55.   make one yourself.  The wiring is:
  56.     SLCTIN    17 - 17
  57.     GROUND    25 - 25
  58.     D0->ERROR    2 - 15        15 - 2
  59.     D1->SLCT    3 - 13        13 - 3
  60.     D2->PAPOUT    4 - 12        12 - 4
  61.     D3->ACK    5 - 10        10 - 5
  62.     D4->BUSY    6 - 11        11 - 6
  63.   Do not connect the other pins.  They are
  64.     D5,D6,D7 are 7,8,9
  65.     STROBE is 1, FEED is 14, INIT is 16
  66.     extra grounds are 18,19,20,21,22,23,24
  67. */
  68.  
  69. #ifdef MODULE
  70. #include <linux/module.h>
  71. #include <linux/version.h>
  72. #else
  73. #define MOD_INC_USE_COUNT
  74. #define MOD_DEC_USE_COUNT
  75. #endif
  76.  
  77. #include <linux/kernel.h>
  78. #include <linux/sched.h>
  79. #include <linux/types.h>
  80. #include <linux/fcntl.h>
  81. #include <linux/interrupt.h>
  82. #include <linux/string.h>
  83. #include <linux/ptrace.h>
  84. #include <linux/if_ether.h>
  85. #include <asm/system.h>
  86. #include <asm/io.h>
  87. #include <linux/in.h>
  88. #include <linux/errno.h>
  89. #include <linux/delay.h>
  90. #include <linux/lp.h>
  91.  
  92. #include <linux/netdevice.h>
  93. #include <linux/etherdevice.h>
  94. #include <linux/skbuff.h>
  95. #include <linux/if_plip.h>
  96.  
  97. #include <linux/tqueue.h>
  98. #include <linux/ioport.h>
  99. #include <asm/bitops.h>
  100. #include <asm/irq.h>
  101.  
  102. /* Use 0 for production, 1 for verification, >2 for debug */
  103. #ifndef NET_DEBUG
  104. #define NET_DEBUG 1
  105. #endif
  106. static unsigned int net_debug = NET_DEBUG;
  107.  
  108. /* In micro second */
  109. #define PLIP_DELAY_UNIT           1
  110.  
  111. /* Connection time out = PLIP_TRIGGER_WAIT * PLIP_DELAY_UNIT usec */
  112. #define PLIP_TRIGGER_WAIT     500
  113.  
  114. /* Nibble time out = PLIP_NIBBLE_WAIT * PLIP_DELAY_UNIT usec */
  115. #define PLIP_NIBBLE_WAIT        3000
  116.  
  117. #define PAR_INTR_ON        (LP_PINITP|LP_PSELECP|LP_PINTEN)
  118. #define PAR_INTR_OFF        (LP_PINITP|LP_PSELECP)
  119. #define PAR_DATA(dev)        ((dev)->base_addr+0)
  120. #define PAR_STATUS(dev)        ((dev)->base_addr+1)
  121. #define PAR_CONTROL(dev)    ((dev)->base_addr+2)
  122.  
  123. /* Bottom halfs */
  124. static void plip_kick_bh(struct device *dev);
  125. static void plip_bh(struct device *dev);
  126.  
  127. /* Interrupt handler */
  128. static void plip_interrupt(int irq, struct pt_regs *regs);
  129.  
  130. /* Functions for DEV methods */
  131. static int plip_rebuild_header(void *buff, struct device *dev,
  132.                    unsigned long raddr, struct sk_buff *skb);
  133. static int plip_tx_packet(struct sk_buff *skb, struct device *dev);
  134. static int plip_open(struct device *dev);
  135. static int plip_close(struct device *dev);
  136. static struct enet_statistics *plip_get_stats(struct device *dev);
  137. static int plip_config(struct device *dev, struct ifmap *map);
  138. static int plip_ioctl(struct device *dev, struct ifreq *ifr, int cmd);
  139.  
  140. enum plip_connection_state {
  141.     PLIP_CN_NONE=0,
  142.     PLIP_CN_RECEIVE,
  143.     PLIP_CN_SEND,
  144.     PLIP_CN_CLOSING,
  145.     PLIP_CN_ERROR
  146. };
  147.  
  148. enum plip_packet_state {
  149.     PLIP_PK_DONE=0,
  150.     PLIP_PK_TRIGGER,
  151.     PLIP_PK_LENGTH_LSB,
  152.     PLIP_PK_LENGTH_MSB,
  153.     PLIP_PK_DATA,
  154.     PLIP_PK_CHECKSUM
  155. };
  156.  
  157. enum plip_nibble_state {
  158.     PLIP_NB_BEGIN,
  159.     PLIP_NB_1,
  160.     PLIP_NB_2,
  161. };
  162.  
  163. struct plip_local {
  164.     enum plip_packet_state state;
  165.     enum plip_nibble_state nibble;
  166.     union {
  167.         struct {
  168. #if defined(__i386__)
  169.             unsigned char lsb;
  170.             unsigned char msb;
  171. #elif defined(__mc68000__)
  172.             unsigned char msb;
  173.             unsigned char lsb;
  174. #elif defined(__sparc__)
  175.             unsigned char msb;
  176.             unsigned char lsb;
  177. #elif defined(__MIPSEL__)
  178.             unsigned char lsb;
  179.             unsigned char msb;
  180. #elif defined(__MIPSEB__)
  181.             unsigned char msb;
  182.             unsigned char lsb;
  183. #elif defined(__alpha__)
  184.             unsigned char lsb;
  185.             unsigned char msb;
  186. #else
  187. #error    "Adjust this structure to match your CPU"
  188. #endif                        
  189.         } b;
  190.         unsigned short h;
  191.     } length;
  192.     unsigned short byte;
  193.     unsigned char  checksum;
  194.     unsigned char  data;
  195.     struct sk_buff *skb;
  196. };
  197.  
  198. struct net_local {
  199.     struct enet_statistics enet_stats;
  200.     struct tq_struct immediate;
  201.     struct tq_struct deferred;
  202.     struct plip_local snd_data;
  203.     struct plip_local rcv_data;
  204.     unsigned long  trigger;
  205.     unsigned long  nibble;
  206.     enum plip_connection_state connection;
  207.     unsigned short timeout_count;
  208.     char is_deferred;
  209.     int (*orig_rebuild_header)(void *eth, struct device *dev,
  210.                    unsigned long raddr, struct sk_buff *skb);
  211. };
  212.  
  213. /* Entry point of PLIP driver.
  214.    Probe the hardware, and register/initialize the driver. */
  215. int
  216. plip_init(struct device *dev)
  217. {
  218.     struct net_local *nl;
  219.  
  220.     /* Check region before the probe */
  221.     if (check_region(PAR_DATA(dev), 3) < 0)
  222.         return -ENODEV;
  223.  
  224.     /* Check that there is something at base_addr. */
  225.     outb(0, PAR_DATA(dev));
  226.     udelay(1000);
  227.     if (inb(PAR_DATA(dev)) != 0)
  228.         return -ENODEV;
  229.  
  230.     printk(version);
  231.     printk("%s: Parallel port at %#3lx, ", dev->name, dev->base_addr);
  232.     if (dev->irq) {
  233.         printk("using assigned IRQ %d.\n", dev->irq);
  234.     } else {
  235.         int irq = 0;
  236. #ifdef MODULE
  237.         /* dev->irq==0 means autoprobe, but we don't try to do so
  238.            with module.  We can change it by ifconfig */
  239. #else
  240.         unsigned int irqs = probe_irq_on();
  241.  
  242.         outb(0x00, PAR_CONTROL(dev));
  243.         udelay(1000);
  244.         outb(PAR_INTR_OFF, PAR_CONTROL(dev));
  245.         outb(PAR_INTR_ON, PAR_CONTROL(dev));
  246.         outb(PAR_INTR_OFF, PAR_CONTROL(dev));
  247.         udelay(1000);
  248.         irq = probe_irq_off(irqs);
  249. #endif
  250.         if (irq > 0) {
  251.             dev->irq = irq;
  252.             printk("using probed IRQ %d.\n", dev->irq);
  253.         } else
  254.             printk("failed to detect IRQ(%d) --"
  255.                    " Please set IRQ by ifconfig.\n", irq);
  256.     }
  257.  
  258.     request_region(PAR_DATA(dev), 3, dev->name);
  259.  
  260.     /* Fill in the generic fields of the device structure. */
  261.     ether_setup(dev);
  262.  
  263.     /* Then, override parts of it */
  264.     dev->hard_start_xmit    = plip_tx_packet;
  265.     dev->open        = plip_open;
  266.     dev->stop        = plip_close;
  267.     dev->get_stats         = plip_get_stats;
  268.     dev->set_config        = plip_config;
  269.     dev->do_ioctl        = plip_ioctl;
  270.     dev->flags            = IFF_POINTOPOINT|IFF_NOARP;
  271.  
  272.     /* Set the private structure */
  273.     dev->priv = kmalloc(sizeof (struct net_local), GFP_KERNEL);
  274.     if (dev->priv == NULL)
  275.         return EAGAIN;
  276.     memset(dev->priv, 0, sizeof(struct net_local));
  277.     nl = (struct net_local *) dev->priv;
  278.  
  279.     nl->orig_rebuild_header = dev->rebuild_header;
  280.     dev->rebuild_header     = plip_rebuild_header;
  281.  
  282.     /* Initialize constants */
  283.     nl->trigger    = PLIP_TRIGGER_WAIT;
  284.     nl->nibble    = PLIP_NIBBLE_WAIT;
  285.  
  286.     /* Initialize task queue structures */
  287.     nl->immediate.next = &tq_last;
  288.     nl->immediate.sync = 0;
  289.     nl->immediate.routine = (void *)(void *)plip_bh;
  290.     nl->immediate.data = dev;
  291.  
  292.     nl->deferred.next = &tq_last;
  293.     nl->deferred.sync = 0;
  294.     nl->deferred.routine = (void *)(void *)plip_kick_bh;
  295.     nl->deferred.data = dev;
  296.  
  297.     return 0;
  298. }
  299.  
  300. /* Bottom half handler for the delayed request.
  301.    This routine is kicked by do_timer().
  302.    Request `plip_bh' to be invoked. */
  303. static void
  304. plip_kick_bh(struct device *dev)
  305. {
  306.     struct net_local *nl = (struct net_local *)dev->priv;
  307.  
  308.     if (nl->is_deferred) {
  309.         queue_task(&nl->immediate, &tq_immediate);
  310.         mark_bh(IMMEDIATE_BH);
  311.     }
  312. }
  313.  
  314. /* Forward declarations of internal routines */
  315. static int plip_none(struct device *, struct net_local *,
  316.              struct plip_local *, struct plip_local *);
  317. static int plip_receive_packet(struct device *, struct net_local *,
  318.                    struct plip_local *, struct plip_local *);
  319. static int plip_send_packet(struct device *, struct net_local *,
  320.                 struct plip_local *, struct plip_local *);
  321. static int plip_connection_close(struct device *, struct net_local *,
  322.                  struct plip_local *, struct plip_local *);
  323. static int plip_error(struct device *, struct net_local *,
  324.               struct plip_local *, struct plip_local *);
  325. static int plip_bh_timeout_error(struct device *dev, struct net_local *nl,
  326.                  struct plip_local *snd,
  327.                  struct plip_local *rcv,
  328.                  int error);
  329.  
  330. #define OK        0
  331. #define TIMEOUT   1
  332. #define ERROR     2
  333.  
  334. typedef int (*plip_func)(struct device *dev, struct net_local *nl,
  335.              struct plip_local *snd, struct plip_local *rcv);
  336.  
  337. static plip_func connection_state_table[] =
  338. {
  339.     plip_none,
  340.     plip_receive_packet,
  341.     plip_send_packet,
  342.     plip_connection_close,
  343.     plip_error
  344. };
  345.  
  346. /* Bottom half handler of PLIP. */
  347. static void
  348. plip_bh(struct device *dev)
  349. {
  350.     struct net_local *nl = (struct net_local *)dev->priv;
  351.     struct plip_local *snd = &nl->snd_data;
  352.     struct plip_local *rcv = &nl->rcv_data;
  353.     plip_func f;
  354.     int r;
  355.  
  356.     nl->is_deferred = 0;
  357.     f = connection_state_table[nl->connection];
  358.     if ((r = (*f)(dev, nl, snd, rcv)) != OK
  359.         && (r = plip_bh_timeout_error(dev, nl, snd, rcv, r)) != OK) {
  360.         nl->is_deferred = 1;
  361.         queue_task(&nl->deferred, &tq_timer);
  362.     }
  363. }
  364.  
  365. static int
  366. plip_bh_timeout_error(struct device *dev, struct net_local *nl,
  367.               struct plip_local *snd, struct plip_local *rcv,
  368.               int error)
  369. {
  370.     unsigned char c0;
  371.  
  372.     cli();
  373.     if (nl->connection == PLIP_CN_SEND) {
  374.  
  375.         if (error != ERROR) { /* Timeout */
  376.             nl->timeout_count++;
  377.             if ((snd->state == PLIP_PK_TRIGGER
  378.                  && nl->timeout_count <= 10)
  379.                 || nl->timeout_count <= 3) {
  380.                 sti();
  381.                 /* Try again later */
  382.                 return TIMEOUT;
  383.             }
  384.             c0 = inb(PAR_STATUS(dev));
  385.             printk("%s: transmit timeout(%d,%02x)\n",
  386.                    dev->name, snd->state, c0);
  387.         }
  388.         nl->enet_stats.tx_errors++;
  389.         nl->enet_stats.tx_aborted_errors++;
  390.     } else if (nl->connection == PLIP_CN_RECEIVE) {
  391.         if (rcv->state == PLIP_PK_TRIGGER) {
  392.             /* Transmission was interrupted. */
  393.             sti();
  394.             return OK;
  395.         }
  396.         if (error != ERROR) { /* Timeout */
  397.             if (++nl->timeout_count <= 3) {
  398.                 sti();
  399.                 /* Try again later */
  400.                 return TIMEOUT;
  401.             }
  402.             c0 = inb(PAR_STATUS(dev));
  403.             printk("%s: receive timeout(%d,%02x)\n",
  404.                    dev->name, rcv->state, c0);
  405.         }
  406.         nl->enet_stats.rx_dropped++;
  407.     }
  408.     rcv->state = PLIP_PK_DONE;
  409.     if (rcv->skb) {
  410.         rcv->skb->free = 1;
  411.         kfree_skb(rcv->skb, FREE_READ);
  412.         rcv->skb = NULL;
  413.     }
  414.     snd->state = PLIP_PK_DONE;
  415.     if (snd->skb) {
  416.         dev_kfree_skb(snd->skb, FREE_WRITE);
  417.         snd->skb = NULL;
  418.     }
  419.     disable_irq(dev->irq);
  420.     outb(PAR_INTR_OFF, PAR_CONTROL(dev));
  421.     dev->tbusy = 1;
  422.     nl->connection = PLIP_CN_ERROR;
  423.     outb(0x00, PAR_DATA(dev));
  424.     sti();
  425.  
  426.     return TIMEOUT;
  427. }
  428.  
  429. static int
  430. plip_none(struct device *dev, struct net_local *nl,
  431.       struct plip_local *snd, struct plip_local *rcv)
  432. {
  433.     return OK;
  434. }
  435.  
  436. /* PLIP_RECEIVE --- receive a byte(two nibbles)
  437.    Returns OK on success, TIMEOUT on timeout */
  438. inline static int
  439. plip_receive(unsigned short nibble_timeout, unsigned short status_addr,
  440.          enum plip_nibble_state *ns_p, unsigned char *data_p)
  441. {
  442.     unsigned char c0, c1;
  443.     unsigned int cx;
  444.  
  445.     switch (*ns_p) {
  446.     case PLIP_NB_BEGIN:
  447.         cx = nibble_timeout;
  448.         while (1) {
  449.             c0 = inb(status_addr);
  450.             udelay(PLIP_DELAY_UNIT);
  451.             if ((c0 & 0x80) == 0) {
  452.                 c1 = inb(status_addr);
  453.                 if (c0 == c1)
  454.                     break;
  455.             }
  456.             if (--cx == 0)
  457.                 return TIMEOUT;
  458.         }
  459.         *data_p = (c0 >> 3) & 0x0f;
  460.         outb(0x10, --status_addr); /* send ACK */
  461.         status_addr++;
  462.         *ns_p = PLIP_NB_1;
  463.  
  464.     case PLIP_NB_1:
  465.         cx = nibble_timeout;
  466.         while (1) {
  467.             c0 = inb(status_addr);
  468.             udelay(PLIP_DELAY_UNIT);
  469.             if (c0 & 0x80) {
  470.                 c1 = inb(status_addr);
  471.                 if (c0 == c1)
  472.                     break;
  473.             }
  474.             if (--cx == 0)
  475.                 return TIMEOUT;
  476.         }
  477.         *data_p |= (c0 << 1) & 0xf0;
  478.         outb(0x00, --status_addr); /* send ACK */
  479.         status_addr++;
  480.         *ns_p = PLIP_NB_BEGIN;
  481.         return OK;
  482.  
  483.     case PLIP_NB_2:
  484.     }
  485. }
  486.  
  487. /* PLIP_RECEIVE_PACKET --- receive a packet */
  488. static int
  489. plip_receive_packet(struct device *dev, struct net_local *nl,
  490.             struct plip_local *snd, struct plip_local *rcv)
  491. {
  492.     unsigned short status_addr = PAR_STATUS(dev);
  493.     unsigned short nibble_timeout = nl->nibble;
  494.     unsigned char *lbuf;
  495.  
  496.     switch (rcv->state) {
  497.     case PLIP_PK_TRIGGER:
  498.         disable_irq(dev->irq);
  499.         outb(PAR_INTR_OFF, PAR_CONTROL(dev));
  500.         dev->interrupt = 0;
  501.         outb(0x01, PAR_DATA(dev)); /* send ACK */
  502.         if (net_debug > 2)
  503.             printk("%s: receive start\n", dev->name);
  504.         rcv->state = PLIP_PK_LENGTH_LSB;
  505.         rcv->nibble = PLIP_NB_BEGIN;
  506.  
  507.     case PLIP_PK_LENGTH_LSB:
  508.         if (snd->state != PLIP_PK_DONE) {
  509.             if (plip_receive(nl->trigger, status_addr,
  510.                      &rcv->nibble, &rcv->length.b.lsb)) {
  511.                 /* collision, here dev->tbusy == 1 */
  512.                 rcv->state = PLIP_PK_DONE;
  513.                 nl->is_deferred = 1;
  514.                 nl->connection = PLIP_CN_SEND;
  515.                 queue_task(&nl->deferred, &tq_timer);
  516.                 outb(PAR_INTR_ON, PAR_CONTROL(dev));
  517.                 enable_irq(dev->irq);
  518.                 return OK;
  519.             }
  520.         } else {
  521.             if (plip_receive(nibble_timeout, status_addr,
  522.                      &rcv->nibble, &rcv->length.b.lsb))
  523.                 return TIMEOUT;
  524.         }
  525.         rcv->state = PLIP_PK_LENGTH_MSB;
  526.  
  527.     case PLIP_PK_LENGTH_MSB:
  528.         if (plip_receive(nibble_timeout, status_addr,
  529.                  &rcv->nibble, &rcv->length.b.msb))
  530.             return TIMEOUT;
  531.         if (rcv->length.h > dev->mtu || rcv->length.h < 8) {
  532.             printk("%s: bogus packet size %d.\n", dev->name, rcv->length.h);
  533.             return ERROR;
  534.         }
  535.         /* Malloc up new buffer. */
  536.         rcv->skb = alloc_skb(rcv->length.h, GFP_ATOMIC);
  537.         if (rcv->skb == NULL) {
  538.             printk("%s: Memory squeeze.\n", dev->name);
  539.             return ERROR;
  540.         }
  541.         rcv->skb->len = rcv->length.h;
  542.         rcv->skb->dev = dev;
  543.         rcv->state = PLIP_PK_DATA;
  544.         rcv->byte = 0;
  545.         rcv->checksum = 0;
  546.  
  547.     case PLIP_PK_DATA:
  548.         lbuf = rcv->skb->data;
  549.         do
  550.             if (plip_receive(nibble_timeout, status_addr, 
  551.                      &rcv->nibble, &lbuf[rcv->byte]))
  552.                 return TIMEOUT;
  553.         while (++rcv->byte < rcv->length.h);
  554.         do
  555.             rcv->checksum += lbuf[--rcv->byte];
  556.         while (rcv->byte);
  557.         rcv->state = PLIP_PK_CHECKSUM;
  558.  
  559.     case PLIP_PK_CHECKSUM:
  560.         if (plip_receive(nibble_timeout, status_addr,
  561.                  &rcv->nibble, &rcv->data))
  562.             return TIMEOUT;
  563.         if (rcv->data != rcv->checksum) {
  564.             nl->enet_stats.rx_crc_errors++;
  565.             if (net_debug)
  566.                 printk("%s: checksum error\n", dev->name);
  567.             return ERROR;
  568.         }
  569.         rcv->state = PLIP_PK_DONE;
  570.  
  571.     case PLIP_PK_DONE:
  572.         /* Inform the upper layer for the arrival of a packet. */
  573.         netif_rx(rcv->skb);
  574.         nl->enet_stats.rx_packets++;
  575.         rcv->skb = NULL;
  576.         if (net_debug > 2)
  577.             printk("%s: receive end\n", dev->name);
  578.  
  579.         /* Close the connection. */
  580.         outb (0x00, PAR_DATA(dev));
  581.         cli();
  582.         if (snd->state != PLIP_PK_DONE) {
  583.             nl->connection = PLIP_CN_SEND;
  584.             sti();
  585.             queue_task(&nl->immediate, &tq_immediate);
  586.             outb(PAR_INTR_ON, PAR_CONTROL(dev));
  587.             enable_irq(dev->irq);
  588.             return OK;
  589.         } else {
  590.             nl->connection = PLIP_CN_NONE;
  591.             sti();
  592.             outb(PAR_INTR_ON, PAR_CONTROL(dev));
  593.             enable_irq(dev->irq);
  594.             return OK;
  595.         }
  596.     }
  597.     return OK;
  598. }
  599.  
  600. /* PLIP_SEND --- send a byte (two nibbles) 
  601.    Returns OK on success, TIMEOUT when timeout    */
  602. inline static int
  603. plip_send(unsigned short nibble_timeout, unsigned short data_addr,
  604.       enum plip_nibble_state *ns_p, unsigned char data)
  605. {
  606.     unsigned char c0;
  607.     unsigned int cx;
  608.  
  609.     switch (*ns_p) {
  610.     case PLIP_NB_BEGIN:
  611.         outb((data & 0x0f), data_addr);
  612.         *ns_p = PLIP_NB_1;
  613.  
  614.     case PLIP_NB_1:
  615.         outb(0x10 | (data & 0x0f), data_addr);
  616.         cx = nibble_timeout;
  617.         data_addr++;
  618.         while (1) {
  619.             c0 = inb(data_addr);
  620.             if ((c0 & 0x80) == 0) 
  621.                 break;
  622.             if (--cx == 0)
  623.                 return TIMEOUT;
  624.             udelay(PLIP_DELAY_UNIT);
  625.         }
  626.         outb(0x10 | (data >> 4), --data_addr);
  627.         *ns_p = PLIP_NB_2;
  628.  
  629.     case PLIP_NB_2:
  630.         outb((data >> 4), data_addr);
  631.         data_addr++;
  632.         cx = nibble_timeout;
  633.         while (1) {
  634.             c0 = inb(data_addr);
  635.             if (c0 & 0x80)
  636.                 break;
  637.             if (--cx == 0)
  638.                 return TIMEOUT;
  639.             udelay(PLIP_DELAY_UNIT);
  640.         }
  641.         data_addr--;
  642.         *ns_p = PLIP_NB_BEGIN;
  643.         return OK;
  644.     }
  645. }
  646.  
  647. /* PLIP_SEND_PACKET --- send a packet */
  648. static int
  649. plip_send_packet(struct device *dev, struct net_local *nl,
  650.          struct plip_local *snd, struct plip_local *rcv)
  651. {
  652.     unsigned short data_addr = PAR_DATA(dev);
  653.     unsigned short nibble_timeout = nl->nibble;
  654.     unsigned char *lbuf;
  655.     unsigned char c0;
  656.     unsigned int cx;
  657.  
  658.     if (snd->skb == NULL || (lbuf = snd->skb->data) == NULL) {
  659.         printk("%s: send skb lost\n", dev->name);
  660.         snd->state = PLIP_PK_DONE;
  661.         snd->skb = NULL;
  662.         return ERROR;
  663.     }
  664.  
  665.     switch (snd->state) {
  666.     case PLIP_PK_TRIGGER:
  667.         /* Trigger remote rx interrupt. */
  668.         outb(0x08, data_addr);
  669.         cx = nl->trigger;
  670.         while (1) {
  671.             udelay(PLIP_DELAY_UNIT);
  672.             cli();
  673.             if (nl->connection == PLIP_CN_RECEIVE) {
  674.                 sti();
  675.                 /* interrupted */
  676.                 nl->enet_stats.collisions++;
  677.                 if (net_debug > 1)
  678.                     printk("%s: collision.\n", dev->name);
  679.                 return OK;
  680.             }
  681.             c0 = inb(PAR_STATUS(dev));
  682.             if (c0 & 0x08) {
  683.                 disable_irq(dev->irq);
  684.                 outb(PAR_INTR_OFF, PAR_CONTROL(dev));
  685.                 if (net_debug > 2)
  686.                     printk("%s: send start\n", dev->name);
  687.                 snd->state = PLIP_PK_LENGTH_LSB;
  688.                 snd->nibble = PLIP_NB_BEGIN;
  689.                 nl->timeout_count = 0;
  690.                 sti();
  691.                 break;
  692.             }
  693.             sti();
  694.             if (--cx == 0) {
  695.                 outb(0x00, data_addr);
  696.                 return TIMEOUT;
  697.             }
  698.         }
  699.  
  700.     case PLIP_PK_LENGTH_LSB:
  701.         if (plip_send(nibble_timeout, data_addr,
  702.                   &snd->nibble, snd->length.b.lsb))
  703.             return TIMEOUT;
  704.         snd->state = PLIP_PK_LENGTH_MSB;
  705.  
  706.     case PLIP_PK_LENGTH_MSB:
  707.         if (plip_send(nibble_timeout, data_addr,
  708.                   &snd->nibble, snd->length.b.msb))
  709.             return TIMEOUT;
  710.         snd->state = PLIP_PK_DATA;
  711.         snd->byte = 0;
  712.         snd->checksum = 0;
  713.  
  714.     case PLIP_PK_DATA:
  715.         do
  716.             if (plip_send(nibble_timeout, data_addr,
  717.                       &snd->nibble, lbuf[snd->byte]))
  718.                 return TIMEOUT;
  719.         while (++snd->byte < snd->length.h);
  720.         do
  721.             snd->checksum += lbuf[--snd->byte];
  722.         while (snd->byte);
  723.         snd->state = PLIP_PK_CHECKSUM;
  724.  
  725.     case PLIP_PK_CHECKSUM:
  726.         if (plip_send(nibble_timeout, data_addr,
  727.                   &snd->nibble, snd->checksum))
  728.             return TIMEOUT;
  729.  
  730.         dev_kfree_skb(snd->skb, FREE_WRITE);
  731.         nl->enet_stats.tx_packets++;
  732.         snd->state = PLIP_PK_DONE;
  733.  
  734.     case PLIP_PK_DONE:
  735.         /* Close the connection */
  736.         outb (0x00, data_addr);
  737.         snd->skb = NULL;
  738.         if (net_debug > 2)
  739.             printk("%s: send end\n", dev->name);
  740.         nl->connection = PLIP_CN_CLOSING;
  741.         nl->is_deferred = 1;
  742.         queue_task(&nl->deferred, &tq_timer);
  743.         outb(PAR_INTR_ON, PAR_CONTROL(dev));
  744.         enable_irq(dev->irq);
  745.         return OK;
  746.     }
  747.     return OK;
  748. }
  749.  
  750. static int
  751. plip_connection_close(struct device *dev, struct net_local *nl,
  752.               struct plip_local *snd, struct plip_local *rcv)
  753. {
  754.     cli();
  755.     if (nl->connection == PLIP_CN_CLOSING) {
  756.         nl->connection = PLIP_CN_NONE;
  757.         dev->tbusy = 0;
  758.         mark_bh(NET_BH);
  759.     }
  760.     sti();
  761.     return OK;
  762. }
  763.  
  764. /* PLIP_ERROR --- wait till other end settled */
  765. static int
  766. plip_error(struct device *dev, struct net_local *nl,
  767.        struct plip_local *snd, struct plip_local *rcv)
  768. {
  769.     unsigned char status;
  770.  
  771.     status = inb(PAR_STATUS(dev));
  772.     if ((status & 0xf8) == 0x80) {
  773.         if (net_debug > 2)
  774.             printk("%s: reset interface.\n", dev->name);
  775.         nl->connection = PLIP_CN_NONE;
  776.         dev->tbusy = 0;
  777.         dev->interrupt = 0;
  778.         outb(PAR_INTR_ON, PAR_CONTROL(dev));
  779.         enable_irq(dev->irq);
  780.         mark_bh(NET_BH);
  781.     } else {
  782.         nl->is_deferred = 1;
  783.         queue_task(&nl->deferred, &tq_timer);
  784.     }
  785.  
  786.     return OK;
  787. }
  788.  
  789. /* Handle the parallel port interrupts. */
  790. static void
  791. plip_interrupt(int irq, struct pt_regs * regs)
  792. {
  793.     struct device *dev = (struct device *) irq2dev_map[irq];
  794.     struct net_local *nl = (struct net_local *)dev->priv;
  795.     struct plip_local *rcv = &nl->rcv_data;
  796.     unsigned char c0;
  797.  
  798.     if (dev == NULL) {
  799.         printk ("plip_interrupt: irq %d for unknown device.\n", irq);
  800.         return;
  801.     }
  802.  
  803.     if (dev->interrupt)
  804.         return;
  805.  
  806.     c0 = inb(PAR_STATUS(dev));
  807.     if ((c0 & 0xf8) != 0xc0) {
  808.         if (net_debug > 1)
  809.             printk("%s: spurious interrupt\n", dev->name);
  810.         return;
  811.     }
  812.     dev->interrupt = 1;
  813.     if (net_debug > 3)
  814.         printk("%s: interrupt.\n", dev->name);
  815.  
  816.     cli();
  817.     switch (nl->connection) {
  818.     case PLIP_CN_CLOSING:
  819.         dev->tbusy = 0;
  820.     case PLIP_CN_NONE:
  821.     case PLIP_CN_SEND:
  822.         dev->last_rx = jiffies;
  823.         rcv->state = PLIP_PK_TRIGGER;
  824.         nl->connection = PLIP_CN_RECEIVE;
  825.         nl->timeout_count = 0;
  826.         queue_task(&nl->immediate, &tq_immediate);
  827.         mark_bh(IMMEDIATE_BH);
  828.         sti();
  829.         break;
  830.  
  831.     case PLIP_CN_RECEIVE:
  832.         sti();
  833.         printk("%s: receive interrupt when receiving packet\n", dev->name);
  834.         break;
  835.  
  836.     case PLIP_CN_ERROR:
  837.         sti();
  838.         printk("%s: receive interrupt in error state\n", dev->name);
  839.         break;
  840.     }
  841. }
  842.  
  843. /* We don't need to send arp, for plip is point-to-point. */
  844. static int
  845. plip_rebuild_header(void *buff, struct device *dev, unsigned long dst,
  846.             struct sk_buff *skb)
  847. {
  848.     struct net_local *nl = (struct net_local *)dev->priv;
  849.     struct ethhdr *eth = (struct ethhdr *)buff;
  850.     int i;
  851.  
  852.     if ((dev->flags & IFF_NOARP)==0)
  853.         return nl->orig_rebuild_header(buff, dev, dst, skb);
  854.  
  855.     if (eth->h_proto != htons(ETH_P_IP)) {
  856.         printk("plip_rebuild_header: Don't know how to resolve type %d addresses?\n", (int)eth->h_proto);
  857.         memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
  858.         return 0;
  859.     }
  860.  
  861.     for (i=0; i < ETH_ALEN - sizeof(unsigned long); i++)
  862.         eth->h_dest[i] = 0xfc;
  863.     memcpy(&(eth->h_dest[i]), &dst, sizeof(unsigned long));
  864.     return 0;
  865. }
  866.  
  867. static int
  868. plip_tx_packet(struct sk_buff *skb, struct device *dev)
  869. {
  870.     struct net_local *nl = (struct net_local *)dev->priv;
  871.     struct plip_local *snd = &nl->snd_data;
  872.  
  873.     if (dev->tbusy)
  874.         return 1;
  875.  
  876.     /* If some higher layer thinks we've missed an tx-done interrupt
  877.        we are passed NULL. Caution: dev_tint() handles the cli()/sti()
  878.        itself. */
  879.     if (skb == NULL) {
  880.         dev_tint(dev);
  881.         return 0;
  882.     }
  883.  
  884.     if (set_bit(0, (void*)&dev->tbusy) != 0) {
  885.         printk("%s: Transmitter access conflict.\n", dev->name);
  886.         return 1;
  887.     }
  888.  
  889.     if (skb->len > dev->mtu) {
  890.         printk("%s: packet too big, %d.\n", dev->name, (int)skb->len);
  891.         dev->tbusy = 0;
  892.         return 0;
  893.     }
  894.  
  895.     if (net_debug > 2)
  896.         printk("%s: send request\n", dev->name);
  897.  
  898.     cli();
  899.     dev->trans_start = jiffies;
  900.     snd->skb = skb;
  901.     snd->length.h = skb->len;
  902.     snd->state = PLIP_PK_TRIGGER;
  903.     if (nl->connection == PLIP_CN_NONE) {
  904.         nl->connection = PLIP_CN_SEND;
  905.         nl->timeout_count = 0;
  906.     }
  907.     queue_task(&nl->immediate, &tq_immediate);
  908.     mark_bh(IMMEDIATE_BH);
  909.     sti();
  910.  
  911.     return 0;
  912. }
  913.  
  914. /* Open/initialize the board.  This is called (in the current kernel)
  915.    sometime after booting when the 'ifconfig' program is run.
  916.  
  917.    This routine gets exclusive access to the parallel port by allocating
  918.    its IRQ line.
  919.  */
  920. static int
  921. plip_open(struct device *dev)
  922. {
  923.     struct net_local *nl = (struct net_local *)dev->priv;
  924.     int i;
  925.  
  926.     if (dev->irq == 0) {
  927.         printk("%s: IRQ is not set.  Please set it by ifconfig.\n", dev->name);
  928.         return -EAGAIN;
  929.     }
  930.     cli();
  931.     if (request_irq(dev->irq , plip_interrupt, 0, dev->name) != 0) {
  932.         sti();
  933.         printk("%s: couldn't get IRQ %d.\n", dev->name, dev->irq);
  934.         return -EAGAIN;
  935.     }
  936.     irq2dev_map[dev->irq] = dev;
  937.     sti();
  938.  
  939.     /* Clear the data port. */
  940.     outb (0x00, PAR_DATA(dev));
  941.  
  942.     /* Enable rx interrupt. */
  943.     outb(PAR_INTR_ON, PAR_CONTROL(dev));
  944.  
  945.     /* Initialize the state machine. */
  946.     nl->rcv_data.state = nl->snd_data.state = PLIP_PK_DONE;
  947.     nl->rcv_data.skb = nl->snd_data.skb = NULL;
  948.     nl->connection = PLIP_CN_NONE;
  949.     nl->is_deferred = 0;
  950.  
  951.     /* Fill in the MAC-level header. */
  952.     for (i=0; i < ETH_ALEN - sizeof(unsigned long); i++)
  953.         dev->dev_addr[i] = 0xfc;
  954.     memcpy(&(dev->dev_addr[i]), &dev->pa_addr, sizeof(unsigned long));
  955.  
  956.     dev->interrupt = 0;
  957.     dev->start = 1;
  958.     dev->tbusy = 0;
  959.     MOD_INC_USE_COUNT;
  960.     return 0;
  961. }
  962.  
  963. /* The inverse routine to plip_open (). */
  964. static int
  965. plip_close(struct device *dev)
  966. {
  967.     struct net_local *nl = (struct net_local *)dev->priv;
  968.     struct plip_local *snd = &nl->snd_data;
  969.     struct plip_local *rcv = &nl->rcv_data;
  970.  
  971.     dev->tbusy = 1;
  972.     dev->start = 0;
  973.     cli();
  974.     free_irq(dev->irq);
  975.     irq2dev_map[dev->irq] = NULL;
  976.     nl->is_deferred = 0;
  977.     nl->connection = PLIP_CN_NONE;
  978.     sti();
  979.     outb(0x00, PAR_DATA(dev));
  980.  
  981.     snd->state = PLIP_PK_DONE;
  982.     if (snd->skb) {
  983.         dev_kfree_skb(snd->skb, FREE_WRITE);
  984.         snd->skb = NULL;
  985.     }
  986.     rcv->state = PLIP_PK_DONE;
  987.     if (rcv->skb) {
  988.         rcv->skb->free = 1;
  989.         kfree_skb(rcv->skb, FREE_READ);
  990.         rcv->skb = NULL;
  991.     }
  992.  
  993.     /* Reset. */
  994.     outb(0x00, PAR_CONTROL(dev));
  995.     MOD_DEC_USE_COUNT;
  996.     return 0;
  997. }
  998.  
  999. static struct enet_statistics *
  1000. plip_get_stats(struct device *dev)
  1001. {
  1002.     struct net_local *nl = (struct net_local *)dev->priv;
  1003.     struct enet_statistics *r = &nl->enet_stats;
  1004.  
  1005.     return r;
  1006. }
  1007.  
  1008. static int
  1009. plip_config(struct device *dev, struct ifmap *map)
  1010. {
  1011.     if (dev->flags & IFF_UP)
  1012.         return -EBUSY;
  1013.  
  1014.     if (map->base_addr != (unsigned long)-1
  1015.         && map->base_addr != dev->base_addr)
  1016.         printk("%s: You cannot change base_addr of this interface (ignored).\n", dev->name);
  1017.  
  1018.     if (map->irq != (unsigned char)-1)
  1019.         dev->irq = map->irq;
  1020.     return 0;
  1021. }
  1022.  
  1023. static int
  1024. plip_ioctl(struct device *dev, struct ifreq *rq, int cmd)
  1025. {
  1026.     struct net_local *nl = (struct net_local *) dev->priv;
  1027.     struct plipconf *pc = (struct plipconf *) &rq->ifr_data;
  1028.     
  1029.     switch(pc->pcmd) {
  1030.     case PLIP_GET_TIMEOUT:
  1031.         pc->trigger = nl->trigger;
  1032.         pc->nibble  = nl->nibble;
  1033.         break;
  1034.     case PLIP_SET_TIMEOUT:
  1035.         nl->trigger = pc->trigger;
  1036.         nl->nibble  = pc->nibble;
  1037.         break;
  1038.     default:
  1039.         return -EOPNOTSUPP;
  1040.     }
  1041.     return 0;
  1042. }
  1043.  
  1044. #ifdef MODULE
  1045. char kernel_version[] = UTS_RELEASE;
  1046.  
  1047. static struct device dev_plip0 = 
  1048. {
  1049.     "plip0" /*"plip"*/,
  1050.     0, 0, 0, 0,        /* memory */
  1051.     0x3BC, 5,        /* base, irq */
  1052.     0, 0, 0, NULL, plip_init 
  1053. };
  1054.  
  1055. static struct device dev_plip1 = 
  1056. {
  1057.     "plip1" /*"plip"*/,
  1058.     0, 0, 0, 0,        /* memory */
  1059.     0x378, 7,        /* base, irq */
  1060.     0, 0, 0, NULL, plip_init 
  1061. };
  1062.  
  1063. static struct device dev_plip2 = 
  1064. {
  1065.     "plip2" /*"plip"*/,
  1066.     0, 0, 0, 0,        /* memory */
  1067.     0x278, 2,        /* base, irq */
  1068.     0, 0, 0, NULL, plip_init 
  1069. };
  1070.  
  1071. int
  1072. init_module(void)
  1073. {
  1074.     int devices=0;
  1075.  
  1076.     if (register_netdev(&dev_plip0) != 0)
  1077.         devices++;
  1078.     if (register_netdev(&dev_plip1) != 0)
  1079.         devices++;
  1080.     if (register_netdev(&dev_plip2) != 0)
  1081.         devices++;
  1082.     if (devices == 0)
  1083.         return -EIO;
  1084.     return 0;
  1085. }
  1086.  
  1087. void
  1088. cleanup_module(void)
  1089. {
  1090.     if (dev_plip0.priv) {
  1091.         unregister_netdev(&dev_plip0);
  1092.         release_region(PAR_DATA(&dev_plip0), 3);
  1093.         kfree_s(dev_plip0.priv, sizeof(struct net_local));
  1094.         dev_plip0.priv = NULL;
  1095.     }
  1096.     if (dev_plip1.priv) {
  1097.         unregister_netdev(&dev_plip1);
  1098.         release_region(PAR_DATA(&dev_plip1), 3);
  1099.         kfree_s(dev_plip1.priv, sizeof(struct net_local));
  1100.         dev_plip1.priv = NULL;
  1101.     }
  1102.     if (dev_plip2.priv) {
  1103.         unregister_netdev(&dev_plip2);
  1104.         release_region(PAR_DATA(&dev_plip2), 3);
  1105.         kfree_s(dev_plip2.priv, sizeof(struct net_local));
  1106.         dev_plip2.priv = NULL;
  1107.     }
  1108. }
  1109. #endif /* MODULE */
  1110.  
  1111. /*
  1112.  * Local variables:
  1113.  * compile-command: "gcc -DMODULE -DCONFIG_MODVERSIONS -D__KERNEL__ -Wall -Wstrict-prototypes -O2 -g -fomit-frame-pointer -pipe -m486 -c plip.c"
  1114.  * End:
  1115.  */
  1116.