Home | History | Annotate | Line # | Download | only in cardbus
cardslot.c revision 1.4
      1 /*	$NetBSD: cardslot.c,v 1.4 1999/10/29 07:27:43 haya Exp $	*/
      2 /*
      3  * Copyright (c) 1999
      4  *       HAYAKAWA Koichi.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by HAYAKAWA Koichi.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     24  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include "opt_cardslot.h"
     35 
     36 #include <sys/types.h>
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/device.h>
     40 #include <sys/malloc.h>
     41 #include <sys/kernel.h>
     42 #include <sys/syslog.h>
     43 #include <sys/kthread.h>
     44 
     45 #include <machine/bus.h>
     46 
     47 #include <dev/cardbus/cardslotvar.h>
     48 #include <dev/cardbus/cardbusvar.h>
     49 #include <dev/pcmcia/pcmciavar.h>
     50 #include <dev/pcmcia/pcmciachip.h>
     51 #include <dev/ic/i82365var.h>
     52 
     53 
     54 #if defined CARDSLOT_DEBUG
     55 #define STATIC
     56 #define DPRINTF(a) printf a
     57 #define DDELAY(x) delay((x)*1000*1000)
     58 #else
     59 #define STATIC static
     60 #define DPRINTF(a)
     61 #endif
     62 
     63 
     64 
     65 STATIC void cardslotattach __P((struct device *, struct device *, void *));
     66 
     67 #if !defined __BROKEN_INDIRECT_CONFIG
     68 STATIC int cardslotmatch __P((struct device *, struct cfdata *, void *));
     69 #else
     70 STATIC int cardslotmatch __P((struct device *, void *, void *));
     71 #endif
     72 static void create_slot_manager __P((void *));
     73 static void cardslot_event_thread __P((void *arg));
     74 
     75 STATIC int cardslot_cb_print __P((void *aux, const char *pcic));
     76 static int cardslot_16_print __P((void *, const char *));
     77 static int cardslot_16_submatch __P((struct device *, struct cfdata *,void *));
     78 
     79 struct cfattach cardslot_ca = {
     80 	sizeof(struct cardslot_softc), cardslotmatch, cardslotattach
     81 };
     82 
     83 #ifndef __NetBSD_Version__
     84 struct cfdriver cardslot_cd = {
     85 	NULL, "cardslot", DV_DULL
     86 };
     87 #endif
     88 
     89 
     90 STATIC int
     91 #if defined __BROKEN_INDIRECT_CONFIG
     92 cardslotmatch(parent, match, aux)
     93      struct device *parent;
     94      void *match;
     95      void *aux;
     96 #else
     97 cardslotmatch(parent, cf, aux)
     98      struct device *parent;
     99      struct cfdata *cf;
    100      void *aux;
    101 #endif
    102 {
    103 #if defined __BROKEN_INDIRECT_CONFIG
    104   struct cfdata *cf = match;
    105 #endif
    106   struct cardslot_attach_args *caa = aux;
    107 
    108   if (caa->caa_cb_attach == NULL && caa->caa_16_attach == NULL) {
    109     /* Neither CardBus nor 16-bit PCMCIA are defined. */
    110     return 0;
    111   }
    112 
    113   return 1;
    114 }
    115 
    116 
    117 
    118 STATIC void
    119 cardslotattach(parent, self, aux)
    120      struct device *parent;
    121      struct device *self;
    122      void *aux;
    123 {
    124   struct cardslot_softc *sc = (struct cardslot_softc *)self;
    125   struct cardslot_attach_args *caa = aux;
    126 
    127   struct cbslot_attach_args *cba = caa->caa_cb_attach;
    128   struct pcmciabus_attach_args *pa = caa->caa_16_attach;
    129 
    130   struct cardbus_softc *csc;
    131   struct pcmcia_softc *psc;
    132 
    133   int card_attach_now;
    134 
    135   sc->sc_slot = sc->sc_dev.dv_unit;
    136   sc->sc_cb_softc = NULL;
    137   sc->sc_16_softc = NULL;
    138   SIMPLEQ_INIT(&sc->sc_events);
    139   sc->sc_th_enable = 0;
    140 
    141   printf(" slot %d flags %x\n", sc->sc_slot, sc->sc_dev.dv_cfdata->cf_flags);
    142 
    143   DPRINTF(("%s attaching CardBus bus...\n", sc->sc_dev.dv_xname));
    144   if (cba != NULL) {
    145     if (NULL != (csc = (void *)config_found(self, cba, cardslot_cb_print))) {
    146       /* cardbus found */
    147       DPRINTF(("cardslotattach: found cardbus on %s\n", sc->sc_dev.dv_xname));
    148       sc->sc_cb_softc = csc;
    149     }
    150   }
    151 
    152   if (pa != NULL) {
    153     if (NULL != (psc = (void *)config_found_sm(self, pa, cardslot_16_print,
    154 				       cardslot_16_submatch))) {
    155       /* pcmcia 16-bit bus found */
    156       DPRINTF(("cardslotattach: found 16-bit pcmcia bus\n"));
    157       sc->sc_16_softc = psc;
    158       /* XXX: dirty.  This code should be removed to achieve MI */
    159       caa->caa_ph->pcmcia = (struct device *)psc;
    160     }
    161   }
    162 
    163   if (csc != NULL || psc != NULL) {
    164 #if __NetBSD_Version__ > 104060000
    165     kthread_create(create_slot_manager, (void *)sc);
    166 #else
    167     kthread_create_deferred(create_slot_manager, (void *)sc);
    168 #endif
    169   }
    170 
    171   card_attach_now = sc->sc_dev.dv_cfdata->cf_flags & 0x01;
    172 
    173   if (csc && (csc->sc_cf->cardbus_ctrl)(csc->sc_cc, CARDBUS_CD)) {
    174     DPRINTF(("cardslotattach: CardBus card found\n"));
    175     if (card_attach_now) {
    176       /* attach now */
    177       cardbus_attach_card(sc->sc_cb_softc);
    178       CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_CB);
    179     } else {
    180       /* attach deffered */
    181       cardslot_event_throw(sc, CARDSLOT_EVENT_INSERTION_CB);
    182     }
    183   }
    184 
    185   if (psc && (psc->pct->card_detect)(psc->pch)) {
    186     DPRINTF(("cardbusattach: 16-bit card found\n"));
    187     if (card_attach_now) {
    188       /* attach now */
    189       pcmcia_card_attach((struct device *)sc->sc_16_softc);
    190       CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_16);
    191     } else {
    192       /* attach deffered */
    193       cardslot_event_throw(sc, CARDSLOT_EVENT_INSERTION_16);
    194     }
    195   }
    196 }
    197 
    198 
    199 
    200 STATIC int
    201 cardslot_cb_print(aux, pnp)
    202      void *aux;
    203      const char *pnp;
    204 {
    205   struct cbslot_attach_args *cba = aux;
    206 
    207   if (pnp) {
    208     printf("cardbus at %s", pnp);
    209     printf(" function %d subordinate bus %d", cba->cba_function, cba->cba_bus);
    210   }
    211 
    212   return UNCONF;
    213 }
    214 
    215 
    216 static int
    217 cardslot_16_submatch(parent, cf, aux)
    218      struct device *parent;
    219      struct cfdata *cf;
    220      void *aux;
    221 {
    222   if (cf->cf_loc[PCMCIABUSCF_CONTROLLER] != PCMCIABUSCF_CONTROLLER_DEFAULT
    223       && cf->cf_loc[PCMCIABUSCF_CONTROLLER] != 0) {
    224     return 0;
    225   }
    226 
    227   if ((cf->cf_loc[PCMCIABUSCF_CONTROLLER] == PCMCIABUSCF_CONTROLLER_DEFAULT)) {
    228     return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    229   }
    230 
    231   return 0;
    232 }
    233 
    234 
    235 
    236 static int
    237 cardslot_16_print(arg, pnp)
    238      void *arg;
    239      const char *pnp;
    240 {
    241 
    242   if (pnp) {
    243     printf("pcmciabus at %s", pnp);
    244   }
    245 
    246   return UNCONF;
    247 }
    248 
    249 
    250 
    251 
    252 static void
    253 create_slot_manager(arg)
    254      void *arg;
    255 {
    256   struct cardslot_softc *sc = (struct cardslot_softc *)arg;
    257 
    258   sc->sc_th_enable = 1;
    259 
    260 #if __NetBSD_Version__ > 104060000
    261  if (kthread_create1(cardslot_event_thread, sc, &sc->sc_event_thread, "%s",
    262 		     sc->sc_dev.dv_xname)) {
    263 #else
    264  if (kthread_create(cardslot_event_thread, sc, &sc->sc_event_thread, "%s",
    265 		     sc->sc_dev.dv_xname)) {
    266 #endif
    267     printf("%s: unable to create event thread for slot %d\n",
    268 	   sc->sc_dev.dv_xname, sc->sc_slot);
    269     panic("create_slot_manager");
    270   }
    271 }
    272 
    273 
    274 
    275 
    276 /*
    277  * void cardslot_event_throw(struct cardslot_softc *sc, int ev)
    278  *
    279  *   This function throws an event to the event handler.  If the state
    280  *   of a slot is changed, it should be noticed using this function.
    281  */
    282 void
    283 cardslot_event_throw(sc, ev)
    284      struct cardslot_softc *sc;
    285      int ev;
    286 {
    287   struct cardslot_event *ce;
    288 
    289   DPRINTF(("cardslot_event_throw: an event %s comes\n",
    290 	   ev == CARDSLOT_EVENT_INSERTION_CB ? "CardBus Card inserted" :
    291 	   ev == CARDSLOT_EVENT_INSERTION_16 ? "16-bit Card inserted" :
    292 	   ev == CARDSLOT_EVENT_REMOVAL_CB ? "CardBus Card removed" :
    293 	   ev == CARDSLOT_EVENT_REMOVAL_16 ? "16-bit Card removed" : "???"));
    294 
    295   if (NULL == (ce = (struct cardslot_event *)malloc(sizeof (struct cardslot_event), M_TEMP, M_NOWAIT))) {
    296     panic("cardslot_enevt");
    297   }
    298 
    299   ce->ce_type = ev;
    300 
    301   {
    302     int s = spltty();
    303     SIMPLEQ_INSERT_TAIL(&sc->sc_events, ce, ce_q);
    304     splx(s);
    305   }
    306 
    307   wakeup(&sc->sc_events);
    308 
    309   return;
    310 }
    311 
    312 
    313 /*
    314  * static void cardslot_event_thread(void *arg)
    315  *
    316  *   This function is the main routine handing cardslot events such as
    317  *   insertions and removals.
    318  *
    319  */
    320 static void
    321 cardslot_event_thread(arg)
    322      void *arg;
    323 {
    324   struct cardslot_softc *sc = arg;
    325   struct cardslot_event *ce;
    326   int s;
    327   static int antonym_ev[4] = {
    328     CARDSLOT_EVENT_REMOVAL_16, CARDSLOT_EVENT_INSERTION_16,
    329     CARDSLOT_EVENT_REMOVAL_CB, CARDSLOT_EVENT_INSERTION_CB
    330   };
    331 
    332   while (sc->sc_th_enable) {
    333     s = spltty();
    334     if ((ce = SIMPLEQ_FIRST(&sc->sc_events)) == NULL) {
    335       splx(s);
    336       (void) tsleep(&sc->sc_events, PWAIT, "cardslotev", 0);
    337       continue;
    338     }
    339     SIMPLEQ_REMOVE_HEAD(&sc->sc_events, ce, ce_q);
    340     splx(s);
    341 
    342     if (IS_CARDSLOT_INSERT_REMOVE_EV(ce->ce_type)) {
    343       /* Chattering supression */
    344       s = spltty();
    345       while (1) {
    346 	struct cardslot_event *ce1, *ce2;
    347 
    348 	if ((ce1 = SIMPLEQ_FIRST(&sc->sc_events)) == NULL) {
    349 	  break;
    350 	}
    351 	if (ce1->ce_type != antonym_ev[ce->ce_type]) {
    352 	  break;
    353 	}
    354 	if ((ce2 = SIMPLEQ_NEXT(ce1, ce_q)) == NULL) {
    355 	  break;
    356 	}
    357 	if (ce2->ce_type == ce->ce_type) {
    358 	  SIMPLEQ_REMOVE_HEAD(&sc->sc_events, ce1, ce_q);
    359 	  free(ce1, M_TEMP);
    360 	  SIMPLEQ_REMOVE_HEAD(&sc->sc_events, ce2, ce_q);
    361 	  free(ce2, M_TEMP);
    362 	}
    363       }
    364       splx(s);
    365     }
    366 
    367     switch (ce->ce_type) {
    368     case CARDSLOT_EVENT_INSERTION_CB:
    369       if (CARDSLOT_CARDTYPE(sc->sc_status) != CARDSLOT_STATUS_CARD_NONE) {
    370 	/* A card has already been inserted. */
    371 	break;
    372       }
    373       if (sc->sc_cb_softc) {
    374 	cardbus_attach_card(sc->sc_cb_softc);
    375 	CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_CB);
    376       } else {
    377 	panic("no cardbus on %s", sc->sc_dev.dv_xname);
    378       }
    379 
    380       break;
    381 
    382     case CARDSLOT_EVENT_INSERTION_16:
    383       if (CARDSLOT_CARDTYPE(sc->sc_status) != CARDSLOT_STATUS_CARD_NONE) {
    384 	/* A card has already been inserted. */
    385 	break;
    386       }
    387       if (sc->sc_16_softc) {
    388 	pcmcia_card_attach((struct device *)sc->sc_16_softc);
    389 	CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_16);
    390       } else {
    391 	panic("no 16-bit pcmcia on %s", sc->sc_dev.dv_xname);
    392       }
    393 
    394       break;
    395 
    396     case CARDSLOT_EVENT_REMOVAL_CB:
    397       if (CARDSLOT_CARDTYPE(sc->sc_status) != CARDSLOT_STATUS_CARD_CB) {
    398 	/* CardBus card has not been inserted. */
    399 	break;
    400       }
    401 				/* not yet */
    402       CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_NONE);
    403       break;
    404 
    405     case CARDSLOT_EVENT_REMOVAL_16:
    406       DPRINTF(("%s: removal event\n", sc->sc_dev.dv_xname));
    407       if (CARDSLOT_CARDTYPE(sc->sc_status) != CARDSLOT_STATUS_CARD_16) {
    408 	/* 16-bit card has not been inserted. */
    409 	break;
    410       }
    411       if (sc->sc_16_softc) {
    412 	struct pcmcia_softc *psc = sc->sc_16_softc;
    413 
    414 	pcmcia_card_deactivate((struct device *)psc);
    415 	pcmcia_chip_socket_disable(psc->pct, psc->pch);
    416 	pcmcia_card_detach((struct device *)psc, DETACH_FORCE);
    417 	CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_NONE);
    418       }
    419       break;
    420 
    421     default:
    422       panic("cardslot_event_thread: unknown event %d", ce->ce_type);
    423     }
    424     free(ce, M_TEMP);
    425   }
    426 
    427   sc->sc_event_thread = NULL;
    428 
    429   /* In case parent is waiting for us to exit. */
    430   wakeup(sc);
    431 
    432   kthread_exit(0);
    433 }
    434