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