Home | History | Annotate | Line # | Download | only in pud
pud_dev.c revision 1.2
      1 /*	$NetBSD: pud_dev.c,v 1.2 2007/11/21 11:19:44 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.2 2007/11/21 11:19:44 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 /*
     45  * Block de-vices
     46  */
     47 
     48 static dev_type_open(pud_bdev_open);
     49 static dev_type_close(pud_bdev_close);
     50 static dev_type_strategy(pud_bdev_strategy);
     51 static dev_type_ioctl(pud_bdev_ioctl);
     52 #if 0
     53 static dev_type_dump(pud_bdev_dump);
     54 static dev_type_size(pud_bdev_size);
     55 #endif
     56 
     57 struct bdevsw pud_bdevsw = {
     58 	.d_open		= pud_bdev_open,
     59 	.d_close	= pud_bdev_close,
     60 	.d_strategy	= pud_bdev_strategy,
     61 	.d_ioctl	= pud_bdev_ioctl,
     62 #if 0
     63 	.d_dump		= pud_bdev_dump,
     64 	.d_psize	= pud_bdev_size,
     65 #endif
     66 };
     67 
     68 static int
     69 pud_bdev_open(dev_t dev, int flags, int fmt, lwp_t *l)
     70 {
     71 
     72 	return EOPNOTSUPP;
     73 }
     74 
     75 static int
     76 pud_bdev_close(dev_t dev, int flags, int fmt, lwp_t *l)
     77 {
     78 
     79 	return EOPNOTSUPP;
     80 }
     81 
     82 static void
     83 pud_bdev_strategy(struct buf *bp)
     84 {
     85 
     86 	bp->b_error = EOPNOTSUPP;
     87 	biodone(bp);
     88 }
     89 
     90 int
     91 pud_bdev_ioctl(dev_t dev, u_long cmd, void *data, int dlen, lwp_t *l)
     92 {
     93 
     94 	return EOPNOTSUPP;
     95 }
     96 
     97 /* hnmmm */
     98 #if 0
     99 int
    100 pud_bdev_dump(dev_t dev, daddr_t addr, void *data, size_t sz)
    101 {
    102 
    103 	return EOPNOTSUPP;
    104 }
    105 
    106 int
    107 pud_bdev_size(dev_t dev)
    108 {
    109 
    110 	return 0;
    111 }
    112 #endif
    113 
    114 /*
    115  * Charrr devices
    116  */
    117 
    118 static dev_type_open(pud_cdev_open);
    119 static dev_type_close(pud_cdev_close);
    120 static dev_type_read(pud_cdev_read);
    121 static dev_type_write(pud_cdev_write);
    122 static dev_type_ioctl(pud_cdev_ioctl);
    123 static dev_type_poll(pud_cdev_poll);
    124 static dev_type_mmap(pud_cdev_mmap);
    125 static dev_type_kqfilter(pud_cdev_kqfilter);
    126 
    127 struct cdevsw pud_cdevsw = {
    128 	.d_open		= pud_cdev_open,
    129 	.d_close	= pud_cdev_close,
    130 	.d_read		= pud_cdev_read,
    131 	.d_write	= pud_cdev_write,
    132 	.d_ioctl	= pud_cdev_ioctl,
    133 #if 0
    134 	.d_stop		= pud_cdev_stop,
    135 	.d_tty		= pud_cdev_tty,
    136 #endif
    137 	.d_poll		= pud_cdev_poll,
    138 	.d_mmap		= pud_cdev_mmap,
    139 	.d_kqfilter	= pud_cdev_kqfilter,
    140 	.d_flag		= D_OTHER,
    141 };
    142 
    143 static int
    144 pud_cdev_open(dev_t dev, int flags, int fmt, lwp_t *l)
    145 {
    146 	struct pud_creq_open pc_open; /* XXX: stack = stupid */
    147 
    148 	pc_open.pm_flags = flags;
    149 	pc_open.pm_fmt = fmt;
    150 
    151 	return pud_request(dev, &pc_open, sizeof(pc_open),
    152 	    PUD_REQ_CDEV, PUD_CDEV_OPEN);
    153 }
    154 
    155 static int
    156 pud_cdev_close(dev_t dev, int flags, int fmt, lwp_t *l)
    157 {
    158 	struct pud_creq_close pc_close; /* XXX: stack = stupid */
    159 
    160 	pc_close.pm_flags = flags;
    161 	pc_close.pm_fmt = fmt;
    162 
    163 	return pud_request(dev, &pc_close, sizeof(pc_close),
    164 	    PUD_REQ_CDEV, PUD_CDEV_CLOSE);
    165 }
    166 
    167 static int
    168 pud_cdev_read(dev_t dev, struct uio *uio, int flag)
    169 {
    170 	struct pud_creq_read *pc_read;
    171 	size_t allocsize;
    172 	int error;
    173 
    174 	allocsize = sizeof(struct pud_creq_read) + uio->uio_resid;
    175 	pc_read = kmem_zalloc(allocsize, KM_SLEEP);
    176 
    177 	pc_read->pm_offset = uio->uio_offset;
    178 	pc_read->pm_resid = uio->uio_resid;
    179 
    180 	error = pud_request(dev, pc_read, allocsize,
    181 	    PUD_REQ_CDEV, PUD_CDEV_READ);
    182 	if (error)
    183 		goto out;
    184 
    185 	if (pc_read->pm_resid > uio->uio_resid) {
    186 		error = EINVAL;
    187 		goto out;
    188 	}
    189 
    190 	error = uiomove(pc_read->pm_data, pc_read->pm_resid, uio);
    191 
    192  out:
    193 	kmem_free(pc_read, allocsize);
    194 	return error;
    195 }
    196 
    197 static int
    198 pud_cdev_write(dev_t dev, struct uio *uio, int flag)
    199 {
    200 	struct pud_creq_write *pc_write;
    201 	size_t allocsize;
    202 	int error;
    203 
    204 	allocsize = sizeof(struct pud_creq_write) + uio->uio_resid;
    205 	pc_write = kmem_zalloc(allocsize, KM_SLEEP);
    206 
    207 	pc_write->pm_offset = uio->uio_offset;
    208 	pc_write->pm_resid = uio->uio_resid;
    209 
    210 	error = uiomove(pc_write->pm_data, uio->uio_resid, uio);
    211 	if (error)
    212 		goto out;
    213 
    214 	error = pud_request(dev, pc_write, allocsize,
    215 	    PUD_REQ_CDEV, PUD_CDEV_WRITE);
    216 	if (error)
    217 		goto out;
    218 
    219 	if (pc_write->pm_resid)
    220 		error = EIO;
    221 
    222  out:
    223 	kmem_free(pc_write, allocsize);
    224 	return error;
    225 }
    226 
    227 static int
    228 pud_cdev_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
    229 {
    230 
    231 	return EOPNOTSUPP;
    232 }
    233 
    234 static paddr_t
    235 pud_cdev_mmap(dev_t dev, off_t off, int flag)
    236 {
    237 
    238 	return (paddr_t)-1;
    239 }
    240 
    241 static int
    242 pud_cdev_poll(dev_t dev, int flag, lwp_t *l)
    243 {
    244 
    245 	return EOPNOTSUPP;
    246 }
    247 
    248 static int
    249 pud_cdev_kqfilter(dev_t dev, struct knote *kn)
    250 {
    251 
    252 	return EOPNOTSUPP;
    253 }
    254