cardslot.c revision 1.9 1 /* $NetBSD: cardslot.c,v 1.9 2000/03/22 09:35:06 haya 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 "opt_cardslot.h"
36
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/device.h>
41 #include <sys/malloc.h>
42 #include <sys/kernel.h>
43 #include <sys/syslog.h>
44 #include <sys/kthread.h>
45
46 #include <machine/bus.h>
47
48 #include <dev/cardbus/cardslotvar.h>
49 #include <dev/cardbus/cardbusvar.h>
50 #include <dev/pcmcia/pcmciavar.h>
51 #include <dev/pcmcia/pcmciachip.h>
52 #include <dev/ic/i82365var.h>
53
54
55 #if defined CARDSLOT_DEBUG
56 #define STATIC
57 #define DPRINTF(a) printf a
58 #else
59 #define STATIC static
60 #define DPRINTF(a)
61 #endif
62
63
64
65 STATIC void cardslotattach __P((struct device *, struct device *, void *));
66
67 #if !defined __BROKEN_INDIRECT_CONFIG
68 STATIC int cardslotmatch __P((struct device *, struct cfdata *, void *));
69 #else
70 STATIC int cardslotmatch __P((struct device *, void *, void *));
71 #endif
72 static void create_slot_manager __P((void *));
73 static void cardslot_event_thread __P((void *arg));
74
75 STATIC int cardslot_cb_print __P((void *aux, const char *pcic));
76 static int cardslot_16_print __P((void *, const char *));
77 static int cardslot_16_submatch __P((struct device *, struct cfdata *,void *));
78
79 struct cfattach cardslot_ca = {
80 sizeof(struct cardslot_softc), cardslotmatch, cardslotattach
81 };
82
83 #ifndef __NetBSD_Version__
84 struct cfdriver cardslot_cd = {
85 NULL, "cardslot", DV_DULL
86 };
87 #endif
88
89
90 STATIC int
91 #if defined __BROKEN_INDIRECT_CONFIG
92 cardslotmatch(parent, match, aux)
93 struct device *parent;
94 void *match;
95 void *aux;
96 #else
97 cardslotmatch(parent, cf, aux)
98 struct device *parent;
99 struct cfdata *cf;
100 void *aux;
101 #endif
102 {
103 #if defined __BROKEN_INDIRECT_CONFIG
104 struct cfdata *cf = match;
105 #endif
106 struct cardslot_attach_args *caa = aux;
107
108 if (caa->caa_cb_attach == NULL && caa->caa_16_attach == NULL) {
109 /* Neither CardBus nor 16-bit PCMCIA are defined. */
110 return 0;
111 }
112
113 return 1;
114 }
115
116
117
118 STATIC void
119 cardslotattach(parent, self, aux)
120 struct device *parent;
121 struct device *self;
122 void *aux;
123 {
124 struct cardslot_softc *sc = (struct cardslot_softc *)self;
125 struct cardslot_attach_args *caa = aux;
126
127 struct cbslot_attach_args *cba = caa->caa_cb_attach;
128 struct pcmciabus_attach_args *pa = caa->caa_16_attach;
129
130 struct cardbus_softc *csc;
131 struct pcmcia_softc *psc;
132
133 int card_attach_now;
134
135 sc->sc_slot = sc->sc_dev.dv_unit;
136 sc->sc_cb_softc = NULL;
137 sc->sc_16_softc = NULL;
138 SIMPLEQ_INIT(&sc->sc_events);
139 sc->sc_th_enable = 0;
140
141 printf(" slot %d flags %x\n", sc->sc_slot, sc->sc_dev.dv_cfdata->cf_flags);
142
143 DPRINTF(("%s attaching CardBus bus...\n", sc->sc_dev.dv_xname));
144 if (cba != NULL) {
145 if (NULL != (csc = (void *)config_found(self, cba, cardslot_cb_print))) {
146 /* cardbus found */
147 DPRINTF(("cardslotattach: found cardbus on %s\n", sc->sc_dev.dv_xname));
148 sc->sc_cb_softc = csc;
149 }
150 }
151
152 if (pa != NULL) {
153 if (NULL != (psc = (void *)config_found_sm(self, pa, cardslot_16_print,
154 cardslot_16_submatch))) {
155 /* pcmcia 16-bit bus found */
156 DPRINTF(("cardslotattach: found 16-bit pcmcia bus\n"));
157 sc->sc_16_softc = psc;
158 /* XXX: dirty. This code should be removed to achieve MI */
159 caa->caa_ph->pcmcia = (struct device *)psc;
160 }
161 }
162
163 if (csc != NULL || psc != NULL) {
164 #if __NetBSD_Version__ > 104060000
165 config_pending_incr();
166 kthread_create(create_slot_manager, (void *)sc);
167 #else
168 kthread_create_deferred(create_slot_manager, (void *)sc);
169 #endif
170 }
171
172 card_attach_now = sc->sc_dev.dv_cfdata->cf_flags & 0x01;
173
174 if (csc && (csc->sc_cf->cardbus_ctrl)(csc->sc_cc, CARDBUS_CD)) {
175 DPRINTF(("cardslotattach: CardBus card found\n"));
176 if (card_attach_now) {
177 if (cardbus_attach_card(sc->sc_cb_softc) > 0) {
178 /* at least one function works */
179 CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_WORKING);
180 } else {
181 /* no functions work or this card is not known */
182 CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_NOTWORK);
183 }
184 CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_CB);
185 } else {
186 /* attach deffered */
187 cardslot_event_throw(sc, CARDSLOT_EVENT_INSERTION_CB);
188 }
189 }
190
191 if (psc && (psc->pct->card_detect)(psc->pch)) {
192 DPRINTF(("cardbusattach: 16-bit card found\n"));
193 if (card_attach_now) {
194 /* attach now */
195 pcmcia_card_attach((struct device *)sc->sc_16_softc);
196 CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_16);
197 } else {
198 /* attach deffered */
199 cardslot_event_throw(sc, CARDSLOT_EVENT_INSERTION_16);
200 }
201 }
202 }
203
204
205
206 STATIC int
207 cardslot_cb_print(aux, pnp)
208 void *aux;
209 const char *pnp;
210 {
211 struct cbslot_attach_args *cba = aux;
212
213 if (pnp) {
214 printf("cardbus at %s subordinate bus %d", pnp, cba->cba_bus);
215 }
216
217 return UNCONF;
218 }
219
220
221 static int
222 cardslot_16_submatch(parent, cf, aux)
223 struct device *parent;
224 struct cfdata *cf;
225 void *aux;
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 ((*cf->cf_attach->ca_match)(parent, cf, aux));
234 }
235
236 return 0;
237 }
238
239
240
241 static int
242 cardslot_16_print(arg, pnp)
243 void *arg;
244 const char *pnp;
245 {
246
247 if (pnp) {
248 printf("pcmciabus at %s", pnp);
249 }
250
251 return UNCONF;
252 }
253
254
255
256
257 static void
258 create_slot_manager(arg)
259 void *arg;
260 {
261 struct cardslot_softc *sc = (struct cardslot_softc *)arg;
262
263 sc->sc_th_enable = 1;
264
265 #if __NetBSD_Version__ > 104060000
266 if (kthread_create1(cardslot_event_thread, sc, &sc->sc_event_thread, "%s",
267 sc->sc_dev.dv_xname)) {
268 #else
269 if (kthread_create(cardslot_event_thread, sc, &sc->sc_event_thread, "%s",
270 sc->sc_dev.dv_xname)) {
271 #endif
272 printf("%s: unable to create event thread for slot %d\n",
273 sc->sc_dev.dv_xname, sc->sc_slot);
274 panic("create_slot_manager");
275 }
276 }
277
278
279
280
281 /*
282 * void cardslot_event_throw(struct cardslot_softc *sc, int ev)
283 *
284 * This function throws an event to the event handler. If the state
285 * of a slot is changed, it should be noticed using this function.
286 */
287 void
288 cardslot_event_throw(sc, ev)
289 struct cardslot_softc *sc;
290 int ev;
291 {
292 struct cardslot_event *ce;
293
294 DPRINTF(("cardslot_event_throw: an event %s comes\n",
295 ev == CARDSLOT_EVENT_INSERTION_CB ? "CardBus Card inserted" :
296 ev == CARDSLOT_EVENT_INSERTION_16 ? "16-bit Card inserted" :
297 ev == CARDSLOT_EVENT_REMOVAL_CB ? "CardBus Card removed" :
298 ev == CARDSLOT_EVENT_REMOVAL_16 ? "16-bit Card removed" : "???"));
299
300 if (NULL == (ce = (struct cardslot_event *)malloc(sizeof (struct cardslot_event), M_TEMP, M_NOWAIT))) {
301 panic("cardslot_enevt");
302 }
303
304 ce->ce_type = ev;
305
306 {
307 int s = spltty();
308 SIMPLEQ_INSERT_TAIL(&sc->sc_events, ce, ce_q);
309 splx(s);
310 }
311
312 wakeup(&sc->sc_events);
313
314 return;
315 }
316
317
318 /*
319 * static void cardslot_event_thread(void *arg)
320 *
321 * This function is the main routine handing cardslot events such as
322 * insertions and removals.
323 *
324 */
325 static void
326 cardslot_event_thread(arg)
327 void *arg;
328 {
329 struct cardslot_softc *sc = arg;
330 struct cardslot_event *ce;
331 int s, first = 1;
332 static int antonym_ev[4] = {
333 CARDSLOT_EVENT_REMOVAL_16, CARDSLOT_EVENT_INSERTION_16,
334 CARDSLOT_EVENT_REMOVAL_CB, CARDSLOT_EVENT_INSERTION_CB
335 };
336
337 while (sc->sc_th_enable) {
338 s = spltty();
339 if ((ce = SIMPLEQ_FIRST(&sc->sc_events)) == NULL) {
340 splx(s);
341 if (first) {
342 first = 0;
343 config_pending_decr();
344 }
345 (void) tsleep(&sc->sc_events, PWAIT, "cardslotev", 0);
346 continue;
347 }
348 SIMPLEQ_REMOVE_HEAD(&sc->sc_events, ce, ce_q);
349 splx(s);
350
351 if (IS_CARDSLOT_INSERT_REMOVE_EV(ce->ce_type)) {
352 /* Chattering supression */
353 s = spltty();
354 while (1) {
355 struct cardslot_event *ce1, *ce2;
356
357 if ((ce1 = SIMPLEQ_FIRST(&sc->sc_events)) == NULL) {
358 break;
359 }
360 if (ce1->ce_type != antonym_ev[ce->ce_type]) {
361 break;
362 }
363 if ((ce2 = SIMPLEQ_NEXT(ce1, ce_q)) == NULL) {
364 break;
365 }
366 if (ce2->ce_type == ce->ce_type) {
367 SIMPLEQ_REMOVE_HEAD(&sc->sc_events, ce1, ce_q);
368 free(ce1, M_TEMP);
369 SIMPLEQ_REMOVE_HEAD(&sc->sc_events, ce2, ce_q);
370 free(ce2, M_TEMP);
371 }
372 }
373 splx(s);
374 }
375
376 switch (ce->ce_type) {
377 case CARDSLOT_EVENT_INSERTION_CB:
378 if ((CARDSLOT_CARDTYPE(sc->sc_status) == CARDSLOT_STATUS_CARD_CB)
379 || (CARDSLOT_CARDTYPE(sc->sc_status) == CARDSLOT_STATUS_CARD_16)) {
380 if (CARDSLOT_WORK(sc->sc_status) == CARDSLOT_STATUS_WORKING) {
381 /* A card has already been inserted and work. */
382 break;
383 }
384 }
385
386 if (sc->sc_cb_softc) {
387 CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_CB);
388 if (cardbus_attach_card(sc->sc_cb_softc) > 0) {
389 /* at least one function works */
390 CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_WORKING);
391 } else {
392 /* no functions work or this card is not known */
393 CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_NOTWORK);
394 }
395 } else {
396 panic("no cardbus on %s", sc->sc_dev.dv_xname);
397 }
398
399 break;
400
401 case CARDSLOT_EVENT_INSERTION_16:
402 if ((CARDSLOT_CARDTYPE(sc->sc_status) == CARDSLOT_STATUS_CARD_CB)
403 || (CARDSLOT_CARDTYPE(sc->sc_status) == CARDSLOT_STATUS_CARD_16)) {
404 if (CARDSLOT_WORK(sc->sc_status) == CARDSLOT_STATUS_WORKING) {
405 /* A card has already been inserted and work. */
406 break;
407 }
408 }
409 if (sc->sc_16_softc) {
410 CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_16);
411 if (pcmcia_card_attach((struct device *)sc->sc_16_softc)) {
412 /* Do not attach */
413 CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_NOTWORK);
414 } else {
415 /* working */
416 CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_WORKING);
417 }
418 } else {
419 panic("no 16-bit pcmcia on %s", sc->sc_dev.dv_xname);
420 }
421
422 break;
423
424 case CARDSLOT_EVENT_REMOVAL_CB:
425 if (CARDSLOT_CARDTYPE(sc->sc_status) == CARDSLOT_STATUS_CARD_CB) {
426 /* CardBus card has not been inserted. */
427 if (CARDSLOT_WORK(sc->sc_status) == CARDSLOT_STATUS_WORKING) {
428 cardbus_detach_card(sc->sc_cb_softc);
429 CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_NOTWORK);
430 CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_CARD_NONE);
431 }
432 CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_NONE);
433 } else if (CARDSLOT_CARDTYPE(sc->sc_status) != CARDSLOT_STATUS_CARD_16) {
434 /* Unknown card... */
435 CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_NONE);
436 }
437 CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_NOTWORK);
438 break;
439
440 case CARDSLOT_EVENT_REMOVAL_16:
441 DPRINTF(("%s: removal event\n", sc->sc_dev.dv_xname));
442 if (CARDSLOT_CARDTYPE(sc->sc_status) != CARDSLOT_STATUS_CARD_16) {
443 /* 16-bit card has not been inserted. */
444 break;
445 }
446 if ((sc->sc_16_softc != NULL)
447 && (CARDSLOT_WORK(sc->sc_status) == CARDSLOT_STATUS_WORKING)) {
448 struct pcmcia_softc *psc = sc->sc_16_softc;
449
450 pcmcia_card_deactivate((struct device *)psc);
451 pcmcia_chip_socket_disable(psc->pct, psc->pch);
452 pcmcia_card_detach((struct device *)psc, DETACH_FORCE);
453 }
454 CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_NONE);
455 CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_NOTWORK);
456 break;
457
458 default:
459 panic("cardslot_event_thread: unknown event %d", ce->ce_type);
460 }
461 free(ce, M_TEMP);
462 }
463
464 sc->sc_event_thread = NULL;
465
466 /* In case parent is waiting for us to exit. */
467 wakeup(sc);
468
469 kthread_exit(0);
470 }
471