pud.c revision 1.3.2.2 1 1.3.2.2 joerg /* $NetBSD: pud.c,v 1.3.2.2 2007/11/21 21:55:43 joerg Exp $ */
2 1.3.2.2 joerg
3 1.3.2.2 joerg /*
4 1.3.2.2 joerg * Copyright (c) 2007 Antti Kantee. All Rights Reserved.
5 1.3.2.2 joerg *
6 1.3.2.2 joerg * Development of this software was supported by the
7 1.3.2.2 joerg * Research Foundation of Helsinki University of Technology
8 1.3.2.2 joerg *
9 1.3.2.2 joerg * Redistribution and use in source and binary forms, with or without
10 1.3.2.2 joerg * modification, are permitted provided that the following conditions
11 1.3.2.2 joerg * are met:
12 1.3.2.2 joerg * 1. Redistributions of source code must retain the above copyright
13 1.3.2.2 joerg * notice, this list of conditions and the following disclaimer.
14 1.3.2.2 joerg * 2. Redistributions in binary form must reproduce the above copyright
15 1.3.2.2 joerg * notice, this list of conditions and the following disclaimer in the
16 1.3.2.2 joerg * documentation and/or other materials provided with the distribution.
17 1.3.2.2 joerg *
18 1.3.2.2 joerg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 1.3.2.2 joerg * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 1.3.2.2 joerg * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 1.3.2.2 joerg * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 1.3.2.2 joerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.3.2.2 joerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 1.3.2.2 joerg * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.3.2.2 joerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 1.3.2.2 joerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.3.2.2 joerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.3.2.2 joerg * SUCH DAMAGE.
29 1.3.2.2 joerg */
30 1.3.2.2 joerg
31 1.3.2.2 joerg #include <sys/cdefs.h>
32 1.3.2.2 joerg __KERNEL_RCSID(0, "$NetBSD: pud.c,v 1.3.2.2 2007/11/21 21:55:43 joerg Exp $");
33 1.3.2.2 joerg
34 1.3.2.2 joerg #include <sys/param.h>
35 1.3.2.2 joerg #include <sys/conf.h>
36 1.3.2.2 joerg #include <sys/kmem.h>
37 1.3.2.2 joerg #include <sys/poll.h>
38 1.3.2.2 joerg #include <sys/queue.h>
39 1.3.2.2 joerg
40 1.3.2.2 joerg #include <dev/pud/pud_sys.h>
41 1.3.2.2 joerg #include <dev/putter/putter_sys.h>
42 1.3.2.2 joerg
43 1.3.2.2 joerg void pudattach(void);
44 1.3.2.2 joerg
45 1.3.2.2 joerg static int pud_putter_getout(void *, size_t, int, uint8_t **,
46 1.3.2.2 joerg size_t *, void **);
47 1.3.2.2 joerg static void pud_putter_releaseout(void *, void *, int);
48 1.3.2.2 joerg static int pud_putter_dispatch(void *, struct putter_hdr *);
49 1.3.2.2 joerg static size_t pud_putter_waitcount(void *);
50 1.3.2.2 joerg static int pud_putter_close(void *);
51 1.3.2.2 joerg
52 1.3.2.2 joerg struct putter_ops pud_putter = {
53 1.3.2.2 joerg .pop_getout = pud_putter_getout,
54 1.3.2.2 joerg .pop_releaseout = pud_putter_releaseout,
55 1.3.2.2 joerg .pop_waitcount = pud_putter_waitcount,
56 1.3.2.2 joerg .pop_dispatch = pud_putter_dispatch,
57 1.3.2.2 joerg .pop_close = pud_putter_close,
58 1.3.2.2 joerg };
59 1.3.2.2 joerg
60 1.3.2.2 joerg extern struct bdevsw pud_bdevsw;
61 1.3.2.2 joerg extern struct cdevsw pud_cdevsw;
62 1.3.2.2 joerg
63 1.3.2.2 joerg kmutex_t pud_mtx;
64 1.3.2.2 joerg static LIST_HEAD(, pud_dev) pudlist = LIST_HEAD_INITIALIZER(pudlist);
65 1.3.2.2 joerg
66 1.3.2.2 joerg static uint64_t
67 1.3.2.2 joerg nextreq(struct pud_dev *pd)
68 1.3.2.2 joerg {
69 1.3.2.2 joerg uint64_t rv;
70 1.3.2.2 joerg
71 1.3.2.2 joerg mutex_enter(&pd->pd_mtx);
72 1.3.2.2 joerg rv = pd->pd_nextreq++;
73 1.3.2.2 joerg mutex_exit(&pd->pd_mtx);
74 1.3.2.2 joerg
75 1.3.2.2 joerg return rv;
76 1.3.2.2 joerg }
77 1.3.2.2 joerg
78 1.3.2.2 joerg static int
79 1.3.2.2 joerg pud_putter_getout(void *this, size_t maxsize, int nonblock,
80 1.3.2.2 joerg uint8_t **data, size_t *dlen, void **cookie)
81 1.3.2.2 joerg {
82 1.3.2.2 joerg struct pud_dev *pd = this;
83 1.3.2.2 joerg struct pud_touser *putp;
84 1.3.2.2 joerg int error = 0;
85 1.3.2.2 joerg
86 1.3.2.2 joerg mutex_enter(&pd->pd_mtx);
87 1.3.2.2 joerg for (;;) {
88 1.3.2.2 joerg if (TAILQ_EMPTY(&pd->pd_waitq_req)) {
89 1.3.2.2 joerg if (nonblock) {
90 1.3.2.2 joerg error = EWOULDBLOCK;
91 1.3.2.2 joerg break;
92 1.3.2.2 joerg }
93 1.3.2.2 joerg
94 1.3.2.2 joerg error = cv_wait_sig(&pd->pd_waitq_req_cv, &pd->pd_mtx);
95 1.3.2.2 joerg if (error)
96 1.3.2.2 joerg break;
97 1.3.2.2 joerg else
98 1.3.2.2 joerg continue;
99 1.3.2.2 joerg }
100 1.3.2.2 joerg
101 1.3.2.2 joerg putp = TAILQ_FIRST(&pd->pd_waitq_req);
102 1.3.2.2 joerg TAILQ_REMOVE(&pd->pd_waitq_req, putp, pt_entries);
103 1.3.2.2 joerg KASSERT(error == 0);
104 1.3.2.2 joerg break;
105 1.3.2.2 joerg }
106 1.3.2.2 joerg mutex_exit(&pd->pd_mtx);
107 1.3.2.2 joerg
108 1.3.2.2 joerg if (error == 0) {
109 1.3.2.2 joerg *data = (uint8_t *)putp->pt_pdr;
110 1.3.2.2 joerg *dlen = putp->pt_pdr->pdr_pth.pth_framelen;
111 1.3.2.2 joerg *cookie = putp;
112 1.3.2.2 joerg }
113 1.3.2.2 joerg
114 1.3.2.2 joerg return error;
115 1.3.2.2 joerg }
116 1.3.2.2 joerg
117 1.3.2.2 joerg static void
118 1.3.2.2 joerg pud_putter_releaseout(void *this, void *cookie, int status)
119 1.3.2.2 joerg {
120 1.3.2.2 joerg struct pud_dev *pd = this;
121 1.3.2.2 joerg struct pud_touser *putp = cookie;
122 1.3.2.2 joerg
123 1.3.2.2 joerg mutex_enter(&pd->pd_mtx);
124 1.3.2.2 joerg TAILQ_INSERT_TAIL(&pd->pd_waitq_resp, putp, pt_entries);
125 1.3.2.2 joerg mutex_exit(&pd->pd_mtx);
126 1.3.2.2 joerg
127 1.3.2.2 joerg }
128 1.3.2.2 joerg
129 1.3.2.2 joerg static size_t
130 1.3.2.2 joerg pud_putter_waitcount(void *this)
131 1.3.2.2 joerg {
132 1.3.2.2 joerg struct pud_dev *pd = this;
133 1.3.2.2 joerg size_t rv;
134 1.3.2.2 joerg
135 1.3.2.2 joerg mutex_enter(&pd->pd_mtx);
136 1.3.2.2 joerg rv = pd->pd_waitcount;
137 1.3.2.2 joerg mutex_exit(&pd->pd_mtx);
138 1.3.2.2 joerg
139 1.3.2.2 joerg return rv;
140 1.3.2.2 joerg }
141 1.3.2.2 joerg
142 1.3.2.2 joerg static int
143 1.3.2.2 joerg pudop_dev(struct pud_dev *pd, struct pud_req *pdr)
144 1.3.2.2 joerg {
145 1.3.2.2 joerg struct putter_hdr *pth = (void *)pdr;
146 1.3.2.2 joerg struct pud_touser *putp;
147 1.3.2.2 joerg
148 1.3.2.2 joerg mutex_enter(&pd->pd_mtx);
149 1.3.2.2 joerg TAILQ_FOREACH(putp, &pd->pd_waitq_resp, pt_entries)
150 1.3.2.2 joerg if (putp->pt_pdr->pdr_reqid == pdr->pdr_reqid)
151 1.3.2.2 joerg break;
152 1.3.2.2 joerg if (putp == NULL) {
153 1.3.2.2 joerg mutex_exit(&pd->pd_mtx);
154 1.3.2.2 joerg return EINVAL;
155 1.3.2.2 joerg }
156 1.3.2.2 joerg TAILQ_REMOVE(&pd->pd_waitq_resp, putp, pt_entries);
157 1.3.2.2 joerg mutex_exit(&pd->pd_mtx);
158 1.3.2.2 joerg
159 1.3.2.2 joerg if (pth->pth_framelen > putp->pt_pdr->pdr_len) {
160 1.3.2.2 joerg return EINVAL;
161 1.3.2.2 joerg }
162 1.3.2.2 joerg memcpy(putp->pt_pdr, pth, pth->pth_framelen);
163 1.3.2.2 joerg cv_signal(&putp->pt_cv);
164 1.3.2.2 joerg
165 1.3.2.2 joerg return 0;
166 1.3.2.2 joerg }
167 1.3.2.2 joerg
168 1.3.2.2 joerg /*
169 1.3.2.2 joerg * Register our major number. Always register char device functions,
170 1.3.2.2 joerg * register block devices optionally.
171 1.3.2.2 joerg *
172 1.3.2.2 joerg * XXX: no way to configure "any major you like" currently.
173 1.3.2.2 joerg */
174 1.3.2.2 joerg static int
175 1.3.2.2 joerg pudconf_reg(struct pud_dev *pd, struct pud_conf_reg *pcr)
176 1.3.2.2 joerg {
177 1.3.2.2 joerg struct bdevsw *bsw;
178 1.3.2.2 joerg int cmajor, bmajor, error;
179 1.3.2.2 joerg
180 1.3.2.2 joerg cmajor = major(pcr->pm_regdev);
181 1.3.2.2 joerg if (pcr->pm_flags & PUD_CONFFLAG_BDEV) {
182 1.3.2.2 joerg bsw = &pud_bdevsw;
183 1.3.2.2 joerg bmajor = cmajor;
184 1.3.2.2 joerg } else {
185 1.3.2.2 joerg bsw = NULL;
186 1.3.2.2 joerg bmajor = -1;
187 1.3.2.2 joerg }
188 1.3.2.2 joerg
189 1.3.2.2 joerg pcr->pm_devname[PUD_DEVNAME_MAX] = '\0';
190 1.3.2.2 joerg error = devsw_attach(pcr->pm_devname, bsw, &bmajor,
191 1.3.2.2 joerg &pud_cdevsw, &cmajor);
192 1.3.2.2 joerg if (error == 0)
193 1.3.2.2 joerg pd->pd_dev = pcr->pm_regdev;
194 1.3.2.2 joerg
195 1.3.2.2 joerg return error;
196 1.3.2.2 joerg }
197 1.3.2.2 joerg
198 1.3.2.2 joerg static int
199 1.3.2.2 joerg pudop_conf(struct pud_dev *pd, struct pud_req *pdr)
200 1.3.2.2 joerg {
201 1.3.2.2 joerg int rv;
202 1.3.2.2 joerg
203 1.3.2.2 joerg switch (pdr->pdr_reqtype) {
204 1.3.2.2 joerg case PUD_CONF_REG:
205 1.3.2.2 joerg rv = pudconf_reg(pd, (struct pud_conf_reg *)pdr);
206 1.3.2.2 joerg break;
207 1.3.2.2 joerg case PUD_CONF_DEREG:
208 1.3.2.2 joerg /* unimplemented */
209 1.3.2.2 joerg rv = 0;
210 1.3.2.2 joerg break;
211 1.3.2.2 joerg case PUD_CONF_IOCTL:
212 1.3.2.2 joerg /* unimplemented */
213 1.3.2.2 joerg rv = 0;
214 1.3.2.2 joerg break;
215 1.3.2.2 joerg default:
216 1.3.2.2 joerg rv = EINVAL;
217 1.3.2.2 joerg break;
218 1.3.2.2 joerg }
219 1.3.2.2 joerg
220 1.3.2.2 joerg return rv;
221 1.3.2.2 joerg }
222 1.3.2.2 joerg
223 1.3.2.2 joerg static int
224 1.3.2.2 joerg pud_putter_dispatch(void *this, struct putter_hdr *pth)
225 1.3.2.2 joerg {
226 1.3.2.2 joerg struct pud_dev *pd = this;
227 1.3.2.2 joerg struct pud_req *pdr = (void *)pth;
228 1.3.2.2 joerg int rv;
229 1.3.2.2 joerg
230 1.3.2.2 joerg if (pdr->pdr_pth.pth_framelen < sizeof(struct pud_req))
231 1.3.2.2 joerg return EINVAL;
232 1.3.2.2 joerg
233 1.3.2.2 joerg switch (pdr->pdr_reqclass) {
234 1.3.2.2 joerg case PUD_REQ_CDEV:
235 1.3.2.2 joerg case PUD_REQ_BDEV:
236 1.3.2.2 joerg rv = pudop_dev(pd, pdr);
237 1.3.2.2 joerg break;
238 1.3.2.2 joerg case PUD_REQ_CONF:
239 1.3.2.2 joerg rv = pudop_conf(pd, pdr);
240 1.3.2.2 joerg break;
241 1.3.2.2 joerg default:
242 1.3.2.2 joerg rv = EINVAL;
243 1.3.2.2 joerg break;
244 1.3.2.2 joerg }
245 1.3.2.2 joerg
246 1.3.2.2 joerg return rv;
247 1.3.2.2 joerg }
248 1.3.2.2 joerg
249 1.3.2.2 joerg /* Device server severed the umbilical cord */
250 1.3.2.2 joerg static int
251 1.3.2.2 joerg pud_putter_close(void *this)
252 1.3.2.2 joerg {
253 1.3.2.2 joerg struct pud_dev *pd = this;
254 1.3.2.2 joerg struct pud_touser *putp;
255 1.3.2.2 joerg
256 1.3.2.2 joerg mutex_enter(&pud_mtx);
257 1.3.2.2 joerg LIST_REMOVE(pd, pd_entries);
258 1.3.2.2 joerg mutex_exit(&pud_mtx);
259 1.3.2.2 joerg
260 1.3.2.2 joerg mutex_enter(&pd->pd_mtx);
261 1.3.2.2 joerg while ((putp = TAILQ_FIRST(&pd->pd_waitq_req)) != NULL) {
262 1.3.2.2 joerg putp->pt_pdr->pdr_rv = ENXIO;
263 1.3.2.2 joerg cv_signal(&putp->pt_cv);
264 1.3.2.2 joerg TAILQ_REMOVE(&pd->pd_waitq_req, putp, pt_entries);
265 1.3.2.2 joerg }
266 1.3.2.2 joerg
267 1.3.2.2 joerg while ((putp = TAILQ_FIRST(&pd->pd_waitq_resp)) != NULL) {
268 1.3.2.2 joerg putp->pt_pdr->pdr_rv = ENXIO;
269 1.3.2.2 joerg cv_signal(&putp->pt_cv);
270 1.3.2.2 joerg TAILQ_REMOVE(&pd->pd_waitq_resp, putp, pt_entries);
271 1.3.2.2 joerg }
272 1.3.2.2 joerg if (pd->pd_waitcount)
273 1.3.2.2 joerg cv_wait(&pd->pd_draincv, &pd->pd_mtx);
274 1.3.2.2 joerg KASSERT(pd->pd_waitcount == 0);
275 1.3.2.2 joerg
276 1.3.2.2 joerg mutex_exit(&pd->pd_mtx);
277 1.3.2.2 joerg
278 1.3.2.2 joerg if (pd->pd_dev)
279 1.3.2.2 joerg devsw_detach(&pud_bdevsw /* XXX */, &pud_cdevsw);
280 1.3.2.2 joerg
281 1.3.2.2 joerg putter_detach(pd->pd_pi);
282 1.3.2.2 joerg kmem_free(pd, sizeof(struct pud_dev));
283 1.3.2.2 joerg
284 1.3.2.2 joerg return 0;
285 1.3.2.2 joerg }
286 1.3.2.2 joerg
287 1.3.2.2 joerg struct pud_dev *
288 1.3.2.2 joerg pud_dev2pud(dev_t dev)
289 1.3.2.2 joerg {
290 1.3.2.2 joerg struct pud_dev *pd;
291 1.3.2.2 joerg
292 1.3.2.2 joerg mutex_enter(&pud_mtx);
293 1.3.2.2 joerg LIST_FOREACH(pd, &pudlist, pd_entries)
294 1.3.2.2 joerg if (major(pd->pd_dev) == major(dev))
295 1.3.2.2 joerg break;
296 1.3.2.2 joerg mutex_exit(&pud_mtx);
297 1.3.2.2 joerg
298 1.3.2.2 joerg return pd;
299 1.3.2.2 joerg }
300 1.3.2.2 joerg
301 1.3.2.2 joerg /* Toss request to the device server and wait for result */
302 1.3.2.2 joerg int
303 1.3.2.2 joerg pud_request(dev_t dev, void *data, size_t dlen, int class, int type)
304 1.3.2.2 joerg {
305 1.3.2.2 joerg struct pud_touser put;
306 1.3.2.2 joerg struct pud_req *pdr = data;
307 1.3.2.2 joerg struct pud_dev *pd;
308 1.3.2.2 joerg
309 1.3.2.2 joerg pd = pud_dev2pud(dev);
310 1.3.2.2 joerg if (pd == NULL)
311 1.3.2.2 joerg return ENXIO;
312 1.3.2.2 joerg
313 1.3.2.2 joerg pdr->pdr_dev = dev;
314 1.3.2.2 joerg pdr->pdr_len = pdr->pdr_pth.pth_framelen = dlen;
315 1.3.2.2 joerg pdr->pdr_reqid = nextreq(pd);
316 1.3.2.2 joerg
317 1.3.2.2 joerg pdr->pdr_reqclass = class;
318 1.3.2.2 joerg pdr->pdr_reqtype = type;
319 1.3.2.2 joerg
320 1.3.2.2 joerg put.pt_pdr = pdr;
321 1.3.2.2 joerg cv_init(&put.pt_cv, "pudresp");
322 1.3.2.2 joerg
323 1.3.2.2 joerg mutex_enter(&pd->pd_mtx);
324 1.3.2.2 joerg pd->pd_waitcount++;
325 1.3.2.2 joerg
326 1.3.2.2 joerg TAILQ_INSERT_TAIL(&pd->pd_waitq_req, &put, pt_entries);
327 1.3.2.2 joerg putter_notify(pd->pd_pi);
328 1.3.2.2 joerg cv_broadcast(&pd->pd_waitq_req_cv);
329 1.3.2.2 joerg cv_wait(&put.pt_cv, &pd->pd_mtx);
330 1.3.2.2 joerg
331 1.3.2.2 joerg if (--pd->pd_waitcount == 0)
332 1.3.2.2 joerg cv_signal(&pd->pd_draincv);
333 1.3.2.2 joerg mutex_exit(&pd->pd_mtx);
334 1.3.2.2 joerg
335 1.3.2.2 joerg return pdr->pdr_rv;
336 1.3.2.2 joerg }
337 1.3.2.2 joerg
338 1.3.2.2 joerg /* Called from putter based on minor dev number */
339 1.3.2.2 joerg int
340 1.3.2.2 joerg pud_config(int fd, int flags, int fmt)
341 1.3.2.2 joerg {
342 1.3.2.2 joerg struct pud_dev *pd;
343 1.3.2.2 joerg
344 1.3.2.2 joerg pd = kmem_zalloc(sizeof(struct pud_dev), KM_SLEEP);
345 1.3.2.2 joerg pd->pd_pi = putter_attach(curlwp->l_proc->p_pid, fd, pd, &pud_putter);
346 1.3.2.2 joerg if (pd->pd_pi == NULL) {
347 1.3.2.2 joerg kmem_free(pd, sizeof(struct pud_dev));
348 1.3.2.2 joerg return ENOENT; /* XXX */
349 1.3.2.2 joerg }
350 1.3.2.2 joerg pd->pd_dev = NODEV;
351 1.3.2.2 joerg
352 1.3.2.2 joerg mutex_init(&pd->pd_mtx, MUTEX_DEFAULT, IPL_NONE);
353 1.3.2.2 joerg TAILQ_INIT(&pd->pd_waitq_req);
354 1.3.2.2 joerg TAILQ_INIT(&pd->pd_waitq_resp);
355 1.3.2.2 joerg cv_init(&pd->pd_waitq_req_cv, "pudreq");
356 1.3.2.2 joerg cv_init(&pd->pd_draincv, "pudrain");
357 1.3.2.2 joerg
358 1.3.2.2 joerg mutex_enter(&pud_mtx);
359 1.3.2.2 joerg LIST_INSERT_HEAD(&pudlist, pd, pd_entries);
360 1.3.2.2 joerg mutex_exit(&pud_mtx);
361 1.3.2.2 joerg
362 1.3.2.2 joerg return 0;
363 1.3.2.2 joerg }
364 1.3.2.2 joerg
365 1.3.2.2 joerg void
366 1.3.2.2 joerg pudattach()
367 1.3.2.2 joerg {
368 1.3.2.2 joerg int error;
369 1.3.2.2 joerg
370 1.3.2.2 joerg if ((error = putter_register(pud_config, PUTTER_MINOR_PUD)) != 0) {
371 1.3.2.2 joerg printf("pudattach: can't register to putter: %d\n", error);
372 1.3.2.2 joerg return;
373 1.3.2.2 joerg }
374 1.3.2.2 joerg mutex_init(&pud_mtx, MUTEX_DEFAULT, IPL_NONE);
375 1.3.2.2 joerg }
376