Home | History | Annotate | Line # | Download | only in pud
pud_dev.c revision 1.7.2.2
      1 /*	$NetBSD: pud_dev.c,v 1.7.2.2 2016/07/18 22:00:05 pgoyette 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.7.2.2 2016/07/18 22:00:05 pgoyette 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/ioccom.h>
     39 #include <sys/kmem.h>
     40 #include <sys/poll.h>
     41 #include <sys/socketvar.h>
     42 #include <sys/localcount.h>
     43 
     44 #include <dev/pud/pud_sys.h>
     45 
     46 /*
     47  * b/c independent helpers
     48  */
     49 
     50 static int
     51 doopenclose(dev_t dev, int flags, int fmt, int class, int type)
     52 {
     53 	struct pud_req_openclose pc_oc; /* XXX: stack = stupid */
     54 
     55 	pc_oc.pm_flags = flags;
     56 	pc_oc.pm_fmt = fmt;
     57 
     58 	return pud_request(dev, &pc_oc, sizeof(pc_oc), class, type);
     59 }
     60 
     61 #include <sys/disklabel.h>
     62 static int
     63 doioctl(dev_t dev, u_long cmd, void *data, int flag, int class, int type)
     64 {
     65 	struct pud_req_ioctl *pc_ioctl;
     66 	size_t dlen, allocsize;
     67 	int error;
     68 
     69 	dlen = IOCPARM_LEN(cmd);
     70 	allocsize = sizeof(struct pud_req_ioctl) + dlen;
     71 	pc_ioctl = kmem_zalloc(allocsize, KM_SLEEP);
     72 
     73 	pc_ioctl->pm_iocmd = cmd;
     74 	pc_ioctl->pm_flag = flag;
     75 
     76 	if (cmd & IOC_IN)
     77 		memcpy(pc_ioctl->pm_data, data, dlen);
     78 	error = pud_request(dev, pc_ioctl, allocsize, class, type);
     79 	if (error)
     80 		goto out;
     81 	if (cmd & IOC_OUT)
     82 		memcpy(data, pc_ioctl->pm_data, dlen);
     83 
     84  out:
     85 	kmem_free(pc_ioctl, allocsize);
     86 	return error;
     87 }
     88 
     89 /*
     90  * Block de-vices
     91  */
     92 
     93 static dev_type_open(pud_bdev_open);
     94 static dev_type_close(pud_bdev_close);
     95 static dev_type_strategy(pud_bdev_strategy);
     96 static dev_type_ioctl(pud_bdev_ioctl);
     97 #if 0
     98 static dev_type_dump(pud_bdev_dump);
     99 static dev_type_size(pud_bdev_size);
    100 #endif
    101 
    102 #ifdef _MODULE
    103 struct localcount pud_b_localcount, pud_c_localcount;
    104 #endif
    105 
    106 struct bdevsw pud_bdevsw = {
    107 	.d_open		= pud_bdev_open,
    108 	.d_close	= pud_bdev_close,
    109 	.d_strategy	= pud_bdev_strategy,
    110 	.d_ioctl	= pud_bdev_ioctl,
    111 #if 0
    112 	.d_dump		= pud_bdev_dump,
    113 	.d_psize	= pud_bdev_size,
    114 #endif
    115 #ifdef _MODULE
    116 	.d_localcount	= &pud_b_localcount,
    117 #endif
    118 };
    119 
    120 static int
    121 pud_bdev_open(dev_t dev, int flags, int fmt, lwp_t *l)
    122 {
    123 
    124 	return doopenclose(dev, flags, fmt, PUD_REQ_BDEV, PUD_BDEV_OPEN);
    125 }
    126 
    127 static int
    128 pud_bdev_close(dev_t dev, int flags, int fmt, lwp_t *l)
    129 {
    130 
    131 	return doopenclose(dev, flags, fmt, PUD_REQ_BDEV, PUD_BDEV_CLOSE);
    132 }
    133 
    134 static void
    135 pud_bdev_strategy(struct buf *bp)
    136 {
    137 	struct pud_req_readwrite *pc_rw;
    138 	size_t allocsize;
    139 	int error;
    140 
    141 	allocsize = sizeof(struct pud_req_readwrite) + bp->b_bcount;
    142 	pc_rw = kmem_zalloc(allocsize, KM_SLEEP);
    143 
    144 	pc_rw->pm_offset = bp->b_blkno << DEV_BSHIFT;
    145 	pc_rw->pm_resid = bp->b_bcount;
    146 
    147 	if (BUF_ISWRITE(bp))
    148 		memcpy(pc_rw->pm_data, bp->b_data, bp->b_bcount);
    149 
    150 	error = pud_request(bp->b_dev, pc_rw, allocsize, PUD_REQ_BDEV,
    151 	    BUF_ISREAD(bp) ? PUD_BDEV_STRATREAD : PUD_BDEV_STRATWRITE);
    152 	if (error)
    153 		goto out;
    154 
    155 	if (pc_rw->pm_resid > bp->b_bcount) {
    156 		error = EINVAL;
    157 		goto out;
    158 	}
    159 
    160 	if (BUF_ISREAD(bp))
    161 		memcpy(bp->b_data,pc_rw->pm_data,bp->b_bcount-pc_rw->pm_resid);
    162 
    163 	bp->b_resid = pc_rw->pm_resid;
    164 
    165  out:
    166 	kmem_free(pc_rw, allocsize);
    167 	bp->b_error = error;
    168 	biodone(bp);
    169 }
    170 
    171 int
    172 pud_bdev_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
    173 {
    174 
    175 	return doioctl(dev, cmd, data, flag, PUD_REQ_BDEV, PUD_BDEV_IOCTL);
    176 }
    177 
    178 /* hnmmm */
    179 #if 0
    180 int
    181 pud_bdev_dump(dev_t dev, daddr_t addr, void *data, size_t sz)
    182 {
    183 
    184 	return EOPNOTSUPP;
    185 }
    186 
    187 int
    188 pud_bdev_size(dev_t dev)
    189 {
    190 
    191 	return 0;
    192 }
    193 #endif
    194 
    195 /*
    196  * Charrr devices
    197  */
    198 
    199 static dev_type_open(pud_cdev_open);
    200 static dev_type_close(pud_cdev_close);
    201 static dev_type_read(pud_cdev_read);
    202 static dev_type_write(pud_cdev_write);
    203 static dev_type_ioctl(pud_cdev_ioctl);
    204 static dev_type_poll(pud_cdev_poll);
    205 static dev_type_mmap(pud_cdev_mmap);
    206 static dev_type_kqfilter(pud_cdev_kqfilter);
    207 
    208 struct cdevsw pud_cdevsw = {
    209 	.d_open		= pud_cdev_open,
    210 	.d_close	= pud_cdev_close,
    211 	.d_read		= pud_cdev_read,
    212 	.d_write	= pud_cdev_write,
    213 	.d_ioctl	= pud_cdev_ioctl,
    214 #if 0
    215 	.d_stop		= pud_cdev_stop,
    216 	.d_tty		= pud_cdev_tty,
    217 #endif
    218 	.d_poll		= pud_cdev_poll,
    219 	.d_mmap		= pud_cdev_mmap,
    220 	.d_kqfilter	= pud_cdev_kqfilter,
    221 #ifdef _MODULE
    222 	.d_localcount	= &pud_b_localcount,
    223 #endif
    224 	.d_flag		= D_OTHER,
    225 };
    226 
    227 static int
    228 pud_cdev_open(dev_t dev, int flags, int fmt, lwp_t *l)
    229 {
    230 
    231 	return doopenclose(dev, flags, fmt, PUD_REQ_CDEV, PUD_CDEV_OPEN);
    232 }
    233 
    234 static int
    235 pud_cdev_close(dev_t dev, int flags, int fmt, lwp_t *l)
    236 {
    237 
    238 	return doopenclose(dev, flags, fmt, PUD_REQ_CDEV, PUD_CDEV_CLOSE);
    239 }
    240 
    241 static int
    242 pud_cdev_read(dev_t dev, struct uio *uio, int flag)
    243 {
    244 	struct pud_creq_read *pc_read;
    245 	size_t allocsize;
    246 	int error;
    247 
    248 	allocsize = sizeof(struct pud_creq_read) + uio->uio_resid;
    249 	pc_read = kmem_zalloc(allocsize, KM_SLEEP);
    250 
    251 	pc_read->pm_offset = uio->uio_offset;
    252 	pc_read->pm_resid = uio->uio_resid;
    253 
    254 	error = pud_request(dev, pc_read, allocsize,
    255 	    PUD_REQ_CDEV, PUD_CDEV_READ);
    256 	if (error)
    257 		goto out;
    258 
    259 	if (pc_read->pm_resid > uio->uio_resid) {
    260 		error = EINVAL;
    261 		goto out;
    262 	}
    263 
    264 	error = uiomove(pc_read->pm_data,
    265 	    uio->uio_resid - pc_read->pm_resid, uio);
    266 
    267  out:
    268 	kmem_free(pc_read, allocsize);
    269 	return error;
    270 }
    271 
    272 static int
    273 pud_cdev_write(dev_t dev, struct uio *uio, int flag)
    274 {
    275 	struct pud_creq_write *pc_write;
    276 	size_t allocsize;
    277 	int error;
    278 
    279 	allocsize = sizeof(struct pud_creq_write) + uio->uio_resid;
    280 	pc_write = kmem_zalloc(allocsize, KM_SLEEP);
    281 
    282 	pc_write->pm_offset = uio->uio_offset;
    283 	pc_write->pm_resid = uio->uio_resid;
    284 
    285 	error = uiomove(pc_write->pm_data, uio->uio_resid, uio);
    286 	if (error)
    287 		goto out;
    288 
    289 	error = pud_request(dev, pc_write, allocsize,
    290 	    PUD_REQ_CDEV, PUD_CDEV_WRITE);
    291 	if (error)
    292 		goto out;
    293 
    294 	if (pc_write->pm_resid)
    295 		error = EIO;
    296 
    297  out:
    298 	kmem_free(pc_write, allocsize);
    299 	return error;
    300 }
    301 
    302 static int
    303 pud_cdev_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
    304 {
    305 
    306 	return doioctl(dev, cmd, data, flag, PUD_REQ_CDEV, PUD_CDEV_IOCTL);
    307 }
    308 
    309 static paddr_t
    310 pud_cdev_mmap(dev_t dev, off_t off, int flag)
    311 {
    312 
    313 	return (paddr_t)-1;
    314 }
    315 
    316 static int
    317 pud_cdev_poll(dev_t dev, int flag, lwp_t *l)
    318 {
    319 
    320 	return EOPNOTSUPP;
    321 }
    322 
    323 static int
    324 pud_cdev_kqfilter(dev_t dev, struct knote *kn)
    325 {
    326 
    327 	return EOPNOTSUPP;
    328 }
    329