Blame src/pci_slot.c

Packit Service db8df9
/*
Packit Service db8df9
 * Intel(R) Enclosure LED Utilities
Packit Service db8df9
 * Copyright (C) 2016-2018 Intel Corporation.
Packit Service db8df9
 *
Packit Service db8df9
 * This program is free software; you can redistribute it and/or modify it
Packit Service db8df9
 * under the terms and conditions of the GNU General Public License,
Packit Service db8df9
 * version 2, as published by the Free Software Foundation.
Packit Service db8df9
 *
Packit Service db8df9
 * This program is distributed in the hope it will be useful, but WITHOUT
Packit Service db8df9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
Packit Service db8df9
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
Packit Service db8df9
 * more details.
Packit Service db8df9
 *
Packit Service db8df9
 * You should have received a copy of the GNU General Public License along with
Packit Service db8df9
 * this program; if not, write to the Free Software Foundation, Inc.,
Packit Service db8df9
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
Packit Service db8df9
 *
Packit Service db8df9
 */
Packit Service db8df9
Packit Service db8df9
#include <limits.h>
Packit Service db8df9
#include <stdint.h>
Packit Service db8df9
#include <stdlib.h>
Packit Service db8df9
#include <string.h>
Packit Service db8df9
#include <unistd.h>
Packit Service db8df9
Packit Service db8df9
#if _HAVE_DMALLOC_H
Packit Service db8df9
#include <dmalloc.h>
Packit Service db8df9
#endif
Packit Service db8df9
Packit Service db8df9
#include "config.h"
Packit Service db8df9
#include "pci_slot.h"
Packit Service db8df9
#include "utils.h"
Packit Service db8df9
Packit Service db8df9
/*
Packit Service db8df9
 * Allocates memory for PCI hotplug slot structure and initializes fields of
Packit Service db8df9
 * the structure.
Packit Service db8df9
 */
Packit Service db8df9
struct pci_slot *pci_slot_init(const char *path)
Packit Service db8df9
{
Packit Service db8df9
	struct pci_slot *result = NULL;
Packit Service db8df9
Packit Service db8df9
	result = malloc(sizeof(struct pci_slot));
Packit Service db8df9
	if (result == NULL)
Packit Service db8df9
		return NULL;
Packit Service db8df9
	result->sysfs_path = str_dup(path);
Packit Service db8df9
	result->address = get_text(path, "address");
Packit Service db8df9
	result->attention = get_int(path, -1, "attention");
Packit Service db8df9
Packit Service db8df9
	if (result->attention == -1) {
Packit Service db8df9
		pci_slot_fini(result);
Packit Service db8df9
		return NULL;
Packit Service db8df9
	}
Packit Service db8df9
Packit Service db8df9
	return result;
Packit Service db8df9
}
Packit Service db8df9
Packit Service db8df9
/*
Packit Service db8df9
 * The function returns memory allocated for fields of hotplug slot structure
Packit Service db8df9
 * to the system.
Packit Service db8df9
 */
Packit Service db8df9
void pci_slot_fini(struct pci_slot *slot)
Packit Service db8df9
{
Packit Service db8df9
	if (slot) {
Packit Service db8df9
		free(slot->sysfs_path);
Packit Service db8df9
		free(slot->address);
Packit Service db8df9
		free(slot);
Packit Service db8df9
	}
Packit Service db8df9
}