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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
Copyright 2010-2011 Red Hat, Inc.
Red Hat Authors:
Hans de Goede <hdegoede@redhat.com>
Richard Hughes <rhughes@redhat.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#ifdef USE_USBREDIR
#include <usbredirhost.h>
#include <gusb/gusb-context-private.h>
#include <gusb/gusb-device-private.h>
#include <gusb/gusb-util.h>
#include "channel-usbredir-priv.h"
#endif
#include "spice-client.h"
#include "spice-common.h"
#include "spice-channel-priv.h"
/**
* SECTION:channel-usbredir
* @short_description: usb redirection
* @title: USB Redirection Channel
* @section_id:
* @stability: API Stable (channel in development)
* @include: channel-usbredir.h
*
* The Spice protocol defines a set of messages to redirect USB devices
* from the Spice client to the VM. This channel handles these messages.
*/
#ifdef USE_USBREDIR
#define SPICE_USBREDIR_CHANNEL_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE((obj), SPICE_TYPE_USBREDIR_CHANNEL, SpiceUsbredirChannelPrivate))
struct _SpiceUsbredirChannelPrivate {
GUsbContext *context;
GUsbDevice *device;
struct usbredirhost *host;
/* To catch usbredirhost error messages and report them as a GError */
GError **catch_error;
/* Data passed from channel handle msg to the usbredirhost read cb */
const uint8_t *read_buf;
int read_buf_size;
SpiceMsgOut *msg_out;
gboolean up;
};
static void spice_usbredir_handle_msg(SpiceChannel *channel, SpiceMsgIn *msg);
static void spice_usbredir_channel_up(SpiceChannel *channel);
static void spice_usbredir_channel_dispose(GObject *obj);
static void usbredir_handle_msg(SpiceChannel *channel, SpiceMsgIn *in);
static void usbredir_log(void *user_data, int level, const char *msg);
static int usbredir_read_callback(void *user_data, uint8_t *data, int count);
static int usbredir_write_callback(void *user_data, uint8_t *data, int count);
#endif
G_DEFINE_TYPE(SpiceUsbredirChannel, spice_usbredir_channel, SPICE_TYPE_CHANNEL)
/* ------------------------------------------------------------------ */
static void spice_usbredir_channel_init(SpiceUsbredirChannel *channel)
{
#ifdef USE_USBREDIR
channel->priv = SPICE_USBREDIR_CHANNEL_GET_PRIVATE(channel);
#endif
}
static void spice_usbredir_channel_class_init(SpiceUsbredirChannelClass *klass)
{
#ifdef USE_USBREDIR
GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
SpiceChannelClass *channel_class = SPICE_CHANNEL_CLASS(klass);
gobject_class->dispose = spice_usbredir_channel_dispose;
channel_class->handle_msg = spice_usbredir_handle_msg;
channel_class->channel_up = spice_usbredir_channel_up;
g_type_class_add_private(klass, sizeof(SpiceUsbredirChannelPrivate));
#endif
}
#ifdef USE_USBREDIR
static void spice_usbredir_channel_dispose(GObject *obj)
{
SpiceUsbredirChannel *channel = SPICE_USBREDIR_CHANNEL(obj);
spice_usbredir_channel_disconnect(channel);
/* Chain up to the parent class */
if (G_OBJECT_CLASS(spice_usbredir_channel_parent_class)->dispose)
G_OBJECT_CLASS(spice_usbredir_channel_parent_class)->dispose(obj);
}
static const spice_msg_handler usbredir_handlers[] = {
[ SPICE_MSG_SPICEVMC_DATA ] = usbredir_handle_msg,
};
/* ------------------------------------------------------------------ */
/* private api */
static gboolean spice_usbredir_channel_open_device(
SpiceUsbredirChannel *channel, GError **err)
{
SpiceUsbredirChannelPrivate *priv = channel->priv;
libusb_device_handle *handle = NULL;
int rc;
rc = libusb_open(_g_usb_device_get_device(priv->device), &handle);
if (rc != 0) {
g_set_error(err, SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,
"Could not open usb device: %s [%i]",
gusb_strerror(rc), rc);
return FALSE;
}
priv->catch_error = err;
priv->host = usbredirhost_open(_g_usb_context_get_context(priv->context),
handle, usbredir_log,
usbredir_read_callback,
usbredir_write_callback,
channel, PACKAGE_STRING,
spice_util_get_debug() ? usbredirparser_debug : usbredirparser_warning,
usbredirhost_fl_write_cb_owns_buffer);
priv->catch_error = NULL;
if (!priv->host) {
g_return_val_if_fail(err == NULL || *err != NULL, FALSE);
return FALSE;
}
spice_channel_connect(SPICE_CHANNEL(channel));
return TRUE;
}
G_GNUC_INTERNAL
void spice_usbredir_channel_connect_async(SpiceUsbredirChannel *channel,
GUsbContext *context,
GUsbDevice *device,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
SpiceUsbredirChannelPrivate *priv = channel->priv;
GSimpleAsyncResult *result;
GError *err = NULL;
g_return_if_fail(SPICE_IS_USBREDIR_CHANNEL(channel));
g_return_if_fail(context != NULL);
g_return_if_fail(device != NULL);
SPICE_DEBUG("connecting usb channel %p", channel);
result = g_simple_async_result_new(G_OBJECT(channel), callback, user_data,
spice_usbredir_channel_connect_async);
if (priv->device) {
g_simple_async_result_set_error(result,
SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,
"Error channel is busy");
goto done;
}
priv->context = g_object_ref(context);
priv->device = g_object_ref(device);
if (!spice_usbredir_channel_open_device(channel, &err)) {
g_simple_async_result_take_error(result, err);
g_clear_object(&priv->context);
g_clear_object(&priv->device);
}
done:
g_simple_async_result_complete_in_idle(result);
g_object_unref(result);
}
G_GNUC_INTERNAL
gboolean spice_usbredir_channel_connect_finish(SpiceUsbredirChannel *channel,
GAsyncResult *res,
GError **err)
{
GSimpleAsyncResult *result = G_SIMPLE_ASYNC_RESULT(res);
g_return_val_if_fail(g_simple_async_result_is_valid(res, G_OBJECT(channel),
spice_usbredir_channel_connect_async),
FALSE);
if (g_simple_async_result_propagate_error(result, err))
return FALSE;
return TRUE;
}
G_GNUC_INTERNAL
void spice_usbredir_channel_disconnect(SpiceUsbredirChannel *channel)
{
SpiceUsbredirChannelPrivate *priv = channel->priv;
SPICE_DEBUG("disconnecting usb channel %p", channel);
spice_channel_disconnect(SPICE_CHANNEL(channel), SPICE_CHANNEL_NONE);
priv->up = FALSE;
if (priv->host) {
/* This also closes the libusb handle we passed to its _open */
usbredirhost_close(priv->host);
priv->host = NULL;
g_clear_object(&priv->device);
g_clear_object(&priv->context);
}
}
G_GNUC_INTERNAL
GUsbDevice *spice_usbredir_channel_get_device(SpiceUsbredirChannel *channel)
{
return channel->priv->device;
}
G_GNUC_INTERNAL
void spice_usbredir_channel_do_write(SpiceUsbredirChannel *channel)
{
SpiceUsbredirChannelPrivate *priv = channel->priv;
/* No recursion allowed! */
g_return_if_fail(priv->msg_out == NULL);
if (!priv->up || !usbredirhost_has_data_to_write(priv->host))
return;
priv->msg_out = spice_msg_out_new(SPICE_CHANNEL(channel),
SPICE_MSGC_SPICEVMC_DATA);
/* Collect all pending writes in priv->msg_out->marshaller */
usbredirhost_write_guest_data(priv->host);
spice_msg_out_send(priv->msg_out);
spice_msg_out_unref(priv->msg_out);
priv->msg_out = NULL;
}
/* ------------------------------------------------------------------ */
/* callbacks (any context) */
static void usbredir_log(void *user_data, int level, const char *msg)
{
SpiceUsbredirChannel *channel = user_data;
SpiceUsbredirChannelPrivate *priv = channel->priv;
if (priv->catch_error && level == usbredirparser_error) {
SPICE_DEBUG("%s", msg);
g_set_error_literal(priv->catch_error, SPICE_CLIENT_ERROR,
SPICE_CLIENT_ERROR_FAILED, msg);
return;
}
switch (level) {
case usbredirparser_error:
g_critical("%s", msg); break;
case usbredirparser_warning:
g_warning("%s", msg); break;
default:
SPICE_DEBUG("%s", msg); break;
}
}
static int usbredir_read_callback(void *user_data, uint8_t *data, int count)
{
SpiceUsbredirChannel *channel = user_data;
SpiceUsbredirChannelPrivate *priv = channel->priv;
if (priv->read_buf_size < count) {
count = priv->read_buf_size;
}
memcpy(data, priv->read_buf, count);
priv->read_buf_size -= count;
if (priv->read_buf_size) {
priv->read_buf += count;
} else {
priv->read_buf = NULL;
}
return count;
}
static void usbredir_free_write_cb_data(uint8_t *data, void *user_data)
{
SpiceUsbredirChannel *channel = user_data;
SpiceUsbredirChannelPrivate *priv = channel->priv;
usbredirhost_free_write_buffer(priv->host, data);
}
static int usbredir_write_callback(void *user_data, uint8_t *data, int count)
{
SpiceUsbredirChannel *channel = user_data;
SpiceUsbredirChannelPrivate *priv = channel->priv;
spice_marshaller_add_ref_full(priv->msg_out->marshaller, data, count,
usbredir_free_write_cb_data, channel);
return count;
}
/* --------------------------------------------------------------------- */
/* coroutine context */
static void spice_usbredir_handle_msg(SpiceChannel *c, SpiceMsgIn *msg)
{
int type = spice_msg_in_type(msg);
SpiceChannelClass *parent_class;
g_return_if_fail(type < SPICE_N_ELEMENTS(usbredir_handlers));
parent_class = SPICE_CHANNEL_CLASS(spice_usbredir_channel_parent_class);
if (usbredir_handlers[type] != NULL)
usbredir_handlers[type](c, msg);
else if (parent_class->handle_msg)
parent_class->handle_msg(c, msg);
else
g_return_if_reached();
}
static void spice_usbredir_channel_up(SpiceChannel *c)
{
SpiceUsbredirChannel *channel = SPICE_USBREDIR_CHANNEL(c);
SpiceUsbredirChannelPrivate *priv = channel->priv;
priv->up = TRUE;
/* Flush any pending writes */
spice_usbredir_channel_do_write(channel);
}
static void usbredir_handle_msg(SpiceChannel *c, SpiceMsgIn *in)
{
SpiceUsbredirChannel *channel = SPICE_USBREDIR_CHANNEL(c);
SpiceUsbredirChannelPrivate *priv = channel->priv;
int size;
uint8_t *buf;
g_return_if_fail(priv->host != NULL);
/* No recursion allowed! */
g_return_if_fail(priv->read_buf == NULL);
buf = spice_msg_in_raw(in, &size);
priv->read_buf = buf;
priv->read_buf_size = size;
usbredirhost_read_guest_data(priv->host);
/* Send any acks, etc. which may be queued now */
spice_usbredir_channel_do_write(channel);
}
#endif /* USE_USBREDIR */
|