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