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

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

unix.superglobalmegacorp.com

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