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