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