]>
Commit | Line | Data |
---|---|---|
a552a966 HG |
1 | /* |
2 | * QEMU USB packet combining code (for input pipelining) | |
3 | * | |
4 | * Copyright(c) 2012 Red Hat, Inc. | |
5 | * | |
6 | * Red Hat Authors: | |
7 | * Hans de Goede <[email protected]> | |
8 | * | |
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 | |
75a49fc6 | 12 | * version 2.1 of the License, or (at your option) any later version. |
a552a966 HG |
13 | * |
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. | |
18 | * | |
75a49fc6 | 19 | * You should have received a copy of the GNU Lesser General Public License |
a552a966 HG |
20 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
21 | */ | |
e532b2e0 | 22 | #include "qemu/osdep.h" |
246e195b | 23 | #include "qemu/units.h" |
a552a966 HG |
24 | #include "qemu-common.h" |
25 | #include "hw/usb.h" | |
1de7afc9 | 26 | #include "qemu/iov.h" |
a552a966 HG |
27 | #include "trace.h" |
28 | ||
29 | static 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 | ||
ffd8a97f | 36 | /* Note will free combined when the last packet gets removed */ |
a552a966 HG |
37 | static 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); | |
ffd8a97f | 43 | if (QTAILQ_EMPTY(&combined->packets)) { |
0ca6db4f | 44 | qemu_iovec_destroy(&combined->iov); |
ffd8a97f HG |
45 | g_free(combined); |
46 | } | |
a552a966 HG |
47 | } |
48 | ||
49 | /* Also handles completion of non combined packets for pipelined input eps */ | |
50 | void usb_combined_input_packet_complete(USBDevice *dev, USBPacket *p) | |
51 | { | |
52 | USBCombinedPacket *combined = p->combined; | |
53 | USBEndpoint *ep = p->ep; | |
54 | USBPacket *next; | |
ffd8a97f HG |
55 | int status, actual_length; |
56 | bool short_not_ok, done = false; | |
a552a966 HG |
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 | ||
9a77a0f5 HG |
65 | status = combined->first->status; |
66 | actual_length = combined->first->actual_length; | |
eae3eb3e | 67 | short_not_ok = QTAILQ_LAST(&combined->packets)->short_not_ok; |
a552a966 HG |
68 | |
69 | QTAILQ_FOREACH_SAFE(p, &combined->packets, combined_entry, next) { | |
ffd8a97f | 70 | if (!done) { |
a552a966 | 71 | /* Distribute data over uncombined packets */ |
9a77a0f5 HG |
72 | if (actual_length >= p->iov.size) { |
73 | p->actual_length = p->iov.size; | |
a552a966 HG |
74 | } else { |
75 | /* Send short or error packet to complete the transfer */ | |
9a77a0f5 | 76 | p->actual_length = actual_length; |
ffd8a97f | 77 | done = true; |
a552a966 | 78 | } |
9a77a0f5 | 79 | /* Report status on the last packet */ |
ffd8a97f | 80 | if (done || next == NULL) { |
9a77a0f5 HG |
81 | p->status = status; |
82 | } else { | |
83 | p->status = USB_RET_SUCCESS; | |
84 | } | |
a552a966 | 85 | p->short_not_ok = short_not_ok; |
ffd8a97f | 86 | /* Note will free combined when the last packet gets removed! */ |
a552a966 HG |
87 | usb_combined_packet_remove(combined, p); |
88 | usb_packet_complete_one(dev, p); | |
9a77a0f5 | 89 | actual_length -= p->actual_length; |
a552a966 HG |
90 | } else { |
91 | /* Remove any leftover packets from the queue */ | |
9a77a0f5 | 92 | p->status = USB_RET_REMOVE_FROM_QUEUE; |
ffd8a97f | 93 | /* Note will free combined on the last packet! */ |
a552a966 HG |
94 | dev->port->ops->complete(dev->port, p); |
95 | } | |
96 | } | |
ffd8a97f | 97 | /* Do not use combined here, it has been freed! */ |
a552a966 HG |
98 | leave: |
99 | /* Check if there are packets in the queue waiting for our completion */ | |
100 | usb_ep_combine_input_packets(ep); | |
101 | } | |
102 | ||
103 | /* May only be called for combined packets! */ | |
104 | void usb_combined_packet_cancel(USBDevice *dev, USBPacket *p) | |
105 | { | |
106 | USBCombinedPacket *combined = p->combined; | |
107 | assert(combined != NULL); | |
ffd8a97f | 108 | USBPacket *first = p->combined->first; |
a552a966 | 109 | |
ffd8a97f | 110 | /* Note will free combined on the last packet! */ |
a552a966 | 111 | usb_combined_packet_remove(combined, p); |
ffd8a97f | 112 | if (p == first) { |
a552a966 HG |
113 | usb_device_cancel_packet(dev, p); |
114 | } | |
a552a966 HG |
115 | } |
116 | ||
117 | /* | |
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) | |
122 | */ | |
123 | void usb_ep_combine_input_packets(USBEndpoint *ep) | |
124 | { | |
125 | USBPacket *p, *u, *next, *prev = NULL, *first = NULL; | |
126 | USBPort *port = ep->dev->port; | |
9a77a0f5 | 127 | int totalsize; |
a552a966 HG |
128 | |
129 | assert(ep->pipeline); | |
130 | assert(ep->pid == USB_TOKEN_IN); | |
131 | ||
132 | QTAILQ_FOREACH_SAFE(p, &ep->queue, queue, next) { | |
133 | /* Empty the queue on a halt */ | |
134 | if (ep->halted) { | |
9a77a0f5 | 135 | p->status = USB_RET_REMOVE_FROM_QUEUE; |
a552a966 HG |
136 | port->ops->complete(port, p); |
137 | continue; | |
138 | } | |
139 | ||
140 | /* Skip packets already submitted to the device */ | |
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 | * 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. | |
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 | /* Is this packet the last one of a (combined) transfer? */ | |
579967be | 171 | totalsize = (p->combined) ? p->combined->iov.size : p->iov.size; |
a552a966 | 172 | if ((p->iov.size % ep->max_packet_size) != 0 || !p->short_not_ok || |
579967be HG |
173 | next == NULL || |
174 | /* Work around for Linux usbfs bulk splitting + migration */ | |
246e195b | 175 | (totalsize == (16 * KiB - 36) && p->int_req)) { |
9a77a0f5 HG |
176 | usb_device_handle_data(ep->dev, first); |
177 | assert(first->status == USB_RET_ASYNC); | |
a552a966 HG |
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 | } |