Home | History | Annotate | Line # | Download | only in ir
irframe.c revision 1.11
      1  1.11  augustss /*	$NetBSD: irframe.c,v 1.11 2001/12/13 15:09:07 augustss Exp $	*/
      2   1.1  augustss 
      3   1.1  augustss /*
      4   1.1  augustss  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5   1.1  augustss  * All rights reserved.
      6   1.1  augustss  *
      7   1.1  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1  augustss  * by Lennart Augustsson (lennart (at) augustsson.net).
      9   1.1  augustss  *
     10   1.1  augustss  * Redistribution and use in source and binary forms, with or without
     11   1.1  augustss  * modification, are permitted provided that the following conditions
     12   1.1  augustss  * are met:
     13   1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     14   1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     15   1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  augustss  *    documentation and/or other materials provided with the distribution.
     18   1.1  augustss  * 3. All advertising materials mentioning features or use of this software
     19   1.1  augustss  *    must display the following acknowledgement:
     20   1.1  augustss  *        This product includes software developed by the NetBSD
     21   1.1  augustss  *        Foundation, Inc. and its contributors.
     22   1.1  augustss  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1  augustss  *    contributors may be used to endorse or promote products derived
     24   1.1  augustss  *    from this software without specific prior written permission.
     25   1.1  augustss  *
     26   1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1  augustss  */
     38   1.1  augustss 
     39   1.4  augustss #include "irframe.h"
     40   1.4  augustss 
     41   1.1  augustss #include <sys/param.h>
     42   1.1  augustss #include <sys/systm.h>
     43   1.1  augustss #include <sys/ioctl.h>
     44   1.1  augustss #include <sys/kernel.h>
     45   1.1  augustss #include <sys/device.h>
     46   1.1  augustss #include <sys/conf.h>
     47   1.1  augustss #include <sys/malloc.h>
     48   1.1  augustss #include <sys/poll.h>
     49   1.1  augustss #include <sys/select.h>
     50   1.1  augustss #include <sys/vnode.h>
     51   1.1  augustss 
     52   1.1  augustss #include <dev/ir/ir.h>
     53   1.1  augustss #include <dev/ir/irdaio.h>
     54   1.1  augustss #include <dev/ir/irframevar.h>
     55   1.1  augustss 
     56   1.1  augustss cdev_decl(irframe);
     57   1.1  augustss 
     58   1.1  augustss int irframe_match(struct device *parent, struct cfdata *match, void *aux);
     59   1.1  augustss void irframe_attach(struct device *parent, struct device *self, void *aux);
     60   1.1  augustss int irframe_activate(struct device *self, enum devact act);
     61   1.1  augustss int irframe_detach(struct device *self, int flags);
     62   1.1  augustss 
     63   1.6  augustss static int irf_reset_params(struct irframe_softc *sc);
     64   1.6  augustss 
     65   1.4  augustss #if NIRFRAME == 0
     66   1.4  augustss /* In case we just have tty attachment. */
     67   1.4  augustss struct cfdriver irframe_cd = {
     68   1.4  augustss 	NULL, "irframe", DV_DULL
     69   1.4  augustss };
     70   1.4  augustss #endif
     71   1.4  augustss 
     72   1.1  augustss struct cfattach irframe_ca = {
     73   1.1  augustss 	sizeof(struct irframe_softc), irframe_match, irframe_attach,
     74   1.1  augustss 	irframe_detach, irframe_activate
     75   1.1  augustss };
     76   1.1  augustss 
     77   1.1  augustss extern struct cfattach irframe_ca;
     78   1.1  augustss extern struct cfdriver irframe_cd;
     79   1.1  augustss 
     80   1.1  augustss #define IRFRAMEUNIT(dev) (minor(dev))
     81   1.1  augustss 
     82   1.1  augustss int
     83   1.1  augustss irframe_match(struct device *parent, struct cfdata *match, void *aux)
     84   1.1  augustss {
     85   1.1  augustss 	struct ir_attach_args *ia = aux;
     86   1.1  augustss 
     87   1.1  augustss 	return (ia->ia_type == IR_TYPE_IRFRAME);
     88   1.1  augustss }
     89   1.1  augustss 
     90   1.1  augustss void
     91   1.1  augustss irframe_attach(struct device *parent, struct device *self, void *aux)
     92   1.1  augustss {
     93   1.1  augustss 	struct irframe_softc *sc = (struct irframe_softc *)self;
     94   1.1  augustss 	struct ir_attach_args *ia = aux;
     95   1.1  augustss 	const char *delim;
     96   1.1  augustss 	int speeds = 0;
     97   1.1  augustss 
     98   1.1  augustss 	sc->sc_methods = ia->ia_methods;
     99   1.1  augustss 	sc->sc_handle = ia->ia_handle;
    100   1.1  augustss 
    101   1.1  augustss #ifdef DIAGNOSTIC
    102   1.1  augustss 	if (sc->sc_methods->im_read == NULL ||
    103   1.1  augustss 	    sc->sc_methods->im_write == NULL ||
    104   1.4  augustss 	    sc->sc_methods->im_poll == NULL ||
    105   1.1  augustss 	    sc->sc_methods->im_set_params == NULL ||
    106   1.1  augustss 	    sc->sc_methods->im_get_speeds == NULL ||
    107   1.1  augustss 	    sc->sc_methods->im_get_turnarounds == NULL)
    108   1.1  augustss 		panic("%s: missing methods\n", sc->sc_dev.dv_xname);
    109   1.1  augustss #endif
    110   1.1  augustss 
    111   1.1  augustss 	(void)sc->sc_methods->im_get_speeds(sc->sc_handle, &speeds);
    112  1.11  augustss 	sc->sc_speedmask = speeds;
    113   1.1  augustss 	delim = ":";
    114   1.1  augustss 	if (speeds & IRDA_SPEEDS_SIR) {
    115   1.1  augustss 		printf("%s SIR", delim);
    116   1.1  augustss 		delim = ",";
    117   1.1  augustss 	}
    118   1.1  augustss 	if (speeds & IRDA_SPEEDS_MIR) {
    119   1.1  augustss 		printf("%s MIR", delim);
    120   1.1  augustss 		delim = ",";
    121   1.1  augustss 	}
    122   1.1  augustss 	if (speeds & IRDA_SPEEDS_FIR) {
    123   1.1  augustss 		printf("%s FIR", delim);
    124   1.1  augustss 		delim = ",";
    125   1.1  augustss 	}
    126   1.1  augustss 	if (speeds & IRDA_SPEEDS_VFIR) {
    127   1.1  augustss 		printf("%s VFIR", delim);
    128   1.1  augustss 		delim = ",";
    129   1.1  augustss 	}
    130   1.1  augustss 	printf("\n");
    131   1.1  augustss }
    132   1.1  augustss 
    133   1.1  augustss int
    134   1.1  augustss irframe_activate(struct device *self, enum devact act)
    135   1.1  augustss {
    136   1.1  augustss 	/*struct irframe_softc *sc = (struct irframe_softc *)self;*/
    137   1.1  augustss 
    138   1.1  augustss 	switch (act) {
    139   1.1  augustss 	case DVACT_ACTIVATE:
    140   1.1  augustss 		return (EOPNOTSUPP);
    141   1.1  augustss 		break;
    142   1.1  augustss 
    143   1.1  augustss 	case DVACT_DEACTIVATE:
    144   1.1  augustss 		break;
    145   1.1  augustss 	}
    146   1.1  augustss 	return (0);
    147   1.1  augustss }
    148   1.1  augustss 
    149   1.1  augustss int
    150   1.1  augustss irframe_detach(struct device *self, int flags)
    151   1.1  augustss {
    152   1.1  augustss 	/*struct irframe_softc *sc = (struct irframe_softc *)self;*/
    153   1.1  augustss 	int maj, mn;
    154   1.1  augustss 
    155   1.1  augustss 	/* locate the major number */
    156   1.1  augustss 	for (maj = 0; maj < nchrdev; maj++)
    157   1.1  augustss 		if (cdevsw[maj].d_open == irframeopen)
    158   1.1  augustss 			break;
    159   1.1  augustss 
    160   1.1  augustss 	/* Nuke the vnodes for any open instances (calls close). */
    161   1.1  augustss 	mn = self->dv_unit;
    162   1.1  augustss 	vdevgone(maj, mn, mn, VCHR);
    163   1.1  augustss 
    164   1.1  augustss 	return (0);
    165   1.1  augustss }
    166   1.1  augustss 
    167   1.1  augustss int
    168   1.1  augustss irframeopen(dev_t dev, int flag, int mode, struct proc *p)
    169   1.1  augustss {
    170   1.1  augustss 	struct irframe_softc *sc;
    171   1.1  augustss 	int error;
    172   1.1  augustss 
    173   1.1  augustss 	sc = device_lookup(&irframe_cd, IRFRAMEUNIT(dev));
    174   1.1  augustss 	if (sc == NULL)
    175   1.1  augustss 		return (ENXIO);
    176   1.1  augustss 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    177   1.1  augustss 		return (EIO);
    178   1.1  augustss 	if (sc->sc_open)
    179   1.1  augustss 		return (EBUSY);
    180   1.1  augustss 	if (sc->sc_methods->im_open != NULL) {
    181   1.1  augustss 		error = sc->sc_methods->im_open(sc->sc_handle, flag, mode, p);
    182   1.1  augustss 		if (error)
    183   1.1  augustss 			return (error);
    184   1.1  augustss 	}
    185   1.1  augustss 	sc->sc_open = 1;
    186  1.11  augustss #ifdef DIAGNOSTIC
    187  1.11  augustss 	sc->sc_speed = IRDA_DEFAULT_SPEED;
    188  1.11  augustss #endif
    189   1.8  augustss 	(void)irf_reset_params(sc);
    190   1.1  augustss 	return (0);
    191   1.1  augustss }
    192   1.1  augustss 
    193   1.1  augustss int
    194   1.1  augustss irframeclose(dev_t dev, int flag, int mode, struct proc *p)
    195   1.1  augustss {
    196   1.1  augustss 	struct irframe_softc *sc;
    197   1.1  augustss 	int error;
    198   1.1  augustss 
    199   1.1  augustss 	sc = device_lookup(&irframe_cd, IRFRAMEUNIT(dev));
    200   1.1  augustss 	if (sc == NULL)
    201   1.1  augustss 		return (ENXIO);
    202   1.1  augustss 	if (sc->sc_methods->im_close != NULL)
    203   1.1  augustss 		error = sc->sc_methods->im_close(sc->sc_handle, flag, mode, p);
    204   1.1  augustss 	else
    205   1.1  augustss 		error = 0;
    206   1.1  augustss 	sc->sc_open = 0;
    207   1.1  augustss 	return (error);
    208   1.1  augustss }
    209   1.1  augustss 
    210   1.1  augustss int
    211   1.1  augustss irframeread(dev_t dev, struct uio *uio, int flag)
    212   1.1  augustss {
    213   1.1  augustss 	struct irframe_softc *sc;
    214   1.1  augustss 
    215   1.1  augustss 	sc = device_lookup(&irframe_cd, IRFRAMEUNIT(dev));
    216   1.1  augustss 	if (sc == NULL)
    217   1.1  augustss 		return (ENXIO);
    218   1.1  augustss 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    219   1.1  augustss 		return (EIO);
    220  1.11  augustss 	if (uio->uio_resid < sc->sc_params.maxsize)
    221  1.11  augustss 		return (EINVAL);
    222   1.1  augustss 	return (sc->sc_methods->im_read(sc->sc_handle, uio, flag));
    223   1.1  augustss }
    224   1.1  augustss 
    225   1.1  augustss int
    226   1.1  augustss irframewrite(dev_t dev, struct uio *uio, int flag)
    227   1.1  augustss {
    228   1.1  augustss 	struct irframe_softc *sc;
    229   1.1  augustss 
    230   1.1  augustss 	sc = device_lookup(&irframe_cd, IRFRAMEUNIT(dev));
    231   1.1  augustss 	if (sc == NULL)
    232   1.1  augustss 		return (ENXIO);
    233   1.1  augustss 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    234   1.1  augustss 		return (EIO);
    235  1.11  augustss 	if (uio->uio_resid > sc->sc_params.maxsize)
    236  1.11  augustss 		return (EINVAL);
    237   1.2  augustss 	return (sc->sc_methods->im_write(sc->sc_handle, uio, flag));
    238   1.1  augustss }
    239   1.1  augustss 
    240   1.1  augustss int
    241   1.6  augustss irf_reset_params(struct irframe_softc *sc)
    242   1.6  augustss {
    243   1.6  augustss 	struct irda_params params;
    244   1.6  augustss 
    245   1.6  augustss 	params.speed = IRDA_DEFAULT_SPEED;
    246   1.6  augustss 	params.ebofs = IRDA_DEFAULT_EBOFS;
    247   1.6  augustss 	params.maxsize = IRDA_DEFAULT_SIZE;
    248   1.6  augustss 	return (sc->sc_methods->im_set_params(sc->sc_handle, &params));
    249   1.6  augustss }
    250   1.6  augustss 
    251   1.6  augustss int
    252   1.1  augustss irframeioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
    253   1.1  augustss {
    254   1.1  augustss 	struct irframe_softc *sc;
    255   1.2  augustss 	void *vaddr = addr;
    256  1.11  augustss 	struct irda_params *parms = vaddr;
    257   1.1  augustss 	int error;
    258   1.1  augustss 
    259   1.1  augustss 	sc = device_lookup(&irframe_cd, IRFRAMEUNIT(dev));
    260   1.1  augustss 	if (sc == NULL)
    261   1.1  augustss 		return (ENXIO);
    262   1.1  augustss 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    263   1.1  augustss 		return (EIO);
    264   1.1  augustss 
    265   1.1  augustss 	switch (cmd) {
    266   1.1  augustss 	case FIONBIO:
    267   1.1  augustss 		/* All handled in the upper FS layer. */
    268   1.1  augustss 		error = 0;
    269   1.1  augustss 		break;
    270   1.1  augustss 
    271   1.1  augustss 	case IRDA_SET_PARAMS:
    272  1.10  augustss #ifdef DIAGNOSTIC
    273  1.11  augustss 		if (parms->speed != sc->sc_speed) {
    274  1.11  augustss 			sc->sc_speed = parms->speed;
    275  1.10  augustss 			printf("%s: set speed %u\n", sc->sc_dev.dv_xname,
    276  1.10  augustss 			       sc->sc_speed);
    277  1.10  augustss 		}
    278  1.10  augustss #endif
    279  1.11  augustss 		if (parms->maxsize > IRDA_MAX_FRAME_SIZE)
    280  1.11  augustss 			return (EINVAL);
    281  1.11  augustss 		if (parms->ebofs > IRDA_MAX_EBOFS)
    282  1.11  augustss 			return (EINVAL);
    283  1.11  augustss 		/* XXX check speed */
    284   1.2  augustss 		error = sc->sc_methods->im_set_params(sc->sc_handle, vaddr);
    285  1.11  augustss 		if (!error)
    286  1.11  augustss 			sc->sc_params = *parms;
    287   1.1  augustss 		break;
    288   1.1  augustss 
    289   1.1  augustss 	case IRDA_RESET_PARAMS:
    290   1.7  augustss 		error = irf_reset_params(sc);
    291   1.1  augustss 		break;
    292   1.1  augustss 
    293   1.1  augustss 	case IRDA_GET_SPEEDMASK:
    294   1.2  augustss 		error = sc->sc_methods->im_get_speeds(sc->sc_handle, vaddr);
    295   1.1  augustss 		break;
    296   1.1  augustss 
    297   1.1  augustss 	case IRDA_GET_TURNAROUNDMASK:
    298   1.2  augustss 		error = sc->sc_methods->im_get_turnarounds(sc->sc_handle,vaddr);
    299   1.1  augustss 		break;
    300   1.1  augustss 
    301   1.1  augustss 	default:
    302   1.1  augustss 		error = EINVAL;
    303   1.1  augustss 		break;
    304   1.1  augustss 	}
    305   1.1  augustss 	return (error);
    306   1.1  augustss }
    307   1.1  augustss 
    308   1.1  augustss int
    309   1.1  augustss irframepoll(dev_t dev, int events, struct proc *p)
    310   1.1  augustss {
    311   1.1  augustss 	struct irframe_softc *sc;
    312   1.1  augustss 
    313   1.1  augustss 	sc = device_lookup(&irframe_cd, IRFRAMEUNIT(dev));
    314   1.1  augustss 	if (sc == NULL)
    315   1.1  augustss 		return (ENXIO);
    316   1.1  augustss 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    317   1.1  augustss 		return (EIO);
    318   1.1  augustss 
    319   1.4  augustss 	return (sc->sc_methods->im_poll(sc->sc_handle, events, p));
    320   1.1  augustss }
    321   1.1  augustss 
    322   1.1  augustss /*********/
    323   1.1  augustss 
    324   1.1  augustss 
    325   1.1  augustss struct device *
    326   1.9  augustss irframe_alloc(size_t size, const struct irframe_methods *m, void *h)
    327   1.1  augustss {
    328   1.1  augustss 	struct cfdriver *cd = &irframe_cd;
    329   1.1  augustss 	struct device *dev;
    330   1.4  augustss 	struct ir_attach_args ia;
    331   1.1  augustss 	int unit;
    332   1.1  augustss 
    333   1.1  augustss 	for (unit = 0; unit < cd->cd_ndevs; unit++)
    334   1.1  augustss 		if (cd->cd_devs[unit] == NULL)
    335   1.1  augustss 			break;
    336   1.1  augustss 	dev = malloc(size, M_DEVBUF, M_WAITOK);
    337   1.1  augustss 	memset(dev, 0, size);
    338   1.1  augustss 	snprintf(dev->dv_xname, sizeof dev->dv_xname, "irframe%d", unit);
    339   1.1  augustss 	dev->dv_unit = unit;
    340   1.1  augustss 	dev->dv_flags = DVF_ACTIVE;	/* always initially active */
    341   1.1  augustss 
    342   1.1  augustss 	config_makeroom(unit, cd);
    343   1.1  augustss 	cd->cd_devs[unit] = dev;
    344   1.4  augustss 
    345   1.4  augustss 	ia.ia_methods = m;
    346   1.4  augustss 	ia.ia_handle = h;
    347   1.6  augustss 	printf("%s", dev->dv_xname);
    348   1.4  augustss 	irframe_attach(NULL, dev, &ia);
    349   1.1  augustss 
    350   1.1  augustss 	return (dev);
    351   1.1  augustss }
    352   1.1  augustss 
    353   1.1  augustss void
    354   1.1  augustss irframe_dealloc(struct device *dev)
    355   1.1  augustss {
    356   1.1  augustss 	struct cfdriver *cd = &irframe_cd;
    357   1.1  augustss 	int unit;
    358   1.1  augustss 
    359   1.1  augustss 	for (unit = 0; unit < cd->cd_ndevs; unit++) {
    360   1.1  augustss 		if (cd->cd_devs[unit] == dev) {
    361   1.1  augustss 			cd->cd_devs[unit] = NULL;
    362   1.1  augustss 			free(dev, M_DEVBUF);
    363   1.1  augustss 			return;
    364   1.1  augustss 		}
    365   1.1  augustss 	}
    366   1.1  augustss 	panic("irframe_dealloc: device not found\n");
    367   1.1  augustss }
    368