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