summaryrefslogtreecommitdiff
path: root/sys/dev/cardbus/cardslot.c
blob: a90ca275e866792802cfa36f2267f3d20ed93f34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*	$OpenBSD: cardslot.c,v 1.22 2020/02/18 12:13:39 mpi Exp $	*/
/*	$NetBSD: cardslot.c,v 1.9 2000/03/22 09:35:06 haya Exp $	*/

/*
 * Copyright (c) 1999 and 2000
 *       HAYAKAWA Koichi.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/syslog.h>
#include <sys/pool.h>
#include <sys/task.h>

#include <dev/cardbus/cardslotvar.h>
#include <dev/cardbus/cardbusvar.h>
#include <dev/pcmcia/pcmciavar.h>
#include <dev/pcmcia/pcmciachip.h>
#include <dev/ic/i82365var.h>

#if defined CARDSLOT_DEBUG
#define STATIC
#define DPRINTF(a) printf a
#else
#ifdef DDB
#define STATIC
#else
#define STATIC static
#endif
#define DPRINTF(a)
#endif

STATIC void cardslotattach(struct device *, struct device *, void *);

STATIC int cardslotmatch(struct device *, void *, void *);
STATIC void cardslot_event(void *arg);
STATIC void cardslot_process_event(struct cardslot_softc *);

STATIC int cardslot_cb_print(void *aux, const char *pcic);
STATIC int cardslot_16_print(void *, const char *);
STATIC int cardslot_16_submatch(struct device *, void *,void *);

struct cfattach cardslot_ca = {
	sizeof(struct cardslot_softc), cardslotmatch, cardslotattach
};

struct cfdriver cardslot_cd = {
	NULL, "cardslot", DV_DULL
};

struct pool cardsloteventpool;

STATIC int
cardslotmatch(struct device *parent, void *match, void *aux)
{
	struct cardslot_attach_args *caa = aux;

	if (caa->caa_cb_attach == NULL && caa->caa_16_attach == NULL) {
		/* Neither CardBus nor 16-bit PCMCIA are defined. */
		return (0);
	}

	return (1);
}

STATIC void
cardslotattach(struct device *parent, struct device *self, void *aux)
{
	struct cardslot_softc *sc = (struct cardslot_softc *)self;
	struct cardslot_attach_args *caa = aux;

	struct cbslot_attach_args *cba = caa->caa_cb_attach;
	struct pcmciabus_attach_args *pa = caa->caa_16_attach;

	struct cardbus_softc *csc = NULL;
	struct pcmcia_softc *psc = NULL;

	if (cardsloteventpool.pr_size == 0) {
		pool_init(&cardsloteventpool, sizeof(struct cardslot_event),
		    0, IPL_BIO, 0, "cardslot", NULL);
	}

	sc->sc_slot = sc->sc_dev.dv_unit;
	sc->sc_cb_softc = NULL;
	sc->sc_16_softc = NULL;
	SIMPLEQ_INIT(&sc->sc_events);
	task_set(&sc->sc_event_task, cardslot_event, sc);

	printf(" slot %d flags %x\n", sc->sc_slot,
	    sc->sc_dev.dv_cfdata->cf_flags);

	DPRINTF(("%s attaching CardBus bus...\n", sc->sc_dev.dv_xname));
	if (cba != NULL) {
		if ((csc = (void *)config_found(self, cba,
		    cardslot_cb_print)) != NULL) {
			/* cardbus found */
			DPRINTF(("cardslotattach: found cardbus on %s\n",
			    sc->sc_dev.dv_xname));
			sc->sc_cb_softc = csc;
		}
	}

	if (pa != NULL) {
		if ((psc = (void *)config_found_sm(self, pa, cardslot_16_print,
		    cardslot_16_submatch)) != NULL) {
			/* pcmcia 16-bit bus found */
			DPRINTF(("cardslotattach: found 16-bit pcmcia bus\n"));
			sc->sc_16_softc = psc;
			/* XXX: dirty.  This code should be removed
			 * to achieve MI
			 */
			caa->caa_ph->pcmcia = (struct device *)psc;
		}
	}

	if (csc && (csc->sc_cf->cardbus_ctrl)(csc->sc_cc, CARDBUS_CD)) {
		DPRINTF(("cardslotattach: CardBus card found\n"));
		/* attach deferred */
		cardslot_event_throw(sc, CARDSLOT_EVENT_INSERTION_CB);
	}

	if (psc && (psc->pct->card_detect)(psc->pch)) {
		DPRINTF(("cardbusattach: 16-bit card found\n"));
		/* attach deferred */
		cardslot_event_throw(sc, CARDSLOT_EVENT_INSERTION_16);
	}
}

STATIC int
cardslot_cb_print(void *aux, const char *pnp)
{
	struct cbslot_attach_args *cba = aux;

	if (pnp)
		printf("cardbus at %s subordinate bus %d", pnp, cba->cba_bus);

	return (UNCONF);
}

STATIC int
cardslot_16_submatch(struct device *parent, void *match, void *aux)
{
	struct cfdata *cf = match;

	if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != 0)
		return (0);

	if (cf->cf_loc[0] == -1)
		return ((*cf->cf_attach->ca_match)(parent, cf, aux));

	return (0);
}

STATIC int
cardslot_16_print(void *arg, const char *pnp)
{
	if (pnp)
		printf("pcmciabus at %s", pnp);

	return (UNCONF);
}

/*
 * void cardslot_event_throw(struct cardslot_softc *sc, int ev)
 *
 *   This function throws an event to the event handler.  If the state
 *   of a slot is changed, it should be noticed using this function.
 */
void
cardslot_event_throw(struct cardslot_softc *sc, int ev)
{
	struct cardslot_event *ce;
	int s;

	DPRINTF(("cardslot_event_throw: an event %s comes\n",
	    ev == CARDSLOT_EVENT_INSERTION_CB ? "CardBus Card inserted" :
	    ev == CARDSLOT_EVENT_INSERTION_16 ? "16-bit Card inserted" :
	    ev == CARDSLOT_EVENT_REMOVAL_CB ? "CardBus Card removed" :
	    ev == CARDSLOT_EVENT_REMOVAL_16 ? "16-bit Card removed" : "???"));

	ce = pool_get(&cardsloteventpool, PR_NOWAIT);
	if (ce == NULL)
		return;
	ce->ce_type = ev;

	s = spltty();
	SIMPLEQ_INSERT_TAIL(&sc->sc_events, ce, ce_q);
	splx(s);

	task_add(systq, &sc->sc_event_task);
}

/*
 * STATIC void cardslot_event(void *arg)
 *
 *   This function is the main routine handing cardslot events such as
 *   insertions and removals.
 *
 */
STATIC void
cardslot_event(void *arg1)
{
	struct cardslot_softc *sc = arg1;

	while (!SIMPLEQ_EMPTY(&sc->sc_events))
		cardslot_process_event(sc);
}


STATIC void
cardslot_process_event(struct cardslot_softc *sc)
{
	struct cardslot_event *ce;
	int s, ev;

	s = spltty();
	if ((ce = SIMPLEQ_FIRST(&sc->sc_events)) == NULL) {
		splx(s);
		return;
	}
	SIMPLEQ_REMOVE_HEAD(&sc->sc_events, ce_q);

	if (IS_CARDSLOT_INSERT_REMOVE_EV(ce->ce_type)) {
		/* Chattering suppression */
		static int antonym_ev[4] = {
			CARDSLOT_EVENT_REMOVAL_16,
			CARDSLOT_EVENT_INSERTION_16,
			CARDSLOT_EVENT_REMOVAL_CB,
			CARDSLOT_EVENT_INSERTION_CB
		};

		while (1) {
			struct cardslot_event *ce1, *ce2;

			if ((ce1 = SIMPLEQ_FIRST(&sc->sc_events)) ==
			    NULL)
				break;
			if (ce1->ce_type != antonym_ev[ce->ce_type])
				break;
			if ((ce2 = SIMPLEQ_NEXT(ce1, ce_q)) == NULL)
				break;
			if (ce2->ce_type == ce->ce_type) {
				SIMPLEQ_REMOVE_HEAD(&sc->sc_events, ce_q);
				pool_put(&cardsloteventpool, ce1);
				SIMPLEQ_REMOVE_HEAD(&sc->sc_events, ce_q);
				pool_put(&cardsloteventpool, ce2);
			}
		}
	}
	splx(s);

	ev = ce->ce_type;
	pool_put(&cardsloteventpool, ce);

	switch (ev) {
	case CARDSLOT_EVENT_INSERTION_CB:
		if ((CARDSLOT_CARDTYPE(sc->sc_status) ==
		     CARDSLOT_STATUS_CARD_CB) ||
		    (CARDSLOT_CARDTYPE(sc->sc_status) ==
		     CARDSLOT_STATUS_CARD_16)) {
			if (CARDSLOT_WORK(sc->sc_status) ==
			    CARDSLOT_STATUS_WORKING) {
				/* A card has already been inserted
				 * and works.
				 */
				break;
			}
		}

		if (sc->sc_cb_softc) {
			CARDSLOT_SET_CARDTYPE(sc->sc_status,
			    CARDSLOT_STATUS_CARD_CB);
			if (cardbus_attach_card(sc->sc_cb_softc) > 0) {
				/* At least one function works */
				CARDSLOT_SET_WORK(sc->sc_status,
				    CARDSLOT_STATUS_WORKING);
			} else {
				/* No functions work or this card is
				 * not known
				 */
				CARDSLOT_SET_WORK(sc->sc_status,
				    CARDSLOT_STATUS_NOTWORK);
			}
		} else {
			printf("%s: CardBus support disabled\n",
			    sc->sc_dev.dv_xname);
		}
		break;

	case CARDSLOT_EVENT_INSERTION_16:
		if ((CARDSLOT_CARDTYPE(sc->sc_status) ==
		     CARDSLOT_STATUS_CARD_CB) ||
		    (CARDSLOT_CARDTYPE(sc->sc_status) ==
		     CARDSLOT_STATUS_CARD_16)) {
			if (CARDSLOT_WORK(sc->sc_status) ==
			    CARDSLOT_STATUS_WORKING) {
				/* A card has already been inserted
				 * and works.
				 */
				break;
			}
		}
		if (sc->sc_16_softc) {
			CARDSLOT_SET_CARDTYPE(sc->sc_status,
			    CARDSLOT_STATUS_CARD_16);
			if (pcmcia_card_attach(
			    (struct device *)sc->sc_16_softc)) {
				/* Do not attach */
				CARDSLOT_SET_WORK(sc->sc_status,
				    CARDSLOT_STATUS_NOTWORK);
			} else {
				/* working */
				CARDSLOT_SET_WORK(sc->sc_status,
				    CARDSLOT_STATUS_WORKING);
			}
		} else {
			panic("no 16-bit pcmcia on %s",
			    sc->sc_dev.dv_xname);
		}
		break;

	case CARDSLOT_EVENT_REMOVAL_CB:
		if (CARDSLOT_CARDTYPE(sc->sc_status) ==
		    CARDSLOT_STATUS_CARD_CB) {
			/* CardBus card has not been inserted. */
			if (CARDSLOT_WORK(sc->sc_status) ==
			    CARDSLOT_STATUS_WORKING) {
				cardbus_detach_card(sc->sc_cb_softc);
				CARDSLOT_SET_WORK(sc->sc_status,
				    CARDSLOT_STATUS_NOTWORK);
				CARDSLOT_SET_WORK(sc->sc_status,
				    CARDSLOT_STATUS_CARD_NONE);
			}
			CARDSLOT_SET_CARDTYPE(sc->sc_status,
			    CARDSLOT_STATUS_CARD_NONE);
		} else if (CARDSLOT_CARDTYPE(sc->sc_status) !=
		    CARDSLOT_STATUS_CARD_16) {
			/* Unknown card... */
			CARDSLOT_SET_CARDTYPE(sc->sc_status,
			    CARDSLOT_STATUS_CARD_NONE);
		}
		CARDSLOT_SET_WORK(sc->sc_status,
		    CARDSLOT_STATUS_NOTWORK);
		break;

	case CARDSLOT_EVENT_REMOVAL_16:
		DPRINTF(("%s: removal event\n", sc->sc_dev.dv_xname));
		if (CARDSLOT_CARDTYPE(sc->sc_status) !=
		    CARDSLOT_STATUS_CARD_16) {
			/* 16-bit card has not been inserted. */
			break;
		}
		if ((sc->sc_16_softc != NULL) &&
		    (CARDSLOT_WORK(sc->sc_status) ==
		     CARDSLOT_STATUS_WORKING)) {
			struct pcmcia_softc *psc = sc->sc_16_softc;

			pcmcia_card_deactivate((struct device *)psc);
			pcmcia_chip_socket_disable(psc->pct, psc->pch);
			pcmcia_card_detach((struct device *)psc,
			    DETACH_FORCE);
		}
		CARDSLOT_SET_CARDTYPE(sc->sc_status,
		    CARDSLOT_STATUS_CARD_NONE);
		CARDSLOT_SET_WORK(sc->sc_status,
		    CARDSLOT_STATUS_NOTWORK);
		break;

	default:
		panic("cardslot_event_thread: unknown event %d", ev);
	}
}