1 /*	$OpenBSD: cardslot.c,v 1.6 2004/07/25 00:13:29 brad Exp $ */
2 /*	$NetBSD: cardslot.c,v 1.9 2000/03/22 09:35:06 haya Exp $	*/
3 
4 /*
5  * Copyright (c) 1999 and 2000
6  *       HAYAKAWA Koichi.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by HAYAKAWA Koichi.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
39 #include <sys/malloc.h>
40 #include <sys/kernel.h>
41 #include <sys/syslog.h>
42 #include <sys/kthread.h>
43 
44 #include <machine/bus.h>
45 
46 #include <dev/cardbus/cardslotvar.h>
47 #include <dev/cardbus/cardbusvar.h>
48 #include <dev/pcmcia/pcmciavar.h>
49 #include <dev/pcmcia/pcmciachip.h>
50 #include <dev/ic/i82365var.h>
51 
52 #if defined CARDSLOT_DEBUG
53 #define STATIC
54 #define DPRINTF(a) printf a
55 #else
56 #define STATIC static
57 #define DPRINTF(a)
58 #endif
59 
60 
61 
62 STATIC void cardslotattach(struct device *, struct device *, void *);
63 
64 STATIC int cardslotmatch(struct device *, void *, void *);
65 static void create_slot_manager(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(struct device *, void *,void *);
71 
72 struct cfattach cardslot_ca = {
73 	sizeof(struct cardslot_softc), cardslotmatch, cardslotattach
74 };
75 
76 #ifndef __NetBSD_Version__
77 struct cfdriver cardslot_cd = {
78 	NULL, "cardslot", DV_DULL
79 };
80 #endif
81 
82 
83 STATIC int
cardslotmatch(parent,match,aux)84 cardslotmatch(parent, match, aux)
85      struct device *parent;
86      void *match;
87      void *aux;
88 {
89   struct cardslot_attach_args *caa = aux;
90 
91   if (caa->caa_cb_attach == NULL && caa->caa_16_attach == NULL) {
92     /* Neither CardBus nor 16-bit PCMCIA are defined. */
93     return 0;
94   }
95 
96   return 1;
97 }
98 
99 
100 
101 STATIC void
cardslotattach(parent,self,aux)102 cardslotattach(parent, self, aux)
103      struct device *parent;
104      struct device *self;
105      void *aux;
106 {
107   struct cardslot_softc *sc = (struct cardslot_softc *)self;
108   struct cardslot_attach_args *caa = aux;
109 
110   struct cbslot_attach_args *cba = caa->caa_cb_attach;
111   struct pcmciabus_attach_args *pa = caa->caa_16_attach;
112 
113   struct cardbus_softc *csc;
114   struct pcmcia_softc *psc;
115 
116   sc->sc_slot = sc->sc_dev.dv_unit;
117   sc->sc_cb_softc = NULL;
118   sc->sc_16_softc = NULL;
119   SIMPLEQ_INIT(&sc->sc_events);
120   sc->sc_th_enable = 0;
121 
122   printf(" slot %d flags %x\n", sc->sc_slot, sc->sc_dev.dv_cfdata->cf_flags);
123 
124   DPRINTF(("%s attaching CardBus bus...\n", sc->sc_dev.dv_xname));
125   if (cba != NULL) {
126     if (NULL != (csc = (void *)config_found(self, cba, cardslot_cb_print))) {
127       /* cardbus found */
128       DPRINTF(("cardslotattach: found cardbus on %s\n", sc->sc_dev.dv_xname));
129       sc->sc_cb_softc = csc;
130     }
131   }
132 
133   if (pa != NULL) {
134     if (NULL != (psc = (void *)config_found_sm(self, pa, cardslot_16_print,
135 				       cardslot_16_submatch))) {
136       /* pcmcia 16-bit bus found */
137       DPRINTF(("cardslotattach: found 16-bit pcmcia bus\n"));
138       sc->sc_16_softc = psc;
139       /* XXX: dirty.  This code should be removed to achieve MI */
140       caa->caa_ph->pcmcia = (struct device *)psc;
141     }
142   }
143 
144   if (csc != NULL || psc != NULL)
145     kthread_create_deferred(create_slot_manager, (void *)sc);
146 
147   if (csc && (csc->sc_cf->cardbus_ctrl)(csc->sc_cc, CARDBUS_CD)) {
148     DPRINTF(("cardslotattach: CardBus card found\n"));
149     /* attach deferred */
150     cardslot_event_throw(sc, CARDSLOT_EVENT_INSERTION_CB);
151   }
152 
153   if (psc && (psc->pct->card_detect)(psc->pch)) {
154     DPRINTF(("cardbusattach: 16-bit card found\n"));
155     /* attach deferred */
156     cardslot_event_throw(sc, CARDSLOT_EVENT_INSERTION_16);
157   }
158 }
159 
160 
161 
162 STATIC int
cardslot_cb_print(aux,pnp)163 cardslot_cb_print(aux, pnp)
164 	void *aux;
165 	const char *pnp;
166 {
167 	struct cbslot_attach_args *cba = aux;
168 
169 	if (pnp) {
170 		printf("cardbus at %s subordinate bus %d", pnp, cba->cba_bus);
171 	}
172 
173 	return UNCONF;
174 }
175 
176 
177 static int
cardslot_16_submatch(parent,match,aux)178 cardslot_16_submatch(parent, match, aux)
179      struct device *parent;
180      void *match;
181      void *aux;
182 {
183   struct cfdata *cf = match;
184 
185   if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != 0)
186     return 0;
187 
188   if (cf->cf_loc[0] == -1) {
189     return ((*cf->cf_attach->ca_match)(parent, cf, aux));
190   }
191 
192   return 0;
193 }
194 
195 
196 
197 static int
cardslot_16_print(arg,pnp)198 cardslot_16_print(arg, pnp)
199      void *arg;
200      const char *pnp;
201 {
202 
203   if (pnp) {
204     printf("pcmciabus at %s", pnp);
205   }
206 
207   return UNCONF;
208 }
209 
210 
211 
212 
213 static void
create_slot_manager(arg)214 create_slot_manager(arg)
215      void *arg;
216 {
217   struct cardslot_softc *sc = (struct cardslot_softc *)arg;
218 
219   sc->sc_th_enable = 1;
220 
221  if (kthread_create(cardslot_event_thread, sc, &sc->sc_event_thread, "%s",
222 		     sc->sc_dev.dv_xname)) {
223     printf("%s: unable to create event thread for slot %d\n",
224 	   sc->sc_dev.dv_xname, sc->sc_slot);
225     panic("create_slot_manager");
226   }
227 }
228 
229 
230 
231 
232 /*
233  * void cardslot_event_throw(struct cardslot_softc *sc, int ev)
234  *
235  *   This function throws an event to the event handler.  If the state
236  *   of a slot is changed, it should be noticed using this function.
237  */
238 void
cardslot_event_throw(sc,ev)239 cardslot_event_throw(sc, ev)
240      struct cardslot_softc *sc;
241      int ev;
242 {
243   struct cardslot_event *ce;
244 
245   DPRINTF(("cardslot_event_throw: an event %s comes\n",
246 	   ev == CARDSLOT_EVENT_INSERTION_CB ? "CardBus Card inserted" :
247 	   ev == CARDSLOT_EVENT_INSERTION_16 ? "16-bit Card inserted" :
248 	   ev == CARDSLOT_EVENT_REMOVAL_CB ? "CardBus Card removed" :
249 	   ev == CARDSLOT_EVENT_REMOVAL_16 ? "16-bit Card removed" : "???"));
250 
251   if (NULL == (ce = (struct cardslot_event *)malloc(sizeof (struct cardslot_event), M_TEMP, M_NOWAIT))) {
252     panic("cardslot_event");
253   }
254 
255   ce->ce_type = ev;
256 
257   {
258     int s = spltty();
259     SIMPLEQ_INSERT_TAIL(&sc->sc_events, ce, ce_q);
260     splx(s);
261   }
262 
263   wakeup(&sc->sc_events);
264 
265   return;
266 }
267 
268 
269 /*
270  * static void cardslot_event_thread(void *arg)
271  *
272  *   This function is the main routine handing cardslot events such as
273  *   insertions and removals.
274  *
275  */
276 static void
cardslot_event_thread(arg)277 cardslot_event_thread(arg)
278      void *arg;
279 {
280   struct cardslot_softc *sc = arg;
281   struct cardslot_event *ce;
282   int s;
283   static int antonym_ev[4] = {
284     CARDSLOT_EVENT_REMOVAL_16, CARDSLOT_EVENT_INSERTION_16,
285     CARDSLOT_EVENT_REMOVAL_CB, CARDSLOT_EVENT_INSERTION_CB
286   };
287 
288   while (sc->sc_th_enable) {
289     s = spltty();
290     if ((ce = SIMPLEQ_FIRST(&sc->sc_events)) == NULL) {
291       splx(s);
292       (void) tsleep(&sc->sc_events, PWAIT, "cardslotev", 0);
293       continue;
294     }
295     SIMPLEQ_REMOVE_HEAD(&sc->sc_events, ce_q);
296     splx(s);
297 
298     if (IS_CARDSLOT_INSERT_REMOVE_EV(ce->ce_type)) {
299       /* Chattering suppression */
300       s = spltty();
301       while (1) {
302 	struct cardslot_event *ce1, *ce2;
303 
304 	if ((ce1 = SIMPLEQ_FIRST(&sc->sc_events)) == NULL) {
305 	  break;
306 	}
307 	if (ce1->ce_type != antonym_ev[ce->ce_type]) {
308 	  break;
309 	}
310 	if ((ce2 = SIMPLEQ_NEXT(ce1, ce_q)) == NULL) {
311 	  break;
312 	}
313 	if (ce2->ce_type == ce->ce_type) {
314 	  SIMPLEQ_REMOVE_HEAD(&sc->sc_events, ce_q);
315 	  free(ce1, M_TEMP);
316 	  SIMPLEQ_REMOVE_HEAD(&sc->sc_events, ce_q);
317 	  free(ce2, M_TEMP);
318 	}
319       }
320       splx(s);
321     }
322 
323     switch (ce->ce_type) {
324     case CARDSLOT_EVENT_INSERTION_CB:
325       if ((CARDSLOT_CARDTYPE(sc->sc_status) == CARDSLOT_STATUS_CARD_CB)
326 	  || (CARDSLOT_CARDTYPE(sc->sc_status) == CARDSLOT_STATUS_CARD_16)) {
327 	if (CARDSLOT_WORK(sc->sc_status) == CARDSLOT_STATUS_WORKING) {
328 	  /* A card has already been inserted and works. */
329 	  break;
330 	}
331       }
332 
333       if (sc->sc_cb_softc) {
334 	CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_CB);
335 	if (cardbus_attach_card(sc->sc_cb_softc) > 0) {
336 	  /* at least one function works */
337 	  CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_WORKING);
338 	} else {
339 	  /* no functions work or this card is not known */
340 	  CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_NOTWORK);
341 	}
342       } else {
343 	panic("no cardbus on %s", sc->sc_dev.dv_xname);
344       }
345 
346       break;
347 
348     case CARDSLOT_EVENT_INSERTION_16:
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 	  /* A card has already been inserted and works. */
353 	  break;
354 	}
355       }
356       if (sc->sc_16_softc) {
357 	CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_16);
358 	if (pcmcia_card_attach((struct device *)sc->sc_16_softc)) {
359 	  /* Do not attach */
360 	  CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_NOTWORK);
361 	} else {
362 	  /* working */
363 	  CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_WORKING);
364 	}
365       } else {
366 	panic("no 16-bit pcmcia on %s", sc->sc_dev.dv_xname);
367       }
368 
369       break;
370 
371     case CARDSLOT_EVENT_REMOVAL_CB:
372       if (CARDSLOT_CARDTYPE(sc->sc_status) == CARDSLOT_STATUS_CARD_CB) {
373 	/* CardBus card has not been inserted. */
374 	if (CARDSLOT_WORK(sc->sc_status) == CARDSLOT_STATUS_WORKING) {
375 	  cardbus_detach_card(sc->sc_cb_softc);
376 	  CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_NOTWORK);
377 	  CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_CARD_NONE);
378 	}
379 	CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_NONE);
380       } else if (CARDSLOT_CARDTYPE(sc->sc_status) != CARDSLOT_STATUS_CARD_16) {
381 	/* Unknown card... */
382 	CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_NONE);
383       }
384       CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_NOTWORK);
385       break;
386 
387     case CARDSLOT_EVENT_REMOVAL_16:
388       DPRINTF(("%s: removal event\n", sc->sc_dev.dv_xname));
389       if (CARDSLOT_CARDTYPE(sc->sc_status) != CARDSLOT_STATUS_CARD_16) {
390 	/* 16-bit card has not been inserted. */
391 	break;
392       }
393       if ((sc->sc_16_softc != NULL)
394 	  && (CARDSLOT_WORK(sc->sc_status) == CARDSLOT_STATUS_WORKING)) {
395 	struct pcmcia_softc *psc = sc->sc_16_softc;
396 
397 	pcmcia_card_deactivate((struct device *)psc);
398 	pcmcia_chip_socket_disable(psc->pct, psc->pch);
399 	pcmcia_card_detach((struct device *)psc, DETACH_FORCE);
400       }
401       CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_NONE);
402       CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_NOTWORK);
403       break;
404 
405     default:
406       panic("cardslot_event_thread: unknown event %d", ce->ce_type);
407     }
408     free(ce, M_TEMP);
409   }
410 
411   sc->sc_event_thread = NULL;
412 
413   /* In case the parent device is waiting for us to exit. */
414   wakeup(sc);
415 
416   kthread_exit(0);
417 }
418