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