Home | History | Annotate | Line # | Download | only in cardbus
cardslot.c revision 1.2.2.3
      1 /*	$NetBSD: cardslot.c,v 1.2.2.3 2001/03/12 13:30:02 bouyer 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 STATIC int cardslotmatch __P((struct device *, struct cfdata *, void *));
     68 static void create_slot_manager __P((void *));
     69 static void cardslot_event_thread __P((void *arg));
     70 
     71 STATIC int cardslot_cb_print __P((void *aux, const char *pcic));
     72 static int cardslot_16_print __P((void *, const char *));
     73 static int cardslot_16_submatch __P((struct device *, struct cfdata *,void *));
     74 
     75 struct cfattach cardslot_ca = {
     76 	sizeof(struct cardslot_softc), cardslotmatch, cardslotattach
     77 };
     78 
     79 #ifndef __NetBSD_Version__
     80 struct cfdriver cardslot_cd = {
     81 	NULL, "cardslot", DV_DULL
     82 };
     83 #endif
     84 
     85 
     86 STATIC int
     87 cardslotmatch(parent, cf, aux)
     88      struct device *parent;
     89      struct cfdata *cf;
     90      void *aux;
     91 {
     92   struct cardslot_attach_args *caa = aux;
     93 
     94   if (caa->caa_cb_attach == NULL && caa->caa_16_attach == NULL) {
     95     /* Neither CardBus nor 16-bit PCMCIA are defined. */
     96     return 0;
     97   }
     98 
     99   return 1;
    100 }
    101 
    102 
    103 
    104 STATIC void
    105 cardslotattach(parent, self, aux)
    106      struct device *parent;
    107      struct device *self;
    108      void *aux;
    109 {
    110   struct cardslot_softc *sc = (struct cardslot_softc *)self;
    111   struct cardslot_attach_args *caa = aux;
    112 
    113   struct cbslot_attach_args *cba = caa->caa_cb_attach;
    114   struct pcmciabus_attach_args *pa = caa->caa_16_attach;
    115 
    116   struct cardbus_softc *csc;
    117   struct pcmcia_softc *psc;
    118 
    119   sc->sc_slot = sc->sc_dev.dv_unit;
    120   sc->sc_cb_softc = NULL;
    121   sc->sc_16_softc = NULL;
    122   SIMPLEQ_INIT(&sc->sc_events);
    123   sc->sc_th_enable = 0;
    124 
    125   printf(" slot %d flags %x\n", sc->sc_slot, sc->sc_dev.dv_cfdata->cf_flags);
    126 
    127   DPRINTF(("%s attaching CardBus bus...\n", sc->sc_dev.dv_xname));
    128   if (cba != NULL) {
    129     if (NULL != (csc = (void *)config_found(self, cba, cardslot_cb_print))) {
    130       /* cardbus found */
    131       DPRINTF(("cardslotattach: found cardbus on %s\n", sc->sc_dev.dv_xname));
    132       sc->sc_cb_softc = csc;
    133     }
    134   }
    135 
    136   if (pa != NULL) {
    137     if (NULL != (psc = (void *)config_found_sm(self, pa, cardslot_16_print,
    138 				       cardslot_16_submatch))) {
    139       /* pcmcia 16-bit bus found */
    140       DPRINTF(("cardslotattach: found 16-bit pcmcia bus\n"));
    141       sc->sc_16_softc = psc;
    142       /* XXX: dirty.  This code should be removed to achieve MI */
    143       caa->caa_ph->pcmcia = (struct device *)psc;
    144     }
    145   }
    146 
    147   if (csc != NULL || psc != NULL) {
    148 #if __NetBSD_Version__ > 104060000
    149     config_pending_incr();
    150     kthread_create(create_slot_manager, (void *)sc);
    151 #else
    152     kthread_create_deferred(create_slot_manager, (void *)sc);
    153 #endif
    154   }
    155 
    156   if (csc && (csc->sc_cf->cardbus_ctrl)(csc->sc_cc, CARDBUS_CD)) {
    157     DPRINTF(("cardslotattach: CardBus card found\n"));
    158     /* attach deferred */
    159     cardslot_event_throw(sc, CARDSLOT_EVENT_INSERTION_CB);
    160   }
    161 
    162   if (psc && (psc->pct->card_detect)(psc->pch)) {
    163     DPRINTF(("cardbusattach: 16-bit card found\n"));
    164     /* attach deferred */
    165     cardslot_event_throw(sc, CARDSLOT_EVENT_INSERTION_16);
    166   }
    167 }
    168 
    169 
    170 
    171 STATIC int
    172 cardslot_cb_print(aux, pnp)
    173 	void *aux;
    174 	const char *pnp;
    175 {
    176 	struct cbslot_attach_args *cba = aux;
    177 
    178 	if (pnp) {
    179 		printf("cardbus at %s subordinate bus %d", pnp, cba->cba_bus);
    180 	}
    181 
    182 	return UNCONF;
    183 }
    184 
    185 
    186 static int
    187 cardslot_16_submatch(parent, cf, aux)
    188      struct device *parent;
    189      struct cfdata *cf;
    190      void *aux;
    191 {
    192   if (cf->cf_loc[PCMCIABUSCF_CONTROLLER] != PCMCIABUSCF_CONTROLLER_DEFAULT
    193       && cf->cf_loc[PCMCIABUSCF_CONTROLLER] != 0) {
    194     return 0;
    195   }
    196 
    197   if ((cf->cf_loc[PCMCIABUSCF_CONTROLLER] == PCMCIABUSCF_CONTROLLER_DEFAULT)) {
    198     return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    199   }
    200 
    201   return 0;
    202 }
    203 
    204 
    205 
    206 static int
    207 cardslot_16_print(arg, pnp)
    208      void *arg;
    209      const char *pnp;
    210 {
    211 
    212   if (pnp) {
    213     printf("pcmciabus at %s", pnp);
    214   }
    215 
    216   return UNCONF;
    217 }
    218 
    219 
    220 
    221 
    222 static void
    223 create_slot_manager(arg)
    224      void *arg;
    225 {
    226   struct cardslot_softc *sc = (struct cardslot_softc *)arg;
    227 
    228   sc->sc_th_enable = 1;
    229 
    230 #if __NetBSD_Version__ > 104060000
    231  if (kthread_create1(cardslot_event_thread, sc, &sc->sc_event_thread, "%s",
    232 		     sc->sc_dev.dv_xname)) {
    233 #else
    234  if (kthread_create(cardslot_event_thread, sc, &sc->sc_event_thread, "%s",
    235 		     sc->sc_dev.dv_xname)) {
    236 #endif
    237     printf("%s: unable to create event thread for slot %d\n",
    238 	   sc->sc_dev.dv_xname, sc->sc_slot);
    239     panic("create_slot_manager");
    240   }
    241 }
    242 
    243 
    244 
    245 
    246 /*
    247  * void cardslot_event_throw(struct cardslot_softc *sc, int ev)
    248  *
    249  *   This function throws an event to the event handler.  If the state
    250  *   of a slot is changed, it should be noticed using this function.
    251  */
    252 void
    253 cardslot_event_throw(sc, ev)
    254      struct cardslot_softc *sc;
    255      int ev;
    256 {
    257   struct cardslot_event *ce;
    258 
    259   DPRINTF(("cardslot_event_throw: an event %s comes\n",
    260 	   ev == CARDSLOT_EVENT_INSERTION_CB ? "CardBus Card inserted" :
    261 	   ev == CARDSLOT_EVENT_INSERTION_16 ? "16-bit Card inserted" :
    262 	   ev == CARDSLOT_EVENT_REMOVAL_CB ? "CardBus Card removed" :
    263 	   ev == CARDSLOT_EVENT_REMOVAL_16 ? "16-bit Card removed" : "???"));
    264 
    265   if (NULL == (ce = (struct cardslot_event *)malloc(sizeof (struct cardslot_event), M_TEMP, M_NOWAIT))) {
    266     panic("cardslot_enevt");
    267   }
    268 
    269   ce->ce_type = ev;
    270 
    271   {
    272     int s = spltty();
    273     SIMPLEQ_INSERT_TAIL(&sc->sc_events, ce, ce_q);
    274     splx(s);
    275   }
    276 
    277   wakeup(&sc->sc_events);
    278 
    279   return;
    280 }
    281 
    282 
    283 /*
    284  * static void cardslot_event_thread(void *arg)
    285  *
    286  *   This function is the main routine handing cardslot events such as
    287  *   insertions and removals.
    288  *
    289  */
    290 static void
    291 cardslot_event_thread(arg)
    292      void *arg;
    293 {
    294   struct cardslot_softc *sc = arg;
    295   struct cardslot_event *ce;
    296   int s, first = 1;
    297   static int antonym_ev[4] = {
    298     CARDSLOT_EVENT_REMOVAL_16, CARDSLOT_EVENT_INSERTION_16,
    299     CARDSLOT_EVENT_REMOVAL_CB, CARDSLOT_EVENT_INSERTION_CB
    300   };
    301 
    302   while (sc->sc_th_enable) {
    303     s = spltty();
    304     if ((ce = SIMPLEQ_FIRST(&sc->sc_events)) == NULL) {
    305       splx(s);
    306       if (first) {
    307         first = 0;
    308         config_pending_decr();
    309       }
    310       (void) tsleep(&sc->sc_events, PWAIT, "cardslotev", 0);
    311       continue;
    312     }
    313     SIMPLEQ_REMOVE_HEAD(&sc->sc_events, ce, ce_q);
    314     splx(s);
    315 
    316     if (IS_CARDSLOT_INSERT_REMOVE_EV(ce->ce_type)) {
    317       /* Chattering supression */
    318       s = spltty();
    319       while (1) {
    320 	struct cardslot_event *ce1, *ce2;
    321 
    322 	if ((ce1 = SIMPLEQ_FIRST(&sc->sc_events)) == NULL) {
    323 	  break;
    324 	}
    325 	if (ce1->ce_type != antonym_ev[ce->ce_type]) {
    326 	  break;
    327 	}
    328 	if ((ce2 = SIMPLEQ_NEXT(ce1, ce_q)) == NULL) {
    329 	  break;
    330 	}
    331 	if (ce2->ce_type == ce->ce_type) {
    332 	  SIMPLEQ_REMOVE_HEAD(&sc->sc_events, ce1, ce_q);
    333 	  free(ce1, M_TEMP);
    334 	  SIMPLEQ_REMOVE_HEAD(&sc->sc_events, ce2, ce_q);
    335 	  free(ce2, M_TEMP);
    336 	}
    337       }
    338       splx(s);
    339     }
    340 
    341     switch (ce->ce_type) {
    342     case CARDSLOT_EVENT_INSERTION_CB:
    343       if ((CARDSLOT_CARDTYPE(sc->sc_status) == CARDSLOT_STATUS_CARD_CB)
    344 	  || (CARDSLOT_CARDTYPE(sc->sc_status) == CARDSLOT_STATUS_CARD_16)) {
    345 	if (CARDSLOT_WORK(sc->sc_status) == CARDSLOT_STATUS_WORKING) {
    346 	  /* A card has already been inserted and works. */
    347 	  break;
    348 	}
    349       }
    350 
    351       if (sc->sc_cb_softc) {
    352 	CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_CB);
    353 	if (cardbus_attach_card(sc->sc_cb_softc) > 0) {
    354 	  /* at least one function works */
    355 	  CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_WORKING);
    356 	} else {
    357 	  /* no functions work or this card is not known */
    358 	  CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_NOTWORK);
    359 	}
    360       } else {
    361 	panic("no cardbus on %s", sc->sc_dev.dv_xname);
    362       }
    363 
    364       break;
    365 
    366     case CARDSLOT_EVENT_INSERTION_16:
    367       if ((CARDSLOT_CARDTYPE(sc->sc_status) == CARDSLOT_STATUS_CARD_CB)
    368 	  || (CARDSLOT_CARDTYPE(sc->sc_status) == CARDSLOT_STATUS_CARD_16)) {
    369 	if (CARDSLOT_WORK(sc->sc_status) == CARDSLOT_STATUS_WORKING) {
    370 	  /* A card has already been inserted and work. */
    371 	  break;
    372 	}
    373       }
    374       if (sc->sc_16_softc) {
    375 	CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_16);
    376 	if (pcmcia_card_attach((struct device *)sc->sc_16_softc)) {
    377 	  /* Do not attach */
    378 	  CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_NOTWORK);
    379 	} else {
    380 	  /* working */
    381 	  CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_WORKING);
    382 	}
    383       } else {
    384 	panic("no 16-bit pcmcia on %s", sc->sc_dev.dv_xname);
    385       }
    386 
    387       break;
    388 
    389     case CARDSLOT_EVENT_REMOVAL_CB:
    390       if (CARDSLOT_CARDTYPE(sc->sc_status) == CARDSLOT_STATUS_CARD_CB) {
    391 	/* CardBus card has not been inserted. */
    392 	if (CARDSLOT_WORK(sc->sc_status) == CARDSLOT_STATUS_WORKING) {
    393 	  cardbus_detach_card(sc->sc_cb_softc);
    394 	  CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_NOTWORK);
    395 	  CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_CARD_NONE);
    396 	}
    397 	CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_NONE);
    398       } else if (CARDSLOT_CARDTYPE(sc->sc_status) != CARDSLOT_STATUS_CARD_16) {
    399 	/* Unknown card... */
    400 	CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_NONE);
    401       }
    402       CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_NOTWORK);
    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 != NULL)
    412 	  && (CARDSLOT_WORK(sc->sc_status) == CARDSLOT_STATUS_WORKING)) {
    413 	struct pcmcia_softc *psc = sc->sc_16_softc;
    414 
    415 	pcmcia_card_deactivate((struct device *)psc);
    416 	pcmcia_chip_socket_disable(psc->pct, psc->pch);
    417 	pcmcia_card_detach((struct device *)psc, DETACH_FORCE);
    418       }
    419       CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_NONE);
    420       CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_NOTWORK);
    421       break;
    422 
    423     default:
    424       panic("cardslot_event_thread: unknown event %d", ce->ce_type);
    425     }
    426     free(ce, M_TEMP);
    427   }
    428 
    429   sc->sc_event_thread = NULL;
    430 
    431   /* In case the parent device is waiting for us to exit. */
    432   wakeup(sc);
    433 
    434   kthread_exit(0);
    435 }
    436