Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / dev / pud / pud.c
blobe1f31077bf71f17ed8a11ee3990a9a59f4c61ba1
1 /* $NetBSD: pud.c,v 1.7 2009/01/20 18:20:48 drochner Exp $ */
3 /*
4 * Copyright (c) 2007 Antti Kantee. All Rights Reserved.
6 * Development of this software was supported by the
7 * Research Foundation of Helsinki University of Technology
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.
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.
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: pud.c,v 1.7 2009/01/20 18:20:48 drochner Exp $");
34 #include <sys/param.h>
35 #include <sys/conf.h>
36 #include <sys/kmem.h>
37 #include <sys/poll.h>
38 #include <sys/queue.h>
40 #include <dev/pud/pud_sys.h>
41 #include <dev/putter/putter_sys.h>
43 void pudattach(void);
45 static int pud_putter_getout(void *, size_t, int, uint8_t **,
46 size_t *, void **);
47 static void pud_putter_releaseout(void *, void *, int);
48 static int pud_putter_dispatch(void *, struct putter_hdr *);
49 static size_t pud_putter_waitcount(void *);
50 static int pud_putter_close(void *);
52 struct putter_ops pud_putter = {
53 .pop_getout = pud_putter_getout,
54 .pop_releaseout = pud_putter_releaseout,
55 .pop_waitcount = pud_putter_waitcount,
56 .pop_dispatch = pud_putter_dispatch,
57 .pop_close = pud_putter_close,
60 extern struct bdevsw pud_bdevsw;
61 extern struct cdevsw pud_cdevsw;
63 kmutex_t pud_mtx;
64 static LIST_HEAD(, pud_dev) pudlist = LIST_HEAD_INITIALIZER(pudlist);
66 static uint64_t
67 nextreq(struct pud_dev *pd)
69 uint64_t rv;
71 mutex_enter(&pd->pd_mtx);
72 rv = pd->pd_nextreq++;
73 mutex_exit(&pd->pd_mtx);
75 return rv;
78 static int
79 pud_putter_getout(void *this, size_t maxsize, int nonblock,
80 uint8_t **data, size_t *dlen, void **cookie)
82 struct pud_dev *pd = this;
83 struct pud_touser *putp;
84 int error = 0;
86 mutex_enter(&pd->pd_mtx);
87 for (;;) {
88 if (TAILQ_EMPTY(&pd->pd_waitq_req)) {
89 if (nonblock) {
90 error = EWOULDBLOCK;
91 break;
94 error = cv_wait_sig(&pd->pd_waitq_req_cv, &pd->pd_mtx);
95 if (error)
96 break;
97 else
98 continue;
101 putp = TAILQ_FIRST(&pd->pd_waitq_req);
102 TAILQ_REMOVE(&pd->pd_waitq_req, putp, pt_entries);
103 KASSERT(error == 0);
104 break;
106 mutex_exit(&pd->pd_mtx);
108 if (error == 0) {
109 *data = (uint8_t *)putp->pt_pdr;
110 *dlen = putp->pt_pdr->pdr_pth.pth_framelen;
111 *cookie = putp;
114 return error;
117 static void
118 pud_putter_releaseout(void *this, void *cookie, int status)
120 struct pud_dev *pd = this;
121 struct pud_touser *putp = cookie;
123 mutex_enter(&pd->pd_mtx);
124 TAILQ_INSERT_TAIL(&pd->pd_waitq_resp, putp, pt_entries);
125 mutex_exit(&pd->pd_mtx);
129 static size_t
130 pud_putter_waitcount(void *this)
132 struct pud_dev *pd = this;
133 size_t rv;
135 mutex_enter(&pd->pd_mtx);
136 rv = pd->pd_waitcount;
137 mutex_exit(&pd->pd_mtx);
139 return rv;
142 static int
143 pudop_dev(struct pud_dev *pd, struct pud_req *pdr)
145 struct putter_hdr *pth = (void *)pdr;
146 struct pud_touser *putp;
148 mutex_enter(&pd->pd_mtx);
149 TAILQ_FOREACH(putp, &pd->pd_waitq_resp, pt_entries)
150 if (putp->pt_pdr->pdr_reqid == pdr->pdr_reqid)
151 break;
152 if (putp == NULL) {
153 mutex_exit(&pd->pd_mtx);
154 return EINVAL;
156 TAILQ_REMOVE(&pd->pd_waitq_resp, putp, pt_entries);
157 mutex_exit(&pd->pd_mtx);
159 if (pth->pth_framelen > putp->pt_pdr->pdr_len) {
160 return EINVAL;
162 memcpy(putp->pt_pdr, pth, pth->pth_framelen);
163 cv_signal(&putp->pt_cv);
165 return 0;
169 * Register our major number. Always register char device functions,
170 * register block devices optionally.
172 * XXX: no way to configure "any major you like" currently.
174 static int
175 pudconf_reg(struct pud_dev *pd, struct pud_conf_reg *pcr)
177 struct bdevsw *bsw;
178 devmajor_t cmajor, bmajor;
179 int error;
181 if (pcr->pm_version != (PUD_DEVELVERSION | PUD_VERSION)) {
182 printf("pud version mismatch %d vs %d\n",
183 pcr->pm_version & ~PUD_DEVELVERSION, PUD_VERSION);
184 return EINVAL; /* XXX */
187 cmajor = major(pcr->pm_regdev);
188 if (pcr->pm_flags & PUD_CONFFLAG_BDEV) {
189 bsw = &pud_bdevsw;
190 bmajor = cmajor;
191 } else {
192 bsw = NULL;
193 bmajor = NODEVMAJOR;
196 pcr->pm_devname[PUD_DEVNAME_MAX] = '\0';
197 error = devsw_attach(pcr->pm_devname, bsw, &bmajor,
198 &pud_cdevsw, &cmajor);
199 if (error == 0)
200 pd->pd_dev = pcr->pm_regdev;
202 return error;
205 static int
206 pudop_conf(struct pud_dev *pd, struct pud_req *pdr)
208 int rv;
210 switch (pdr->pdr_reqtype) {
211 case PUD_CONF_REG:
212 rv = pudconf_reg(pd, (struct pud_conf_reg *)pdr);
213 break;
214 case PUD_CONF_DEREG:
215 /* unimplemented */
216 rv = 0;
217 break;
218 default:
219 rv = EINVAL;
220 break;
223 return rv;
226 static int
227 pud_putter_dispatch(void *this, struct putter_hdr *pth)
229 struct pud_dev *pd = this;
230 struct pud_req *pdr = (void *)pth;
231 int rv;
233 if (pdr->pdr_pth.pth_framelen < sizeof(struct pud_req))
234 return EINVAL;
236 switch (pdr->pdr_reqclass) {
237 case PUD_REQ_CDEV:
238 case PUD_REQ_BDEV:
239 rv = pudop_dev(pd, pdr);
240 break;
241 case PUD_REQ_CONF:
242 rv = pudop_conf(pd, pdr);
243 break;
244 default:
245 rv = EINVAL;
246 break;
249 return rv;
252 /* Device server severed the umbilical cord */
253 static int
254 pud_putter_close(void *this)
256 struct pud_dev *pd = this;
257 struct pud_touser *putp;
259 mutex_enter(&pud_mtx);
260 LIST_REMOVE(pd, pd_entries);
261 mutex_exit(&pud_mtx);
263 mutex_enter(&pd->pd_mtx);
264 while ((putp = TAILQ_FIRST(&pd->pd_waitq_req)) != NULL) {
265 putp->pt_pdr->pdr_rv = ENXIO;
266 cv_signal(&putp->pt_cv);
267 TAILQ_REMOVE(&pd->pd_waitq_req, putp, pt_entries);
270 while ((putp = TAILQ_FIRST(&pd->pd_waitq_resp)) != NULL) {
271 putp->pt_pdr->pdr_rv = ENXIO;
272 cv_signal(&putp->pt_cv);
273 TAILQ_REMOVE(&pd->pd_waitq_resp, putp, pt_entries);
275 if (pd->pd_waitcount)
276 cv_wait(&pd->pd_draincv, &pd->pd_mtx);
277 KASSERT(pd->pd_waitcount == 0);
279 mutex_exit(&pd->pd_mtx);
281 if (pd->pd_dev)
282 devsw_detach(&pud_bdevsw /* XXX */, &pud_cdevsw);
284 putter_detach(pd->pd_pi);
286 mutex_destroy(&pd->pd_mtx);
287 cv_destroy(&pd->pd_draincv);
288 cv_destroy(&pd->pd_waitq_req_cv);
289 kmem_free(pd, sizeof(struct pud_dev));
291 return 0;
294 struct pud_dev *
295 pud_dev2pud(dev_t dev)
297 struct pud_dev *pd;
299 mutex_enter(&pud_mtx);
300 LIST_FOREACH(pd, &pudlist, pd_entries)
301 if (major(pd->pd_dev) == major(dev))
302 break;
303 mutex_exit(&pud_mtx);
305 return pd;
308 /* Toss request to the device server and wait for result */
310 pud_request(dev_t dev, void *data, size_t dlen, int class, int type)
312 struct pud_touser put;
313 struct pud_req *pdr = data;
314 struct pud_dev *pd;
316 pd = pud_dev2pud(dev);
317 if (pd == NULL)
318 return ENXIO;
320 pdr->pdr_dev = dev;
321 pdr->pdr_len = pdr->pdr_pth.pth_framelen = dlen;
322 pdr->pdr_reqid = nextreq(pd);
324 pdr->pdr_reqclass = class;
325 pdr->pdr_reqtype = type;
327 put.pt_pdr = pdr;
328 cv_init(&put.pt_cv, "pudresp");
330 mutex_enter(&pd->pd_mtx);
331 pd->pd_waitcount++;
333 TAILQ_INSERT_TAIL(&pd->pd_waitq_req, &put, pt_entries);
334 putter_notify(pd->pd_pi);
335 cv_broadcast(&pd->pd_waitq_req_cv);
336 cv_wait(&put.pt_cv, &pd->pd_mtx);
338 if (--pd->pd_waitcount == 0)
339 cv_signal(&pd->pd_draincv);
340 mutex_exit(&pd->pd_mtx);
341 cv_destroy(&put.pt_cv);
343 return pdr->pdr_rv;
346 /* Called from putter based on minor dev number */
348 pud_config(int fd, int flags, int fmt)
350 struct pud_dev *pd;
352 pd = kmem_zalloc(sizeof(struct pud_dev), KM_SLEEP);
353 pd->pd_pi = putter_attach(curlwp->l_proc->p_pid, fd, pd, &pud_putter);
354 if (pd->pd_pi == NULL) {
355 kmem_free(pd, sizeof(struct pud_dev));
356 return ENOENT; /* XXX */
358 pd->pd_dev = NODEV;
360 mutex_init(&pd->pd_mtx, MUTEX_DEFAULT, IPL_NONE);
361 TAILQ_INIT(&pd->pd_waitq_req);
362 TAILQ_INIT(&pd->pd_waitq_resp);
363 cv_init(&pd->pd_waitq_req_cv, "pudreq");
364 cv_init(&pd->pd_draincv, "pudrain");
366 mutex_enter(&pud_mtx);
367 LIST_INSERT_HEAD(&pudlist, pd, pd_entries);
368 mutex_exit(&pud_mtx);
370 return 0;
373 void
374 pudattach(void)
376 int error;
378 if ((error = putter_register(pud_config, PUTTER_MINOR_PUD)) != 0) {
379 printf("pudattach: can't register to putter: %d\n", error);
380 return;
382 mutex_init(&pud_mtx, MUTEX_DEFAULT, IPL_NONE);