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