3 * Copyright (c) 2016 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_scan(void *arg
)
91 TAILQ_HEAD(, libusb_device
) hotplug_devs
;
92 libusb_hotplug_callback_handle acbh
;
93 libusb_hotplug_callback_handle bcbh
;
94 libusb_context
*ctx
= arg
;
95 libusb_device
**ppdev
;
108 TAILQ_INIT(&hotplug_devs
);
110 if (ctx
->hotplug_handler
!= NO_THREAD
) {
111 count
= libusb_get_device_list(ctx
, &ppdev
);
114 for (x
= 0; x
!= count
; x
++) {
115 TAILQ_INSERT_TAIL(&hotplug_devs
, ppdev
[x
],
118 libusb_free_device_list(ppdev
, 0);
123 /* figure out which devices are gone */
124 TAILQ_FOREACH_MUTABLE(adev
, &ctx
->hotplug_devs
, hotplug_entry
, temp
) {
125 TAILQ_FOREACH(bdev
, &hotplug_devs
, hotplug_entry
) {
126 if (libusb_hotplug_equal(adev
, bdev
))
130 TAILQ_REMOVE(&ctx
->hotplug_devs
, adev
, hotplug_entry
);
131 TAILQ_FOREACH_MUTABLE(acbh
, &ctx
->hotplug_cbh
, entry
, bcbh
) {
132 if (libusb_hotplug_filter(ctx
, acbh
, adev
,
133 LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT
) == 0)
135 TAILQ_REMOVE(&ctx
->hotplug_cbh
, acbh
, entry
);
138 libusb_unref_device(adev
);
142 /* figure out which devices are new */
143 TAILQ_FOREACH_MUTABLE(adev
, &hotplug_devs
, hotplug_entry
, temp
) {
144 TAILQ_FOREACH(bdev
, &ctx
->hotplug_devs
, hotplug_entry
) {
145 if (libusb_hotplug_equal(adev
, bdev
))
149 TAILQ_REMOVE(&hotplug_devs
, adev
, hotplug_entry
);
150 TAILQ_INSERT_TAIL(&ctx
->hotplug_devs
, adev
, hotplug_entry
);
151 TAILQ_FOREACH_MUTABLE(acbh
, &ctx
->hotplug_cbh
, entry
, bcbh
) {
152 if (libusb_hotplug_filter(ctx
, acbh
, adev
,
153 LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED
) == 0)
155 TAILQ_REMOVE(&ctx
->hotplug_cbh
, acbh
, entry
);
162 /* unref remaining devices */
163 while ((adev
= TAILQ_FIRST(&hotplug_devs
)) != NULL
) {
164 TAILQ_REMOVE(&hotplug_devs
, adev
, hotplug_entry
);
165 libusb_unref_device(adev
);
171 int libusb_hotplug_register_callback(libusb_context
*ctx
,
172 libusb_hotplug_event events
, libusb_hotplug_flag flags
,
173 int vendor_id
, int product_id
, int dev_class
,
174 libusb_hotplug_callback_fn cb_fn
, void *user_data
,
175 libusb_hotplug_callback_handle
*phandle
)
177 libusb_hotplug_callback_handle handle
;
178 struct libusb_device
*adev
;
180 ctx
= GET_CONTEXT(ctx
);
182 if (ctx
== NULL
|| cb_fn
== NULL
|| events
== 0 ||
183 vendor_id
< -1 || vendor_id
> 0xffff ||
184 product_id
< -1 || product_id
> 0xffff ||
185 dev_class
< -1 || dev_class
> 0xff)
186 return (LIBUSB_ERROR_INVALID_PARAM
);
188 handle
= malloc(sizeof(*handle
));
190 return (LIBUSB_ERROR_NO_MEM
);
193 if (ctx
->hotplug_handler
== NO_THREAD
) {
194 if (pthread_create(&ctx
->hotplug_handler
, NULL
,
195 &libusb_hotplug_scan
, ctx
) != 0)
196 ctx
->hotplug_handler
= NO_THREAD
;
198 handle
->events
= events
;
199 handle
->vendor
= vendor_id
;
200 handle
->product
= product_id
;
201 handle
->devclass
= dev_class
;
203 handle
->user_data
= user_data
;
205 if (flags
& LIBUSB_HOTPLUG_ENUMERATE
) {
206 TAILQ_FOREACH(adev
, &ctx
->hotplug_devs
, hotplug_entry
) {
207 if (libusb_hotplug_filter(ctx
, handle
, adev
,
208 LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED
) == 0)
216 TAILQ_INSERT_TAIL(&ctx
->hotplug_cbh
, handle
, entry
);
221 return (LIBUSB_SUCCESS
);
224 void libusb_hotplug_deregister_callback(libusb_context
*ctx
,
225 libusb_hotplug_callback_handle handle
)
227 ctx
= GET_CONTEXT(ctx
);
229 if (ctx
== NULL
|| handle
== NULL
)
233 TAILQ_REMOVE(&ctx
->hotplug_cbh
, handle
, entry
);