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