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