diff --git a/drivers/usbhost/usbhost_registerclass.c b/drivers/usbhost/usbhost_registerclass.c index dab177ed49e..f79d2886bf5 100644 --- a/drivers/usbhost/usbhost_registerclass.c +++ b/drivers/usbhost/usbhost_registerclass.c @@ -81,6 +81,7 @@ int usbhost_registerclass(struct usbhost_registry_s *usbclass) { + FAR struct usbhost_registry_s *curr; irqstate_t flags; uinfo("Registering class:%p nids:%d\n", usbclass, usbclass->nids); @@ -93,6 +94,22 @@ int usbhost_registerclass(struct usbhost_registry_s *usbclass) flags = spin_lock_irqsave(&g_classregistry_lock); + /* Refuse an entry that is already registered. + * + * These are static structures, so registering one twice points the + * entry's own link at itself and the list loses its end. A later search + * for a class that is not there never returns, holding this lock. + */ + + for (curr = g_classregistry; curr != NULL; curr = curr->flink) + { + if (curr == usbclass) + { + spin_unlock_irqrestore(&g_classregistry_lock, flags); + return OK; + } + } + /* Add the new class ID info to the head of the list */ usbclass->flink = g_classregistry;