staging: usbip: bugfix for deadlock
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / usbip / vhci_rx.c
blob7fd76fedde8bb165632f0a92b5c48450f43080ef
1 /*
2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
20 #include "usbip_common.h"
21 #include "vhci.h"
24 /* get URB from transmitted urb queue. caller must hold vdev->priv_lock */
25 struct urb *pickup_urb_and_free_priv(struct vhci_device *vdev,
26 __u32 seqnum)
28 struct vhci_priv *priv, *tmp;
29 struct urb *urb = NULL;
30 int status;
32 list_for_each_entry_safe(priv, tmp, &vdev->priv_rx, list) {
33 if (priv->seqnum == seqnum) {
34 urb = priv->urb;
35 status = urb->status;
37 usbip_dbg_vhci_rx("find urb %p vurb %p seqnum %u\n",
38 urb, priv, seqnum);
40 /* TODO: fix logic here to improve indent situtation */
41 if (status != -EINPROGRESS) {
42 if (status == -ENOENT ||
43 status == -ECONNRESET)
44 dev_info(&urb->dev->dev,
45 "urb %p was unlinked "
46 "%ssynchronuously.\n", urb,
47 status == -ENOENT ? "" : "a");
48 else
49 dev_info(&urb->dev->dev,
50 "urb %p may be in a error, "
51 "status %d\n", urb, status);
54 list_del(&priv->list);
55 kfree(priv);
56 urb->hcpriv = NULL;
58 break;
62 return urb;
65 static void vhci_recv_ret_submit(struct vhci_device *vdev,
66 struct usbip_header *pdu)
68 struct usbip_device *ud = &vdev->ud;
69 struct urb *urb;
70 unsigned long flags;
72 spin_lock(&vdev->priv_lock);
74 urb = pickup_urb_and_free_priv(vdev, pdu->base.seqnum);
76 spin_unlock(&vdev->priv_lock);
78 if (!urb) {
79 usbip_uerr("cannot find a urb of seqnum %u\n",
80 pdu->base.seqnum);
81 usbip_uinfo("max seqnum %d\n",
82 atomic_read(&the_controller->seqnum));
83 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
84 return;
88 /* unpack the pdu to a urb */
89 usbip_pack_pdu(pdu, urb, USBIP_RET_SUBMIT, 0);
92 /* recv transfer buffer */
93 if (usbip_recv_xbuff(ud, urb) < 0)
94 return;
97 /* recv iso_packet_descriptor */
98 if (usbip_recv_iso(ud, urb) < 0)
99 return;
101 /* restore the padding in iso packets */
102 if (usbip_pad_iso(ud, urb) < 0)
103 return;
105 if (usbip_dbg_flag_vhci_rx)
106 usbip_dump_urb(urb);
109 usbip_dbg_vhci_rx("now giveback urb %p\n", urb);
111 spin_lock_irqsave(&the_controller->lock, flags);
112 usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
113 spin_unlock_irqrestore(&the_controller->lock, flags);
115 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status);
118 usbip_dbg_vhci_rx("Leave\n");
120 return;
124 static struct vhci_unlink *dequeue_pending_unlink(struct vhci_device *vdev,
125 struct usbip_header *pdu)
127 struct vhci_unlink *unlink, *tmp;
129 spin_lock(&vdev->priv_lock);
131 list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) {
132 usbip_uinfo("unlink->seqnum %lu\n", unlink->seqnum);
133 if (unlink->seqnum == pdu->base.seqnum) {
134 usbip_dbg_vhci_rx("found pending unlink, %lu\n",
135 unlink->seqnum);
136 list_del(&unlink->list);
138 spin_unlock(&vdev->priv_lock);
139 return unlink;
143 spin_unlock(&vdev->priv_lock);
145 return NULL;
149 static void vhci_recv_ret_unlink(struct vhci_device *vdev,
150 struct usbip_header *pdu)
152 struct vhci_unlink *unlink;
153 struct urb *urb;
154 unsigned long flags;
156 usbip_dump_header(pdu);
158 unlink = dequeue_pending_unlink(vdev, pdu);
159 if (!unlink) {
160 usbip_uinfo("cannot find the pending unlink %u\n",
161 pdu->base.seqnum);
162 return;
165 spin_lock(&vdev->priv_lock);
167 urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
169 spin_unlock(&vdev->priv_lock);
171 if (!urb) {
173 * I get the result of a unlink request. But, it seems that I
174 * already received the result of its submit result and gave
175 * back the URB.
177 usbip_uinfo("the urb (seqnum %d) was already given backed\n",
178 pdu->base.seqnum);
179 } else {
180 usbip_dbg_vhci_rx("now giveback urb %p\n", urb);
182 /* If unlink is succeed, status is -ECONNRESET */
183 urb->status = pdu->u.ret_unlink.status;
184 usbip_uinfo("%d\n", urb->status);
186 spin_lock_irqsave(&the_controller->lock, flags);
187 usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
188 spin_unlock_irqrestore(&the_controller->lock, flags);
190 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
191 urb->status);
194 kfree(unlink);
196 return;
199 /* recv a pdu */
200 static void vhci_rx_pdu(struct usbip_device *ud)
202 int ret;
203 struct usbip_header pdu;
204 struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
207 usbip_dbg_vhci_rx("Enter\n");
209 memset(&pdu, 0, sizeof(pdu));
212 /* 1. receive a pdu header */
213 ret = usbip_xmit(0, ud->tcp_socket, (char *) &pdu, sizeof(pdu), 0);
214 if (ret != sizeof(pdu)) {
215 usbip_uerr("receiving pdu failed! size is %d, should be %d\n",
216 ret, (unsigned int)sizeof(pdu));
217 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
218 return;
221 usbip_header_correct_endian(&pdu, 0);
223 if (usbip_dbg_flag_vhci_rx)
224 usbip_dump_header(&pdu);
226 switch (pdu.base.command) {
227 case USBIP_RET_SUBMIT:
228 vhci_recv_ret_submit(vdev, &pdu);
229 break;
230 case USBIP_RET_UNLINK:
231 vhci_recv_ret_unlink(vdev, &pdu);
232 break;
233 default:
234 /* NOTREACHED */
235 usbip_uerr("unknown pdu %u\n", pdu.base.command);
236 usbip_dump_header(&pdu);
237 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
242 /*-------------------------------------------------------------------------*/
244 void vhci_rx_loop(struct usbip_task *ut)
246 struct usbip_device *ud = container_of(ut, struct usbip_device, tcp_rx);
249 while (1) {
250 if (signal_pending(current)) {
251 usbip_dbg_vhci_rx("signal catched!\n");
252 break;
256 if (usbip_event_happened(ud))
257 break;
259 vhci_rx_pdu(ud);