1 /* -*- Mode: C; indent-tabs-mode:t ; c-basic-offset:8 -*- */
3 * Hotplug functions for libusb
4 * Copyright © 2012-2013 Nathan Hjelm <hjelmn@mac.com>
5 * Copyright © 2012-2013 Peter Stuge <peter@stuge.se>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #ifdef HAVE_SYS_TYPES_H
29 #include <sys/types.h>
37 * @defgroup libusb_hotplug Device hotplug event notification
38 * This page details how to use the libusb hotplug interface, where available.
40 * Be mindful that not all platforms currently implement hotplug notification and
41 * that you should first call on \ref libusb_has_capability() with parameter
42 * \ref LIBUSB_CAP_HAS_HOTPLUG to confirm that hotplug support is available.
44 * \page libusb_hotplug Device hotplug event notification
46 * \section hotplug_intro Introduction
48 * Version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102, has added support
49 * for hotplug events on <b>some</b> platforms (you should test if your platform
50 * supports hotplug notification by calling \ref libusb_has_capability() with
51 * parameter \ref LIBUSB_CAP_HAS_HOTPLUG).
53 * This interface allows you to request notification for the arrival and departure
54 * of matching USB devices.
56 * To receive hotplug notification you register a callback by calling
57 * \ref libusb_hotplug_register_callback(). This function will optionally return
58 * a callback handle that can be passed to \ref libusb_hotplug_deregister_callback().
60 * A callback function must return an int (0 or 1) indicating whether the callback is
61 * expecting additional events. Returning 0 will rearm the callback and 1 will cause
62 * the callback to be deregistered. Note that when callbacks are called from
63 * libusb_hotplug_register_callback() because of the \ref LIBUSB_HOTPLUG_ENUMERATE
64 * flag, the callback return value is ignored, iow you cannot cause a callback
65 * to be deregistered by returning 1 when it is called from
66 * libusb_hotplug_register_callback().
68 * Callbacks for a particular context are automatically deregistered by libusb_exit().
70 * As of 1.0.16 there are two supported hotplug events:
71 * - LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED: A device has arrived and is ready to use
72 * - LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT: A device has left and is no longer available
74 * A hotplug event can listen for either or both of these events.
76 * Note: If you receive notification that a device has left and you have any
77 * a libusb_device_handles for the device it is up to you to call libusb_close()
78 * on each device handle to free up any remaining resources associated with the device.
79 * Once a device has left any libusb_device_handle associated with the device
80 * are invalid and will remain so even if the device comes back.
82 * When handling a LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED event it is considered
83 * safe to call any libusb function that takes a libusb_device. It also safe to
84 * open a device and submit asynchronous transfers. However, most other functions
85 * that take a libusb_device_handle are <b>not</b> safe to call. Examples of such
86 * functions are any of the \ref libusb_syncio "synchronous API" functions or the blocking
87 * functions that retrieve various \ref libusb_desc "USB descriptors". These functions must
88 * be used outside of the context of the hotplug callback.
90 * When handling a LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT event the only safe function
91 * is libusb_get_device_descriptor().
93 * The following code provides an example of the usage of the hotplug interface:
101 int hotplug_callback(struct libusb_context *ctx, struct libusb_device *dev,
102 libusb_hotplug_event event, void *user_data) {
103 static libusb_device_handle *dev_handle = NULL;
104 struct libusb_device_descriptor desc;
107 (void)libusb_get_device_descriptor(dev, &desc);
109 if (LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED == event) {
110 rc = libusb_open(dev, &dev_handle);
111 if (LIBUSB_SUCCESS != rc) {
112 printf("Could not open USB device\n");
114 } else if (LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT == event) {
116 libusb_close(dev_handle);
120 printf("Unhandled event %d\n", event);
128 libusb_hotplug_callback_handle callback_handle;
133 rc = libusb_hotplug_register_callback(NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED |
134 LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, 0, 0x045a, 0x5005,
135 LIBUSB_HOTPLUG_MATCH_ANY, hotplug_callback, NULL,
137 if (LIBUSB_SUCCESS != rc) {
138 printf("Error creating a hotplug callback\n");
144 libusb_handle_events_completed(NULL, NULL);
148 libusb_hotplug_deregister_callback(NULL, callback_handle);
156 static int usbi_hotplug_match_cb (struct libusb_context *ctx,
157 struct libusb_device *dev, libusb_hotplug_event event,
158 struct libusb_hotplug_callback *hotplug_cb)
160 /* Handle lazy deregistration of callback */
161 if (hotplug_cb->needs_free) {
166 if (!(hotplug_cb->events & event)) {
170 if (LIBUSB_HOTPLUG_MATCH_ANY != hotplug_cb->vendor_id &&
171 hotplug_cb->vendor_id != dev->device_descriptor.idVendor) {
175 if (LIBUSB_HOTPLUG_MATCH_ANY != hotplug_cb->product_id &&
176 hotplug_cb->product_id != dev->device_descriptor.idProduct) {
180 if (LIBUSB_HOTPLUG_MATCH_ANY != hotplug_cb->dev_class &&
181 hotplug_cb->dev_class != dev->device_descriptor.bDeviceClass) {
185 return hotplug_cb->cb (ctx, dev, event, hotplug_cb->user_data);
188 void usbi_hotplug_match(struct libusb_context *ctx, struct libusb_device *dev,
189 libusb_hotplug_event event)
191 struct libusb_hotplug_callback *hotplug_cb, *next;
194 usbi_mutex_lock(&ctx->hotplug_cbs_lock);
196 list_for_each_entry_safe(hotplug_cb, next, &ctx->hotplug_cbs, list, struct libusb_hotplug_callback) {
197 usbi_mutex_unlock(&ctx->hotplug_cbs_lock);
198 ret = usbi_hotplug_match_cb (ctx, dev, event, hotplug_cb);
199 usbi_mutex_lock(&ctx->hotplug_cbs_lock);
202 list_del(&hotplug_cb->list);
207 usbi_mutex_unlock(&ctx->hotplug_cbs_lock);
209 /* the backend is expected to call the callback for each active transfer */
212 void usbi_hotplug_notification(struct libusb_context *ctx, struct libusb_device *dev,
213 libusb_hotplug_event event)
216 libusb_hotplug_message *message = calloc(1, sizeof(*message));
219 usbi_err(ctx, "error allocating hotplug message");
223 message->event = event;
224 message->device = dev;
226 /* Take the event data lock and add this message to the list.
227 * Only signal an event if there are no prior pending events. */
228 usbi_mutex_lock(&ctx->event_data_lock);
229 pending_events = usbi_pending_events(ctx);
230 list_add_tail(&message->list, &ctx->hotplug_msgs);
232 usbi_signal_event(ctx);
233 usbi_mutex_unlock(&ctx->event_data_lock);
236 int API_EXPORTED libusb_hotplug_register_callback(libusb_context *ctx,
237 libusb_hotplug_event events, libusb_hotplug_flag flags,
238 int vendor_id, int product_id, int dev_class,
239 libusb_hotplug_callback_fn cb_fn, void *user_data,
240 libusb_hotplug_callback_handle *callback_handle)
242 libusb_hotplug_callback *new_callback;
243 static int handle_id = 1;
245 /* check for hotplug support */
246 if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {
247 return LIBUSB_ERROR_NOT_SUPPORTED;
250 /* check for sane values */
251 if ((LIBUSB_HOTPLUG_MATCH_ANY != vendor_id && (~0xffff & vendor_id)) ||
252 (LIBUSB_HOTPLUG_MATCH_ANY != product_id && (~0xffff & product_id)) ||
253 (LIBUSB_HOTPLUG_MATCH_ANY != dev_class && (~0xff & dev_class)) ||
255 return LIBUSB_ERROR_INVALID_PARAM;
258 USBI_GET_CONTEXT(ctx);
260 new_callback = (libusb_hotplug_callback *)calloc(1, sizeof (*new_callback));
262 return LIBUSB_ERROR_NO_MEM;
265 new_callback->ctx = ctx;
266 new_callback->vendor_id = vendor_id;
267 new_callback->product_id = product_id;
268 new_callback->dev_class = dev_class;
269 new_callback->flags = flags;
270 new_callback->events = events;
271 new_callback->cb = cb_fn;
272 new_callback->user_data = user_data;
273 new_callback->needs_free = 0;
275 usbi_mutex_lock(&ctx->hotplug_cbs_lock);
277 /* protect the handle by the context hotplug lock. it doesn't matter if the same handle
278 * is used for different contexts only that the handle is unique for this context */
279 new_callback->handle = handle_id++;
281 list_add(&new_callback->list, &ctx->hotplug_cbs);
283 usbi_mutex_unlock(&ctx->hotplug_cbs_lock);
286 if (flags & LIBUSB_HOTPLUG_ENUMERATE) {
288 struct libusb_device **devs;
290 len = (int) libusb_get_device_list(ctx, &devs);
292 libusb_hotplug_deregister_callback(ctx,
293 new_callback->handle);
297 for (i = 0; i < len; i++) {
298 usbi_hotplug_match_cb(ctx, devs[i],
299 LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED,
303 libusb_free_device_list(devs, 1);
308 *callback_handle = new_callback->handle;
310 return LIBUSB_SUCCESS;
313 void API_EXPORTED libusb_hotplug_deregister_callback (struct libusb_context *ctx,
314 libusb_hotplug_callback_handle callback_handle)
316 struct libusb_hotplug_callback *hotplug_cb;
318 /* check for hotplug support */
319 if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {
323 USBI_GET_CONTEXT(ctx);
325 usbi_mutex_lock(&ctx->hotplug_cbs_lock);
326 list_for_each_entry(hotplug_cb, &ctx->hotplug_cbs, list,
327 struct libusb_hotplug_callback) {
328 if (callback_handle == hotplug_cb->handle) {
329 /* Mark this callback for deregistration */
330 hotplug_cb->needs_free = 1;
333 usbi_mutex_unlock(&ctx->hotplug_cbs_lock);
335 usbi_hotplug_notification(ctx, NULL, 0);
338 void usbi_hotplug_deregister_all(struct libusb_context *ctx) {
339 struct libusb_hotplug_callback *hotplug_cb, *next;
341 usbi_mutex_lock(&ctx->hotplug_cbs_lock);
342 list_for_each_entry_safe(hotplug_cb, next, &ctx->hotplug_cbs, list,
343 struct libusb_hotplug_callback) {
344 list_del(&hotplug_cb->list);
348 usbi_mutex_unlock(&ctx->hotplug_cbs_lock);