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