pud.c revision 1.1 1 1.1 pooka /* $NetBSD: pud.c,v 1.1 2007/11/20 18:47:05 pooka 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.1 pooka __KERNEL_RCSID(0, "$NetBSD: pud.c,v 1.1 2007/11/20 18:47:05 pooka 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.1 pooka int error;
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.1 pooka break;
104 1.1 pooka }
105 1.1 pooka mutex_exit(&pd->pd_mtx);
106 1.1 pooka
107 1.1 pooka if (error == 0) {
108 1.1 pooka *data = (uint8_t *)putp->pt_pdr;
109 1.1 pooka *dlen = putp->pt_pdr->pdr_pth.pth_framelen;
110 1.1 pooka *cookie = putp;
111 1.1 pooka }
112 1.1 pooka
113 1.1 pooka return error;
114 1.1 pooka }
115 1.1 pooka
116 1.1 pooka static void
117 1.1 pooka pud_putter_releaseout(void *this, void *cookie, int status)
118 1.1 pooka {
119 1.1 pooka struct pud_dev *pd = this;
120 1.1 pooka struct pud_touser *putp = cookie;
121 1.1 pooka
122 1.1 pooka mutex_enter(&pd->pd_mtx);
123 1.1 pooka TAILQ_INSERT_TAIL(&pd->pd_waitq_resp, putp, pt_entries);
124 1.1 pooka mutex_exit(&pd->pd_mtx);
125 1.1 pooka
126 1.1 pooka }
127 1.1 pooka
128 1.1 pooka static size_t
129 1.1 pooka pud_putter_waitcount(void *this)
130 1.1 pooka {
131 1.1 pooka struct pud_dev *pd = this;
132 1.1 pooka size_t rv;
133 1.1 pooka
134 1.1 pooka mutex_enter(&pd->pd_mtx);
135 1.1 pooka rv = pd->pd_waitcount;
136 1.1 pooka mutex_exit(&pd->pd_mtx);
137 1.1 pooka
138 1.1 pooka return rv;
139 1.1 pooka }
140 1.1 pooka
141 1.1 pooka static int
142 1.1 pooka pudop_dev(struct pud_dev *pd, struct pud_req *pdr)
143 1.1 pooka {
144 1.1 pooka struct putter_hdr *pth = (void *)pdr;
145 1.1 pooka struct pud_touser *putp;
146 1.1 pooka
147 1.1 pooka mutex_enter(&pd->pd_mtx);
148 1.1 pooka TAILQ_FOREACH(putp, &pd->pd_waitq_resp, pt_entries)
149 1.1 pooka if (putp->pt_pdr->pdr_reqid == pdr->pdr_reqid)
150 1.1 pooka break;
151 1.1 pooka if (putp == NULL) {
152 1.1 pooka mutex_exit(&pd->pd_mtx);
153 1.1 pooka return EINVAL;
154 1.1 pooka }
155 1.1 pooka TAILQ_REMOVE(&pd->pd_waitq_resp, putp, pt_entries);
156 1.1 pooka mutex_exit(&pd->pd_mtx);
157 1.1 pooka
158 1.1 pooka if (pth->pth_framelen > putp->pt_pdr->pdr_len) {
159 1.1 pooka return EINVAL;
160 1.1 pooka }
161 1.1 pooka memcpy(putp->pt_pdr, pth, pth->pth_framelen);
162 1.1 pooka cv_signal(&putp->pt_cv);
163 1.1 pooka
164 1.1 pooka return 0;
165 1.1 pooka }
166 1.1 pooka
167 1.1 pooka /*
168 1.1 pooka * Register our major number. Always register char device functions,
169 1.1 pooka * register block devices optionally.
170 1.1 pooka *
171 1.1 pooka * XXX: no way to configure "any major you like" currently.
172 1.1 pooka */
173 1.1 pooka static int
174 1.1 pooka pudconf_reg(struct pud_dev *pd, struct pud_conf_reg *pcr)
175 1.1 pooka {
176 1.1 pooka struct bdevsw *bsw;
177 1.1 pooka int cmajor, bmajor, error;
178 1.1 pooka
179 1.1 pooka cmajor = major(pcr->pm_regdev);
180 1.1 pooka if (pcr->pm_flags & PUD_CONF_BDEV) {
181 1.1 pooka bsw = &pud_bdevsw;
182 1.1 pooka bmajor = cmajor;
183 1.1 pooka } else {
184 1.1 pooka bsw = NULL;
185 1.1 pooka bmajor = -1;
186 1.1 pooka }
187 1.1 pooka
188 1.1 pooka pcr->pm_devname[PUD_DEVNAME_MAX] = '\0';
189 1.1 pooka error = devsw_attach(pcr->pm_devname, bsw, &bmajor,
190 1.1 pooka &pud_cdevsw, &cmajor);
191 1.1 pooka if (error == 0)
192 1.1 pooka pd->pd_dev = pcr->pm_regdev;
193 1.1 pooka
194 1.1 pooka return error;
195 1.1 pooka }
196 1.1 pooka
197 1.1 pooka static int
198 1.1 pooka pudop_conf(struct pud_dev *pd, struct pud_req *pdr)
199 1.1 pooka {
200 1.1 pooka int rv;
201 1.1 pooka
202 1.1 pooka switch (pdr->pdr_reqtype) {
203 1.1 pooka case PUD_CONF_REG:
204 1.1 pooka rv = pudconf_reg(pd, (struct pud_conf_reg *)pdr);
205 1.1 pooka break;
206 1.1 pooka case PUD_CONF_DEREG:
207 1.1 pooka /* unimplemented */
208 1.1 pooka rv = 0;
209 1.1 pooka break;
210 1.1 pooka case PUD_CONF_IOCTL:
211 1.1 pooka /* unimplemented */
212 1.1 pooka rv = 0;
213 1.1 pooka break;
214 1.1 pooka default:
215 1.1 pooka rv = EINVAL;
216 1.1 pooka break;
217 1.1 pooka }
218 1.1 pooka
219 1.1 pooka return rv;
220 1.1 pooka }
221 1.1 pooka
222 1.1 pooka static int
223 1.1 pooka pud_putter_dispatch(void *this, struct putter_hdr *pth)
224 1.1 pooka {
225 1.1 pooka struct pud_dev *pd = this;
226 1.1 pooka struct pud_req *pdr = (void *)pth;
227 1.1 pooka int rv;
228 1.1 pooka
229 1.1 pooka if (pdr->pdr_pth.pth_framelen < sizeof(struct pud_req))
230 1.1 pooka return EINVAL;
231 1.1 pooka
232 1.1 pooka switch (pdr->pdr_reqclass) {
233 1.1 pooka case PUD_REQ_CDEV:
234 1.1 pooka case PUD_REQ_BDEV:
235 1.1 pooka rv = pudop_dev(pd, pdr);
236 1.1 pooka break;
237 1.1 pooka case PUD_REQ_CONF:
238 1.1 pooka rv = pudop_conf(pd, pdr);
239 1.1 pooka break;
240 1.1 pooka default:
241 1.1 pooka rv = EINVAL;
242 1.1 pooka break;
243 1.1 pooka }
244 1.1 pooka
245 1.1 pooka return rv;
246 1.1 pooka }
247 1.1 pooka
248 1.1 pooka /* Device server severed the umbilical cord */
249 1.1 pooka static int
250 1.1 pooka pud_putter_close(void *this)
251 1.1 pooka {
252 1.1 pooka struct pud_dev *pd = this;
253 1.1 pooka struct pud_touser *putp;
254 1.1 pooka
255 1.1 pooka mutex_enter(&pud_mtx);
256 1.1 pooka LIST_REMOVE(pd, pd_entries);
257 1.1 pooka mutex_exit(&pud_mtx);
258 1.1 pooka
259 1.1 pooka mutex_enter(&pd->pd_mtx);
260 1.1 pooka while ((putp = TAILQ_FIRST(&pd->pd_waitq_req)) != NULL) {
261 1.1 pooka putp->pt_pdr->pdr_rv = ENXIO;
262 1.1 pooka cv_signal(&putp->pt_cv);
263 1.1 pooka TAILQ_REMOVE(&pd->pd_waitq_req, putp, pt_entries);
264 1.1 pooka }
265 1.1 pooka
266 1.1 pooka while ((putp = TAILQ_FIRST(&pd->pd_waitq_resp)) != NULL) {
267 1.1 pooka putp->pt_pdr->pdr_rv = ENXIO;
268 1.1 pooka cv_signal(&putp->pt_cv);
269 1.1 pooka TAILQ_REMOVE(&pd->pd_waitq_resp, putp, pt_entries);
270 1.1 pooka }
271 1.1 pooka if (pd->pd_waitcount)
272 1.1 pooka cv_wait(&pd->pd_draincv, &pd->pd_mtx);
273 1.1 pooka KASSERT(pd->pd_waitcount == 0);
274 1.1 pooka
275 1.1 pooka mutex_exit(&pd->pd_mtx);
276 1.1 pooka
277 1.1 pooka if (pd->pd_dev)
278 1.1 pooka devsw_detach(&pud_bdevsw /* XXX */, &pud_cdevsw);
279 1.1 pooka
280 1.1 pooka putter_detach(pd->pd_pi);
281 1.1 pooka kmem_free(pd, sizeof(struct pud_dev));
282 1.1 pooka
283 1.1 pooka return 0;
284 1.1 pooka }
285 1.1 pooka
286 1.1 pooka struct pud_dev *
287 1.1 pooka pud_dev2pud(dev_t dev)
288 1.1 pooka {
289 1.1 pooka struct pud_dev *pd;
290 1.1 pooka
291 1.1 pooka mutex_enter(&pud_mtx);
292 1.1 pooka LIST_FOREACH(pd, &pudlist, pd_entries)
293 1.1 pooka if (major(pd->pd_dev) == major(dev))
294 1.1 pooka break;
295 1.1 pooka mutex_exit(&pud_mtx);
296 1.1 pooka
297 1.1 pooka return pd;
298 1.1 pooka }
299 1.1 pooka
300 1.1 pooka /* Toss request to the device server and wait for result */
301 1.1 pooka int
302 1.1 pooka pud_request(dev_t dev, void *data, size_t dlen, int class, int type)
303 1.1 pooka {
304 1.1 pooka struct pud_touser put;
305 1.1 pooka struct pud_req *pdr = data;
306 1.1 pooka struct pud_dev *pd;
307 1.1 pooka
308 1.1 pooka pd = pud_dev2pud(dev);
309 1.1 pooka if (pd == NULL)
310 1.1 pooka return ENXIO;
311 1.1 pooka
312 1.1 pooka pdr->pdr_dev = dev;
313 1.1 pooka pdr->pdr_len = pdr->pdr_pth.pth_framelen = dlen;
314 1.1 pooka pdr->pdr_reqid = nextreq(pd);
315 1.1 pooka
316 1.1 pooka pdr->pdr_reqclass = class;
317 1.1 pooka pdr->pdr_reqtype = type;
318 1.1 pooka
319 1.1 pooka put.pt_pdr = pdr;
320 1.1 pooka cv_init(&put.pt_cv, "pudresp");
321 1.1 pooka
322 1.1 pooka mutex_enter(&pd->pd_mtx);
323 1.1 pooka pd->pd_waitcount++;
324 1.1 pooka
325 1.1 pooka TAILQ_INSERT_TAIL(&pd->pd_waitq_req, &put, pt_entries);
326 1.1 pooka putter_notify(pd->pd_pi);
327 1.1 pooka cv_broadcast(&pd->pd_waitq_req_cv);
328 1.1 pooka cv_wait(&put.pt_cv, &pd->pd_mtx);
329 1.1 pooka
330 1.1 pooka if (--pd->pd_waitcount == 0)
331 1.1 pooka cv_signal(&pd->pd_draincv);
332 1.1 pooka mutex_exit(&pd->pd_mtx);
333 1.1 pooka
334 1.1 pooka return pdr->pdr_rv;
335 1.1 pooka }
336 1.1 pooka
337 1.1 pooka /* Called from putter based on minor dev number */
338 1.1 pooka int
339 1.1 pooka pud_config(int fd, int flags, int fmt)
340 1.1 pooka {
341 1.1 pooka struct pud_dev *pd;
342 1.1 pooka
343 1.1 pooka pd = kmem_zalloc(sizeof(struct pud_dev), KM_SLEEP);
344 1.1 pooka pd->pd_pi = putter_attach(curlwp->l_proc->p_pid, fd, pd, &pud_putter);
345 1.1 pooka if (pd->pd_pi == NULL) {
346 1.1 pooka kmem_free(pd, sizeof(struct pud_dev));
347 1.1 pooka return ENOENT; /* XXX */
348 1.1 pooka }
349 1.1 pooka pd->pd_dev = NODEV;
350 1.1 pooka
351 1.1 pooka mutex_init(&pd->pd_mtx, MUTEX_DEFAULT, IPL_NONE);
352 1.1 pooka TAILQ_INIT(&pd->pd_waitq_req);
353 1.1 pooka TAILQ_INIT(&pd->pd_waitq_resp);
354 1.1 pooka cv_init(&pd->pd_waitq_req_cv, "pudreq");
355 1.1 pooka cv_init(&pd->pd_draincv, "pudrain");
356 1.1 pooka
357 1.1 pooka mutex_enter(&pud_mtx);
358 1.1 pooka LIST_INSERT_HEAD(&pudlist, pd, pd_entries);
359 1.1 pooka mutex_exit(&pud_mtx);
360 1.1 pooka
361 1.1 pooka return 0;
362 1.1 pooka }
363 1.1 pooka
364 1.1 pooka void
365 1.1 pooka pudattach()
366 1.1 pooka {
367 1.1 pooka int error;
368 1.1 pooka
369 1.1 pooka if ((error = putter_register(pud_config, PUTTER_MINOR_PUD)) != 0) {
370 1.1 pooka printf("pudattach: can't register to putter: %d\n", error);
371 1.1 pooka return;
372 1.1 pooka }
373 1.1 pooka mutex_init(&pud_mtx, MUTEX_DEFAULT, IPL_NONE);
374 1.1 pooka }
375