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