1 /* $NetBSD: pud.c,v 1.6 2007/11/28 17:01:59 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.6 2007/11/28 17:01:59 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 = 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 int cmajor, bmajor, error; 179 180 if (pcr->pm_version != (PUD_DEVELVERSION | PUD_VERSION)) { 181 printf("pud version mismatch %d vs %d\n", 182 pcr->pm_version & ~PUD_DEVELVERSION, PUD_VERSION); 183 return EINVAL; /* XXX */ 184 } 185 186 cmajor = major(pcr->pm_regdev); 187 if (pcr->pm_flags & PUD_CONFFLAG_BDEV) { 188 bsw = &pud_bdevsw; 189 bmajor = cmajor; 190 } else { 191 bsw = NULL; 192 bmajor = -1; 193 } 194 195 pcr->pm_devname[PUD_DEVNAME_MAX] = '\0'; 196 error = devsw_attach(pcr->pm_devname, bsw, &bmajor, 197 &pud_cdevsw, &cmajor); 198 if (error == 0) 199 pd->pd_dev = pcr->pm_regdev; 200 201 return error; 202 } 203 204 static int 205 pudop_conf(struct pud_dev *pd, struct pud_req *pdr) 206 { 207 int rv; 208 209 switch (pdr->pdr_reqtype) { 210 case PUD_CONF_REG: 211 rv = pudconf_reg(pd, (struct pud_conf_reg *)pdr); 212 break; 213 case PUD_CONF_DEREG: 214 /* unimplemented */ 215 rv = 0; 216 break; 217 default: 218 rv = EINVAL; 219 break; 220 } 221 222 return rv; 223 } 224 225 static int 226 pud_putter_dispatch(void *this, struct putter_hdr *pth) 227 { 228 struct pud_dev *pd = this; 229 struct pud_req *pdr = (void *)pth; 230 int rv; 231 232 if (pdr->pdr_pth.pth_framelen < sizeof(struct pud_req)) 233 return EINVAL; 234 235 switch (pdr->pdr_reqclass) { 236 case PUD_REQ_CDEV: 237 case PUD_REQ_BDEV: 238 rv = pudop_dev(pd, pdr); 239 break; 240 case PUD_REQ_CONF: 241 rv = pudop_conf(pd, pdr); 242 break; 243 default: 244 rv = EINVAL; 245 break; 246 } 247 248 return rv; 249 } 250 251 /* Device server severed the umbilical cord */ 252 static int 253 pud_putter_close(void *this) 254 { 255 struct pud_dev *pd = this; 256 struct pud_touser *putp; 257 258 mutex_enter(&pud_mtx); 259 LIST_REMOVE(pd, pd_entries); 260 mutex_exit(&pud_mtx); 261 262 mutex_enter(&pd->pd_mtx); 263 while ((putp = TAILQ_FIRST(&pd->pd_waitq_req)) != NULL) { 264 putp->pt_pdr->pdr_rv = ENXIO; 265 cv_signal(&putp->pt_cv); 266 TAILQ_REMOVE(&pd->pd_waitq_req, putp, pt_entries); 267 } 268 269 while ((putp = TAILQ_FIRST(&pd->pd_waitq_resp)) != NULL) { 270 putp->pt_pdr->pdr_rv = ENXIO; 271 cv_signal(&putp->pt_cv); 272 TAILQ_REMOVE(&pd->pd_waitq_resp, putp, pt_entries); 273 } 274 if (pd->pd_waitcount) 275 cv_wait(&pd->pd_draincv, &pd->pd_mtx); 276 KASSERT(pd->pd_waitcount == 0); 277 278 mutex_exit(&pd->pd_mtx); 279 280 if (pd->pd_dev) 281 devsw_detach(&pud_bdevsw /* XXX */, &pud_cdevsw); 282 283 putter_detach(pd->pd_pi); 284 285 mutex_destroy(&pd->pd_mtx); 286 cv_destroy(&pd->pd_draincv); 287 cv_destroy(&pd->pd_waitq_req_cv); 288 kmem_free(pd, sizeof(struct pud_dev)); 289 290 return 0; 291 } 292 293 struct pud_dev * 294 pud_dev2pud(dev_t dev) 295 { 296 struct pud_dev *pd; 297 298 mutex_enter(&pud_mtx); 299 LIST_FOREACH(pd, &pudlist, pd_entries) 300 if (major(pd->pd_dev) == major(dev)) 301 break; 302 mutex_exit(&pud_mtx); 303 304 return pd; 305 } 306 307 /* Toss request to the device server and wait for result */ 308 int 309 pud_request(dev_t dev, void *data, size_t dlen, int class, int type) 310 { 311 struct pud_touser put; 312 struct pud_req *pdr = data; 313 struct pud_dev *pd; 314 315 pd = pud_dev2pud(dev); 316 if (pd == NULL) 317 return ENXIO; 318 319 pdr->pdr_dev = dev; 320 pdr->pdr_len = pdr->pdr_pth.pth_framelen = dlen; 321 pdr->pdr_reqid = nextreq(pd); 322 323 pdr->pdr_reqclass = class; 324 pdr->pdr_reqtype = type; 325 326 put.pt_pdr = pdr; 327 cv_init(&put.pt_cv, "pudresp"); 328 329 mutex_enter(&pd->pd_mtx); 330 pd->pd_waitcount++; 331 332 TAILQ_INSERT_TAIL(&pd->pd_waitq_req, &put, pt_entries); 333 putter_notify(pd->pd_pi); 334 cv_broadcast(&pd->pd_waitq_req_cv); 335 cv_wait(&put.pt_cv, &pd->pd_mtx); 336 337 if (--pd->pd_waitcount == 0) 338 cv_signal(&pd->pd_draincv); 339 mutex_exit(&pd->pd_mtx); 340 cv_destroy(&put.pt_cv); 341 342 return pdr->pdr_rv; 343 } 344 345 /* Called from putter based on minor dev number */ 346 int 347 pud_config(int fd, int flags, int fmt) 348 { 349 struct pud_dev *pd; 350 351 pd = kmem_zalloc(sizeof(struct pud_dev), KM_SLEEP); 352 pd->pd_pi = putter_attach(curlwp->l_proc->p_pid, fd, pd, &pud_putter); 353 if (pd->pd_pi == NULL) { 354 kmem_free(pd, sizeof(struct pud_dev)); 355 return ENOENT; /* XXX */ 356 } 357 pd->pd_dev = NODEV; 358 359 mutex_init(&pd->pd_mtx, MUTEX_DEFAULT, IPL_NONE); 360 TAILQ_INIT(&pd->pd_waitq_req); 361 TAILQ_INIT(&pd->pd_waitq_resp); 362 cv_init(&pd->pd_waitq_req_cv, "pudreq"); 363 cv_init(&pd->pd_draincv, "pudrain"); 364 365 mutex_enter(&pud_mtx); 366 LIST_INSERT_HEAD(&pudlist, pd, pd_entries); 367 mutex_exit(&pud_mtx); 368 369 return 0; 370 } 371 372 void 373 pudattach() 374 { 375 int error; 376 377 if ((error = putter_register(pud_config, PUTTER_MINOR_PUD)) != 0) { 378 printf("pudattach: can't register to putter: %d\n", error); 379 return; 380 } 381 mutex_init(&pud_mtx, MUTEX_DEFAULT, IPL_NONE); 382 } 383