hotplug.c revision 89b5407b9aa74790a64b6f32a5b14a96242ba128
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/** 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 * 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 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 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() 787801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * on each 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 831eff220474f63d7ea7f8f99bef2a3da9da5324ebhjelmn@cs.unm.edu * safe to call any libusb function that takes a libusb_device. On the other hand, 847801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * when handling a LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT event the only safe function 857801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * is libusb_get_device_descriptor(). 867801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * 877801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * The following code provides an example of the usage of the hotplug interface: 887801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm\code 8993de199d8d60822fc42b1cbf8b920132cd8bbbc6Markus Heidelberg#include <stdio.h> 9093de199d8d60822fc42b1cbf8b920132cd8bbbc6Markus Heidelberg#include <stdlib.h> 9193de199d8d60822fc42b1cbf8b920132cd8bbbc6Markus Heidelberg#include <libusb.h> 9293de199d8d60822fc42b1cbf8b920132cd8bbbc6Markus Heidelberg 937801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelmstatic int count = 0; 947801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 957801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelmint hotplug_callback(struct libusb_context *ctx, struct libusb_device *dev, 967801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm libusb_hotplug_event event, void *user_data) { 977801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm static libusb_device_handle *handle = NULL; 987801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm struct libusb_device_descriptor desc; 997801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm int rc; 1007801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1017801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm (void)libusb_get_device_descriptor(dev, &desc); 1027801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1037801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED == event) { 1047801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm rc = libusb_open(dev, &handle); 1057801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (LIBUSB_SUCCESS != rc) { 1067801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm printf("Could not open USB device\n"); 1077801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 1087801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } else if (LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT == event) { 1097801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (handle) { 1107801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm libusb_close(handle); 1117801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm handle = NULL; 1127801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 1137801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } else { 1147801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm printf("Unhandled event %d\n", event); 1157801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 1167801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm count++; 1177801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1187801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm return 0; 1197801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm} 1207801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1217801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelmint main (void) { 1227801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm libusb_hotplug_callback_handle handle; 1237801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm int rc; 1247801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1257801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm libusb_init(NULL); 1267801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1277801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm rc = libusb_hotplug_register_callback(NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | 1287801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, 0, 0x045a, 0x5005, 1297801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm LIBUSB_HOTPLUG_MATCH_ANY, hotplug_callback, NULL, 1307801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm &handle); 1317801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (LIBUSB_SUCCESS != rc) { 1327801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm printf("Error creating a hotplug callback\n"); 1337801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm libusb_exit(NULL); 1347801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm return EXIT_FAILURE; 1357801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 1367801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1377801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm while (count < 2) { 13893de199d8d60822fc42b1cbf8b920132cd8bbbc6Markus Heidelberg libusb_handle_events_completed(NULL, NULL); 1397801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm usleep(10000); 1407801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 1417801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 14293de199d8d60822fc42b1cbf8b920132cd8bbbc6Markus Heidelberg libusb_hotplug_deregister_callback(NULL, handle); 1437801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm libusb_exit(NULL); 1447801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1457801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm return 0; 1467801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm} 1477801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm\endcode 1487801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm */ 1497801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 150f50592979811a00d52305ea02b420cb9cacc99b4Hans de Goedestatic int usbi_hotplug_match_cb (struct libusb_context *ctx, 151f50592979811a00d52305ea02b420cb9cacc99b4Hans de Goede struct libusb_device *dev, libusb_hotplug_event event, 152f50592979811a00d52305ea02b420cb9cacc99b4Hans de Goede struct libusb_hotplug_callback *hotplug_cb) 1537801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm{ 1547801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm /* Handle lazy deregistration of callback */ 1557801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (hotplug_cb->needs_free) { 1567801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm /* Free callback */ 1577801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm return 1; 1587801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 1597801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1607801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (!(hotplug_cb->events & event)) { 1617801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm return 0; 1627801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 1637801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1647801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (LIBUSB_HOTPLUG_MATCH_ANY != hotplug_cb->vendor_id && 1657801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm hotplug_cb->vendor_id != dev->device_descriptor.idVendor) { 1667801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm return 0; 1677801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 1687801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1697801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (LIBUSB_HOTPLUG_MATCH_ANY != hotplug_cb->product_id && 1707801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm hotplug_cb->product_id != dev->device_descriptor.idProduct) { 1717801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm return 0; 1727801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 1737801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1747801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (LIBUSB_HOTPLUG_MATCH_ANY != hotplug_cb->dev_class && 1757801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm hotplug_cb->dev_class != dev->device_descriptor.bDeviceClass) { 1767801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm return 0; 1777801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 1787801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 17997958ba756b2f90aa9f65cc7674bc558768dde9fFlorian Albrechtskirchinger return hotplug_cb->cb (ctx, dev, event, hotplug_cb->user_data); 1807801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm} 1817801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 182f50592979811a00d52305ea02b420cb9cacc99b4Hans de Goedevoid usbi_hotplug_match(struct libusb_context *ctx, struct libusb_device *dev, 183f50592979811a00d52305ea02b420cb9cacc99b4Hans de Goede libusb_hotplug_event event) 1847801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm{ 1857801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm struct libusb_hotplug_callback *hotplug_cb, *next; 1867801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm int ret; 1877801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1887801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm usbi_mutex_lock(&ctx->hotplug_cbs_lock); 1897801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1907801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm list_for_each_entry_safe(hotplug_cb, next, &ctx->hotplug_cbs, list, struct libusb_hotplug_callback) { 1917801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm usbi_mutex_unlock(&ctx->hotplug_cbs_lock); 192f50592979811a00d52305ea02b420cb9cacc99b4Hans de Goede ret = usbi_hotplug_match_cb (ctx, dev, event, hotplug_cb); 1937801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm usbi_mutex_lock(&ctx->hotplug_cbs_lock); 1947801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 1957801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (ret) { 1967801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm list_del(&hotplug_cb->list); 1977801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm free(hotplug_cb); 1987801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 1997801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 2007801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 2017801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm usbi_mutex_unlock(&ctx->hotplug_cbs_lock); 2027801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 203707d500b9fea002f075cf30458a602f28dbd1348Nathan Hjelm /* the backend is expected to call the callback for each active transfer */ 2047801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm} 2057801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 2063b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickensvoid usbi_hotplug_notification(struct libusb_context *ctx, struct libusb_device *dev, 2073b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens libusb_hotplug_event event) 2083b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens{ 20989b5407b9aa74790a64b6f32a5b14a96242ba128Chris Dickens int pending_events;; 2103b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens libusb_hotplug_message *message = calloc(1, sizeof(*message)); 2113b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens 2123b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens if (!message) { 2133b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens usbi_err(ctx, "error allocating hotplug message"); 2143b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens return; 2153b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens } 2163b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens 2173b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens message->event = event; 2183b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens message->device = dev; 2193b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens 22089b5407b9aa74790a64b6f32a5b14a96242ba128Chris Dickens /* Take the event data lock and add this message to the list. 22189b5407b9aa74790a64b6f32a5b14a96242ba128Chris Dickens * Only signal an event if there are no prior pending events. */ 2223b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens usbi_mutex_lock(&ctx->event_data_lock); 22389b5407b9aa74790a64b6f32a5b14a96242ba128Chris Dickens pending_events = usbi_pending_events(ctx); 2243b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens list_add_tail(&message->list, &ctx->hotplug_msgs); 22589b5407b9aa74790a64b6f32a5b14a96242ba128Chris Dickens if (!pending_events) 22689b5407b9aa74790a64b6f32a5b14a96242ba128Chris Dickens usbi_signal_event(ctx); 2273b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens usbi_mutex_unlock(&ctx->event_data_lock); 2283b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens} 2293b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens 2307801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelmint API_EXPORTED libusb_hotplug_register_callback(libusb_context *ctx, 2317801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm libusb_hotplug_event events, libusb_hotplug_flag flags, 2327801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm int vendor_id, int product_id, int dev_class, 2337801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm libusb_hotplug_callback_fn cb_fn, void *user_data, 2347801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm libusb_hotplug_callback_handle *handle) 2357801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm{ 2367801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm libusb_hotplug_callback *new_callback; 2377801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm static int handle_id = 1; 2387801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 2397801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm /* check for hotplug support */ 2407801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) { 2417801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm return LIBUSB_ERROR_NOT_SUPPORTED; 2427801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 2437801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 2447801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm /* check for sane values */ 2457801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if ((LIBUSB_HOTPLUG_MATCH_ANY != vendor_id && (~0xffff & vendor_id)) || 2467801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm (LIBUSB_HOTPLUG_MATCH_ANY != product_id && (~0xffff & product_id)) || 2477801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm (LIBUSB_HOTPLUG_MATCH_ANY != dev_class && (~0xff & dev_class)) || 2487801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm !cb_fn) { 2497801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm return LIBUSB_ERROR_INVALID_PARAM; 2507801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 2517801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 2527801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm USBI_GET_CONTEXT(ctx); 2537801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 2547801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm new_callback = (libusb_hotplug_callback *)calloc(1, sizeof (*new_callback)); 2557801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (!new_callback) { 2567801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm return LIBUSB_ERROR_NO_MEM; 2577801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 2587801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 2597801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm new_callback->ctx = ctx; 2607801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm new_callback->vendor_id = vendor_id; 2617801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm new_callback->product_id = product_id; 2627801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm new_callback->dev_class = dev_class; 2637801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm new_callback->flags = flags; 2647801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm new_callback->events = events; 2657801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm new_callback->cb = cb_fn; 2667801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm new_callback->user_data = user_data; 2677801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm new_callback->needs_free = 0; 2687801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 2697801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm usbi_mutex_lock(&ctx->hotplug_cbs_lock); 2707801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 2717801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm /* protect the handle by the context hotplug lock. it doesn't matter if the same handle 2727801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm * is used for different contexts only that the handle is unique for this context */ 2737801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm new_callback->handle = handle_id++; 2747801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 2757801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm list_add(&new_callback->list, &ctx->hotplug_cbs); 2767801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 277be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede usbi_mutex_unlock(&ctx->hotplug_cbs_lock); 278be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede 2797801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 280be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede if (flags & LIBUSB_HOTPLUG_ENUMERATE) { 281be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede int i, len; 282be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede struct libusb_device **devs; 283be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede 284def6a8f879e77bbf1606e21f8dc8d7335da9d381Xiaofan Chen len = (int) libusb_get_device_list(ctx, &devs); 285be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede if (len < 0) { 286be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede libusb_hotplug_deregister_callback(ctx, 287be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede new_callback->handle); 288be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede return len; 289be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede } 2907801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 291be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede for (i = 0; i < len; i++) { 292be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede usbi_hotplug_match_cb(ctx, devs[i], 293be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED, 294be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede new_callback); 2957801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 2967801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 297be76bef5b716d27b78fba120d3f9b815ed3380b9Hans de Goede libusb_free_device_list(devs, 1); 2987801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 2997801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 3007801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 3017801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (handle) { 3027801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm *handle = new_callback->handle; 3037801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 3047801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 3057801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm return LIBUSB_SUCCESS; 3067801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm} 3077801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 3087801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelmvoid API_EXPORTED libusb_hotplug_deregister_callback (struct libusb_context *ctx, 3097801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm libusb_hotplug_callback_handle handle) 3107801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm{ 3117801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm struct libusb_hotplug_callback *hotplug_cb; 3127801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 3137801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm /* check for hotplug support */ 3147801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) { 3157801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm return; 3167801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 3177801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 3187801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm USBI_GET_CONTEXT(ctx); 3197801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 3207801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm usbi_mutex_lock(&ctx->hotplug_cbs_lock); 3217801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm list_for_each_entry(hotplug_cb, &ctx->hotplug_cbs, list, 3227801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm struct libusb_hotplug_callback) { 3237801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm if (handle == hotplug_cb->handle) { 3247801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm /* Mark this callback for deregistration */ 3257801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm hotplug_cb->needs_free = 1; 3267801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 3277801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 3287801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm usbi_mutex_unlock(&ctx->hotplug_cbs_lock); 329ac41bfc0fdcf2229fbeb19cdf802cc6b551fd365Hans de Goede 3303b371f1a6ab30bdebdadfcde1b96a9b98fb806caChris Dickens usbi_hotplug_notification(ctx, NULL, 0); 3317801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm} 3327801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 3337801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelmvoid usbi_hotplug_deregister_all(struct libusb_context *ctx) { 3347801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm struct libusb_hotplug_callback *hotplug_cb, *next; 3357801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 3367801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm usbi_mutex_lock(&ctx->hotplug_cbs_lock); 3377801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm list_for_each_entry_safe(hotplug_cb, next, &ctx->hotplug_cbs, list, 3387801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm struct libusb_hotplug_callback) { 3397801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm list_del(&hotplug_cb->list); 3407801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm free(hotplug_cb); 3417801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm } 3427801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm 3437801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm usbi_mutex_unlock(&ctx->hotplug_cbs_lock); 3447801ff94fa6e49fe98433eccc7f2e461590a6f7cNathan Hjelm} 345