Home | History | Annotate | Line # | Download | only in common
devopen.c revision 1.1.2.2
      1  1.1.2.2  thorpej /*	$NetBSD: devopen.c,v 1.1.2.2 2002/01/10 19:48:35 thorpej Exp $	*/
      2  1.1.2.2  thorpej 
      3  1.1.2.2  thorpej /*-
      4  1.1.2.2  thorpej  * Copyright (c) 1992, 1993
      5  1.1.2.2  thorpej  *	The Regents of the University of California.  All rights reserved.
      6  1.1.2.2  thorpej  *
      7  1.1.2.2  thorpej  * This code is derived from software contributed to Berkeley by
      8  1.1.2.2  thorpej  * Ralph Campbell.
      9  1.1.2.2  thorpej  *
     10  1.1.2.2  thorpej  * Redistribution and use in source and binary forms, with or without
     11  1.1.2.2  thorpej  * modification, are permitted provided that the following conditions
     12  1.1.2.2  thorpej  * are met:
     13  1.1.2.2  thorpej  * 1. Redistributions of source code must retain the above copyright
     14  1.1.2.2  thorpej  *    notice, this list of conditions and the following disclaimer.
     15  1.1.2.2  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1.2.2  thorpej  *    notice, this list of conditions and the following disclaimer in the
     17  1.1.2.2  thorpej  *    documentation and/or other materials provided with the distribution.
     18  1.1.2.2  thorpej  * 3. All advertising materials mentioning features or use of this software
     19  1.1.2.2  thorpej  *    must display the following acknowledgement:
     20  1.1.2.2  thorpej  *	This product includes software developed by the University of
     21  1.1.2.2  thorpej  *	California, Berkeley and its contributors.
     22  1.1.2.2  thorpej  * 4. Neither the name of the University nor the names of its contributors
     23  1.1.2.2  thorpej  *    may be used to endorse or promote products derived from this software
     24  1.1.2.2  thorpej  *    without specific prior written permission.
     25  1.1.2.2  thorpej  *
     26  1.1.2.2  thorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  1.1.2.2  thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  1.1.2.2  thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  1.1.2.2  thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  1.1.2.2  thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  1.1.2.2  thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  1.1.2.2  thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  1.1.2.2  thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  1.1.2.2  thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  1.1.2.2  thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  1.1.2.2  thorpej  * SUCH DAMAGE.
     37  1.1.2.2  thorpej  *
     38  1.1.2.2  thorpej  *	@(#)devopen.c	8.1 (Berkeley) 6/10/93
     39  1.1.2.2  thorpej  */
     40  1.1.2.2  thorpej 
     41  1.1.2.2  thorpej #include <lib/libsa/stand.h>
     42  1.1.2.2  thorpej #include <lib/libkern/libkern.h>
     43  1.1.2.2  thorpej 
     44  1.1.2.2  thorpej /*
     45  1.1.2.2  thorpej  * Decode the string 'fname', open the device and return the remaining
     46  1.1.2.2  thorpej  * file name if any.
     47  1.1.2.2  thorpej  */
     48  1.1.2.2  thorpej int
     49  1.1.2.2  thorpej devopen(f, fname, file)
     50  1.1.2.2  thorpej 	struct open_file *f;
     51  1.1.2.2  thorpej 	const char *fname;
     52  1.1.2.2  thorpej 	char **file;	/* out */
     53  1.1.2.2  thorpej {
     54  1.1.2.2  thorpej /*	int ctlr = 0, unit = 0, part = 0; */
     55  1.1.2.2  thorpej 	int rc;
     56  1.1.2.2  thorpej 	char namebuf[128];
     57  1.1.2.2  thorpej 	char devtype[16];
     58  1.1.2.2  thorpej 	const char *cp;
     59  1.1.2.2  thorpej 	char *ncp;
     60  1.1.2.2  thorpej #if !defined(LIBSA_SINGLE_DEVICE)
     61  1.1.2.2  thorpej 	int i;
     62  1.1.2.2  thorpej 	struct devsw *dp;
     63  1.1.2.2  thorpej #endif
     64  1.1.2.2  thorpej 
     65  1.1.2.2  thorpej 	cp = fname;
     66  1.1.2.2  thorpej 	ncp = (char *)fname;
     67  1.1.2.2  thorpej 
     68  1.1.2.2  thorpej 	/*
     69  1.1.2.2  thorpej 	 * look for a string like 'scsi(0)disk(0)rdisk(0)partition(0)netbsd'
     70  1.1.2.2  thorpej 	 * or 'dksc(0,0,0)netbsd'
     71  1.1.2.2  thorpej 	 */
     72  1.1.2.2  thorpej 	if (cp[0] == 's' && cp[1] == 'c' && cp[2] == 's' && cp[3] == 'i') {
     73  1.1.2.2  thorpej 		strcpy(devtype, "scsi");
     74  1.1.2.2  thorpej 	} else if (cp[0] == 'd' && cp[1] == 'k' && cp[2] == 's' && cp[3] == 'c') {
     75  1.1.2.2  thorpej 		strcpy(devtype, "dksi");
     76  1.1.2.2  thorpej 	} else
     77  1.1.2.2  thorpej 		return (ENXIO);
     78  1.1.2.2  thorpej 	while (ncp != NULL && *ncp != 0) {
     79  1.1.2.2  thorpej 		while (*ncp && *ncp++ != ')')
     80  1.1.2.2  thorpej 			;
     81  1.1.2.2  thorpej 		if (*ncp)
     82  1.1.2.2  thorpej 			cp = ncp;
     83  1.1.2.2  thorpej 	}
     84  1.1.2.2  thorpej 	strncpy(namebuf, fname, sizeof(namebuf));
     85  1.1.2.2  thorpej 	namebuf[cp - fname] = 0;
     86  1.1.2.2  thorpej 
     87  1.1.2.2  thorpej 	printf("devopen: %s type %s file %s\n", namebuf, devtype, cp);
     88  1.1.2.2  thorpej #ifdef LIBSA_SINGLE_DEVICE
     89  1.1.2.2  thorpej 	rc = DEV_OPEN(dp)(f, fname);
     90  1.1.2.2  thorpej #else /* !LIBSA_SINGLE_DEVICE */
     91  1.1.2.2  thorpej 	for (dp = devsw, i = 0; i < ndevs; dp++, i++)
     92  1.1.2.2  thorpej 		if (dp->dv_name && strcmp(devtype, dp->dv_name) == 0)
     93  1.1.2.2  thorpej 			goto fnd;
     94  1.1.2.2  thorpej 	printf("Unknown device '%s'\nKnown devices are:", devtype);
     95  1.1.2.2  thorpej 	for (dp = devsw, i = 0; i < ndevs; dp++, i++)
     96  1.1.2.2  thorpej 		if (dp->dv_name)
     97  1.1.2.2  thorpej 			printf(" %s", dp->dv_name);
     98  1.1.2.2  thorpej 	printf("\n");
     99  1.1.2.2  thorpej 	return (ENXIO);
    100  1.1.2.2  thorpej 
    101  1.1.2.2  thorpej fnd:
    102  1.1.2.2  thorpej 	rc = (dp->dv_open)(f, namebuf);
    103  1.1.2.2  thorpej #endif /* !LIBSA_SINGLE_DEVICE */
    104  1.1.2.2  thorpej 	if (rc)
    105  1.1.2.2  thorpej 		return (rc);
    106  1.1.2.2  thorpej 
    107  1.1.2.2  thorpej #ifndef LIBSA_SINGLE_DEVICE
    108  1.1.2.2  thorpej 	f->f_dev = dp;
    109  1.1.2.2  thorpej #endif
    110  1.1.2.2  thorpej 	if (file && *cp != '\0')
    111  1.1.2.2  thorpej 		*file = (char *)cp;	/* XXX */
    112  1.1.2.2  thorpej 	return (0);
    113  1.1.2.2  thorpej }
    114