libusb10_hotplug.c revision 349669
1/* $FreeBSD: stable/10/lib/libusb/libusb10_hotplug.c 349669 2019-07-03 18:26:07Z hselasky $ */
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
53static int
54libusb_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
70static int
71libusb_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
88static int
89libusb_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
106static void *
107libusb_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				continue;
128		} else {
129			do_loop = 0;
130		}
131
132		/* figure out which devices are gone */
133		TAILQ_FOREACH_SAFE(adev, &ctx->hotplug_devs, hotplug_entry, temp) {
134			TAILQ_FOREACH(bdev, &hotplug_devs, hotplug_entry) {
135				if (libusb_hotplug_equal(adev, bdev))
136					break;
137			}
138			if (bdev == NULL) {
139				TAILQ_REMOVE(&ctx->hotplug_devs, adev, hotplug_entry);
140				TAILQ_FOREACH_SAFE(acbh, &ctx->hotplug_cbh, entry, bcbh) {
141					if (libusb_hotplug_filter(ctx, acbh, adev,
142					    LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT) == 0)
143						continue;
144					TAILQ_REMOVE(&ctx->hotplug_cbh, acbh, entry);
145					free(acbh);
146				}
147				libusb_unref_device(adev);
148			}
149		}
150
151		/* figure out which devices are new */
152		TAILQ_FOREACH_SAFE(adev, &hotplug_devs, hotplug_entry, temp) {
153			TAILQ_FOREACH(bdev, &ctx->hotplug_devs, hotplug_entry) {
154				if (libusb_hotplug_equal(adev, bdev))
155					break;
156			}
157			if (bdev == NULL) {
158				TAILQ_REMOVE(&hotplug_devs, adev, hotplug_entry);
159				TAILQ_INSERT_TAIL(&ctx->hotplug_devs, adev, hotplug_entry);
160				TAILQ_FOREACH_SAFE(acbh, &ctx->hotplug_cbh, entry, bcbh) {
161					if (libusb_hotplug_filter(ctx, acbh, adev,
162					    LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED) == 0)
163						continue;
164					TAILQ_REMOVE(&ctx->hotplug_cbh, acbh, entry);
165					free(acbh);
166				}
167			}
168		}
169		HOTPLUG_UNLOCK(ctx);
170
171		/* unref remaining devices */
172		while ((adev = TAILQ_FIRST(&hotplug_devs)) != NULL) {
173			TAILQ_REMOVE(&hotplug_devs, adev, hotplug_entry);
174			libusb_unref_device(adev);
175		}
176	}
177	return (NULL);
178}
179
180int libusb_hotplug_register_callback(libusb_context *ctx,
181    libusb_hotplug_event events, libusb_hotplug_flag flags,
182    int vendor_id, int product_id, int dev_class,
183    libusb_hotplug_callback_fn cb_fn, void *user_data,
184    libusb_hotplug_callback_handle *phandle)
185{
186	libusb_hotplug_callback_handle handle;
187	struct libusb_device *adev;
188
189	ctx = GET_CONTEXT(ctx);
190
191	if (ctx == NULL || cb_fn == NULL || events == 0 ||
192	    vendor_id < -1 || vendor_id > 0xffff ||
193	    product_id < -1 || product_id > 0xffff ||
194	    dev_class < -1 || dev_class > 0xff)
195		return (LIBUSB_ERROR_INVALID_PARAM);
196
197	handle = malloc(sizeof(*handle));
198	if (handle == NULL)
199		return (LIBUSB_ERROR_NO_MEM);
200
201	HOTPLUG_LOCK(ctx);
202	if (ctx->hotplug_handler == NO_THREAD) {
203	  	libusb_hotplug_enumerate(ctx, &ctx->hotplug_devs);
204
205		if (pthread_create(&ctx->hotplug_handler, NULL,
206		    &libusb_hotplug_scan, ctx) != 0)
207			ctx->hotplug_handler = NO_THREAD;
208	}
209	handle->events = events;
210	handle->vendor = vendor_id;
211	handle->product = product_id;
212	handle->devclass = dev_class;
213	handle->fn = cb_fn;
214	handle->user_data = user_data;
215
216	if (flags & LIBUSB_HOTPLUG_ENUMERATE) {
217		TAILQ_FOREACH(adev, &ctx->hotplug_devs, hotplug_entry) {
218			if (libusb_hotplug_filter(ctx, handle, adev,
219			    LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED) == 0)
220				continue;
221			free(handle);
222			handle = NULL;
223			break;
224		}
225	}
226	if (handle != NULL)
227		TAILQ_INSERT_TAIL(&ctx->hotplug_cbh, handle, entry);
228	HOTPLUG_UNLOCK(ctx);
229
230	if (phandle != NULL)
231		*phandle = handle;
232	return (LIBUSB_SUCCESS);
233}
234
235void libusb_hotplug_deregister_callback(libusb_context *ctx,
236    libusb_hotplug_callback_handle handle)
237{
238  	ctx = GET_CONTEXT(ctx);
239
240	if (ctx == NULL || handle == NULL)
241		return;
242
243	HOTPLUG_LOCK(ctx);
244	TAILQ_REMOVE(&ctx->hotplug_cbh, handle, entry);
245	HOTPLUG_UNLOCK(ctx);
246
247	free(handle);
248}
249