xref: /dragonfly/lib/libusb/libusb10_hotplug.c (revision 7bcb6caf)
1 /* $FreeBSD$ */
2 /*-
3  * Copyright (c) 2016 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
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
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 void *
89 libusb_hotplug_scan(void *arg)
90 {
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;
96 	libusb_device *temp;
97 	libusb_device *adev;
98 	libusb_device *bdev;
99 	unsigned do_loop = 1;
100 	ssize_t count;
101 	ssize_t x;
102 
103 	while (do_loop) {
104 		usleep(4000000);
105 
106 		HOTPLUG_LOCK(ctx);
107 
108 		TAILQ_INIT(&hotplug_devs);
109 
110 		if (ctx->hotplug_handler != NO_THREAD) {
111 			count = libusb_get_device_list(ctx, &ppdev);
112 			if (count < 0)
113 				continue;
114 			for (x = 0; x != count; x++) {
115 				TAILQ_INSERT_TAIL(&hotplug_devs, ppdev[x],
116 				    hotplug_entry);
117 			}
118 			libusb_free_device_list(ppdev, 0);
119 		} else {
120 			do_loop = 0;
121 		}
122 
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))
127 					break;
128 			}
129 			if (bdev == NULL) {
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)
134 						continue;
135 					TAILQ_REMOVE(&ctx->hotplug_cbh, acbh, entry);
136 					free(acbh);
137 				}
138 				libusb_unref_device(adev);
139 			}
140 		}
141 
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))
146 					break;
147 			}
148 			if (bdev == NULL) {
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)
154 						continue;
155 					TAILQ_REMOVE(&ctx->hotplug_cbh, acbh, entry);
156 					free(acbh);
157 				}
158 			}
159 		}
160 		HOTPLUG_UNLOCK(ctx);
161 
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);
166 		}
167 	}
168 	return (NULL);
169 }
170 
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)
176 {
177 	libusb_hotplug_callback_handle handle;
178 	struct libusb_device *adev;
179 
180 	ctx = GET_CONTEXT(ctx);
181 
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);
187 
188 	handle = malloc(sizeof(*handle));
189 	if (handle == NULL)
190 		return (LIBUSB_ERROR_NO_MEM);
191 
192 	HOTPLUG_LOCK(ctx);
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;
197 	}
198 	handle->events = events;
199 	handle->vendor = vendor_id;
200 	handle->product = product_id;
201 	handle->devclass = dev_class;
202 	handle->fn = cb_fn;
203 	handle->user_data = user_data;
204 
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)
209 				continue;
210 			free(handle);
211 			handle = NULL;
212 			break;
213 		}
214 	}
215 	if (handle != NULL)
216 		TAILQ_INSERT_TAIL(&ctx->hotplug_cbh, handle, entry);
217 	HOTPLUG_UNLOCK(ctx);
218 
219 	if (phandle != NULL)
220 		*phandle = handle;
221 	return (LIBUSB_SUCCESS);
222 }
223 
224 void libusb_hotplug_deregister_callback(libusb_context *ctx,
225     libusb_hotplug_callback_handle handle)
226 {
227 	ctx = GET_CONTEXT(ctx);
228 
229 	if (ctx == NULL || handle == NULL)
230 		return;
231 
232 	HOTPLUG_LOCK(ctx);
233 	TAILQ_REMOVE(&ctx->hotplug_cbh, handle, entry);
234 	HOTPLUG_UNLOCK(ctx);
235 
236 	free(handle);
237 }
238