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