• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* -*- Mode: C; indent-tabs-mode:t ; c-basic-offset:8 -*- */
2 /*
3  * Hotplug support for libusb
4  * Copyright © 2012-2013 Nathan Hjelm <hjelmn@mac.com>
5  * Copyright © 2012-2013 Peter Stuge <peter@stuge.se>
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 #ifndef USBI_HOTPLUG_H
23 #define USBI_HOTPLUG_H
24 
25 #include "libusbi.h"
26 
27 enum usbi_hotplug_flags {
28 	/* This callback is interested in device arrivals */
29 	USBI_HOTPLUG_DEVICE_ARRIVED = LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED,
30 
31 	/* This callback is interested in device removals */
32 	USBI_HOTPLUG_DEVICE_LEFT = LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT,
33 
34 	/* IMPORTANT: The values for the below entries must start *after*
35 	 * the highest value of the above entries!!!
36 	 */
37 
38 	/* The vendor_id field is valid for matching */
39 	USBI_HOTPLUG_VENDOR_ID_VALID = (1U << 3),
40 
41 	/* The product_id field is valid for matching */
42 	USBI_HOTPLUG_PRODUCT_ID_VALID = (1U << 4),
43 
44 	/* The dev_class field is valid for matching */
45 	USBI_HOTPLUG_DEV_CLASS_VALID = (1U << 5),
46 
47 	/* This callback has been unregistered and needs to be freed */
48 	USBI_HOTPLUG_NEEDS_FREE = (1U << 6),
49 };
50 
51 /** \ingroup hotplug
52  * The hotplug callback structure. The user populates this structure with
53  * libusb_hotplug_prepare_callback() and then calls libusb_hotplug_register_callback()
54  * to receive notification of hotplug events.
55  */
56 struct libusb_hotplug_callback {
57 	/** Flags that control how this callback behaves */
58 	uint8_t flags;
59 
60 	/** Vendor ID to match (if flags says this is valid) */
61 	uint16_t vendor_id;
62 
63 	/** Product ID to match (if flags says this is valid) */
64 	uint16_t product_id;
65 
66 	/** Device class to match (if flags says this is valid) */
67 	uint8_t dev_class;
68 
69 	/** Callback function to invoke for matching event/device */
70 	libusb_hotplug_callback_fn cb;
71 
72 	/** Handle for this callback (used to match on deregister) */
73 	libusb_hotplug_callback_handle handle;
74 
75 	/** User data that will be passed to the callback function */
76 	void *user_data;
77 
78 	/** List this callback is registered in (ctx->hotplug_cbs) */
79 	struct list_head list;
80 };
81 
82 struct libusb_hotplug_message {
83 	/** The hotplug event that occurred */
84 	libusb_hotplug_event event;
85 
86 	/** The device for which this hotplug event occurred */
87 	struct libusb_device *device;
88 
89 	/** List this message is contained in (ctx->hotplug_msgs) */
90 	struct list_head list;
91 };
92 
93 #define for_each_hotplug_cb(ctx, c) \
94 	for_each_helper(c, &(ctx)->hotplug_cbs, struct libusb_hotplug_callback)
95 
96 #define for_each_hotplug_cb_safe(ctx, c, n) \
97 	for_each_safe_helper(c, n, &(ctx)->hotplug_cbs, struct libusb_hotplug_callback)
98 
99 void usbi_hotplug_deregister(struct libusb_context *ctx, int forced);
100 void usbi_hotplug_match(struct libusb_context *ctx, struct libusb_device *dev,
101 	libusb_hotplug_event event);
102 void usbi_hotplug_notification(struct libusb_context *ctx, struct libusb_device *dev,
103 	libusb_hotplug_event event);
104 
105 #endif
106