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