xref: /netbsd-src/sys/arch/sgimips/stand/common/devopen.c (revision 95e1ffb15694e54f29f8baaa4232152b703c2a5a)
1*95e1ffb1Schristos /*	$NetBSD: devopen.c,v 1.6 2005/12/11 12:18:58 christos Exp $	*/
24e63f44fSthorpej 
34e63f44fSthorpej /*-
44e63f44fSthorpej  * Copyright (c) 1992, 1993
54e63f44fSthorpej  *	The Regents of the University of California.  All rights reserved.
64e63f44fSthorpej  *
74e63f44fSthorpej  * This code is derived from software contributed to Berkeley by
84e63f44fSthorpej  * Ralph Campbell.
94e63f44fSthorpej  *
104e63f44fSthorpej  * Redistribution and use in source and binary forms, with or without
114e63f44fSthorpej  * modification, are permitted provided that the following conditions
124e63f44fSthorpej  * are met:
134e63f44fSthorpej  * 1. Redistributions of source code must retain the above copyright
144e63f44fSthorpej  *    notice, this list of conditions and the following disclaimer.
154e63f44fSthorpej  * 2. Redistributions in binary form must reproduce the above copyright
164e63f44fSthorpej  *    notice, this list of conditions and the following disclaimer in the
174e63f44fSthorpej  *    documentation and/or other materials provided with the distribution.
18aad01611Sagc  * 3. Neither the name of the University nor the names of its contributors
194e63f44fSthorpej  *    may be used to endorse or promote products derived from this software
204e63f44fSthorpej  *    without specific prior written permission.
214e63f44fSthorpej  *
224e63f44fSthorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
234e63f44fSthorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
244e63f44fSthorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
254e63f44fSthorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
264e63f44fSthorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
274e63f44fSthorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
284e63f44fSthorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
294e63f44fSthorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
304e63f44fSthorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
314e63f44fSthorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
324e63f44fSthorpej  * SUCH DAMAGE.
334e63f44fSthorpej  *
344e63f44fSthorpej  *	@(#)devopen.c	8.1 (Berkeley) 6/10/93
354e63f44fSthorpej  */
364e63f44fSthorpej 
374e63f44fSthorpej #include <lib/libsa/stand.h>
384e63f44fSthorpej #include <lib/libkern/libkern.h>
394e63f44fSthorpej 
404e63f44fSthorpej /*
414e63f44fSthorpej  * Decode the string 'fname', open the device and return the remaining
424e63f44fSthorpej  * file name if any.
434e63f44fSthorpej  */
444e63f44fSthorpej int
devopen(struct open_file * f,const char * fname,char ** file)45fa657c9cStsutsui devopen(struct open_file *f, const char *fname, char **file)
464e63f44fSthorpej {
47fa657c9cStsutsui #if 0
48fa657c9cStsutsui 	int ctlr = 0, unit = 0, part = 0;
49fa657c9cStsutsui #endif
50fa657c9cStsutsui 	int error;
514e63f44fSthorpej 	char namebuf[128];
524e63f44fSthorpej 	char devtype[16];
534e63f44fSthorpej 	const char *cp;
544e63f44fSthorpej 	char *ncp;
554e63f44fSthorpej #if !defined(LIBSA_SINGLE_DEVICE)
564e63f44fSthorpej 	int i;
574e63f44fSthorpej 	struct devsw *dp;
584e63f44fSthorpej #endif
594e63f44fSthorpej 
604e63f44fSthorpej 	cp = fname;
614e63f44fSthorpej 	ncp = (char *)fname;
624e63f44fSthorpej 
634e63f44fSthorpej 	/*
645bd093cfSrafal 	 * If device starts with a PCI bus specifier, skip past it so the
655bd093cfSrafal 	 * device-matching code below gets the actual device type. Leave
665bd093cfSrafal 	 * fname as is, since it'll be passed back to ARCS to open the
675bd093cfSrafal 	 * device.  This is necessary for the IP32.
684e63f44fSthorpej 	 */
695bd093cfSrafal 	if (strncmp(cp, "pci", 3) == 0) {
705bd093cfSrafal 		while (*ncp && *ncp++ != ')')
715bd093cfSrafal 			;
725bd093cfSrafal 		if (*ncp)
735bd093cfSrafal 			cp = ncp;
745bd093cfSrafal 	}
755bd093cfSrafal 
765bd093cfSrafal 	/*
775bd093cfSrafal 	 * Look for a string like 'scsi(0)disk(0)rdisk(0)partition(0)netbsd'
785bd093cfSrafal 	 * or 'dksc(0,0,0)/netbsd' (the file can either be a relative path
795bd093cfSrafal 	 * or an abosolute path).
805bd093cfSrafal 	 */
815bd093cfSrafal 	if (strncmp(cp, "scsi", 4) == 0) {
824e63f44fSthorpej 		strcpy(devtype, "scsi");
835bd093cfSrafal 	} else if (strncmp(cp, "dksc", 4) == 0) {
84026b6946Stsutsui 		strcpy(devtype, "dksc");
855bd093cfSrafal 	} else {
865bd093cfSrafal 		return ENXIO;
875bd093cfSrafal 	}
885bd093cfSrafal 
894e63f44fSthorpej 	while (ncp != NULL && *ncp != 0) {
904e63f44fSthorpej 		while (*ncp && *ncp++ != ')')
914e63f44fSthorpej 			;
924e63f44fSthorpej 		if (*ncp)
934e63f44fSthorpej 			cp = ncp;
944e63f44fSthorpej 	}
955bd093cfSrafal 
964e63f44fSthorpej 	strncpy(namebuf, fname, sizeof(namebuf));
974e63f44fSthorpej 	namebuf[cp - fname] = 0;
984e63f44fSthorpej 
994e63f44fSthorpej 	printf("devopen: %s type %s file %s\n", namebuf, devtype, cp);
1004e63f44fSthorpej #ifdef LIBSA_SINGLE_DEVICE
101fa657c9cStsutsui 	error = DEV_OPEN(dp)(f, fname);
1024e63f44fSthorpej #else /* !LIBSA_SINGLE_DEVICE */
1034e63f44fSthorpej 	for (dp = devsw, i = 0; i < ndevs; dp++, i++)
1044e63f44fSthorpej 		if (dp->dv_name && strcmp(devtype, dp->dv_name) == 0)
105fa657c9cStsutsui 			goto found;
1064e63f44fSthorpej 	printf("Unknown device '%s'\nKnown devices are:", devtype);
1074e63f44fSthorpej 	for (dp = devsw, i = 0; i < ndevs; dp++, i++)
1084e63f44fSthorpej 		if (dp->dv_name)
1094e63f44fSthorpej 			printf(" %s", dp->dv_name);
1104e63f44fSthorpej 	printf("\n");
1115bd093cfSrafal 	return ENXIO;
1124e63f44fSthorpej 
113fa657c9cStsutsui  found:
114fa657c9cStsutsui 	error = (dp->dv_open)(f, namebuf);
1154e63f44fSthorpej #endif /* !LIBSA_SINGLE_DEVICE */
116fa657c9cStsutsui 	if (error)
117fa657c9cStsutsui 		return error;
1184e63f44fSthorpej 
1194e63f44fSthorpej #ifndef LIBSA_SINGLE_DEVICE
1204e63f44fSthorpej 	f->f_dev = dp;
1214e63f44fSthorpej #endif
1224e63f44fSthorpej 	if (file && *cp != '\0')
1234e63f44fSthorpej 		*file = (char *)cp;	/* XXX */
1245bd093cfSrafal 	return 0;
1254e63f44fSthorpej }
126