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