No empty .Rs/.Re
[netbsd-mini2440.git] / sys / dev / gpio / gpio.c
blob284c62a916b889498ee3495d578d5bb9f06178b3
1 /* $NetBSD: gpio.c,v 1.29 2009/11/05 18:20:40 dyoung Exp $ */
2 /* $OpenBSD: gpio.c,v 1.6 2006/01/14 12:33:49 grange Exp $ */
4 /*
5 * Copyright (c) 2008, 2009 Marc Balmer <marc@msys.ch>
6 * Copyright (c) 2004, 2006 Alexander Yurchenko <grange@openbsd.org>
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.
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.
21 #include <sys/cdefs.h>
22 __KERNEL_RCSID(0, "$NetBSD: gpio.c,v 1.29 2009/11/05 18:20:40 dyoung Exp $");
25 * General Purpose Input/Output framework.
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/conf.h>
31 #include <sys/device.h>
32 #include <sys/fcntl.h>
33 #include <sys/ioctl.h>
34 #include <sys/gpio.h>
35 #include <sys/vnode.h>
36 #include <sys/kmem.h>
37 #include <sys/queue.h>
38 #include <sys/kauth.h>
40 #include <dev/gpio/gpiovar.h>
42 #include "locators.h"
44 #ifdef GPIO_DEBUG
45 #define DPRINTFN(n, x) do { if (gpiodebug > (n)) printf x; } while (0)
46 int gpiodebug = 0;
47 #else
48 #define DPRINTFN(n, x)
49 #endif
50 #define DPRINTF(x) DPRINTFN(0, x)
52 struct gpio_softc {
53 device_t sc_dev;
55 gpio_chipset_tag_t sc_gc; /* GPIO controller */
56 gpio_pin_t *sc_pins; /* pins array */
57 int sc_npins; /* number of pins */
59 int sc_opened;
60 LIST_HEAD(, gpio_dev) sc_devs; /* devices */
61 LIST_HEAD(, gpio_name) sc_names; /* named pins */
64 int gpio_match(device_t, cfdata_t, void *);
65 int gpio_submatch(device_t, cfdata_t, const int *, void *);
66 void gpio_attach(device_t, device_t, void *);
67 int gpio_rescan(device_t, const char *, const int *);
68 void gpio_childdetached(device_t, device_t);
69 bool gpio_resume(device_t, pmf_qual_t);
70 int gpio_detach(device_t, int);
71 int gpio_search(device_t, cfdata_t, const int *, void *);
72 int gpio_print(void *, const char *);
73 int gpio_pinbyname(struct gpio_softc *, char *);
75 /* Old API */
76 int gpio_ioctl_oapi(struct gpio_softc *, u_long, void *, int, kauth_cred_t);
78 CFATTACH_DECL3_NEW(gpio, sizeof(struct gpio_softc),
79 gpio_match, gpio_attach, gpio_detach, NULL, gpio_rescan,
80 gpio_childdetached, DVF_DETACH_SHUTDOWN);
82 dev_type_open(gpioopen);
83 dev_type_close(gpioclose);
84 dev_type_ioctl(gpioioctl);
86 const struct cdevsw gpio_cdevsw = {
87 gpioopen, gpioclose, noread, nowrite, gpioioctl,
88 nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
91 extern struct cfdriver gpio_cd;
93 int
94 gpio_match(device_t parent, cfdata_t cf, void *aux)
96 return 1;
99 int
100 gpio_submatch(device_t parent, cfdata_t cf, const int *ip, void *aux)
102 struct gpio_attach_args *ga = aux;
104 if (ga->ga_offset == -1)
105 return 0;
107 return strcmp(ga->ga_dvname, cf->cf_name) == 0;
110 bool
111 gpio_resume(device_t self, pmf_qual_t qual)
113 struct gpio_softc *sc = device_private(self);
114 int pin;
116 for (pin = 0; pin < sc->sc_npins; pin++) {
117 gpiobus_pin_ctl(sc->sc_gc, pin, sc->sc_pins[pin].pin_flags);
118 gpiobus_pin_write(sc->sc_gc, pin, sc->sc_pins[pin].pin_state);
120 return true;
123 void
124 gpio_childdetached(device_t self, device_t child)
126 /* gpio(4) keeps no references to its children, so do nothing. */
130 gpio_rescan(device_t self, const char *ifattr, const int *locators)
132 struct gpio_softc *sc = device_private(self);
134 config_search_loc(gpio_search, self, ifattr, locators, sc);
136 return 0;
139 void
140 gpio_attach(device_t parent, device_t self, void *aux)
142 struct gpio_softc *sc = device_private(self);
143 struct gpiobus_attach_args *gba = aux;
145 sc->sc_dev = self;
146 sc->sc_gc = gba->gba_gc;
147 sc->sc_pins = gba->gba_pins;
148 sc->sc_npins = gba->gba_npins;
150 printf(": %d pins\n", sc->sc_npins);
152 if (!pmf_device_register(self, NULL, gpio_resume))
153 aprint_error_dev(self, "couldn't establish power handler\n");
156 * Attach all devices that can be connected to the GPIO pins
157 * described in the kernel configuration file.
159 gpio_rescan(self, "gpio", NULL);
163 gpio_detach(device_t self, int flags)
165 int rc;
167 if ((rc = config_detach_children(self, flags)) != 0)
168 return rc;
170 #if 0
171 int maj, mn;
173 /* Locate the major number */
174 for (maj = 0; maj < nchrdev; maj++)
175 if (cdevsw[maj].d_open == gpioopen)
176 break;
178 /* Nuke the vnodes for any open instances (calls close) */
179 mn = device_unit(self);
180 vdevgone(maj, mn, mn, VCHR);
181 #endif
182 return 0;
186 gpio_search(device_t parent, cfdata_t cf,
187 const int *ldesc, void *aux)
189 struct gpio_attach_args ga;
191 ga.ga_gpio = aux;
192 ga.ga_offset = cf->cf_loc[GPIOCF_OFFSET];
193 ga.ga_mask = cf->cf_loc[GPIOCF_MASK];
195 if (config_match(parent, cf, &ga) > 0)
196 config_attach(parent, cf, &ga, gpio_print);
198 return 0;
202 gpio_print(void *aux, const char *pnp)
204 struct gpio_attach_args *ga = aux;
205 int i;
207 printf(" pins");
208 for (i = 0; i < 32; i++)
209 if (ga->ga_mask & (1 << i))
210 printf(" %d", ga->ga_offset + i);
212 return UNCONF;
216 gpiobus_print(void *aux, const char *pnp)
218 #if 0
219 struct gpiobus_attach_args *gba = aux;
220 #endif
221 if (pnp != NULL)
222 printf("gpiobus at %s", pnp);
224 return UNCONF;
227 /* return 1 if all pins can be mapped, 0 if not */
230 gpio_pin_can_map(void *gpio, int offset, u_int32_t mask)
232 struct gpio_softc *sc = gpio;
233 int npins, pin, i;
235 npins = gpio_npins(mask);
236 if (npins > sc->sc_npins)
237 return 0;
239 for (npins = 0, i = 0; i < 32; i++)
240 if (mask & (1 << i)) {
241 pin = offset + i;
242 if (pin < 0 || pin >= sc->sc_npins)
243 return 0;
244 if (sc->sc_pins[pin].pin_mapped)
245 return 0;
248 return 1;
252 gpio_pin_map(void *gpio, int offset, u_int32_t mask, struct gpio_pinmap *map)
254 struct gpio_softc *sc = gpio;
255 int npins, pin, i;
257 npins = gpio_npins(mask);
258 if (npins > sc->sc_npins)
259 return 1;
261 for (npins = 0, i = 0; i < 32; i++)
262 if (mask & (1 << i)) {
263 pin = offset + i;
264 if (pin < 0 || pin >= sc->sc_npins)
265 return 1;
266 if (sc->sc_pins[pin].pin_mapped)
267 return 1;
268 sc->sc_pins[pin].pin_mapped = 1;
269 map->pm_map[npins++] = pin;
271 map->pm_size = npins;
273 return 0;
276 void
277 gpio_pin_unmap(void *gpio, struct gpio_pinmap *map)
279 struct gpio_softc *sc = gpio;
280 int pin, i;
282 for (i = 0; i < map->pm_size; i++) {
283 pin = map->pm_map[i];
284 sc->sc_pins[pin].pin_mapped = 0;
289 gpio_pin_read(void *gpio, struct gpio_pinmap *map, int pin)
291 struct gpio_softc *sc = gpio;
293 return gpiobus_pin_read(sc->sc_gc, map->pm_map[pin]);
296 void
297 gpio_pin_write(void *gpio, struct gpio_pinmap *map, int pin, int value)
299 struct gpio_softc *sc = gpio;
301 gpiobus_pin_write(sc->sc_gc, map->pm_map[pin], value);
302 sc->sc_pins[map->pm_map[pin]].pin_state = value;
305 void
306 gpio_pin_ctl(void *gpio, struct gpio_pinmap *map, int pin, int flags)
308 struct gpio_softc *sc = gpio;
310 return gpiobus_pin_ctl(sc->sc_gc, map->pm_map[pin], flags);
314 gpio_pin_caps(void *gpio, struct gpio_pinmap *map, int pin)
316 struct gpio_softc *sc = gpio;
318 return sc->sc_pins[map->pm_map[pin]].pin_caps;
322 gpio_npins(u_int32_t mask)
324 int npins, i;
326 for (npins = 0, i = 0; i < 32; i++)
327 if (mask & (1 << i))
328 npins++;
330 return npins;
334 gpioopen(dev_t dev, int flag, int mode, struct lwp *l)
336 struct gpio_softc *sc;
337 int ret;
339 sc = device_lookup_private(&gpio_cd, minor(dev));
340 if (sc == NULL)
341 return ENXIO;
342 DPRINTF(("%s: opening\n", device_xname(sc->sc_dev)));
343 if (sc->sc_opened) {
344 DPRINTF(("%s: already opened\n", device_xname(sc->sc_dev)));
345 return EBUSY;
348 if ((ret = gpiobus_open(sc->sc_gc, sc->sc_dev))) {
349 DPRINTF(("%s: gpiobus_open returned %d\n",
350 device_xname(sc->sc_dev),
351 ret));
352 return ret;
355 sc->sc_opened = 1;
357 return 0;
361 gpioclose(dev_t dev, int flag, int mode, struct lwp *l)
363 struct gpio_softc *sc;
365 sc = device_lookup_private(&gpio_cd, minor(dev));
366 DPRINTF(("%s: closing\n", device_xname(sc->sc_dev)));
367 gpiobus_close(sc->sc_gc, sc->sc_dev);
368 sc->sc_opened = 0;
370 return 0;
374 gpio_pinbyname(struct gpio_softc *sc, char *gp_name)
376 struct gpio_name *nm;
378 LIST_FOREACH(nm, &sc->sc_names, gp_next)
379 if (!strcmp(nm->gp_name, gp_name))
380 return nm->gp_pin;
381 return -1;
385 gpioioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
387 struct gpio_softc *sc;
388 gpio_chipset_tag_t gc;
389 struct gpio_info *info;
390 struct gpio_attach *attach;
391 struct gpio_attach_args ga;
392 struct gpio_dev *gdev;
393 struct gpio_req *req;
394 struct gpio_name *nm;
395 struct gpio_set *set;
396 device_t dv;
397 cfdata_t cf;
398 kauth_cred_t cred;
399 int locs[GPIOCF_NLOCS];
400 int pin, value, flags, npins;
402 sc = device_lookup_private(&gpio_cd, minor(dev));
403 gc = sc->sc_gc;
405 if (cmd != GPIOINFO && !device_is_active(sc->sc_dev)) {
406 DPRINTF(("%s: device is not active\n",
407 device_xname(sc->sc_dev)));
408 return EBUSY;
411 cred = kauth_cred_get();
413 switch (cmd) {
414 case GPIOINFO:
415 info = (struct gpio_info *)data;
416 if (!kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
417 NULL, NULL, NULL, NULL))
418 info->gpio_npins = sc->sc_npins;
419 else {
420 for (pin = npins = 0; pin < sc->sc_npins; pin++)
421 if (sc->sc_pins[pin].pin_flags & GPIO_PIN_SET)
422 ++npins;
423 info->gpio_npins = npins;
425 break;
426 case GPIOREAD:
427 req = (struct gpio_req *)data;
429 if (req->gp_name[0] != '\0') {
430 pin = gpio_pinbyname(sc, req->gp_name);
431 if (pin == -1)
432 return EINVAL;
433 } else
434 pin = req->gp_pin;
436 if (pin < 0 || pin >= sc->sc_npins)
437 return EINVAL;
439 if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
440 kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
441 NULL, NULL, NULL, NULL))
442 return EPERM;
444 /* return read value */
445 req->gp_value = gpiobus_pin_read(gc, pin);
446 break;
447 case GPIOWRITE:
448 if ((flag & FWRITE) == 0)
449 return EBADF;
451 req = (struct gpio_req *)data;
453 if (req->gp_name[0] != '\0') {
454 pin = gpio_pinbyname(sc, req->gp_name);
455 if (pin == -1)
456 return EINVAL;
457 } else
458 pin = req->gp_pin;
460 if (pin < 0 || pin >= sc->sc_npins)
461 return EINVAL;
463 if (sc->sc_pins[pin].pin_mapped)
464 return EBUSY;
466 if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
467 kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
468 NULL, NULL, NULL, NULL))
469 return EPERM;
471 value = req->gp_value;
472 if (value != GPIO_PIN_LOW && value != GPIO_PIN_HIGH)
473 return EINVAL;
475 gpiobus_pin_write(gc, pin, value);
476 /* return old value */
477 req->gp_value = sc->sc_pins[pin].pin_state;
478 /* update current value */
479 sc->sc_pins[pin].pin_state = value;
480 break;
481 case GPIOTOGGLE:
482 if ((flag & FWRITE) == 0)
483 return EBADF;
485 req = (struct gpio_req *)data;
487 if (req->gp_name[0] != '\0') {
488 pin = gpio_pinbyname(sc, req->gp_name);
489 if (pin == -1)
490 return EINVAL;
491 } else
492 pin = req->gp_pin;
494 if (pin < 0 || pin >= sc->sc_npins)
495 return EINVAL;
497 if (sc->sc_pins[pin].pin_mapped)
498 return EBUSY;
500 if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
501 kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
502 NULL, NULL, NULL, NULL))
503 return EPERM;
505 value = (sc->sc_pins[pin].pin_state == GPIO_PIN_LOW ?
506 GPIO_PIN_HIGH : GPIO_PIN_LOW);
507 gpiobus_pin_write(gc, pin, value);
508 /* return old value */
509 req->gp_value = sc->sc_pins[pin].pin_state;
510 /* update current value */
511 sc->sc_pins[pin].pin_state = value;
512 break;
513 case GPIOATTACH:
514 if (kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
515 NULL, NULL, NULL, NULL))
516 return EPERM;
518 attach = (struct gpio_attach *)data;
520 /* do not try to attach if the pins are already mapped */
521 if (!gpio_pin_can_map(sc, attach->ga_offset, attach->ga_mask))
522 return EBUSY;
524 ga.ga_gpio = sc;
525 ga.ga_dvname = attach->ga_dvname;
526 ga.ga_offset = attach->ga_offset;
527 ga.ga_mask = attach->ga_mask;
528 DPRINTF(("%s: attach %s with offset %d and mask 0x%02x\n",
529 device_xname(sc->sc_dev), ga.ga_dvname, ga.ga_offset,
530 ga.ga_mask));
532 locs[GPIOCF_OFFSET] = ga.ga_offset;
533 locs[GPIOCF_MASK] = ga.ga_mask;
535 cf = config_search_loc(NULL, sc->sc_dev, "gpio", locs, &ga);
536 if (cf != NULL) {
537 dv = config_attach_loc(sc->sc_dev, cf, locs, &ga,
538 gpiobus_print);
539 if (dv != NULL) {
540 gdev = kmem_alloc(sizeof(struct gpio_dev),
541 KM_SLEEP);
542 gdev->sc_dev = dv;
543 LIST_INSERT_HEAD(&sc->sc_devs, gdev, sc_next);
544 } else
545 return EINVAL;
546 } else
547 return EINVAL;
548 break;
549 case GPIODETACH:
550 if (kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
551 NULL, NULL, NULL, NULL))
552 return EPERM;
554 attach = (struct gpio_attach *)data;
555 LIST_FOREACH(gdev, &sc->sc_devs, sc_next) {
556 if (strcmp(device_xname(gdev->sc_dev),
557 attach->ga_dvname) == 0) {
558 if (config_detach(gdev->sc_dev, 0) == 0) {
559 LIST_REMOVE(gdev, sc_next);
560 kmem_free(gdev,
561 sizeof(struct gpio_dev));
562 return 0;
564 break;
567 return EINVAL;
568 break;
569 case GPIOSET:
570 if (kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
571 NULL, NULL, NULL, NULL))
572 return EPERM;
574 set = (struct gpio_set *)data;
576 if (set->gp_name[0] != '\0') {
577 pin = gpio_pinbyname(sc, set->gp_name);
578 if (pin == -1)
579 return EINVAL;
580 } else
581 pin = set->gp_pin;
582 if (pin < 0 || pin >= sc->sc_npins)
583 return EINVAL;
584 flags = set->gp_flags;
586 /* check that the controller supports all requested flags */
587 if ((flags & sc->sc_pins[pin].pin_caps) != flags)
588 return ENODEV;
589 flags = set->gp_flags | GPIO_PIN_SET;
591 set->gp_caps = sc->sc_pins[pin].pin_caps;
592 /* return old value */
593 set->gp_flags = sc->sc_pins[pin].pin_flags;
594 if (flags > 0) {
595 gpiobus_pin_ctl(gc, pin, flags);
596 /* update current value */
597 sc->sc_pins[pin].pin_flags = flags;
600 /* rename pin or new pin? */
601 if (set->gp_name2[0] != '\0') {
602 struct gpio_name *gnm;
604 gnm = NULL;
605 LIST_FOREACH(nm, &sc->sc_names, gp_next) {
606 if (!strcmp(nm->gp_name, set->gp_name2) &&
607 nm->gp_pin != pin)
608 return EINVAL; /* duplicate name */
609 if (nm->gp_pin == pin)
610 gnm = nm;
612 if (gnm != NULL)
613 strlcpy(gnm->gp_name, set->gp_name2,
614 sizeof(gnm->gp_name));
615 else {
616 nm = kmem_alloc(sizeof(struct gpio_name),
617 KM_SLEEP);
618 strlcpy(nm->gp_name, set->gp_name2,
619 sizeof(nm->gp_name));
620 nm->gp_pin = set->gp_pin;
621 LIST_INSERT_HEAD(&sc->sc_names, nm, gp_next);
624 break;
625 case GPIOUNSET:
626 if (kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
627 NULL, NULL, NULL, NULL))
628 return EPERM;
630 set = (struct gpio_set *)data;
631 if (set->gp_name[0] != '\0') {
632 pin = gpio_pinbyname(sc, set->gp_name);
633 if (pin == -1)
634 return EINVAL;
635 } else
636 pin = set->gp_pin;
638 if (pin < 0 || pin >= sc->sc_npins)
639 return EINVAL;
640 if (sc->sc_pins[pin].pin_mapped)
641 return EBUSY;
642 if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET))
643 return EINVAL;
645 LIST_FOREACH(nm, &sc->sc_names, gp_next) {
646 if (nm->gp_pin == pin) {
647 LIST_REMOVE(nm, gp_next);
648 kmem_free(nm, sizeof(struct gpio_name));
649 break;
652 sc->sc_pins[pin].pin_flags &= ~GPIO_PIN_SET;
653 break;
654 default:
655 /* Try the old API */
656 DPRINTF(("%s: trying the old API\n", device_xname(sc->sc_dev)));
657 return gpio_ioctl_oapi(sc, cmd, data, flag, cred);
659 return 0;
663 gpio_ioctl_oapi(struct gpio_softc *sc, u_long cmd, void *data, int flag,
664 kauth_cred_t cred)
666 gpio_chipset_tag_t gc;
667 struct gpio_pin_op *op;
668 struct gpio_pin_ctl *ctl;
669 int pin, value, flags;
671 gc = sc->sc_gc;
673 switch (cmd) {
674 case GPIOPINREAD:
675 op = (struct gpio_pin_op *)data;
677 pin = op->gp_pin;
679 if (pin < 0 || pin >= sc->sc_npins)
680 return EINVAL;
682 if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
683 kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
684 NULL, NULL, NULL, NULL))
685 return EPERM;
687 /* return read value */
688 op->gp_value = gpiobus_pin_read(gc, pin);
689 break;
690 case GPIOPINWRITE:
691 if ((flag & FWRITE) == 0)
692 return EBADF;
694 op = (struct gpio_pin_op *)data;
696 pin = op->gp_pin;
698 if (pin < 0 || pin >= sc->sc_npins)
699 return EINVAL;
701 if (sc->sc_pins[pin].pin_mapped)
702 return EBUSY;
704 if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
705 kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
706 NULL, NULL, NULL, NULL))
707 return EPERM;
709 value = op->gp_value;
710 if (value != GPIO_PIN_LOW && value != GPIO_PIN_HIGH)
711 return EINVAL;
713 gpiobus_pin_write(gc, pin, value);
714 /* return old value */
715 op->gp_value = sc->sc_pins[pin].pin_state;
716 /* update current value */
717 sc->sc_pins[pin].pin_state = value;
718 break;
719 case GPIOPINTOGGLE:
720 if ((flag & FWRITE) == 0)
721 return EBADF;
723 op = (struct gpio_pin_op *)data;
725 pin = op->gp_pin;
727 if (pin < 0 || pin >= sc->sc_npins)
728 return EINVAL;
730 if (sc->sc_pins[pin].pin_mapped)
731 return EBUSY;
733 if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
734 kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
735 NULL, NULL, NULL, NULL))
736 return EPERM;
738 value = (sc->sc_pins[pin].pin_state == GPIO_PIN_LOW ?
739 GPIO_PIN_HIGH : GPIO_PIN_LOW);
740 gpiobus_pin_write(gc, pin, value);
741 /* return old value */
742 op->gp_value = sc->sc_pins[pin].pin_state;
743 /* update current value */
744 sc->sc_pins[pin].pin_state = value;
745 break;
746 case GPIOPINCTL:
747 ctl = (struct gpio_pin_ctl *) data;
749 if (kauth_authorize_device(cred, KAUTH_DEVICE_GPIO_PINSET,
750 NULL, NULL, NULL, NULL))
751 return EPERM;
753 pin = ctl->gp_pin;
755 if (pin < 0 || pin >= sc->sc_npins)
756 return EINVAL;
757 if (sc->sc_pins[pin].pin_mapped)
758 return EBUSY;
759 flags = ctl->gp_flags;
761 /* check that the controller supports all requested flags */
762 if ((flags & sc->sc_pins[pin].pin_caps) != flags)
763 return ENODEV;
765 ctl->gp_caps = sc->sc_pins[pin].pin_caps;
766 /* return old value */
767 ctl->gp_flags = sc->sc_pins[pin].pin_flags;
768 if (flags > 0) {
769 gpiobus_pin_ctl(gc, pin, flags);
770 /* update current value */
771 sc->sc_pins[pin].pin_flags = flags;
773 break;
774 default:
775 return ENOTTY;
777 return 0;