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