usb/combined-packet: Move freeing of combined to usb_combined_packet_remove()

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
This commit is contained in:
Hans de Goede 2012-11-01 17:15:06 +01:00 committed by Gerd Hoffmann
parent 9b8251c5c4
commit ffd8a97fb3
1 changed files with 15 additions and 18 deletions

View File

@ -31,12 +31,16 @@ static void usb_combined_packet_add(USBCombinedPacket *combined, USBPacket *p)
p->combined = combined; p->combined = combined;
} }
/* Note will free combined when the last packet gets removed */
static void usb_combined_packet_remove(USBCombinedPacket *combined, static void usb_combined_packet_remove(USBCombinedPacket *combined,
USBPacket *p) USBPacket *p)
{ {
assert(p->combined == combined); assert(p->combined == combined);
p->combined = NULL; p->combined = NULL;
QTAILQ_REMOVE(&combined->packets, p, combined_entry); QTAILQ_REMOVE(&combined->packets, p, combined_entry);
if (QTAILQ_EMPTY(&combined->packets)) {
g_free(combined);
}
} }
/* Also handles completion of non combined packets for pipelined input eps */ /* Also handles completion of non combined packets for pipelined input eps */
@ -45,9 +49,8 @@ void usb_combined_input_packet_complete(USBDevice *dev, USBPacket *p)
USBCombinedPacket *combined = p->combined; USBCombinedPacket *combined = p->combined;
USBEndpoint *ep = p->ep; USBEndpoint *ep = p->ep;
USBPacket *next; USBPacket *next;
enum { completing, complete, leftover }; int status, actual_length;
int status, actual_length, state = completing; bool short_not_ok, done = false;
bool short_not_ok;
if (combined == NULL) { if (combined == NULL) {
usb_packet_complete_one(dev, p); usb_packet_complete_one(dev, p);
@ -61,39 +64,34 @@ void usb_combined_input_packet_complete(USBDevice *dev, USBPacket *p)
short_not_ok = QTAILQ_LAST(&combined->packets, packets_head)->short_not_ok; short_not_ok = QTAILQ_LAST(&combined->packets, packets_head)->short_not_ok;
QTAILQ_FOREACH_SAFE(p, &combined->packets, combined_entry, next) { QTAILQ_FOREACH_SAFE(p, &combined->packets, combined_entry, next) {
if (state == completing) { if (!done) {
/* Distribute data over uncombined packets */ /* Distribute data over uncombined packets */
if (actual_length >= p->iov.size) { if (actual_length >= p->iov.size) {
p->actual_length = p->iov.size; p->actual_length = p->iov.size;
} else { } else {
/* Send short or error packet to complete the transfer */ /* Send short or error packet to complete the transfer */
p->actual_length = actual_length; p->actual_length = actual_length;
state = complete; done = true;
} }
/* Report status on the last packet */ /* Report status on the last packet */
if (state == complete || next == NULL) { if (done || next == NULL) {
p->status = status; p->status = status;
} else { } else {
p->status = USB_RET_SUCCESS; p->status = USB_RET_SUCCESS;
} }
p->short_not_ok = short_not_ok; p->short_not_ok = short_not_ok;
/* Note will free combined when the last packet gets removed! */
usb_combined_packet_remove(combined, p); usb_combined_packet_remove(combined, p);
usb_packet_complete_one(dev, p); usb_packet_complete_one(dev, p);
actual_length -= p->actual_length; actual_length -= p->actual_length;
} else { } else {
/* Remove any leftover packets from the queue */ /* Remove any leftover packets from the queue */
state = leftover;
p->status = USB_RET_REMOVE_FROM_QUEUE; p->status = USB_RET_REMOVE_FROM_QUEUE;
/* Note will free combined on the last packet! */
dev->port->ops->complete(dev->port, p); dev->port->ops->complete(dev->port, p);
} }
} }
/* /* Do not use combined here, it has been freed! */
* If we had leftover packets the hcd driver will have cancelled them
* and usb_combined_packet_cancel has already freed combined!
*/
if (state != leftover) {
g_free(combined);
}
leave: leave:
/* Check if there are packets in the queue waiting for our completion */ /* Check if there are packets in the queue waiting for our completion */
usb_ep_combine_input_packets(ep); usb_ep_combine_input_packets(ep);
@ -104,14 +102,13 @@ void usb_combined_packet_cancel(USBDevice *dev, USBPacket *p)
{ {
USBCombinedPacket *combined = p->combined; USBCombinedPacket *combined = p->combined;
assert(combined != NULL); assert(combined != NULL);
USBPacket *first = p->combined->first;
/* Note will free combined on the last packet! */
usb_combined_packet_remove(combined, p); usb_combined_packet_remove(combined, p);
if (p == combined->first) { if (p == first) {
usb_device_cancel_packet(dev, p); usb_device_cancel_packet(dev, p);
} }
if (QTAILQ_EMPTY(&combined->packets)) {
g_free(combined);
}
} }
/* /*