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