gpio.c revision 1.42 1 /* $NetBSD: gpio.c,v 1.42 2011/10/02 09:33:19 mbalmer Exp $ */
2 /* $OpenBSD: gpio.c,v 1.6 2006/01/14 12:33:49 grange Exp $ */
3
4 /*
5 * Copyright (c) 2008, 2009, 2010, 2011 Marc Balmer <marc (at) msys.ch>
6 * Copyright (c) 2004, 2006 Alexander Yurchenko <grange (at) openbsd.org>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
20
21 #include <sys/cdefs.h>
22 __KERNEL_RCSID(0, "$NetBSD: gpio.c,v 1.42 2011/10/02 09:33:19 mbalmer Exp $");
23
24 /*
25 * General Purpose Input/Output framework.
26 */
27
28 #include <sys/param.h>
29 #include <sys/callout.h>
30 #include <sys/systm.h>
31 #include <sys/conf.h>
32 #include <sys/device.h>
33 #include <sys/fcntl.h>
34 #include <sys/ioctl.h>
35 #include <sys/gpio.h>
36 #include <sys/kernel.h>
37 #include <sys/vnode.h>
38 #include <sys/kmem.h>
39 #include <sys/mutex.h>
40 #include <sys/condvar.h>
41 #include <sys/queue.h>
42 #include <sys/kauth.h>
43 #include <sys/module.h>
44 #include <dev/gpio/gpiovar.h>
45
46 #include "locators.h"
47
48 #ifdef GPIO_DEBUG
49 #define DPRINTFN(n, x) do { if (gpiodebug > (n)) printf x; } while (0)
50 int gpiodebug = 0;
51 #else
52 #define DPRINTFN(n, x)
53 #endif
54 #define DPRINTF(x) DPRINTFN(0, x)
55
56 struct gpio_softc {
57 device_t sc_dev;
58
59 gpio_chipset_tag_t sc_gc; /* GPIO controller */
60 gpio_pin_t *sc_pins; /* pins array */
61 int sc_npins; /* number of pins */
62
63 kmutex_t sc_mtx;
64 kcondvar_t sc_ioctl; /* ioctl in progress */
65 int sc_ioctl_busy; /* ioctl is busy */
66 kcondvar_t sc_attach; /* attach/detach in progress */
67 int sc_attach_busy;/* busy in attach/detach */
68 LIST_HEAD(, gpio_dev) sc_devs; /* devices */
69 LIST_HEAD(, gpio_name) sc_names; /* named pins */
70 };
71
72 static int gpio_match(device_t, cfdata_t, void *);
73 int gpio_submatch(device_t, cfdata_t, const int *, void *);
74 static void gpio_attach(device_t, device_t, void *);
75 static int gpio_rescan(device_t, const char *, const int *);
76 static void gpio_childdetached(device_t, device_t);
77 static bool gpio_resume(device_t, const pmf_qual_t *);
78 static int gpio_detach(device_t, int);
79 static int gpio_search(device_t, cfdata_t, const int *, void *);
80 static int gpio_print(void *, const char *);
81 static int gpio_pinbyname(struct gpio_softc *, char *);
82 static void gpio_pulse(void *);
83 static int gpio_ioctl(struct gpio_softc *, u_long, void *, int,
84 struct lwp *);
85
86 #ifdef COMPAT_50
87 /* Old API */
88 static int gpio_ioctl_oapi(struct gpio_softc *, u_long, void *, int,
89 kauth_cred_t);
90 #endif
91
92 CFATTACH_DECL3_NEW(gpio, sizeof(struct gpio_softc),
93 gpio_match, gpio_attach, gpio_detach, NULL, gpio_rescan,
94 gpio_childdetached, DVF_DETACH_SHUTDOWN);
95
96 dev_type_open(gpioopen);
97 dev_type_close(gpioclose);
98 dev_type_ioctl(gpioioctl);
99 dev_type_ioctl(gpioioctl_locked);
100
101 const struct cdevsw gpio_cdevsw = {
102 gpioopen, gpioclose, noread, nowrite, gpioioctl,
103 nostop, notty, nopoll, nommap, nokqfilter, D_OTHER | D_MPSAFE
104 };
105
106 extern struct cfdriver gpio_cd;
107
108 static int
109 gpio_match(device_t parent, cfdata_t cf, void *aux)
110 {
111 return 1;
112 }
113
114 int
115 gpio_submatch(device_t parent, cfdata_t cf, const int *ip, void *aux)
116 {
117 struct gpio_attach_args *ga = aux;
118
119 if (ga->ga_offset == -1)
120 return 0;
121
122 return strcmp(ga->ga_dvname, cf->cf_name) == 0;
123 }
124
125 static bool
126 gpio_resume(device_t self, const pmf_qual_t *qual)
127 {
128 struct gpio_softc *sc = device_private(self);
129 int pin;
130
131 for (pin = 0; pin < sc->sc_npins; pin++) {
132 gpiobus_pin_ctl(sc->sc_gc, pin, sc->sc_pins[pin].pin_flags);
133 gpiobus_pin_write(sc->sc_gc, pin, sc->sc_pins[pin].pin_state);
134 }
135 return true;
136 }
137
138 static void
139 gpio_childdetached(device_t self, device_t child)
140 {
141 struct gpio_dev *gdev;
142 struct gpio_softc *sc;
143 int error;
144
145 /*
146 * gpio_childetached is serialized because it can be entered in
147 * different ways concurrently, e.g. via the GPIODETACH ioctl and
148 * drvctl(8) or modunload(8).
149 */
150 sc = device_private(self);
151 error = 0;
152 mutex_enter(&sc->sc_mtx);
153 while (sc->sc_attach_busy) {
154 error = cv_wait_sig(&sc->sc_attach, &sc->sc_mtx);
155 if (error)
156 break;
157 }
158 if (!error)
159 sc->sc_attach_busy = 1;
160 mutex_exit(&sc->sc_mtx);
161 if (error)
162 return;
163
164 LIST_FOREACH(gdev, &sc->sc_devs, sc_next)
165 if (gdev->sc_dev == child) {
166 LIST_REMOVE(gdev, sc_next);
167 kmem_free(gdev, sizeof(struct gpio_dev));
168 break;
169 }
170
171 mutex_enter(&sc->sc_mtx);
172 sc->sc_attach_busy = 0;
173 cv_signal(&sc->sc_attach);
174 mutex_exit(&sc->sc_mtx);
175 }
176
177 static int
178 gpio_rescan(device_t self, const char *ifattr, const int *locators)
179 {
180 struct gpio_softc *sc = device_private(self);
181
182 config_search_loc(gpio_search, self, ifattr, locators, sc);
183
184 return 0;
185 }
186
187 static void
188 gpio_attach(device_t parent, device_t self, void *aux)
189 {
190 struct gpio_softc *sc = device_private(self);
191 struct gpiobus_attach_args *gba = aux;
192 int pin;
193 sc->sc_dev = self;
194 sc->sc_gc = gba->gba_gc;
195 sc->sc_pins = gba->gba_pins;
196 sc->sc_npins = gba->gba_npins;
197
198 printf(": %d pins\n", sc->sc_npins);
199
200 for (pin = 0; pin < sc->sc_npins; pin++) {
201 callout_init(&sc->sc_pins[pin].pin_pulse, CALLOUT_MPSAFE);
202 callout_setfunc(&sc->sc_pins[pin].pin_pulse, gpio_pulse,
203 &sc->sc_pins[pin]);
204 }
205 if (!pmf_device_register(self, NULL, gpio_resume))
206 aprint_error_dev(self, "couldn't establish power handler\n");
207 mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_VM);
208 cv_init(&sc->sc_ioctl, "gpioctl");
209 cv_init(&sc->sc_attach, "gpioatch");
210 /*
211 * Attach all devices that can be connected to the GPIO pins
212 * described in the kernel configuration file.
213 */
214 gpio_rescan(self, "gpio", NULL);
215 }
216
217 static int
218 gpio_detach(device_t self, int flags)
219 {
220 struct gpio_softc *sc;
221 int pin, rc;
222
223 sc = device_private(self);
224
225 for (pin = 0; pin < sc->sc_npins; pin++) {
226 if (sc->sc_pins[pin].pin_state & GPIO_PIN_PULSE) {
227 callout_halt(&sc->sc_pins[pin].pin_pulse, NULL);
228 callout_destroy(&sc->sc_pins[pin].pin_pulse);
229 sc->sc_pins[pin].pin_state &= ~GPIO_PIN_PULSE;
230 }
231 }
232
233 if ((rc = config_detach_children(self, flags)) != 0)
234 return rc;
235 mutex_destroy(&sc->sc_mtx);
236 cv_destroy(&sc->sc_ioctl);
237 #if 0
238 int maj, mn;
239
240 /* Locate the major number */
241 for (maj = 0; maj < nchrdev; maj++)
242 if (cdevsw[maj].d_open == gpioopen)
243 break;
244
245 /* Nuke the vnodes for any open instances (calls close) */
246 mn = device_unit(self);
247 vdevgone(maj, mn, mn, VCHR);
248 #endif
249 return 0;
250 }
251
252 static int
253 gpio_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
254 {
255 struct gpio_attach_args ga;
256
257 ga.ga_gpio = aux;
258 ga.ga_offset = cf->cf_loc[GPIOCF_OFFSET];
259 ga.ga_mask = cf->cf_loc[GPIOCF_MASK];
260 ga.ga_flags = cf->cf_loc[GPIOCF_FLAG];
261
262 if (config_match(parent, cf, &ga) > 0)
263 config_attach(parent, cf, &ga, gpio_print);
264
265 return 0;
266 }
267
268 int
269 gpio_print(void *aux, const char *pnp)
270 {
271 struct gpio_attach_args *ga = aux;
272 int i;
273
274 printf(" pins");
275 for (i = 0; i < 32; i++)
276 if (ga->ga_mask & (1 << i))
277 printf(" %d", ga->ga_offset + i);
278
279 return UNCONF;
280 }
281
282 int
283 gpiobus_print(void *aux, const char *pnp)
284 {
285 #if 0
286 struct gpiobus_attach_args *gba = aux;
287 #endif
288 if (pnp != NULL)
289 aprint_normal("gpiobus at %s", pnp);
290
291 return UNCONF;
292 }
293
294 /* return 1 if all pins can be mapped, 0 if not */
295 int
296 gpio_pin_can_map(void *gpio, int offset, uint32_t mask)
297 {
298 struct gpio_softc *sc = gpio;
299 int npins, pin, i;
300
301 npins = gpio_npins(mask);
302 if (npins > sc->sc_npins)
303 return 0;
304
305 for (npins = 0, i = 0; i < 32; i++)
306 if (mask & (1 << i)) {
307 pin = offset + i;
308 if (pin < 0 || pin >= sc->sc_npins)
309 return 0;
310 if (sc->sc_pins[pin].pin_mapped)
311 return 0;
312 }
313
314 return 1;
315 }
316
317 int
318 gpio_pin_map(void *gpio, int offset, uint32_t mask, struct gpio_pinmap *map)
319 {
320 struct gpio_softc *sc = gpio;
321 int npins, pin, i;
322
323 npins = gpio_npins(mask);
324 if (npins > sc->sc_npins)
325 return 1;
326
327 for (npins = 0, i = 0; i < 32; i++)
328 if (mask & (1 << i)) {
329 pin = offset + i;
330 if (pin < 0 || pin >= sc->sc_npins)
331 return 1;
332 if (sc->sc_pins[pin].pin_mapped)
333 return 1;
334 sc->sc_pins[pin].pin_mapped = 1;
335 map->pm_map[npins++] = pin;
336 }
337 map->pm_size = npins;
338
339 return 0;
340 }
341
342 void
343 gpio_pin_unmap(void *gpio, struct gpio_pinmap *map)
344 {
345 struct gpio_softc *sc = gpio;
346 int pin, i;
347
348 for (i = 0; i < map->pm_size; i++) {
349 pin = map->pm_map[i];
350 sc->sc_pins[pin].pin_mapped = 0;
351 }
352 }
353
354 int
355 gpio_pin_read(void *gpio, struct gpio_pinmap *map, int pin)
356 {
357 struct gpio_softc *sc = gpio;
358
359 return gpiobus_pin_read(sc->sc_gc, map->pm_map[pin]);
360 }
361
362 void
363 gpio_pin_write(void *gpio, struct gpio_pinmap *map, int pin, int value)
364 {
365 struct gpio_softc *sc = gpio;
366
367 gpiobus_pin_write(sc->sc_gc, map->pm_map[pin], value);
368 sc->sc_pins[map->pm_map[pin]].pin_state = value;
369 }
370
371 void
372 gpio_pin_ctl(void *gpio, struct gpio_pinmap *map, int pin, int flags)
373 {
374 struct gpio_softc *sc = gpio;
375
376 return gpiobus_pin_ctl(sc->sc_gc, map->pm_map[pin], flags);
377 }
378
379 int
380 gpio_pin_caps(void *gpio, struct gpio_pinmap *map, int pin)
381 {
382 struct gpio_softc *sc = gpio;
383
384 return sc->sc_pins[map->pm_map[pin]].pin_caps;
385 }
386
387 int
388 gpio_npins(uint32_t mask)
389 {
390 int npins, i;
391
392 for (npins = 0, i = 0; i < 32; i++)
393 if (mask & (1 << i))
394 npins++;
395
396 return npins;
397 }
398
399 int
400 gpio_lock(void *data)
401 {
402 struct gpio_softc *sc;
403 int error;
404
405 error = 0;
406 sc = (struct gpio_softc *)data;
407 mutex_enter(&sc->sc_mtx);
408 while (sc->sc_ioctl_busy) {
409 error = cv_wait_sig(&sc->sc_ioctl, &sc->sc_mtx);
410 if (error)
411 break;
412 }
413 if (!error)
414 sc->sc_ioctl_busy = 1;
415 mutex_exit(&sc->sc_mtx);
416 return error;
417 }
418
419 void
420 gpio_unlock(void *data)
421 {
422 struct gpio_softc *sc;
423
424 sc = (struct gpio_softc *)data;
425 mutex_enter(&sc->sc_mtx);
426 sc->sc_ioctl_busy = 0;
427 cv_signal(&sc->sc_ioctl);
428 mutex_exit(&sc->sc_mtx);
429 }
430
431 int
432 gpioopen(dev_t dev, int flag, int mode, struct lwp *l)
433 {
434 struct gpio_softc *sc;
435
436 sc = device_lookup_private(&gpio_cd, minor(dev));
437 if (sc == NULL)
438 return ENXIO;
439
440 return gpiobus_open(sc->sc_gc, sc->sc_dev);
441 }
442
443 int
444 gpioclose(dev_t dev, int flag, int mode, struct lwp *l)
445 {
446 struct gpio_softc *sc;
447
448 sc = device_lookup_private(&gpio_cd, minor(dev));
449 gpiobus_close(sc->sc_gc, sc->sc_dev);
450 return 0;
451 }
452
453 static int
454 gpio_pinbyname(struct gpio_softc *sc, char *gp_name)
455 {
456 struct gpio_name *nm;
457
458 LIST_FOREACH(nm, &sc->sc_names, gp_next)
459 if (!strcmp(nm->gp_name, gp_name))
460 return nm->gp_pin;
461 return -1;
462 }
463
464 static void
465 gpio_pulse(void *arg)
466 {
467 struct gpio_pin *pin;
468
469 pin = (struct gpio_pin *)arg;
470 if ((pin->pin_state & GPIO_PIN_PULSE) == 0)
471 return;
472
473 if (pin->pin_state & GPIO_PIN_HIGH) {
474 gpiobus_pin_write(pin->pin_gc, pin->pin_num, GPIO_PIN_LOW);
475 pin->pin_state &= ~GPIO_PIN_HIGH;
476 callout_schedule(&pin->pin_pulse, pin->pin_ticks_off);
477 } else {
478 gpiobus_pin_write(pin->pin_gc, pin->pin_num, GPIO_PIN_HIGH);
479 pin->pin_state |= GPIO_PIN_HIGH;
480 callout_schedule(&pin->pin_pulse, pin->pin_ticks_on);
481 }
482 }
483
484 int
485 gpioioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
486 {
487 int error;
488 struct gpio_softc *sc;
489
490 sc = device_lookup_private(&gpio_cd, minor(dev));
491
492 error = gpio_lock(sc);
493 if (error)
494 return error;
495
496 error = gpio_ioctl(sc, cmd, data, flag, l);
497 gpio_unlock(sc);
498 return error;
499 }
500
501 static int
502 gpio_ioctl(struct gpio_softc *sc, u_long cmd, void *data, int flag,
503 struct lwp *l)
504 {
505 gpio_chipset_tag_t gc;
506 struct gpio_info *info;
507 struct gpio_attach *attach;
508 struct gpio_attach_args ga;
509 struct gpio_dev *gdev;
510 struct gpio_req *req;
511 struct gpio_pulse *pulse;
512 struct gpio_name *nm;
513 struct gpio_set *set;
514 struct gpio_pin *gpin;
515 device_t dv;
516 cfdata_t cf;
517 kauth_cred_t cred;
518 int locs[GPIOCF_NLOCS];
519 int error, pin, value, flags, npins;
520
521 gc = sc->sc_gc;
522 ga.ga_flags = 0;
523
524 if (cmd != GPIOINFO && !device_is_active(sc->sc_dev)) {
525 DPRINTF(("%s: device is not active\n",
526 device_xname(sc->sc_dev)));
527 return EBUSY;
528 }
529
530 cred = kauth_cred_get();
531
532 switch (cmd) {
533 case GPIOINFO:
534 info = (struct gpio_info *)data;
535 if (!kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
536 NULL, NULL, NULL, NULL))
537 info->gpio_npins = sc->sc_npins;
538 else {
539 for (pin = npins = 0; pin < sc->sc_npins; pin++)
540 if (sc->sc_pins[pin].pin_flags & GPIO_PIN_SET)
541 ++npins;
542 info->gpio_npins = npins;
543 }
544 break;
545 case GPIOREAD:
546 req = (struct gpio_req *)data;
547
548 if (req->gp_name[0] != '\0')
549 pin = gpio_pinbyname(sc, req->gp_name);
550 else
551 pin = req->gp_pin;
552
553 if (pin < 0 || pin >= sc->sc_npins)
554 return EINVAL;
555
556 if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
557 kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
558 NULL, NULL, NULL, NULL))
559 return EPERM;
560
561 /* return read value */
562 req->gp_value = gpiobus_pin_read(gc, pin);
563 break;
564 case GPIOWRITE:
565 if ((flag & FWRITE) == 0)
566 return EBADF;
567
568 req = (struct gpio_req *)data;
569
570 if (req->gp_name[0] != '\0')
571 pin = gpio_pinbyname(sc, req->gp_name);
572 else
573 pin = req->gp_pin;
574
575 if (pin < 0 || pin >= sc->sc_npins)
576 return EINVAL;
577
578 if (sc->sc_pins[pin].pin_mapped)
579 return EBUSY;
580
581 if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
582 kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
583 NULL, NULL, NULL, NULL))
584 return EPERM;
585
586 value = req->gp_value;
587 if (value != GPIO_PIN_LOW && value != GPIO_PIN_HIGH)
588 return EINVAL;
589
590 if (sc->sc_pins[pin].pin_state & GPIO_PIN_PULSE) {
591 callout_halt(&sc->sc_pins[pin].pin_pulse, NULL);
592 sc->sc_pins[pin].pin_state &= ~GPIO_PIN_PULSE;
593 }
594 gpiobus_pin_write(gc, pin, value);
595 /* return old value */
596 req->gp_value = sc->sc_pins[pin].pin_state;
597 /* update current value */
598 sc->sc_pins[pin].pin_state = value;
599 break;
600 case GPIOPULSE:
601 if ((flag & FWRITE) == 0)
602 return EBADF;
603
604 pulse = (struct gpio_pulse *)data;
605 if (pulse->gp_name[0] != '\0')
606 pin = gpio_pinbyname(sc, pulse->gp_name);
607 else
608 pin = pulse->gp_pin;
609
610 if (pin < 0 || pin >= sc->sc_npins)
611 return EINVAL;
612
613 gpin = &sc->sc_pins[pin];
614 if (gpin->pin_mapped)
615 return EBUSY;
616
617 if (!(gpin->pin_flags & GPIO_PIN_SET) &&
618 kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
619 NULL, NULL, NULL, NULL))
620 return EPERM;
621
622 if (gpin->pin_flags & GPIO_PIN_PULSATE) {
623 gpiobus_pin_write(gc, pin, GPIO_PIN_HIGH);
624 gpin->pin_state = GPIO_PIN_PULSE;
625 return 0;
626 }
627
628 if (gpin->pin_state & GPIO_PIN_PULSE)
629 callout_halt(&gpin->pin_pulse, NULL);
630
631 gpin->pin_gc = gc;
632
633 gpin->pin_ticks_on = tvtohz(&pulse->gp_pulse_on);
634 gpin->pin_ticks_off = tvtohz(&pulse->gp_pulse_off);
635 if (gpin->pin_ticks_on == 0 || gpin->pin_ticks_off == 0) {
636 gpin->pin_ticks_on = hz / 2;
637 gpin->pin_ticks_off = hz / 2;
638 }
639 gpiobus_pin_write(gc, pin, GPIO_PIN_HIGH);
640 gpin->pin_state = GPIO_PIN_HIGH | GPIO_PIN_PULSE;
641 callout_schedule(&gpin->pin_pulse, gpin->pin_ticks_on);
642 break;
643 case GPIOTOGGLE:
644 if ((flag & FWRITE) == 0)
645 return EBADF;
646
647 req = (struct gpio_req *)data;
648
649 if (req->gp_name[0] != '\0')
650 pin = gpio_pinbyname(sc, req->gp_name);
651 else
652 pin = req->gp_pin;
653
654 if (pin < 0 || pin >= sc->sc_npins)
655 return EINVAL;
656
657 if (sc->sc_pins[pin].pin_mapped)
658 return EBUSY;
659
660 if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
661 kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
662 NULL, NULL, NULL, NULL))
663 return EPERM;
664
665 value = (sc->sc_pins[pin].pin_state == GPIO_PIN_LOW ?
666 GPIO_PIN_HIGH : GPIO_PIN_LOW);
667 gpiobus_pin_write(gc, pin, value);
668 /* return old value */
669 req->gp_value = sc->sc_pins[pin].pin_state;
670 /* update current value */
671 sc->sc_pins[pin].pin_state = value;
672 break;
673 case GPIOATTACH:
674 attach = (struct gpio_attach *)data;
675 ga.ga_flags = attach->ga_flags;
676 #ifdef COMPAT_50
677 /* FALLTHROUGH */
678 case GPIOATTACH50:
679 /*
680 * The double assignment to 'attach' in case of GPIOATTACH
681 * and COMPAT_50 is on purpose. It ensures backward
682 * compatability in case we are called through the old
683 * GPIOATTACH50 ioctl(2), which had not the ga_flags field
684 * in struct gpio_attach.
685 */
686 attach = (struct gpio_attach *)data;
687 #endif
688 if (kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
689 NULL, NULL, NULL, NULL))
690 return EPERM;
691
692 /* do not try to attach if the pins are already mapped */
693 if (!gpio_pin_can_map(sc, attach->ga_offset, attach->ga_mask))
694 return EBUSY;
695
696 error = 0;
697 mutex_enter(&sc->sc_mtx);
698 while (sc->sc_attach_busy) {
699 error = cv_wait_sig(&sc->sc_attach, &sc->sc_mtx);
700 if (error)
701 break;
702 }
703 if (!error)
704 sc->sc_attach_busy = 1;
705 mutex_exit(&sc->sc_mtx);
706 if (error)
707 return EBUSY;
708
709 ga.ga_gpio = sc;
710 /* Don't access attach->ga_flags here. */
711 ga.ga_dvname = attach->ga_dvname;
712 ga.ga_offset = attach->ga_offset;
713 ga.ga_mask = attach->ga_mask;
714 DPRINTF(("%s: attach %s with offset %d, mask "
715 "0x%02x, and flags 0x%02x\n", device_xname(sc->sc_dev),
716 ga.ga_dvname, ga.ga_offset, ga.ga_mask, ga.ga_flags));
717
718 locs[GPIOCF_OFFSET] = ga.ga_offset;
719 locs[GPIOCF_MASK] = ga.ga_mask;
720 locs[GPIOCF_FLAG] = ga.ga_flags;
721
722 cf = config_search_loc(NULL, sc->sc_dev, "gpio", locs, &ga);
723 if (cf != NULL) {
724 dv = config_attach_loc(sc->sc_dev, cf, locs, &ga,
725 gpiobus_print);
726 if (dv != NULL) {
727 gdev = kmem_alloc(sizeof(struct gpio_dev),
728 KM_SLEEP);
729 gdev->sc_dev = dv;
730 LIST_INSERT_HEAD(&sc->sc_devs, gdev, sc_next);
731 } else
732 error = EINVAL;
733 } else
734 error = EINVAL;
735 mutex_enter(&sc->sc_mtx);
736 sc->sc_attach_busy = 0;
737 cv_signal(&sc->sc_attach);
738 mutex_exit(&sc->sc_mtx);
739 return error;
740 #ifdef COMPAT_50
741 case GPIODETACH50:
742 /* FALLTHOUGH */
743 #endif
744 case GPIODETACH:
745 if (kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
746 NULL, NULL, NULL, NULL))
747 return EPERM;
748
749 error = 0;
750 mutex_enter(&sc->sc_mtx);
751 while (sc->sc_attach_busy) {
752 error = cv_wait_sig(&sc->sc_attach, &sc->sc_mtx);
753 if (error)
754 break;
755 }
756 if (!error)
757 sc->sc_attach_busy = 1;
758 mutex_exit(&sc->sc_mtx);
759 if (error)
760 return EBUSY;
761
762 attach = (struct gpio_attach *)data;
763 LIST_FOREACH(gdev, &sc->sc_devs, sc_next) {
764 if (strcmp(device_xname(gdev->sc_dev),
765 attach->ga_dvname) == 0) {
766 mutex_enter(&sc->sc_mtx);
767 sc->sc_attach_busy = 0;
768 cv_signal(&sc->sc_attach);
769 mutex_exit(&sc->sc_mtx);
770
771 if (config_detach(gdev->sc_dev, 0) == 0)
772 return 0;
773 break;
774 }
775 }
776 if (gdev == NULL) {
777 mutex_enter(&sc->sc_mtx);
778 sc->sc_attach_busy = 0;
779 cv_signal(&sc->sc_attach);
780 mutex_exit(&sc->sc_mtx);
781 }
782 return EINVAL;
783 case GPIOSET:
784 if (kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
785 NULL, NULL, NULL, NULL))
786 return EPERM;
787
788 set = (struct gpio_set *)data;
789
790 if (set->gp_name[0] != '\0')
791 pin = gpio_pinbyname(sc, set->gp_name);
792 else
793 pin = set->gp_pin;
794
795 if (pin < 0 || pin >= sc->sc_npins)
796 return EINVAL;
797 flags = set->gp_flags;
798
799 /* check that the controller supports all requested flags */
800 if ((flags & sc->sc_pins[pin].pin_caps) != flags)
801 return ENODEV;
802 flags = set->gp_flags | GPIO_PIN_SET;
803
804 set->gp_caps = sc->sc_pins[pin].pin_caps;
805 /* return old value */
806 set->gp_flags = sc->sc_pins[pin].pin_flags;
807 if (flags > 0) {
808 gpiobus_pin_ctl(gc, pin, flags);
809 /* update current value */
810 sc->sc_pins[pin].pin_flags = flags;
811 }
812
813 /* rename pin or new pin? */
814 if (set->gp_name2[0] != '\0') {
815 struct gpio_name *gnm;
816
817 gnm = NULL;
818 LIST_FOREACH(nm, &sc->sc_names, gp_next) {
819 if (!strcmp(nm->gp_name, set->gp_name2) &&
820 nm->gp_pin != pin)
821 return EINVAL; /* duplicate name */
822 if (nm->gp_pin == pin)
823 gnm = nm;
824 }
825 if (gnm != NULL)
826 strlcpy(gnm->gp_name, set->gp_name2,
827 sizeof(gnm->gp_name));
828 else {
829 nm = kmem_alloc(sizeof(struct gpio_name),
830 KM_SLEEP);
831 strlcpy(nm->gp_name, set->gp_name2,
832 sizeof(nm->gp_name));
833 nm->gp_pin = set->gp_pin;
834 LIST_INSERT_HEAD(&sc->sc_names, nm, gp_next);
835 }
836 }
837 break;
838 case GPIOUNSET:
839 if (kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
840 NULL, NULL, NULL, NULL))
841 return EPERM;
842
843 set = (struct gpio_set *)data;
844 if (set->gp_name[0] != '\0')
845 pin = gpio_pinbyname(sc, set->gp_name);
846 else
847 pin = set->gp_pin;
848
849 if (pin < 0 || pin >= sc->sc_npins)
850 return EINVAL;
851 if (sc->sc_pins[pin].pin_mapped)
852 return EBUSY;
853 if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET))
854 return EINVAL;
855
856 LIST_FOREACH(nm, &sc->sc_names, gp_next) {
857 if (nm->gp_pin == pin) {
858 LIST_REMOVE(nm, gp_next);
859 kmem_free(nm, sizeof(struct gpio_name));
860 break;
861 }
862 }
863 sc->sc_pins[pin].pin_flags &= ~GPIO_PIN_SET;
864 break;
865 default:
866 #ifdef COMPAT_50
867 /* Try the old API */
868 DPRINTF(("%s: trying the old API\n", device_xname(sc->sc_dev)));
869 return gpio_ioctl_oapi(sc, cmd, data, flag, cred);
870 #else
871 return ENOTTY;
872 #endif
873 }
874 return 0;
875 }
876
877 #ifdef COMPAT_50
878 static int
879 gpio_ioctl_oapi(struct gpio_softc *sc, u_long cmd, void *data, int flag,
880 kauth_cred_t cred)
881 {
882 gpio_chipset_tag_t gc;
883 struct gpio_pin_op *op;
884 struct gpio_pin_ctl *ctl;
885 int pin, value, flags;
886
887 gc = sc->sc_gc;
888
889 switch (cmd) {
890 case GPIOPINREAD:
891 op = (struct gpio_pin_op *)data;
892
893 pin = op->gp_pin;
894
895 if (pin < 0 || pin >= sc->sc_npins)
896 return EINVAL;
897
898 if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
899 kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
900 NULL, NULL, NULL, NULL))
901 return EPERM;
902
903 /* return read value */
904 op->gp_value = gpiobus_pin_read(gc, pin);
905 break;
906 case GPIOPINWRITE:
907 if ((flag & FWRITE) == 0)
908 return EBADF;
909
910 op = (struct gpio_pin_op *)data;
911
912 pin = op->gp_pin;
913
914 if (pin < 0 || pin >= sc->sc_npins)
915 return EINVAL;
916
917 if (sc->sc_pins[pin].pin_mapped)
918 return EBUSY;
919
920 if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
921 kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
922 NULL, NULL, NULL, NULL))
923 return EPERM;
924
925 value = op->gp_value;
926 if (value != GPIO_PIN_LOW && value != GPIO_PIN_HIGH)
927 return EINVAL;
928
929 gpiobus_pin_write(gc, pin, value);
930 /* return old value */
931 op->gp_value = sc->sc_pins[pin].pin_state;
932 /* update current value */
933 sc->sc_pins[pin].pin_state = value;
934 break;
935 case GPIOPINTOGGLE:
936 if ((flag & FWRITE) == 0)
937 return EBADF;
938
939 op = (struct gpio_pin_op *)data;
940
941 pin = op->gp_pin;
942
943 if (pin < 0 || pin >= sc->sc_npins)
944 return EINVAL;
945
946 if (sc->sc_pins[pin].pin_mapped)
947 return EBUSY;
948
949 if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
950 kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
951 NULL, NULL, NULL, NULL))
952 return EPERM;
953
954 value = (sc->sc_pins[pin].pin_state == GPIO_PIN_LOW ?
955 GPIO_PIN_HIGH : GPIO_PIN_LOW);
956 gpiobus_pin_write(gc, pin, value);
957 /* return old value */
958 op->gp_value = sc->sc_pins[pin].pin_state;
959 /* update current value */
960 sc->sc_pins[pin].pin_state = value;
961 break;
962 case GPIOPINCTL:
963 ctl = (struct gpio_pin_ctl *) data;
964
965 if (kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
966 NULL, NULL, NULL, NULL))
967 return EPERM;
968
969 pin = ctl->gp_pin;
970
971 if (pin < 0 || pin >= sc->sc_npins)
972 return EINVAL;
973 if (sc->sc_pins[pin].pin_mapped)
974 return EBUSY;
975 flags = ctl->gp_flags;
976
977 /* check that the controller supports all requested flags */
978 if ((flags & sc->sc_pins[pin].pin_caps) != flags)
979 return ENODEV;
980
981 ctl->gp_caps = sc->sc_pins[pin].pin_caps;
982 /* return old value */
983 ctl->gp_flags = sc->sc_pins[pin].pin_flags;
984 if (flags > 0) {
985 gpiobus_pin_ctl(gc, pin, flags);
986 /* update current value */
987 sc->sc_pins[pin].pin_flags = flags;
988 }
989 break;
990 default:
991 return ENOTTY;
992 }
993 return 0;
994 }
995 #endif /* COMPAT_50 */
996
997 MODULE(MODULE_CLASS_DRIVER, gpio, NULL);
998
999 #ifdef _MODULE
1000 #include "ioconf.c"
1001 #endif
1002
1003 static int
1004 gpio_modcmd(modcmd_t cmd, void *opaque)
1005 {
1006 #ifdef _MODULE
1007 devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
1008 int error;
1009 #endif
1010 switch (cmd) {
1011 case MODULE_CMD_INIT:
1012 #ifdef _MODULE
1013 error = config_init_component(cfdriver_ioconf_gpio,
1014 cfattach_ioconf_gpio, cfdata_ioconf_gpio);
1015 if (error) {
1016 aprint_error("%s: unable to init component\n",
1017 gpio_cd.cd_name);
1018 return error;
1019 }
1020 error = devsw_attach(gpio_cd.cd_name, NULL, &bmajor,
1021 &gpio_cdevsw, &cmajor);
1022 if (error) {
1023 aprint_error("%s: unable to register devsw\n",
1024 gpio_cd.cd_name);
1025 return config_fini_component(cfdriver_ioconf_gpio,
1026 cfattach_ioconf_gpio, cfdata_ioconf_gpio);
1027 }
1028 #endif
1029 return 0;
1030 case MODULE_CMD_FINI:
1031 #ifdef _MODULE
1032 config_fini_component(cfdriver_ioconf_gpio,
1033 cfattach_ioconf_gpio, cfdata_ioconf_gpio);
1034 devsw_detach(NULL, &gpio_cdevsw);
1035 #endif
1036 return 0;
1037 default:
1038 return ENOTTY;
1039 }
1040 }
1041