gpioirq.c revision 1.1.36.1 1 1.1.36.1 bouyer /* $NetBSD: gpioirq.c,v 1.1.36.1 2023/11/26 11:45:16 bouyer Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1.36.1 bouyer * Copyright (c) 2016, 2023 Brad Spencer <brad (at) anduin.eldar.org>
5 1.1 thorpej *
6 1.1 thorpej * Permission to use, copy, modify, and distribute this software for any
7 1.1 thorpej * purpose with or without fee is hereby granted, provided that the above
8 1.1 thorpej * copyright notice and this permission notice appear in all copies.
9 1.1 thorpej *
10 1.1 thorpej * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 1.1 thorpej * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 1.1 thorpej * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 1.1 thorpej * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 1.1 thorpej * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 1.1 thorpej * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 1.1 thorpej * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1.1 thorpej */
18 1.1 thorpej
19 1.1 thorpej #include <sys/cdefs.h>
20 1.1.36.1 bouyer __KERNEL_RCSID(0, "$NetBSD: gpioirq.c,v 1.1.36.1 2023/11/26 11:45:16 bouyer Exp $");
21 1.1 thorpej
22 1.1 thorpej /*
23 1.1.36.1 bouyer * GPIO driver that uses interrupts and can send that fact to userland.
24 1.1 thorpej */
25 1.1 thorpej
26 1.1 thorpej #include <sys/param.h>
27 1.1 thorpej #include <sys/systm.h>
28 1.1 thorpej #include <sys/device.h>
29 1.1.36.1 bouyer #include <sys/device_impl.h>
30 1.1 thorpej #include <sys/gpio.h>
31 1.1 thorpej #include <sys/module.h>
32 1.1.36.1 bouyer #include <sys/conf.h>
33 1.1.36.1 bouyer #include <sys/proc.h>
34 1.1.36.1 bouyer #include <sys/pool.h>
35 1.1.36.1 bouyer #include <sys/kmem.h>
36 1.1.36.1 bouyer #include <sys/condvar.h>
37 1.1 thorpej
38 1.1 thorpej #include <dev/gpio/gpiovar.h>
39 1.1 thorpej
40 1.1.36.1 bouyer #define GPIOIRQ_NPINS 64
41 1.1.36.1 bouyer
42 1.1.36.1 bouyer struct gpioirq_iv {
43 1.1.36.1 bouyer char sc_intrstr[128];
44 1.1.36.1 bouyer void * sc_ih;
45 1.1.36.1 bouyer int i_thispin_index;
46 1.1.36.1 bouyer uint8_t i_thispin_num;
47 1.1.36.1 bouyer uint8_t i_parentunit;
48 1.1.36.1 bouyer struct gpioirq_softc *sc;
49 1.1.36.1 bouyer };
50 1.1 thorpej
51 1.1 thorpej struct gpioirq_softc {
52 1.1 thorpej device_t sc_dev;
53 1.1.36.1 bouyer device_t sc_parentdev;
54 1.1 thorpej void * sc_gpio;
55 1.1 thorpej struct gpio_pinmap sc_map;
56 1.1 thorpej int _map[GPIOIRQ_NPINS];
57 1.1.36.1 bouyer struct gpioirq_iv sc_intrs[GPIOIRQ_NPINS];
58 1.1.36.1 bouyer int sc_npins;
59 1.1 thorpej kmutex_t sc_lock;
60 1.1.36.1 bouyer kmutex_t sc_read_mutex;
61 1.1.36.1 bouyer kmutex_t sc_dying_mutex;
62 1.1 thorpej bool sc_verbose;
63 1.1 thorpej bool sc_functional;
64 1.1.36.1 bouyer bool sc_opened;
65 1.1.36.1 bouyer bool sc_dying;
66 1.1.36.1 bouyer kcondvar_t sc_condreadready;
67 1.1.36.1 bouyer kcondvar_t sc_cond_dying;
68 1.1.36.1 bouyer pool_cache_t sc_readpool;
69 1.1.36.1 bouyer char *sc_readpoolname;
70 1.1.36.1 bouyer SIMPLEQ_HEAD(,gpioirq_read_q) sc_read_queue;
71 1.1.36.1 bouyer };
72 1.1.36.1 bouyer
73 1.1.36.1 bouyer struct gpioirq_read_q {
74 1.1.36.1 bouyer int parentunit;
75 1.1.36.1 bouyer int thepin;
76 1.1.36.1 bouyer int theval;
77 1.1.36.1 bouyer SIMPLEQ_ENTRY(gpioirq_read_q) read_q;
78 1.1 thorpej };
79 1.1 thorpej
80 1.1 thorpej #define GPIOIRQ_FLAGS_IRQMODE GPIO_INTR_MODE_MASK
81 1.1 thorpej #define GPIOIRQ_FLAGS_VERBOSE 0x1000
82 1.1 thorpej
83 1.1 thorpej static int gpioirq_match(device_t, cfdata_t, void *);
84 1.1 thorpej static void gpioirq_attach(device_t, device_t, void *);
85 1.1 thorpej static int gpioirq_detach(device_t, int);
86 1.1 thorpej static int gpioirq_activate(device_t, enum devact);
87 1.1 thorpej static int gpioirq_intr(void *);
88 1.1.36.1 bouyer static uint8_t gpioirq_index_to_pin_num(struct gpioirq_softc *, int);
89 1.1.36.1 bouyer static uint8_t gpioirq_parent_unit(struct gpioirq_softc *);
90 1.1 thorpej
91 1.1 thorpej CFATTACH_DECL_NEW(gpioirq, sizeof(struct gpioirq_softc),
92 1.1 thorpej gpioirq_match, gpioirq_attach,
93 1.1 thorpej gpioirq_detach, gpioirq_activate);
94 1.1 thorpej
95 1.1 thorpej extern struct cfdriver gpioirq_cd;
96 1.1 thorpej
97 1.1.36.1 bouyer static dev_type_open(gpioirq_open);
98 1.1.36.1 bouyer static dev_type_read(gpioirq_read);
99 1.1.36.1 bouyer static dev_type_close(gpioirq_close);
100 1.1.36.1 bouyer const struct cdevsw gpioirq_cdevsw = {
101 1.1.36.1 bouyer .d_open = gpioirq_open,
102 1.1.36.1 bouyer .d_close = gpioirq_close,
103 1.1.36.1 bouyer .d_read = gpioirq_read,
104 1.1.36.1 bouyer .d_write = nowrite,
105 1.1.36.1 bouyer .d_ioctl = noioctl,
106 1.1.36.1 bouyer .d_stop = nostop,
107 1.1.36.1 bouyer .d_tty = notty,
108 1.1.36.1 bouyer .d_poll = nopoll,
109 1.1.36.1 bouyer .d_mmap = nommap,
110 1.1.36.1 bouyer .d_kqfilter = nokqfilter,
111 1.1.36.1 bouyer .d_discard = nodiscard,
112 1.1.36.1 bouyer .d_flag = D_OTHER
113 1.1.36.1 bouyer };
114 1.1.36.1 bouyer
115 1.1.36.1 bouyer static uint8_t
116 1.1.36.1 bouyer gpioirq_index_to_pin_num(struct gpioirq_softc *sc, int index)
117 1.1.36.1 bouyer {
118 1.1.36.1 bouyer return (uint8_t)gpio_pin_to_pin_num(sc->sc_gpio, &sc->sc_map, index);
119 1.1.36.1 bouyer }
120 1.1.36.1 bouyer
121 1.1.36.1 bouyer static uint8_t
122 1.1.36.1 bouyer gpioirq_parent_unit(struct gpioirq_softc *sc)
123 1.1.36.1 bouyer {
124 1.1.36.1 bouyer device_t parent = sc->sc_parentdev;
125 1.1.36.1 bouyer
126 1.1.36.1 bouyer return (uint8_t)parent->dv_unit;
127 1.1.36.1 bouyer }
128 1.1.36.1 bouyer
129 1.1 thorpej static int
130 1.1 thorpej gpioirq_match(device_t parent, cfdata_t cf, void *aux)
131 1.1 thorpej {
132 1.1 thorpej struct gpio_attach_args *ga = aux;
133 1.1 thorpej
134 1.1 thorpej if (strcmp(ga->ga_dvname, cf->cf_name))
135 1.1 thorpej return (0);
136 1.1 thorpej
137 1.1.36.1 bouyer if (ga->ga_offset == -1)
138 1.1 thorpej return (0);
139 1.1 thorpej
140 1.1 thorpej return (1);
141 1.1 thorpej }
142 1.1 thorpej
143 1.1 thorpej static void
144 1.1 thorpej gpioirq_attach(device_t parent, device_t self, void *aux)
145 1.1 thorpej {
146 1.1 thorpej struct gpioirq_softc *sc = device_private(self);
147 1.1 thorpej struct gpio_attach_args *ga = aux;
148 1.1.36.1 bouyer int mask = ga->ga_mask;
149 1.1 thorpej int irqmode, flags;
150 1.1 thorpej
151 1.1 thorpej sc->sc_dev = self;
152 1.1.36.1 bouyer sc->sc_parentdev = parent;
153 1.1.36.1 bouyer sc->sc_opened = false;
154 1.1.36.1 bouyer sc->sc_dying = false;
155 1.1.36.1 bouyer sc->sc_readpoolname = NULL;
156 1.1 thorpej
157 1.1 thorpej /* Map pins */
158 1.1 thorpej sc->sc_gpio = ga->ga_gpio;
159 1.1 thorpej sc->sc_map.pm_map = sc->_map;
160 1.1 thorpej
161 1.1.36.1 bouyer /* Determine our pin configuation. */
162 1.1.36.1 bouyer sc->sc_npins = gpio_npins(mask);
163 1.1.36.1 bouyer if (sc->sc_npins == 0) {
164 1.1.36.1 bouyer sc->sc_npins = 1;
165 1.1.36.1 bouyer mask = 0x1;
166 1.1.36.1 bouyer }
167 1.1.36.1 bouyer
168 1.1.36.1 bouyer /* XXX - exit if more than allowed number of pins */
169 1.1.36.1 bouyer
170 1.1 thorpej if (gpio_pin_map(sc->sc_gpio, ga->ga_offset,
171 1.1.36.1 bouyer mask, &sc->sc_map)) {
172 1.1 thorpej aprint_error(": can't map pins\n");
173 1.1 thorpej return;
174 1.1 thorpej }
175 1.1 thorpej
176 1.1 thorpej aprint_normal("\n");
177 1.1 thorpej
178 1.1 thorpej if (ga->ga_flags & GPIOIRQ_FLAGS_VERBOSE)
179 1.1 thorpej sc->sc_verbose = true;
180 1.1 thorpej
181 1.1 thorpej irqmode = ga->ga_flags & GPIOIRQ_FLAGS_IRQMODE;
182 1.1 thorpej
183 1.1 thorpej mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
184 1.1.36.1 bouyer mutex_init(&sc->sc_dying_mutex, MUTEX_DEFAULT, IPL_VM);
185 1.1.36.1 bouyer mutex_init(&sc->sc_read_mutex, MUTEX_DEFAULT, IPL_VM);
186 1.1.36.1 bouyer cv_init(&sc->sc_cond_dying, "girqdie");
187 1.1.36.1 bouyer cv_init(&sc->sc_condreadready,"girqrr");
188 1.1.36.1 bouyer sc->sc_readpoolname = kmem_asprintf("girqread%d",device_unit(self));
189 1.1.36.1 bouyer sc->sc_readpool = pool_cache_init(sizeof(struct gpioirq_read_q),0,0,0,sc->sc_readpoolname,NULL,IPL_VM,NULL,NULL,NULL);
190 1.1.36.1 bouyer pool_cache_sethiwat(sc->sc_readpool,100);
191 1.1.36.1 bouyer SIMPLEQ_INIT(&sc->sc_read_queue);
192 1.1.36.1 bouyer
193 1.1.36.1 bouyer for(int apin = 0; apin < sc->sc_npins; apin++) {
194 1.1.36.1 bouyer if (!gpio_intr_str(sc->sc_gpio, &sc->sc_map, apin, irqmode,
195 1.1.36.1 bouyer sc->sc_intrs[apin].sc_intrstr, sizeof(sc->sc_intrs[apin].sc_intrstr))) {
196 1.1.36.1 bouyer aprint_error_dev(self, "failed to decode interrupt\n");
197 1.1.36.1 bouyer return;
198 1.1.36.1 bouyer }
199 1.1.36.1 bouyer
200 1.1.36.1 bouyer if (!gpio_pin_irqmode_issupported(sc->sc_gpio, &sc->sc_map, apin,
201 1.1.36.1 bouyer irqmode)) {
202 1.1.36.1 bouyer aprint_error_dev(self,
203 1.1.36.1 bouyer "irqmode not supported: %s\n", sc->sc_intrs[apin].sc_intrstr);
204 1.1.36.1 bouyer gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
205 1.1.36.1 bouyer return;
206 1.1.36.1 bouyer }
207 1.1.36.1 bouyer
208 1.1.36.1 bouyer flags = gpio_pin_get_conf(sc->sc_gpio, &sc->sc_map, apin);
209 1.1.36.1 bouyer flags = (flags & ~(GPIO_PIN_OUTPUT|GPIO_PIN_INOUT)) |
210 1.1.36.1 bouyer GPIO_PIN_INPUT;
211 1.1.36.1 bouyer if (!gpio_pin_set_conf(sc->sc_gpio, &sc->sc_map, apin, flags)) {
212 1.1.36.1 bouyer aprint_error_dev(sc->sc_dev, "pin not capable of input\n");
213 1.1.36.1 bouyer gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
214 1.1.36.1 bouyer return;
215 1.1.36.1 bouyer }
216 1.1.36.1 bouyer
217 1.1.36.1 bouyer /* These are static for each pin, so just stuff them in here,
218 1.1.36.1 bouyer * so they don't need to be looked up again.
219 1.1.36.1 bouyer */
220 1.1.36.1 bouyer sc->sc_intrs[apin].i_thispin_index = apin;
221 1.1.36.1 bouyer sc->sc_intrs[apin].i_thispin_num = gpioirq_index_to_pin_num(sc,apin);
222 1.1.36.1 bouyer sc->sc_intrs[apin].i_parentunit = gpioirq_parent_unit(sc);
223 1.1.36.1 bouyer sc->sc_intrs[apin].sc = sc;
224 1.1.36.1 bouyer
225 1.1.36.1 bouyer sc->sc_intrs[apin].sc_ih = gpio_intr_establish(sc->sc_gpio, &sc->sc_map, apin, IPL_VM,
226 1.1.36.1 bouyer irqmode | GPIO_INTR_MPSAFE,
227 1.1.36.1 bouyer gpioirq_intr, &sc->sc_intrs[apin]);
228 1.1.36.1 bouyer if (sc->sc_intrs[apin].sc_ih == NULL) {
229 1.1.36.1 bouyer aprint_error_dev(self,
230 1.1.36.1 bouyer "unable to establish interrupt on %s\n", sc->sc_intrs[apin].sc_intrstr);
231 1.1.36.1 bouyer gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
232 1.1.36.1 bouyer return;
233 1.1.36.1 bouyer }
234 1.1.36.1 bouyer aprint_normal_dev(self, "interrupting on %s\n", sc->sc_intrs[apin].sc_intrstr);
235 1.1 thorpej }
236 1.1 thorpej
237 1.1 thorpej sc->sc_functional = true;
238 1.1 thorpej }
239 1.1 thorpej
240 1.1 thorpej int
241 1.1 thorpej gpioirq_intr(void *arg)
242 1.1 thorpej {
243 1.1.36.1 bouyer struct gpioirq_iv *is = arg;
244 1.1.36.1 bouyer struct gpioirq_softc *sc = is->sc;
245 1.1.36.1 bouyer struct gpioirq_read_q *q;
246 1.1 thorpej int val;
247 1.1 thorpej
248 1.1 thorpej mutex_enter(&sc->sc_lock);
249 1.1 thorpej
250 1.1.36.1 bouyer val = gpio_pin_read(sc->sc_gpio, &sc->sc_map, is->i_thispin_index);
251 1.1 thorpej
252 1.1 thorpej if (sc->sc_verbose)
253 1.1 thorpej printf("%s: interrupt on %s --> %d\n",
254 1.1.36.1 bouyer device_xname(sc->sc_dev), sc->sc_intrs[is->i_thispin_index].sc_intrstr, val);
255 1.1 thorpej
256 1.1 thorpej mutex_exit(&sc->sc_lock);
257 1.1 thorpej
258 1.1.36.1 bouyer if (sc->sc_opened) {
259 1.1.36.1 bouyer mutex_enter(&sc->sc_read_mutex);
260 1.1.36.1 bouyer q = pool_cache_get(sc->sc_readpool,PR_NOWAIT);
261 1.1.36.1 bouyer if (q != NULL) {
262 1.1.36.1 bouyer q->thepin = is->i_thispin_num;
263 1.1.36.1 bouyer q->parentunit = is->i_parentunit;
264 1.1.36.1 bouyer q->theval = val;
265 1.1.36.1 bouyer SIMPLEQ_INSERT_TAIL(&sc->sc_read_queue,q,read_q);
266 1.1.36.1 bouyer cv_signal(&sc->sc_condreadready);
267 1.1.36.1 bouyer } else {
268 1.1.36.1 bouyer aprint_error("Could not allocate memory for read pool\n");
269 1.1.36.1 bouyer }
270 1.1.36.1 bouyer mutex_exit(&sc->sc_read_mutex);
271 1.1.36.1 bouyer }
272 1.1.36.1 bouyer
273 1.1 thorpej return (1);
274 1.1 thorpej }
275 1.1 thorpej
276 1.1.36.1 bouyer static int
277 1.1.36.1 bouyer gpioirq_open(dev_t dev, int flags, int fmt, struct lwp *l)
278 1.1.36.1 bouyer {
279 1.1.36.1 bouyer struct gpioirq_softc *sc;
280 1.1.36.1 bouyer
281 1.1.36.1 bouyer sc = device_lookup_private(&gpioirq_cd, minor(dev));
282 1.1.36.1 bouyer if (!sc)
283 1.1.36.1 bouyer return (ENXIO);
284 1.1.36.1 bouyer
285 1.1.36.1 bouyer if (sc->sc_opened)
286 1.1.36.1 bouyer return (EBUSY);
287 1.1.36.1 bouyer
288 1.1.36.1 bouyer mutex_enter(&sc->sc_lock);
289 1.1.36.1 bouyer sc->sc_opened = true;
290 1.1.36.1 bouyer mutex_exit(&sc->sc_lock);
291 1.1.36.1 bouyer
292 1.1.36.1 bouyer return (0);
293 1.1.36.1 bouyer }
294 1.1.36.1 bouyer
295 1.1.36.1 bouyer static int
296 1.1.36.1 bouyer gpioirq_read(dev_t dev, struct uio *uio, int flags)
297 1.1.36.1 bouyer {
298 1.1.36.1 bouyer struct gpioirq_softc *sc;
299 1.1.36.1 bouyer struct gpioirq_read_q *chp;
300 1.1.36.1 bouyer int error = 0,any;
301 1.1.36.1 bouyer uint8_t obuf[3];
302 1.1.36.1 bouyer
303 1.1.36.1 bouyer sc = device_lookup_private(&gpioirq_cd, minor(dev));
304 1.1.36.1 bouyer if (!sc)
305 1.1.36.1 bouyer return (ENXIO);
306 1.1.36.1 bouyer
307 1.1.36.1 bouyer while (uio->uio_resid > 0) {
308 1.1.36.1 bouyer any = 0;
309 1.1.36.1 bouyer error = 0;
310 1.1.36.1 bouyer mutex_enter(&sc->sc_read_mutex);
311 1.1.36.1 bouyer
312 1.1.36.1 bouyer while (any == 0) {
313 1.1.36.1 bouyer chp = SIMPLEQ_FIRST(&sc->sc_read_queue);
314 1.1.36.1 bouyer if (chp != NULL) {
315 1.1.36.1 bouyer SIMPLEQ_REMOVE_HEAD(&sc->sc_read_queue, read_q);
316 1.1.36.1 bouyer any = 1;
317 1.1.36.1 bouyer break;
318 1.1.36.1 bouyer } else {
319 1.1.36.1 bouyer error = cv_wait_sig(&sc->sc_condreadready,&sc->sc_read_mutex);
320 1.1.36.1 bouyer if (sc->sc_dying)
321 1.1.36.1 bouyer error = EIO;
322 1.1.36.1 bouyer if (error == 0)
323 1.1.36.1 bouyer continue;
324 1.1.36.1 bouyer break;
325 1.1.36.1 bouyer }
326 1.1.36.1 bouyer }
327 1.1.36.1 bouyer
328 1.1.36.1 bouyer if (any == 1 && error == 0) {
329 1.1.36.1 bouyer obuf[0] = (uint8_t)chp->parentunit;
330 1.1.36.1 bouyer obuf[1] = (uint8_t)chp->thepin;
331 1.1.36.1 bouyer obuf[2] = (uint8_t)chp->theval;
332 1.1.36.1 bouyer pool_cache_put(sc->sc_readpool,chp);
333 1.1.36.1 bouyer mutex_exit(&sc->sc_read_mutex);
334 1.1.36.1 bouyer if ((error = uiomove(&obuf[0], 3, uio)) != 0) {
335 1.1.36.1 bouyer break;
336 1.1.36.1 bouyer }
337 1.1.36.1 bouyer } else {
338 1.1.36.1 bouyer mutex_exit(&sc->sc_read_mutex);
339 1.1.36.1 bouyer if (error) {
340 1.1.36.1 bouyer break;
341 1.1.36.1 bouyer }
342 1.1.36.1 bouyer }
343 1.1.36.1 bouyer }
344 1.1.36.1 bouyer
345 1.1.36.1 bouyer if (sc->sc_dying) {
346 1.1.36.1 bouyer mutex_enter(&sc->sc_dying_mutex);
347 1.1.36.1 bouyer cv_signal(&sc->sc_cond_dying);
348 1.1.36.1 bouyer mutex_exit(&sc->sc_dying_mutex);
349 1.1.36.1 bouyer }
350 1.1.36.1 bouyer return error;
351 1.1.36.1 bouyer }
352 1.1.36.1 bouyer
353 1.1.36.1 bouyer static int
354 1.1.36.1 bouyer gpioirq_close(dev_t dev, int flags, int fmt, struct lwp *l)
355 1.1.36.1 bouyer {
356 1.1.36.1 bouyer struct gpioirq_softc *sc;
357 1.1.36.1 bouyer struct gpioirq_read_q *q;
358 1.1.36.1 bouyer
359 1.1.36.1 bouyer sc = device_lookup_private(&gpioirq_cd, minor(dev));
360 1.1.36.1 bouyer
361 1.1.36.1 bouyer mutex_enter(&sc->sc_lock);
362 1.1.36.1 bouyer while ((q = SIMPLEQ_FIRST(&sc->sc_read_queue)) != NULL) {
363 1.1.36.1 bouyer SIMPLEQ_REMOVE_HEAD(&sc->sc_read_queue, read_q);
364 1.1.36.1 bouyer pool_cache_put(sc->sc_readpool,q);
365 1.1.36.1 bouyer }
366 1.1.36.1 bouyer sc->sc_opened = false;
367 1.1.36.1 bouyer mutex_exit(&sc->sc_lock);
368 1.1.36.1 bouyer
369 1.1.36.1 bouyer return(0);
370 1.1.36.1 bouyer }
371 1.1.36.1 bouyer
372 1.1 thorpej int
373 1.1 thorpej gpioirq_detach(device_t self, int flags)
374 1.1 thorpej {
375 1.1 thorpej struct gpioirq_softc *sc = device_private(self);
376 1.1.36.1 bouyer struct gpioirq_read_q *q;
377 1.1 thorpej
378 1.1 thorpej /* Clear the handler and disable the interrupt. */
379 1.1.36.1 bouyer for(int apin = 0;apin < sc->sc_npins;apin++) {
380 1.1.36.1 bouyer gpio_intr_disestablish(sc->sc_gpio, sc->sc_intrs[apin].sc_ih);
381 1.1.36.1 bouyer }
382 1.1 thorpej
383 1.1 thorpej /* Release the pin. */
384 1.1 thorpej gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
385 1.1 thorpej
386 1.1.36.1 bouyer sc->sc_dying = true;
387 1.1.36.1 bouyer
388 1.1.36.1 bouyer if (sc->sc_opened) {
389 1.1.36.1 bouyer mutex_enter(&sc->sc_dying_mutex);
390 1.1.36.1 bouyer mutex_enter(&sc->sc_read_mutex);
391 1.1.36.1 bouyer cv_signal(&sc->sc_condreadready);
392 1.1.36.1 bouyer mutex_exit(&sc->sc_read_mutex);
393 1.1.36.1 bouyer /* In the worst case this will time out after 5 seconds.
394 1.1.36.1 bouyer * It really should not take that long for the drain / whatever
395 1.1.36.1 bouyer * to happen
396 1.1.36.1 bouyer */
397 1.1.36.1 bouyer cv_timedwait_sig(&sc->sc_cond_dying,
398 1.1.36.1 bouyer &sc->sc_dying_mutex, mstohz(5000));
399 1.1.36.1 bouyer mutex_exit(&sc->sc_dying_mutex);
400 1.1.36.1 bouyer cv_destroy(&sc->sc_condreadready);
401 1.1.36.1 bouyer cv_destroy(&sc->sc_cond_dying);
402 1.1.36.1 bouyer }
403 1.1.36.1 bouyer
404 1.1.36.1 bouyer /* Drain any read pools */
405 1.1.36.1 bouyer while ((q = SIMPLEQ_FIRST(&sc->sc_read_queue)) != NULL) {
406 1.1.36.1 bouyer SIMPLEQ_REMOVE_HEAD(&sc->sc_read_queue, read_q);
407 1.1.36.1 bouyer pool_cache_put(sc->sc_readpool,q);
408 1.1.36.1 bouyer }
409 1.1.36.1 bouyer
410 1.1.36.1 bouyer if (sc->sc_readpoolname != NULL) {
411 1.1.36.1 bouyer kmem_free(sc->sc_readpoolname,strlen(sc->sc_readpoolname) + 1);
412 1.1.36.1 bouyer }
413 1.1.36.1 bouyer
414 1.1.36.1 bouyer mutex_destroy(&sc->sc_read_mutex);
415 1.1.36.1 bouyer mutex_destroy(&sc->sc_lock);
416 1.1.36.1 bouyer
417 1.1 thorpej return (0);
418 1.1 thorpej }
419 1.1 thorpej
420 1.1 thorpej int
421 1.1 thorpej gpioirq_activate(device_t self, enum devact act)
422 1.1 thorpej {
423 1.1 thorpej
424 1.1.36.1 bouyer struct gpioirq_softc *sc = device_private(self);
425 1.1.36.1 bouyer
426 1.1 thorpej switch (act) {
427 1.1 thorpej case DVACT_DEACTIVATE:
428 1.1.36.1 bouyer sc->sc_dying = true;
429 1.1 thorpej return (0);
430 1.1 thorpej default:
431 1.1 thorpej return (EOPNOTSUPP);
432 1.1 thorpej }
433 1.1 thorpej }
434 1.1 thorpej
435 1.1 thorpej MODULE(MODULE_CLASS_DRIVER, gpioirq, "gpio");
436 1.1 thorpej
437 1.1 thorpej #ifdef _MODULE
438 1.1 thorpej #include "ioconf.c"
439 1.1 thorpej #endif
440 1.1 thorpej
441 1.1 thorpej static int
442 1.1 thorpej gpioirq_modcmd(modcmd_t cmd, void *opaque)
443 1.1 thorpej {
444 1.1 thorpej int error = 0;
445 1.1.36.1 bouyer #ifdef _MODULE
446 1.1.36.1 bouyer int bmaj = -1, cmaj = -1;
447 1.1.36.1 bouyer #endif
448 1.1 thorpej
449 1.1 thorpej switch (cmd) {
450 1.1 thorpej case MODULE_CMD_INIT:
451 1.1 thorpej #ifdef _MODULE
452 1.1 thorpej error = config_init_component(cfdriver_ioconf_gpioirq,
453 1.1 thorpej cfattach_ioconf_gpioirq, cfdata_ioconf_gpioirq);
454 1.1.36.1 bouyer if (error) {
455 1.1 thorpej aprint_error("%s: unable to init component\n",
456 1.1 thorpej gpioirq_cd.cd_name);
457 1.1.36.1 bouyer return (error);
458 1.1.36.1 bouyer }
459 1.1.36.1 bouyer
460 1.1.36.1 bouyer error = devsw_attach("gpioirq", NULL, &bmaj,
461 1.1.36.1 bouyer &gpioirq_cdevsw, &cmaj);
462 1.1.36.1 bouyer if (error) {
463 1.1.36.1 bouyer aprint_error("%s: unable to attach devsw\n",
464 1.1.36.1 bouyer gpioirq_cd.cd_name);
465 1.1.36.1 bouyer config_fini_component(cfdriver_ioconf_gpioirq,
466 1.1.36.1 bouyer cfattach_ioconf_gpioirq, cfdata_ioconf_gpioirq);
467 1.1.36.1 bouyer }
468 1.1 thorpej #endif
469 1.1.36.1 bouyer return (error);
470 1.1 thorpej case MODULE_CMD_FINI:
471 1.1 thorpej #ifdef _MODULE
472 1.1.36.1 bouyer devsw_detach(NULL, &gpioirq_cdevsw);
473 1.1 thorpej config_fini_component(cfdriver_ioconf_gpioirq,
474 1.1 thorpej cfattach_ioconf_gpioirq, cfdata_ioconf_gpioirq);
475 1.1 thorpej #endif
476 1.1.36.1 bouyer return (0);
477 1.1 thorpej default:
478 1.1.36.1 bouyer return (ENOTTY);
479 1.1 thorpej }
480 1.1 thorpej }
481