2 * QEMU USB packet combining code (for input pipelining)
4 * Copyright(c) 2012 Red Hat, Inc.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or(at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "qemu-common.h"
27 static void usb_combined_packet_add(USBCombinedPacket *combined, USBPacket *p)
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;
34 static void usb_combined_packet_remove(USBCombinedPacket *combined,
37 assert(p->combined == combined);
39 QTAILQ_REMOVE(&combined->packets, p, combined_entry);
42 /* Also handles completion of non combined packets for pipelined input eps */
43 void usb_combined_input_packet_complete(USBDevice *dev, USBPacket *p)
45 USBCombinedPacket *combined = p->combined;
46 USBEndpoint *ep = p->ep;
48 enum { completing, complete, leftover };
49 int status, actual_length, state = completing;
52 if (combined == NULL) {
53 usb_packet_complete_one(dev, p);
57 assert(combined->first == p && p == QTAILQ_FIRST(&combined->packets));
59 status = combined->first->status;
60 actual_length = combined->first->actual_length;
61 short_not_ok = QTAILQ_LAST(&combined->packets, packets_head)->short_not_ok;
63 QTAILQ_FOREACH_SAFE(p, &combined->packets, combined_entry, next) {
64 if (state == completing) {
65 /* Distribute data over uncombined packets */
66 if (actual_length >= p->iov.size) {
67 p->actual_length = p->iov.size;
69 /* Send short or error packet to complete the transfer */
70 p->actual_length = actual_length;
73 /* Report status on the last packet */
74 if (state == complete || next == NULL) {
77 p->status = USB_RET_SUCCESS;
79 p->short_not_ok = short_not_ok;
80 usb_combined_packet_remove(combined, p);
81 usb_packet_complete_one(dev, p);
82 actual_length -= p->actual_length;
84 /* Remove any leftover packets from the queue */
86 p->status = USB_RET_REMOVE_FROM_QUEUE;
87 dev->port->ops->complete(dev->port, p);
91 * If we had leftover packets the hcd driver will have cancelled them
92 * and usb_combined_packet_cancel has already freed combined!
94 if (state != leftover) {
98 /* Check if there are packets in the queue waiting for our completion */
99 usb_ep_combine_input_packets(ep);
102 /* May only be called for combined packets! */
103 void usb_combined_packet_cancel(USBDevice *dev, USBPacket *p)
105 USBCombinedPacket *combined = p->combined;
106 assert(combined != NULL);
108 usb_combined_packet_remove(combined, p);
109 if (p == combined->first) {
110 usb_device_cancel_packet(dev, p);
112 if (QTAILQ_EMPTY(&combined->packets)) {
118 * Large input transfers can get split into multiple input packets, this
119 * function recombines them, removing the short_not_ok checks which all but
120 * the last packet of such splits transfers have, thereby allowing input
121 * transfer pipelining (which we cannot do on short_not_ok transfers)
123 void usb_ep_combine_input_packets(USBEndpoint *ep)
125 USBPacket *p, *u, *next, *prev = NULL, *first = NULL;
126 USBPort *port = ep->dev->port;
129 assert(ep->pipeline);
130 assert(ep->pid == USB_TOKEN_IN);
132 QTAILQ_FOREACH_SAFE(p, &ep->queue, queue, next) {
133 /* Empty the queue on a halt */
135 p->status = USB_RET_REMOVE_FROM_QUEUE;
136 port->ops->complete(port, p);
140 /* Skip packets already submitted to the device */
141 if (p->state == USB_PACKET_ASYNC) {
145 usb_packet_check_state(p, USB_PACKET_QUEUED);
148 * If the previous (combined) packet has the short_not_ok flag set
149 * stop, as we must not submit packets to the device after a transfer
150 * ending with short_not_ok packet.
152 if (prev && prev->short_not_ok) {
157 if (first->combined == NULL) {
158 USBCombinedPacket *combined = g_new0(USBCombinedPacket, 1);
160 combined->first = first;
161 QTAILQ_INIT(&combined->packets);
162 qemu_iovec_init(&combined->iov, 2);
163 usb_combined_packet_add(combined, first);
165 usb_combined_packet_add(first->combined, p);
170 /* Is this packet the last one of a (combined) transfer? */
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 ||
174 /* Work around for Linux usbfs bulk splitting + migration */
175 (totalsize == 16348 && 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);
183 usb_packet_set_state(first, USB_PACKET_ASYNC);