1 /* $NetBSD: irframe.c,v 1.44 2009/12/06 22:40:56 dyoung Exp $ */ 2 3 /* 4 * Copyright (c) 2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Lennart Augustsson (lennart@augustsson.net). 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: irframe.c,v 1.44 2009/12/06 22:40:56 dyoung Exp $"); 34 35 #include "irframe.h" 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/ioctl.h> 40 #include <sys/kernel.h> 41 #include <sys/device.h> 42 #include <sys/conf.h> 43 #include <sys/malloc.h> 44 #include <sys/poll.h> 45 #include <sys/select.h> 46 #include <sys/vnode.h> 47 48 #include <dev/ir/ir.h> 49 #include <dev/ir/irdaio.h> 50 #include <dev/ir/irframevar.h> 51 52 #ifdef IRFRAME_DEBUG 53 #define DPRINTF(x) if (irframedebug) printf x 54 #define Static 55 int irframedebug = 0; 56 #else 57 #define DPRINTF(x) 58 #define Static static 59 #endif 60 61 dev_type_open(irframeopen); 62 dev_type_close(irframeclose); 63 dev_type_read(irframeread); 64 dev_type_write(irframewrite); 65 dev_type_ioctl(irframeioctl); 66 dev_type_poll(irframepoll); 67 dev_type_kqfilter(irframekqfilter); 68 69 const struct cdevsw irframe_cdevsw = { 70 irframeopen, irframeclose, irframeread, irframewrite, irframeioctl, 71 nostop, notty, irframepoll, nommap, irframekqfilter, D_OTHER, 72 }; 73 74 int irframe_match(device_t parent, cfdata_t match, void *aux); 75 76 Static int irf_set_params(struct irframe_softc *sc, struct irda_params *p); 77 Static int irf_reset_params(struct irframe_softc *sc); 78 79 #if NIRFRAME == 0 80 /* In case we just have tty attachment. */ 81 CFDRIVER_DECL(irframe, DV_DULL, NULL); 82 #endif 83 84 CFATTACH_DECL_NEW(irframe, sizeof(struct irframe_softc), 85 irframe_match, irframe_attach, irframe_detach, NULL); 86 87 extern struct cfdriver irframe_cd; 88 89 #define IRFRAMEUNIT(dev) (minor(dev)) 90 91 int 92 irframe_match(device_t parent, cfdata_t match, void *aux) 93 { 94 struct ir_attach_args *ia = aux; 95 96 return (ia->ia_type == IR_TYPE_IRFRAME); 97 } 98 99 void 100 irframe_attach(device_t parent, device_t self, void *aux) 101 { 102 struct irframe_softc *sc = device_private(self); 103 struct ir_attach_args *ia = aux; 104 const char *delim; 105 int speeds = 0; 106 107 sc->sc_dev = self; 108 sc->sc_methods = ia->ia_methods; 109 sc->sc_handle = ia->ia_handle; 110 111 #ifdef DIAGNOSTIC 112 if (sc->sc_methods->im_read == NULL || 113 sc->sc_methods->im_write == NULL || 114 sc->sc_methods->im_poll == NULL || 115 sc->sc_methods->im_kqfilter == NULL || 116 sc->sc_methods->im_set_params == NULL || 117 sc->sc_methods->im_get_speeds == NULL || 118 sc->sc_methods->im_get_turnarounds == NULL) 119 panic("%s: missing methods", device_xname(self)); 120 #endif 121 122 (void)sc->sc_methods->im_get_speeds(sc->sc_handle, &speeds); 123 sc->sc_speedmask = speeds; 124 delim = ":"; 125 if (speeds & IRDA_SPEEDS_SIR) { 126 printf("%s SIR", delim); 127 delim = ","; 128 } 129 if (speeds & IRDA_SPEEDS_MIR) { 130 printf("%s MIR", delim); 131 delim = ","; 132 } 133 if (speeds & IRDA_SPEEDS_FIR) { 134 printf("%s FIR", delim); 135 delim = ","; 136 } 137 if (speeds & IRDA_SPEEDS_VFIR) { 138 printf("%s VFIR", delim); 139 delim = ","; 140 } 141 printf("\n"); 142 143 if (!pmf_device_register(self, NULL, NULL)) 144 aprint_error_dev(self, "couldn't establish power handler\n"); 145 } 146 147 int 148 irframe_detach(device_t self, int flags) 149 { 150 /*struct irframe_softc *sc = device_private(self);*/ 151 int maj, mn; 152 153 pmf_device_deregister(self); 154 155 /* XXX needs reference count */ 156 157 /* locate the major number */ 158 maj = cdevsw_lookup_major(&irframe_cdevsw); 159 160 /* Nuke the vnodes for any open instances (calls close). */ 161 mn = device_unit(self); 162 vdevgone(maj, mn, mn, VCHR); 163 164 return (0); 165 } 166 167 int 168 irframeopen(dev_t dev, int flag, int mode, struct lwp *l) 169 { 170 struct irframe_softc *sc; 171 int error; 172 173 sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev)); 174 if (sc == NULL) 175 return (ENXIO); 176 if (!device_is_active(sc->sc_dev)) 177 return (EIO); 178 if (sc->sc_open) 179 return (EBUSY); 180 if (sc->sc_methods->im_open != NULL) { 181 error = sc->sc_methods->im_open(sc->sc_handle, flag, mode, l); 182 if (error) 183 return (error); 184 } 185 sc->sc_open = 1; 186 #ifdef DIAGNOSTIC 187 sc->sc_speed = IRDA_DEFAULT_SPEED; 188 #endif 189 (void)irf_reset_params(sc); 190 return (0); 191 } 192 193 int 194 irframeclose(dev_t dev, int flag, int mode, struct lwp *l) 195 { 196 struct irframe_softc *sc; 197 int error; 198 199 sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev)); 200 if (sc == NULL) 201 return (ENXIO); 202 sc->sc_open = 0; 203 if (sc->sc_methods->im_close != NULL) 204 error = sc->sc_methods->im_close(sc->sc_handle, flag, mode, l); 205 else 206 error = 0; 207 return (error); 208 } 209 210 int 211 irframeread(dev_t dev, struct uio *uio, int flag) 212 { 213 struct irframe_softc *sc; 214 215 sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev)); 216 if (sc == NULL) 217 return (ENXIO); 218 if (!device_is_active(sc->sc_dev) || !sc->sc_open) 219 return (EIO); 220 if (uio->uio_resid < sc->sc_params.maxsize) { 221 #ifdef DIAGNOSTIC 222 printf("irframeread: short read %ld < %d\n", 223 (long)uio->uio_resid, sc->sc_params.maxsize); 224 #endif 225 return (EINVAL); 226 } 227 return (sc->sc_methods->im_read(sc->sc_handle, uio, flag)); 228 } 229 230 int 231 irframewrite(dev_t dev, struct uio *uio, int flag) 232 { 233 struct irframe_softc *sc; 234 235 sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev)); 236 if (sc == NULL) 237 return (ENXIO); 238 if (!device_is_active(sc->sc_dev) || !sc->sc_open) 239 return (EIO); 240 if (uio->uio_resid > sc->sc_params.maxsize) { 241 #ifdef DIAGNOSTIC 242 printf("irframeread: long write %ld > %d\n", 243 (long)uio->uio_resid, sc->sc_params.maxsize); 244 #endif 245 return (EINVAL); 246 } 247 return (sc->sc_methods->im_write(sc->sc_handle, uio, flag)); 248 } 249 250 int 251 irf_set_params(struct irframe_softc *sc, struct irda_params *p) 252 { 253 int error; 254 255 DPRINTF(("irf_set_params: set params speed=%u ebofs=%u maxsize=%u " 256 "speedmask=0x%x\n", p->speed, p->ebofs, p->maxsize, 257 sc->sc_speedmask)); 258 259 if (p->maxsize > IRDA_MAX_FRAME_SIZE) { 260 #ifdef IRFRAME_DEBUG 261 printf("irf_set_params: bad maxsize=%u\n", p->maxsize); 262 #endif 263 return (EINVAL); 264 } 265 266 if (p->ebofs > IRDA_MAX_EBOFS) { 267 #ifdef IRFRAME_DEBUG 268 printf("irf_set_params: bad maxsize=%u\n", p->maxsize); 269 #endif 270 return (EINVAL); 271 } 272 273 #define CONC(x,y) x##y 274 #define CASE(s) case s: if (!(sc->sc_speedmask & CONC(IRDA_SPEED_,s))) return (EINVAL); break 275 switch (p->speed) { 276 CASE(2400); 277 CASE(9600); 278 CASE(19200); 279 CASE(38400); 280 CASE(57600); 281 CASE(115200); 282 CASE(576000); 283 CASE(1152000); 284 CASE(4000000); 285 CASE(16000000); 286 default: return (EINVAL); 287 } 288 #undef CONC 289 #undef CASE 290 291 error = sc->sc_methods->im_set_params(sc->sc_handle, p); 292 if (!error) { 293 sc->sc_params = *p; 294 DPRINTF(("irf_set_params: ok\n")); 295 #ifdef DIAGNOSTIC 296 if (p->speed != sc->sc_speed) { 297 sc->sc_speed = p->speed; 298 aprint_verbose_dev(sc->sc_dev, "set speed %u\n", 299 sc->sc_speed); 300 } 301 #endif 302 } else { 303 #ifdef IRFRAME_DEBUG 304 printf("irf_set_params: error=%d\n", error); 305 #endif 306 } 307 return (error); 308 } 309 310 int 311 irf_reset_params(struct irframe_softc *sc) 312 { 313 struct irda_params params; 314 315 params.speed = IRDA_DEFAULT_SPEED; 316 params.ebofs = IRDA_DEFAULT_EBOFS; 317 params.maxsize = IRDA_DEFAULT_SIZE; 318 return (irf_set_params(sc, ¶ms)); 319 } 320 321 int 322 irframeioctl(dev_t dev, u_long cmd, void *addr, int flag, 323 struct lwp *l) 324 { 325 struct irframe_softc *sc; 326 void *vaddr = addr; 327 int error; 328 329 sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev)); 330 if (sc == NULL) 331 return (ENXIO); 332 if (!device_is_active(sc->sc_dev) || !sc->sc_open) 333 return (EIO); 334 335 switch (cmd) { 336 case FIONBIO: 337 /* All handled in the upper FS layer. */ 338 error = 0; 339 break; 340 341 case IRDA_SET_PARAMS: 342 error = irf_set_params(sc, vaddr); 343 break; 344 345 case IRDA_RESET_PARAMS: 346 error = irf_reset_params(sc); 347 break; 348 349 case IRDA_GET_SPEEDMASK: 350 error = sc->sc_methods->im_get_speeds(sc->sc_handle, vaddr); 351 break; 352 353 case IRDA_GET_TURNAROUNDMASK: 354 error = sc->sc_methods->im_get_turnarounds(sc->sc_handle,vaddr); 355 break; 356 357 default: 358 error = EINVAL; 359 break; 360 } 361 return (error); 362 } 363 364 int 365 irframepoll(dev_t dev, int events, struct lwp *l) 366 { 367 struct irframe_softc *sc; 368 369 sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev)); 370 if (sc == NULL) 371 return (POLLHUP); 372 if (!device_is_active(sc->sc_dev) || !sc->sc_open) 373 return (POLLHUP); 374 375 return (sc->sc_methods->im_poll(sc->sc_handle, events, l)); 376 } 377 378 int 379 irframekqfilter(dev_t dev, struct knote *kn) 380 { 381 struct irframe_softc *sc; 382 383 sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev)); 384 if (!device_is_active(sc->sc_dev) || !sc->sc_open) 385 return (1); 386 387 return (sc->sc_methods->im_kqfilter(sc->sc_handle, kn)); 388 } 389