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