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