Home | History | Annotate | Line # | Download | only in cardbus
cardslot.c revision 1.5
      1 /*	$NetBSD: cardslot.c,v 1.5 1999/11/15 06:08:02 haya 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     kthread_create(create_slot_manager, (void *)sc);
    167 #else
    168     kthread_create_deferred(create_slot_manager, (void *)sc);
    169 #endif
    170   }
    171 
    172   card_attach_now = sc->sc_dev.dv_cfdata->cf_flags & 0x01;
    173 
    174   if (csc && (csc->sc_cf->cardbus_ctrl)(csc->sc_cc, CARDBUS_CD)) {
    175     DPRINTF(("cardslotattach: CardBus card found\n"));
    176     if (card_attach_now) {
    177       if (cardbus_attach_card(sc->sc_cb_softc) > 0) {
    178 	/* at least one function works */
    179 	CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_WORKING);
    180       } else {
    181 	/* no functions work or this card is not known */
    182 	CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_NOTWORK);
    183       }
    184       CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_CB);
    185     } else {
    186       /* attach deffered */
    187       cardslot_event_throw(sc, CARDSLOT_EVENT_INSERTION_CB);
    188     }
    189   }
    190 
    191   if (psc && (psc->pct->card_detect)(psc->pch)) {
    192     DPRINTF(("cardbusattach: 16-bit card found\n"));
    193     if (card_attach_now) {
    194       /* attach now */
    195       pcmcia_card_attach((struct device *)sc->sc_16_softc);
    196       CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_16);
    197     } else {
    198       /* attach deffered */
    199       cardslot_event_throw(sc, CARDSLOT_EVENT_INSERTION_16);
    200     }
    201   }
    202 }
    203 
    204 
    205 
    206 STATIC int
    207 cardslot_cb_print(aux, pnp)
    208      void *aux;
    209      const char *pnp;
    210 {
    211   struct cbslot_attach_args *cba = aux;
    212 
    213   if (pnp) {
    214     printf("cardbus at %s", pnp);
    215     printf(" function %d subordinate bus %d", cba->cba_function, cba->cba_bus);
    216   }
    217 
    218   return UNCONF;
    219 }
    220 
    221 
    222 static int
    223 cardslot_16_submatch(parent, cf, aux)
    224      struct device *parent;
    225      struct cfdata *cf;
    226      void *aux;
    227 {
    228   if (cf->cf_loc[PCMCIABUSCF_CONTROLLER] != PCMCIABUSCF_CONTROLLER_DEFAULT
    229       && cf->cf_loc[PCMCIABUSCF_CONTROLLER] != 0) {
    230     return 0;
    231   }
    232 
    233   if ((cf->cf_loc[PCMCIABUSCF_CONTROLLER] == PCMCIABUSCF_CONTROLLER_DEFAULT)) {
    234     return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    235   }
    236 
    237   return 0;
    238 }
    239 
    240 
    241 
    242 static int
    243 cardslot_16_print(arg, pnp)
    244      void *arg;
    245      const char *pnp;
    246 {
    247 
    248   if (pnp) {
    249     printf("pcmciabus at %s", pnp);
    250   }
    251 
    252   return UNCONF;
    253 }
    254 
    255 
    256 
    257 
    258 static void
    259 create_slot_manager(arg)
    260      void *arg;
    261 {
    262   struct cardslot_softc *sc = (struct cardslot_softc *)arg;
    263 
    264   sc->sc_th_enable = 1;
    265 
    266 #if __NetBSD_Version__ > 104060000
    267  if (kthread_create1(cardslot_event_thread, sc, &sc->sc_event_thread, "%s",
    268 		     sc->sc_dev.dv_xname)) {
    269 #else
    270  if (kthread_create(cardslot_event_thread, sc, &sc->sc_event_thread, "%s",
    271 		     sc->sc_dev.dv_xname)) {
    272 #endif
    273     printf("%s: unable to create event thread for slot %d\n",
    274 	   sc->sc_dev.dv_xname, sc->sc_slot);
    275     panic("create_slot_manager");
    276   }
    277 }
    278 
    279 
    280 
    281 
    282 /*
    283  * void cardslot_event_throw(struct cardslot_softc *sc, int ev)
    284  *
    285  *   This function throws an event to the event handler.  If the state
    286  *   of a slot is changed, it should be noticed using this function.
    287  */
    288 void
    289 cardslot_event_throw(sc, ev)
    290      struct cardslot_softc *sc;
    291      int ev;
    292 {
    293   struct cardslot_event *ce;
    294 
    295   DPRINTF(("cardslot_event_throw: an event %s comes\n",
    296 	   ev == CARDSLOT_EVENT_INSERTION_CB ? "CardBus Card inserted" :
    297 	   ev == CARDSLOT_EVENT_INSERTION_16 ? "16-bit Card inserted" :
    298 	   ev == CARDSLOT_EVENT_REMOVAL_CB ? "CardBus Card removed" :
    299 	   ev == CARDSLOT_EVENT_REMOVAL_16 ? "16-bit Card removed" : "???"));
    300 
    301   if (NULL == (ce = (struct cardslot_event *)malloc(sizeof (struct cardslot_event), M_TEMP, M_NOWAIT))) {
    302     panic("cardslot_enevt");
    303   }
    304 
    305   ce->ce_type = ev;
    306 
    307   {
    308     int s = spltty();
    309     SIMPLEQ_INSERT_TAIL(&sc->sc_events, ce, ce_q);
    310     splx(s);
    311   }
    312 
    313   wakeup(&sc->sc_events);
    314 
    315   return;
    316 }
    317 
    318 
    319 /*
    320  * static void cardslot_event_thread(void *arg)
    321  *
    322  *   This function is the main routine handing cardslot events such as
    323  *   insertions and removals.
    324  *
    325  */
    326 static void
    327 cardslot_event_thread(arg)
    328      void *arg;
    329 {
    330   struct cardslot_softc *sc = arg;
    331   struct cardslot_event *ce;
    332   int s;
    333   static int antonym_ev[4] = {
    334     CARDSLOT_EVENT_REMOVAL_16, CARDSLOT_EVENT_INSERTION_16,
    335     CARDSLOT_EVENT_REMOVAL_CB, CARDSLOT_EVENT_INSERTION_CB
    336   };
    337 
    338   while (sc->sc_th_enable) {
    339     s = spltty();
    340     if ((ce = SIMPLEQ_FIRST(&sc->sc_events)) == NULL) {
    341       splx(s);
    342       (void) tsleep(&sc->sc_events, PWAIT, "cardslotev", 0);
    343       continue;
    344     }
    345     SIMPLEQ_REMOVE_HEAD(&sc->sc_events, ce, ce_q);
    346     splx(s);
    347 
    348     if (IS_CARDSLOT_INSERT_REMOVE_EV(ce->ce_type)) {
    349       /* Chattering supression */
    350       s = spltty();
    351       while (1) {
    352 	struct cardslot_event *ce1, *ce2;
    353 
    354 	if ((ce1 = SIMPLEQ_FIRST(&sc->sc_events)) == NULL) {
    355 	  break;
    356 	}
    357 	if (ce1->ce_type != antonym_ev[ce->ce_type]) {
    358 	  break;
    359 	}
    360 	if ((ce2 = SIMPLEQ_NEXT(ce1, ce_q)) == NULL) {
    361 	  break;
    362 	}
    363 	if (ce2->ce_type == ce->ce_type) {
    364 	  SIMPLEQ_REMOVE_HEAD(&sc->sc_events, ce1, ce_q);
    365 	  free(ce1, M_TEMP);
    366 	  SIMPLEQ_REMOVE_HEAD(&sc->sc_events, ce2, ce_q);
    367 	  free(ce2, M_TEMP);
    368 	}
    369       }
    370       splx(s);
    371     }
    372 
    373     switch (ce->ce_type) {
    374     case CARDSLOT_EVENT_INSERTION_CB:
    375       if ((CARDSLOT_CARDTYPE(sc->sc_status) == CARDSLOT_STATUS_CARD_CB)
    376 	  || (CARDSLOT_CARDTYPE(sc->sc_status) == CARDSLOT_STATUS_CARD_16)) {
    377 	if (CARDSLOT_WORK(sc->sc_status) == CARDSLOT_STATUS_WORKING) {
    378 	  /* A card has already been inserted and work. */
    379 	  break;
    380 	}
    381       }
    382 
    383       if (sc->sc_cb_softc) {
    384 	CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_CB);
    385 	if (cardbus_attach_card(sc->sc_cb_softc) > 0) {
    386 	  /* at least one function works */
    387 	  CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_WORKING);
    388 	} else {
    389 	  /* no functions work or this card is not known */
    390 	  CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_NOTWORK);
    391 	}
    392       } else {
    393 	panic("no cardbus on %s", sc->sc_dev.dv_xname);
    394       }
    395 
    396       break;
    397 
    398     case CARDSLOT_EVENT_INSERTION_16:
    399       if ((CARDSLOT_CARDTYPE(sc->sc_status) == CARDSLOT_STATUS_CARD_CB)
    400 	  || (CARDSLOT_CARDTYPE(sc->sc_status) == CARDSLOT_STATUS_CARD_16)) {
    401 	if (CARDSLOT_WORK(sc->sc_status) == CARDSLOT_STATUS_WORKING) {
    402 	  /* A card has already been inserted and work. */
    403 	  break;
    404 	}
    405       }
    406       if (sc->sc_16_softc) {
    407 	CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_16);
    408 	if (pcmcia_card_attach((struct device *)sc->sc_16_softc)) {
    409 	  /* Do not attach */
    410 	  CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_NOTWORK);
    411 	} else {
    412 	  /* working */
    413 	  CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_WORKING);
    414 	}
    415       } else {
    416 	panic("no 16-bit pcmcia on %s", sc->sc_dev.dv_xname);
    417       }
    418 
    419       break;
    420 
    421     case CARDSLOT_EVENT_REMOVAL_CB:
    422       if (CARDSLOT_CARDTYPE(sc->sc_status) == CARDSLOT_STATUS_CARD_CB) {
    423 	/* CardBus card has not been inserted. */
    424 #if notyet
    425 	if (CARDSLOT_WORK(sc->sc_status) == CARDSLOT_STATUS_WORKING) {
    426 	  cardbus_detach_card(sc->sc_cb_softc);
    427 	}
    428 #endif
    429 	CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_NONE);
    430       } else if (CARDSLOT_CARDTYPE(sc->sc_status) != CARDSLOT_STATUS_CARD_16) {
    431 	/* Unknown card... */
    432 	CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_NONE);
    433       }
    434       CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_NOTWORK);
    435       break;
    436 
    437     case CARDSLOT_EVENT_REMOVAL_16:
    438       DPRINTF(("%s: removal event\n", sc->sc_dev.dv_xname));
    439       if (CARDSLOT_CARDTYPE(sc->sc_status) != CARDSLOT_STATUS_CARD_16) {
    440 	/* 16-bit card has not been inserted. */
    441 	break;
    442       }
    443       if ((sc->sc_16_softc != NULL)
    444 	  && (CARDSLOT_WORK(sc->sc_status) == CARDSLOT_STATUS_WORKING)) {
    445 	struct pcmcia_softc *psc = sc->sc_16_softc;
    446 
    447 	pcmcia_card_deactivate((struct device *)psc);
    448 	pcmcia_chip_socket_disable(psc->pct, psc->pch);
    449 	pcmcia_card_detach((struct device *)psc, DETACH_FORCE);
    450       }
    451       CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_NONE);
    452       CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_NOTWORK);
    453       break;
    454 
    455     default:
    456       panic("cardslot_event_thread: unknown event %d", ce->ce_type);
    457     }
    458     free(ce, M_TEMP);
    459   }
    460 
    461   sc->sc_event_thread = NULL;
    462 
    463   /* In case parent is waiting for us to exit. */
    464   wakeup(sc);
    465 
    466   kthread_exit(0);
    467 }
    468