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