static char* lora_gpio_pin_name_by_attr_name(const char* name, int port) {
	switch (port) {
		case port_1:
			if (! strcmp(name, "reset")) {
				return "ap1-reset";
			} else if (!strcmp(name,"cdone")) {
				return "ap1-cdone";
			} else if (!strcmp(name,"creset")) {
				return "ap1-creset";
			} else {
				log_error("attribute name [%s] is invalid for LORA in port %d", name, port);
				return "";
			}

		case port_2:
			if (! strcmp(name, "reset")) {
				return "ap2-reset";
			} else if (!strcmp(name,"cdone")) {
				return "ap2-cdone";
			} else if (!strcmp(name,"creset")) {
				return "ap2-creset";
			} else {
				log_error("attribute name [%s] is invalid for LORA in port %d", name, port);
				return "";
			}
	}
}

// 1 reset
// 1 vendor-id
// 1 product-id
// 1 device-id
// 1 hw-version
// 1 eui
// 1 cdone
// 1 creset
// NULL
static size_t ap_lora_attrs_size = 9;

// Set the hardware version if the ROM string matches one of the valid
// hardware versions.
// Lengths of strings must be the same for a match, then can compare
// text.  Without a length check a subset of a string could be a match.
#define COMPARE_AND_ASSIGN(CANDIDATE) 	((hw_version_len == (sizeof(HW_VERSION_ ## CANDIDATE)-1)) && \
	  (strncmp(ap_eeprom[port_index].hw_version, HW_VERSION_ ## CANDIDATE, sizeof(HW_VERSION_ ## CANDIDATE)) == 0)) \
		lora_hw_version = CANDIDATE;

static bool lora_setup(enum ap port) {
	int i;
	int port_index = port - 1;
	int index = 0;
	int count = 0;
	int ret;
	char buf[32];
	struct kobj_attribute* attr;
	struct attribute **attrs;
	int lora_hw_version;
	int hw_version_len;

	log_info("loading LORA accessory card in port %d", port);

	sprintf(buf, "ap%d", port);
	ap_subdirs[port_index] = kobject_create_and_add(buf, &mts_io_platform_device->dev.kobj);
	if (! ap_subdirs[port_index]) {
		log_error("kobject_create_and_add in port %d failed", port);
		return false;
	}

	// create the link to the apX directory this card is in
	// if we're in the first slot, we get plain "lora"
	// if we're in a different slot, we might need to use "lora-2" to differentiate
	if (port > 1) {
		for (i = 1; i < port; i++) {
			if (port_info[i - 1]) {
				if (strstr(port_info[i - 1]->product_id, PRODUCT_ID_MTAC_LORA)) {
					count++;
				}
			}
		}
	}
	if (count > 0) {
		sprintf(buf, "lora-%d", count + 1);
	} else {
		sprintf(buf, "lora");
	}
	ret = sysfs_create_link(ap_subdirs[port_index]->parent, ap_subdirs[port_index], buf);
	if (ret) {
		log_error("failed to link [%s] to [%s], %d", buf, ap_subdirs[port_index]->name, ret);
	} else
		log_info("created link [%s] to [%s], success:%d", buf, ap_subdirs[port_index]->name, ret);

	attrs = kzalloc(sizeof(struct attribute*) * ap_lora_attrs_size, GFP_KERNEL);
	if (! attrs) {
		log_error("failed to allocate attribute space for port %d", port);
		return false;
	}

	// hw_version string length, null character not counted.
	hw_version_len = strnlen(ap_eeprom[port_index].hw_version,sizeof ap_eeprom[port_index].hw_version);

	if COMPARE_AND_ASSIGN(MTAC_LORA_0_0)
	else if COMPARE_AND_ASSIGN(MTAC_LORA_1_0)
	else if COMPARE_AND_ASSIGN(MTAC_LORA_1_5)
	else {
		log_error("Unknown hw-version in port %d", port);
		kfree(attrs);
		return false;
	}

	// add reset line attribute for MTAC_LORA_0_0
	if ((lora_hw_version == MTAC_LORA_0_0) || (lora_hw_version == MTAC_LORA_1_0)) {
		sprintf(buf, "reset");
		attr = create_attribute(buf, MTS_ATTR_MODE_RW);
		if (! attr) {
			log_error("failed to create attribute [%s] for LORA in port %d", buf, port);
			kfree(attrs);
			return false;
		}
		attr->show = mts_attr_show_ap_gpio_pin;
		attr->store = mts_attr_store_ap_gpio_pin;
		attrs[index++] = &attr->attr;
	}
	else if (lora_hw_version == MTAC_LORA_1_5) {
                int ipin = 0;
		// Substitute pins for this port
		log_info("Substitute pins");
		while(*(lora_h[port_index][ipin].name)) {
			struct gpio_pin *p;
			p = gpio_pin_by_num(lora_h[port_index][ipin].pin.gpio);
			if(p) {
				log_info("LORA H: Replace name %s with name %s",p->name,lora_h[port_index][ipin].name);
				log_info("LORA H: Replace pin number %u with number %u",p->pin.gpio,lora_h[port_index][ipin].pin.gpio);
				*p = lora_h[port_index][ipin];
			}
			ipin++;
		}

		//add support for reset
		sprintf(buf, "reset");
		attr = create_attribute(buf, MTS_ATTR_MODE_RW);
		if (! attr) {
			log_error("failed to create attribute [%s] for LORA in port %d", buf, port);
			kfree(attrs);
			return false;
		}
		attr->show = mts_attr_show_ap_gpio_pin;
		attr->store = mts_attr_store_ap_gpio_pin;
		attrs[index++] = &attr->attr;

		//GPIO1 - cdone on FPGA - input to CPU
		sprintf(buf, "cdone");
		attr = create_attribute(buf, MTS_ATTR_MODE_RO);
		if (! attr) {
			log_error("failed to create attribute [%s] for LORA in port %d", buf, port);
			kfree(attrs);
			return false;
		}
		attr->show = mts_attr_show_ap_gpio_pin;
		attrs[index++] = &attr->attr;

		//GPIO2 reset on FPGA - output from CPU
		sprintf(buf, "creset");
		attr = create_attribute(buf, MTS_ATTR_MODE_RW);
		if (! attr) {
			log_error("failed to create attribute [%s] for LORA in port %d", buf, port);
			kfree(attrs);
	 		return false;
	 	}
		attr->show = mts_attr_show_ap_gpio_pin;
		attr->store = mts_attr_store_ap_gpio_pin;
		attrs[index++] = &attr->attr;
		if(index >= ap_lora_attrs_size) {
			panic("Internal error, too many attributes on the LORA card index %d >= %d",
					index,ap_lora_attrs_size);
		}
	}

	// add attributes for eeprom contents
	ret = ap_add_product_info_attributes(port, lora_hw_version, attrs, &index);

	attrs[index] = NULL;  // Terminate the array.
	ap_attr_groups[port_index].attrs = attrs;  // attrs available for teardown.

	if (!ret) {
		log_error("failed to add product info attributes for LORA in port %d", port);
		return false;
	}
    log_info("ap_subdirs[port_index=%d] = %p ap_subdirs[port_index=%d]=%p",
		port_index,ap_subdirs[port_index],port_index,&ap_attr_groups[port_index]);
	if (sysfs_create_group(ap_subdirs[port_index], &ap_attr_groups[port_index])) {
		log_error("sysfs_create_group failed for LORA in port %d", port);
		return false;
	}

	return true;
}

static bool lora_teardown(enum ap port) {
	int i;
	int port_index = port - 1;
	struct attribute **attrs = ap_attr_groups[port_index].attrs;

	log_info("unloading LORA accessory card in port %d", port);
 
	if(attrs) {
	  // clean up allocated memory for attributes
	  for (i = 0; i < ap_lora_attrs_size; i++) {
		  if (attrs[i]) {
			  if (attrs[i]->name)
				  kfree(attrs[i]->name);

			  kfree(attrs[i]);
		  }
	  }

	  kfree(attrs);
	}
	// clean up our "apX/" kobject if it exists
	if (ap_subdirs[port_index]) {
		kobject_put(ap_subdirs[port_index]);
	}

	return true;
}

bool set_lora_info(struct ap_info* info) {
	snprintf(info->product_id, 32, "%s", PRODUCT_ID_MTAC_LORA);
	info->setup = &lora_setup;
	info->teardown = &lora_teardown;
	info->gpio_pin_name_by_attr_name = &lora_gpio_pin_name_by_attr_name;

	return true;
}