Home | History | Annotate | Line # | Download | only in pcmcia
com_pcmcia.c revision 1.9.2.1
      1 /*	$NetBSD: com_pcmcia.c,v 1.9.2.1 1998/08/08 03:06:50 eeh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1993, 1994, 1995, 1996
      5  *	Charles M. Hannum.  All rights reserved.
      6  * Copyright (c) 1991 The Regents of the University of California.
      7  * All rights reserved.
      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  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by the University of
     20  *	California, Berkeley and its contributors.
     21  * 4. Neither the name of the University nor the names of its contributors
     22  *    may be used to endorse or promote products derived from this software
     23  *    without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  * SUCH DAMAGE.
     36  *
     37  *	@(#)com.c	7.5 (Berkeley) 5/16/91
     38  */
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/ioctl.h>
     43 #include <sys/select.h>
     44 #include <sys/tty.h>
     45 #include <sys/proc.h>
     46 #include <sys/user.h>
     47 #include <sys/conf.h>
     48 #include <sys/file.h>
     49 #include <sys/uio.h>
     50 #include <sys/kernel.h>
     51 #include <sys/syslog.h>
     52 #include <sys/types.h>
     53 #include <sys/device.h>
     54 
     55 #include <machine/intr.h>
     56 #include <machine/bus.h>
     57 
     58 #include <dev/pcmcia/pcmciavar.h>
     59 #include <dev/pcmcia/pcmciareg.h>
     60 #include <dev/pcmcia/pcmciadevs.h>
     61 
     62 #include <dev/ic/comreg.h>
     63 #include <dev/ic/comvar.h>
     64 
     65 #include <dev/isa/isareg.h>
     66 
     67 struct com_dev {
     68 	char *name;
     69 	int manufacturer;
     70 	int product;
     71 	char *cis1_info[4];
     72 };
     73 
     74 static struct com_dev com_devs[] = {
     75 	{ PCMCIA_STR_3COM_3C562,
     76 	  PCMCIA_VENDOR_3COM, PCMCIA_PRODUCT_3COM_3C562,
     77 	  PCMCIA_CIS_3COM_3C562 },
     78 
     79 	{ PCMCIA_STR_MOTOROLA_POWER144,
     80 	  PCMCIA_VENDOR_MOTOROLA, PCMCIA_PRODUCT_MOTOROLA_POWER144,
     81 	  PCMCIA_CIS_MOTOROLA_POWER144 },
     82 
     83 	{ PCMCIA_STR_IBM_HOME_AND_AWAY,
     84 	  PCMCIA_VENDOR_IBM, PCMCIA_PRODUCT_IBM_HOME_AND_AWAY,
     85 	  PCMCIA_CIS_IBM_HOME_AND_AWAY },
     86 
     87 	{ PCMCIA_STR_MEGAHERTZ_XJ4288,
     88 	  PCMCIA_VENDOR_MEGAHERTZ, PCMCIA_PRODUCT_MEGAHERTZ_XJ4288,
     89 	  PCMCIA_CIS_MEGAHERTZ_XJ4288 },
     90 
     91 	{ PCMCIA_STR_MEGAHERTZ_XJ4288,
     92 	  PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_MEGAHERTZ_XJ4288,
     93 	  PCMCIA_CIS_MEGAHERTZ_XJ4288 },
     94 
     95 	{ PCMCIA_STR_USROBOTICS_WORLDPORT144,
     96 	  PCMCIA_VENDOR_USROBOTICS, PCMCIA_PRODUCT_USROBOTICS_WORLDPORT144,
     97 	  PCMCIA_CIS_USROBOTICS_WORLDPORT144 },
     98 
     99 	{ PCMCIA_STR_SOCKET_PAGECARD,
    100 	  PCMCIA_VENDOR_SOCKET, PCMCIA_PRODUCT_SOCKET_PAGECARD,
    101 	  PCMCIA_CIS_SOCKET_PAGECARD },
    102 
    103 	{ PCMCIA_STR_MOTOROLA_PM100C,
    104 	  PCMCIA_VENDOR_MOTOROLA, PCMCIA_PRODUCT_MOTOROLA_PM100C,
    105 	  PCMCIA_CIS_MOTOROLA_PM100C },
    106 
    107 	{ PCMCIA_STR_SIMPLETECH_COMMUNICATOR288,
    108 	  PCMCIA_VENDOR_SIMPLETECH, PCMCIA_PRODUCT_SIMPLETECH_COMMUNICATOR288,
    109 	  PCMCIA_CIS_SIMPLETECH_COMMUNICATOR288 },
    110 };
    111 
    112 static struct com_dev generic = {
    113 	"Generic Modem",
    114 	PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
    115 	PCMCIA_CIS_INVALID
    116 };
    117 
    118 static int com_devs_size = sizeof(com_devs) / sizeof(com_devs[0]);
    119 static struct com_dev *com_dev_match __P((struct pcmcia_card *));
    120 
    121 int com_pcmcia_match __P((struct device *, struct cfdata *, void *));
    122 void com_pcmcia_attach __P((struct device *, struct device *, void *));
    123 void com_pcmcia_cleanup __P((void *));
    124 
    125 int com_pcmcia_enable __P((struct com_softc *));
    126 void com_pcmcia_disable __P((struct com_softc *));
    127 int com_pcmcia_enable1 __P((struct com_softc *));
    128 void com_pcmcia_disable1 __P((struct com_softc *));
    129 
    130 struct com_pcmcia_softc {
    131 	struct com_softc sc_com;		/* real "com" softc */
    132 
    133 	/* PCMCIA-specific goo */
    134 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
    135 	int sc_io_window;			/* our i/o window */
    136 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
    137 	void *sc_ih;				/* interrupt handler */
    138 };
    139 
    140 struct cfattach com_pcmcia_ca = {
    141 	sizeof(struct com_pcmcia_softc), com_pcmcia_match, com_pcmcia_attach
    142 };
    143 
    144 static struct com_dev *
    145 com_dev_match(card)
    146 	struct pcmcia_card *card;
    147 {
    148 	int i;
    149 
    150 	/* 1. check our table */
    151 	for (i = 0; i < com_devs_size; i++) {
    152 		if (com_devs[i].manufacturer != -1) {
    153 			/* manufacturer/product match */
    154 			if (com_devs[i].manufacturer == card->manufacturer &&
    155 			    com_devs[i].product == card->product)
    156 				return &com_devs[i];
    157 		} else {
    158 			/* cis strings match */
    159 			int j;
    160 
    161 			for (j = 0; j < 4; j++)
    162 				if (com_devs[i].cis1_info[j] &&
    163 				    strcmp(com_devs[i].cis1_info[j],
    164 				    card->cis1_info[j]))
    165 					break;
    166 			if (j == 4)
    167 				return &com_devs[i];
    168 		}
    169 	}
    170 
    171 
    172 	/*
    173 	 * 2. Be selective about devices by checking for the word modem in
    174 	 *    the cis strings.
    175 	 */
    176 	for (i = 0; i < 4; i++)
    177 		if (card->cis1_info[i] &&
    178 		    pmatch(card->cis1_info[i],
    179 			   "*[Mm][Oo][Dd][Ee][Mm]*", NULL) > 0)
    180 			return &generic;
    181 
    182 	return NULL;
    183 }
    184 
    185 
    186 int
    187 com_pcmcia_match(parent, match, aux)
    188 	struct device *parent;
    189 	struct cfdata *match;
    190 	void *aux;
    191 {
    192 	struct pcmcia_attach_args *pa = aux;
    193 	struct pcmcia_config_entry *cfe;
    194 
    195 	/* find a cfe we can use (if it matches a standard COM port) */
    196 	for (cfe = pa->pf->cfe_head.sqh_first; cfe;
    197 	    cfe = cfe->cfe_list.sqe_next)
    198 		if (cfe->iospace[0].start == IO_COM1 ||
    199 		    cfe->iospace[0].start == IO_COM2 ||
    200 		    cfe->iospace[0].start == IO_COM3 ||
    201 		    cfe->iospace[0].start == IO_COM4)
    202 			break;
    203 
    204 	/* No appropriate cfe, bail out */
    205 	if (cfe == NULL)
    206 		return 0;
    207 
    208 	/*
    209 	 * We found a cfe at a standard com port location; check if it
    210 	 * is really a modem/serial device
    211 	 */
    212 	if (com_dev_match(pa->card) == NULL)
    213 		return 0;
    214 
    215 	return 1;
    216 }
    217 
    218 void
    219 com_pcmcia_attach(parent, self, aux)
    220 	struct device *parent, *self;
    221 	void *aux;
    222 {
    223 	struct com_pcmcia_softc *psc = (void *) self;
    224 	struct com_softc *sc = &psc->sc_com;
    225 	struct pcmcia_attach_args *pa = aux;
    226 	struct pcmcia_config_entry *cfe;
    227 	struct com_dev *comdev;
    228 
    229 	psc->sc_pf = pa->pf;
    230 
    231 	/* find a cfe we can use */
    232 
    233 	for (cfe = pa->pf->cfe_head.sqh_first; cfe;
    234 	     cfe = cfe->cfe_list.sqe_next) {
    235 		if (cfe->num_memspace != 0)
    236 			continue;
    237 
    238 		if (cfe->num_iospace != 1)
    239 			continue;
    240 
    241 		if (cfe->iomask == 3) {
    242 			if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length,
    243 			    cfe->iospace[0].length, &psc->sc_pcioh)) {
    244 				continue;
    245 			}
    246 		} else {
    247 			if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
    248 			    cfe->iospace[0].length, 0, &psc->sc_pcioh)) {
    249 				continue;
    250 			}
    251 		}
    252 
    253 		break;
    254 	}
    255 
    256 	if (!cfe) {
    257 		printf(": can't allocate i/o space\n");
    258 		return;
    259 	}
    260 
    261 	sc->sc_iot = psc->sc_pcioh.iot;
    262 	sc->sc_ioh = psc->sc_pcioh.ioh;
    263 
    264 	/* Enable the card. */
    265 	pcmcia_function_init(pa->pf, cfe);
    266 	if (com_pcmcia_enable1(sc))
    267 		printf(": function enable failed\n");
    268 
    269 	sc->enabled = 1;
    270 
    271 	/* map in the io space */
    272 
    273 	if (pcmcia_io_map(pa->pf, ((cfe->flags & PCMCIA_CFE_IO16) ?
    274 	    PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8), 0, psc->sc_pcioh.size,
    275 	    &psc->sc_pcioh, &psc->sc_io_window)) {
    276 		printf(": can't map i/o space\n");
    277 		return;
    278 	}
    279 
    280 	sc->sc_iobase = -1;
    281 	sc->sc_frequency = COM_FREQ;
    282 
    283 	sc->enable = com_pcmcia_enable;
    284 	sc->disable = com_pcmcia_disable;
    285 
    286 	if ((comdev = com_dev_match(pa->card)) != NULL)
    287 		printf(": %s", comdev->name);
    288 
    289 	com_attach_subr(sc);
    290 
    291 	sc->enabled = 0;
    292 
    293 	com_pcmcia_disable1(sc);
    294 }
    295 
    296 int
    297 com_pcmcia_enable(sc)
    298 	struct com_softc *sc;
    299 {
    300 	struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
    301 	struct pcmcia_function *pf = psc->sc_pf;
    302 
    303 	/* establish the interrupt. */
    304 	psc->sc_ih = pcmcia_intr_establish(pf, IPL_SERIAL, comintr, sc);
    305 	if (psc->sc_ih == NULL) {
    306 		printf("%s: couldn't establish interrupt\n",
    307 		       sc->sc_dev.dv_xname);
    308 		return (1);
    309 	}
    310 	return com_pcmcia_enable1(sc);
    311 }
    312 
    313 int
    314 com_pcmcia_enable1(sc)
    315 	struct com_softc *sc;
    316 {
    317 	struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
    318 	struct pcmcia_function *pf = psc->sc_pf;
    319 	int ret;
    320 
    321 	if ((ret = pcmcia_function_enable(pf)))
    322 	    return(ret);
    323 
    324 	if (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3C562) {
    325 		int reg;
    326 
    327 		/* turn off the ethernet-disable bit */
    328 
    329 		reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
    330 		if (reg & 0x08) {
    331 		    reg &= ~0x08;
    332 		    pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    333 		}
    334 	}
    335 
    336 	return(ret);
    337 }
    338 
    339 void
    340 com_pcmcia_disable(sc)
    341 	struct com_softc *sc;
    342 {
    343 	struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
    344 
    345 	com_pcmcia_disable1(sc);
    346 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    347 }
    348 
    349 void
    350 com_pcmcia_disable1(sc)
    351 	struct com_softc *sc;
    352 {
    353 	struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
    354 
    355 	pcmcia_function_disable(psc->sc_pf);
    356 }
    357