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