static void blockset_close(struct blockset *bset) { if (file_valid(bset->desc)) { file_close(bset->desc); } free(bset->filename); free(bset); }
int libsoc_gpio_free (gpio * gpio) { char tmp_str[STR_BUF]; int fd; if (gpio == NULL) { libsoc_gpio_debug (__func__, -1, "invalid gpio pointer"); return EXIT_FAILURE; } libsoc_gpio_debug (__func__, gpio->gpio, "freeing gpio"); if (gpio->callback != NULL) { printf ("Freeing callback!\n"); // Turn off the callback if there is one enabled libsoc_gpio_callback_interrupt_cancel (gpio); } if (file_close (gpio->value_fd) < 0) return EXIT_FAILURE; if (gpio->shared == 1) { free (gpio); return EXIT_SUCCESS; } fd = file_open ("/sys/class/gpio/unexport", O_SYNC | O_WRONLY); if (fd < 0) return EXIT_FAILURE; sprintf (tmp_str, "%d", gpio->gpio); if (file_write (fd, tmp_str, STR_BUF) < 0) return EXIT_FAILURE; if (file_close (fd) < 0) return EXIT_FAILURE; sprintf (tmp_str, "/sys/class/gpio/gpio%d", gpio->gpio); if (file_valid (tmp_str)) { libsoc_gpio_debug (__func__, gpio->gpio, "freeing failed"); return EXIT_FAILURE; } free (gpio); return EXIT_SUCCESS; }
spi * libsoc_spi_init (uint8_t spidev_device, uint8_t chip_select) { spi *spi_dev; libsoc_spi_debug (__func__, NULL, "initialising spidev device %d.%d", spidev_device, chip_select); spi_dev = malloc (sizeof (spi)); if (spi_dev == NULL) { libsoc_spi_debug (__func__, NULL, "failed to allocate memory"); return NULL; } char path[40]; spi_dev->spi_dev = spidev_device; spi_dev->chip_select = chip_select; sprintf (path, "/dev/spidev%d.%d", spi_dev->spi_dev, spi_dev->chip_select); if (!file_valid (path)) { libsoc_spi_debug (__func__, spi_dev, "%s not a vaild device", path); goto error; } spi_dev->fd = file_open (path, O_SYNC | O_RDWR); if (spi_dev->fd < 0) { libsoc_spi_debug (__func__, spi_dev, "%s could not be opened", path); goto error; } return spi_dev; error: free (spi_dev); return NULL; }
i2c * libsoc_i2c_init (uint8_t i2c_bus, uint8_t i2c_address) { i2c *i2c_dev; libsoc_i2c_debug (__func__, NULL, "initialising i2c bus %d at " "address %d", i2c_bus, i2c_address); i2c_dev = malloc (sizeof (i2c)); if (i2c_dev == NULL) { libsoc_i2c_debug (__func__, NULL, "failed to allocate memory"); return NULL; } char path[40]; i2c_dev->bus = i2c_bus; i2c_dev->address = i2c_address; sprintf (path, "/dev/i2c-%d", i2c_dev->bus); if (!file_valid (path)) { libsoc_i2c_debug (__func__, i2c_dev, "%s not a vaild device", path); goto error; } i2c_dev->fd = file_open (path, O_SYNC | O_RDWR); if (i2c_dev->fd < 0) { libsoc_i2c_debug (__func__, i2c_dev, "%s could not be opened", path); goto error; } return i2c_dev; error: free (i2c_dev); return NULL; }
gpio * libsoc_gpio_request (unsigned int gpio_id, enum gpio_mode mode) { gpio *new_gpio; char tmp_str[STR_BUF]; int shared = 0; if (mode != LS_SHARED && mode != LS_GREEDY && mode != LS_WEAK) { libsoc_gpio_debug (__func__, gpio_id, "mode was not set, or invalid," " setting mode to LS_SHARED"); mode = LS_SHARED; } libsoc_gpio_debug (__func__, gpio_id, "requested gpio"); sprintf (tmp_str, "/sys/class/gpio/gpio%d/value", gpio_id); if (file_valid (tmp_str)) { libsoc_gpio_debug (__func__, gpio_id, "GPIO already exported"); switch (mode) { case LS_WEAK: { return NULL; } case LS_SHARED: { shared = 1; break; } default: { break; } } } else { int fd = file_open ("/sys/class/gpio/export", O_SYNC | O_WRONLY); if (fd < 0) return NULL; sprintf (tmp_str, "%d", gpio_id); if (file_write (fd, tmp_str, STR_BUF) < 0) return NULL; if (file_close (fd)) return NULL; sprintf (tmp_str, "/sys/class/gpio/gpio%d", gpio_id); if (!file_valid (tmp_str)) { libsoc_gpio_debug (__func__, gpio_id, "gpio did not export correctly"); perror ("libsoc-gpio-debug"); return NULL; } } new_gpio = malloc (sizeof (gpio)); if (new_gpio == NULL) return NULL; sprintf (tmp_str, "/sys/class/gpio/gpio%d/value", gpio_id); new_gpio->value_fd = file_open (tmp_str, O_SYNC | O_RDWR); if (new_gpio->value_fd < 0) { free(new_gpio); return NULL; } new_gpio->gpio = gpio_id; new_gpio->shared = shared; new_gpio->callback = NULL; return new_gpio; }