add devsw_name2chr() function to look up character devices

This commit is contained in:
plunky 2008-03-21 19:32:07 +00:00
parent 755dfb4f38
commit 606c30fdca
2 changed files with 51 additions and 3 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: subr_devsw.c,v 1.15 2008/02/13 18:43:16 matt Exp $ */
/* $NetBSD: subr_devsw.c,v 1.16 2008/03/21 19:32:07 plunky Exp $ */
/*-
* Copyright (c) 2001, 2002, 2007 The NetBSD Foundation, Inc.
@ -76,7 +76,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: subr_devsw.c,v 1.15 2008/02/13 18:43:16 matt Exp $");
__KERNEL_RCSID(0, "$NetBSD: subr_devsw.c,v 1.16 2008/03/21 19:32:07 plunky Exp $");
#include <sys/param.h>
#include <sys/conf.h>
@ -506,6 +506,53 @@ devsw_name2blk(const char *name, char *devname, size_t devnamelen)
return (-1);
}
/*
* Convert from device name to char major number.
*
* => Caller must ensure that the device is not detached, and therefore
* that the major number is still valid when dereferenced.
*/
int
devsw_name2chr(const char *name, char *devname, size_t devnamelen)
{
struct devsw_conv *conv;
int cmajor, i;
if (name == NULL)
return (-1);
mutex_enter(&devsw_lock);
for (i = 0 ; i < max_devsw_convs ; i++) {
size_t len;
conv = &devsw_conv[i];
if (conv->d_name == NULL)
continue;
len = strlen(conv->d_name);
if (strncmp(conv->d_name, name, len) != 0)
continue;
if (*(name +len) && !isdigit(*(name + len)))
continue;
cmajor = conv->d_cmajor;
if (cmajor < 0 || cmajor >= max_cdevsws ||
cdevsw[cmajor] == NULL)
break;
if (devname != NULL) {
#ifdef DEVSW_DEBUG
if (strlen(conv->d_name) >= devnamelen)
printf("devsw_name2chr: too short buffer");
#endif /* DEVSW_DEBUG */
strncpy(devname, conv->d_name, devnamelen);
devname[devnamelen - 1] = '\0';
}
mutex_exit(&devsw_lock);
return (cmajor);
}
mutex_exit(&devsw_lock);
return (-1);
}
/*
* Convert from character dev_t to block dev_t.
*

View File

@ -1,4 +1,4 @@
/* $NetBSD: conf.h,v 1.129 2007/11/07 15:56:23 ad Exp $ */
/* $NetBSD: conf.h,v 1.130 2008/03/21 19:32:07 plunky Exp $ */
/*-
* Copyright (c) 1990, 1993
@ -236,6 +236,7 @@ struct devsw_conv {
void devsw_init(void);
const char *devsw_blk2name(int);
int devsw_name2blk(const char *, char *, size_t);
int devsw_name2chr(const char *, char *, size_t);
dev_t devsw_chr2blk(dev_t);
dev_t devsw_blk2chr(dev_t);
#endif /* _KERNEL */