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