1 /* $OpenBSD: gpio.c,v 1.11 2009/08/29 11:04:56 miod 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 135 if (cf->cf_attach->ca_match(parent, cf, &ga) > 0) 136 config_attach(parent, cf, &ga, gpio_print); 137 138 return (0); 139 } 140 141 int 142 gpio_print(void *aux, const char *pnp) 143 { 144 struct gpio_attach_args *ga = aux; 145 int i; 146 147 printf(" pins"); 148 for (i = 0; i < 32; i++) 149 if (ga->ga_mask & (1 << i)) 150 printf(" %d", ga->ga_offset + i); 151 152 return (UNCONF); 153 } 154 155 int 156 gpiobus_print(void *aux, const char *pnp) 157 { 158 struct gpiobus_attach_args *gba = aux; 159 160 if (pnp != NULL) 161 printf("%s at %s", gba->gba_name, pnp); 162 163 return (UNCONF); 164 } 165 166 int 167 gpio_pin_map(void *gpio, int offset, u_int32_t mask, struct gpio_pinmap *map) 168 { 169 struct gpio_softc *sc = gpio; 170 int npins, pin, i; 171 172 npins = gpio_npins(mask); 173 if (npins > sc->sc_npins) 174 return (1); 175 176 for (npins = 0, i = 0; i < 32; i++) 177 if (mask & (1 << i)) { 178 pin = offset + i; 179 if (pin < 0 || pin >= sc->sc_npins) 180 return (1); 181 if (sc->sc_pins[pin].pin_mapped) 182 return (1); 183 sc->sc_pins[pin].pin_mapped = 1; 184 map->pm_map[npins++] = pin; 185 } 186 map->pm_size = npins; 187 188 return (0); 189 } 190 191 void 192 gpio_pin_unmap(void *gpio, struct gpio_pinmap *map) 193 { 194 struct gpio_softc *sc = gpio; 195 int pin, i; 196 197 for (i = 0; i < map->pm_size; i++) { 198 pin = map->pm_map[i]; 199 sc->sc_pins[pin].pin_mapped = 0; 200 } 201 } 202 203 int 204 gpio_pin_read(void *gpio, struct gpio_pinmap *map, int pin) 205 { 206 struct gpio_softc *sc = gpio; 207 208 return (gpiobus_pin_read(sc->sc_gc, map->pm_map[pin])); 209 } 210 211 void 212 gpio_pin_write(void *gpio, struct gpio_pinmap *map, int pin, int value) 213 { 214 struct gpio_softc *sc = gpio; 215 216 return (gpiobus_pin_write(sc->sc_gc, map->pm_map[pin], value)); 217 } 218 219 void 220 gpio_pin_ctl(void *gpio, struct gpio_pinmap *map, int pin, int flags) 221 { 222 struct gpio_softc *sc = gpio; 223 224 return (gpiobus_pin_ctl(sc->sc_gc, map->pm_map[pin], flags)); 225 } 226 227 int 228 gpio_pin_caps(void *gpio, struct gpio_pinmap *map, int pin) 229 { 230 struct gpio_softc *sc = gpio; 231 232 return (sc->sc_pins[map->pm_map[pin]].pin_caps); 233 } 234 235 int 236 gpio_npins(u_int32_t mask) 237 { 238 int npins, i; 239 240 for (npins = 0, i = 0; i < 32; i++) 241 if (mask & (1 << i)) 242 npins++; 243 244 return (npins); 245 } 246 247 int 248 gpioopen(dev_t dev, int flag, int mode, struct proc *p) 249 { 250 struct gpio_softc *sc; 251 252 sc = (struct gpio_softc *)device_lookup(&gpio_cd, minor(dev)); 253 if (sc == NULL) 254 return (ENXIO); 255 256 if (sc->sc_opened) 257 return (EBUSY); 258 sc->sc_opened = 1; 259 260 return (0); 261 } 262 263 int 264 gpioclose(dev_t dev, int flag, int mode, struct proc *p) 265 { 266 struct gpio_softc *sc; 267 268 sc = (struct gpio_softc *)device_lookup(&gpio_cd, minor(dev)); 269 sc->sc_opened = 0; 270 271 return (0); 272 } 273 274 int 275 gpio_pinbyname(struct gpio_softc *sc, char *gp_name) 276 { 277 struct gpio_name *nm; 278 279 LIST_FOREACH(nm, &sc->sc_names, gp_next) 280 if (!strcmp(nm->gp_name, gp_name)) 281 return (nm->gp_pin); 282 return (-1); 283 } 284 285 int 286 gpioioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) 287 { 288 struct gpio_softc *sc; 289 gpio_chipset_tag_t gc; 290 struct gpio_info *info; 291 struct gpio_pin_op *op; 292 struct gpio_attach *attach; 293 struct gpio_attach_args ga; 294 struct gpio_dev *gdev; 295 struct gpio_name *nm; 296 struct gpio_pin_set *set; 297 struct device *dv; 298 int pin, value, flags, npins, found; 299 300 sc = (struct gpio_softc *)device_lookup(&gpio_cd, minor(dev)); 301 gc = sc->sc_gc; 302 303 switch (cmd) { 304 case GPIOINFO: 305 info = (struct gpio_info *)data; 306 if (securelevel < 1) 307 info->gpio_npins = sc->sc_npins; 308 else { 309 for (pin = npins = 0; pin < sc->sc_npins; pin++) 310 if (sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) 311 ++npins; 312 info->gpio_npins = npins; 313 } 314 break; 315 case GPIOPINREAD: 316 op = (struct gpio_pin_op *)data; 317 318 if (op->gp_name[0] != '\0') { 319 pin = gpio_pinbyname(sc, op->gp_name); 320 if (pin == -1) 321 return (EINVAL); 322 } else 323 pin = op->gp_pin; 324 325 if (pin < 0 || pin >= sc->sc_npins) 326 return (EINVAL); 327 328 if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) && 329 securelevel > 0) 330 return (EPERM); 331 332 /* return read value */ 333 op->gp_value = gpiobus_pin_read(gc, pin); 334 break; 335 case GPIOPINWRITE: 336 if ((flag & FWRITE) == 0) 337 return (EBADF); 338 339 op = (struct gpio_pin_op *)data; 340 341 if (op->gp_name[0] != '\0') { 342 pin = gpio_pinbyname(sc, op->gp_name); 343 if (pin == -1) 344 return (EINVAL); 345 } else 346 pin = op->gp_pin; 347 348 if (pin < 0 || pin >= sc->sc_npins) 349 return (EINVAL); 350 351 if (sc->sc_pins[pin].pin_mapped) 352 return (EBUSY); 353 354 if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) && 355 securelevel > 0) 356 return (EPERM); 357 358 value = op->gp_value; 359 if (value != GPIO_PIN_LOW && value != GPIO_PIN_HIGH) 360 return (EINVAL); 361 362 gpiobus_pin_write(gc, pin, value); 363 /* return old value */ 364 op->gp_value = sc->sc_pins[pin].pin_state; 365 /* update current value */ 366 sc->sc_pins[pin].pin_state = value; 367 break; 368 case GPIOPINTOGGLE: 369 if ((flag & FWRITE) == 0) 370 return (EBADF); 371 372 op = (struct gpio_pin_op *)data; 373 374 if (op->gp_name[0] != '\0') { 375 pin = gpio_pinbyname(sc, op->gp_name); 376 if (pin == -1) 377 return (EINVAL); 378 } else 379 pin = op->gp_pin; 380 381 if (pin < 0 || pin >= sc->sc_npins) 382 return (EINVAL); 383 384 if (sc->sc_pins[pin].pin_mapped) 385 return (EBUSY); 386 387 if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) && 388 securelevel > 0) 389 return (EPERM); 390 391 value = (sc->sc_pins[pin].pin_state == GPIO_PIN_LOW ? 392 GPIO_PIN_HIGH : GPIO_PIN_LOW); 393 gpiobus_pin_write(gc, pin, value); 394 /* return old value */ 395 op->gp_value = sc->sc_pins[pin].pin_state; 396 /* update current value */ 397 sc->sc_pins[pin].pin_state = value; 398 break; 399 case GPIOATTACH: 400 if (securelevel > 0) 401 return (EPERM); 402 403 attach = (struct gpio_attach *)data; 404 bzero(&ga, sizeof(ga)); 405 ga.ga_gpio = sc; 406 ga.ga_dvname = attach->ga_dvname; 407 ga.ga_offset = attach->ga_offset; 408 ga.ga_mask = attach->ga_mask; 409 dv = config_found_sm((struct device *)sc, &ga, gpiobus_print, 410 gpio_submatch); 411 if (dv != NULL) { 412 gdev = malloc(sizeof(struct gpio_dev), M_DEVBUF, 413 M_WAITOK); 414 gdev->sc_dev = dv; 415 LIST_INSERT_HEAD(&sc->sc_devs, gdev, sc_next); 416 } 417 break; 418 case GPIODETACH: 419 if (securelevel > 0) 420 return (EPERM); 421 422 attach = (struct gpio_attach *)data; 423 LIST_FOREACH(gdev, &sc->sc_devs, sc_next) { 424 if (strcmp(gdev->sc_dev->dv_xname, attach->ga_dvname) 425 == 0) { 426 if (config_detach(gdev->sc_dev, 0) == 0) { 427 LIST_REMOVE(gdev, sc_next); 428 free(gdev, M_DEVBUF); 429 } 430 break; 431 } 432 } 433 break; 434 case GPIOPINSET: 435 if (securelevel > 0) 436 return (EPERM); 437 438 set = (struct gpio_pin_set *)data; 439 440 if (set->gp_name[0] != '\0') { 441 pin = gpio_pinbyname(sc, set->gp_name); 442 if (pin == -1) 443 return (EINVAL); 444 } else 445 pin = set->gp_pin; 446 if (pin < 0 || pin >= sc->sc_npins) 447 return (EINVAL); 448 flags = set->gp_flags; 449 /* check that the controller supports all requested flags */ 450 if ((flags & sc->sc_pins[pin].pin_caps) != flags) 451 return (ENODEV); 452 flags = set->gp_flags | GPIO_PIN_SET; 453 454 set->gp_caps = sc->sc_pins[pin].pin_caps; 455 /* return old value */ 456 set->gp_flags = sc->sc_pins[pin].pin_flags; 457 if (flags > 0) { 458 gpiobus_pin_ctl(gc, pin, flags); 459 /* update current value */ 460 sc->sc_pins[pin].pin_flags = flags; 461 } 462 463 /* rename pin or new pin? */ 464 if (set->gp_name2[0] != '\0') { 465 found = 0; 466 LIST_FOREACH(nm, &sc->sc_names, gp_next) 467 if (nm->gp_pin == pin) { 468 strlcpy(nm->gp_name, set->gp_name2, 469 sizeof(nm->gp_name)); 470 found = 1; 471 break; 472 } 473 if (!found) { 474 nm = malloc(sizeof(struct gpio_name), 475 M_DEVBUF, M_WAITOK); 476 strlcpy(nm->gp_name, set->gp_name2, 477 sizeof(nm->gp_name)); 478 nm->gp_pin = set->gp_pin; 479 LIST_INSERT_HEAD(&sc->sc_names, nm, gp_next); 480 } 481 } 482 break; 483 case GPIOPINUNSET: 484 if (securelevel > 0) 485 return (EPERM); 486 487 set = (struct gpio_pin_set *)data; 488 if (set->gp_name[0] != '\0') { 489 pin = gpio_pinbyname(sc, set->gp_name); 490 if (pin == -1) 491 return (EINVAL); 492 } else 493 pin = set->gp_pin; 494 495 if (pin < 0 || pin >= sc->sc_npins) 496 return (EINVAL); 497 if (sc->sc_pins[pin].pin_mapped) 498 return (EBUSY); 499 if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET)) 500 return (EINVAL); 501 502 LIST_FOREACH(nm, &sc->sc_names, gp_next) { 503 if (nm->gp_pin == pin) { 504 LIST_REMOVE(nm, gp_next); 505 free(nm, M_DEVBUF); 506 break; 507 } 508 } 509 sc->sc_pins[pin].pin_flags &= ~GPIO_PIN_SET; 510 break; 511 default: 512 return (ENOTTY); 513 } 514 515 return (0); 516 } 517