Skip to content

Commit 733ffff

Browse files
committed
Make usbi_get_device_by_session_id return a ref to the found device
Signed-off-by: Hans de Goede <[email protected]>
1 parent 4ffa16f commit 733ffff

File tree

9 files changed

+26
-28
lines changed

9 files changed

+26
-28
lines changed

libusb/core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ struct libusb_device *usbi_get_device_by_session_id(struct libusb_context *ctx,
620620
usbi_mutex_lock(&ctx->usb_devs_lock);
621621
list_for_each_entry(dev, &ctx->usb_devs, list, struct libusb_device)
622622
if (dev->session_data == session_id) {
623-
ret = dev;
623+
ret = libusb_ref_device(dev);
624624
break;
625625
}
626626
usbi_mutex_unlock(&ctx->usb_devs_lock);

libusb/libusbi.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,8 +534,8 @@ struct usbi_os_backend {
534534
*
535535
* After computing a session ID for a device, call
536536
* usbi_get_device_by_session_id(). This function checks if libusbx already
537-
* knows about the device, and if so, it provides you with a libusb_device
538-
* structure for it.
537+
* knows about the device, and if so, it provides you with a reference
538+
* to a libusb_device structure for it.
539539
*
540540
* If usbi_get_device_by_session_id() returns NULL, it is time to allocate
541541
* a new device structure for the device. Call usbi_alloc_device() to

libusb/os/darwin_usb.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ static void darwin_devices_detached (void *ptr, io_iterator_t rem_devices) {
293293
/* signal the core that this device has been disconnected. the core will tear down this device
294294
when the reference count reaches 0 */
295295
usbi_disconnect_device(dev);
296+
usb_unref_device(dev);
296297
}
297298
}
298299

@@ -864,11 +865,6 @@ static int process_new_device (struct libusb_context *ctx, io_service_t service)
864865
dev->bus_number = cached_device->location >> 24;
865866
dev->device_address = cached_device->address;
866867

867-
/* need to add a reference to the parent device */
868-
if (dev->parent_dev) {
869-
libusb_ref_device(dev->parent_dev);
870-
}
871-
872868
(*(priv->dev->device))->GetDeviceSpeed (priv->dev->device, &devSpeed);
873869

874870
switch (devSpeed) {

libusb/os/linux_usbfs.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1048,9 +1048,11 @@ int linux_enumerate_device(struct libusb_context *ctx,
10481048
usbi_dbg("busnum %d devaddr %d session_id %ld", busnum, devaddr,
10491049
session_id);
10501050

1051-
if (usbi_get_device_by_session_id(ctx, session_id)) {
1051+
dev = usbi_get_device_by_session_id(ctx, session_id);
1052+
if (dev) {
10521053
/* device already exists in the context */
10531054
usbi_dbg("session_id %ld already exists", session_id);
1055+
libusb_unref_device(dev);
10541056
return LIBUSB_SUCCESS;
10551057
}
10561058

@@ -1101,6 +1103,7 @@ void linux_device_disconnected(uint8_t busnum, uint8_t devaddr, const char *sys_
11011103
dev = usbi_get_device_by_session_id (ctx, session_id);
11021104
if (NULL != dev) {
11031105
usbi_disconnect_device (dev);
1106+
libusb_unref_device(dev);
11041107
} else {
11051108
usbi_dbg("device not found for session %x", session_id);
11061109
}

libusb/os/netbsd_usb.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,7 @@ netbsd_get_device_list(struct libusb_context * ctx,
161161
session_id = (di.udi_bus << 8 | di.udi_addr);
162162
dev = usbi_get_device_by_session_id(ctx, session_id);
163163

164-
if (dev) {
165-
dev = libusb_ref_device(dev);
166-
} else {
164+
if (dev == NULL) {
167165
dev = usbi_alloc_device(ctx, session_id);
168166
if (dev == NULL)
169167
return (LIBUSB_ERROR_NO_MEM);

libusb/os/openbsd_usb.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,7 @@ obsd_get_device_list(struct libusb_context * ctx,
186186
session_id = (di.udi_bus << 8 | di.udi_addr);
187187
dev = usbi_get_device_by_session_id(ctx, session_id);
188188

189-
if (dev) {
190-
dev = libusb_ref_device(dev);
191-
} else {
189+
if (dev == NULL) {
192190
dev = usbi_alloc_device(ctx, session_id);
193191
if (dev == NULL) {
194192
close(fd);

libusb/os/wince_usb.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,6 @@ static int wince_get_device_list(
372372
if (dev) {
373373
usbi_dbg("using existing device for %d/%d (session %ld)",
374374
bus_addr, dev_addr, session_id);
375-
libusb_ref_device(dev);
376375
// Release just this element in the device list (as we already hold a
377376
// reference to it).
378377
UkwReleaseDeviceList(driver_handle, &devices[i], 1);

libusb/os/windows_usb.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,8 +1103,10 @@ static int init_device(struct libusb_device* dev, struct libusb_device* parent_d
11031103
if (tmp_dev->bus_number != 0) {
11041104
usbi_dbg("got bus number from ancestor #%d", i);
11051105
parent_dev->bus_number = tmp_dev->bus_number;
1106+
libusb_unref_device(tmp_dev);
11061107
break;
11071108
}
1109+
libusb_unref_device(tmp_dev);
11081110
}
11091111
}
11101112
if (parent_dev->bus_number == 0) {
@@ -1116,7 +1118,7 @@ static int init_device(struct libusb_device* dev, struct libusb_device* parent_d
11161118
dev->port_number = port_number;
11171119
priv->depth = parent_priv->depth + 1;
11181120
priv->parent_dev = parent_dev;
1119-
dev->parent_dev = libusb_ref_device(parent_dev);
1121+
dev->parent_dev = parent_dev;
11201122

11211123
// If the device address is already set, we can stop here
11221124
if (dev->device_address != 0) {
@@ -1537,6 +1539,7 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered
15371539
parent_priv = _device_priv(parent_dev);
15381540
// virtual USB devices are also listed during GEN - don't process these yet
15391541
if ( (pass == GEN_PASS) && (parent_priv->apib->id != USB_API_HUB) ) {
1542+
libusb_unref_device(parent_dev);
15401543
continue;
15411544
}
15421545
break;
@@ -1559,20 +1562,20 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered
15591562
LOOP_BREAK(LIBUSB_ERROR_NO_MEM);
15601563
}
15611564
windows_device_priv_init(dev);
1562-
// Keep track of devices that need unref
1563-
unref_list[unref_cur++] = dev;
1564-
if (unref_cur >= unref_size) {
1565-
unref_size += 64;
1566-
unref_list = usbi_reallocf(unref_list, unref_size*sizeof(libusb_device*));
1567-
if (unref_list == NULL) {
1568-
usbi_err(ctx, "could not realloc list for unref - aborting.");
1569-
LOOP_BREAK(LIBUSB_ERROR_NO_MEM);
1570-
}
1571-
}
15721565
} else {
15731566
usbi_dbg("found existing device for session [%X] (%d.%d)",
15741567
session_id, dev->bus_number, dev->device_address);
15751568
}
1569+
// Keep track of devices that need unref
1570+
unref_list[unref_cur++] = dev;
1571+
if (unref_cur >= unref_size) {
1572+
unref_size += 64;
1573+
unref_list = usbi_reallocf(unref_list, unref_size*sizeof(libusb_device*));
1574+
if (unref_list == NULL) {
1575+
usbi_err(ctx, "could not realloc list for unref - aborting.");
1576+
LOOP_BREAK(LIBUSB_ERROR_NO_MEM);
1577+
}
1578+
}
15761579
priv = _device_priv(dev);
15771580
}
15781581

@@ -1658,6 +1661,7 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered
16581661
break;
16591662
}
16601663
}
1664+
libusb_unref_device(parent_dev);
16611665
break;
16621666
}
16631667
}

libusb/version_nano.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
#define LIBUSB_NANO 10833
1+
#define LIBUSB_NANO 10834

0 commit comments

Comments
 (0)