Home | History | Annotate | Line # | Download | only in cardbus
cardslot.c revision 1.57.4.1
      1 /*	$NetBSD: cardslot.c,v 1.57.4.1 2021/03/22 02:00:59 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  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     25  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: cardslot.c,v 1.57.4.1 2021/03/22 02:00:59 thorpej Exp $");
     31 
     32 #include "opt_cardslot.h"
     33 
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 #include <sys/device.h>
     37 #include <sys/malloc.h>
     38 #include <sys/kernel.h>
     39 #include <sys/syslog.h>
     40 #include <sys/kthread.h>
     41 
     42 #include <sys/bus.h>
     43 
     44 #include <dev/cardbus/cardslotvar.h>
     45 #include <dev/cardbus/cardbusvar.h>
     46 #include <dev/pcmcia/pcmciavar.h>
     47 #include <dev/pcmcia/pcmciachip.h>
     48 
     49 #include "locators.h"
     50 
     51 #if defined CARDSLOT_DEBUG
     52 #define STATIC
     53 #define DPRINTF(a) printf a
     54 #else
     55 #define STATIC static
     56 #define DPRINTF(a)
     57 #endif
     58 
     59 int pcmcia_error(device_t);
     60 int
     61 pcmcia_error(device_t dev)
     62 {
     63 
     64 	return 1;
     65 }
     66 __weak_alias(pcmcia_card_attach, pcmcia_error);
     67 __weak_alias(pcmcia_card_deactivate, pcmcia_error);
     68 __weak_alias(pcmcia_card_detach, pcmcia_error);
     69 
     70 
     71 STATIC void cardslotchilddet(device_t, device_t);
     72 STATIC void cardslotattach(device_t, device_t, void *);
     73 STATIC int cardslotdetach(device_t, int);
     74 
     75 STATIC int cardslotmatch(device_t, cfdata_t, void *);
     76 static void cardslot_event_thread(void *arg);
     77 
     78 STATIC int cardslot_cb_print(void *aux, const char *pcic);
     79 static int cardslot_16_print(void *, const char *);
     80 static int cardslot_16_submatch(device_t, cfdata_t, const int *, void *);
     81 
     82 CFATTACH_DECL3_NEW(cardslot, sizeof(struct cardslot_softc),
     83     cardslotmatch, cardslotattach, cardslotdetach, NULL, NULL, cardslotchilddet,
     84     DVF_DETACH_SHUTDOWN);
     85 
     86 STATIC int
     87 cardslotmatch(device_t parent, cfdata_t cf,
     88     void *aux)
     89 {
     90 	struct cardslot_attach_args *caa = aux;
     91 
     92 	if (caa->caa_cb_attach == NULL && caa->caa_16_attach == NULL) {
     93 		/* Neither CardBus nor 16-bit PCMCIA are defined. */
     94 		return 0;
     95 	}
     96 
     97 	return 1;
     98 }
     99 
    100 STATIC void
    101 cardslotchilddet(device_t self, device_t child)
    102 {
    103 	struct cardslot_softc *sc = device_private(self);
    104 
    105 	KASSERT(sc->sc_cb_softc == device_private(child) ||
    106 	    sc->sc_16_softc == child);
    107 
    108 	if (sc->sc_cb_softc == device_private(child))
    109 		sc->sc_cb_softc = NULL;
    110 	else if (sc->sc_16_softc == child)
    111 		sc->sc_16_softc = NULL;
    112 }
    113 
    114 STATIC void
    115 cardslotattach(device_t parent, device_t self, void *aux)
    116 {
    117 	struct cardslot_softc *sc = device_private(self);
    118 	struct cardslot_attach_args *caa = aux;
    119 
    120 	struct cbslot_attach_args *cba = caa->caa_cb_attach;
    121 	struct pcmciabus_attach_args *pa = caa->caa_16_attach;
    122 
    123 	struct cardbus_softc *csc = NULL;
    124 	struct pcmcia_softc *psc = NULL;
    125 
    126 	sc->sc_dev = self;
    127 
    128 	sc->sc_cb_softc = NULL;
    129 	sc->sc_16_softc = NULL;
    130 	SIMPLEQ_INIT(&sc->sc_events);
    131 	sc->sc_th_enable = 0;
    132 
    133 	aprint_naive("\n");
    134 	aprint_normal("\n");
    135 
    136 	DPRINTF(("%s attaching CardBus bus...\n", device_xname(self)));
    137 	if (cba != NULL) {
    138 		csc = device_private(config_found_ia(self, "cbbus", cba,
    139 				     cardslot_cb_print));
    140 		if (csc) {
    141 			/* cardbus found */
    142 			DPRINTF(("%s: found cardbus on %s\n", __func__,
    143 				 device_xname(self)));
    144 			sc->sc_cb_softc = csc;
    145 		}
    146 	}
    147 
    148 	if (pa != NULL) {
    149 		sc->sc_16_softc = config_found(self, pa, cardslot_16_print,
    150 		    CFARG_SUBMATCH, cardslot_16_submatch,
    151 		    CFARG_IATTR, "pcmciabus",
    152 		    CFARG_EOL);
    153 		if (sc->sc_16_softc) {
    154 			/* pcmcia 16-bit bus found */
    155 			DPRINTF(("%s: found 16-bit pcmcia bus\n", __func__));
    156 			psc = device_private(sc->sc_16_softc);
    157 		}
    158 	}
    159 
    160 	if (csc != NULL || psc != NULL) {
    161 		sc->sc_th_enable = 1;
    162 		config_pending_incr(self);
    163 		if (kthread_create(PRI_NONE, 0, NULL, cardslot_event_thread,
    164 		    sc, &sc->sc_event_thread, "%s", device_xname(self))) {
    165 			aprint_error_dev(sc->sc_dev,
    166 					 "unable to create thread\n");
    167 			panic("cardslotattach");
    168 		}
    169 	}
    170 
    171 	if (csc && (csc->sc_cf->cardbus_ctrl)(csc->sc_cc, CARDBUS_CD)) {
    172 		DPRINTF(("%s: CardBus card found\n", __func__));
    173 		/* attach deferred */
    174 		cardslot_event_throw(sc, CARDSLOT_EVENT_INSERTION_CB);
    175 	}
    176 
    177 	if (psc && (psc->pct->card_detect)(psc->pch)) {
    178 		DPRINTF(("%s: 16-bit card found\n", __func__));
    179 		/* attach deferred */
    180 		cardslot_event_throw(sc, CARDSLOT_EVENT_INSERTION_16);
    181 	}
    182 
    183 	if (!pmf_device_register(self, NULL, NULL))
    184 		aprint_error_dev(self, "couldn't establish power handler\n");
    185 }
    186 
    187 STATIC int
    188 cardslotdetach(device_t self, int flags)
    189 {
    190 	int rc;
    191 	struct cardslot_softc *sc = device_private(self);
    192 
    193 	if ((rc = config_detach_children(self, flags)) != 0)
    194 		return rc;
    195 
    196 	sc->sc_th_enable = 0;
    197 	wakeup(&sc->sc_events);
    198 	while (sc->sc_event_thread != NULL)
    199 		(void)tsleep(sc, PWAIT, "cardslotthd", 0);
    200 
    201 	if (!SIMPLEQ_EMPTY(&sc->sc_events))
    202 		aprint_error_dev(self, "events outstanding");
    203 
    204 	pmf_device_deregister(self);
    205 	return 0;
    206 }
    207 
    208 STATIC int
    209 cardslot_cb_print(void *aux, const char *pnp)
    210 {
    211 	struct cbslot_attach_args *cba = aux;
    212 
    213 	if (pnp != NULL) {
    214 		aprint_normal("cardbus at %s subordinate bus %d",
    215 		    pnp, cba->cba_bus);
    216 	}
    217 
    218 	return UNCONF;
    219 }
    220 
    221 
    222 static int
    223 cardslot_16_submatch(device_t parent, cfdata_t cf,
    224     const int *ldesc, void *aux)
    225 {
    226 
    227 	if (cf->cf_loc[PCMCIABUSCF_CONTROLLER] != PCMCIABUSCF_CONTROLLER_DEFAULT
    228 	    && cf->cf_loc[PCMCIABUSCF_CONTROLLER] != 0) {
    229 		return 0;
    230 	}
    231 
    232 	if (cf->cf_loc[PCMCIABUSCF_CONTROLLER] == PCMCIABUSCF_CONTROLLER_DEFAULT) {
    233 		return (config_match(parent, cf, aux));
    234 	}
    235 
    236 	return 0;
    237 }
    238 
    239 
    240 
    241 static int
    242 cardslot_16_print(void *arg, const char *pnp)
    243 {
    244 
    245 	if (pnp != NULL) {
    246 		aprint_normal("pcmciabus at %s", pnp);
    247 	}
    248 
    249 	return UNCONF;
    250 }
    251 
    252 
    253 /*
    254  * void cardslot_event_throw(struct cardslot_softc *sc, int ev)
    255  *
    256  *   This function throws an event to the event handler.  If the state
    257  *   of a slot is changed, it should be noticed using this function.
    258  */
    259 void
    260 cardslot_event_throw(struct cardslot_softc *sc, int ev)
    261 {
    262 	struct cardslot_event *ce;
    263 
    264 	DPRINTF(("cardslot_event_throw: an event %s comes\n",
    265 	    ev == CARDSLOT_EVENT_INSERTION_CB ? "CardBus Card inserted" :
    266 	    ev == CARDSLOT_EVENT_INSERTION_16 ? "16-bit Card inserted" :
    267 	    ev == CARDSLOT_EVENT_REMOVAL_CB ? "CardBus Card removed" :
    268 	    ev == CARDSLOT_EVENT_REMOVAL_16 ? "16-bit Card removed" : "???"));
    269 
    270 	if (NULL == (ce = (struct cardslot_event *)malloc(sizeof (struct cardslot_event), M_TEMP, M_NOWAIT))) {
    271 		panic("cardslot_enevt");
    272 	}
    273 
    274 	ce->ce_type = ev;
    275 
    276 	{
    277 		int s = spltty();
    278 		SIMPLEQ_INSERT_TAIL(&sc->sc_events, ce, ce_q);
    279 		splx(s);
    280 	}
    281 
    282 	wakeup(&sc->sc_events);
    283 
    284 	return;
    285 }
    286 
    287 
    288 /*
    289  * static void cardslot_event_thread(void *arg)
    290  *
    291  *   This function is the main routine handing cardslot events such as
    292  *   insertions and removals.
    293  *
    294  */
    295 static void
    296 cardslot_event_thread(void *arg)
    297 {
    298 	struct cardslot_softc *sc = arg;
    299 	struct cardslot_event *ce;
    300 	int s, first = 1;
    301 	static int antonym_ev[4] = {
    302 		CARDSLOT_EVENT_REMOVAL_16, CARDSLOT_EVENT_INSERTION_16,
    303 		CARDSLOT_EVENT_REMOVAL_CB, CARDSLOT_EVENT_INSERTION_CB
    304 	};
    305 
    306 	while (sc->sc_th_enable) {
    307 		s = spltty();
    308 		if ((ce = SIMPLEQ_FIRST(&sc->sc_events)) == NULL) {
    309 			splx(s);
    310 			if (first) {
    311 				first = 0;
    312 				config_pending_decr(sc->sc_dev);
    313 			}
    314 			(void) tsleep(&sc->sc_events, PWAIT, "cardslotev", 0);
    315 			continue;
    316 		}
    317 		SIMPLEQ_REMOVE_HEAD(&sc->sc_events, ce_q);
    318 		splx(s);
    319 
    320 		if (IS_CARDSLOT_INSERT_REMOVE_EV(ce->ce_type)) {
    321 			/* Chattering suppression */
    322 			s = spltty();
    323 			while (1) {
    324 				struct cardslot_event *ce1, *ce2;
    325 
    326 				if ((ce1 = SIMPLEQ_FIRST(&sc->sc_events)) == NULL) {
    327 					break;
    328 				}
    329 				if (ce1->ce_type != antonym_ev[ce->ce_type]) {
    330 					break;
    331 				}
    332 				if ((ce2 = SIMPLEQ_NEXT(ce1, ce_q)) == NULL) {
    333 					break;
    334 				}
    335 				if (ce2->ce_type == ce->ce_type) {
    336 					SIMPLEQ_REMOVE_HEAD(&sc->sc_events,
    337 					    ce_q);
    338 					free(ce1, M_TEMP);
    339 					SIMPLEQ_REMOVE_HEAD(&sc->sc_events,
    340 					    ce_q);
    341 					free(ce2, M_TEMP);
    342 				}
    343 			}
    344 			splx(s);
    345 		}
    346 
    347 		switch (ce->ce_type) {
    348 		case CARDSLOT_EVENT_INSERTION_CB:
    349 			if ((CARDSLOT_CARDTYPE(sc->sc_status) == CARDSLOT_STATUS_CARD_CB)
    350 			    || (CARDSLOT_CARDTYPE(sc->sc_status) == CARDSLOT_STATUS_CARD_16)) {
    351 				if (CARDSLOT_WORK(sc->sc_status) == CARDSLOT_STATUS_WORKING) {
    352 					/*
    353 					 * A card has already been
    354 					 * inserted and works.
    355 					 */
    356 					break;
    357 				}
    358 			}
    359 
    360 			if (sc->sc_cb_softc) {
    361 				CARDSLOT_SET_CARDTYPE(sc->sc_status,
    362 				    CARDSLOT_STATUS_CARD_CB);
    363 				if (cardbus_attach_card(sc->sc_cb_softc) > 0) {
    364 					/* at least one function works */
    365 					CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_WORKING);
    366 				} else {
    367 					/*
    368 					 * no functions work or this
    369 					 * card is not known
    370 					 */
    371 					CARDSLOT_SET_WORK(sc->sc_status,
    372 					    CARDSLOT_STATUS_NOTWORK);
    373 				}
    374 			} else {
    375 				panic("no cardbus on %s",
    376 				      device_xname(sc->sc_dev));
    377 			}
    378 
    379 			break;
    380 
    381 		case CARDSLOT_EVENT_INSERTION_16:
    382 			if ((CARDSLOT_CARDTYPE(sc->sc_status) == CARDSLOT_STATUS_CARD_CB)
    383 			    || (CARDSLOT_CARDTYPE(sc->sc_status) == CARDSLOT_STATUS_CARD_16)) {
    384 				if (CARDSLOT_WORK(sc->sc_status) == CARDSLOT_STATUS_WORKING) {
    385 					/*
    386 					 * A card has already been
    387 					 * inserted and work.
    388 					 */
    389 					break;
    390 				}
    391 			}
    392 			if (sc->sc_16_softc) {
    393 				CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_16);
    394 				if (pcmcia_card_attach(sc->sc_16_softc)) {
    395 					/* Do not attach */
    396 					CARDSLOT_SET_WORK(sc->sc_status,
    397 					    CARDSLOT_STATUS_NOTWORK);
    398 				} else {
    399 					/* working */
    400 					CARDSLOT_SET_WORK(sc->sc_status,
    401 					    CARDSLOT_STATUS_WORKING);
    402 				}
    403 			} else {
    404 				panic("no 16-bit pcmcia on %s",
    405 				      device_xname(sc->sc_dev));
    406 			}
    407 
    408 			break;
    409 
    410 		case CARDSLOT_EVENT_REMOVAL_CB:
    411 			if (CARDSLOT_CARDTYPE(sc->sc_status) == CARDSLOT_STATUS_CARD_CB) {
    412 				/* CardBus card has not been inserted. */
    413 				if (CARDSLOT_WORK(sc->sc_status) == CARDSLOT_STATUS_WORKING) {
    414 					cardbus_detach_card(sc->sc_cb_softc);
    415 					CARDSLOT_SET_WORK(sc->sc_status,
    416 					    CARDSLOT_STATUS_NOTWORK);
    417 					CARDSLOT_SET_WORK(sc->sc_status,
    418 					    CARDSLOT_STATUS_CARD_NONE);
    419 				}
    420 				CARDSLOT_SET_CARDTYPE(sc->sc_status,
    421 				    CARDSLOT_STATUS_CARD_NONE);
    422 			} else if (CARDSLOT_CARDTYPE(sc->sc_status) != CARDSLOT_STATUS_CARD_16) {
    423 				/* Unknown card... */
    424 				CARDSLOT_SET_CARDTYPE(sc->sc_status,
    425 				    CARDSLOT_STATUS_CARD_NONE);
    426 			}
    427 			CARDSLOT_SET_WORK(sc->sc_status,
    428 			    CARDSLOT_STATUS_NOTWORK);
    429 			break;
    430 
    431 		case CARDSLOT_EVENT_REMOVAL_16:
    432 			DPRINTF(("%s: removal event\n", device_xname(sc->sc_dev)));
    433 			if (CARDSLOT_CARDTYPE(sc->sc_status) != CARDSLOT_STATUS_CARD_16) {
    434 				/* 16-bit card has not been inserted. */
    435 				break;
    436 			}
    437 			if ((sc->sc_16_softc != NULL)
    438 			    && (CARDSLOT_WORK(sc->sc_status) == CARDSLOT_STATUS_WORKING)) {
    439 				struct pcmcia_softc *psc =
    440 					device_private(sc->sc_16_softc);
    441 
    442 				pcmcia_card_deactivate(sc->sc_16_softc);
    443 				pcmcia_chip_socket_disable(psc->pct, psc->pch);
    444 				pcmcia_card_detach(sc->sc_16_softc,
    445 						   DETACH_FORCE);
    446 			}
    447 			CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_NONE);
    448 			CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_NOTWORK);
    449 			break;
    450 
    451 		default:
    452 			panic("cardslot_event_thread: unknown event %d", ce->ce_type);
    453 		}
    454 		free(ce, M_TEMP);
    455 	}
    456 
    457 	sc->sc_event_thread = NULL;
    458 
    459 	/* In case the parent device is waiting for us to exit. */
    460 	wakeup(sc);
    461 
    462 	kthread_exit(0);
    463 }
    464