1 /* $FreeBSD$ */
2 /*-
3 * Copyright (c) 2016-2019 Hans Petter Selasky. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 */
26
27 #ifdef LIBUSB_GLOBAL_INCLUDE_FILE
28 #include LIBUSB_GLOBAL_INCLUDE_FILE
29 #else
30 #include <assert.h>
31 #include <errno.h>
32 #include <poll.h>
33 #include <pthread.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <time.h>
39 #include <sys/fcntl.h>
40 #include <sys/ioctl.h>
41 #include <sys/queue.h>
42 #include <sys/endian.h>
43 #endif
44
45 #define libusb_device_handle libusb20_device
46
47 #include "libusb20.h"
48 #include "libusb20_desc.h"
49 #include "libusb20_int.h"
50 #include "libusb.h"
51 #include "libusb10.h"
52
53 static int
libusb_hotplug_equal(libusb_device * _adev,libusb_device * _bdev)54 libusb_hotplug_equal(libusb_device *_adev, libusb_device *_bdev)
55 {
56 struct libusb20_device *adev = _adev->os_priv;
57 struct libusb20_device *bdev = _bdev->os_priv;
58
59 if (adev->bus_number != bdev->bus_number)
60 return (0);
61 if (adev->device_address != bdev->device_address)
62 return (0);
63 if (memcmp(&adev->ddesc, &bdev->ddesc, sizeof(adev->ddesc)))
64 return (0);
65 if (memcmp(&adev->session_data, &bdev->session_data, sizeof(adev->session_data)))
66 return (0);
67 return (1);
68 }
69
70 static int
libusb_hotplug_filter(libusb_context * ctx,libusb_hotplug_callback_handle pcbh,libusb_device * dev,libusb_hotplug_event event)71 libusb_hotplug_filter(libusb_context *ctx, libusb_hotplug_callback_handle pcbh,
72 libusb_device *dev, libusb_hotplug_event event)
73 {
74 if (!(pcbh->events & event))
75 return (0);
76 if (pcbh->vendor != LIBUSB_HOTPLUG_MATCH_ANY &&
77 pcbh->vendor != libusb20_dev_get_device_desc(dev->os_priv)->idVendor)
78 return (0);
79 if (pcbh->product != LIBUSB_HOTPLUG_MATCH_ANY &&
80 pcbh->product != libusb20_dev_get_device_desc(dev->os_priv)->idProduct)
81 return (0);
82 if (pcbh->devclass != LIBUSB_HOTPLUG_MATCH_ANY &&
83 pcbh->devclass != libusb20_dev_get_device_desc(dev->os_priv)->bDeviceClass)
84 return (0);
85 return (pcbh->fn(ctx, dev, event, pcbh->user_data));
86 }
87
88 static int
libusb_hotplug_enumerate(libusb_context * ctx,struct libusb_device_head * phead)89 libusb_hotplug_enumerate(libusb_context *ctx, struct libusb_device_head *phead)
90 {
91 libusb_device **ppdev;
92 ssize_t count;
93 ssize_t x;
94
95 count = libusb_get_device_list(ctx, &ppdev);
96 if (count < 0)
97 return (-1);
98
99 for (x = 0; x != count; x++)
100 TAILQ_INSERT_TAIL(phead, ppdev[x], hotplug_entry);
101
102 libusb_free_device_list(ppdev, 0);
103 return (0);
104 }
105
106 static void *
libusb_hotplug_scan(void * arg)107 libusb_hotplug_scan(void *arg)
108 {
109 struct libusb_device_head hotplug_devs;
110 libusb_hotplug_callback_handle acbh;
111 libusb_hotplug_callback_handle bcbh;
112 libusb_context *ctx = arg;
113 libusb_device *temp;
114 libusb_device *adev;
115 libusb_device *bdev;
116 unsigned do_loop = 1;
117
118 while (do_loop) {
119 usleep(4000000);
120
121 HOTPLUG_LOCK(ctx);
122
123 TAILQ_INIT(&hotplug_devs);
124
125 if (ctx->hotplug_handler != NO_THREAD) {
126 if (libusb_hotplug_enumerate(ctx, &hotplug_devs) < 0) {
127 HOTPLUG_UNLOCK(ctx);
128 continue;
129 }
130 } else {
131 do_loop = 0;
132 }
133
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))
138 break;
139 }
140 if (bdev == NULL) {
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)
145 continue;
146 TAILQ_REMOVE(&ctx->hotplug_cbh, acbh, entry);
147 free(acbh);
148 }
149 libusb_unref_device(adev);
150 }
151 }
152
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))
157 break;
158 }
159 if (bdev == NULL) {
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)
165 continue;
166 TAILQ_REMOVE(&ctx->hotplug_cbh, acbh, entry);
167 free(acbh);
168 }
169 }
170 }
171 HOTPLUG_UNLOCK(ctx);
172
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);
177 }
178 }
179 return (NULL);
180 }
181
libusb_hotplug_register_callback(libusb_context * ctx,libusb_hotplug_event events,libusb_hotplug_flag flags,int vendor_id,int product_id,int dev_class,libusb_hotplug_callback_fn cb_fn,void * user_data,libusb_hotplug_callback_handle * phandle)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)
187 {
188 libusb_hotplug_callback_handle handle;
189 struct libusb_device *adev;
190
191 ctx = GET_CONTEXT(ctx);
192
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);
198
199 handle = malloc(sizeof(*handle));
200 if (handle == NULL)
201 return (LIBUSB_ERROR_NO_MEM);
202
203 HOTPLUG_LOCK(ctx);
204 if (ctx->hotplug_handler == NO_THREAD) {
205 libusb_hotplug_enumerate(ctx, &ctx->hotplug_devs);
206
207 if (pthread_create(&ctx->hotplug_handler, NULL,
208 &libusb_hotplug_scan, ctx) != 0)
209 ctx->hotplug_handler = NO_THREAD;
210 }
211 handle->events = events;
212 handle->vendor = vendor_id;
213 handle->product = product_id;
214 handle->devclass = dev_class;
215 handle->fn = cb_fn;
216 handle->user_data = user_data;
217
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)
222 continue;
223 free(handle);
224 handle = NULL;
225 break;
226 }
227 }
228 if (handle != NULL)
229 TAILQ_INSERT_TAIL(&ctx->hotplug_cbh, handle, entry);
230 HOTPLUG_UNLOCK(ctx);
231
232 if (phandle != NULL)
233 *phandle = handle;
234 return (LIBUSB_SUCCESS);
235 }
236
libusb_hotplug_deregister_callback(libusb_context * ctx,libusb_hotplug_callback_handle handle)237 void libusb_hotplug_deregister_callback(libusb_context *ctx,
238 libusb_hotplug_callback_handle handle)
239 {
240 ctx = GET_CONTEXT(ctx);
241
242 if (ctx == NULL || handle == NULL)
243 return;
244
245 HOTPLUG_LOCK(ctx);
246 TAILQ_REMOVE(&ctx->hotplug_cbh, handle, entry);
247 HOTPLUG_UNLOCK(ctx);
248
249 free(handle);
250 }
251