xref: /freebsd/lib/libusb/libusb10_hotplug.c (revision dd21556857e8d40f66bf5ad54754d9d52669ebf7)
1 /*-
2  * Copyright (c) 2016-2019 Hans Petter Selasky. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #ifdef LIBUSB_GLOBAL_INCLUDE_FILE
27 #include LIBUSB_GLOBAL_INCLUDE_FILE
28 #else
29 #include <assert.h>
30 #include <errno.h>
31 #include <poll.h>
32 #include <pthread.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <time.h>
38 #include <sys/fcntl.h>
39 #include <sys/ioctl.h>
40 #include <sys/queue.h>
41 #include <sys/endian.h>
42 #endif
43 
44 #define	libusb_device_handle libusb20_device
45 
46 #include "libusb20.h"
47 #include "libusb20_desc.h"
48 #include "libusb20_int.h"
49 #include "libusb.h"
50 #include "libusb10.h"
51 
52 static int
53 libusb_hotplug_equal(libusb_device *_adev, libusb_device *_bdev)
54 {
55 	struct libusb20_device *adev = _adev->os_priv;
56 	struct libusb20_device *bdev = _bdev->os_priv;
57 
58 	if (adev->bus_number != bdev->bus_number)
59 		return (0);
60 	if (adev->device_address != bdev->device_address)
61 		return (0);
62 	if (memcmp(&adev->ddesc, &bdev->ddesc, sizeof(adev->ddesc)))
63 		return (0);
64 	if (memcmp(&adev->session_data, &bdev->session_data, sizeof(adev->session_data)))
65 		return (0);
66 	return (1);
67 }
68 
69 static int
70 libusb_hotplug_filter(libusb_context *ctx, libusb_hotplug_callback_handle pcbh,
71     libusb_device *dev, libusb_hotplug_event event)
72 {
73 	if (!(pcbh->events & event))
74 		return (0);
75 	if (pcbh->vendor != LIBUSB_HOTPLUG_MATCH_ANY &&
76 	    pcbh->vendor != libusb20_dev_get_device_desc(dev->os_priv)->idVendor)
77 		return (0);
78 	if (pcbh->product != LIBUSB_HOTPLUG_MATCH_ANY &&
79 	    pcbh->product != libusb20_dev_get_device_desc(dev->os_priv)->idProduct)
80 		return (0);
81 	if (pcbh->devclass != LIBUSB_HOTPLUG_MATCH_ANY &&
82 	    pcbh->devclass != libusb20_dev_get_device_desc(dev->os_priv)->bDeviceClass)
83 		return (0);
84 	return (pcbh->fn(ctx, dev, event, pcbh->user_data));
85 }
86 
87 static int
88 libusb_hotplug_enumerate(libusb_context *ctx, struct libusb_device_head *phead)
89 {
90 	libusb_device **ppdev;
91 	ssize_t count;
92 	ssize_t x;
93 
94 	count = libusb_get_device_list(ctx, &ppdev);
95 	if (count < 0)
96 		return (-1);
97 
98 	for (x = 0; x != count; x++)
99 		TAILQ_INSERT_TAIL(phead, ppdev[x], hotplug_entry);
100 
101 	libusb_free_device_list(ppdev, 0);
102 	return (0);
103 }
104 
105 static void *
106 libusb_hotplug_scan(void *arg)
107 {
108 	struct libusb_device_head hotplug_devs;
109 	libusb_hotplug_callback_handle acbh;
110 	libusb_hotplug_callback_handle bcbh;
111 	libusb_context *ctx = arg;
112 	libusb_device *temp;
113 	libusb_device *adev;
114 	libusb_device *bdev;
115 
116 	for (;;) {
117 		usleep(4000000);
118 
119 		HOTPLUG_LOCK(ctx);
120 		if (ctx->hotplug_handler == NO_THREAD) {
121 			while ((adev = TAILQ_FIRST(&ctx->hotplug_devs)) != NULL) {
122 				TAILQ_REMOVE(&ctx->hotplug_devs, adev, hotplug_entry);
123 				libusb_unref_device(adev);
124 			}
125 			HOTPLUG_UNLOCK(ctx);
126 			break;
127 		}
128 
129 		TAILQ_INIT(&hotplug_devs);
130 
131 		if (libusb_hotplug_enumerate(ctx, &hotplug_devs) < 0) {
132 			HOTPLUG_UNLOCK(ctx);
133 			continue;
134 		}
135 
136 		/* figure out which devices are gone */
137 		TAILQ_FOREACH_SAFE(adev, &ctx->hotplug_devs, hotplug_entry, temp) {
138 			TAILQ_FOREACH(bdev, &hotplug_devs, hotplug_entry) {
139 				if (libusb_hotplug_equal(adev, bdev))
140 					break;
141 			}
142 			if (bdev == NULL) {
143 				TAILQ_REMOVE(&ctx->hotplug_devs, adev, hotplug_entry);
144 				TAILQ_FOREACH_SAFE(acbh, &ctx->hotplug_cbh, entry, bcbh) {
145 					if (libusb_hotplug_filter(ctx, acbh, adev,
146 					    LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT) == 0)
147 						continue;
148 					TAILQ_REMOVE(&ctx->hotplug_cbh, acbh, entry);
149 					free(acbh);
150 				}
151 				libusb_unref_device(adev);
152 			}
153 		}
154 
155 		/* figure out which devices are new */
156 		TAILQ_FOREACH_SAFE(adev, &hotplug_devs, hotplug_entry, temp) {
157 			TAILQ_FOREACH(bdev, &ctx->hotplug_devs, hotplug_entry) {
158 				if (libusb_hotplug_equal(adev, bdev))
159 					break;
160 			}
161 			if (bdev == NULL) {
162 				TAILQ_REMOVE(&hotplug_devs, adev, hotplug_entry);
163 				TAILQ_INSERT_TAIL(&ctx->hotplug_devs, adev, hotplug_entry);
164 				TAILQ_FOREACH_SAFE(acbh, &ctx->hotplug_cbh, entry, bcbh) {
165 					if (libusb_hotplug_filter(ctx, acbh, adev,
166 					    LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED) == 0)
167 						continue;
168 					TAILQ_REMOVE(&ctx->hotplug_cbh, acbh, entry);
169 					free(acbh);
170 				}
171 			}
172 		}
173 		HOTPLUG_UNLOCK(ctx);
174 
175 		/* unref remaining devices */
176 		while ((adev = TAILQ_FIRST(&hotplug_devs)) != NULL) {
177 			TAILQ_REMOVE(&hotplug_devs, adev, hotplug_entry);
178 			libusb_unref_device(adev);
179 		}
180 	}
181 	return (NULL);
182 }
183 
184 int libusb_hotplug_register_callback(libusb_context *ctx,
185     libusb_hotplug_event events, libusb_hotplug_flag flags,
186     int vendor_id, int product_id, int dev_class,
187     libusb_hotplug_callback_fn cb_fn, void *user_data,
188     libusb_hotplug_callback_handle *phandle)
189 {
190 	libusb_hotplug_callback_handle handle;
191 	struct libusb_device *adev;
192 
193 	ctx = GET_CONTEXT(ctx);
194 
195 	if (ctx == NULL || cb_fn == NULL || events == 0 ||
196 	    vendor_id < -1 || vendor_id > 0xffff ||
197 	    product_id < -1 || product_id > 0xffff ||
198 	    dev_class < -1 || dev_class > 0xff)
199 		return (LIBUSB_ERROR_INVALID_PARAM);
200 
201 	handle = malloc(sizeof(*handle));
202 	if (handle == NULL)
203 		return (LIBUSB_ERROR_NO_MEM);
204 
205 	HOTPLUG_LOCK(ctx);
206 	if (ctx->hotplug_handler == NO_THREAD) {
207 	  	libusb_hotplug_enumerate(ctx, &ctx->hotplug_devs);
208 
209 		if (pthread_create(&ctx->hotplug_handler, NULL,
210 		    &libusb_hotplug_scan, ctx) != 0)
211 			ctx->hotplug_handler = NO_THREAD;
212 	}
213 	handle->events = events;
214 	handle->vendor = vendor_id;
215 	handle->product = product_id;
216 	handle->devclass = dev_class;
217 	handle->fn = cb_fn;
218 	handle->user_data = user_data;
219 
220 	if (flags & LIBUSB_HOTPLUG_ENUMERATE) {
221 		TAILQ_FOREACH(adev, &ctx->hotplug_devs, hotplug_entry) {
222 			if (libusb_hotplug_filter(ctx, handle, adev,
223 			    LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED) == 0)
224 				continue;
225 			free(handle);
226 			handle = NULL;
227 			break;
228 		}
229 	}
230 	if (handle != NULL)
231 		TAILQ_INSERT_TAIL(&ctx->hotplug_cbh, handle, entry);
232 	HOTPLUG_UNLOCK(ctx);
233 
234 	if (phandle != NULL)
235 		*phandle = handle;
236 	return (LIBUSB_SUCCESS);
237 }
238 
239 void libusb_hotplug_deregister_callback(libusb_context *ctx,
240     libusb_hotplug_callback_handle handle)
241 {
242   	ctx = GET_CONTEXT(ctx);
243 
244 	if (ctx == NULL || handle == NULL)
245 		return;
246 
247 	HOTPLUG_LOCK(ctx);
248 	TAILQ_REMOVE(&ctx->hotplug_cbh, handle, entry);
249 	HOTPLUG_UNLOCK(ctx);
250 
251 	free(handle);
252 }
253