1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include "qemu/osdep.h"
23#include "qemu/units.h"
24#include "hw/usb.h"
25#include "qemu/iov.h"
26#include "trace.h"
27
28static void usb_combined_packet_add(USBCombinedPacket *combined, USBPacket *p)
29{
30 qemu_iovec_concat(&combined->iov, &p->iov, 0, p->iov.size);
31 QTAILQ_INSERT_TAIL(&combined->packets, p, combined_entry);
32 p->combined = combined;
33}
34
35
36static void usb_combined_packet_remove(USBCombinedPacket *combined,
37 USBPacket *p)
38{
39 assert(p->combined == combined);
40 p->combined = NULL;
41 QTAILQ_REMOVE(&combined->packets, p, combined_entry);
42 if (QTAILQ_EMPTY(&combined->packets)) {
43 qemu_iovec_destroy(&combined->iov);
44 g_free(combined);
45 }
46}
47
48
49void usb_combined_input_packet_complete(USBDevice *dev, USBPacket *p)
50{
51 USBCombinedPacket *combined = p->combined;
52 USBEndpoint *ep = p->ep;
53 USBPacket *next;
54 int status, actual_length;
55 bool short_not_ok, done = false;
56
57 if (combined == NULL) {
58 usb_packet_complete_one(dev, p);
59 goto leave;
60 }
61
62 assert(combined->first == p && p == QTAILQ_FIRST(&combined->packets));
63
64 status = combined->first->status;
65 actual_length = combined->first->actual_length;
66 short_not_ok = QTAILQ_LAST(&combined->packets)->short_not_ok;
67
68 QTAILQ_FOREACH_SAFE(p, &combined->packets, combined_entry, next) {
69 if (!done) {
70
71 if (actual_length >= p->iov.size) {
72 p->actual_length = p->iov.size;
73 } else {
74
75 p->actual_length = actual_length;
76 done = true;
77 }
78
79 if (done || next == NULL) {
80 p->status = status;
81 } else {
82 p->status = USB_RET_SUCCESS;
83 }
84 p->short_not_ok = short_not_ok;
85
86 usb_combined_packet_remove(combined, p);
87 usb_packet_complete_one(dev, p);
88 actual_length -= p->actual_length;
89 } else {
90
91 p->status = USB_RET_REMOVE_FROM_QUEUE;
92
93 dev->port->ops->complete(dev->port, p);
94 }
95 }
96
97leave:
98
99 usb_ep_combine_input_packets(ep);
100}
101
102
103void usb_combined_packet_cancel(USBDevice *dev, USBPacket *p)
104{
105 USBCombinedPacket *combined = p->combined;
106 assert(combined != NULL);
107 USBPacket *first = p->combined->first;
108
109
110 usb_combined_packet_remove(combined, p);
111 if (p == first) {
112 usb_device_cancel_packet(dev, p);
113 }
114}
115
116
117
118
119
120
121
122void usb_ep_combine_input_packets(USBEndpoint *ep)
123{
124 USBPacket *p, *u, *next, *prev = NULL, *first = NULL;
125 USBPort *port = ep->dev->port;
126 int totalsize;
127
128 assert(ep->pipeline);
129 assert(ep->pid == USB_TOKEN_IN);
130
131 QTAILQ_FOREACH_SAFE(p, &ep->queue, queue, next) {
132
133 if (ep->halted) {
134 p->status = USB_RET_REMOVE_FROM_QUEUE;
135 port->ops->complete(port, p);
136 continue;
137 }
138
139
140 if (p->state == USB_PACKET_ASYNC) {
141 prev = p;
142 continue;
143 }
144 usb_packet_check_state(p, USB_PACKET_QUEUED);
145
146
147
148
149
150
151 if (prev && prev->short_not_ok) {
152 break;
153 }
154
155 if (first) {
156 if (first->combined == NULL) {
157 USBCombinedPacket *combined = g_new0(USBCombinedPacket, 1);
158
159 combined->first = first;
160 QTAILQ_INIT(&combined->packets);
161 qemu_iovec_init(&combined->iov, 2);
162 usb_combined_packet_add(combined, first);
163 }
164 usb_combined_packet_add(first->combined, p);
165 } else {
166 first = p;
167 }
168
169
170 totalsize = (p->combined) ? p->combined->iov.size : p->iov.size;
171 if ((p->iov.size % ep->max_packet_size) != 0 || !p->short_not_ok ||
172 next == NULL ||
173
174 (totalsize == (16 * KiB - 36) && p->int_req) ||
175
176 totalsize > 1 * MiB - ep->max_packet_size) {
177 usb_device_handle_data(ep->dev, first);
178 assert(first->status == USB_RET_ASYNC);
179 if (first->combined) {
180 QTAILQ_FOREACH(u, &first->combined->packets, combined_entry) {
181 usb_packet_set_state(u, USB_PACKET_ASYNC);
182 }
183 } else {
184 usb_packet_set_state(first, USB_PACKET_ASYNC);
185 }
186 first = NULL;
187 prev = p;
188 }
189 }
190}
191