1 /* $OpenBSD: gpio.c,v 1.15 2017/08/18 12:15:35 jsg Exp $ */ 2 3 /* 4 * Copyright (c) 2008 Marc Balmer <mbalmer@openbsd.org> 5 * Copyright (c) 2004, 2006 Alexander Yurchenko <grange@openbsd.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 /* 21 * General Purpose Input/Output framework. 22 */ 23 24 #include <sys/param.h> 25 #include <sys/systm.h> 26 #include <sys/conf.h> 27 #include <sys/device.h> 28 #include <sys/fcntl.h> 29 #include <sys/ioctl.h> 30 #include <sys/gpio.h> 31 #include <sys/vnode.h> 32 #include <sys/malloc.h> 33 #include <sys/queue.h> 34 35 #include <dev/gpio/gpiovar.h> 36 37 struct gpio_softc { 38 struct device sc_dev; 39 40 gpio_chipset_tag_t sc_gc; /* GPIO controller */ 41 gpio_pin_t *sc_pins; /* pins array */ 42 int sc_npins; /* number of pins */ 43 44 int sc_opened; 45 LIST_HEAD(, gpio_dev) sc_devs; /* devices */ 46 LIST_HEAD(, gpio_name) sc_names; /* named pins */ 47 }; 48 49 int gpio_match(struct device *, void *, void *); 50 int gpio_submatch(struct device *, void *, void *); 51 void gpio_attach(struct device *, struct device *, void *); 52 int gpio_detach(struct device *, int); 53 int gpio_search(struct device *, void *, void *); 54 int gpio_print(void *, const char *); 55 int gpio_pinbyname(struct gpio_softc *, char *gp_name); 56 57 struct cfattach gpio_ca = { 58 sizeof (struct gpio_softc), 59 gpio_match, 60 gpio_attach, 61 gpio_detach 62 }; 63 64 struct cfdriver gpio_cd = { 65 NULL, "gpio", DV_DULL 66 }; 67 68 int 69 gpio_match(struct device *parent, void *match, void *aux) 70 { 71 struct cfdata *cf = match; 72 struct gpiobus_attach_args *gba = aux; 73 74 return (strcmp(gba->gba_name, cf->cf_driver->cd_name) == 0); 75 } 76 77 int 78 gpio_submatch(struct device *parent, void *match, void *aux) 79 { 80 struct cfdata *cf = match; 81 struct gpio_attach_args *ga = aux; 82 83 if (strcmp(ga->ga_dvname, cf->cf_driver->cd_name) != 0) 84 return (0); 85 86 return ((*cf->cf_attach->ca_match)(parent, match, aux)); 87 } 88 89 void 90 gpio_attach(struct device *parent, struct device *self, void *aux) 91 { 92 struct gpio_softc *sc = (struct gpio_softc *)self; 93 struct gpiobus_attach_args *gba = aux; 94 95 sc->sc_gc = gba->gba_gc; 96 sc->sc_pins = gba->gba_pins; 97 sc->sc_npins = gba->gba_npins; 98 99 printf(": %d pins\n", sc->sc_npins); 100 101 /* 102 * Attach all devices that can be connected to the GPIO pins 103 * described in the kernel configuration file. 104 */ 105 config_search(gpio_search, self, sc); 106 } 107 108 int 109 gpio_detach(struct device *self, int flags) 110 { 111 int maj, mn; 112 113 /* Locate the major number */ 114 for (maj = 0; maj < nchrdev; maj++) 115 if (cdevsw[maj].d_open == gpioopen) 116 break; 117 118 /* Nuke the vnodes for any open instances (calls close) */ 119 mn = self->dv_unit; 120 vdevgone(maj, mn, mn, VCHR); 121 122 return (0); 123 } 124 125 int 126 gpio_search(struct device *parent, void *arg, void *aux) 127 { 128 struct cfdata *cf = arg; 129 struct gpio_attach_args ga; 130 131 ga.ga_gpio = aux; 132 ga.ga_offset = cf->cf_loc[0]; 133 ga.ga_mask = cf->cf_loc[1]; 134 ga.ga_flags = cf->cf_loc[2]; 135 136 if (cf->cf_attach->ca_match(parent, cf, &ga) > 0) 137 config_attach(parent, cf, &ga, gpio_print); 138 139 return (0); 140 } 141 142 int 143 gpio_print(void *aux, const char *pnp) 144 { 145 struct gpio_attach_args *ga = aux; 146 int i; 147 148 printf(" pins"); 149 for (i = 0; i < 32; i++) 150 if (ga->ga_mask & (1 << i)) 151 printf(" %d", ga->ga_offset + i); 152 153 return (UNCONF); 154 } 155 156 int 157 gpiobus_print(void *aux, const char *pnp) 158 { 159 struct gpiobus_attach_args *gba = aux; 160 161 if (pnp != NULL) 162 printf("%s at %s", gba->gba_name, pnp); 163 164 return (UNCONF); 165 } 166 167 int 168 gpio_pin_map(void *gpio, int offset, u_int32_t mask, struct gpio_pinmap *map) 169 { 170 struct gpio_softc *sc = gpio; 171 int npins, pin, i; 172 173 npins = gpio_npins(mask); 174 if (npins > sc->sc_npins) 175 return (1); 176 177 for (npins = 0, i = 0; i < 32; i++) 178 if (mask & (1 << i)) { 179 pin = offset + i; 180 if (pin < 0 || pin >= sc->sc_npins) 181 return (1); 182 if (sc->sc_pins[pin].pin_mapped) 183 return (1); 184 sc->sc_pins[pin].pin_mapped = 1; 185 map->pm_map[npins++] = pin; 186 } 187 map->pm_size = npins; 188 189 return (0); 190 } 191 192 void 193 gpio_pin_unmap(void *gpio, struct gpio_pinmap *map) 194 { 195 struct gpio_softc *sc = gpio; 196 int pin, i; 197 198 for (i = 0; i < map->pm_size; i++) { 199 pin = map->pm_map[i]; 200 sc->sc_pins[pin].pin_mapped = 0; 201 } 202 } 203 204 int 205 gpio_pin_read(void *gpio, struct gpio_pinmap *map, int pin) 206 { 207 struct gpio_softc *sc = gpio; 208 209 return (gpiobus_pin_read(sc->sc_gc, map->pm_map[pin])); 210 } 211 212 void 213 gpio_pin_write(void *gpio, struct gpio_pinmap *map, int pin, int value) 214 { 215 struct gpio_softc *sc = gpio; 216 217 return (gpiobus_pin_write(sc->sc_gc, map->pm_map[pin], value)); 218 } 219 220 void 221 gpio_pin_ctl(void *gpio, struct gpio_pinmap *map, int pin, int flags) 222 { 223 struct gpio_softc *sc = gpio; 224 225 return (gpiobus_pin_ctl(sc->sc_gc, map->pm_map[pin], flags)); 226 } 227 228 int 229 gpio_pin_caps(void *gpio, struct gpio_pinmap *map, int pin) 230 { 231 struct gpio_softc *sc = gpio; 232 233 return (sc->sc_pins[map->pm_map[pin]].pin_caps); 234 } 235 236 int 237 gpio_npins(u_int32_t mask) 238 { 239 int npins, i; 240 241 for (npins = 0, i = 0; i < 32; i++) 242 if (mask & (1 << i)) 243 npins++; 244 245 return (npins); 246 } 247 248 int 249 gpioopen(dev_t dev, int flag, int mode, struct proc *p) 250 { 251 struct gpio_softc *sc; 252 253 sc = (struct gpio_softc *)device_lookup(&gpio_cd, minor(dev)); 254 if (sc == NULL) 255 return (ENXIO); 256 257 if (sc->sc_opened) 258 return (EBUSY); 259 sc->sc_opened = 1; 260 261 return (0); 262 } 263 264 int 265 gpioclose(dev_t dev, int flag, int mode, struct proc *p) 266 { 267 struct gpio_softc *sc; 268 269 sc = (struct gpio_softc *)device_lookup(&gpio_cd, minor(dev)); 270 if (sc == NULL) 271 return (ENXIO); 272 273 sc->sc_opened = 0; 274 275 return (0); 276 } 277 278 int 279 gpio_pinbyname(struct gpio_softc *sc, char *gp_name) 280 { 281 struct gpio_name *nm; 282 283 LIST_FOREACH(nm, &sc->sc_names, gp_next) 284 if (!strcmp(nm->gp_name, gp_name)) 285 return (nm->gp_pin); 286 return (-1); 287 } 288 289 int 290 gpioioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) 291 { 292 struct gpio_softc *sc; 293 gpio_chipset_tag_t gc; 294 struct gpio_info *info; 295 struct gpio_pin_op *op; 296 struct gpio_attach *attach; 297 struct gpio_attach_args ga; 298 struct gpio_dev *gdev; 299 struct gpio_name *nm; 300 struct gpio_pin_set *set; 301 struct device *dv; 302 int pin, value, flags, npins, found; 303 304 sc = (struct gpio_softc *)device_lookup(&gpio_cd, minor(dev)); 305 if (sc == NULL) 306 return (ENXIO); 307 308 gc = sc->sc_gc; 309 310 switch (cmd) { 311 case GPIOINFO: 312 info = (struct gpio_info *)data; 313 if (securelevel < 1) 314 info->gpio_npins = sc->sc_npins; 315 else { 316 for (pin = npins = 0; pin < sc->sc_npins; pin++) 317 if (sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) 318 ++npins; 319 info->gpio_npins = npins; 320 } 321 break; 322 case GPIOPINREAD: 323 op = (struct gpio_pin_op *)data; 324 325 if (op->gp_name[0] != '\0') { 326 pin = gpio_pinbyname(sc, op->gp_name); 327 if (pin == -1) 328 return (EINVAL); 329 } else 330 pin = op->gp_pin; 331 332 if (pin < 0 || pin >= sc->sc_npins) 333 return (EINVAL); 334 335 if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) && 336 securelevel > 0) 337 return (EPERM); 338 339 /* return read value */ 340 op->gp_value = gpiobus_pin_read(gc, pin); 341 break; 342 case GPIOPINWRITE: 343 if ((flag & FWRITE) == 0) 344 return (EBADF); 345 346 op = (struct gpio_pin_op *)data; 347 348 if (op->gp_name[0] != '\0') { 349 pin = gpio_pinbyname(sc, op->gp_name); 350 if (pin == -1) 351 return (EINVAL); 352 } else 353 pin = op->gp_pin; 354 355 if (pin < 0 || pin >= sc->sc_npins) 356 return (EINVAL); 357 358 if (sc->sc_pins[pin].pin_mapped) 359 return (EBUSY); 360 361 if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) && 362 securelevel > 0) 363 return (EPERM); 364 365 value = op->gp_value; 366 if (value != GPIO_PIN_LOW && value != GPIO_PIN_HIGH) 367 return (EINVAL); 368 369 gpiobus_pin_write(gc, pin, value); 370 /* return old value */ 371 op->gp_value = sc->sc_pins[pin].pin_state; 372 /* update current value */ 373 sc->sc_pins[pin].pin_state = value; 374 break; 375 case GPIOPINTOGGLE: 376 if ((flag & FWRITE) == 0) 377 return (EBADF); 378 379 op = (struct gpio_pin_op *)data; 380 381 if (op->gp_name[0] != '\0') { 382 pin = gpio_pinbyname(sc, op->gp_name); 383 if (pin == -1) 384 return (EINVAL); 385 } else 386 pin = op->gp_pin; 387 388 if (pin < 0 || pin >= sc->sc_npins) 389 return (EINVAL); 390 391 if (sc->sc_pins[pin].pin_mapped) 392 return (EBUSY); 393 394 if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) && 395 securelevel > 0) 396 return (EPERM); 397 398 value = (sc->sc_pins[pin].pin_state == GPIO_PIN_LOW ? 399 GPIO_PIN_HIGH : GPIO_PIN_LOW); 400 gpiobus_pin_write(gc, pin, value); 401 /* return old value */ 402 op->gp_value = sc->sc_pins[pin].pin_state; 403 /* update current value */ 404 sc->sc_pins[pin].pin_state = value; 405 break; 406 case GPIOATTACH: 407 if (securelevel > 0) 408 return (EPERM); 409 410 attach = (struct gpio_attach *)data; 411 bzero(&ga, sizeof(ga)); 412 ga.ga_gpio = sc; 413 ga.ga_dvname = attach->ga_dvname; 414 ga.ga_offset = attach->ga_offset; 415 ga.ga_mask = attach->ga_mask; 416 ga.ga_flags = attach->ga_flags; 417 dv = config_found_sm((struct device *)sc, &ga, gpiobus_print, 418 gpio_submatch); 419 if (dv != NULL) { 420 gdev = malloc(sizeof(*gdev), M_DEVBUF, 421 M_WAITOK); 422 gdev->sc_dev = dv; 423 LIST_INSERT_HEAD(&sc->sc_devs, gdev, sc_next); 424 } 425 break; 426 case GPIODETACH: 427 if (securelevel > 0) 428 return (EPERM); 429 430 attach = (struct gpio_attach *)data; 431 LIST_FOREACH(gdev, &sc->sc_devs, sc_next) { 432 if (strcmp(gdev->sc_dev->dv_xname, attach->ga_dvname) 433 == 0) { 434 if (config_detach(gdev->sc_dev, 0) == 0) { 435 LIST_REMOVE(gdev, sc_next); 436 free(gdev, M_DEVBUF, sizeof(*gdev)); 437 } 438 break; 439 } 440 } 441 break; 442 case GPIOPINSET: 443 if (securelevel > 0) 444 return (EPERM); 445 446 set = (struct gpio_pin_set *)data; 447 448 if (set->gp_name[0] != '\0') { 449 pin = gpio_pinbyname(sc, set->gp_name); 450 if (pin == -1) 451 return (EINVAL); 452 } else 453 pin = set->gp_pin; 454 if (pin < 0 || pin >= sc->sc_npins) 455 return (EINVAL); 456 flags = set->gp_flags; 457 /* check that the controller supports all requested flags */ 458 if ((flags & sc->sc_pins[pin].pin_caps) != flags) 459 return (ENODEV); 460 flags = set->gp_flags | GPIO_PIN_SET; 461 462 set->gp_caps = sc->sc_pins[pin].pin_caps; 463 /* return old value */ 464 set->gp_flags = sc->sc_pins[pin].pin_flags; 465 if (flags > 0) { 466 gpiobus_pin_ctl(gc, pin, flags); 467 /* update current value */ 468 sc->sc_pins[pin].pin_flags = flags; 469 } 470 471 /* rename pin or new pin? */ 472 if (set->gp_name2[0] != '\0') { 473 found = 0; 474 LIST_FOREACH(nm, &sc->sc_names, gp_next) 475 if (nm->gp_pin == pin) { 476 strlcpy(nm->gp_name, set->gp_name2, 477 sizeof(nm->gp_name)); 478 found = 1; 479 break; 480 } 481 if (!found) { 482 nm = malloc(sizeof(*nm), M_DEVBUF, M_WAITOK); 483 strlcpy(nm->gp_name, set->gp_name2, 484 sizeof(nm->gp_name)); 485 nm->gp_pin = set->gp_pin; 486 LIST_INSERT_HEAD(&sc->sc_names, nm, gp_next); 487 } 488 } 489 break; 490 case GPIOPINUNSET: 491 if (securelevel > 0) 492 return (EPERM); 493 494 set = (struct gpio_pin_set *)data; 495 if (set->gp_name[0] != '\0') { 496 pin = gpio_pinbyname(sc, set->gp_name); 497 if (pin == -1) 498 return (EINVAL); 499 } else 500 pin = set->gp_pin; 501 502 if (pin < 0 || pin >= sc->sc_npins) 503 return (EINVAL); 504 if (sc->sc_pins[pin].pin_mapped) 505 return (EBUSY); 506 if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET)) 507 return (EINVAL); 508 509 LIST_FOREACH(nm, &sc->sc_names, gp_next) { 510 if (nm->gp_pin == pin) { 511 LIST_REMOVE(nm, gp_next); 512 free(nm, M_DEVBUF, sizeof(*nm)); 513 break; 514 } 515 } 516 sc->sc_pins[pin].pin_flags &= ~GPIO_PIN_SET; 517 break; 518 default: 519 return (ENOTTY); 520 } 521 522 return (0); 523 } 524