Home | History | Annotate | Line # | Download | only in pcmcia
slhci_pcmcia.c revision 1.1.2.1
      1 /*	$NetBSD: slhci_pcmcia.c,v 1.1.2.1 2007/05/22 14:57:34 itohy Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001, 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Tetsuya Isaki.
      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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *      This product includes software developed by the NetBSD
     21  *      Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: slhci_pcmcia.c,v 1.1.2.1 2007/05/22 14:57:34 itohy Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/device.h>
     45 
     46 #include <machine/bus.h>
     47 #include <machine/cpu.h>
     48 
     49 #include <dev/usb/usb.h>
     50 #include <dev/usb/usbdi.h>
     51 #include <dev/usb/usbdivar.h>
     52 #include <dev/usb/usb_mem_nodma.h>
     53 
     54 #include <dev/ic/sl811hsreg.h>
     55 #include <dev/ic/sl811hsvar.h>
     56 
     57 #include <dev/pcmcia/pcmciareg.h>
     58 #include <dev/pcmcia/pcmciavar.h>
     59 #include <dev/pcmcia/pcmciadevs.h>
     60 
     61 struct slhci_pcmcia_softc {
     62 	struct slhci_softc sc_sc;
     63 
     64 	/* PCMCIA-specific goo. */
     65 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
     66 	void *sc_ih;				/* interrupt handler */
     67 
     68 	int sc_state;
     69 #define SLHCI_PCMCIA_ATTACHED	1
     70 };
     71 
     72 static int  slhci_pcmcia_match(struct device *, struct cfdata *, void *);
     73 static int  slhci_pcmcia_validate_config(struct pcmcia_config_entry *);
     74 static void slhci_pcmcia_attach(struct device *, struct device *, void *);
     75 static int  slhci_pcmcia_detach(struct device *, int);
     76 
     77 CFATTACH_DECL(slhci_pcmcia, sizeof(struct slhci_pcmcia_softc),
     78     slhci_pcmcia_match, slhci_pcmcia_attach, slhci_pcmcia_detach, NULL);
     79 
     80 static const struct pcmcia_product slhci_pcmcia_products[] = {
     81 	{ PCMCIA_VENDOR_RATOC, PCMCIA_PRODUCT_RATOC_REX_CFU1,
     82 	  PCMCIA_CIS_RATOC_REX_CFU1 },
     83 };
     84 static const size_t slhci_pcmcia_nproducts =
     85     sizeof(slhci_pcmcia_products) / sizeof(slhci_pcmcia_products[0]);
     86 
     87 static int
     88 slhci_pcmcia_match(struct device *parent, struct cfdata *cf, void *aux)
     89 {
     90 	struct pcmcia_attach_args *pa = aux;
     91 
     92 	if (pcmcia_product_lookup(pa, slhci_pcmcia_products,
     93 	    slhci_pcmcia_nproducts, sizeof(slhci_pcmcia_products[0]), NULL))
     94 		return 1;
     95 	return 0;
     96 }
     97 
     98 static int
     99 slhci_pcmcia_validate_config(struct pcmcia_config_entry *cfe)
    100 {
    101 
    102 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
    103 	    cfe->num_memspace != 0 ||
    104 	    cfe->num_iospace != 1)
    105 		return EINVAL;
    106 	return 0;
    107 }
    108 
    109 static void
    110 slhci_pcmcia_attach(struct device *parent, struct device *self, void *aux)
    111 {
    112 	struct slhci_pcmcia_softc *psc = (struct slhci_pcmcia_softc *)self;
    113 	struct slhci_softc *sc = &psc->sc_sc;
    114 	struct pcmcia_attach_args *pa = aux;
    115 	struct pcmcia_config_entry *cfe;
    116 	struct pcmcia_function *pf = pa->pf;
    117 	bus_space_tag_t iot;
    118 	bus_space_handle_t ioh;
    119 	int error;
    120 
    121 	psc->sc_pf = pf;
    122 
    123 	/* Map I/O space */
    124 	error = pcmcia_function_configure(pf, slhci_pcmcia_validate_config);
    125 	if (error) {
    126 		aprint_error("%s: configure failed, error=%d\n",
    127 		    self->dv_xname, error);
    128 		return;
    129 	}
    130 
    131 	cfe = pf->cfe;
    132 	iot = cfe->iospace[0].handle.iot;
    133 	ioh = cfe->iospace[0].handle.ioh;
    134 
    135 	/* Initialize sc */
    136 	sc->sc_iot = iot;
    137 	sc->sc_ioh = ioh;
    138 #if 0	/* not used */
    139 	sc->sc_enable_power = slhci_pcmcia_enable_power;
    140 	sc->sc_enable_intr  = slhci_pcmcia_enable_intr;
    141 	sc->sc_arg = psc;
    142 #endif
    143 
    144 	/* Establish the interrupt handler */
    145 	if ((psc->sc_ih = pcmcia_intr_establish(pf, IPL_USB, slhci_intr, sc))
    146 	    == NULL) {
    147 		printf("%s: can't establish interrupt\n", self->dv_xname);
    148 		goto fail;
    149 	}
    150 
    151 	/* Enable the function */
    152 	error = pcmcia_function_enable(pf);
    153 	if (error) {
    154 		printf("%s: can't enable function\n", self->dv_xname);
    155 		goto fail1;
    156 	}
    157 
    158 	/* Attach SL811HS/T */
    159 	if (slhci_attach(sc))
    160 		goto fail1;
    161 
    162 	psc->sc_state = SLHCI_PCMCIA_ATTACHED;
    163 	return;
    164 
    165 fail1:
    166 	pcmcia_intr_disestablish(pf, psc->sc_ih);
    167 fail:
    168 	pcmcia_function_unconfigure(pf);
    169 }
    170 
    171 static int
    172 slhci_pcmcia_detach(struct device *self, int flags)
    173 {
    174 	struct slhci_pcmcia_softc *psc = (struct slhci_pcmcia_softc *)self;
    175 	struct pcmcia_function *pf = psc->sc_pf;
    176 	int error;
    177 
    178 	if (psc->sc_state != SLHCI_PCMCIA_ATTACHED)
    179 		return 0;
    180 
    181 	error = slhci_detach(&psc->sc_sc, flags);
    182 	if (error)
    183 		return error;
    184 
    185 	pcmcia_function_disable(pf);
    186 	pcmcia_intr_disestablish(pf, psc->sc_ih);
    187 	pcmcia_function_unconfigure(pf);
    188 
    189 	return 0;
    190 }
    191