17801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm/* -*- Mode: C; indent-tabs-mode:t ; c-basic-offset:8 -*- */
27801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm/*
31eff220474f63d7ea7f8f99bef2a3da9da5324ebhjelmn@cs.unm.edu * Hotplug functions for libusb
47801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * Copyright © 2012-2013 Nathan Hjelm <hjelmn@mac.com>
57801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * Copyright © 2012-2013 Peter Stuge <peter@stuge.se>
67801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm *
77801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * This library is free software; you can redistribute it and/or
87801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * modify it under the terms of the GNU Lesser General Public
97801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * License as published by the Free Software Foundation; either
107801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * version 2.1 of the License, or (at your option) any later version.
117801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm *
127801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * This library is distributed in the hope that it will be useful,
137801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * but WITHOUT ANY WARRANTY; without even the implied warranty of
147801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
157801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * Lesser General Public License for more details.
167801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm *
177801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * You should have received a copy of the GNU Lesser General Public
187801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * License along with this library; if not, write to the Free Software
197801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
207801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm */
217801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
227801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm#include <config.h>
237801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
247801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm#include <errno.h>
257801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm#include <stdio.h>
267801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm#include <stdlib.h>
277801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm#include <string.h>
283abc6f3ba50bd6fd82b839f698ca2c7fa34ea066Toby Gray#ifdef HAVE_SYS_TYPES_H
297801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm#include <sys/types.h>
303abc6f3ba50bd6fd82b839f698ca2c7fa34ea066Toby Gray#endif
317801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm#include <assert.h>
327801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
337801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm#include "libusbi.h"
347801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm#include "hotplug.h"
357801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
367801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm/**
377ee92df12adb8015451aaecfeccd7dd62cc2314eChris Dickens * @defgroup libusb_hotplug Device hotplug event notification
38b4c18fac65a594502eec5edd2611d5953e7950f7Pete Batard * This page details how to use the libusb hotplug interface, where available.
39b4c18fac65a594502eec5edd2611d5953e7950f7Pete Batard *
40b4c18fac65a594502eec5edd2611d5953e7950f7Pete Batard * Be mindful that not all platforms currently implement hotplug notification and
41b4c18fac65a594502eec5edd2611d5953e7950f7Pete Batard * that you should first call on \ref libusb_has_capability() with parameter
42b4c18fac65a594502eec5edd2611d5953e7950f7Pete Batard * \ref LIBUSB_CAP_HAS_HOTPLUG to confirm that hotplug support is available.
437801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm *
447ee92df12adb8015451aaecfeccd7dd62cc2314eChris Dickens * \page libusb_hotplug Device hotplug event notification
457801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm *
46de6130dc0d09d1416ca7f5bc6af80c04b2e2f840Ludovic Rousseau * \section hotplug_intro Introduction
477801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm *
481eff220474f63d7ea7f8f99bef2a3da9da5324ebhjelmn@cs.unm.edu * Version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102, has added support
49b4c18fac65a594502eec5edd2611d5953e7950f7Pete Batard * for hotplug events on <b>some</b> platforms (you should test if your platform
50b4c18fac65a594502eec5edd2611d5953e7950f7Pete Batard * supports hotplug notification by calling \ref libusb_has_capability() with
51b4c18fac65a594502eec5edd2611d5953e7950f7Pete Batard * parameter \ref LIBUSB_CAP_HAS_HOTPLUG).
52b4c18fac65a594502eec5edd2611d5953e7950f7Pete Batard *
53b4c18fac65a594502eec5edd2611d5953e7950f7Pete Batard * This interface allows you to request notification for the arrival and departure
54b4c18fac65a594502eec5edd2611d5953e7950f7Pete Batard * of matching USB devices.
557801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm *
567801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * To receive hotplug notification you register a callback by calling
57b4c18fac65a594502eec5edd2611d5953e7950f7Pete Batard * \ref libusb_hotplug_register_callback(). This function will optionally return
588a0c14372f98d56529556f18126a42fda2ab0137Chris Dickens * a callback handle that can be passed to \ref libusb_hotplug_deregister_callback().
597801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm *
607801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * A callback function must return an int (0 or 1) indicating whether the callback is
617801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * expecting additional events. Returning 0 will rearm the callback and 1 will cause
620fc304da83595da9830bf368f66312ffd793f10cHans de Goede * the callback to be deregistered. Note that when callbacks are called from
630fc304da83595da9830bf368f66312ffd793f10cHans de Goede * libusb_hotplug_register_callback() because of the \ref LIBUSB_HOTPLUG_ENUMERATE
640fc304da83595da9830bf368f66312ffd793f10cHans de Goede * flag, the callback return value is ignored, iow you cannot cause a callback
650fc304da83595da9830bf368f66312ffd793f10cHans de Goede * to be deregistered by returning 1 when it is called from
660fc304da83595da9830bf368f66312ffd793f10cHans de Goede * libusb_hotplug_register_callback().
677801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm *
68b4c18fac65a594502eec5edd2611d5953e7950f7Pete Batard * Callbacks for a particular context are automatically deregistered by libusb_exit().
697801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm *
7086031e26ad8c1516ded02a06834312951a7a0492Hans de Goede * As of 1.0.16 there are two supported hotplug events:
717801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm *  - LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED: A device has arrived and is ready to use
727801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm *  - LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT: A device has left and is no longer available
737801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm *
747801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * A hotplug event can listen for either or both of these events.
757801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm *
767801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * Note: If you receive notification that a device has left and you have any
777801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * a libusb_device_handles for the device it is up to you to call libusb_close()
788a0c14372f98d56529556f18126a42fda2ab0137Chris Dickens * on each device handle to free up any remaining resources associated with the device.
797801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * Once a device has left any libusb_device_handle associated with the device
807801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * are invalid and will remain so even if the device comes back.
817801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm *
827801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * When handling a LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED event it is considered
83960a6e7506c4d0d59052056ae2ebfffbe8587175Chris Dickens * safe to call any libusb function that takes a libusb_device. It also safe to
84960a6e7506c4d0d59052056ae2ebfffbe8587175Chris Dickens * open a device and submit asynchronous transfers. However, most other functions
85960a6e7506c4d0d59052056ae2ebfffbe8587175Chris Dickens * that take a libusb_device_handle are <b>not</b> safe to call. Examples of such
867ee92df12adb8015451aaecfeccd7dd62cc2314eChris Dickens * functions are any of the \ref libusb_syncio "synchronous API" functions or the blocking
877ee92df12adb8015451aaecfeccd7dd62cc2314eChris Dickens * functions that retrieve various \ref libusb_desc "USB descriptors". These functions must
88960a6e7506c4d0d59052056ae2ebfffbe8587175Chris Dickens * be used outside of the context of the hotplug callback.
89960a6e7506c4d0d59052056ae2ebfffbe8587175Chris Dickens *
90960a6e7506c4d0d59052056ae2ebfffbe8587175Chris Dickens * When handling a LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT event the only safe function
917801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * is libusb_get_device_descriptor().
927801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm *
937801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * The following code provides an example of the usage of the hotplug interface:
947801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm\code
9593de199d8d60822fc42b1cbf8b920132cd8bbbc6Markus Heidelberg#include <stdio.h>
9693de199d8d60822fc42b1cbf8b920132cd8bbbc6Markus Heidelberg#include <stdlib.h>
971fd08a1513dc323ddf7fae97888eb09215a1f0b6Stefan Tauner#include <time.h>
9893de199d8d60822fc42b1cbf8b920132cd8bbbc6Markus Heidelberg#include <libusb.h>
9993de199d8d60822fc42b1cbf8b920132cd8bbbc6Markus Heidelberg
1007801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelmstatic int count = 0;
1017801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
1027801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelmint hotplug_callback(struct libusb_context *ctx, struct libusb_device *dev,
1037801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm                     libusb_hotplug_event event, void *user_data) {
1048a0c14372f98d56529556f18126a42fda2ab0137Chris Dickens  static libusb_device_handle *dev_handle = NULL;
1057801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm  struct libusb_device_descriptor desc;
1067801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm  int rc;
1077801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
1087801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm  (void)libusb_get_device_descriptor(dev, &desc);
1097801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
1107801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm  if (LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED == event) {
1118a0c14372f98d56529556f18126a42fda2ab0137Chris Dickens    rc = libusb_open(dev, &dev_handle);
1127801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm    if (LIBUSB_SUCCESS != rc) {
1137801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm      printf("Could not open USB device\n");
1147801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm    }
1157801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm  } else if (LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT == event) {
1168a0c14372f98d56529556f18126a42fda2ab0137Chris Dickens    if (dev_handle) {
1178a0c14372f98d56529556f18126a42fda2ab0137Chris Dickens      libusb_close(dev_handle);
1188a0c14372f98d56529556f18126a42fda2ab0137Chris Dickens      dev_handle = NULL;
1197801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm    }
1207801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm  } else {
1217801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm    printf("Unhandled event %d\n", event);
1227801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm  }
1237801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm  count++;
1247801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
1257801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm  return 0;
1267801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm}
1277801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
1287801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelmint main (void) {
1298a0c14372f98d56529556f18126a42fda2ab0137Chris Dickens  libusb_hotplug_callback_handle callback_handle;
1307801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm  int rc;
1317801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
1327801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm  libusb_init(NULL);
1337801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
1347801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm  rc = libusb_hotplug_register_callback(NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED |
1357801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm                                        LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, 0, 0x045a, 0x5005,
1367801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm                                        LIBUSB_HOTPLUG_MATCH_ANY, hotplug_callback, NULL,
1378a0c14372f98d56529556f18126a42fda2ab0137Chris Dickens                                        &callback_handle);
1387801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm  if (LIBUSB_SUCCESS != rc) {
1397801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm    printf("Error creating a hotplug callback\n");
1407801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm    libusb_exit(NULL);
1417801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm    return EXIT_FAILURE;
1427801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm  }
1437801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
1447801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm  while (count < 2) {
14593de199d8d60822fc42b1cbf8b920132cd8bbbc6Markus Heidelberg    libusb_handle_events_completed(NULL, NULL);
1461fd08a1513dc323ddf7fae97888eb09215a1f0b6Stefan Tauner    nanosleep(&(struct timespec){0, 10000000UL}, NULL);
1477801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm  }
1487801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
1498a0c14372f98d56529556f18126a42fda2ab0137Chris Dickens  libusb_hotplug_deregister_callback(NULL, callback_handle);
1507801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm  libusb_exit(NULL);
1517801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
1527801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm  return 0;
1537801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm}
1547801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm\endcode
1557801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm */
1567801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
157f50592979811a00d52305ea02b420cb9cacc99b4Hans de Goedestatic int usbi_hotplug_match_cb (struct libusb_context *ctx,
158f50592979811a00d52305ea02b420cb9cacc99b4Hans de Goede	struct libusb_device *dev, libusb_hotplug_event event,
159f50592979811a00d52305ea02b420cb9cacc99b4Hans de Goede	struct libusb_hotplug_callback *hotplug_cb)
1607801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm{
1617801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	/* Handle lazy deregistration of callback */
1627801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	if (hotplug_cb->needs_free) {
1637801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm		/* Free callback */
1647801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm		return 1;
1657801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	}
1667801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
1677801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	if (!(hotplug_cb->events & event)) {
1687801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm		return 0;
1697801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	}
1707801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
1717801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	if (LIBUSB_HOTPLUG_MATCH_ANY != hotplug_cb->vendor_id &&
1727801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	    hotplug_cb->vendor_id != dev->device_descriptor.idVendor) {
1737801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm		return 0;
1747801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	}
1757801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
1767801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	if (LIBUSB_HOTPLUG_MATCH_ANY != hotplug_cb->product_id &&
1777801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	    hotplug_cb->product_id != dev->device_descriptor.idProduct) {
1787801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm		return 0;
1797801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	}
1807801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
1817801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	if (LIBUSB_HOTPLUG_MATCH_ANY != hotplug_cb->dev_class &&
1827801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	    hotplug_cb->dev_class != dev->device_descriptor.bDeviceClass) {
1837801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm		return 0;
1847801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	}
1857801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
18697958ba756b2f90aa9f65cc7674bc558768dde9fFlorian Albrechtskirchinger	return hotplug_cb->cb (ctx, dev, event, hotplug_cb->user_data);
1877801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm}
1887801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
189f50592979811a00d52305ea02b420cb9cacc99b4Hans de Goedevoid usbi_hotplug_match(struct libusb_context *ctx, struct libusb_device *dev,
190f50592979811a00d52305ea02b420cb9cacc99b4Hans de Goede	libusb_hotplug_event event)
1917801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm{
1927801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	struct libusb_hotplug_callback *hotplug_cb, *next;
1937801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	int ret;
1947801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
1957801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	usbi_mutex_lock(&ctx->hotplug_cbs_lock);
1967801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
1977801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	list_for_each_entry_safe(hotplug_cb, next, &ctx->hotplug_cbs, list, struct libusb_hotplug_callback) {
1987801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm		usbi_mutex_unlock(&ctx->hotplug_cbs_lock);
199f50592979811a00d52305ea02b420cb9cacc99b4Hans de Goede		ret = usbi_hotplug_match_cb (ctx, dev, event, hotplug_cb);
2007801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm		usbi_mutex_lock(&ctx->hotplug_cbs_lock);
2017801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
2027801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm		if (ret) {
2037801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm			list_del(&hotplug_cb->list);
2047801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm			free(hotplug_cb);
2057801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm		}
2067801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	}
2077801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
2087801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	usbi_mutex_unlock(&ctx->hotplug_cbs_lock);
2097801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
210707d500b9fea002f075cf30458a602f28dbd1348Nathan Hjelm	/* the backend is expected to call the callback for each active transfer */
2117801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm}
2127801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
2133b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickensvoid usbi_hotplug_notification(struct libusb_context *ctx, struct libusb_device *dev,
2143b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens	libusb_hotplug_event event)
2153b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens{
216542fd04e021ce8a268c88b5e93ce191569fdc9ebChris Dickens	int pending_events;
2173b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens	libusb_hotplug_message *message = calloc(1, sizeof(*message));
2183b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens
2193b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens	if (!message) {
2203b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens		usbi_err(ctx, "error allocating hotplug message");
2213b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens		return;
2223b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens	}
2233b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens
2243b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens	message->event = event;
2253b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens	message->device = dev;
2263b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens
22789b5407b9aa74790a64b6f32a5b14a96242ba128Chris Dickens	/* Take the event data lock and add this message to the list.
22889b5407b9aa74790a64b6f32a5b14a96242ba128Chris Dickens	 * Only signal an event if there are no prior pending events. */
2293b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens	usbi_mutex_lock(&ctx->event_data_lock);
23089b5407b9aa74790a64b6f32a5b14a96242ba128Chris Dickens	pending_events = usbi_pending_events(ctx);
2313b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens	list_add_tail(&message->list, &ctx->hotplug_msgs);
23289b5407b9aa74790a64b6f32a5b14a96242ba128Chris Dickens	if (!pending_events)
23389b5407b9aa74790a64b6f32a5b14a96242ba128Chris Dickens		usbi_signal_event(ctx);
2343b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens	usbi_mutex_unlock(&ctx->event_data_lock);
2353b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens}
2363b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens
2377801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelmint API_EXPORTED libusb_hotplug_register_callback(libusb_context *ctx,
2387801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	libusb_hotplug_event events, libusb_hotplug_flag flags,
2397801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	int vendor_id, int product_id, int dev_class,
2407801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	libusb_hotplug_callback_fn cb_fn, void *user_data,
2418a0c14372f98d56529556f18126a42fda2ab0137Chris Dickens	libusb_hotplug_callback_handle *callback_handle)
2427801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm{
2437801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	libusb_hotplug_callback *new_callback;
2447801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	static int handle_id = 1;
2457801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
2467801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	/* check for hotplug support */
2477801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {
2487801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm		return LIBUSB_ERROR_NOT_SUPPORTED;
2497801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	}
2507801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
2517801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	/* check for sane values */
2527801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	if ((LIBUSB_HOTPLUG_MATCH_ANY != vendor_id && (~0xffff & vendor_id)) ||
2537801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	    (LIBUSB_HOTPLUG_MATCH_ANY != product_id && (~0xffff & product_id)) ||
2547801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	    (LIBUSB_HOTPLUG_MATCH_ANY != dev_class && (~0xff & dev_class)) ||
2557801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	    !cb_fn) {
2567801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm		return LIBUSB_ERROR_INVALID_PARAM;
2577801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	}
2587801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
2597801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	USBI_GET_CONTEXT(ctx);
2607801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
2617801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	new_callback = (libusb_hotplug_callback *)calloc(1, sizeof (*new_callback));
2627801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	if (!new_callback) {
2637801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm		return LIBUSB_ERROR_NO_MEM;
2647801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	}
2657801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
2667801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	new_callback->ctx = ctx;
2677801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	new_callback->vendor_id = vendor_id;
2687801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	new_callback->product_id = product_id;
2697801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	new_callback->dev_class = dev_class;
2707801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	new_callback->flags = flags;
2717801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	new_callback->events = events;
2727801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	new_callback->cb = cb_fn;
2737801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	new_callback->user_data = user_data;
2747801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	new_callback->needs_free = 0;
2757801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
2767801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	usbi_mutex_lock(&ctx->hotplug_cbs_lock);
2777801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
2787801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	/* protect the handle by the context hotplug lock. it doesn't matter if the same handle
2797801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	 * is used for different contexts only that the handle is unique for this context */
2807801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	new_callback->handle = handle_id++;
2817801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
2827801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	list_add(&new_callback->list, &ctx->hotplug_cbs);
2837801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
284be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede	usbi_mutex_unlock(&ctx->hotplug_cbs_lock);
285be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede
2867801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
287be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede	if (flags & LIBUSB_HOTPLUG_ENUMERATE) {
288be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede		int i, len;
289be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede		struct libusb_device **devs;
290be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede
291def6a8f879e77bbf1606e21f8dc8d7335da9d381Xiaofan Chen		len = (int) libusb_get_device_list(ctx, &devs);
292be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede		if (len < 0) {
293be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede			libusb_hotplug_deregister_callback(ctx,
294be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede							new_callback->handle);
295be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede			return len;
296be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede		}
2977801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
298be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede		for (i = 0; i < len; i++) {
299be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede			usbi_hotplug_match_cb(ctx, devs[i],
300be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede					LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED,
301be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede					new_callback);
3027801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm		}
3037801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
304be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede		libusb_free_device_list(devs, 1);
3057801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	}
3067801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
3077801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
3088a0c14372f98d56529556f18126a42fda2ab0137Chris Dickens	if (callback_handle)
3098a0c14372f98d56529556f18126a42fda2ab0137Chris Dickens		*callback_handle = new_callback->handle;
3107801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
3117801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	return LIBUSB_SUCCESS;
3127801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm}
3137801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
3147801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelmvoid API_EXPORTED libusb_hotplug_deregister_callback (struct libusb_context *ctx,
3158a0c14372f98d56529556f18126a42fda2ab0137Chris Dickens	libusb_hotplug_callback_handle callback_handle)
3167801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm{
3177801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	struct libusb_hotplug_callback *hotplug_cb;
3187801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
3197801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	/* check for hotplug support */
3207801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {
3217801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm		return;
3227801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	}
3237801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
3247801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	USBI_GET_CONTEXT(ctx);
3257801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
3267801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	usbi_mutex_lock(&ctx->hotplug_cbs_lock);
3277801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	list_for_each_entry(hotplug_cb, &ctx->hotplug_cbs, list,
3287801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm			    struct libusb_hotplug_callback) {
3298a0c14372f98d56529556f18126a42fda2ab0137Chris Dickens		if (callback_handle == hotplug_cb->handle) {
3307801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm			/* Mark this callback for deregistration */
3317801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm			hotplug_cb->needs_free = 1;
3327801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm		}
3337801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	}
3347801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	usbi_mutex_unlock(&ctx->hotplug_cbs_lock);
335ac41bfc0fdcf2229fbeb19cdf802cc6b551fd365Hans de Goede
3363b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens	usbi_hotplug_notification(ctx, NULL, 0);
3377801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm}
3387801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
3397801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelmvoid usbi_hotplug_deregister_all(struct libusb_context *ctx) {
3407801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	struct libusb_hotplug_callback *hotplug_cb, *next;
3417801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
3427801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	usbi_mutex_lock(&ctx->hotplug_cbs_lock);
3437801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	list_for_each_entry_safe(hotplug_cb, next, &ctx->hotplug_cbs, list,
3447801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm				 struct libusb_hotplug_callback) {
3457801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm		list_del(&hotplug_cb->list);
3467801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm		free(hotplug_cb);
3477801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	}
3487801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm
3497801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm	usbi_mutex_unlock(&ctx->hotplug_cbs_lock);
3507801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm}
351