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