Home | History | Annotate | Line # | Download | only in pud
pud.c revision 1.8
      1  1.8    cegger /*	$NetBSD: pud.c,v 1.8 2009/03/18 10:22:41 cegger 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.8    cegger __KERNEL_RCSID(0, "$NetBSD: pud.c,v 1.8 2009/03/18 10:22:41 cegger Exp $");
     33  1.1     pooka 
     34  1.1     pooka #include <sys/param.h>
     35  1.1     pooka #include <sys/conf.h>
     36  1.1     pooka #include <sys/kmem.h>
     37  1.1     pooka #include <sys/poll.h>
     38  1.1     pooka #include <sys/queue.h>
     39  1.1     pooka 
     40  1.1     pooka #include <dev/pud/pud_sys.h>
     41  1.1     pooka #include <dev/putter/putter_sys.h>
     42  1.1     pooka 
     43  1.1     pooka void	pudattach(void);
     44  1.1     pooka 
     45  1.1     pooka static int	pud_putter_getout(void *, size_t, int, uint8_t **,
     46  1.1     pooka 				  size_t *, void **);
     47  1.1     pooka static void	pud_putter_releaseout(void *, void *, int);
     48  1.1     pooka static int	pud_putter_dispatch(void *, struct putter_hdr *);
     49  1.1     pooka static size_t	pud_putter_waitcount(void *);
     50  1.1     pooka static int	pud_putter_close(void *);
     51  1.1     pooka 
     52  1.1     pooka struct putter_ops pud_putter = {
     53  1.1     pooka 	.pop_getout	= pud_putter_getout,
     54  1.1     pooka 	.pop_releaseout	= pud_putter_releaseout,
     55  1.1     pooka 	.pop_waitcount	= pud_putter_waitcount,
     56  1.1     pooka 	.pop_dispatch	= pud_putter_dispatch,
     57  1.1     pooka 	.pop_close	= pud_putter_close,
     58  1.1     pooka };
     59  1.1     pooka 
     60  1.1     pooka extern struct bdevsw pud_bdevsw;
     61  1.1     pooka extern struct cdevsw pud_cdevsw;
     62  1.1     pooka 
     63  1.1     pooka kmutex_t pud_mtx;
     64  1.1     pooka static LIST_HEAD(, pud_dev) pudlist = LIST_HEAD_INITIALIZER(pudlist);
     65  1.1     pooka 
     66  1.1     pooka static uint64_t
     67  1.1     pooka nextreq(struct pud_dev *pd)
     68  1.1     pooka {
     69  1.1     pooka 	uint64_t rv;
     70  1.1     pooka 
     71  1.1     pooka 	mutex_enter(&pd->pd_mtx);
     72  1.1     pooka 	rv = pd->pd_nextreq++;
     73  1.1     pooka 	mutex_exit(&pd->pd_mtx);
     74  1.1     pooka 
     75  1.1     pooka 	return rv;
     76  1.1     pooka }
     77  1.1     pooka 
     78  1.1     pooka static int
     79  1.1     pooka pud_putter_getout(void *this, size_t maxsize, int nonblock,
     80  1.1     pooka 	uint8_t **data, size_t *dlen, void **cookie)
     81  1.1     pooka {
     82  1.1     pooka 	struct pud_dev *pd = this;
     83  1.1     pooka 	struct pud_touser *putp;
     84  1.2    dogcow 	int error = 0;
     85  1.1     pooka 
     86  1.1     pooka 	mutex_enter(&pd->pd_mtx);
     87  1.1     pooka 	for (;;) {
     88  1.1     pooka 		if (TAILQ_EMPTY(&pd->pd_waitq_req)) {
     89  1.1     pooka 			if (nonblock) {
     90  1.1     pooka 				error = EWOULDBLOCK;
     91  1.1     pooka 				break;
     92  1.1     pooka 			}
     93  1.1     pooka 
     94  1.1     pooka 			error = cv_wait_sig(&pd->pd_waitq_req_cv, &pd->pd_mtx);
     95  1.1     pooka 			if (error)
     96  1.1     pooka 				break;
     97  1.1     pooka 			else
     98  1.1     pooka 				continue;
     99  1.1     pooka 		}
    100  1.1     pooka 
    101  1.1     pooka 		putp = TAILQ_FIRST(&pd->pd_waitq_req);
    102  1.1     pooka 		TAILQ_REMOVE(&pd->pd_waitq_req, putp, pt_entries);
    103  1.3     pooka 		KASSERT(error == 0);
    104  1.1     pooka 		break;
    105  1.1     pooka 	}
    106  1.1     pooka 	mutex_exit(&pd->pd_mtx);
    107  1.1     pooka 
    108  1.1     pooka 	if (error == 0) {
    109  1.1     pooka 		*data = (uint8_t *)putp->pt_pdr;
    110  1.1     pooka 		*dlen = putp->pt_pdr->pdr_pth.pth_framelen;
    111  1.1     pooka 		*cookie = putp;
    112  1.1     pooka 	}
    113  1.1     pooka 
    114  1.1     pooka 	return error;
    115  1.1     pooka }
    116  1.1     pooka 
    117  1.1     pooka static void
    118  1.1     pooka pud_putter_releaseout(void *this, void *cookie, int status)
    119  1.1     pooka {
    120  1.1     pooka 	struct pud_dev *pd = this;
    121  1.1     pooka 	struct pud_touser *putp = cookie;
    122  1.1     pooka 
    123  1.1     pooka 	mutex_enter(&pd->pd_mtx);
    124  1.1     pooka 	TAILQ_INSERT_TAIL(&pd->pd_waitq_resp, putp, pt_entries);
    125  1.1     pooka 	mutex_exit(&pd->pd_mtx);
    126  1.1     pooka 
    127  1.1     pooka }
    128  1.1     pooka 
    129  1.1     pooka static size_t
    130  1.1     pooka pud_putter_waitcount(void *this)
    131  1.1     pooka {
    132  1.1     pooka 	struct pud_dev *pd = this;
    133  1.1     pooka 	size_t rv;
    134  1.1     pooka 
    135  1.1     pooka 	mutex_enter(&pd->pd_mtx);
    136  1.1     pooka 	rv = pd->pd_waitcount;
    137  1.1     pooka 	mutex_exit(&pd->pd_mtx);
    138  1.1     pooka 
    139  1.1     pooka 	return rv;
    140  1.1     pooka }
    141  1.1     pooka 
    142  1.1     pooka static int
    143  1.1     pooka pudop_dev(struct pud_dev *pd, struct pud_req *pdr)
    144  1.1     pooka {
    145  1.1     pooka 	struct putter_hdr *pth = (void *)pdr;
    146  1.1     pooka 	struct pud_touser *putp;
    147  1.1     pooka 
    148  1.1     pooka 	mutex_enter(&pd->pd_mtx);
    149  1.1     pooka 	TAILQ_FOREACH(putp, &pd->pd_waitq_resp, pt_entries)
    150  1.1     pooka 		if (putp->pt_pdr->pdr_reqid == pdr->pdr_reqid)
    151  1.1     pooka 			break;
    152  1.1     pooka 	if (putp == NULL) {
    153  1.1     pooka 		mutex_exit(&pd->pd_mtx);
    154  1.1     pooka 		return EINVAL;
    155  1.1     pooka 	}
    156  1.1     pooka 	TAILQ_REMOVE(&pd->pd_waitq_resp, putp, pt_entries);
    157  1.1     pooka 	mutex_exit(&pd->pd_mtx);
    158  1.1     pooka 
    159  1.1     pooka 	if (pth->pth_framelen > putp->pt_pdr->pdr_len) {
    160  1.1     pooka 		return EINVAL;
    161  1.1     pooka 	}
    162  1.1     pooka 	memcpy(putp->pt_pdr, pth, pth->pth_framelen);
    163  1.1     pooka 	cv_signal(&putp->pt_cv);
    164  1.1     pooka 
    165  1.1     pooka 	return 0;
    166  1.1     pooka }
    167  1.1     pooka 
    168  1.1     pooka /*
    169  1.1     pooka  * Register our major number.  Always register char device functions,
    170  1.1     pooka  * register block devices optionally.
    171  1.1     pooka  *
    172  1.1     pooka  * XXX: no way to configure "any major you like" currently.
    173  1.1     pooka  */
    174  1.1     pooka static int
    175  1.1     pooka pudconf_reg(struct pud_dev *pd, struct pud_conf_reg *pcr)
    176  1.1     pooka {
    177  1.1     pooka 	struct bdevsw *bsw;
    178  1.7  drochner 	devmajor_t cmajor, bmajor;
    179  1.7  drochner 	int error;
    180  1.1     pooka 
    181  1.5     pooka 	if (pcr->pm_version != (PUD_DEVELVERSION | PUD_VERSION)) {
    182  1.5     pooka 		printf("pud version mismatch %d vs %d\n",
    183  1.5     pooka 		    pcr->pm_version & ~PUD_DEVELVERSION, PUD_VERSION);
    184  1.5     pooka 		return EINVAL; /* XXX */
    185  1.5     pooka 	}
    186  1.5     pooka 
    187  1.1     pooka 	cmajor = major(pcr->pm_regdev);
    188  1.3     pooka 	if (pcr->pm_flags & PUD_CONFFLAG_BDEV) {
    189  1.1     pooka 		bsw = &pud_bdevsw;
    190  1.1     pooka 		bmajor = cmajor;
    191  1.1     pooka 	} else {
    192  1.1     pooka 		bsw = NULL;
    193  1.7  drochner 		bmajor = NODEVMAJOR;
    194  1.1     pooka 	}
    195  1.1     pooka 
    196  1.1     pooka 	pcr->pm_devname[PUD_DEVNAME_MAX] = '\0';
    197  1.1     pooka         error = devsw_attach(pcr->pm_devname, bsw, &bmajor,
    198  1.1     pooka 	    &pud_cdevsw, &cmajor);
    199  1.1     pooka 	if (error == 0)
    200  1.1     pooka 		pd->pd_dev = pcr->pm_regdev;
    201  1.1     pooka 
    202  1.1     pooka 	return error;
    203  1.1     pooka }
    204  1.1     pooka 
    205  1.1     pooka static int
    206  1.1     pooka pudop_conf(struct pud_dev *pd, struct pud_req *pdr)
    207  1.1     pooka {
    208  1.1     pooka 	int rv;
    209  1.1     pooka 
    210  1.1     pooka 	switch (pdr->pdr_reqtype) {
    211  1.1     pooka 	case PUD_CONF_REG:
    212  1.1     pooka 		rv = pudconf_reg(pd, (struct pud_conf_reg *)pdr);
    213  1.1     pooka 		break;
    214  1.1     pooka 	case PUD_CONF_DEREG:
    215  1.1     pooka 		/* unimplemented */
    216  1.1     pooka 		rv = 0;
    217  1.1     pooka 		break;
    218  1.1     pooka 	default:
    219  1.1     pooka 		rv = EINVAL;
    220  1.1     pooka 		break;
    221  1.1     pooka 	}
    222  1.1     pooka 
    223  1.1     pooka 	return rv;
    224  1.1     pooka }
    225  1.1     pooka 
    226  1.1     pooka static int
    227  1.1     pooka pud_putter_dispatch(void *this, struct putter_hdr *pth)
    228  1.1     pooka {
    229  1.1     pooka 	struct pud_dev *pd = this;
    230  1.1     pooka 	struct pud_req *pdr = (void *)pth;
    231  1.1     pooka 	int rv;
    232  1.1     pooka 
    233  1.1     pooka 	if (pdr->pdr_pth.pth_framelen < sizeof(struct pud_req))
    234  1.1     pooka 		return EINVAL;
    235  1.1     pooka 
    236  1.1     pooka 	switch (pdr->pdr_reqclass) {
    237  1.1     pooka 	case PUD_REQ_CDEV:
    238  1.1     pooka 	case PUD_REQ_BDEV:
    239  1.1     pooka 		rv = pudop_dev(pd, pdr);
    240  1.1     pooka 		break;
    241  1.1     pooka 	case PUD_REQ_CONF:
    242  1.1     pooka 		rv = pudop_conf(pd, pdr);
    243  1.1     pooka 		break;
    244  1.1     pooka 	default:
    245  1.1     pooka 		rv = EINVAL;
    246  1.1     pooka 		break;
    247  1.1     pooka 	}
    248  1.1     pooka 
    249  1.1     pooka 	return rv;
    250  1.1     pooka }
    251  1.1     pooka 
    252  1.1     pooka /* Device server severed the umbilical cord */
    253  1.1     pooka static int
    254  1.1     pooka pud_putter_close(void *this)
    255  1.1     pooka {
    256  1.1     pooka 	struct pud_dev *pd = this;
    257  1.1     pooka 	struct pud_touser *putp;
    258  1.1     pooka 
    259  1.1     pooka 	mutex_enter(&pud_mtx);
    260  1.1     pooka 	LIST_REMOVE(pd, pd_entries);
    261  1.1     pooka 	mutex_exit(&pud_mtx);
    262  1.1     pooka 
    263  1.1     pooka 	mutex_enter(&pd->pd_mtx);
    264  1.1     pooka 	while ((putp = TAILQ_FIRST(&pd->pd_waitq_req)) != NULL) {
    265  1.1     pooka 		putp->pt_pdr->pdr_rv = ENXIO;
    266  1.1     pooka 		cv_signal(&putp->pt_cv);
    267  1.1     pooka 		TAILQ_REMOVE(&pd->pd_waitq_req, putp, pt_entries);
    268  1.1     pooka 	}
    269  1.1     pooka 
    270  1.1     pooka 	while ((putp = TAILQ_FIRST(&pd->pd_waitq_resp)) != NULL) {
    271  1.1     pooka 		putp->pt_pdr->pdr_rv = ENXIO;
    272  1.1     pooka 		cv_signal(&putp->pt_cv);
    273  1.1     pooka 		TAILQ_REMOVE(&pd->pd_waitq_resp, putp, pt_entries);
    274  1.1     pooka 	}
    275  1.1     pooka 	if (pd->pd_waitcount)
    276  1.1     pooka 		cv_wait(&pd->pd_draincv, &pd->pd_mtx);
    277  1.1     pooka 	KASSERT(pd->pd_waitcount == 0);
    278  1.1     pooka 
    279  1.1     pooka 	mutex_exit(&pd->pd_mtx);
    280  1.1     pooka 
    281  1.1     pooka 	if (pd->pd_dev)
    282  1.1     pooka 		devsw_detach(&pud_bdevsw /* XXX */, &pud_cdevsw);
    283  1.1     pooka 
    284  1.1     pooka 	putter_detach(pd->pd_pi);
    285  1.5     pooka 
    286  1.5     pooka 	mutex_destroy(&pd->pd_mtx);
    287  1.5     pooka 	cv_destroy(&pd->pd_draincv);
    288  1.5     pooka 	cv_destroy(&pd->pd_waitq_req_cv);
    289  1.1     pooka 	kmem_free(pd, sizeof(struct pud_dev));
    290  1.1     pooka 
    291  1.1     pooka 	return 0;
    292  1.1     pooka }
    293  1.1     pooka 
    294  1.1     pooka struct pud_dev *
    295  1.1     pooka pud_dev2pud(dev_t dev)
    296  1.1     pooka {
    297  1.1     pooka 	struct pud_dev *pd;
    298  1.1     pooka 
    299  1.1     pooka 	mutex_enter(&pud_mtx);
    300  1.1     pooka 	LIST_FOREACH(pd, &pudlist, pd_entries)
    301  1.1     pooka 		if (major(pd->pd_dev) == major(dev))
    302  1.1     pooka 			break;
    303  1.1     pooka 	mutex_exit(&pud_mtx);
    304  1.1     pooka 
    305  1.1     pooka 	return pd;
    306  1.1     pooka }
    307  1.1     pooka 
    308  1.1     pooka /* Toss request to the device server and wait for result */
    309  1.1     pooka int
    310  1.1     pooka pud_request(dev_t dev, void *data, size_t dlen, int class, int type)
    311  1.1     pooka {
    312  1.1     pooka 	struct pud_touser put;
    313  1.1     pooka 	struct pud_req *pdr = data;
    314  1.1     pooka 	struct pud_dev *pd;
    315  1.1     pooka 
    316  1.1     pooka 	pd = pud_dev2pud(dev);
    317  1.1     pooka 	if (pd == NULL)
    318  1.1     pooka 		return ENXIO;
    319  1.1     pooka 
    320  1.1     pooka 	pdr->pdr_dev = dev;
    321  1.1     pooka 	pdr->pdr_len = pdr->pdr_pth.pth_framelen = dlen;
    322  1.1     pooka 	pdr->pdr_reqid = nextreq(pd);
    323  1.1     pooka 
    324  1.1     pooka 	pdr->pdr_reqclass = class;
    325  1.1     pooka 	pdr->pdr_reqtype = type;
    326  1.1     pooka 
    327  1.1     pooka 	put.pt_pdr = pdr;
    328  1.1     pooka 	cv_init(&put.pt_cv, "pudresp");
    329  1.1     pooka 
    330  1.1     pooka 	mutex_enter(&pd->pd_mtx);
    331  1.1     pooka 	pd->pd_waitcount++;
    332  1.1     pooka 
    333  1.1     pooka 	TAILQ_INSERT_TAIL(&pd->pd_waitq_req, &put, pt_entries);
    334  1.1     pooka 	putter_notify(pd->pd_pi);
    335  1.1     pooka 	cv_broadcast(&pd->pd_waitq_req_cv);
    336  1.1     pooka 	cv_wait(&put.pt_cv, &pd->pd_mtx);
    337  1.1     pooka 
    338  1.1     pooka 	if (--pd->pd_waitcount == 0)
    339  1.1     pooka 		cv_signal(&pd->pd_draincv);
    340  1.1     pooka 	mutex_exit(&pd->pd_mtx);
    341  1.6     pooka 	cv_destroy(&put.pt_cv);
    342  1.1     pooka 
    343  1.1     pooka 	return pdr->pdr_rv;
    344  1.1     pooka }
    345  1.1     pooka 
    346  1.1     pooka /* Called from putter based on minor dev number */
    347  1.1     pooka int
    348  1.1     pooka pud_config(int fd, int flags, int fmt)
    349  1.1     pooka {
    350  1.1     pooka 	struct pud_dev *pd;
    351  1.1     pooka 
    352  1.1     pooka 	pd = kmem_zalloc(sizeof(struct pud_dev), KM_SLEEP);
    353  1.1     pooka 	pd->pd_pi = putter_attach(curlwp->l_proc->p_pid, fd, pd, &pud_putter);
    354  1.1     pooka 	if (pd->pd_pi == NULL) {
    355  1.1     pooka 		kmem_free(pd, sizeof(struct pud_dev));
    356  1.1     pooka 		return ENOENT; /* XXX */
    357  1.1     pooka 	}
    358  1.1     pooka 	pd->pd_dev = NODEV;
    359  1.1     pooka 
    360  1.1     pooka 	mutex_init(&pd->pd_mtx, MUTEX_DEFAULT, IPL_NONE);
    361  1.1     pooka 	TAILQ_INIT(&pd->pd_waitq_req);
    362  1.1     pooka 	TAILQ_INIT(&pd->pd_waitq_resp);
    363  1.1     pooka 	cv_init(&pd->pd_waitq_req_cv, "pudreq");
    364  1.1     pooka 	cv_init(&pd->pd_draincv, "pudrain");
    365  1.1     pooka 
    366  1.1     pooka 	mutex_enter(&pud_mtx);
    367  1.1     pooka 	LIST_INSERT_HEAD(&pudlist, pd, pd_entries);
    368  1.1     pooka 	mutex_exit(&pud_mtx);
    369  1.1     pooka 
    370  1.1     pooka 	return 0;
    371  1.1     pooka }
    372  1.1     pooka 
    373  1.1     pooka void
    374  1.8    cegger pudattach(void)
    375  1.1     pooka {
    376  1.1     pooka 	int error;
    377  1.1     pooka 
    378  1.1     pooka 	if ((error = putter_register(pud_config, PUTTER_MINOR_PUD)) != 0) {
    379  1.1     pooka 		printf("pudattach: can't register to putter: %d\n", error);
    380  1.1     pooka 		return;
    381  1.1     pooka 	}
    382  1.1     pooka 	mutex_init(&pud_mtx, MUTEX_DEFAULT, IPL_NONE);
    383  1.1     pooka }
    384