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