Home | History | Annotate | Line # | Download | only in dev
bio.c revision 1.1.14.2
      1  1.1.14.2     joerg /*	$NetBSD: bio.c,v 1.1.14.2 2007/11/21 21:54:01 joerg Exp $ */
      2       1.1    bouyer /*	$OpenBSD: bio.c,v 1.9 2007/03/20 02:35:55 marco Exp $	*/
      3       1.1    bouyer 
      4       1.1    bouyer /*
      5       1.1    bouyer  * Copyright (c) 2002 Niklas Hallqvist.  All rights reserved.
      6       1.1    bouyer  *
      7       1.1    bouyer  * Redistribution and use in source and binary forms, with or without
      8       1.1    bouyer  * modification, are permitted provided that the following conditions
      9       1.1    bouyer  * are met:
     10       1.1    bouyer  * 1. Redistributions of source code must retain the above copyright
     11       1.1    bouyer  *    notice, this list of conditions and the following disclaimer.
     12       1.1    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1    bouyer  *    notice, this list of conditions and the following disclaimer in the
     14       1.1    bouyer  *    documentation and/or other materials provided with the distribution.
     15       1.1    bouyer  *
     16       1.1    bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17       1.1    bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18       1.1    bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19       1.1    bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20       1.1    bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21       1.1    bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22       1.1    bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23       1.1    bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24       1.1    bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25       1.1    bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26       1.1    bouyer  */
     27       1.1    bouyer 
     28       1.1    bouyer /* A device controller ioctl tunnelling device.  */
     29       1.1    bouyer 
     30       1.1    bouyer #include <sys/cdefs.h>
     31  1.1.14.2     joerg __KERNEL_RCSID(0, "$NetBSD: bio.c,v 1.1.14.2 2007/11/21 21:54:01 joerg Exp $");
     32       1.1    bouyer 
     33       1.1    bouyer #include <sys/param.h>
     34       1.1    bouyer #include <sys/conf.h>
     35       1.1    bouyer #include <sys/device.h>
     36       1.1    bouyer #include <sys/event.h>
     37       1.1    bouyer #include <sys/ioctl.h>
     38       1.1    bouyer #include <sys/malloc.h>
     39       1.1    bouyer #include <sys/queue.h>
     40       1.1    bouyer #include <sys/systm.h>
     41       1.1    bouyer #include <sys/mutex.h>
     42       1.1    bouyer #include <sys/proc.h>
     43       1.1    bouyer #include <sys/kauth.h>
     44       1.1    bouyer 
     45       1.1    bouyer #include <dev/biovar.h>
     46       1.1    bouyer 
     47       1.1    bouyer struct bio_mapping {
     48       1.1    bouyer 	LIST_ENTRY(bio_mapping) bm_link;
     49       1.1    bouyer 	struct device *bm_dev;
     50       1.1    bouyer 	int (*bm_ioctl)(struct device *, u_long, void *);
     51       1.1    bouyer };
     52       1.1    bouyer 
     53       1.1    bouyer LIST_HEAD(, bio_mapping) bios = LIST_HEAD_INITIALIZER(bios);
     54       1.1    bouyer static kmutex_t bio_lock;
     55  1.1.14.2     joerg static bool bio_lock_initialized = false;
     56       1.1    bouyer 
     57  1.1.14.2     joerg static void bio_initialize(void);
     58       1.1    bouyer void	bioattach(int);
     59       1.1    bouyer int	bioclose(dev_t, int, int, struct lwp *);
     60       1.1    bouyer int	bioioctl(dev_t, u_long, void *, int, struct lwp *);
     61       1.1    bouyer int	bioopen(dev_t, int, int, struct lwp *);
     62       1.1    bouyer 
     63       1.1    bouyer int	bio_delegate_ioctl(struct bio_mapping *, u_long, void *);
     64       1.1    bouyer struct	bio_mapping *bio_lookup(char *);
     65       1.1    bouyer int	bio_validate(void *);
     66       1.1    bouyer 
     67       1.1    bouyer const struct cdevsw bio_cdevsw = {
     68       1.1    bouyer         bioopen, bioclose, noread, nowrite, bioioctl,
     69       1.1    bouyer         nostop, notty, nopoll, nommap, nokqfilter, 0
     70       1.1    bouyer };
     71       1.1    bouyer 
     72       1.1    bouyer 
     73       1.1    bouyer void
     74  1.1.14.2     joerg bio_initialize(void)
     75       1.1    bouyer {
     76  1.1.14.2     joerg 	if (bio_lock_initialized)
     77  1.1.14.2     joerg 		return;
     78  1.1.14.2     joerg 
     79       1.1    bouyer 	mutex_init(&bio_lock, MUTEX_DRIVER, IPL_BIO);
     80  1.1.14.2     joerg 	bio_lock_initialized = true;
     81  1.1.14.2     joerg }
     82  1.1.14.2     joerg 
     83  1.1.14.2     joerg void
     84  1.1.14.2     joerg bioattach(int nunits)
     85  1.1.14.2     joerg {
     86  1.1.14.2     joerg 	if (!bio_lock_initialized)
     87  1.1.14.2     joerg 		bio_initialize();
     88       1.1    bouyer }
     89       1.1    bouyer 
     90       1.1    bouyer int
     91       1.1    bouyer bioopen(dev_t dev, int flags, int mode, struct lwp *l)
     92       1.1    bouyer {
     93  1.1.14.1  jmcneill 	return 0;
     94       1.1    bouyer }
     95       1.1    bouyer 
     96       1.1    bouyer int
     97       1.1    bouyer bioclose(dev_t dev, int flags, int mode, struct lwp *l)
     98       1.1    bouyer {
     99  1.1.14.1  jmcneill 	return 0;
    100       1.1    bouyer }
    101       1.1    bouyer 
    102       1.1    bouyer int
    103       1.1    bouyer bioioctl(dev_t dev, u_long cmd, void *addr, int flag, struct  lwp *l)
    104       1.1    bouyer {
    105       1.1    bouyer 	struct bio_locate *locate;
    106       1.1    bouyer 	struct bio_common *common;
    107       1.1    bouyer 	char name[16];
    108       1.1    bouyer 	int error;
    109       1.1    bouyer 
    110       1.1    bouyer 	switch(cmd) {
    111       1.1    bouyer 	case BIOCLOCATE:
    112       1.1    bouyer 	case BIOCINQ:
    113       1.1    bouyer 	case BIOCDISK:
    114       1.1    bouyer 	case BIOCVOL:
    115       1.1    bouyer 		error = kauth_authorize_device_passthru(l->l_cred, dev,
    116       1.1    bouyer 		    KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READCONF, addr);
    117       1.1    bouyer 		if (error)
    118  1.1.14.1  jmcneill 			return error;
    119       1.1    bouyer 		break;
    120       1.1    bouyer 	case BIOCBLINK:
    121       1.1    bouyer 	case BIOCSETSTATE:
    122       1.1    bouyer 		error = kauth_authorize_device_passthru(l->l_cred, dev,
    123       1.1    bouyer 		    KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITECONF, addr);
    124       1.1    bouyer 		if (error)
    125  1.1.14.1  jmcneill 			return error;
    126       1.1    bouyer 		break;
    127       1.1    bouyer 	case BIOCALARM: {
    128       1.1    bouyer 		struct bioc_alarm *alarm = (struct bioc_alarm *)addr;
    129       1.1    bouyer 		switch (alarm->ba_opcode) {
    130       1.1    bouyer 		case BIOC_SADISABLE:
    131       1.1    bouyer 		case BIOC_SAENABLE:
    132       1.1    bouyer 		case BIOC_SASILENCE:
    133       1.1    bouyer 		case BIOC_SATEST:
    134       1.1    bouyer 			error = kauth_authorize_device_passthru(l->l_cred, dev,
    135       1.1    bouyer 			    KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITECONF, addr);
    136       1.1    bouyer 			if (error)
    137  1.1.14.1  jmcneill 				return error;
    138       1.1    bouyer 			break;
    139       1.1    bouyer 		case BIOC_GASTATUS:
    140       1.1    bouyer 			error = kauth_authorize_device_passthru(l->l_cred, dev,
    141       1.1    bouyer 			    KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READCONF, addr);
    142       1.1    bouyer 			if (error)
    143  1.1.14.1  jmcneill 				return error;
    144       1.1    bouyer 			break;
    145       1.1    bouyer 		default:
    146       1.1    bouyer 			return EINVAL;
    147       1.1    bouyer 		}
    148       1.1    bouyer 		break;
    149       1.1    bouyer 	}
    150       1.1    bouyer 	default:
    151       1.1    bouyer 		return ENOTTY;
    152       1.1    bouyer 	}
    153       1.1    bouyer 
    154       1.1    bouyer 	switch (cmd) {
    155       1.1    bouyer 	case BIOCLOCATE:
    156       1.1    bouyer 		locate = (struct bio_locate *)addr;
    157       1.1    bouyer 		error = copyinstr(locate->bl_name, name, 16, NULL);
    158       1.1    bouyer 		if (error != 0)
    159  1.1.14.1  jmcneill 			return error;
    160       1.1    bouyer 		locate->bl_cookie = bio_lookup(name);
    161       1.1    bouyer 		if (locate->bl_cookie == NULL)
    162  1.1.14.1  jmcneill 			return ENOENT;
    163       1.1    bouyer 		break;
    164       1.1    bouyer 
    165       1.1    bouyer 	default:
    166       1.1    bouyer 		common = (struct bio_common *)addr;
    167       1.1    bouyer 		mutex_enter(&bio_lock);
    168       1.1    bouyer 		if (!bio_validate(common->bc_cookie)) {
    169       1.1    bouyer 			mutex_exit(&bio_lock);
    170  1.1.14.1  jmcneill 			return ENOENT;
    171       1.1    bouyer 		}
    172       1.1    bouyer 		mutex_exit(&bio_lock);
    173  1.1.14.1  jmcneill 		error = bio_delegate_ioctl(
    174       1.1    bouyer 		    (struct bio_mapping *)common->bc_cookie, cmd, addr);
    175  1.1.14.1  jmcneill 		return error;
    176       1.1    bouyer 	}
    177  1.1.14.1  jmcneill 	return 0;
    178       1.1    bouyer }
    179       1.1    bouyer 
    180       1.1    bouyer int
    181       1.1    bouyer bio_register(struct device *dev, int (*ioctl)(struct device *, u_long, void *))
    182       1.1    bouyer {
    183       1.1    bouyer 	struct bio_mapping *bm;
    184       1.1    bouyer 
    185  1.1.14.2     joerg 	if (!bio_lock_initialized)
    186  1.1.14.2     joerg 		bio_initialize();
    187  1.1.14.2     joerg 
    188  1.1.14.1  jmcneill 	bm = (struct bio_mapping *)malloc(sizeof(*bm), M_DEVBUF,
    189  1.1.14.1  jmcneill 	    M_NOWAIT|M_ZERO);
    190       1.1    bouyer 	if (bm == NULL)
    191  1.1.14.1  jmcneill 		return ENOMEM;
    192       1.1    bouyer 	bm->bm_dev = dev;
    193       1.1    bouyer 	bm->bm_ioctl = ioctl;
    194       1.1    bouyer 	mutex_enter(&bio_lock);
    195       1.1    bouyer 	LIST_INSERT_HEAD(&bios, bm, bm_link);
    196       1.1    bouyer 	mutex_exit(&bio_lock);
    197  1.1.14.1  jmcneill 	return 0;
    198       1.1    bouyer }
    199       1.1    bouyer 
    200       1.1    bouyer void
    201       1.1    bouyer bio_unregister(struct device *dev)
    202       1.1    bouyer {
    203       1.1    bouyer 	struct bio_mapping *bm, *next;
    204       1.1    bouyer 
    205       1.1    bouyer 	mutex_enter(&bio_lock);
    206       1.1    bouyer 	for (bm = LIST_FIRST(&bios); bm != NULL; bm = next) {
    207       1.1    bouyer 		next = LIST_NEXT(bm, bm_link);
    208       1.1    bouyer 
    209       1.1    bouyer 		if (dev == bm->bm_dev) {
    210       1.1    bouyer 			LIST_REMOVE(bm, bm_link);
    211       1.1    bouyer 			free(bm, M_DEVBUF);
    212       1.1    bouyer 		}
    213       1.1    bouyer 	}
    214       1.1    bouyer 	mutex_exit(&bio_lock);
    215       1.1    bouyer }
    216       1.1    bouyer 
    217       1.1    bouyer struct bio_mapping *
    218       1.1    bouyer bio_lookup(char *name)
    219       1.1    bouyer {
    220       1.1    bouyer 	struct bio_mapping *bm;
    221       1.1    bouyer 
    222       1.1    bouyer 	mutex_enter(&bio_lock);
    223       1.1    bouyer 	LIST_FOREACH(bm, &bios, bm_link) {
    224       1.1    bouyer 		if (strcmp(name, bm->bm_dev->dv_xname) == 0) {
    225       1.1    bouyer 			mutex_exit(&bio_lock);
    226  1.1.14.1  jmcneill 			return bm;
    227       1.1    bouyer 		}
    228       1.1    bouyer 	}
    229       1.1    bouyer 	mutex_exit(&bio_lock);
    230  1.1.14.1  jmcneill 	return NULL;
    231       1.1    bouyer }
    232       1.1    bouyer 
    233       1.1    bouyer int
    234       1.1    bouyer bio_validate(void *cookie)
    235       1.1    bouyer {
    236       1.1    bouyer 	struct bio_mapping *bm;
    237       1.1    bouyer 
    238  1.1.14.1  jmcneill 	LIST_FOREACH(bm, &bios, bm_link)
    239  1.1.14.1  jmcneill 		if (bm == cookie)
    240  1.1.14.1  jmcneill 			return 1;
    241  1.1.14.1  jmcneill 
    242  1.1.14.1  jmcneill 	return 0;
    243       1.1    bouyer }
    244       1.1    bouyer 
    245       1.1    bouyer int
    246       1.1    bouyer bio_delegate_ioctl(struct bio_mapping *bm, u_long cmd, void *addr)
    247       1.1    bouyer {
    248       1.1    bouyer 
    249  1.1.14.1  jmcneill 	return bm->bm_ioctl(bm->bm_dev, cmd, addr);
    250       1.1    bouyer }
    251