Annotation of Gnu-Mach/linux/src/drivers/scsi/ppa.c, revision 1.1.1.3

1.1       root        1: /* ppa.c   --  low level driver for the IOMEGA PPA3 
                      2:  * parallel port SCSI host adapter.
                      3:  * 
                      4:  * (The PPA3 is the embedded controller in the ZIP drive.)
                      5:  * 
                      6:  * (c) 1995,1996 Grant R. Guenther, [email protected],
                      7:  * under the terms of the GNU Public License.
                      8:  *
                      9:  * Current Maintainer: David Campbell (Perth, Western Australia)
                     10:  *                     [email protected]
                     11:  *                     [email protected]
                     12:  *
                     13:  * My unoffical company acronym list is 21 pages long:
                     14:  *      FLA:    Four letter acronym with built in facility for
                     15:  *              future expansion to five letters.
                     16:  */
                     17: 
                     18: #include <linux/config.h>
                     19: 
                     20: /* The following #define is to avoid a clash with hosts.c */
                     21: #define PPA_CODE 1
                     22: #ifndef HAVE_PC87332
                     23: #define HAVE_PC87332    0
                     24: #endif
                     25: #define PPA_PROBE_SPP   0x0001
                     26: #define PPA_PROBE_PS2   0x0002
                     27: #define PPA_PROBE_ECR   0x0010
                     28: #define PPA_PROBE_EPP17 0x0100
                     29: #define PPA_PROBE_EPP19 0x0200
                     30: int port_probe(unsigned short);
                     31: 
                     32: #include <linux/blk.h>
                     33: #include "sd.h"
                     34: #include "hosts.h"
                     35: typedef struct {
                     36:     int base;                  /* Actual port address          */
                     37:     int mode;                  /* Transfer mode                */
                     38:     int host;                  /* Host number (for proc)       */
                     39:     Scsi_Cmnd *cur_cmd;                /* Current queued command       */
                     40:     struct tq_struct ppa_tq;   /* Polling interupt stuff       */
                     41:     unsigned long jstart;      /* Jiffies at start             */
                     42:     unsigned failed:1;         /* Failure flag                 */
                     43: } ppa_struct;
                     44: 
                     45: #define PPA_EMPTY \
                     46: {-1,            /* base */      \
                     47: PPA_AUTODETECT, /* mode */      \
                     48: -1,             /* host */      \
                     49: NULL,           /* cur_cmd */   \
                     50: {0, 0, ppa_interrupt, NULL},    \
                     51: 0,              /* jstart */    \
                     52: 0               /* failed */    \
                     53: }
                     54: 
                     55: #include "ppa.h"
                     56: #undef CONFIG_PARPORT
                     57: #define NO_HOSTS 4
                     58: static ppa_struct ppa_hosts[NO_HOSTS] =
                     59: {PPA_EMPTY, PPA_EMPTY, PPA_EMPTY, PPA_EMPTY};
                     60: 
                     61: #define PPA_BASE(x)    ppa_hosts[(x)].base
                     62: 
                     63: int base[NO_HOSTS] =
                     64: {0x03bc, 0x0378, 0x0278, 0x0000};
                     65: #define parbus_base    base
                     66: #define parbus_no      NO_HOSTS
                     67: 
                     68: static inline int ppa_pb_claim(int host_no)
                     69: {
                     70:     if (ppa_hosts[host_no].cur_cmd)
                     71:        ppa_hosts[host_no].cur_cmd->SCp.phase++;
                     72:     return 0;
                     73: }
                     74: 
                     75: /***************************************************************************
                     76:  *                   Parallel port probing routines                        *
                     77:  ***************************************************************************/
                     78:   
                     79: #ifndef MODULE
                     80: /*
                     81:  * Command line parameters (for built-in driver):
                     82:  *
                     83:  * Syntax:  ppa=base[,mode[,use_sg]]
                     84:  *
                     85:  * For example:  ppa=0x378   or   ppa=0x378,0,3
                     86:  *
                     87:  */
                     88: 
                     89: void ppa_setup(char *str, int *ints)
                     90: {
                     91:     static int x = 0;
                     92: 
                     93:     if (x == 0) {              /* Disable ALL known ports */
                     94:        int i;
                     95: 
                     96:        for (i = 0; i < NO_HOSTS; i++)
                     97:            parbus_base[i] = 0x0000;
                     98:     }
                     99:     switch (ints[0]) {
                    100:     case 3:
                    101:        ppa_sg = ints[3];
                    102:     case 2:
                    103:        ppa_hosts[x].mode = ints[2];
                    104:        parbus_base[x] = ints[1];
                    105:        break;
                    106:     default:
                    107:        printk("PPA: I only use between 2 to 3 parameters.\n");
                    108:        break;
                    109:     }
                    110:     x++;
                    111:   }
                    112: #else
                    113: Scsi_Host_Template driver_template = PPA;
                    114: #include  "scsi_module.c"
                    115: #endif
                    116:   
                    117: /*
                    118:  * Start of Chipset kludges
                    119:  */
                    120: 
                    121: #if HAVE_PC87332 > 0
                    122: #warning PC87332 Kludge code included
                    123: static inline int pc87332_port(int host_no)
                    124: {
                    125:     /* A routine to detect and kludge pc87332 chipsets into the
                    126:      * "optimum" mode for parallel port data transfer.
                    127:      * This assumes EPP is better than ECP...
                    128:      * (Which it is for disk drives but not printers and scanners)
                    129:      */
                    130:     int base = ppa_hosts[host_no].base;
                    131: 
                    132:     /* This is where an pc87332 can hide */
                    133:     unsigned short index_addr[4] =
                    134:     {
                    135:        0x0398, 0x026e, 0x015c, 0x002e
                    136:     };
                    137: 
                    138:     /* Bits 0&1 of FAR (Function Address Register) which specify where
                    139:      * the LPT port will show up at.
                    140:      */
                    141:     unsigned short port_ref[4] =
                    142:     {
                    143:        0x378, 0x3bc, 0x278, 0xffff
                    144:     };
                    145: 
                    146:     unsigned char a;
                    147:     int loop;
                    148: 
                    149:     for (loop = 0; loop < 4; loop++) {
                    150:        /* Clear the "wax" out of the pc87332, only needed after hard
                    151:         * reset.
                    152:         */
                    153:        inb(index_addr[loop]);
                    154:        inb(index_addr[loop]);
                    155:        inb(index_addr[loop]);
                    156:        inb(index_addr[loop]);
                    157: 
                    158:        /* Anyone home ?? */
                    159:        outb(0xff, index_addr[loop]);
                    160:        a = inb(index_addr[loop]);
                    161:        switch (a) {
                    162:        case (0x0f):            /* PC87732 */
                    163:            break;
                    164:        case (0x1f):            /* PC87306 */
                    165:            break;
                    166:        case (0x7f):            /* PC87??? */
                    167:            break;
                    168:        default:
                    169:            continue;
                    170:        }                       /* Is this pc87332 on the desired port */
                    171:        outb(0x01, index_addr[loop]);
                    172:        a = inb(index_addr[loop] + 1);
                    173:        if (port_ref[a & 0x03] != base)
                    174:            continue;
                    175: 
                    176:        /* Found a pc87332 */
                    177:        printk("NatSemi PC87332 (or variant) at 0x%04x\n", base);
                    178: 
                    179:        /* Try to enable EPP modes
                    180:         * with hardware data direction
                    181:         */
                    182:        if (base != 0x3bc) {
                    183:            /* EPP 1.9 */
                    184:            outb(0x04, index_addr[loop]);
                    185:            a = inb(index_addr[loop] + 1);
                    186:            printk("Old reg1 = %02x\n", a);
                    187:            /* 0x01 for EPP 1.7, 0x03 for EPP 1.9, 0x0c for ECP */
                    188:            a = (a & 0xf0) | 0x03;
                    189:            outb(a, index_addr[loop] + 1);
                    190:            outb(a, index_addr[loop] + 1);
                    191: 
                    192:            /* Software data direction selection */
                    193:            outb(0x02, index_addr[loop]);
                    194:            a = inb(index_addr[loop] + 1);
                    195:            printk("Old reg2 = %02x\n", a);
                    196:            /* 0x80 for software, 0x00 for hardware */
                    197:            a = (a & 0x7f) | 0x80;
                    198:            outb(a, index_addr[loop] + 1);
                    199:            outb(a, index_addr[loop] + 1);
                    200:            ppa_hosts[host_no].mode = PPA_EPP_32;
                    201:        } else {
                    202:            /* There is not enough address space for the 0x3bc port
                    203:             * to have EPP registers so we will kludge it into an
                    204:             * ECP
                    205:             * port to allow bi-directional byte mode...
                    206:             */
                    207:            /* ECP */
                    208:            outb(0x04, index_addr[loop]);
                    209:            a = inb(index_addr[loop] + 1);
                    210:            a = (a & 0xfb) | 0x06;
                    211:            outb(a, index_addr[loop] + 1);
                    212:            outb(a, index_addr[loop] + 1);
                    213:            ppa_hosts[host_no].mode = PPA_PS2;
                    214:        }
                    215: 
                    216:        outb(0x04, index_addr[loop]);
                    217:        a = inb(index_addr[loop] + 1);
                    218:        return ppa_hosts[host_no].mode;
                    219:     }
                    220:     return 0;
                    221:   }
                    222: #else
                    223: #define pc87332_port(x)
                    224: #endif                         /* HAVE_PC87332 */
                    225:   
                    226: static inline int generic_port(int host_no)
                    227: {
                    228:     /* Generic parallel port detection
                    229:      * This will try to discover if the port is
                    230:      * EPP, ECP, PS/2 or NIBBLE (In that order, approx....)
                    231:      */
                    232:     unsigned int save_ctr, save_ecr, r;
                    233:     int ppb = PPA_BASE(host_no);
                    234: 
                    235:     save_ctr = r_ctr(ppb);
                    236:     save_ecr = r_ecr(ppb);
                    237:     r = port_probe(ppb);
                    238:     w_ecr(ppb, save_ecr);
                    239:     w_ctr(ppb, save_ctr);
                    240: 
                    241:     if (r & PPA_PROBE_SPP)
                    242:        ppa_hosts[host_no].mode = PPA_NIBBLE;
                    243: 
                    244:     if (r & PPA_PROBE_PS2) {
                    245:        ppa_hosts[host_no].mode = PPA_PS2;
                    246:        if (r & PPA_PROBE_ECR)
                    247:            w_ecr(ppb, 0x20);
                    248:     }
                    249:     if ((r & PPA_PROBE_EPP17) || (r & PPA_PROBE_EPP19)) {
                    250:        /* ppa_hosts[host_no].mode = PPA_EPP_32; */
                    251:        if (r & PPA_PROBE_ECR)
                    252:            w_ecr(ppb, 0x80);
                    253:     }
                    254:     return ppa_hosts[host_no].mode;
                    255: }
                    256: 
                    257: int ppa_detect(Scsi_Host_Template * host)
                    258: {
                    259:     struct Scsi_Host *hreg;
                    260:     int ports;
                    261:     int i, nhosts;
                    262: 
                    263:     printk("ppa: Version %s\n", PPA_VERSION);
                    264:     nhosts = 0;
                    265: 
                    266:     for (i = 0; i < parbus_no; i++) {
                    267:        if (parbus_base[i] == 0x0000)
                    268:            continue;
1.1.1.3 ! root      269:        ppa_hosts[i].base = parbus_base[i];
1.1       root      270: 
                    271:        /* sanity checks */
                    272:        if (check_region(parbus_base[i],
                    273:                         (parbus_base[i] == 0x03bc) ? 3 : 8))
                    274:            continue;
                    275: 
                    276:        pc87332_port(i);
                    277:        if (!generic_port(i))
                    278:            continue;
                    279: 
                    280:        if (ppa_init(i))
                    281:            continue;
                    282: 
                    283:        /* now the glue ... */
                    284:        switch (ppa_hosts[i].mode) {
                    285:        case PPA_NIBBLE:
                    286:        case PPA_PS2:
                    287:            ports = 3;
                    288:            break;
                    289:        case PPA_EPP_8:
                    290:        case PPA_EPP_16:
                    291:        case PPA_EPP_32:
                    292:            ports = 8;
                    293:            break;
                    294:        default:                /* Never gets here */
                    295:            continue;
                    296:        }
                    297:        request_region(ppa_hosts[i].base, ports, "ppa");
                    298:        host->can_queue = PPA_CAN_QUEUE;
                    299:        host->sg_tablesize = ppa_sg;
                    300:        hreg = scsi_register(host, 0);
                    301:        hreg->io_port = ppa_hosts[i].base;
                    302:        hreg->n_io_port = ports;
                    303:        hreg->dma_channel = -1;
                    304:        hreg->unique_id = i;
                    305:        ppa_hosts[i].host = hreg->host_no;
                    306:        nhosts++;
                    307:     }
                    308:     if (nhosts == 0)
                    309:        return 0;
                    310:     else
                    311:        return 1;               /* return number of hosts detected */
                    312: }
                    313: 
                    314: /* This is to give the ppa driver a way to modify the timings (and other
                    315:  * parameters) by writing to the /proc/scsi/ppa/0 file.
                    316:  * Very simple method really... (To simple, no error checking :( )
                    317:  * Reason: Kernel hackers HATE having to unload and reload modules for
                    318:  * testing...
                    319:  * Also gives a method to use a script to obtain optimum timings (TODO)
                    320:  */
                    321: 
                    322: static inline int ppa_strncmp(const char *a, const char *b, int len)
                    323: {
                    324:     int loop;
                    325:     for (loop = 0; loop < len; loop++)
                    326:        if (a[loop] != b[loop])
                    327:            return 1;
                    328: 
                    329:     return 0;
                    330: }
                    331: static inline int ppa_proc_write(int hostno, char *buffer, int length)
                    332: {
                    333:     unsigned long x;
                    334: 
                    335:     if ((length > 5) && (ppa_strncmp(buffer, "mode=", 5) == 0)) {
                    336:        x = simple_strtoul(buffer + 5, NULL, 0);
                    337:        ppa_hosts[hostno].mode = x;
                    338:        return length;
                    339:     }
                    340:     printk("ppa /proc: invalid variable\n");
                    341:     return (-EINVAL);
                    342: }
                    343: 
                    344: int ppa_proc_info(char *buffer, char **start, off_t offset,
                    345:                  int length, int hostno, int inout)
                    346: {
                    347:     int i;
                    348:     int len = 0;
                    349: 
                    350:     for (i = 0; i < 4; i++)
                    351:        if (ppa_hosts[i].host == hostno)
                    352:            break;
                    353: 
                    354:     if (inout)
                    355:        return ppa_proc_write(i, buffer, length);
                    356: 
                    357:     len += sprintf(buffer + len, "Version : %s\n", PPA_VERSION);
                    358:     len += sprintf(buffer + len, "Port    : 0x%04x\n", ppa_hosts[i].base);
                    359:     len += sprintf(buffer + len, "Mode    : %s\n", PPA_MODE_STRING[ppa_hosts[i].mode]);
                    360: 
                    361:     /* Request for beyond end of buffer */
                    362:     if (offset > len)
                    363:        return 0;
                    364: 
                    365:     *start = buffer + offset;
                    366:     len -= offset;
                    367:     if (len > length)
                    368:        len = length;
                    369:     return len;
                    370: }                              /* end of ppa.c */
                    371: static int device_check(int host_no);
                    372: 
                    373: #if PPA_DEBUG > 0
                    374: #define ppa_fail(x,y) printk("ppa: ppa_fail(%i) from %s at line %d\n",\
                    375:           y, __FUNCTION__, __LINE__); ppa_fail_func(x,y);
                    376: static inline void ppa_fail_func(int host_no, int error_code)
                    377: #else
                    378: static inline void ppa_fail(int host_no, int error_code)
                    379:   #endif
                    380: {
                    381:     /* If we fail a device then we trash status / message bytes */
                    382:     if (ppa_hosts[host_no].cur_cmd) {
                    383:        ppa_hosts[host_no].cur_cmd->result = error_code << 16;
                    384:        ppa_hosts[host_no].failed = 1;
                    385:     }
                    386: }
                    387: 
                    388: /*
                    389:  * Wait for the high bit to be set.
                    390:  * 
                    391:  * In principle, this could be tied to an interrupt, but the adapter
                    392:  * doesn't appear to be designed to support interrupts.  We spin on
                    393:  * the 0x80 ready bit. 
                    394:  */
                    395: static unsigned char ppa_wait(int host_no)
                    396: {
                    397:     int k;
                    398:     unsigned short ppb = PPA_BASE(host_no);
                    399:     unsigned char r;
                    400: 
                    401:     k = PPA_SPIN_TMO;
                    402:     do {
                    403:        r = r_str(ppb);
                    404:        k--;
                    405:        udelay(1);
                    406:     }
                    407:     while (!(r & 0x80) && (k));
                    408: 
                    409:     /*
                    410:      * return some status information.
                    411:      * Semantics: 0xc0 = ZIP wants more data
                    412:      *            0xd0 = ZIP wants to send more data
                    413:      *            0xe0 = ZIP is expecting SCSI command data
                    414:      *            0xf0 = end of transfer, ZIP is sending status
                    415:      */
                    416:     if (k)
                    417:        return (r & 0xf0);
                    418: 
                    419:     /* Counter expired - Time out occurred */
                    420:     ppa_fail(host_no, DID_TIME_OUT);
                    421:     printk("ppa timeout in ppa_wait\n");
                    422:     return 0;                  /* command timed out */
                    423: }
                    424: 
                    425: /*
                    426:  * output a string, in whatever mode is available, according to the
                    427:  * PPA protocol. 
                    428:  */
                    429: static inline void epp_reset(unsigned short ppb)
                    430: {
                    431:     int i;
                    432: 
                    433:     i = r_str(ppb);
                    434:     w_str(ppb, i);
                    435:     w_str(ppb, i & 0xfe);
                    436: }
                    437: 
                    438: static inline void ecp_sync(unsigned short ppb)
                    439: {
                    440:     int i;
                    441: 
                    442:     if ((r_ecr(ppb) & 0xe0) != 0x80)
                    443:        return;
                    444: 
                    445:     for (i = 0; i < 100; i++) {
                    446:        if (r_ecr(ppb) & 0x01)
                    447:            return;
                    448:        udelay(5);
                    449:     }
                    450:     printk("ppa: ECP sync failed as data still present in FIFO.\n");
                    451: }
                    452: 
                    453: /*
                    454:  * Here is the asm code for the SPP/PS2 protocols for the i386.
                    455:  * This has been optimised for speed on 386/486 machines. There will
                    456:  * be very little improvement on the current 586+ machines as it is the
                    457:  * IO statements which will limit throughput.
                    458:  */
                    459: #ifdef __i386__
                    460: #define BYTE_OUT(reg) \
                    461:        "       movb " #reg ",%%al\n" \
                    462:        "       outb %%al,(%%dx)\n" \
                    463:        "       addl $2,%%edx\n" \
                    464:        "       movb $0x0e,%%al\n" \
                    465:        "       outb %%al,(%%dx)\n" \
                    466:        "       movb $0x0c,%%al\n" \
                    467:        "       outb %%al,(%%dx)\n" \
                    468:        "       subl $2,%%edx\n"
                    469: 
                    470: static inline int ppa_byte_out(unsigned short base, char *buffer, unsigned int len)
                    471: {
1.1.1.2   root      472:     int i;
                    473: 
                    474:     for (i = len; i; i--) {
                    475:         w_dtr(base, *buffer++);
                    476:         w_ctr(base, 0xe);
                    477:         w_ctr(base, 0xc);
                    478:     }
1.1       root      479:     return 1;                  /* All went well - we hope! */
                    480: }
                    481: 
                    482: #define BYTE_IN(reg) \
                    483:        "       inb (%%dx),%%al\n" \
                    484:        "       movb %%al," #reg "\n" \
                    485:        "       addl $2,%%edx\n" \
                    486:        "       movb $0x27,%%al\n" \
                    487:        "       outb %%al,(%%dx)\n" \
                    488:        "       movb $0x25,%%al\n" \
                    489:        "       outb %%al,(%%dx)\n" \
                    490:        "       subl $2,%%edx\n"
                    491: 
                    492: static inline int ppa_byte_in(unsigned short base, char *buffer, int len)
                    493: {
1.1.1.2   root      494:     int i;
                    495: 
                    496:     for (i = len; i; i--) {
                    497:         *buffer++ = r_dtr(base);
                    498:         w_ctr(base, 0x27);
                    499:         w_ctr(base, 0x25);
                    500:     }
1.1       root      501:     return 1;                  /* All went well - we hope! */
                    502: }
                    503: 
                    504: #define NIBBLE_IN(reg) \
                    505:        "       incl %%edx\n" \
                    506:        "       movb $0x04,%%al\n" \
                    507:        "       outb %%al,(%%dx)\n" \
                    508:        "       decl %%edx\n" \
                    509:        "       inb (%%dx),%%al\n" \
                    510:        "       andb $0xf0,%%al\n" \
                    511:        "       movb %%al," #reg "\n" \
                    512:        "       incl %%edx\n" \
                    513:        "       movb $0x06,%%al\n" \
                    514:        "       outb %%al,(%%dx)\n" \
                    515:        "       decl %%edx\n" \
                    516:        "       inb (%%dx),%%al\n" \
                    517:        "       shrb $4,%%al\n" \
                    518:        "       orb %%al," #reg "\n"
                    519: 
1.1.1.3 ! root      520: static inline int ppa_nibble_in(unsigned short base, char *buffer, int len)
1.1       root      521: {
1.1.1.2   root      522:     for (; len; len--) {
                    523:         unsigned char h;
                    524: 
                    525:         w_ctr(base, 0x4);
                    526:         h = r_str(base) & 0xf0;
                    527:         w_ctr(base, 0x6);
                    528:         *buffer++ = h | ((r_str(base) & 0xf0) >> 4);
                    529:     }
1.1       root      530:     return 1;                  /* All went well - we hope! */
                    531: }
                    532: #else                          /* Old style C routines */
                    533: 
                    534: static inline int ppa_byte_out(unsigned short base, const char *buffer, int len)
                    535: {
                    536:     unsigned short ctr_p = base + 2;
                    537:     int i;
                    538: 
                    539:     for (i = len; i; i--) {
                    540:        outb(*buffer++, base);
                    541:        outb(0xe, ctr_p);
                    542:        outb(0xc, ctr_p);
                    543:     }
                    544:     return 1;                  /* All went well - we hope! */
                    545: }
                    546: 
                    547: static inline int ppa_byte_in(unsigned short base, char *buffer, int len)
                    548: {
                    549:     unsigned short ctr_p = base + 2;
                    550:     int i;
                    551: 
                    552:     for (i = len; i; i--) {
                    553:        *buffer++ = inb(base);
                    554:        outb(0x27, ctr_p);
                    555:        outb(0x25, ctr_p);
                    556:     }
                    557:     return 1;                  /* All went well - we hope! */
                    558: }
                    559: 
                    560: static inline int ppa_nibble_in(unsigned short str_p, char *buffer, int len)
                    561: {
                    562:     unsigned short ctr_p = str_p + 1;
                    563:     unsigned char h, l;
                    564:     int i;
                    565: 
                    566:     for (i = len; i; i--) {
                    567:        outb(0x4, ctr_p);
                    568:        h = inb(str_p);
                    569:        outb(0x6, ctr_p);
                    570:        l = inb(str_p);
                    571:        *buffer++ = (h & 0xf0) | ((l & 0xf0) >> 4);
                    572:     }
                    573:     return 1;                  /* All went well - we hope! */
                    574:   }
                    575:   #endif
                    576:   
                    577: static inline int ppa_epp_out(unsigned short epp_p, unsigned short str_p, const char *buffer, int len)
                    578: {
                    579:     int i;
                    580:     for (i = len; i; i--) {
                    581:        outb(*buffer++, epp_p);
                    582: #ifdef CONFIG_SCSI_PPA_HAVE_PEDANTIC
                    583:        if (inb(str_p) & 0x01)
                    584:            return 0;
                    585:   #endif
                    586:     }
                    587:     return 1;
                    588:   }
                    589:   
                    590: static int ppa_out(int host_no, char *buffer, int len)
                    591: {
                    592:     int r;
                    593:     unsigned short ppb = PPA_BASE(host_no);
                    594: 
                    595:     r = ppa_wait(host_no);
                    596: 
                    597:     if ((r & 0x50) != 0x40) {
                    598:        ppa_fail(host_no, DID_ERROR);
                    599:        return 0;
                    600:     }
                    601:     switch (ppa_hosts[host_no].mode) {
                    602:     case PPA_NIBBLE:
                    603:     case PPA_PS2:
                    604:        /* 8 bit output, with a loop */
                    605:        r = ppa_byte_out(ppb, buffer, len);
                    606:        break;
                    607: 
                    608:     case PPA_EPP_32:
                    609:     case PPA_EPP_16:
                    610:     case PPA_EPP_8:
                    611:        epp_reset(ppb);
                    612:        w_ctr(ppb, 0x4);
                    613: #ifdef CONFIG_SCSI_PPA_HAVE_PEDANTIC
                    614:        r = ppa_epp_out(ppb + 4, ppb + 1, buffer, len);
                    615: #else
                    616:        if (!(((long) buffer | len) & 0x03))
                    617:            outsl(ppb + 4, buffer, len >> 2);
                    618:        else
                    619:            outsb(ppb + 4, buffer, len);
                    620:        w_ctr(ppb, 0xc);
                    621:        r = !(r_str(ppb) & 0x01);
                    622: #endif
                    623:        w_ctr(ppb, 0xc);
                    624:        ecp_sync(ppb);
                    625:        break;
                    626: 
                    627:     default:
                    628:        printk("PPA: bug in ppa_out()\n");
                    629:        r = 0;
                    630:     }
                    631:     return r;
                    632: }
                    633: 
                    634: static inline int ppa_epp_in(int epp_p, int str_p, char *buffer, int len)
                    635: {
                    636:     int i;
                    637:     for (i = len; i; i--) {
                    638:        *buffer++ = inb(epp_p);
                    639: #ifdef CONFIG_SCSI_PPA_HAVE_PEDANTIC
                    640:        if (inb(str_p) & 0x01)
                    641:            return 0;
                    642: #endif
                    643:     }
                    644:     return 1;
                    645:   }
                    646:   
                    647: static int ppa_in(int host_no, char *buffer, int len)
                    648: {
                    649:     int r;
                    650:     unsigned short ppb = PPA_BASE(host_no);
                    651: 
                    652:     r = ppa_wait(host_no);
                    653: 
                    654:     if ((r & 0x50) != 0x50) {
                    655:        ppa_fail(host_no, DID_ERROR);
                    656:        return 0;
                    657:     }
                    658:     switch (ppa_hosts[host_no].mode) {
                    659:     case PPA_NIBBLE:
                    660:        /* 4 bit input, with a loop */
                    661:        r = ppa_nibble_in(ppb + 1, buffer, len);
                    662:        w_ctr(ppb, 0xc);
                    663:        break;
                    664: 
                    665:     case PPA_PS2:
                    666:        /* 8 bit input, with a loop */
                    667:        w_ctr(ppb, 0x25);
                    668:        r = ppa_byte_in(ppb, buffer, len);
                    669:        w_ctr(ppb, 0x4);
                    670:        w_ctr(ppb, 0xc);
                    671:        break;
                    672: 
                    673:     case PPA_EPP_32:
                    674:     case PPA_EPP_16:
                    675:     case PPA_EPP_8:
                    676:        epp_reset(ppb);
                    677:        w_ctr(ppb, 0x24);
                    678: #ifdef CONFIG_SCSI_PPA_HAVE_PEDANTIC
                    679:        r = ppa_epp_in(ppb + 4, ppb + 1, buffer, len);
                    680:   #else
                    681:        if (!(((long) buffer | len) & 0x03))
                    682:            insl(ppb + 4, buffer, len >> 2);
                    683:        else
                    684:            insb(ppb + 4, buffer, len);
                    685:        w_ctr(ppb, 0x2c);
                    686:        r = !(r_str(ppb) & 0x01);
                    687: #endif
                    688:        w_ctr(ppb, 0x2c);
                    689:        ecp_sync(ppb);
                    690:        break;
                    691: 
                    692:     default:
                    693:        printk("PPA: bug in ppa_ins()\n");
                    694:        r = 0;
                    695:        break;
                    696:     }
                    697:     return r;
                    698: }
                    699: 
                    700: /* end of ppa_io.h */
                    701: static inline void ppa_d_pulse(unsigned short ppb, unsigned char b)
                    702: {
                    703:     w_dtr(ppb, b);
                    704:     w_ctr(ppb, 0xc);
                    705:     w_ctr(ppb, 0xe);
                    706:     w_ctr(ppb, 0xc);
                    707:     w_ctr(ppb, 0x4);
                    708:     w_ctr(ppb, 0xc);
                    709: }
                    710: 
                    711: static void ppa_disconnect(int host_no)
                    712: {
                    713:     unsigned short ppb = PPA_BASE(host_no);
                    714: 
                    715:     ppa_d_pulse(ppb, 0);
                    716:     ppa_d_pulse(ppb, 0x3c);
                    717:     ppa_d_pulse(ppb, 0x20);
                    718:     ppa_d_pulse(ppb, 0xf);
                    719: }
                    720: 
                    721: static inline void ppa_c_pulse(unsigned short ppb, unsigned char b)
                    722: {
                    723:     w_dtr(ppb, b);
                    724:     w_ctr(ppb, 0x4);
                    725:     w_ctr(ppb, 0x6);
                    726:     w_ctr(ppb, 0x4);
                    727:     w_ctr(ppb, 0xc);
                    728: }
                    729: 
                    730: static inline void ppa_connect(int host_no, int flag)
                    731: {
                    732:     unsigned short ppb = PPA_BASE(host_no);
                    733: 
                    734:     ppa_c_pulse(ppb, 0);
                    735:     ppa_c_pulse(ppb, 0x3c);
                    736:     ppa_c_pulse(ppb, 0x20);
                    737:     if ((flag == CONNECT_EPP_MAYBE) &&
                    738:        IN_EPP_MODE(ppa_hosts[host_no].mode))
                    739:        ppa_c_pulse(ppb, 0xcf);
                    740:     else
                    741:        ppa_c_pulse(ppb, 0x8f);
                    742: }
                    743: 
                    744: static int ppa_select(int host_no, int target)
                    745: {
                    746:     int k;
                    747:     unsigned short ppb = PPA_BASE(host_no);
                    748: 
                    749:     /*
                    750:      * Bit 6 (0x40) is the device selected bit.
                    751:      * First we must wait till the current device goes off line...
                    752:      */
                    753:     k = PPA_SELECT_TMO;
                    754:     do {
                    755:        k--;
                    756:     } while ((r_str(ppb) & 0x40) && (k));
                    757:     if (!k)
                    758:        return 0;
                    759: 
                    760:     w_dtr(ppb, (1 << target));
                    761:     w_ctr(ppb, 0xe);
                    762:     w_ctr(ppb, 0xc);
                    763:     w_dtr(ppb, 0x80);          /* This is NOT the initator */
                    764:     w_ctr(ppb, 0x8);
                    765: 
                    766:     k = PPA_SELECT_TMO;
                    767:     do {
                    768:        k--;
                    769:     }
                    770:     while (!(r_str(ppb) & 0x40) && (k));
                    771:     if (!k)
                    772:        return 0;
                    773: 
                    774:     return 1;
                    775: }
                    776: 
                    777: /* 
                    778:  * This is based on a trace of what the Iomega DOS 'guest' driver does.
                    779:  * I've tried several different kinds of parallel ports with guest and
                    780:  * coded this to react in the same ways that it does.
                    781:  * 
                    782:  * The return value from this function is just a hint about where the
                    783:  * handshaking failed.
                    784:  * 
                    785:  */
                    786: static int ppa_init(int host_no)
                    787: {
                    788:     int retv;
                    789:     unsigned short ppb = PPA_BASE(host_no);
                    790: 
                    791:     ppa_disconnect(host_no);
                    792:     ppa_connect(host_no, CONNECT_NORMAL);
                    793: 
                    794:     retv = 2;                  /* Failed */
                    795: 
                    796:     w_ctr(ppb, 0xe);
                    797:     if ((r_str(ppb) & 0x08) == 0x08)
                    798:        retv--;
                    799: 
                    800:     w_ctr(ppb, 0xc);
                    801:     if ((r_str(ppb) & 0x08) == 0x00)
                    802:        retv--;
                    803: 
                    804:     /* This is a SCSI BUS reset signal */
                    805:     if (!retv) {
                    806:        w_dtr(ppb, 0x40);
                    807:        w_ctr(ppb, 0x08);
                    808:        udelay(30);
                    809:        w_ctr(ppb, 0x0c);
                    810:        udelay(1000);           /* Allow devices to settle down */
                    811:     }
                    812:     ppa_disconnect(host_no);
                    813:     udelay(1000);              /* Another delay to allow devices to settle */
                    814: 
                    815:     if (!retv)
                    816:        retv = device_check(host_no);
                    817: 
                    818:     return retv;
                    819: }
                    820: 
                    821: static inline int ppa_send_command(Scsi_Cmnd * cmd)
                    822: {
                    823:     int host_no = cmd->host->unique_id;
                    824:     int k;
                    825: 
                    826:     w_ctr(PPA_BASE(host_no), 0x0c);
                    827: 
                    828:     for (k = 0; k < cmd->cmd_len; k++)
                    829:        if (!ppa_out(host_no, &cmd->cmnd[k], 1))
                    830:            return 0;
                    831:     return 1;
                    832: }
                    833: 
                    834: /*
                    835:  * The bulk flag enables some optimisations in the data transfer loops,
                    836:  * it should be true for any command that transfers data in integral
                    837:  * numbers of sectors.
                    838:  * 
                    839:  * The driver appears to remain stable if we speed up the parallel port
                    840:  * i/o in this function, but not elsewhere.
                    841:  */
                    842: static int ppa_completion(Scsi_Cmnd * cmd)
                    843: {
                    844:     /* Return codes:
                    845:      * -1     Error
                    846:      *  0     Told to schedule
                    847:      *  1     Finished data transfer
                    848:      */
                    849:     int host_no = cmd->host->unique_id;
                    850:     unsigned short ppb = PPA_BASE(host_no);
                    851:     unsigned long start_jiffies = jiffies;
                    852: 
                    853:     unsigned char r, v;
                    854:     int fast, bulk, status;
                    855: 
                    856:     v = cmd->cmnd[0];
                    857:     bulk = ((v == READ_6) ||
                    858:            (v == READ_10) ||
                    859:            (v == WRITE_6) ||
                    860:            (v == WRITE_10));
                    861: 
                    862:     /*
                    863:      * We only get here if the drive is ready to comunicate,
                    864:      * hence no need for a full ppa_wait.
                    865:      */
                    866:     r = (r_str(ppb) & 0xf0);
                    867: 
                    868:     while (r != (unsigned char) 0xf0) {
                    869:        /*
                    870:         * If we have been running for more than a full timer tick
                    871:         * then take a rest.
                    872:         */
                    873:        if (jiffies > start_jiffies + 1)
                    874:            return 0;
                    875: 
                    876:        if (((r & 0xc0) != 0xc0) || (cmd->SCp.this_residual <= 0)) {
                    877:            ppa_fail(host_no, DID_ERROR);
                    878:            return -1;          /* ERROR_RETURN */
                    879:        }
                    880:        /* determine if we should use burst I/O */ fast = (bulk && (cmd->SCp.this_residual >= PPA_BURST_SIZE))
                    881:            ? PPA_BURST_SIZE : 1;
                    882: 
                    883:        if (r == (unsigned char) 0xc0)
                    884:            status = ppa_out(host_no, cmd->SCp.ptr, fast);
                    885:        else
                    886:            status = ppa_in(host_no, cmd->SCp.ptr, fast);
                    887: 
                    888:        cmd->SCp.ptr += fast;
                    889:        cmd->SCp.this_residual -= fast;
                    890: 
                    891:        if (!status) {
                    892:            ppa_fail(host_no, DID_BUS_BUSY);
                    893:            return -1;          /* ERROR_RETURN */
                    894:        }
                    895:        if (cmd->SCp.buffer && !cmd->SCp.this_residual) {
                    896:            /* if scatter/gather, advance to the next segment */
                    897:            if (cmd->SCp.buffers_residual--) {
                    898:                cmd->SCp.buffer++;
                    899:                cmd->SCp.this_residual = cmd->SCp.buffer->length;
                    900:                cmd->SCp.ptr = cmd->SCp.buffer->address;
                    901:            }
                    902:        }
                    903:        /* Now check to see if the drive is ready to comunicate */
                    904:        r = (r_str(ppb) & 0xf0);
                    905:        /* If not, drop back down to the scheduler and wait a timer tick */
                    906:        if (!(r & 0x80))
                    907:            return 0;
                    908:     }
                    909:     return 1;                  /* FINISH_RETURN */
                    910: }
                    911: 
                    912: /*
                    913:  * Since the PPA itself doesn't generate interrupts, we use
                    914:  * the scheduler's task queue to generate a stream of call-backs and
                    915:  * complete the request when the drive is ready.
                    916:  */
                    917: static void ppa_interrupt(void *data)
                    918: {
                    919:     ppa_struct *tmp = (ppa_struct *) data;
                    920:     Scsi_Cmnd *cmd = tmp->cur_cmd;
                    921: 
                    922:     if (!cmd) {
                    923:        printk("PPA: bug in ppa_interrupt\n");
                    924:        return;
                    925:     }
                    926:     if (ppa_engine(tmp, cmd)) {
                    927:        tmp->ppa_tq.data = (void *) tmp;
                    928:        tmp->ppa_tq.sync = 0;
                    929:        queue_task(&tmp->ppa_tq, &tq_timer);
                    930:        return;
                    931:     }
                    932:     /* Command must of completed hence it is safe to let go... */
                    933: #if PPA_DEBUG > 0
                    934:     switch ((cmd->result >> 16) & 0xff) {
                    935:     case DID_OK:
                    936:        break;
                    937:     case DID_NO_CONNECT:
                    938:        printk("ppa: no device at SCSI ID %i\n", cmd->target);
                    939:        break;
                    940:     case DID_BUS_BUSY:
                    941:        printk("ppa: BUS BUSY - EPP timeout detected\n");
                    942:        break;
                    943:     case DID_TIME_OUT:
                    944:        printk("ppa: unknown timeout\n");
                    945:        break;
                    946:     case DID_ABORT:
                    947:        printk("ppa: told to abort\n");
                    948:        break;
                    949:     case DID_PARITY:
                    950:        printk("ppa: parity error (???)\n");
                    951:        break;
                    952:     case DID_ERROR:
                    953:        printk("ppa: internal driver error\n");
                    954:        break;
                    955:     case DID_RESET:
                    956:        printk("ppa: told to reset device\n");
                    957:        break;
                    958:     case DID_BAD_INTR:
                    959:        printk("ppa: bad interrupt (???)\n");
                    960:        break;
                    961:     default:
                    962:        printk("ppa: bad return code (%02x)\n", (cmd->result >> 16) & 0xff);
                    963:     }
                    964:   #endif
                    965:   
                    966:     if (cmd->SCp.phase > 1)
                    967:        ppa_disconnect(cmd->host->unique_id);
                    968: 
                    969:     tmp->cur_cmd = 0;
                    970:     cmd->scsi_done(cmd);
                    971:     return;
                    972: }
                    973: 
                    974: static int ppa_engine(ppa_struct * tmp, Scsi_Cmnd * cmd)
                    975: {
                    976:     int host_no = cmd->host->unique_id;
                    977:     unsigned short ppb = PPA_BASE(host_no);
                    978:     unsigned char l = 0, h = 0;
                    979:     int retv;
                    980: 
                    981:     /* First check for any errors that may have occurred
                    982:      * Here we check for internal errors
                    983:      */
                    984:     if (tmp->failed)
                    985:        return 0;
                    986: 
                    987:     switch (cmd->SCp.phase) {
                    988:     case 0:                    /* Phase 0 - Waiting for parport */
                    989:        if ((jiffies - tmp->jstart) > HZ) {
                    990:            /*
                    991:             * We waited more than a second
                    992:             * for parport to call us
                    993:             */
                    994:            ppa_fail(host_no, DID_BUS_BUSY);
                    995:            return 0;
                    996:        }
                    997:        return 1;               /* wait until ppa_wakeup claims parport */
                    998:     case 1:                    /* Phase 1 - Connected */
                    999:        {                       /* Perform a sanity check for cable unplugged */
                   1000:            int retv = 2;       /* Failed */
                   1001: 
                   1002:            ppa_connect(host_no, CONNECT_EPP_MAYBE);
                   1003: 
                   1004:            w_ctr(ppb, 0xe);
                   1005:            if ((r_str(ppb) & 0x08) == 0x08)
                   1006:                retv--;
                   1007: 
                   1008:            w_ctr(ppb, 0xc);
                   1009:            if ((r_str(ppb) & 0x08) == 0x00)
                   1010:                retv--;
                   1011: 
                   1012:            if (retv)
                   1013:                if ((jiffies - tmp->jstart) > (1 * HZ)) {
                   1014:                    printk("ppa: Parallel port cable is unplugged!!\n");
                   1015:                    ppa_fail(host_no, DID_BUS_BUSY);
                   1016:                    return 0;
                   1017:                } else {
                   1018:                    ppa_disconnect(host_no);
                   1019:                    return 1;   /* Try again in a jiffy */
                   1020:                }
                   1021:            cmd->SCp.phase++;
                   1022:        }
                   1023: 
                   1024:     case 2:                    /* Phase 2 - We are now talking to the scsi bus */
                   1025:        if (!ppa_select(host_no, cmd->target)) {
                   1026:            ppa_fail(host_no, DID_NO_CONNECT);
                   1027:            return 0;
                   1028:        }
                   1029:        cmd->SCp.phase++;
                   1030: 
                   1031:     case 3:                    /* Phase 3 - Ready to accept a command */
                   1032:        w_ctr(ppb, 0x0c);
                   1033:        if (!(r_str(ppb) & 0x80))
                   1034:            return 1;
                   1035: 
                   1036:        if (!ppa_send_command(cmd))
                   1037:            return 0;
                   1038:        cmd->SCp.phase++;
                   1039: 
                   1040:     case 4:                    /* Phase 4 - Setup scatter/gather buffers */
                   1041:        if (cmd->use_sg) {
                   1042:            /* if many buffers are available, start filling the first */
                   1043:            cmd->SCp.buffer = (struct scatterlist *) cmd->request_buffer;
                   1044:            cmd->SCp.this_residual = cmd->SCp.buffer->length;
                   1045:            cmd->SCp.ptr = cmd->SCp.buffer->address;
                   1046:        } else {
                   1047:            /* else fill the only available buffer */
                   1048:            cmd->SCp.buffer = NULL;
                   1049:            cmd->SCp.this_residual = cmd->request_bufflen;
                   1050:            cmd->SCp.ptr = cmd->request_buffer;
                   1051:        }
                   1052:        cmd->SCp.buffers_residual = cmd->use_sg;
                   1053:        cmd->SCp.phase++;
                   1054: 
                   1055:     case 5:                    /* Phase 5 - Data transfer stage */
                   1056:        w_ctr(ppb, 0x0c);
                   1057:        if (!(r_str(ppb) & 0x80))
                   1058:            return 1;
                   1059: 
                   1060:        retv = ppa_completion(cmd);
                   1061:        if (retv == -1)
                   1062:            return 0;
                   1063:        if (retv == 0)
                   1064:            return 1;
                   1065:        cmd->SCp.phase++;
                   1066: 
                   1067:     case 6:                    /* Phase 6 - Read status/message */
                   1068:        cmd->result = DID_OK << 16;
                   1069:        /* Check for data overrun */
                   1070:        if (ppa_wait(host_no) != (unsigned char) 0xf0) {
                   1071:            ppa_fail(host_no, DID_ERROR);
                   1072:            return 0;
                   1073:        }
                   1074:        if (ppa_in(host_no, &l, 1)) {   /* read status byte */
                   1075:            /* Check for optional message byte */
                   1076:            if (ppa_wait(host_no) == (unsigned char) 0xf0)
                   1077:                ppa_in(host_no, &h, 1);
                   1078:            cmd->result = (DID_OK << 16) + (h << 8) + (l & STATUS_MASK);
                   1079:        }
                   1080:        return 0;               /* Finished */
                   1081:        break;
                   1082: 
                   1083:     default:
                   1084:        printk("ppa: Invalid scsi phase\n");
                   1085:     }
                   1086:     return 0;
                   1087: }
                   1088: 
                   1089: int ppa_queuecommand(Scsi_Cmnd * cmd, void (*done) (Scsi_Cmnd *))
                   1090: {
                   1091:     int host_no = cmd->host->unique_id;
                   1092: 
                   1093:     if (ppa_hosts[host_no].cur_cmd) {
                   1094:        printk("PPA: bug in ppa_queuecommand\n");
                   1095:        return 0;
                   1096:     }
                   1097:     ppa_hosts[host_no].failed = 0;
                   1098:     ppa_hosts[host_no].jstart = jiffies;
                   1099:     ppa_hosts[host_no].cur_cmd = cmd;
                   1100:     cmd->scsi_done = done;
                   1101:     cmd->result = DID_ERROR << 16;     /* default return code */
                   1102:     cmd->SCp.phase = 0;                /* bus free */
                   1103: 
                   1104:     ppa_pb_claim(host_no);
                   1105: 
                   1106:     ppa_hosts[host_no].ppa_tq.data = ppa_hosts + host_no;
                   1107:     ppa_hosts[host_no].ppa_tq.sync = 0;
                   1108:     queue_task(&ppa_hosts[host_no].ppa_tq, &tq_immediate);
                   1109:     mark_bh(IMMEDIATE_BH);
                   1110: 
                   1111:     return 0;
                   1112: }
                   1113: 
                   1114: /*
                   1115:  * Apparently the disk->capacity attribute is off by 1 sector 
                   1116:  * for all disk drives.  We add the one here, but it should really
                   1117:  * be done in sd.c.  Even if it gets fixed there, this will still
                   1118:  * work.
                   1119:  */
                   1120: int ppa_biosparam(Disk * disk, kdev_t dev, int ip[])
                   1121: {
                   1122:     ip[0] = 0x40;
                   1123:     ip[1] = 0x20;
                   1124:     ip[2] = (disk->capacity + 1) / (ip[0] * ip[1]);
                   1125:     if (ip[2] > 1024) {
                   1126:        ip[0] = 0xff;
                   1127:        ip[1] = 0x3f;
                   1128:        ip[2] = (disk->capacity + 1) / (ip[0] * ip[1]);
                   1129:        if (ip[2] > 1023)
                   1130:            ip[2] = 1023;
                   1131:     }
                   1132:     return 0;
                   1133: }
                   1134: 
                   1135: int ppa_abort(Scsi_Cmnd * cmd)
                   1136: {
                   1137:     /*
                   1138:      * There is no method for aborting commands since Iomega
                   1139:      * have tied the SCSI_MESSAGE line high in the interface
                   1140:      */
                   1141: 
                   1142:     switch (cmd->SCp.phase) {
                   1143:     case 0:                    /* Do not have access to parport */
                   1144:     case 1:                    /* Have not connected to interface */
                   1145:        cmd->result = DID_ABORT;
                   1146:        cmd->done(cmd);
                   1147:        return SCSI_ABORT_SUCCESS;
                   1148:        break;
                   1149:     default:                   /* SCSI command sent, can not abort */
                   1150:        return SCSI_ABORT_BUSY;
                   1151:        break;
                   1152:     }
                   1153: }
                   1154: 
                   1155: int ppa_reset(Scsi_Cmnd * cmd, unsigned int x)
                   1156: {
                   1157:     int host_no = cmd->host->unique_id;
                   1158:     int ppb = PPA_BASE(host_no);
                   1159: 
                   1160:     /*
                   1161:      * PHASE1:
                   1162:      * Bring the interface crashing down on whatever is running
                   1163:      * hopefully this will kill the request.
                   1164:      * Bring back up the interface, reset the drive (and anything
                   1165:      * attached for that manner)
                   1166:      */
                   1167:     if (cmd)
                   1168:        if (cmd->SCp.phase)
                   1169:            ppa_disconnect(cmd->host->unique_id);
                   1170: 
                   1171:     ppa_connect(host_no, CONNECT_NORMAL);
                   1172:     w_dtr(ppb, 0x40);
                   1173:     w_ctr(ppb, 0x8);
                   1174:     udelay(30);
                   1175:     w_ctr(ppb, 0xc);
                   1176:     udelay(1000);              /* delay for devices to settle down */
                   1177:     ppa_disconnect(host_no);
                   1178:     udelay(1000);              /* Additional delay to allow devices to settle down */
                   1179: 
                   1180:     /*
                   1181:      * PHASE2:
                   1182:      * Sanity check for the sake of mid-level driver
                   1183:      */
                   1184:     if (!cmd) {
                   1185:        printk("ppa bus reset called for invalid command.\n");
                   1186:        return SCSI_RESET_NOT_RUNNING;
                   1187:     }
                   1188:     /*
                   1189:      * PHASE3:
                   1190:      * Flag the current command as having died due to reset
                   1191:      */
                   1192:     ppa_connect(host_no, CONNECT_NORMAL);
                   1193:     ppa_fail(host_no, DID_RESET);
                   1194: 
                   1195:     /* Since the command was already on the timer queue ppa_interrupt
                   1196:      * will be called shortly.
                   1197:      */
                   1198:     return SCSI_RESET_PENDING;
                   1199: }
                   1200: 
                   1201: static int device_check(int host_no)
                   1202: {
                   1203:     /* This routine looks for a device and then attempts to use EPP
                   1204:        to send a command. If all goes as planned then EPP is available. */
                   1205: 
                   1206:     static char cmd[6] =
                   1207:     {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
                   1208:     int loop, old_mode, status, k, ppb = PPA_BASE(host_no);
                   1209:     unsigned char l;
                   1210: 
                   1211:     old_mode = ppa_hosts[host_no].mode;
                   1212:     for (loop = 0; loop < 8; loop++) {
                   1213:        /* Attempt to use EPP for Test Unit Ready */
                   1214:        if ((ppb & 0x0007) == 0x0000)
                   1215:            ppa_hosts[host_no].mode = PPA_EPP_32;
                   1216: 
                   1217:       second_pass:
                   1218:        ppa_connect(host_no, CONNECT_EPP_MAYBE);
                   1219:        /* Select SCSI device */
                   1220:        if (!ppa_select(host_no, loop)) {
                   1221:            ppa_disconnect(host_no);
                   1222:            continue;
                   1223:        }
                   1224:        printk("ppa: Found device at ID %i, Attempting to use %s\n", loop,
                   1225:               PPA_MODE_STRING[ppa_hosts[host_no].mode]);
                   1226: 
                   1227:        /* Send SCSI command */
                   1228:        status = 1;
                   1229:        w_ctr(ppb, 0x0c);
                   1230:        for (l = 0; (l < 6) && (status); l++)
                   1231:            status = ppa_out(host_no, cmd, 1);
                   1232: 
                   1233:        if (!status) {
                   1234:            ppa_disconnect(host_no);
                   1235:            ppa_connect(host_no, CONNECT_EPP_MAYBE);
                   1236:            w_dtr(ppb, 0x40);
                   1237:            w_ctr(ppb, 0x08);
                   1238:            udelay(30);
                   1239:            w_ctr(ppb, 0x0c);
                   1240:            udelay(1000);
                   1241:            ppa_disconnect(host_no);
                   1242:            udelay(1000);
                   1243:            if (ppa_hosts[host_no].mode == PPA_EPP_32) {
                   1244:                ppa_hosts[host_no].mode = old_mode;
                   1245:                goto second_pass;
                   1246:            }
                   1247:            printk("ppa: Unable to establish communication, aborting driver load.\n");
                   1248:            return 1;
                   1249:        }
                   1250:        w_ctr(ppb, 0x0c);
                   1251:        k = 1000000;            /* 1 Second */
                   1252:        do {
                   1253:            l = r_str(ppb);
                   1254:            k--;
                   1255:            udelay(1);
                   1256:        } while (!(l & 0x80) && (k));
                   1257: 
                   1258:        l &= 0xf0;
                   1259: 
                   1260:        if (l != 0xf0) {
                   1261:            ppa_disconnect(host_no);
                   1262:            ppa_connect(host_no, CONNECT_EPP_MAYBE);
                   1263:            w_dtr(ppb, 0x40);
                   1264:            w_ctr(ppb, 0x08);
                   1265:            udelay(30);
                   1266:            w_ctr(ppb, 0x0c);
                   1267:            udelay(1000);
                   1268:            ppa_disconnect(host_no);
                   1269:            udelay(1000);
                   1270:            if (ppa_hosts[host_no].mode == PPA_EPP_32) {
                   1271:                ppa_hosts[host_no].mode = old_mode;
                   1272:                goto second_pass;
                   1273:            }
                   1274:            printk("ppa: Unable to establish communication, aborting driver load.\n");
                   1275:            return 1;
                   1276:        }
                   1277:        ppa_disconnect(host_no);
                   1278:        printk("ppa: Communication established with ID %i using %s\n", loop,
                   1279:               PPA_MODE_STRING[ppa_hosts[host_no].mode]);
                   1280:        return 0;
                   1281:     }
                   1282:     printk("ppa: No devices found, aborting driver load.\n");
                   1283:     return 1;
                   1284: }
                   1285: 
                   1286: #define PPA_ID "ppa: "
                   1287: 
                   1288: int port_probe(unsigned short port)
                   1289: {
                   1290:     int retv = 0;
                   1291:     unsigned char a, b, c;
                   1292:     unsigned int i, j;
                   1293: 
                   1294: 
                   1295:     printk(PPA_ID "Probing port %04x\n", port);
                   1296: 
                   1297: /*                 #####  ######  ######
                   1298:  *                #     # #     # #     #
                   1299:  *                #       #     # #     #
                   1300:  *                 #####  ######  ######
                   1301:  *                      # #       #
                   1302:  *                #     # #       #
                   1303:  *                 #####  #       #
                   1304:  */
                   1305: 
                   1306:     outb(0x0c, port + 0x402);
                   1307:     outb(0x0c, port + 0x002);
                   1308:     outb(0x55, port);
                   1309:     a = inb(port);
                   1310:     if (a != 0x55)
                   1311:        return retv;
                   1312:     printk(PPA_ID "    SPP port present\n");
                   1313: 
                   1314:     retv += PPA_PROBE_SPP;
                   1315: 
                   1316: /*                #######  #####  ######
                   1317:  *                #       #     # #     #
                   1318:  *                #       #       #     #
                   1319:  *                #####   #       ######
                   1320:  *                #       #       #
                   1321:  *                #       #     # #
                   1322:  *                #######  #####  #
                   1323:  */
                   1324: 
                   1325:     for (i = 1024; i > 0; i--) {       /* clear at most 1k of data from FIFO */
                   1326:        a = inb(port + 0x402);
                   1327:        if ((a & 0x03) == 0x03)
                   1328:            goto no_ecp;
                   1329:        if (a & 0x01)
                   1330:            break;
                   1331:        inb(port + 0x400);      /* Remove byte from FIFO */
                   1332:     }
                   1333: 
                   1334:     if (i <= 0)
                   1335:        goto no_ecp;
                   1336: 
                   1337:     b = a ^ 3;
                   1338:     outb(b, port + 0x402);
                   1339:     c = inb(port + 0x402);
                   1340: 
                   1341:     if (a == c) {
                   1342:        outb(0xc0, port + 0x402);       /* FIFO test */
                   1343:        j = 0;
                   1344:        while (!(inb(port + 0x402) & 0x01) && (j < 1024)) {
                   1345:            inb(port + 0x400);
                   1346:            j++;
                   1347:        }
                   1348:        if (j >= 1024)
                   1349:            goto no_ecp;
                   1350:        i = 0;
                   1351:        j = 0;
                   1352:        while (!(inb(port + 0x402) & 0x02) && (j < 1024)) {
                   1353:            outb(0x00, port + 0x400);
                   1354:            i++;
                   1355:            j++;
                   1356:        }
                   1357:        if (j >= 1024)
                   1358:            goto no_ecp;
                   1359:        j = 0;
                   1360:        while (!(inb(port + 0x402) & 0x01) && (j < 1024)) {
                   1361:            inb(port + 0x400);
                   1362:            j++;
                   1363:        }
                   1364:        if (j >= 1024)
                   1365:            goto no_ecp;
                   1366:        printk(PPA_ID "    ECP with a %i byte FIFO present\n", i);
                   1367: 
                   1368:        retv += PPA_PROBE_ECR;
                   1369:     }
                   1370: /*                ######   #####   #####
                   1371:  *                #     # #     # #     #
                   1372:  *                #     # #             #
                   1373:  *                ######   #####   #####
                   1374:  *                #             # #
                   1375:  *                #       #     # #
                   1376:  *                #        #####  #######
                   1377:  */
                   1378: 
                   1379:   no_ecp:
                   1380:     if (retv & PPA_PROBE_ECR)
                   1381:        outb(0x20, port + 0x402);
                   1382: 
                   1383:     outb(0x55, port);
                   1384:     outb(0x0c, port + 2);
                   1385:     a = inb(port);
                   1386:     outb(0x55, port);
                   1387:     outb(0x2c, port + 2);
                   1388:     b = inb(port);
                   1389:     if (a != b) {
                   1390:        printk(PPA_ID "    PS/2 bidirectional port present\n");
                   1391:        retv += PPA_PROBE_PS2;
                   1392:     }
                   1393: /*                ####### ######  ######
                   1394:  *                #       #     # #     #
                   1395:  *                #       #     # #     #
                   1396:  *                #####   ######  ######
                   1397:  *                #       #       #
                   1398:  *                #       #       #
                   1399:  *                ####### #       #
                   1400:  */
                   1401: 
                   1402:     if (port & 0x007) {
                   1403:        printk(PPA_ID "    EPP not supported at this address\n");
                   1404:        return retv;
                   1405:     }
                   1406:     if (retv & PPA_PROBE_ECR) {
                   1407:        for (i = 0x00; i < 0x80; i += 0x20) {
                   1408:            outb(i, port + 0x402);
                   1409: 
                   1410:            a = inb(port + 1);
                   1411:            outb(a, port + 1);
                   1412:            outb(a & 0xfe, port + 1);
                   1413:            a = inb(port + 1);
                   1414:            if (!(a & 0x01)) {
                   1415:                printk(PPA_ID "    Failed Intel bug check. (Phony EPP in ECP)\n");
                   1416:                return retv;
                   1417:            }
                   1418:        }
                   1419:        printk(PPA_ID "    Passed Intel bug check.\n");
                   1420:        outb(0x80, port + 0x402);
                   1421:     }
                   1422:     a = inb(port + 1);
                   1423:     outb(a, port + 1);
                   1424:     outb(a & 0xfe, port + 1);
                   1425:     a = inb(port + 1);
                   1426: 
                   1427:     if (a & 0x01) {
                   1428:        outb(0x0c, port + 0x402);
                   1429:        outb(0x0c, port + 0x002);
                   1430:        return retv;
                   1431:     }
                   1432: 
                   1433:     outb(0x04, port + 2);
                   1434:     inb(port + 4);
                   1435:     a = inb(port + 1);
                   1436:     outb(a, port + 1);
                   1437:     outb(a & 0xfe, port + 1);
                   1438: 
                   1439:     if (a & 0x01) {
                   1440:        printk(PPA_ID "    EPP 1.9 with hardware direction protocol\n");
                   1441:        retv += PPA_PROBE_EPP19;
                   1442:     } else {
                   1443:        /* The EPP timeout bit was not set, this could either be:
                   1444:         * EPP 1.7
                   1445:         * EPP 1.9 with software direction
                   1446:         */
                   1447:        outb(0x24, port + 2);
                   1448:        inb(port + 4);
                   1449:        a = inb(port + 1);
                   1450:        outb(a, port + 1);
                   1451:        outb(a & 0xfe, port + 1);
                   1452:        if (a & 0x01) {
                   1453:            printk(PPA_ID "    EPP 1.9 with software direction protocol\n");
                   1454:            retv += PPA_PROBE_EPP19;
                   1455:        } else {
                   1456:            printk(PPA_ID "    EPP 1.7\n");
                   1457:            retv += PPA_PROBE_EPP17;
                   1458:        }
                   1459:     }
                   1460: 
                   1461:     outb(0x0c, port + 0x402);
                   1462:     outb(0x0c, port + 0x002);
                   1463:     return retv;
                   1464: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.