File size: 1,296 Bytes
8df6da4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*
 * Copyright (C) 2014, Red Hat Inc, Andrew Jones <drjones@redhat.com>
 *
 * This work is licensed under the terms of the GNU LGPL, version 2.
 */
#include "libcflat.h"
#include "virtio.h"
#include "asm/spinlock.h"

#define TESTDEV_NAME "chr-testdev"

static struct virtio_device *vcon;
static struct virtqueue *in_vq, *out_vq;
static struct spinlock lock;

static void __testdev_send(char *buf, unsigned int len)
{
	int ret;

	ret = virtqueue_add_outbuf(out_vq, buf, len);
	virtqueue_kick(out_vq);

	if (ret < 0)
		return;

	while (!virtqueue_get_buf(out_vq, &len))
		;
}

void chr_testdev_exit(int code)
{
	unsigned int len;
	char buf[8];

	snprintf(buf, sizeof(buf), "%dq", code);
	len = strlen(buf);

	spin_lock(&lock);

	if (!vcon)
		goto out;

	__testdev_send(buf, len);

out:
	spin_unlock(&lock);
}

void chr_testdev_init(void)
{
	const char *io_names[] = { "input", "output" };
	struct virtqueue *vqs[2];
	int ret;

	vcon = virtio_bind(VIRTIO_ID_CONSOLE);
	if (vcon == NULL) {
		printf("%s: %s: can't find a virtio-console\n",
				__func__, TESTDEV_NAME);
		return;
	}

	ret = vcon->config->find_vqs(vcon, 2, vqs, NULL, io_names);
	if (ret < 0) {
		printf("%s: %s: can't init virtqueues\n",
				__func__, TESTDEV_NAME);
		vcon = NULL;
		return;
	}

	in_vq = vqs[0];
	out_vq = vqs[1];
}