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