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