xref: /netbsd/sys/dev/pud/pud.c (revision df7f595e)
1 /*	$NetBSD: pud.c,v 1.8 2009/03/18 10:22:41 cegger 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.8 2009/03/18 10:22:41 cegger 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 = 0;
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 		KASSERT(error == 0);
104 		break;
105 	}
106 	mutex_exit(&pd->pd_mtx);
107 
108 	if (error == 0) {
109 		*data = (uint8_t *)putp->pt_pdr;
110 		*dlen = putp->pt_pdr->pdr_pth.pth_framelen;
111 		*cookie = putp;
112 	}
113 
114 	return error;
115 }
116 
117 static void
118 pud_putter_releaseout(void *this, void *cookie, int status)
119 {
120 	struct pud_dev *pd = this;
121 	struct pud_touser *putp = cookie;
122 
123 	mutex_enter(&pd->pd_mtx);
124 	TAILQ_INSERT_TAIL(&pd->pd_waitq_resp, putp, pt_entries);
125 	mutex_exit(&pd->pd_mtx);
126 
127 }
128 
129 static size_t
130 pud_putter_waitcount(void *this)
131 {
132 	struct pud_dev *pd = this;
133 	size_t rv;
134 
135 	mutex_enter(&pd->pd_mtx);
136 	rv = pd->pd_waitcount;
137 	mutex_exit(&pd->pd_mtx);
138 
139 	return rv;
140 }
141 
142 static int
143 pudop_dev(struct pud_dev *pd, struct pud_req *pdr)
144 {
145 	struct putter_hdr *pth = (void *)pdr;
146 	struct pud_touser *putp;
147 
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;
155 	}
156 	TAILQ_REMOVE(&pd->pd_waitq_resp, putp, pt_entries);
157 	mutex_exit(&pd->pd_mtx);
158 
159 	if (pth->pth_framelen > putp->pt_pdr->pdr_len) {
160 		return EINVAL;
161 	}
162 	memcpy(putp->pt_pdr, pth, pth->pth_framelen);
163 	cv_signal(&putp->pt_cv);
164 
165 	return 0;
166 }
167 
168 /*
169  * Register our major number.  Always register char device functions,
170  * register block devices optionally.
171  *
172  * XXX: no way to configure "any major you like" currently.
173  */
174 static int
175 pudconf_reg(struct pud_dev *pd, struct pud_conf_reg *pcr)
176 {
177 	struct bdevsw *bsw;
178 	devmajor_t cmajor, bmajor;
179 	int error;
180 
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 */
185 	}
186 
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;
194 	}
195 
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;
201 
202 	return error;
203 }
204 
205 static int
206 pudop_conf(struct pud_dev *pd, struct pud_req *pdr)
207 {
208 	int rv;
209 
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;
221 	}
222 
223 	return rv;
224 }
225 
226 static int
227 pud_putter_dispatch(void *this, struct putter_hdr *pth)
228 {
229 	struct pud_dev *pd = this;
230 	struct pud_req *pdr = (void *)pth;
231 	int rv;
232 
233 	if (pdr->pdr_pth.pth_framelen < sizeof(struct pud_req))
234 		return EINVAL;
235 
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;
247 	}
248 
249 	return rv;
250 }
251 
252 /* Device server severed the umbilical cord */
253 static int
254 pud_putter_close(void *this)
255 {
256 	struct pud_dev *pd = this;
257 	struct pud_touser *putp;
258 
259 	mutex_enter(&pud_mtx);
260 	LIST_REMOVE(pd, pd_entries);
261 	mutex_exit(&pud_mtx);
262 
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);
268 	}
269 
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);
274 	}
275 	if (pd->pd_waitcount)
276 		cv_wait(&pd->pd_draincv, &pd->pd_mtx);
277 	KASSERT(pd->pd_waitcount == 0);
278 
279 	mutex_exit(&pd->pd_mtx);
280 
281 	if (pd->pd_dev)
282 		devsw_detach(&pud_bdevsw /* XXX */, &pud_cdevsw);
283 
284 	putter_detach(pd->pd_pi);
285 
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));
290 
291 	return 0;
292 }
293 
294 struct pud_dev *
295 pud_dev2pud(dev_t dev)
296 {
297 	struct pud_dev *pd;
298 
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);
304 
305 	return pd;
306 }
307 
308 /* Toss request to the device server and wait for result */
309 int
310 pud_request(dev_t dev, void *data, size_t dlen, int class, int type)
311 {
312 	struct pud_touser put;
313 	struct pud_req *pdr = data;
314 	struct pud_dev *pd;
315 
316 	pd = pud_dev2pud(dev);
317 	if (pd == NULL)
318 		return ENXIO;
319 
320 	pdr->pdr_dev = dev;
321 	pdr->pdr_len = pdr->pdr_pth.pth_framelen = dlen;
322 	pdr->pdr_reqid = nextreq(pd);
323 
324 	pdr->pdr_reqclass = class;
325 	pdr->pdr_reqtype = type;
326 
327 	put.pt_pdr = pdr;
328 	cv_init(&put.pt_cv, "pudresp");
329 
330 	mutex_enter(&pd->pd_mtx);
331 	pd->pd_waitcount++;
332 
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);
337 
338 	if (--pd->pd_waitcount == 0)
339 		cv_signal(&pd->pd_draincv);
340 	mutex_exit(&pd->pd_mtx);
341 	cv_destroy(&put.pt_cv);
342 
343 	return pdr->pdr_rv;
344 }
345 
346 /* Called from putter based on minor dev number */
347 int
348 pud_config(int fd, int flags, int fmt)
349 {
350 	struct pud_dev *pd;
351 
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 */
357 	}
358 	pd->pd_dev = NODEV;
359 
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");
365 
366 	mutex_enter(&pud_mtx);
367 	LIST_INSERT_HEAD(&pudlist, pd, pd_entries);
368 	mutex_exit(&pud_mtx);
369 
370 	return 0;
371 }
372 
373 void
374 pudattach(void)
375 {
376 	int error;
377 
378 	if ((error = putter_register(pud_config, PUTTER_MINOR_PUD)) != 0) {
379 		printf("pudattach: can't register to putter: %d\n", error);
380 		return;
381 	}
382 	mutex_init(&pud_mtx, MUTEX_DEFAULT, IPL_NONE);
383 }
384