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