Home | History | Annotate | Line # | Download | only in scsipi
scsipiconf.c revision 1.4
      1  1.4  thorpej /*	$NetBSD: scsipiconf.c,v 1.4 1997/10/03 02:04:20 thorpej Exp $	*/
      2  1.2   bouyer 
      3  1.2   bouyer /*
      4  1.2   bouyer  * Copyright (c) 1994 Charles Hannum.  All rights reserved.
      5  1.2   bouyer  *
      6  1.2   bouyer  * Redistribution and use in source and binary forms, with or without
      7  1.2   bouyer  * modification, are permitted provided that the following conditions
      8  1.2   bouyer  * are met:
      9  1.2   bouyer  * 1. Redistributions of source code must retain the above copyright
     10  1.2   bouyer  *    notice, this list of conditions and the following disclaimer.
     11  1.2   bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.2   bouyer  *    notice, this list of conditions and the following disclaimer in the
     13  1.2   bouyer  *    documentation and/or other materials provided with the distribution.
     14  1.2   bouyer  * 3. All advertising materials mentioning features or use of this software
     15  1.2   bouyer  *    must display the following acknowledgement:
     16  1.2   bouyer  *	This product includes software developed by Charles Hannum.
     17  1.2   bouyer  * 4. The name of the author may not be used to endorse or promote products
     18  1.2   bouyer  *    derived from this software without specific prior written permission.
     19  1.2   bouyer  *
     20  1.2   bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  1.2   bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  1.2   bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  1.2   bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  1.2   bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  1.2   bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  1.2   bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  1.2   bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  1.2   bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  1.2   bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  1.2   bouyer  */
     31  1.2   bouyer 
     32  1.2   bouyer /*
     33  1.2   bouyer  * Originally written by Julian Elischer (julian (at) tfs.com)
     34  1.2   bouyer  * for TRW Financial Systems for use under the MACH(2.5) operating system.
     35  1.2   bouyer  *
     36  1.2   bouyer  * TRW Financial Systems, in accordance with their agreement with Carnegie
     37  1.2   bouyer  * Mellon University, makes this software available to CMU to distribute
     38  1.2   bouyer  * or use in any manner that they see fit as long as this message is kept with
     39  1.2   bouyer  * the software. For this reason TFS also grants any other persons or
     40  1.2   bouyer  * organisations permission to use or modify this software.
     41  1.2   bouyer  *
     42  1.2   bouyer  * TFS supplies this software to be publicly redistributed
     43  1.2   bouyer  * on the understanding that TFS is not responsible for the correct
     44  1.2   bouyer  * functioning of this software in any circumstances.
     45  1.2   bouyer  *
     46  1.2   bouyer  * Ported to run under 386BSD by Julian Elischer (julian (at) tfs.com) Sept 1992
     47  1.2   bouyer  */
     48  1.2   bouyer 
     49  1.2   bouyer #include <sys/types.h>
     50  1.2   bouyer #include <sys/param.h>
     51  1.2   bouyer #include <sys/systm.h>
     52  1.2   bouyer #include <sys/malloc.h>
     53  1.2   bouyer #include <sys/device.h>
     54  1.2   bouyer 
     55  1.2   bouyer #include <dev/scsipi/scsipi_all.h>
     56  1.2   bouyer #include <dev/scsipi/scsi_all.h>
     57  1.2   bouyer #include <dev/scsipi/scsipiconf.h>
     58  1.2   bouyer 
     59  1.2   bouyer /*
     60  1.2   bouyer  * Return a priority based on how much of the inquiry data matches
     61  1.2   bouyer  * the patterns for the particular driver.
     62  1.2   bouyer  */
     63  1.2   bouyer caddr_t
     64  1.2   bouyer scsipi_inqmatch(inqbuf, base, nmatches, matchsize, bestpriority)
     65  1.2   bouyer 	struct scsipi_inquiry_pattern *inqbuf;
     66  1.2   bouyer 	caddr_t base;
     67  1.2   bouyer 	int nmatches, matchsize;
     68  1.2   bouyer 	int *bestpriority;
     69  1.2   bouyer {
     70  1.2   bouyer 	u_int8_t type;
     71  1.2   bouyer 	caddr_t bestmatch;
     72  1.2   bouyer 
     73  1.2   bouyer 	/* Include the qualifier to catch vendor-unique types. */
     74  1.2   bouyer 	type = inqbuf->type;
     75  1.2   bouyer 
     76  1.2   bouyer 	for (*bestpriority = 0, bestmatch = 0; nmatches--; base += matchsize) {
     77  1.2   bouyer 		struct scsipi_inquiry_pattern *match = (void *)base;
     78  1.2   bouyer 		int priority, len;
     79  1.2   bouyer 
     80  1.2   bouyer 		if (type != match->type)
     81  1.2   bouyer 			continue;
     82  1.2   bouyer 		if (inqbuf->removable != match->removable)
     83  1.2   bouyer 			continue;
     84  1.2   bouyer 		priority = 2;
     85  1.2   bouyer 		len = strlen(match->vendor);
     86  1.2   bouyer 		if (bcmp(inqbuf->vendor, match->vendor, len))
     87  1.2   bouyer 			continue;
     88  1.2   bouyer 		priority += len;
     89  1.2   bouyer 		len = strlen(match->product);
     90  1.2   bouyer 		if (bcmp(inqbuf->product, match->product, len))
     91  1.2   bouyer 			continue;
     92  1.2   bouyer 		priority += len;
     93  1.2   bouyer 		len = strlen(match->revision);
     94  1.2   bouyer 		if (bcmp(inqbuf->revision, match->revision, len))
     95  1.2   bouyer 			continue;
     96  1.2   bouyer 		priority += len;
     97  1.2   bouyer 
     98  1.2   bouyer #ifdef SCSIDEBUG
     99  1.2   bouyer 		printf("scsipi_inqmatch: %d/%d/%d <%s, %s, %s>\n",
    100  1.2   bouyer 		    priority, match->type, match->removable,
    101  1.2   bouyer 		    match->vendor, match->product, match->revision);
    102  1.2   bouyer #endif
    103  1.2   bouyer 		if (priority > *bestpriority) {
    104  1.2   bouyer 			*bestpriority = priority;
    105  1.2   bouyer 			bestmatch = base;
    106  1.2   bouyer 		}
    107  1.2   bouyer 	}
    108  1.2   bouyer 
    109  1.2   bouyer 	return (bestmatch);
    110  1.2   bouyer }
    111  1.2   bouyer 
    112  1.2   bouyer char *
    113  1.2   bouyer scsipi_dtype(type)
    114  1.2   bouyer 	int type;
    115  1.2   bouyer {
    116  1.2   bouyer 	char *dtype;
    117  1.3    enami 
    118  1.2   bouyer 	switch (type) {
    119  1.2   bouyer 	case T_DIRECT:
    120  1.2   bouyer 		dtype = "direct";
    121  1.4  thorpej 		break;
    122  1.2   bouyer 	case T_SEQUENTIAL:
    123  1.2   bouyer 		dtype = "sequential";
    124  1.2   bouyer 		break;
    125  1.2   bouyer 	case T_PRINTER:
    126  1.2   bouyer 		dtype = "printer";
    127  1.2   bouyer 		break;
    128  1.2   bouyer 	case T_PROCESSOR:
    129  1.2   bouyer 		dtype = "processor";
    130  1.2   bouyer 		break;
    131  1.4  thorpej 	case T_WORM:
    132  1.4  thorpej 		dtype = "worm";
    133  1.4  thorpej 		break;
    134  1.2   bouyer 	case T_CDROM:
    135  1.2   bouyer 		dtype = "cdrom";
    136  1.2   bouyer 		break;
    137  1.2   bouyer 	case T_SCANNER:
    138  1.2   bouyer 		dtype = "scanner";
    139  1.2   bouyer 		break;
    140  1.2   bouyer 	case T_OPTICAL:
    141  1.2   bouyer 		dtype = "optical";
    142  1.2   bouyer 		break;
    143  1.2   bouyer 	case T_CHANGER:
    144  1.2   bouyer 		dtype = "changer";
    145  1.2   bouyer 		break;
    146  1.2   bouyer 	case T_COMM:
    147  1.2   bouyer 		dtype = "communication";
    148  1.4  thorpej 		break;
    149  1.4  thorpej 	case T_IT8_1:
    150  1.4  thorpej 	case T_IT8_2:
    151  1.4  thorpej 		dtype = "it8";		/* ??? */
    152  1.4  thorpej 		break;
    153  1.4  thorpej 	case T_STORARRAY:
    154  1.4  thorpej 		dtype = "storage array";
    155  1.4  thorpej 		break;
    156  1.4  thorpej 	case T_ENCLOSURE:
    157  1.4  thorpej 		dtype = "enclosure services";
    158  1.2   bouyer 		break;
    159  1.2   bouyer 	case T_NODEVICE:
    160  1.2   bouyer 		panic("scsipi_dtype: impossible device type");
    161  1.2   bouyer 	default:
    162  1.2   bouyer 		dtype = "unknown";
    163  1.2   bouyer 		break;
    164  1.2   bouyer 	}
    165  1.3    enami 	return (dtype);
    166  1.2   bouyer }
    167  1.2   bouyer 
    168  1.2   bouyer void
    169  1.2   bouyer scsipi_strvis(dst, src, len)
    170  1.2   bouyer 	u_char *dst, *src;
    171  1.2   bouyer 	int len;
    172  1.2   bouyer {
    173  1.2   bouyer 
    174  1.2   bouyer 	/* Trim leading and trailing blanks and NULs. */
    175  1.2   bouyer 	while (len > 0 && (src[0] == ' ' || src[0] == '\0'))
    176  1.2   bouyer 		++src, --len;
    177  1.2   bouyer 	while (len > 0 && (src[len-1] == ' ' || src[len-1] == '\0'))
    178  1.2   bouyer 		--len;
    179  1.2   bouyer 
    180  1.2   bouyer 	while (len > 0) {
    181  1.2   bouyer 		if (*src < 0x20 || *src >= 0x80) {
    182  1.2   bouyer 			/* non-printable characters */
    183  1.2   bouyer 			*dst++ = '\\';
    184  1.2   bouyer 			*dst++ = ((*src & 0300) >> 6) + '0';
    185  1.2   bouyer 			*dst++ = ((*src & 0070) >> 3) + '0';
    186  1.2   bouyer 			*dst++ = ((*src & 0007) >> 0) + '0';
    187  1.2   bouyer 		} else if (*src == '\\') {
    188  1.2   bouyer 			/* quote characters */
    189  1.2   bouyer 			*dst++ = '\\';
    190  1.2   bouyer 			*dst++ = '\\';
    191  1.2   bouyer 		} else {
    192  1.2   bouyer 			/* normal characters */
    193  1.2   bouyer 			*dst++ = *src;
    194  1.2   bouyer 		}
    195  1.2   bouyer 		++src, --len;
    196  1.2   bouyer 	}
    197  1.2   bouyer 
    198  1.2   bouyer 	*dst++ = 0;
    199  1.2   bouyer }
    200