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