cardslot.c revision 1.54 1 /* $NetBSD: cardslot.c,v 1.54 2012/10/27 17:18:15 chs 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 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: cardslot.c,v 1.54 2012/10/27 17:18:15 chs Exp $");
31
32 #include "opt_cardslot.h"
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/device.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
39 #include <sys/syslog.h>
40 #include <sys/kthread.h>
41
42 #include <sys/bus.h>
43
44 #include <dev/cardbus/cardslotvar.h>
45 #include <dev/cardbus/cardbusvar.h>
46 #include <dev/pcmcia/pcmciavar.h>
47 #include <dev/pcmcia/pcmciachip.h>
48
49 #include "locators.h"
50
51 #if defined CARDSLOT_DEBUG
52 #define STATIC
53 #define DPRINTF(a) printf a
54 #else
55 #define STATIC static
56 #define DPRINTF(a)
57 #endif
58
59
60
61 STATIC void cardslotchilddet(device_t, device_t);
62 STATIC void cardslotattach(device_t, device_t, void *);
63 STATIC int cardslotdetach(device_t, int);
64
65 STATIC int cardslotmatch(device_t, cfdata_t, 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(device_t, cfdata_t, const int *, void *);
71
72 CFATTACH_DECL3_NEW(cardslot, sizeof(struct cardslot_softc),
73 cardslotmatch, cardslotattach, cardslotdetach, NULL, NULL, cardslotchilddet,
74 DVF_DETACH_SHUTDOWN);
75
76 STATIC int
77 cardslotmatch(device_t parent, cfdata_t cf,
78 void *aux)
79 {
80 struct cardslot_attach_args *caa = aux;
81
82 if (caa->caa_cb_attach == NULL && caa->caa_16_attach == NULL) {
83 /* Neither CardBus nor 16-bit PCMCIA are defined. */
84 return 0;
85 }
86
87 return 1;
88 }
89
90 STATIC void
91 cardslotchilddet(device_t self, device_t child)
92 {
93 struct cardslot_softc *sc = device_private(self);
94
95 KASSERT(sc->sc_cb_softc == device_private(child) ||
96 sc->sc_16_softc == child);
97
98 if (sc->sc_cb_softc == device_private(child))
99 sc->sc_cb_softc = NULL;
100 else if (sc->sc_16_softc == child)
101 sc->sc_16_softc = NULL;
102 }
103
104 STATIC void
105 cardslotattach(device_t parent, device_t self, void *aux)
106 {
107 struct cardslot_softc *sc = device_private(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 = NULL;
114 struct pcmcia_softc *psc = NULL;
115
116 sc->sc_dev = self;
117
118 sc->sc_cb_softc = NULL;
119 sc->sc_16_softc = NULL;
120 SIMPLEQ_INIT(&sc->sc_events);
121 sc->sc_th_enable = 0;
122
123 aprint_naive("\n");
124 aprint_normal("\n");
125
126 DPRINTF(("%s attaching CardBus bus...\n", device_xname(self)));
127 if (cba != NULL) {
128 csc = device_private(config_found_ia(self, "cbbus", cba,
129 cardslot_cb_print));
130 if (csc) {
131 /* cardbus found */
132 DPRINTF(("%s: found cardbus on %s\n", __func__,
133 device_xname(self)));
134 sc->sc_cb_softc = csc;
135 }
136 }
137
138 if (pa != NULL) {
139 sc->sc_16_softc = config_found_sm_loc(self, "pcmciabus", NULL,
140 pa, cardslot_16_print,
141 cardslot_16_submatch);
142 if (sc->sc_16_softc) {
143 /* pcmcia 16-bit bus found */
144 DPRINTF(("%s: found 16-bit pcmcia bus\n", __func__));
145 psc = device_private(sc->sc_16_softc);
146 }
147 }
148
149 if (csc != NULL || psc != NULL) {
150 config_pending_incr();
151 if (kthread_create(PRI_NONE, 0, NULL, cardslot_event_thread,
152 sc, &sc->sc_event_thread, "%s", device_xname(self))) {
153 aprint_error_dev(sc->sc_dev,
154 "unable to create thread\n");
155 panic("cardslotattach");
156 }
157 sc->sc_th_enable = 1;
158 }
159
160 if (csc && (csc->sc_cf->cardbus_ctrl)(csc->sc_cc, CARDBUS_CD)) {
161 DPRINTF(("%s: CardBus card found\n", __func__));
162 /* attach deferred */
163 cardslot_event_throw(sc, CARDSLOT_EVENT_INSERTION_CB);
164 }
165
166 if (psc && (psc->pct->card_detect)(psc->pch)) {
167 DPRINTF(("%s: 16-bit card found\n", __func__));
168 /* attach deferred */
169 cardslot_event_throw(sc, CARDSLOT_EVENT_INSERTION_16);
170 }
171
172 if (!pmf_device_register(self, NULL, NULL))
173 aprint_error_dev(self, "couldn't establish power handler\n");
174 }
175
176 STATIC int
177 cardslotdetach(device_t self, int flags)
178 {
179 int rc;
180 struct cardslot_softc *sc = device_private(self);
181
182 if ((rc = config_detach_children(self, flags)) != 0)
183 return rc;
184
185 sc->sc_th_enable = 0;
186 wakeup(&sc->sc_events);
187 while (sc->sc_event_thread != NULL)
188 (void)tsleep(sc, PWAIT, "cardslotthd", 0);
189
190 if (!SIMPLEQ_EMPTY(&sc->sc_events))
191 aprint_error_dev(self, "events outstanding");
192
193 pmf_device_deregister(self);
194 return 0;
195 }
196
197 STATIC int
198 cardslot_cb_print(void *aux, const char *pnp)
199 {
200 struct cbslot_attach_args *cba = aux;
201
202 if (pnp != NULL) {
203 aprint_normal("cardbus at %s subordinate bus %d",
204 pnp, cba->cba_bus);
205 }
206
207 return UNCONF;
208 }
209
210
211 static int
212 cardslot_16_submatch(device_t parent, cfdata_t cf,
213 const int *ldesc, void *aux)
214 {
215
216 if (cf->cf_loc[PCMCIABUSCF_CONTROLLER] != PCMCIABUSCF_CONTROLLER_DEFAULT
217 && cf->cf_loc[PCMCIABUSCF_CONTROLLER] != 0) {
218 return 0;
219 }
220
221 if (cf->cf_loc[PCMCIABUSCF_CONTROLLER] == PCMCIABUSCF_CONTROLLER_DEFAULT) {
222 return (config_match(parent, cf, aux));
223 }
224
225 return 0;
226 }
227
228
229
230 static int
231 cardslot_16_print(void *arg, const char *pnp)
232 {
233
234 if (pnp != NULL) {
235 aprint_normal("pcmciabus at %s", pnp);
236 }
237
238 return UNCONF;
239 }
240
241
242 /*
243 * void cardslot_event_throw(struct cardslot_softc *sc, int ev)
244 *
245 * This function throws an event to the event handler. If the state
246 * of a slot is changed, it should be noticed using this function.
247 */
248 void
249 cardslot_event_throw(struct cardslot_softc *sc, int ev)
250 {
251 struct cardslot_event *ce;
252
253 DPRINTF(("cardslot_event_throw: an event %s comes\n",
254 ev == CARDSLOT_EVENT_INSERTION_CB ? "CardBus Card inserted" :
255 ev == CARDSLOT_EVENT_INSERTION_16 ? "16-bit Card inserted" :
256 ev == CARDSLOT_EVENT_REMOVAL_CB ? "CardBus Card removed" :
257 ev == CARDSLOT_EVENT_REMOVAL_16 ? "16-bit Card removed" : "???"));
258
259 if (NULL == (ce = (struct cardslot_event *)malloc(sizeof (struct cardslot_event), M_TEMP, M_NOWAIT))) {
260 panic("cardslot_enevt");
261 }
262
263 ce->ce_type = ev;
264
265 {
266 int s = spltty();
267 SIMPLEQ_INSERT_TAIL(&sc->sc_events, ce, ce_q);
268 splx(s);
269 }
270
271 wakeup(&sc->sc_events);
272
273 return;
274 }
275
276
277 /*
278 * static void cardslot_event_thread(void *arg)
279 *
280 * This function is the main routine handing cardslot events such as
281 * insertions and removals.
282 *
283 */
284 static void
285 cardslot_event_thread(void *arg)
286 {
287 struct cardslot_softc *sc = arg;
288 struct cardslot_event *ce;
289 int s, first = 1;
290 static int antonym_ev[4] = {
291 CARDSLOT_EVENT_REMOVAL_16, CARDSLOT_EVENT_INSERTION_16,
292 CARDSLOT_EVENT_REMOVAL_CB, CARDSLOT_EVENT_INSERTION_CB
293 };
294
295 while (sc->sc_th_enable) {
296 s = spltty();
297 if ((ce = SIMPLEQ_FIRST(&sc->sc_events)) == NULL) {
298 splx(s);
299 if (first) {
300 first = 0;
301 config_pending_decr();
302 }
303 (void) tsleep(&sc->sc_events, PWAIT, "cardslotev", 0);
304 continue;
305 }
306 SIMPLEQ_REMOVE_HEAD(&sc->sc_events, ce_q);
307 splx(s);
308
309 if (IS_CARDSLOT_INSERT_REMOVE_EV(ce->ce_type)) {
310 /* Chattering suppression */
311 s = spltty();
312 while (1) {
313 struct cardslot_event *ce1, *ce2;
314
315 if ((ce1 = SIMPLEQ_FIRST(&sc->sc_events)) == NULL) {
316 break;
317 }
318 if (ce1->ce_type != antonym_ev[ce->ce_type]) {
319 break;
320 }
321 if ((ce2 = SIMPLEQ_NEXT(ce1, ce_q)) == NULL) {
322 break;
323 }
324 if (ce2->ce_type == ce->ce_type) {
325 SIMPLEQ_REMOVE_HEAD(&sc->sc_events,
326 ce_q);
327 free(ce1, M_TEMP);
328 SIMPLEQ_REMOVE_HEAD(&sc->sc_events,
329 ce_q);
330 free(ce2, M_TEMP);
331 }
332 }
333 splx(s);
334 }
335
336 switch (ce->ce_type) {
337 case CARDSLOT_EVENT_INSERTION_CB:
338 if ((CARDSLOT_CARDTYPE(sc->sc_status) == CARDSLOT_STATUS_CARD_CB)
339 || (CARDSLOT_CARDTYPE(sc->sc_status) == CARDSLOT_STATUS_CARD_16)) {
340 if (CARDSLOT_WORK(sc->sc_status) == CARDSLOT_STATUS_WORKING) {
341 /*
342 * A card has already been
343 * inserted and works.
344 */
345 break;
346 }
347 }
348
349 if (sc->sc_cb_softc) {
350 CARDSLOT_SET_CARDTYPE(sc->sc_status,
351 CARDSLOT_STATUS_CARD_CB);
352 if (cardbus_attach_card(sc->sc_cb_softc) > 0) {
353 /* at least one function works */
354 CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_WORKING);
355 } else {
356 /*
357 * no functions work or this
358 * card is not known
359 */
360 CARDSLOT_SET_WORK(sc->sc_status,
361 CARDSLOT_STATUS_NOTWORK);
362 }
363 } else {
364 panic("no cardbus on %s",
365 device_xname(sc->sc_dev));
366 }
367
368 break;
369
370 case CARDSLOT_EVENT_INSERTION_16:
371 if ((CARDSLOT_CARDTYPE(sc->sc_status) == CARDSLOT_STATUS_CARD_CB)
372 || (CARDSLOT_CARDTYPE(sc->sc_status) == CARDSLOT_STATUS_CARD_16)) {
373 if (CARDSLOT_WORK(sc->sc_status) == CARDSLOT_STATUS_WORKING) {
374 /*
375 * A card has already been
376 * inserted and work.
377 */
378 break;
379 }
380 }
381 if (sc->sc_16_softc) {
382 CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_16);
383 if (pcmcia_card_attach(sc->sc_16_softc)) {
384 /* Do not attach */
385 CARDSLOT_SET_WORK(sc->sc_status,
386 CARDSLOT_STATUS_NOTWORK);
387 } else {
388 /* working */
389 CARDSLOT_SET_WORK(sc->sc_status,
390 CARDSLOT_STATUS_WORKING);
391 }
392 } else {
393 panic("no 16-bit pcmcia on %s",
394 device_xname(sc->sc_dev));
395 }
396
397 break;
398
399 case CARDSLOT_EVENT_REMOVAL_CB:
400 if (CARDSLOT_CARDTYPE(sc->sc_status) == CARDSLOT_STATUS_CARD_CB) {
401 /* CardBus card has not been inserted. */
402 if (CARDSLOT_WORK(sc->sc_status) == CARDSLOT_STATUS_WORKING) {
403 cardbus_detach_card(sc->sc_cb_softc);
404 CARDSLOT_SET_WORK(sc->sc_status,
405 CARDSLOT_STATUS_NOTWORK);
406 CARDSLOT_SET_WORK(sc->sc_status,
407 CARDSLOT_STATUS_CARD_NONE);
408 }
409 CARDSLOT_SET_CARDTYPE(sc->sc_status,
410 CARDSLOT_STATUS_CARD_NONE);
411 } else if (CARDSLOT_CARDTYPE(sc->sc_status) != CARDSLOT_STATUS_CARD_16) {
412 /* Unknown card... */
413 CARDSLOT_SET_CARDTYPE(sc->sc_status,
414 CARDSLOT_STATUS_CARD_NONE);
415 }
416 CARDSLOT_SET_WORK(sc->sc_status,
417 CARDSLOT_STATUS_NOTWORK);
418 break;
419
420 case CARDSLOT_EVENT_REMOVAL_16:
421 DPRINTF(("%s: removal event\n", device_xname(sc->sc_dev)));
422 if (CARDSLOT_CARDTYPE(sc->sc_status) != CARDSLOT_STATUS_CARD_16) {
423 /* 16-bit card has not been inserted. */
424 break;
425 }
426 if ((sc->sc_16_softc != NULL)
427 && (CARDSLOT_WORK(sc->sc_status) == CARDSLOT_STATUS_WORKING)) {
428 struct pcmcia_softc *psc =
429 device_private(sc->sc_16_softc);
430
431 pcmcia_card_deactivate(sc->sc_16_softc);
432 pcmcia_chip_socket_disable(psc->pct, psc->pch);
433 pcmcia_card_detach(sc->sc_16_softc,
434 DETACH_FORCE);
435 }
436 CARDSLOT_SET_CARDTYPE(sc->sc_status, CARDSLOT_STATUS_CARD_NONE);
437 CARDSLOT_SET_WORK(sc->sc_status, CARDSLOT_STATUS_NOTWORK);
438 break;
439
440 default:
441 panic("cardslot_event_thread: unknown event %d", ce->ce_type);
442 }
443 free(ce, M_TEMP);
444 }
445
446 sc->sc_event_thread = NULL;
447
448 /* In case the parent device is waiting for us to exit. */
449 wakeup(sc);
450
451 kthread_exit(0);
452 }
453