Move debug key handling into adapter_ops

There's no need for the core daemon to have special handling for debug
keys anymore as long as it tells adapter_ops if they are enabled or not
(new parameter added to adapter_ops->load_keys).
diff --git a/plugins/hciops.c b/plugins/hciops.c
index 2e2f39a..1b0396f 100644
--- a/plugins/hciops.c
+++ b/plugins/hciops.c
@@ -91,6 +91,7 @@
 	GIOChannel *io;
 	guint watch_id;
 
+	gboolean debug_keys;
 	GSList *keys;
 	int pin_length;
 } *devs = NULL;
@@ -665,9 +666,7 @@
 	} else
 		type = 0xff;
 
-	if (device && device_get_debug_key(device, key))
-		type = 0x03;
-	else if (key_info == NULL || key_info->type == 0x03) {
+	if (key_info == NULL || (!dev->debug_keys && key_info->type == 0x03)) {
 		/* Link key not found */
 		hci_send_cmd(dev->sk, OGF_LINK_CTL, OCF_LINK_KEY_NEG_REPLY,
 								6, dba);
@@ -3160,16 +3159,18 @@
 	return 0;
 }
 
-static int hciops_load_keys(int index, GSList *keys)
+static int hciops_load_keys(int index, GSList *keys, gboolean debug_keys)
 {
 	struct dev_info *dev = &devs[index];
 
-	DBG("hci%d keys %d", index, g_slist_length(keys));
+	DBG("hci%d keys %d debug_keys %d", index, g_slist_length(keys),
+								debug_keys);
 
 	if (dev->keys != NULL)
 		return -EEXIST;
 
 	dev->keys = keys;
+	dev->debug_keys = debug_keys;
 
 	return 0;
 }
diff --git a/plugins/mgmtops.c b/plugins/mgmtops.c
index 9d6b522..a9c1b2f 100644
--- a/plugins/mgmtops.c
+++ b/plugins/mgmtops.c
@@ -819,9 +819,10 @@
 	return -ENOSYS;
 }
 
-static int mgmt_load_keys(int index, GSList *keys)
+static int mgmt_load_keys(int index, GSList *keys, gboolean debug_keys)
 {
-	DBG("index %d keys %d", index, g_slist_length(keys));
+	DBG("index %d keys %d debug_keys %d", index, g_slist_length(keys),
+								debug_keys);
 	return -ENOSYS;
 }
 
diff --git a/src/adapter.c b/src/adapter.c
index 669a0ac..cf3566d 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -1913,7 +1913,8 @@
 	create_name(filename, PATH_MAX, STORAGEDIR, srcaddr, "linkkeys");
 	textfile_foreach(filename, create_stored_device_from_linkkeys, &keys);
 
-	err = adapter_ops->load_keys(adapter->dev_id, keys.keys);
+	err = adapter_ops->load_keys(adapter->dev_id, keys.keys,
+							main_opts.debug_keys);
 	if (err < 0) {
 		error("Unable to load keys to adapter_ops: %s (%d)",
 							strerror(-err), -err);
diff --git a/src/adapter.h b/src/adapter.h
index a390088..77ae1e9 100644
--- a/src/adapter.h
+++ b/src/adapter.h
@@ -239,7 +239,7 @@
 	int (*services_updated) (int index);
 	int (*disable_cod_cache) (int index);
 	int (*restore_powered) (int index);
-	int (*load_keys) (int index, GSList *keys);
+	int (*load_keys) (int index, GSList *keys, gboolean debug_keys);
 };
 
 int btd_register_adapter_ops(struct btd_adapter_ops *ops, gboolean priority);
diff --git a/src/device.c b/src/device.c
index d68c0a3..bef8e71 100644
--- a/src/device.c
+++ b/src/device.c
@@ -143,9 +143,6 @@
 
 	gboolean	authorizing;
 	gint		ref;
-
-	gboolean	has_debug_key;
-	uint8_t		debug_key[16];
 };
 
 static uint16_t uuid_list[] = {
@@ -2366,33 +2363,6 @@
 	return find_record_in_list(device->tmp_records, uuid);
 }
 
-gboolean device_set_debug_key(struct btd_device *device, uint8_t *key)
-{
-	if (key == NULL) {
-		device->has_debug_key = FALSE;
-		return TRUE;
-	}
-
-	memcpy(device->debug_key, key, 16);
-	device->has_debug_key = TRUE;
-
-	return TRUE;
-}
-
-gboolean device_get_debug_key(struct btd_device *device, uint8_t *key)
-{
-	if (main_opts.debug_keys == FALSE)
-		return FALSE;
-
-	if (!device->has_debug_key)
-		return FALSE;
-
-	if (key != NULL)
-		memcpy(key, device->debug_key, 16);
-
-	return TRUE;
-}
-
 int btd_register_device_driver(struct btd_device_driver *driver)
 {
 	device_drivers = g_slist_append(device_drivers, driver);
diff --git a/src/event.c b/src/event.c
index 22f4f72..95e5bfc 100644
--- a/src/event.c
+++ b/src/event.c
@@ -708,8 +708,6 @@
 	new_key_type = key_type;
 
 	if (key_type == 0x06) {
-		if (device_get_debug_key(device, NULL))
-			old_key_type = 0x03;
 		/* Some buggy controller combinations generate a changed
 		 * combination key for legacy pairing even when there's no
 		 * previous key */
@@ -731,9 +729,6 @@
 	DBG("local auth 0x%02x and remote auth 0x%02x",
 					local_auth, remote_auth);
 
-	/* Clear any previous debug key */
-	device_set_debug_key(device, NULL);
-
 	/* If this is not the first link key set a flag so a subsequent auth
 	 * complete event doesn't trigger SDP and remove any stored key */
 	if (old_key_type != 0xff) {
@@ -741,9 +736,11 @@
 		device_remove_bonding(device);
 	}
 
-	/* Store the link key only in runtime memory if it's a debug
-	 * key, else store the link key persistently if one of the
-	 * following is true:
+	/* Skip the storage check if this is a debug key */
+	if (new_key_type == 0x03)
+		goto proceed;
+
+	/* Store the link key persistently if one of the following is true:
 	 * 1. this is a legacy link key
 	 * 2. this is a changed combination key and there was a previously
 	 *    stored one
@@ -754,11 +751,7 @@
 	 * If none of the above match only keep the link key around for
 	 * this connection and set the temporary flag for the device.
 	 */
-	if (new_key_type == 0x03) {
-		DBG("Storing debug key in runtime memory");
-		device_set_debug_key(device, key);
-	} else if (key_type < 0x03 ||
-				(key_type == 0x06 && old_key_type != 0xff) ||
+	if (key_type < 0x03 || (key_type == 0x06 && old_key_type != 0xff) ||
 				(local_auth > 0x01 && remote_auth > 0x01) ||
 				(local_auth == 0x02 || local_auth == 0x03) ||
 				(remote_auth == 0x02 || remote_auth == 0x03)) {
@@ -775,6 +768,7 @@
 	} else
 		temporary = TRUE;
 
+proceed:
 	if (!device_is_connected(device))
 		device_set_secmode3_conn(device, TRUE);
 	else if (!device_is_bonding(device, NULL)) {