summaryrefslogtreecommitdiff
path: root/chip/stm32/usb_dwc_stream.c
blob: 78e0a4b484068ef9d52941859112c5da76b82c2c (plain)
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
/* Copyright 2016 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "console.h"
#include "registers.h"
#include "timer.h"
#include "usb_dwc_stream.h"
#include "util.h"
#define CPRINTF(format, args...) cprintf(CC_USB, format, ##args)

/*
 * This function tries to shove new bytes from the USB host into the queue for
 * consumption elsewhere. It is invoked either by a HW interrupt (telling us we
 * have new bytes from the USB host), or by whoever is reading bytes out of the
 * other end of the queue (telling us that there's now more room in the queue
 * if we still have bytes to shove in there).
 */
int rx_stream_handler(struct usb_stream_config const *config)
{
	int rx_count = rx_ep_pending(config->endpoint);

	/* If we have some, try to shove them into the queue */
	if (rx_count) {
		size_t added = QUEUE_ADD_UNITS(config->producer.queue,
					       config->rx_ram, rx_count);
		if (added != rx_count) {
			CPRINTF("rx_stream_handler: failed ep%d "
				"queue %d bytes, accepted %d\n",
				config->endpoint, rx_count, added);
		}
	}

	if (!rx_ep_is_active(config->endpoint))
		usb_read_ep(config->endpoint, config->rx_size, config->rx_ram);

	return rx_count;
}

/* Try to send some bytes to the host */
int tx_stream_handler(struct usb_stream_config const *config)
{
	size_t count;

	if (!*(config->is_reset))
		return 0;
	if (!tx_ep_is_ready(config->endpoint))
		return 0;

	count = QUEUE_REMOVE_UNITS(config->consumer.queue, config->tx_ram,
				   config->tx_size);
	if (count)
		usb_write_ep(config->endpoint, count, config->tx_ram);

	return count;
}

/* Reset stream */
void usb_stream_event(struct usb_stream_config const *config,
		      enum usb_ep_event evt)
{
	if (evt != USB_EVENT_RESET)
		return;

	epN_reset(config->endpoint);

	*(config->is_reset) = 1;

	/* Flush any queued data */
	hook_call_deferred(config->deferred_tx, 0);
	hook_call_deferred(config->deferred_rx, 0);
}

static void usb_read(struct producer const *producer, size_t count)
{
	struct usb_stream_config const *config =
		DOWNCAST(producer, struct usb_stream_config, producer);

	hook_call_deferred(config->deferred_rx, 0);
}

static void usb_written(struct consumer const *consumer, size_t count)
{
	struct usb_stream_config const *config =
		DOWNCAST(consumer, struct usb_stream_config, consumer);

	hook_call_deferred(config->deferred_tx, 0);
}

struct producer_ops const usb_stream_producer_ops = {
	.read = usb_read,
};

struct consumer_ops const usb_stream_consumer_ops = {
	.written = usb_written,
};