3 * Copyright (c) 2016-2019 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #ifdef LIBUSB_GLOBAL_INCLUDE_FILE
28 #include LIBUSB_GLOBAL_INCLUDE_FILE
39 #include <sys/fcntl.h>
40 #include <sys/ioctl.h>
41 #include <sys/queue.h>
42 #include <sys/endian.h>
45 #define libusb_device_handle libusb20_device
48 #include "libusb20_desc.h"
49 #include "libusb20_int.h"
54 libusb_hotplug_equal(libusb_device *_adev, libusb_device *_bdev)
56 struct libusb20_device *adev = _adev->os_priv;
57 struct libusb20_device *bdev = _bdev->os_priv;
59 if (adev->bus_number != bdev->bus_number)
61 if (adev->device_address != bdev->device_address)
63 if (memcmp(&adev->ddesc, &bdev->ddesc, sizeof(adev->ddesc)))
65 if (memcmp(&adev->session_data, &bdev->session_data, sizeof(adev->session_data)))
71 libusb_hotplug_filter(libusb_context *ctx, libusb_hotplug_callback_handle pcbh,
72 libusb_device *dev, libusb_hotplug_event event)
74 if (!(pcbh->events & event))
76 if (pcbh->vendor != LIBUSB_HOTPLUG_MATCH_ANY &&
77 pcbh->vendor != libusb20_dev_get_device_desc(dev->os_priv)->idVendor)
79 if (pcbh->product != LIBUSB_HOTPLUG_MATCH_ANY &&
80 pcbh->product != libusb20_dev_get_device_desc(dev->os_priv)->idProduct)
82 if (pcbh->devclass != LIBUSB_HOTPLUG_MATCH_ANY &&
83 pcbh->devclass != libusb20_dev_get_device_desc(dev->os_priv)->bDeviceClass)
85 return (pcbh->fn(ctx, dev, event, pcbh->user_data));
89 libusb_hotplug_enumerate(libusb_context *ctx, struct libusb_device_head *phead)
91 libusb_device **ppdev;
95 count = libusb_get_device_list(ctx, &ppdev);
99 for (x = 0; x != count; x++)
100 TAILQ_INSERT_TAIL(phead, ppdev[x], hotplug_entry);
102 libusb_free_device_list(ppdev, 0);
107 libusb_hotplug_scan(void *arg)
109 struct libusb_device_head hotplug_devs;
110 libusb_hotplug_callback_handle acbh;
111 libusb_hotplug_callback_handle bcbh;
112 libusb_context *ctx = arg;
116 unsigned do_loop = 1;
123 TAILQ_INIT(&hotplug_devs);
125 if (ctx->hotplug_handler != NO_THREAD) {
126 if (libusb_hotplug_enumerate(ctx, &hotplug_devs) < 0) {
134 /* figure out which devices are gone */
135 TAILQ_FOREACH_SAFE(adev, &ctx->hotplug_devs, hotplug_entry, temp) {
136 TAILQ_FOREACH(bdev, &hotplug_devs, hotplug_entry) {
137 if (libusb_hotplug_equal(adev, bdev))
141 TAILQ_REMOVE(&ctx->hotplug_devs, adev, hotplug_entry);
142 TAILQ_FOREACH_SAFE(acbh, &ctx->hotplug_cbh, entry, bcbh) {
143 if (libusb_hotplug_filter(ctx, acbh, adev,
144 LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT) == 0)
146 TAILQ_REMOVE(&ctx->hotplug_cbh, acbh, entry);
149 libusb_unref_device(adev);
153 /* figure out which devices are new */
154 TAILQ_FOREACH_SAFE(adev, &hotplug_devs, hotplug_entry, temp) {
155 TAILQ_FOREACH(bdev, &ctx->hotplug_devs, hotplug_entry) {
156 if (libusb_hotplug_equal(adev, bdev))
160 TAILQ_REMOVE(&hotplug_devs, adev, hotplug_entry);
161 TAILQ_INSERT_TAIL(&ctx->hotplug_devs, adev, hotplug_entry);
162 TAILQ_FOREACH_SAFE(acbh, &ctx->hotplug_cbh, entry, bcbh) {
163 if (libusb_hotplug_filter(ctx, acbh, adev,
164 LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED) == 0)
166 TAILQ_REMOVE(&ctx->hotplug_cbh, acbh, entry);
173 /* unref remaining devices */
174 while ((adev = TAILQ_FIRST(&hotplug_devs)) != NULL) {
175 TAILQ_REMOVE(&hotplug_devs, adev, hotplug_entry);
176 libusb_unref_device(adev);
182 int libusb_hotplug_register_callback(libusb_context *ctx,
183 libusb_hotplug_event events, libusb_hotplug_flag flags,
184 int vendor_id, int product_id, int dev_class,
185 libusb_hotplug_callback_fn cb_fn, void *user_data,
186 libusb_hotplug_callback_handle *phandle)
188 libusb_hotplug_callback_handle handle;
189 struct libusb_device *adev;
191 ctx = GET_CONTEXT(ctx);
193 if (ctx == NULL || cb_fn == NULL || events == 0 ||
194 vendor_id < -1 || vendor_id > 0xffff ||
195 product_id < -1 || product_id > 0xffff ||
196 dev_class < -1 || dev_class > 0xff)
197 return (LIBUSB_ERROR_INVALID_PARAM);
199 handle = malloc(sizeof(*handle));
201 return (LIBUSB_ERROR_NO_MEM);
204 if (ctx->hotplug_handler == NO_THREAD) {
205 libusb_hotplug_enumerate(ctx, &ctx->hotplug_devs);
207 if (pthread_create(&ctx->hotplug_handler, NULL,
208 &libusb_hotplug_scan, ctx) != 0)
209 ctx->hotplug_handler = NO_THREAD;
211 handle->events = events;
212 handle->vendor = vendor_id;
213 handle->product = product_id;
214 handle->devclass = dev_class;
216 handle->user_data = user_data;
218 if (flags & LIBUSB_HOTPLUG_ENUMERATE) {
219 TAILQ_FOREACH(adev, &ctx->hotplug_devs, hotplug_entry) {
220 if (libusb_hotplug_filter(ctx, handle, adev,
221 LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED) == 0)
229 TAILQ_INSERT_TAIL(&ctx->hotplug_cbh, handle, entry);
234 return (LIBUSB_SUCCESS);
237 void libusb_hotplug_deregister_callback(libusb_context *ctx,
238 libusb_hotplug_callback_handle handle)
240 ctx = GET_CONTEXT(ctx);
242 if (ctx == NULL || handle == NULL)
246 TAILQ_REMOVE(&ctx->hotplug_cbh, handle, entry);