hotplug.c revision 97958ba756b2f90aa9f65cc7674bc558768dde9f
17801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm/* -*- Mode: C; indent-tabs-mode:t ; c-basic-offset:8 -*- */ 27801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm/* 37801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * Hotplug functions for libusbx 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/** 377801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * @defgroup 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 * 447801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * \page hotplug Device hotplug event notification 457801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * 467801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * \section intro Introduction 477801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * 4886031e26ad8c1516ded02a06834312951a7a0492Hans de Goede * Version 1.0.16, \ref LIBUSBX_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 58b4c18fac65a594502eec5edd2611d5953e7950f7Pete Batard * a 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 627801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * the callback to be deregistered. 637801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * 64b4c18fac65a594502eec5edd2611d5953e7950f7Pete Batard * Callbacks for a particular context are automatically deregistered by libusb_exit(). 657801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * 6686031e26ad8c1516ded02a06834312951a7a0492Hans de Goede * As of 1.0.16 there are two supported hotplug events: 677801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * - LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED: A device has arrived and is ready to use 687801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * - LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT: A device has left and is no longer available 697801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * 707801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * A hotplug event can listen for either or both of these events. 717801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * 727801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * Note: If you receive notification that a device has left and you have any 737801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * a libusb_device_handles for the device it is up to you to call libusb_close() 747801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * on each handle to free up any remaining resources associated with the device. 757801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * Once a device has left any libusb_device_handle associated with the device 767801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * are invalid and will remain so even if the device comes back. 777801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * 787801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * When handling a LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED event it is considered 797801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * safe to call any libusbx function that takes a libusb_device. On the other hand, 807801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * when handling a LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT event the only safe function 817801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * is libusb_get_device_descriptor(). 827801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * 837801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * The following code provides an example of the usage of the hotplug interface: 847801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm\code 857801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelmstatic int count = 0; 867801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 877801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelmint hotplug_callback(struct libusb_context *ctx, struct libusb_device *dev, 887801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm libusb_hotplug_event event, void *user_data) { 897801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm static libusb_device_handle *handle = NULL; 907801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm struct libusb_device_descriptor desc; 917801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm int rc; 927801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 937801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm (void)libusb_get_device_descriptor(dev, &desc); 947801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 957801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED == event) { 967801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm rc = libusb_open(dev, &handle); 977801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (LIBUSB_SUCCESS != rc) { 987801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm printf("Could not open USB device\n"); 997801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 1007801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } else if (LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT == event) { 1017801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (handle) { 1027801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm libusb_close(handle); 1037801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm handle = NULL; 1047801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 1057801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } else { 1067801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm printf("Unhandled event %d\n", event); 1077801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 1087801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm count++; 1097801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1107801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm return 0; 1117801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm} 1127801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1137801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelmint main (void) { 1147801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm libusb_hotplug_callback_handle handle; 1157801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm int rc; 1167801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1177801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm libusb_init(NULL); 1187801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1197801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm rc = libusb_hotplug_register_callback(NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | 1207801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, 0, 0x045a, 0x5005, 1217801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm LIBUSB_HOTPLUG_MATCH_ANY, hotplug_callback, NULL, 1227801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm &handle); 1237801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (LIBUSB_SUCCESS != rc) { 1247801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm printf("Error creating a hotplug callback\n"); 1257801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm libusb_exit(NULL); 1267801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm return EXIT_FAILURE; 1277801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 1287801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1297801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm while (count < 2) { 1307801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm usleep(10000); 1317801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 1327801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1337801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm libusb_hotplug_deregister_callback(handle); 1347801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm libusb_exit(NULL); 1357801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1367801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm return 0; 1377801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm} 1387801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm\endcode 1397801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm */ 1407801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 141f50592979811a00d52305ea02b420cb9cacc99b4Hans de Goedestatic int usbi_hotplug_match_cb (struct libusb_context *ctx, 142f50592979811a00d52305ea02b420cb9cacc99b4Hans de Goede struct libusb_device *dev, libusb_hotplug_event event, 143f50592979811a00d52305ea02b420cb9cacc99b4Hans de Goede struct libusb_hotplug_callback *hotplug_cb) 1447801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm{ 1457801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm /* Handle lazy deregistration of callback */ 1467801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (hotplug_cb->needs_free) { 1477801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm /* Free callback */ 1487801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm return 1; 1497801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 1507801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1517801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (!(hotplug_cb->events & event)) { 1527801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm return 0; 1537801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 1547801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1557801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (LIBUSB_HOTPLUG_MATCH_ANY != hotplug_cb->vendor_id && 1567801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm hotplug_cb->vendor_id != dev->device_descriptor.idVendor) { 1577801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm return 0; 1587801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 1597801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1607801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (LIBUSB_HOTPLUG_MATCH_ANY != hotplug_cb->product_id && 1617801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm hotplug_cb->product_id != dev->device_descriptor.idProduct) { 1627801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm return 0; 1637801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 1647801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1657801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (LIBUSB_HOTPLUG_MATCH_ANY != hotplug_cb->dev_class && 1667801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm hotplug_cb->dev_class != dev->device_descriptor.bDeviceClass) { 1677801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm return 0; 1687801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 1697801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 17097958ba756b2f90aa9f65cc7674bc558768dde9fFlorian Albrechtskirchinger return hotplug_cb->cb (ctx, dev, event, hotplug_cb->user_data); 1717801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm} 1727801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 173f50592979811a00d52305ea02b420cb9cacc99b4Hans de Goedevoid usbi_hotplug_match(struct libusb_context *ctx, struct libusb_device *dev, 174f50592979811a00d52305ea02b420cb9cacc99b4Hans de Goede libusb_hotplug_event event) 1757801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm{ 1767801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm struct libusb_hotplug_callback *hotplug_cb, *next; 1777801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm int ret; 1787801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1797801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm usbi_mutex_lock(&ctx->hotplug_cbs_lock); 1807801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1817801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm list_for_each_entry_safe(hotplug_cb, next, &ctx->hotplug_cbs, list, struct libusb_hotplug_callback) { 1827801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm usbi_mutex_unlock(&ctx->hotplug_cbs_lock); 183f50592979811a00d52305ea02b420cb9cacc99b4Hans de Goede ret = usbi_hotplug_match_cb (ctx, dev, event, hotplug_cb); 1847801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm usbi_mutex_lock(&ctx->hotplug_cbs_lock); 1857801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1867801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (ret) { 1877801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm list_del(&hotplug_cb->list); 1887801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm free(hotplug_cb); 1897801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 1907801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 1917801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1927801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm usbi_mutex_unlock(&ctx->hotplug_cbs_lock); 1937801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1947801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm /* loop through and disconnect all open handles for this device */ 1957801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT == event) { 1967801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm struct libusb_device_handle *handle; 1977801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1987801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm usbi_mutex_lock(&ctx->open_devs_lock); 1997801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm list_for_each_entry(handle, &ctx->open_devs, list, struct libusb_device_handle) { 2007801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (dev == handle->dev) { 2017801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm usbi_handle_disconnect (handle); 2027801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 2037801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 2047801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm usbi_mutex_unlock(&ctx->open_devs_lock); 2057801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 2067801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm} 2077801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 2087801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelmint API_EXPORTED libusb_hotplug_register_callback(libusb_context *ctx, 2097801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm libusb_hotplug_event events, libusb_hotplug_flag flags, 2107801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm int vendor_id, int product_id, int dev_class, 2117801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm libusb_hotplug_callback_fn cb_fn, void *user_data, 2127801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm libusb_hotplug_callback_handle *handle) 2137801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm{ 2147801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm libusb_hotplug_callback *new_callback; 2157801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm static int handle_id = 1; 2167801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 2177801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm /* check for hotplug support */ 2187801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) { 2197801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm return LIBUSB_ERROR_NOT_SUPPORTED; 2207801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 2217801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 2227801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm /* check for sane values */ 2237801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if ((LIBUSB_HOTPLUG_MATCH_ANY != vendor_id && (~0xffff & vendor_id)) || 2247801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm (LIBUSB_HOTPLUG_MATCH_ANY != product_id && (~0xffff & product_id)) || 2257801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm (LIBUSB_HOTPLUG_MATCH_ANY != dev_class && (~0xff & dev_class)) || 2267801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm !cb_fn) { 2277801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm return LIBUSB_ERROR_INVALID_PARAM; 2287801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 2297801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 2307801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm USBI_GET_CONTEXT(ctx); 2317801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 2327801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm new_callback = (libusb_hotplug_callback *)calloc(1, sizeof (*new_callback)); 2337801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (!new_callback) { 2347801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm return LIBUSB_ERROR_NO_MEM; 2357801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 2367801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 2377801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm new_callback->ctx = ctx; 2387801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm new_callback->vendor_id = vendor_id; 2397801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm new_callback->product_id = product_id; 2407801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm new_callback->dev_class = dev_class; 2417801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm new_callback->flags = flags; 2427801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm new_callback->events = events; 2437801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm new_callback->cb = cb_fn; 2447801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm new_callback->user_data = user_data; 2457801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm new_callback->needs_free = 0; 2467801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 2477801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm usbi_mutex_lock(&ctx->hotplug_cbs_lock); 2487801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 2497801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm /* protect the handle by the context hotplug lock. it doesn't matter if the same handle 2507801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * is used for different contexts only that the handle is unique for this context */ 2517801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm new_callback->handle = handle_id++; 2527801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 2537801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm list_add(&new_callback->list, &ctx->hotplug_cbs); 2547801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 2557801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (flags & LIBUSB_HOTPLUG_ENUMERATE) { 2567801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm struct libusb_device *dev; 2577801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 2587801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm usbi_mutex_lock(&ctx->usb_devs_lock); 2597801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 2607801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm list_for_each_entry(dev, &ctx->usb_devs, list, struct libusb_device) { 261f50592979811a00d52305ea02b420cb9cacc99b4Hans de Goede (void) usbi_hotplug_match_cb (ctx, dev, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED, new_callback); 2627801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 2637801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 2647801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm usbi_mutex_unlock(&ctx->usb_devs_lock); 2657801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 2667801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 2677801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm usbi_mutex_unlock(&ctx->hotplug_cbs_lock); 2687801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 2697801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (handle) { 2707801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm *handle = new_callback->handle; 2717801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 2727801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 2737801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm return LIBUSB_SUCCESS; 2747801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm} 2757801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 2767801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelmvoid API_EXPORTED libusb_hotplug_deregister_callback (struct libusb_context *ctx, 2777801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm libusb_hotplug_callback_handle handle) 2787801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm{ 2797801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm struct libusb_hotplug_callback *hotplug_cb; 280ac41bfc0fdcf2229fbeb19cdf802cc6b551fd365Hans de Goede libusb_hotplug_message message; 281ac41bfc0fdcf2229fbeb19cdf802cc6b551fd365Hans de Goede ssize_t ret; 2827801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 2837801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm /* check for hotplug support */ 2847801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) { 2857801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm return; 2867801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 2877801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 2887801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm USBI_GET_CONTEXT(ctx); 2897801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 2907801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm usbi_mutex_lock(&ctx->hotplug_cbs_lock); 2917801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm list_for_each_entry(hotplug_cb, &ctx->hotplug_cbs, list, 2927801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm struct libusb_hotplug_callback) { 2937801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (handle == hotplug_cb->handle) { 2947801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm /* Mark this callback for deregistration */ 2957801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm hotplug_cb->needs_free = 1; 2967801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 2977801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 2987801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm usbi_mutex_unlock(&ctx->hotplug_cbs_lock); 299ac41bfc0fdcf2229fbeb19cdf802cc6b551fd365Hans de Goede 300ac41bfc0fdcf2229fbeb19cdf802cc6b551fd365Hans de Goede /* wakeup handle_events to do the actual free */ 301ac41bfc0fdcf2229fbeb19cdf802cc6b551fd365Hans de Goede memset(&message, 0, sizeof(message)); 302ac41bfc0fdcf2229fbeb19cdf802cc6b551fd365Hans de Goede ret = usbi_write(ctx->hotplug_pipe[1], &message, sizeof(message)); 303ac41bfc0fdcf2229fbeb19cdf802cc6b551fd365Hans de Goede if (sizeof(message) != ret) { 304ac41bfc0fdcf2229fbeb19cdf802cc6b551fd365Hans de Goede usbi_err(ctx, "error writing hotplug message"); 305ac41bfc0fdcf2229fbeb19cdf802cc6b551fd365Hans de Goede } 3067801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm} 3077801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 3087801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelmvoid usbi_hotplug_deregister_all(struct libusb_context *ctx) { 3097801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm struct libusb_hotplug_callback *hotplug_cb, *next; 3107801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 3117801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm usbi_mutex_lock(&ctx->hotplug_cbs_lock); 3127801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm list_for_each_entry_safe(hotplug_cb, next, &ctx->hotplug_cbs, list, 3137801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm struct libusb_hotplug_callback) { 3147801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm list_del(&hotplug_cb->list); 3157801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm free(hotplug_cb); 3167801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 3177801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 3187801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm usbi_mutex_unlock(&ctx->hotplug_cbs_lock); 3197801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm} 320