Home | History | Annotate | Line # | Download | only in pud
pud_dev.c revision 1.3
      1 /*	$NetBSD: pud_dev.c,v 1.3 2007/11/21 18:10:48 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2007  Antti Kantee.  All Rights Reserved.
      5  *
      6  * Development of this software was supported by the
      7  * Research Foundation of Helsinki University of Technology
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: pud_dev.c,v 1.3 2007/11/21 18:10:48 pooka Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/buf.h>
     36 #include <sys/conf.h>
     37 #include <sys/event.h>
     38 #include <sys/kmem.h>
     39 #include <sys/poll.h>
     40 #include <sys/socketvar.h>
     41 
     42 #include <dev/pud/pud_sys.h>
     43 
     44 static int
     45 openclose(dev_t dev, int flags, int fmt, int class, int type)
     46 {
     47 	struct pud_req_openclose pc_oc; /* XXX: stack = stupid */
     48 
     49 	pc_oc.pm_flags = flags;
     50 	pc_oc.pm_fmt = fmt;
     51 
     52 	return pud_request(dev, &pc_oc, sizeof(pc_oc), class, type);
     53 }
     54 
     55 /*
     56  * Block de-vices
     57  */
     58 
     59 static dev_type_open(pud_bdev_open);
     60 static dev_type_close(pud_bdev_close);
     61 static dev_type_strategy(pud_bdev_strategy);
     62 static dev_type_ioctl(pud_bdev_ioctl);
     63 #if 0
     64 static dev_type_dump(pud_bdev_dump);
     65 static dev_type_size(pud_bdev_size);
     66 #endif
     67 
     68 struct bdevsw pud_bdevsw = {
     69 	.d_open		= pud_bdev_open,
     70 	.d_close	= pud_bdev_close,
     71 	.d_strategy	= pud_bdev_strategy,
     72 	.d_ioctl	= pud_bdev_ioctl,
     73 #if 0
     74 	.d_dump		= pud_bdev_dump,
     75 	.d_psize	= pud_bdev_size,
     76 #endif
     77 };
     78 
     79 static int
     80 pud_bdev_open(dev_t dev, int flags, int fmt, lwp_t *l)
     81 {
     82 
     83 	return openclose(dev, flags, fmt, PUD_REQ_BDEV, PUD_BDEV_OPEN);
     84 }
     85 
     86 static int
     87 pud_bdev_close(dev_t dev, int flags, int fmt, lwp_t *l)
     88 {
     89 
     90 	return openclose(dev, flags, fmt, PUD_REQ_BDEV, PUD_BDEV_CLOSE);
     91 }
     92 
     93 static void
     94 pud_bdev_strategy(struct buf *bp)
     95 {
     96 	struct pud_req_readwrite *pc_rw;
     97 	size_t allocsize;
     98 	int error;
     99 
    100 	allocsize = sizeof(struct pud_req_readwrite) + bp->b_bcount;
    101 	pc_rw = kmem_zalloc(allocsize, KM_SLEEP);
    102 
    103 	pc_rw->pm_offset = bp->b_blkno << DEV_BSHIFT;
    104 	pc_rw->pm_resid = bp->b_bcount;
    105 
    106 	if (BUF_ISWRITE(bp))
    107 		memcpy(pc_rw->pm_data, bp->b_data, bp->b_bcount);
    108 
    109 	error = pud_request(bp->b_dev, pc_rw, allocsize, PUD_REQ_BDEV,
    110 	    BUF_ISREAD(bp) ? PUD_BDEV_STRATREAD : PUD_BDEV_STRATWRITE);
    111 	if (error)
    112 		goto out;
    113 
    114 	if (pc_rw->pm_resid > bp->b_bcount) {
    115 		error = EINVAL;
    116 		goto out;
    117 	}
    118 
    119 	if (BUF_ISREAD(bp))
    120 		memcpy(bp->b_data,pc_rw->pm_data,bp->b_bcount-pc_rw->pm_resid);
    121 
    122 	bp->b_resid = pc_rw->pm_resid;
    123 
    124  out:
    125 	kmem_free(pc_rw, allocsize);
    126 	bp->b_error = error;
    127 	biodone(bp);
    128 }
    129 
    130 int
    131 pud_bdev_ioctl(dev_t dev, u_long cmd, void *data, int dlen, lwp_t *l)
    132 {
    133 
    134 	return EOPNOTSUPP;
    135 }
    136 
    137 /* hnmmm */
    138 #if 0
    139 int
    140 pud_bdev_dump(dev_t dev, daddr_t addr, void *data, size_t sz)
    141 {
    142 
    143 	return EOPNOTSUPP;
    144 }
    145 
    146 int
    147 pud_bdev_size(dev_t dev)
    148 {
    149 
    150 	return 0;
    151 }
    152 #endif
    153 
    154 /*
    155  * Charrr devices
    156  */
    157 
    158 static dev_type_open(pud_cdev_open);
    159 static dev_type_close(pud_cdev_close);
    160 static dev_type_read(pud_cdev_read);
    161 static dev_type_write(pud_cdev_write);
    162 static dev_type_ioctl(pud_cdev_ioctl);
    163 static dev_type_poll(pud_cdev_poll);
    164 static dev_type_mmap(pud_cdev_mmap);
    165 static dev_type_kqfilter(pud_cdev_kqfilter);
    166 
    167 struct cdevsw pud_cdevsw = {
    168 	.d_open		= pud_cdev_open,
    169 	.d_close	= pud_cdev_close,
    170 	.d_read		= pud_cdev_read,
    171 	.d_write	= pud_cdev_write,
    172 	.d_ioctl	= pud_cdev_ioctl,
    173 #if 0
    174 	.d_stop		= pud_cdev_stop,
    175 	.d_tty		= pud_cdev_tty,
    176 #endif
    177 	.d_poll		= pud_cdev_poll,
    178 	.d_mmap		= pud_cdev_mmap,
    179 	.d_kqfilter	= pud_cdev_kqfilter,
    180 	.d_flag		= D_OTHER,
    181 };
    182 
    183 static int
    184 pud_cdev_open(dev_t dev, int flags, int fmt, lwp_t *l)
    185 {
    186 
    187 	return openclose(dev, flags, fmt, PUD_REQ_CDEV, PUD_CDEV_OPEN);
    188 }
    189 
    190 static int
    191 pud_cdev_close(dev_t dev, int flags, int fmt, lwp_t *l)
    192 {
    193 
    194 	return openclose(dev, flags, fmt, PUD_REQ_CDEV, PUD_CDEV_OPEN);
    195 }
    196 
    197 static int
    198 pud_cdev_read(dev_t dev, struct uio *uio, int flag)
    199 {
    200 	struct pud_creq_read *pc_read;
    201 	size_t allocsize;
    202 	int error;
    203 
    204 	allocsize = sizeof(struct pud_creq_read) + uio->uio_resid;
    205 	pc_read = kmem_zalloc(allocsize, KM_SLEEP);
    206 
    207 	pc_read->pm_offset = uio->uio_offset;
    208 	pc_read->pm_resid = uio->uio_resid;
    209 
    210 	error = pud_request(dev, pc_read, allocsize,
    211 	    PUD_REQ_CDEV, PUD_CDEV_READ);
    212 	if (error)
    213 		goto out;
    214 
    215 	if (pc_read->pm_resid > uio->uio_resid) {
    216 		error = EINVAL;
    217 		goto out;
    218 	}
    219 
    220 	error = uiomove(pc_read->pm_data,
    221 	    uio->uio_resid - pc_read->pm_resid, uio);
    222 
    223  out:
    224 	kmem_free(pc_read, allocsize);
    225 	return error;
    226 }
    227 
    228 static int
    229 pud_cdev_write(dev_t dev, struct uio *uio, int flag)
    230 {
    231 	struct pud_creq_write *pc_write;
    232 	size_t allocsize;
    233 	int error;
    234 
    235 	allocsize = sizeof(struct pud_creq_write) + uio->uio_resid;
    236 	pc_write = kmem_zalloc(allocsize, KM_SLEEP);
    237 
    238 	pc_write->pm_offset = uio->uio_offset;
    239 	pc_write->pm_resid = uio->uio_resid;
    240 
    241 	error = uiomove(pc_write->pm_data, uio->uio_resid, uio);
    242 	if (error)
    243 		goto out;
    244 
    245 	error = pud_request(dev, pc_write, allocsize,
    246 	    PUD_REQ_CDEV, PUD_CDEV_WRITE);
    247 	if (error)
    248 		goto out;
    249 
    250 	if (pc_write->pm_resid)
    251 		error = EIO;
    252 
    253  out:
    254 	kmem_free(pc_write, allocsize);
    255 	return error;
    256 }
    257 
    258 static int
    259 pud_cdev_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
    260 {
    261 
    262 	return EOPNOTSUPP;
    263 }
    264 
    265 static paddr_t
    266 pud_cdev_mmap(dev_t dev, off_t off, int flag)
    267 {
    268 
    269 	return (paddr_t)-1;
    270 }
    271 
    272 static int
    273 pud_cdev_poll(dev_t dev, int flag, lwp_t *l)
    274 {
    275 
    276 	return EOPNOTSUPP;
    277 }
    278 
    279 static int
    280 pud_cdev_kqfilter(dev_t dev, struct knote *kn)
    281 {
    282 
    283 	return EOPNOTSUPP;
    284 }
    285