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