Home | History | Annotate | Line # | Download | only in kern
subr_devsw.c revision 1.23
      1  1.23    pooka /*	$NetBSD: subr_devsw.c,v 1.23 2008/12/29 17:41:18 pooka Exp $	*/
      2  1.11       ad 
      3   1.2  gehenna /*-
      4  1.20       ad  * Copyright (c) 2001, 2002, 2007, 2008 The NetBSD Foundation, Inc.
      5   1.2  gehenna  * All rights reserved.
      6   1.2  gehenna  *
      7   1.2  gehenna  * This code is derived from software contributed to The NetBSD Foundation
      8  1.11       ad  * by MAEKAWA Masahide <gehenna (at) NetBSD.org>, and by Andrew Doran.
      9   1.2  gehenna  *
     10   1.2  gehenna  * Redistribution and use in source and binary forms, with or without
     11   1.2  gehenna  * modification, are permitted provided that the following conditions
     12   1.2  gehenna  * are met:
     13   1.2  gehenna  * 1. Redistributions of source code must retain the above copyright
     14   1.2  gehenna  *    notice, this list of conditions and the following disclaimer.
     15   1.2  gehenna  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.2  gehenna  *    notice, this list of conditions and the following disclaimer in the
     17   1.2  gehenna  *    documentation and/or other materials provided with the distribution.
     18   1.2  gehenna  *
     19   1.2  gehenna  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.2  gehenna  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.2  gehenna  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.2  gehenna  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.2  gehenna  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.2  gehenna  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.2  gehenna  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.2  gehenna  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.2  gehenna  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.2  gehenna  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.2  gehenna  * POSSIBILITY OF SUCH DAMAGE.
     30   1.2  gehenna  */
     31  1.11       ad 
     32  1.11       ad /*
     33  1.11       ad  * Overview
     34  1.11       ad  *
     35  1.11       ad  *	subr_devsw.c: registers device drivers by name and by major
     36  1.11       ad  *	number, and provides wrapper methods for performing I/O and
     37  1.11       ad  *	other tasks on device drivers, keying on the device number
     38  1.11       ad  *	(dev_t).
     39  1.11       ad  *
     40  1.11       ad  *	When the system is built, the config(8) command generates
     41  1.11       ad  *	static tables of device drivers built into the kernel image
     42  1.11       ad  *	along with their associated methods.  These are recorded in
     43  1.11       ad  *	the cdevsw0 and bdevsw0 tables.  Drivers can also be added to
     44  1.11       ad  *	and removed from the system dynamically.
     45  1.11       ad  *
     46  1.11       ad  * Allocation
     47  1.11       ad  *
     48  1.11       ad  *	When the system initially boots only the statically allocated
     49  1.11       ad  *	indexes (bdevsw0, cdevsw0) are used.  If these overflow due to
     50  1.11       ad  *	allocation, we allocate a fixed block of memory to hold the new,
     51  1.11       ad  *	expanded index.  This "fork" of the table is only ever performed
     52  1.11       ad  *	once in order to guarantee that other threads may safely access
     53  1.11       ad  *	the device tables:
     54  1.11       ad  *
     55  1.11       ad  *	o Once a thread has a "reference" to the table via an earlier
     56  1.11       ad  *	  open() call, we know that the entry in the table must exist
     57  1.11       ad  *	  and so it is safe to access it.
     58  1.11       ad  *
     59  1.11       ad  *	o Regardless of whether other threads see the old or new
     60  1.11       ad  *	  pointers, they will point to a correct device switch
     61  1.11       ad  *	  structure for the operation being performed.
     62  1.11       ad  *
     63  1.11       ad  *	XXX Currently, the wrapper methods such as cdev_read() verify
     64  1.11       ad  *	that a device driver does in fact exist before calling the
     65  1.11       ad  *	associated driver method.  This should be changed so that
     66  1.11       ad  *	once the device is has been referenced by a vnode (opened),
     67  1.11       ad  *	calling	the other methods should be valid until that reference
     68  1.11       ad  *	is dropped.
     69  1.11       ad  */
     70   1.7    lukem 
     71   1.7    lukem #include <sys/cdefs.h>
     72  1.23    pooka __KERNEL_RCSID(0, "$NetBSD: subr_devsw.c,v 1.23 2008/12/29 17:41:18 pooka Exp $");
     73   1.2  gehenna 
     74   1.2  gehenna #include <sys/param.h>
     75   1.2  gehenna #include <sys/conf.h>
     76  1.11       ad #include <sys/kmem.h>
     77   1.2  gehenna #include <sys/systm.h>
     78  1.11       ad #include <sys/poll.h>
     79  1.11       ad #include <sys/tty.h>
     80  1.15     matt #include <sys/cpu.h>
     81  1.11       ad #include <sys/buf.h>
     82   1.2  gehenna 
     83   1.2  gehenna #ifdef DEVSW_DEBUG
     84   1.2  gehenna #define	DPRINTF(x)	printf x
     85   1.2  gehenna #else /* DEVSW_DEBUG */
     86   1.2  gehenna #define	DPRINTF(x)
     87   1.2  gehenna #endif /* DEVSW_DEBUG */
     88   1.2  gehenna 
     89  1.11       ad #define	MAXDEVSW	512	/* the maximum of major device number */
     90   1.2  gehenna #define	BDEVSW_SIZE	(sizeof(struct bdevsw *))
     91   1.2  gehenna #define	CDEVSW_SIZE	(sizeof(struct cdevsw *))
     92   1.2  gehenna #define	DEVSWCONV_SIZE	(sizeof(struct devsw_conv))
     93   1.2  gehenna 
     94   1.2  gehenna extern const struct bdevsw **bdevsw, *bdevsw0[];
     95   1.2  gehenna extern const struct cdevsw **cdevsw, *cdevsw0[];
     96   1.2  gehenna extern struct devsw_conv *devsw_conv, devsw_conv0[];
     97   1.2  gehenna extern const int sys_bdevsws, sys_cdevsws;
     98   1.2  gehenna extern int max_bdevsws, max_cdevsws, max_devsw_convs;
     99   1.2  gehenna 
    100  1.14    pooka static int bdevsw_attach(const struct bdevsw *, int *);
    101  1.14    pooka static int cdevsw_attach(const struct cdevsw *, int *);
    102  1.11       ad static void devsw_detach_locked(const struct bdevsw *, const struct cdevsw *);
    103  1.11       ad 
    104  1.23    pooka kmutex_t device_lock;
    105  1.23    pooka 
    106  1.11       ad void
    107  1.11       ad devsw_init(void)
    108  1.11       ad {
    109  1.11       ad 
    110  1.11       ad 	KASSERT(sys_bdevsws < MAXDEVSW - 1);
    111  1.11       ad 	KASSERT(sys_cdevsws < MAXDEVSW - 1);
    112  1.23    pooka 	mutex_init(&device_lock, MUTEX_DEFAULT, IPL_NONE);
    113  1.11       ad }
    114   1.2  gehenna 
    115   1.2  gehenna int
    116   1.2  gehenna devsw_attach(const char *devname, const struct bdevsw *bdev, int *bmajor,
    117   1.2  gehenna 	     const struct cdevsw *cdev, int *cmajor)
    118   1.2  gehenna {
    119   1.2  gehenna 	struct devsw_conv *conv;
    120   1.2  gehenna 	char *name;
    121   1.2  gehenna 	int error, i;
    122   1.2  gehenna 
    123   1.2  gehenna 	if (devname == NULL || cdev == NULL)
    124   1.2  gehenna 		return (EINVAL);
    125   1.2  gehenna 
    126  1.23    pooka 	mutex_enter(&device_lock);
    127  1.11       ad 
    128   1.2  gehenna 	for (i = 0 ; i < max_devsw_convs ; i++) {
    129   1.2  gehenna 		conv = &devsw_conv[i];
    130   1.2  gehenna 		if (conv->d_name == NULL || strcmp(devname, conv->d_name) != 0)
    131   1.2  gehenna 			continue;
    132   1.2  gehenna 
    133   1.2  gehenna 		if (*bmajor < 0)
    134   1.2  gehenna 			*bmajor = conv->d_bmajor;
    135   1.2  gehenna 		if (*cmajor < 0)
    136   1.2  gehenna 			*cmajor = conv->d_cmajor;
    137   1.2  gehenna 
    138  1.11       ad 		if (*bmajor != conv->d_bmajor || *cmajor != conv->d_cmajor) {
    139  1.11       ad 			error = EINVAL;
    140  1.11       ad 			goto fail;
    141  1.11       ad 		}
    142  1.11       ad 		if ((*bmajor >= 0 && bdev == NULL) || *cmajor < 0) {
    143  1.11       ad 			error = EINVAL;
    144  1.11       ad 			goto fail;
    145  1.11       ad 		}
    146   1.2  gehenna 
    147   1.2  gehenna 		if ((*bmajor >= 0 && bdevsw[*bmajor] != NULL) ||
    148  1.11       ad 		    cdevsw[*cmajor] != NULL) {
    149  1.11       ad 			error = EEXIST;
    150  1.11       ad 			goto fail;
    151  1.11       ad 		}
    152   1.2  gehenna 
    153   1.2  gehenna 		if (bdev != NULL)
    154   1.2  gehenna 			bdevsw[*bmajor] = bdev;
    155   1.2  gehenna 		cdevsw[*cmajor] = cdev;
    156   1.2  gehenna 
    157  1.23    pooka 		mutex_exit(&device_lock);
    158   1.2  gehenna 		return (0);
    159   1.2  gehenna 	}
    160   1.2  gehenna 
    161  1.14    pooka 	error = bdevsw_attach(bdev, bmajor);
    162  1.11       ad 	if (error != 0)
    163  1.11       ad 		goto fail;
    164  1.14    pooka 	error = cdevsw_attach(cdev, cmajor);
    165   1.2  gehenna 	if (error != 0) {
    166  1.11       ad 		devsw_detach_locked(bdev, NULL);
    167  1.11       ad 		goto fail;
    168   1.2  gehenna 	}
    169   1.2  gehenna 
    170   1.2  gehenna 	for (i = 0 ; i < max_devsw_convs ; i++) {
    171   1.2  gehenna 		if (devsw_conv[i].d_name == NULL)
    172   1.2  gehenna 			break;
    173   1.2  gehenna 	}
    174   1.2  gehenna 	if (i == max_devsw_convs) {
    175   1.2  gehenna 		struct devsw_conv *newptr;
    176   1.2  gehenna 		int old, new;
    177   1.2  gehenna 
    178   1.2  gehenna 		old = max_devsw_convs;
    179   1.2  gehenna 		new = old + 1;
    180   1.2  gehenna 
    181  1.11       ad 		newptr = kmem_zalloc(new * DEVSWCONV_SIZE, KM_NOSLEEP);
    182   1.2  gehenna 		if (newptr == NULL) {
    183  1.11       ad 			devsw_detach_locked(bdev, cdev);
    184  1.11       ad 			error = ENOMEM;
    185  1.11       ad 			goto fail;
    186   1.2  gehenna 		}
    187   1.2  gehenna 		newptr[old].d_name = NULL;
    188   1.2  gehenna 		newptr[old].d_bmajor = -1;
    189   1.2  gehenna 		newptr[old].d_cmajor = -1;
    190   1.2  gehenna 		memcpy(newptr, devsw_conv, old * DEVSWCONV_SIZE);
    191   1.2  gehenna 		if (devsw_conv != devsw_conv0)
    192  1.11       ad 			kmem_free(devsw_conv, old * DEVSWCONV_SIZE);
    193   1.2  gehenna 		devsw_conv = newptr;
    194   1.2  gehenna 		max_devsw_convs = new;
    195   1.2  gehenna 	}
    196   1.2  gehenna 
    197   1.6   itojun 	i = strlen(devname) + 1;
    198  1.11       ad 	name = kmem_alloc(i, KM_NOSLEEP);
    199   1.2  gehenna 	if (name == NULL) {
    200  1.11       ad 		devsw_detach_locked(bdev, cdev);
    201  1.11       ad 		goto fail;
    202   1.2  gehenna 	}
    203   1.6   itojun 	strlcpy(name, devname, i);
    204   1.2  gehenna 
    205   1.2  gehenna 	devsw_conv[i].d_name = name;
    206   1.2  gehenna 	devsw_conv[i].d_bmajor = *bmajor;
    207   1.2  gehenna 	devsw_conv[i].d_cmajor = *cmajor;
    208   1.2  gehenna 
    209  1.23    pooka 	mutex_exit(&device_lock);
    210   1.2  gehenna 	return (0);
    211  1.11       ad  fail:
    212  1.23    pooka 	mutex_exit(&device_lock);
    213  1.11       ad 	return (error);
    214   1.2  gehenna }
    215   1.2  gehenna 
    216   1.2  gehenna static int
    217  1.14    pooka bdevsw_attach(const struct bdevsw *devsw, int *devmajor)
    218   1.2  gehenna {
    219  1.11       ad 	const struct bdevsw **newptr;
    220   1.2  gehenna 	int bmajor, i;
    221   1.2  gehenna 
    222  1.23    pooka 	KASSERT(mutex_owned(&device_lock));
    223  1.11       ad 
    224   1.2  gehenna 	if (devsw == NULL)
    225   1.2  gehenna 		return (0);
    226   1.2  gehenna 
    227   1.2  gehenna 	if (*devmajor < 0) {
    228   1.2  gehenna 		for (bmajor = sys_bdevsws ; bmajor < max_bdevsws ; bmajor++) {
    229   1.2  gehenna 			if (bdevsw[bmajor] != NULL)
    230   1.2  gehenna 				continue;
    231   1.2  gehenna 			for (i = 0 ; i < max_devsw_convs ; i++) {
    232   1.2  gehenna 				if (devsw_conv[i].d_bmajor == bmajor)
    233   1.2  gehenna 					break;
    234   1.2  gehenna 			}
    235   1.2  gehenna 			if (i != max_devsw_convs)
    236   1.2  gehenna 				continue;
    237   1.2  gehenna 			break;
    238   1.2  gehenna 		}
    239   1.3  gehenna 		*devmajor = bmajor;
    240   1.2  gehenna 	}
    241  1.11       ad 
    242   1.2  gehenna 	if (*devmajor >= MAXDEVSW) {
    243  1.11       ad 		printf("bdevsw_attach: block majors exhausted");
    244   1.2  gehenna 		return (ENOMEM);
    245   1.2  gehenna 	}
    246   1.2  gehenna 
    247   1.2  gehenna 	if (*devmajor >= max_bdevsws) {
    248  1.11       ad 		KASSERT(bdevsw == bdevsw0);
    249  1.11       ad 		newptr = kmem_zalloc(MAXDEVSW * BDEVSW_SIZE, KM_NOSLEEP);
    250   1.2  gehenna 		if (newptr == NULL)
    251   1.2  gehenna 			return (ENOMEM);
    252  1.11       ad 		memcpy(newptr, bdevsw, max_bdevsws * BDEVSW_SIZE);
    253   1.2  gehenna 		bdevsw = newptr;
    254  1.11       ad 		max_bdevsws = MAXDEVSW;
    255   1.2  gehenna 	}
    256   1.2  gehenna 
    257   1.2  gehenna 	if (bdevsw[*devmajor] != NULL)
    258   1.2  gehenna 		return (EEXIST);
    259   1.2  gehenna 
    260   1.2  gehenna 	bdevsw[*devmajor] = devsw;
    261   1.2  gehenna 
    262   1.2  gehenna 	return (0);
    263   1.2  gehenna }
    264   1.2  gehenna 
    265   1.2  gehenna static int
    266  1.14    pooka cdevsw_attach(const struct cdevsw *devsw, int *devmajor)
    267   1.2  gehenna {
    268  1.11       ad 	const struct cdevsw **newptr;
    269   1.2  gehenna 	int cmajor, i;
    270   1.2  gehenna 
    271  1.23    pooka 	KASSERT(mutex_owned(&device_lock));
    272  1.11       ad 
    273   1.2  gehenna 	if (*devmajor < 0) {
    274   1.2  gehenna 		for (cmajor = sys_cdevsws ; cmajor < max_cdevsws ; cmajor++) {
    275   1.2  gehenna 			if (cdevsw[cmajor] != NULL)
    276   1.2  gehenna 				continue;
    277   1.2  gehenna 			for (i = 0 ; i < max_devsw_convs ; i++) {
    278   1.2  gehenna 				if (devsw_conv[i].d_cmajor == cmajor)
    279   1.2  gehenna 					break;
    280   1.2  gehenna 			}
    281   1.2  gehenna 			if (i != max_devsw_convs)
    282   1.2  gehenna 				continue;
    283   1.2  gehenna 			break;
    284   1.2  gehenna 		}
    285   1.3  gehenna 		*devmajor = cmajor;
    286   1.2  gehenna 	}
    287  1.11       ad 
    288   1.2  gehenna 	if (*devmajor >= MAXDEVSW) {
    289  1.11       ad 		printf("cdevsw_attach: character majors exhausted");
    290   1.2  gehenna 		return (ENOMEM);
    291   1.2  gehenna 	}
    292   1.2  gehenna 
    293   1.2  gehenna 	if (*devmajor >= max_cdevsws) {
    294  1.11       ad 		KASSERT(cdevsw == cdevsw0);
    295  1.11       ad 		newptr = kmem_zalloc(MAXDEVSW * CDEVSW_SIZE, KM_NOSLEEP);
    296   1.2  gehenna 		if (newptr == NULL)
    297   1.2  gehenna 			return (ENOMEM);
    298  1.11       ad 		memcpy(newptr, cdevsw, max_cdevsws * CDEVSW_SIZE);
    299   1.2  gehenna 		cdevsw = newptr;
    300  1.11       ad 		max_cdevsws = MAXDEVSW;
    301   1.2  gehenna 	}
    302   1.2  gehenna 
    303   1.2  gehenna 	if (cdevsw[*devmajor] != NULL)
    304   1.2  gehenna 		return (EEXIST);
    305   1.2  gehenna 
    306   1.2  gehenna 	cdevsw[*devmajor] = devsw;
    307   1.2  gehenna 
    308   1.2  gehenna 	return (0);
    309   1.2  gehenna }
    310   1.2  gehenna 
    311  1.11       ad static void
    312  1.11       ad devsw_detach_locked(const struct bdevsw *bdev, const struct cdevsw *cdev)
    313   1.2  gehenna {
    314   1.2  gehenna 	int i;
    315   1.2  gehenna 
    316  1.23    pooka 	KASSERT(mutex_owned(&device_lock));
    317  1.11       ad 
    318   1.2  gehenna 	if (bdev != NULL) {
    319   1.2  gehenna 		for (i = 0 ; i < max_bdevsws ; i++) {
    320   1.2  gehenna 			if (bdevsw[i] != bdev)
    321   1.2  gehenna 				continue;
    322   1.2  gehenna 			bdevsw[i] = NULL;
    323   1.2  gehenna 			break;
    324   1.2  gehenna 		}
    325   1.2  gehenna 	}
    326   1.2  gehenna 	if (cdev != NULL) {
    327   1.2  gehenna 		for (i = 0 ; i < max_cdevsws ; i++) {
    328   1.2  gehenna 			if (cdevsw[i] != cdev)
    329   1.2  gehenna 				continue;
    330   1.2  gehenna 			cdevsw[i] = NULL;
    331   1.2  gehenna 			break;
    332   1.2  gehenna 		}
    333   1.2  gehenna 	}
    334   1.2  gehenna }
    335   1.2  gehenna 
    336  1.19       ad int
    337  1.11       ad devsw_detach(const struct bdevsw *bdev, const struct cdevsw *cdev)
    338  1.11       ad {
    339  1.11       ad 
    340  1.23    pooka 	mutex_enter(&device_lock);
    341  1.11       ad 	devsw_detach_locked(bdev, cdev);
    342  1.23    pooka 	mutex_exit(&device_lock);
    343  1.19       ad 	return 0;
    344  1.11       ad }
    345  1.11       ad 
    346  1.11       ad /*
    347  1.11       ad  * Look up a block device by number.
    348  1.11       ad  *
    349  1.11       ad  * => Caller must ensure that the device is attached.
    350  1.11       ad  */
    351   1.2  gehenna const struct bdevsw *
    352   1.2  gehenna bdevsw_lookup(dev_t dev)
    353   1.2  gehenna {
    354   1.2  gehenna 	int bmajor;
    355   1.2  gehenna 
    356   1.2  gehenna 	if (dev == NODEV)
    357   1.2  gehenna 		return (NULL);
    358   1.2  gehenna 	bmajor = major(dev);
    359   1.2  gehenna 	if (bmajor < 0 || bmajor >= max_bdevsws)
    360   1.2  gehenna 		return (NULL);
    361   1.2  gehenna 
    362   1.2  gehenna 	return (bdevsw[bmajor]);
    363   1.2  gehenna }
    364   1.2  gehenna 
    365  1.11       ad /*
    366  1.11       ad  * Look up a character device by number.
    367  1.11       ad  *
    368  1.11       ad  * => Caller must ensure that the device is attached.
    369  1.11       ad  */
    370   1.2  gehenna const struct cdevsw *
    371   1.2  gehenna cdevsw_lookup(dev_t dev)
    372   1.2  gehenna {
    373   1.2  gehenna 	int cmajor;
    374   1.2  gehenna 
    375   1.2  gehenna 	if (dev == NODEV)
    376   1.2  gehenna 		return (NULL);
    377   1.2  gehenna 	cmajor = major(dev);
    378   1.2  gehenna 	if (cmajor < 0 || cmajor >= max_cdevsws)
    379   1.2  gehenna 		return (NULL);
    380   1.2  gehenna 
    381   1.2  gehenna 	return (cdevsw[cmajor]);
    382   1.2  gehenna }
    383   1.2  gehenna 
    384  1.11       ad /*
    385  1.11       ad  * Look up a block device by reference to its operations set.
    386  1.11       ad  *
    387  1.11       ad  * => Caller must ensure that the device is not detached, and therefore
    388  1.11       ad  *    that the returned major is still valid when dereferenced.
    389  1.11       ad  */
    390   1.2  gehenna int
    391   1.2  gehenna bdevsw_lookup_major(const struct bdevsw *bdev)
    392   1.2  gehenna {
    393   1.2  gehenna 	int bmajor;
    394   1.2  gehenna 
    395   1.2  gehenna 	for (bmajor = 0 ; bmajor < max_bdevsws ; bmajor++) {
    396   1.2  gehenna 		if (bdevsw[bmajor] == bdev)
    397   1.2  gehenna 			return (bmajor);
    398   1.2  gehenna 	}
    399   1.2  gehenna 
    400   1.2  gehenna 	return (-1);
    401   1.2  gehenna }
    402   1.2  gehenna 
    403  1.11       ad /*
    404  1.11       ad  * Look up a character device by reference to its operations set.
    405  1.11       ad  *
    406  1.11       ad  * => Caller must ensure that the device is not detached, and therefore
    407  1.11       ad  *    that the returned major is still valid when dereferenced.
    408  1.11       ad  */
    409   1.2  gehenna int
    410   1.2  gehenna cdevsw_lookup_major(const struct cdevsw *cdev)
    411   1.2  gehenna {
    412   1.2  gehenna 	int cmajor;
    413   1.2  gehenna 
    414   1.2  gehenna 	for (cmajor = 0 ; cmajor < max_cdevsws ; cmajor++) {
    415   1.2  gehenna 		if (cdevsw[cmajor] == cdev)
    416   1.2  gehenna 			return (cmajor);
    417   1.2  gehenna 	}
    418   1.2  gehenna 
    419   1.2  gehenna 	return (-1);
    420   1.2  gehenna }
    421   1.2  gehenna 
    422   1.2  gehenna /*
    423   1.2  gehenna  * Convert from block major number to name.
    424  1.11       ad  *
    425  1.11       ad  * => Caller must ensure that the device is not detached, and therefore
    426  1.11       ad  *    that the name pointer is still valid when dereferenced.
    427   1.2  gehenna  */
    428   1.2  gehenna const char *
    429   1.2  gehenna devsw_blk2name(int bmajor)
    430   1.2  gehenna {
    431  1.11       ad 	const char *name;
    432   1.2  gehenna 	int cmajor, i;
    433   1.2  gehenna 
    434  1.11       ad 	name = NULL;
    435  1.11       ad 	cmajor = -1;
    436  1.11       ad 
    437  1.23    pooka 	mutex_enter(&device_lock);
    438  1.11       ad 	if (bmajor < 0 || bmajor >= max_bdevsws || bdevsw[bmajor] == NULL) {
    439  1.23    pooka 		mutex_exit(&device_lock);
    440   1.2  gehenna 		return (NULL);
    441   1.2  gehenna 	}
    442  1.11       ad 	for (i = 0 ; i < max_devsw_convs; i++) {
    443  1.11       ad 		if (devsw_conv[i].d_bmajor == bmajor) {
    444  1.11       ad 			cmajor = devsw_conv[i].d_cmajor;
    445  1.11       ad 			break;
    446  1.11       ad 		}
    447  1.11       ad 	}
    448  1.11       ad 	if (cmajor >= 0 && cmajor < max_cdevsws && cdevsw[cmajor] != NULL)
    449  1.11       ad 		name = devsw_conv[i].d_name;
    450  1.23    pooka 	mutex_exit(&device_lock);
    451   1.2  gehenna 
    452  1.11       ad 	return (name);
    453   1.2  gehenna }
    454   1.2  gehenna 
    455   1.2  gehenna /*
    456   1.2  gehenna  * Convert from device name to block major number.
    457  1.11       ad  *
    458  1.11       ad  * => Caller must ensure that the device is not detached, and therefore
    459  1.11       ad  *    that the major number is still valid when dereferenced.
    460   1.2  gehenna  */
    461   1.2  gehenna int
    462   1.2  gehenna devsw_name2blk(const char *name, char *devname, size_t devnamelen)
    463   1.2  gehenna {
    464   1.2  gehenna 	struct devsw_conv *conv;
    465   1.2  gehenna 	int bmajor, i;
    466   1.2  gehenna 
    467   1.2  gehenna 	if (name == NULL)
    468   1.2  gehenna 		return (-1);
    469   1.2  gehenna 
    470  1.23    pooka 	mutex_enter(&device_lock);
    471   1.2  gehenna 	for (i = 0 ; i < max_devsw_convs ; i++) {
    472   1.5      mrg 		size_t len;
    473   1.5      mrg 
    474   1.2  gehenna 		conv = &devsw_conv[i];
    475   1.2  gehenna 		if (conv->d_name == NULL)
    476   1.2  gehenna 			continue;
    477   1.5      mrg 		len = strlen(conv->d_name);
    478   1.5      mrg 		if (strncmp(conv->d_name, name, len) != 0)
    479   1.5      mrg 			continue;
    480   1.5      mrg 		if (*(name +len) && !isdigit(*(name + len)))
    481   1.2  gehenna 			continue;
    482   1.2  gehenna 		bmajor = conv->d_bmajor;
    483   1.2  gehenna 		if (bmajor < 0 || bmajor >= max_bdevsws ||
    484   1.2  gehenna 		    bdevsw[bmajor] == NULL)
    485   1.5      mrg 			break;
    486   1.2  gehenna 		if (devname != NULL) {
    487   1.2  gehenna #ifdef DEVSW_DEBUG
    488   1.2  gehenna 			if (strlen(conv->d_name) >= devnamelen)
    489   1.2  gehenna 				printf("devsw_name2blk: too short buffer");
    490   1.2  gehenna #endif /* DEVSW_DEBUG */
    491   1.4  tsutsui 			strncpy(devname, conv->d_name, devnamelen);
    492   1.2  gehenna 			devname[devnamelen - 1] = '\0';
    493   1.2  gehenna 		}
    494  1.23    pooka 		mutex_exit(&device_lock);
    495   1.2  gehenna 		return (bmajor);
    496   1.2  gehenna 	}
    497   1.2  gehenna 
    498  1.23    pooka 	mutex_exit(&device_lock);
    499   1.2  gehenna 	return (-1);
    500   1.2  gehenna }
    501   1.2  gehenna 
    502   1.2  gehenna /*
    503  1.16   plunky  * Convert from device name to char major number.
    504  1.16   plunky  *
    505  1.16   plunky  * => Caller must ensure that the device is not detached, and therefore
    506  1.16   plunky  *    that the major number is still valid when dereferenced.
    507  1.16   plunky  */
    508  1.16   plunky int
    509  1.16   plunky devsw_name2chr(const char *name, char *devname, size_t devnamelen)
    510  1.16   plunky {
    511  1.16   plunky 	struct devsw_conv *conv;
    512  1.16   plunky 	int cmajor, i;
    513  1.16   plunky 
    514  1.16   plunky 	if (name == NULL)
    515  1.16   plunky 		return (-1);
    516  1.16   plunky 
    517  1.23    pooka 	mutex_enter(&device_lock);
    518  1.16   plunky 	for (i = 0 ; i < max_devsw_convs ; i++) {
    519  1.16   plunky 		size_t len;
    520  1.16   plunky 
    521  1.16   plunky 		conv = &devsw_conv[i];
    522  1.16   plunky 		if (conv->d_name == NULL)
    523  1.16   plunky 			continue;
    524  1.16   plunky 		len = strlen(conv->d_name);
    525  1.16   plunky 		if (strncmp(conv->d_name, name, len) != 0)
    526  1.16   plunky 			continue;
    527  1.16   plunky 		if (*(name +len) && !isdigit(*(name + len)))
    528  1.16   plunky 			continue;
    529  1.16   plunky 		cmajor = conv->d_cmajor;
    530  1.16   plunky 		if (cmajor < 0 || cmajor >= max_cdevsws ||
    531  1.16   plunky 		    cdevsw[cmajor] == NULL)
    532  1.16   plunky 			break;
    533  1.16   plunky 		if (devname != NULL) {
    534  1.16   plunky #ifdef DEVSW_DEBUG
    535  1.16   plunky 			if (strlen(conv->d_name) >= devnamelen)
    536  1.16   plunky 				printf("devsw_name2chr: too short buffer");
    537  1.16   plunky #endif /* DEVSW_DEBUG */
    538  1.16   plunky 			strncpy(devname, conv->d_name, devnamelen);
    539  1.16   plunky 			devname[devnamelen - 1] = '\0';
    540  1.16   plunky 		}
    541  1.23    pooka 		mutex_exit(&device_lock);
    542  1.16   plunky 		return (cmajor);
    543  1.16   plunky 	}
    544  1.16   plunky 
    545  1.23    pooka 	mutex_exit(&device_lock);
    546  1.16   plunky 	return (-1);
    547  1.16   plunky }
    548  1.16   plunky 
    549  1.16   plunky /*
    550   1.2  gehenna  * Convert from character dev_t to block dev_t.
    551  1.11       ad  *
    552  1.11       ad  * => Caller must ensure that the device is not detached, and therefore
    553  1.11       ad  *    that the major number is still valid when dereferenced.
    554   1.2  gehenna  */
    555   1.2  gehenna dev_t
    556   1.2  gehenna devsw_chr2blk(dev_t cdev)
    557   1.2  gehenna {
    558   1.2  gehenna 	int bmajor, cmajor, i;
    559  1.11       ad 	dev_t rv;
    560   1.2  gehenna 
    561   1.2  gehenna 	cmajor = major(cdev);
    562  1.11       ad 	bmajor = -1;
    563  1.11       ad 	rv = NODEV;
    564   1.2  gehenna 
    565  1.23    pooka 	mutex_enter(&device_lock);
    566  1.11       ad 	if (cmajor < 0 || cmajor >= max_cdevsws || cdevsw[cmajor] == NULL) {
    567  1.23    pooka 		mutex_exit(&device_lock);
    568  1.11       ad 		return (NODEV);
    569  1.11       ad 	}
    570   1.2  gehenna 	for (i = 0 ; i < max_devsw_convs ; i++) {
    571  1.11       ad 		if (devsw_conv[i].d_cmajor == cmajor) {
    572  1.11       ad 			bmajor = devsw_conv[i].d_bmajor;
    573  1.11       ad 			break;
    574  1.11       ad 		}
    575   1.2  gehenna 	}
    576  1.11       ad 	if (bmajor >= 0 && bmajor < max_bdevsws && bdevsw[bmajor] != NULL)
    577  1.11       ad 		rv = makedev(bmajor, minor(cdev));
    578  1.23    pooka 	mutex_exit(&device_lock);
    579   1.2  gehenna 
    580  1.11       ad 	return (rv);
    581   1.2  gehenna }
    582   1.2  gehenna 
    583   1.2  gehenna /*
    584   1.2  gehenna  * Convert from block dev_t to character dev_t.
    585  1.11       ad  *
    586  1.11       ad  * => Caller must ensure that the device is not detached, and therefore
    587  1.11       ad  *    that the major number is still valid when dereferenced.
    588   1.2  gehenna  */
    589   1.2  gehenna dev_t
    590   1.2  gehenna devsw_blk2chr(dev_t bdev)
    591   1.2  gehenna {
    592   1.2  gehenna 	int bmajor, cmajor, i;
    593  1.11       ad 	dev_t rv;
    594   1.2  gehenna 
    595  1.11       ad 	bmajor = major(bdev);
    596  1.11       ad 	cmajor = -1;
    597  1.11       ad 	rv = NODEV;
    598  1.11       ad 
    599  1.23    pooka 	mutex_enter(&device_lock);
    600  1.11       ad 	if (bmajor < 0 || bmajor >= max_bdevsws || bdevsw[bmajor] == NULL) {
    601  1.23    pooka 		mutex_exit(&device_lock);
    602   1.2  gehenna 		return (NODEV);
    603  1.11       ad 	}
    604  1.11       ad 	for (i = 0 ; i < max_devsw_convs ; i++) {
    605  1.11       ad 		if (devsw_conv[i].d_bmajor == bmajor) {
    606  1.11       ad 			cmajor = devsw_conv[i].d_cmajor;
    607  1.11       ad 			break;
    608  1.11       ad 		}
    609  1.11       ad 	}
    610  1.11       ad 	if (cmajor >= 0 && cmajor < max_cdevsws && cdevsw[cmajor] != NULL)
    611  1.11       ad 		rv = makedev(cmajor, minor(bdev));
    612  1.23    pooka 	mutex_exit(&device_lock);
    613   1.2  gehenna 
    614  1.11       ad 	return (rv);
    615  1.11       ad }
    616  1.11       ad 
    617  1.11       ad /*
    618  1.11       ad  * Device access methods.
    619  1.11       ad  */
    620  1.11       ad 
    621  1.11       ad #define	DEV_LOCK(d)						\
    622  1.17       ad 	if ((mpflag = (d->d_flag & D_MPSAFE)) == 0) {		\
    623  1.17       ad 		KERNEL_LOCK(1, NULL);				\
    624  1.11       ad 	}
    625   1.2  gehenna 
    626  1.11       ad #define	DEV_UNLOCK(d)						\
    627  1.17       ad 	if (mpflag == 0) {					\
    628  1.17       ad 		KERNEL_UNLOCK_ONE(NULL);			\
    629   1.2  gehenna 	}
    630   1.2  gehenna 
    631  1.11       ad int
    632  1.11       ad bdev_open(dev_t dev, int flag, int devtype, lwp_t *l)
    633  1.11       ad {
    634  1.11       ad 	const struct bdevsw *d;
    635  1.17       ad 	int rv, mpflag;
    636  1.11       ad 
    637  1.11       ad 	/*
    638  1.11       ad 	 * For open we need to lock, in order to synchronize
    639  1.11       ad 	 * with attach/detach.
    640  1.11       ad 	 */
    641  1.23    pooka 	mutex_enter(&device_lock);
    642  1.11       ad 	d = bdevsw_lookup(dev);
    643  1.23    pooka 	mutex_exit(&device_lock);
    644  1.11       ad 	if (d == NULL)
    645  1.11       ad 		return ENXIO;
    646  1.11       ad 
    647  1.11       ad 	DEV_LOCK(d);
    648  1.11       ad 	rv = (*d->d_open)(dev, flag, devtype, l);
    649  1.11       ad 	DEV_UNLOCK(d);
    650  1.11       ad 
    651  1.11       ad 	return rv;
    652  1.11       ad }
    653  1.11       ad 
    654  1.11       ad int
    655  1.11       ad bdev_close(dev_t dev, int flag, int devtype, lwp_t *l)
    656  1.11       ad {
    657  1.11       ad 	const struct bdevsw *d;
    658  1.17       ad 	int rv, mpflag;
    659  1.11       ad 
    660  1.11       ad 	if ((d = bdevsw_lookup(dev)) == NULL)
    661  1.11       ad 		return ENXIO;
    662  1.11       ad 
    663  1.11       ad 	DEV_LOCK(d);
    664  1.11       ad 	rv = (*d->d_close)(dev, flag, devtype, l);
    665  1.11       ad 	DEV_UNLOCK(d);
    666  1.11       ad 
    667  1.11       ad 	return rv;
    668  1.11       ad }
    669  1.11       ad 
    670  1.11       ad void
    671  1.11       ad bdev_strategy(struct buf *bp)
    672  1.11       ad {
    673  1.11       ad 	const struct bdevsw *d;
    674  1.17       ad 	int mpflag;
    675  1.11       ad 
    676  1.11       ad 	if ((d = bdevsw_lookup(bp->b_dev)) == NULL)
    677  1.11       ad 		panic("bdev_strategy");
    678  1.11       ad 
    679  1.11       ad 	DEV_LOCK(d);
    680  1.11       ad 	(*d->d_strategy)(bp);
    681  1.11       ad 	DEV_UNLOCK(d);
    682  1.11       ad }
    683  1.11       ad 
    684  1.11       ad int
    685  1.11       ad bdev_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
    686  1.11       ad {
    687  1.11       ad 	const struct bdevsw *d;
    688  1.17       ad 	int rv, mpflag;
    689  1.11       ad 
    690  1.11       ad 	if ((d = bdevsw_lookup(dev)) == NULL)
    691  1.11       ad 		return ENXIO;
    692  1.11       ad 
    693  1.11       ad 	DEV_LOCK(d);
    694  1.11       ad 	rv = (*d->d_ioctl)(dev, cmd, data, flag, l);
    695  1.11       ad 	DEV_UNLOCK(d);
    696  1.11       ad 
    697  1.11       ad 	return rv;
    698  1.11       ad }
    699  1.11       ad 
    700  1.11       ad int
    701  1.11       ad bdev_dump(dev_t dev, daddr_t addr, void *data, size_t sz)
    702  1.11       ad {
    703  1.11       ad 	const struct bdevsw *d;
    704  1.11       ad 	int rv;
    705  1.11       ad 
    706  1.11       ad 	/*
    707  1.11       ad 	 * Dump can be called without the device open.  Since it can
    708  1.11       ad 	 * currently only be called with the system paused (and in a
    709  1.11       ad 	 * potentially unstable state), we don't perform any locking.
    710  1.11       ad 	 */
    711  1.11       ad 	if ((d = bdevsw_lookup(dev)) == NULL)
    712  1.11       ad 		return ENXIO;
    713  1.11       ad 
    714  1.11       ad 	/* DEV_LOCK(d); */
    715  1.11       ad 	rv = (*d->d_dump)(dev, addr, data, sz);
    716  1.11       ad 	/* DEV_UNLOCK(d); */
    717  1.11       ad 
    718  1.11       ad 	return rv;
    719  1.11       ad }
    720  1.11       ad 
    721  1.11       ad int
    722  1.11       ad bdev_type(dev_t dev)
    723  1.11       ad {
    724  1.11       ad 	const struct bdevsw *d;
    725  1.11       ad 
    726  1.11       ad 	if ((d = bdevsw_lookup(dev)) == NULL)
    727  1.11       ad 		return D_OTHER;
    728  1.11       ad 	return d->d_flag & D_TYPEMASK;
    729  1.11       ad }
    730  1.11       ad 
    731  1.11       ad int
    732  1.11       ad cdev_open(dev_t dev, int flag, int devtype, lwp_t *l)
    733  1.11       ad {
    734  1.11       ad 	const struct cdevsw *d;
    735  1.17       ad 	int rv, mpflag;
    736  1.11       ad 
    737  1.11       ad 	/*
    738  1.11       ad 	 * For open we need to lock, in order to synchronize
    739  1.11       ad 	 * with attach/detach.
    740  1.11       ad 	 */
    741  1.23    pooka 	mutex_enter(&device_lock);
    742  1.11       ad 	d = cdevsw_lookup(dev);
    743  1.23    pooka 	mutex_exit(&device_lock);
    744  1.11       ad 	if (d == NULL)
    745  1.11       ad 		return ENXIO;
    746  1.11       ad 
    747  1.11       ad 	DEV_LOCK(d);
    748  1.11       ad 	rv = (*d->d_open)(dev, flag, devtype, l);
    749  1.11       ad 	DEV_UNLOCK(d);
    750  1.11       ad 
    751  1.11       ad 	return rv;
    752  1.11       ad }
    753  1.11       ad 
    754  1.11       ad int
    755  1.11       ad cdev_close(dev_t dev, int flag, int devtype, lwp_t *l)
    756  1.11       ad {
    757  1.11       ad 	const struct cdevsw *d;
    758  1.17       ad 	int rv, mpflag;
    759  1.11       ad 
    760  1.11       ad 	if ((d = cdevsw_lookup(dev)) == NULL)
    761  1.11       ad 		return ENXIO;
    762  1.11       ad 
    763  1.11       ad 	DEV_LOCK(d);
    764  1.11       ad 	rv = (*d->d_close)(dev, flag, devtype, l);
    765  1.11       ad 	DEV_UNLOCK(d);
    766  1.11       ad 
    767  1.11       ad 	return rv;
    768  1.11       ad }
    769  1.11       ad 
    770  1.11       ad int
    771  1.11       ad cdev_read(dev_t dev, struct uio *uio, int flag)
    772  1.11       ad {
    773  1.11       ad 	const struct cdevsw *d;
    774  1.17       ad 	int rv, mpflag;
    775  1.11       ad 
    776  1.11       ad 	if ((d = cdevsw_lookup(dev)) == NULL)
    777  1.11       ad 		return ENXIO;
    778  1.11       ad 
    779  1.11       ad 	DEV_LOCK(d);
    780  1.11       ad 	rv = (*d->d_read)(dev, uio, flag);
    781  1.11       ad 	DEV_UNLOCK(d);
    782  1.11       ad 
    783  1.11       ad 	return rv;
    784  1.11       ad }
    785  1.11       ad 
    786  1.11       ad int
    787  1.11       ad cdev_write(dev_t dev, struct uio *uio, int flag)
    788  1.11       ad {
    789  1.11       ad 	const struct cdevsw *d;
    790  1.17       ad 	int rv, mpflag;
    791  1.11       ad 
    792  1.11       ad 	if ((d = cdevsw_lookup(dev)) == NULL)
    793  1.11       ad 		return ENXIO;
    794  1.11       ad 
    795  1.11       ad 	DEV_LOCK(d);
    796  1.11       ad 	rv = (*d->d_write)(dev, uio, flag);
    797  1.11       ad 	DEV_UNLOCK(d);
    798  1.11       ad 
    799  1.11       ad 	return rv;
    800  1.11       ad }
    801  1.11       ad 
    802  1.11       ad int
    803  1.11       ad cdev_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
    804  1.11       ad {
    805  1.11       ad 	const struct cdevsw *d;
    806  1.17       ad 	int rv, mpflag;
    807  1.11       ad 
    808  1.11       ad 	if ((d = cdevsw_lookup(dev)) == NULL)
    809  1.11       ad 		return ENXIO;
    810  1.11       ad 
    811  1.11       ad 	DEV_LOCK(d);
    812  1.11       ad 	rv = (*d->d_ioctl)(dev, cmd, data, flag, l);
    813  1.11       ad 	DEV_UNLOCK(d);
    814  1.11       ad 
    815  1.11       ad 	return rv;
    816  1.11       ad }
    817  1.11       ad 
    818  1.11       ad void
    819  1.11       ad cdev_stop(struct tty *tp, int flag)
    820  1.11       ad {
    821  1.11       ad 	const struct cdevsw *d;
    822  1.17       ad 	int mpflag;
    823  1.11       ad 
    824  1.11       ad 	if ((d = cdevsw_lookup(tp->t_dev)) == NULL)
    825  1.11       ad 		return;
    826  1.11       ad 
    827  1.11       ad 	DEV_LOCK(d);
    828  1.11       ad 	(*d->d_stop)(tp, flag);
    829  1.11       ad 	DEV_UNLOCK(d);
    830  1.11       ad }
    831  1.11       ad 
    832  1.11       ad struct tty *
    833  1.11       ad cdev_tty(dev_t dev)
    834  1.11       ad {
    835  1.11       ad 	const struct cdevsw *d;
    836  1.11       ad 
    837  1.11       ad 	if ((d = cdevsw_lookup(dev)) == NULL)
    838  1.11       ad 		return NULL;
    839  1.11       ad 
    840  1.12       ad 	/* XXX Check if necessary. */
    841  1.12       ad 	if (d->d_tty == NULL)
    842  1.12       ad 		return NULL;
    843  1.12       ad 
    844  1.21       ad 	return (*d->d_tty)(dev);
    845  1.11       ad }
    846  1.11       ad 
    847  1.11       ad int
    848  1.11       ad cdev_poll(dev_t dev, int flag, lwp_t *l)
    849  1.11       ad {
    850  1.11       ad 	const struct cdevsw *d;
    851  1.17       ad 	int rv, mpflag;
    852  1.11       ad 
    853  1.11       ad 	if ((d = cdevsw_lookup(dev)) == NULL)
    854  1.11       ad 		return POLLERR;
    855  1.11       ad 
    856  1.11       ad 	DEV_LOCK(d);
    857  1.11       ad 	rv = (*d->d_poll)(dev, flag, l);
    858  1.11       ad 	DEV_UNLOCK(d);
    859  1.11       ad 
    860  1.11       ad 	return rv;
    861  1.11       ad }
    862  1.11       ad 
    863  1.11       ad paddr_t
    864  1.11       ad cdev_mmap(dev_t dev, off_t off, int flag)
    865  1.11       ad {
    866  1.11       ad 	const struct cdevsw *d;
    867  1.11       ad 	paddr_t rv;
    868  1.17       ad 	int mpflag;
    869  1.11       ad 
    870  1.11       ad 	if ((d = cdevsw_lookup(dev)) == NULL)
    871  1.11       ad 		return (paddr_t)-1LL;
    872  1.11       ad 
    873  1.11       ad 	DEV_LOCK(d);
    874  1.11       ad 	rv = (*d->d_mmap)(dev, off, flag);
    875  1.11       ad 	DEV_UNLOCK(d);
    876  1.11       ad 
    877  1.11       ad 	return rv;
    878  1.11       ad }
    879  1.11       ad 
    880  1.11       ad int
    881  1.11       ad cdev_kqfilter(dev_t dev, struct knote *kn)
    882  1.11       ad {
    883  1.11       ad 	const struct cdevsw *d;
    884  1.17       ad 	int rv, mpflag;
    885  1.11       ad 
    886  1.11       ad 	if ((d = cdevsw_lookup(dev)) == NULL)
    887  1.11       ad 		return ENXIO;
    888  1.11       ad 
    889  1.11       ad 	DEV_LOCK(d);
    890  1.11       ad 	rv = (*d->d_kqfilter)(dev, kn);
    891  1.11       ad 	DEV_UNLOCK(d);
    892  1.11       ad 
    893  1.11       ad 	return rv;
    894  1.11       ad }
    895  1.11       ad 
    896  1.11       ad int
    897  1.11       ad cdev_type(dev_t dev)
    898  1.11       ad {
    899  1.11       ad 	const struct cdevsw *d;
    900  1.11       ad 
    901  1.11       ad 	if ((d = cdevsw_lookup(dev)) == NULL)
    902  1.11       ad 		return D_OTHER;
    903  1.11       ad 	return d->d_flag & D_TYPEMASK;
    904   1.2  gehenna }
    905