File: open-fd.patch

package info (click to toggle)
qtwebengine-opensource-src 5.7.1+dfsg-6.1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 1,028,096 kB
  • ctags: 1,436,736
  • sloc: cpp: 5,960,176; ansic: 3,477,727; asm: 395,492; python: 320,633; sh: 96,504; perl: 69,449; xml: 42,977; makefile: 28,408; objc: 9,962; yacc: 9,847; tcl: 5,430; lex: 2,259; ruby: 1,053; lisp: 522; awk: 497; pascal: 310; cs: 249; sed: 53
file content (236 lines) | stat: -rw-r--r-- 9,119 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
diff --git a/third_party/libusb/src/libusb/core.c b/third_party/libusb/src/libusb/core.c
index e816284..abc4f89 100644
--- a/third_party/libusb/src/libusb/core.c
+++ b/third_party/libusb/src/libusb/core.c
@@ -1129,6 +1129,83 @@ int API_EXPORTED libusb_open(libusb_device *dev,
 }
 
 /** \ingroup dev
+ * Open a device and obtain a device handle. A handle allows you to perform
+ * I/O on the device in question.
+ *
+ * Instead of opening the device itself this function accepts an open file
+ * descriptor that it will take ownership of.
+ *
+ * Internally, this function adds a reference to the device and makes it
+ * available to you through libusb_get_device(). This reference is removed
+ * during libusb_close().
+ *
+ * This is a non-blocking function; no requests are sent over the bus.
+ *
+ * \param dev the device to open
+ * \param fd open file handle to the device
+ * \param handle output location for the returned device handle pointer. Only
+ * populated when the return code is 0.
+ * \returns 0 on success
+ * \returns LIBUSB_ERROR_NO_MEM on memory allocation failure
+ * \returns LIBUSB_ERROR_ACCESS if the user has insufficient permissions
+ * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
+ * \returns another LIBUSB_ERROR code on other failure
+ */
+int API_EXPORTED libusb_open_fd(libusb_device *dev,
+  int fd,
+  libusb_device_handle **handle)
+{
+  struct libusb_context *ctx = DEVICE_CTX(dev);
+  struct libusb_device_handle *_handle;
+  size_t priv_size = usbi_backend->device_handle_priv_size;
+  int r;
+  usbi_dbg("open %d.%d", dev->bus_number, dev->device_address);
+
+  if (!dev->attached) {
+    return LIBUSB_ERROR_NO_DEVICE;
+  }
+
+  _handle = malloc(sizeof(*_handle) + priv_size);
+  if (!_handle)
+    return LIBUSB_ERROR_NO_MEM;
+
+  r = usbi_mutex_init(&_handle->lock, NULL);
+  if (r) {
+    free(_handle);
+    return LIBUSB_ERROR_OTHER;
+  }
+
+  _handle->dev = libusb_ref_device(dev);
+  _handle->auto_detach_kernel_driver = 0;
+  _handle->claimed_interfaces = 0;
+  memset(&_handle->os_priv, 0, priv_size);
+
+  r = usbi_backend->open_fd(_handle, fd);
+  if (r < 0) {
+    usbi_dbg("open %d.%d returns %d", dev->bus_number, dev->device_address, r);
+    libusb_unref_device(dev);
+    usbi_mutex_destroy(&_handle->lock);
+    free(_handle);
+    return r;
+  }
+
+  usbi_mutex_lock(&ctx->open_devs_lock);
+  list_add(&_handle->list, &ctx->open_devs);
+  usbi_mutex_unlock(&ctx->open_devs_lock);
+  *handle = _handle;
+
+  /* At this point, we want to interrupt any existing event handlers so
+   * that they realise the addition of the new device's poll fd. One
+   * example when this is desirable is if the user is running a separate
+   * dedicated libusbx events handling thread, which is running with a long
+   * or infinite timeout. We want to interrupt that iteration of the loop,
+   * so that it picks up the new fd, and then continues. */
+  usbi_fd_notification(ctx);
+
+  return 0;
+}
+
+/** \ingroup dev
  * Convenience function for finding a device with a particular
  * <tt>idVendor</tt>/<tt>idProduct</tt> combination. This function is intended
  * for those scenarios where you are using libusbx to knock up a quick test
diff --git a/third_party/libusb/src/libusb/libusb.h b/third_party/libusb/src/libusb/libusb.h
index d144b3e..5d60951 100644
--- a/third_party/libusb/src/libusb/libusb.h
+++ b/third_party/libusb/src/libusb/libusb.h
@@ -1371,6 +1371,8 @@ int LIBUSB_CALL libusb_get_max_iso_packet_size(libusb_device *dev,
 	unsigned char endpoint);
 
 int LIBUSB_CALL libusb_open(libusb_device *dev, libusb_device_handle **handle);
+int LIBUSB_CALL libusb_open_fd(libusb_device *dev, int fd,
+	libusb_device_handle **handle);
 void LIBUSB_CALL libusb_close(libusb_device_handle *dev_handle);
 libusb_device * LIBUSB_CALL libusb_get_device(libusb_device_handle *dev_handle);
 
diff --git a/third_party/libusb/src/libusb/libusbi.h b/third_party/libusb/src/libusb/libusbi.h
index bc608b92..eb2f0e6 100644
--- a/third_party/libusb/src/libusb/libusbi.h
+++ b/third_party/libusb/src/libusb/libusbi.h
@@ -615,6 +615,11 @@ struct usbi_os_backend {
 	 */
 	int (*open)(struct libusb_device_handle *handle);
 
+	/* Like open() above but uses the file descriptor provided instead of opening
+	 * one on its own.
+	 */
+	int (*open_fd)(struct libusb_device_handle *handle, int fd);
+
 	/* Close a device such that the handle cannot be used again. Your backend
 	 * should destroy any resources that were allocated in the open path.
 	 * This may also be a good place to call usbi_remove_pollfd() to inform
diff --git a/third_party/libusb/src/libusb/os/darwin_usb.c b/third_party/libusb/src/libusb/os/darwin_usb.c
index f95706a..f6b397e 100644
--- a/third_party/libusb/src/libusb/os/darwin_usb.c
+++ b/third_party/libusb/src/libusb/os/darwin_usb.c
@@ -1877,6 +1877,7 @@ const struct usbi_os_backend darwin_backend = {
         .get_config_descriptor = darwin_get_config_descriptor,
 
         .open = darwin_open,
+        .open_fd = NULL, /* not implemented */
         .close = darwin_close,
         .get_configuration = darwin_get_configuration,
         .set_configuration = darwin_set_configuration,
diff --git a/third_party/libusb/src/libusb/os/linux_usbfs.c b/third_party/libusb/src/libusb/os/linux_usbfs.c
index 142fa2b..e965856 100644
--- a/third_party/libusb/src/libusb/os/linux_usbfs.c
+++ b/third_party/libusb/src/libusb/os/linux_usbfs.c
@@ -1259,26 +1259,12 @@ static int linux_default_scan_devices (struct libusb_context *ctx)
 }
 #endif
 
-static int op_open(struct libusb_device_handle *handle)
+static int op_open_fd(struct libusb_device_handle *handle, int fd)
 {
 	struct linux_device_handle_priv *hpriv = _device_handle_priv(handle);
 	int r;
 
-	hpriv->fd = _get_usbfs_fd(handle->dev, O_RDWR, 0);
-	if (hpriv->fd < 0) {
-		if (hpriv->fd == LIBUSB_ERROR_NO_DEVICE) {
-			/* device will still be marked as attached if hotplug monitor thread
-			 * hasn't processed remove event yet */
-			usbi_mutex_static_lock(&linux_hotplug_lock);
-			if (handle->dev->attached) {
-				usbi_dbg("open failed with no device, but device still attached");
-				linux_device_disconnected(handle->dev->bus_number,
-						handle->dev->device_address, NULL);
-			}
-			usbi_mutex_static_unlock(&linux_hotplug_lock);
-		}
-		return hpriv->fd;
-	}
+	hpriv->fd = fd;
 
 	r = ioctl(hpriv->fd, IOCTL_USBFS_GET_CAPABILITIES, &hpriv->caps);
 	if (r < 0) {
@@ -1296,6 +1282,29 @@ static int op_open(struct libusb_device_handle *handle)
 	return usbi_add_pollfd(HANDLE_CTX(handle), hpriv->fd, POLLOUT);
 }
 
+static int op_open(struct libusb_device_handle *handle)
+{
+	struct linux_device_handle_priv *hpriv = _device_handle_priv(handle);
+	int fd = _get_usbfs_fd(handle->dev, O_RDWR, 0);
+
+	if (fd < 0) {
+		if (fd == LIBUSB_ERROR_NO_DEVICE) {
+			/* device will still be marked as attached if hotplug monitor thread
+			 * hasn't processed remove event yet */
+			usbi_mutex_static_lock(&linux_hotplug_lock);
+			if (handle->dev->attached) {
+				usbi_dbg("open failed with no device, but device still attached");
+				linux_device_disconnected(handle->dev->bus_number,
+						handle->dev->device_address, NULL);
+			}
+			usbi_mutex_static_unlock(&linux_hotplug_lock);
+		}
+		return fd;
+	}
+
+	return op_open_fd(handle, fd);
+}
+
 static void op_close(struct libusb_device_handle *dev_handle)
 {
 	int fd = _device_handle_priv(dev_handle)->fd;
@@ -2570,6 +2579,7 @@ const struct usbi_os_backend linux_usbfs_backend = {
 	.get_config_descriptor_by_value = op_get_config_descriptor_by_value,
 
 	.open = op_open,
+	.open_fd = op_open_fd,
 	.close = op_close,
 	.get_configuration = op_get_configuration,
 	.set_configuration = op_set_configuration,
diff --git a/third_party/libusb/src/libusb/os/openbsd_usb.c b/third_party/libusb/src/libusb/os/openbsd_usb.c
index 2997e53..2d24f2c 100644
--- a/third_party/libusb/src/libusb/os/openbsd_usb.c
+++ b/third_party/libusb/src/libusb/os/openbsd_usb.c
@@ -98,6 +98,7 @@ const struct usbi_os_backend openbsd_backend = {
 	obsd_get_device_list,
 	NULL,				/* hotplug_poll */
 	obsd_open,
+	NULL,       /* open_fd */
 	obsd_close,
 
 	obsd_get_device_descriptor,
diff --git a/third_party/libusb/src/libusb/os/wince_usb.c b/third_party/libusb/src/libusb/os/wince_usb.c
index 90c129b..c069c56 100644
--- a/third_party/libusb/src/libusb/os/wince_usb.c
+++ b/third_party/libusb/src/libusb/os/wince_usb.c
@@ -990,6 +990,7 @@ const struct usbi_os_backend wince_backend = {
         wince_get_device_list,
 	NULL,				/* hotplug_poll */
         wince_open,
+        NULL, /* open_fd */
         wince_close,
 
         wince_get_device_descriptor,
diff --git a/third_party/libusb/src/libusb/os/windows_usb.c b/third_party/libusb/src/libusb/os/windows_usb.c
index 4469992..bc4def6 100644
--- a/third_party/libusb/src/libusb/os/windows_usb.c
+++ b/third_party/libusb/src/libusb/os/windows_usb.c
@@ -2290,6 +2290,7 @@ const struct usbi_os_backend windows_backend = {
 	windows_get_device_list,
 	NULL,				/* hotplug_poll */
 	windows_open,
+	NULL,       /* open_fd */
 	windows_close,
 
 	windows_get_device_descriptor,