Staging: usbip: fix build warning on 64bit kernels
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / usbip / vhci_rx.c
blob58e3995d0e2ce169ba9bea352b97271315ca9cf4
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 */
25 static 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 spin_lock(&vdev->priv_lock);
34 list_for_each_entry_safe(priv, tmp, &vdev->priv_rx, list) {
35 if (priv->seqnum == seqnum) {
36 urb = priv->urb;
37 status = urb->status;
39 dbg_vhci_rx("find urb %p vurb %p seqnum %u\n",
40 urb, priv, seqnum);
42 /* TODO: fix logic here to improve indent situtation */
43 if (status != -EINPROGRESS) {
44 if (status == -ENOENT ||
45 status == -ECONNRESET)
46 dev_info(&urb->dev->dev,
47 "urb %p was unlinked "
48 "%ssynchronuously.\n", urb,
49 status == -ENOENT ? "" : "a");
50 else
51 dev_info(&urb->dev->dev,
52 "urb %p may be in a error, "
53 "status %d\n", urb, status);
56 list_del(&priv->list);
57 kfree(priv);
58 urb->hcpriv = NULL;
60 break;
64 spin_unlock(&vdev->priv_lock);
66 return urb;
69 static void vhci_recv_ret_submit(struct vhci_device *vdev,
70 struct usbip_header *pdu)
72 struct usbip_device *ud = &vdev->ud;
73 struct urb *urb;
76 urb = pickup_urb_and_free_priv(vdev, pdu->base.seqnum);
79 if (!urb) {
80 uerr("cannot find a urb of seqnum %u\n", pdu->base.seqnum);
81 uinfo("max seqnum %d\n", atomic_read(&the_controller->seqnum));
82 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
83 return;
87 /* unpack the pdu to a urb */
88 usbip_pack_pdu(pdu, urb, USBIP_RET_SUBMIT, 0);
91 /* recv transfer buffer */
92 if (usbip_recv_xbuff(ud, urb) < 0)
93 return;
96 /* recv iso_packet_descriptor */
97 if (usbip_recv_iso(ud, urb) < 0)
98 return;
101 if (dbg_flag_vhci_rx)
102 usbip_dump_urb(urb);
105 dbg_vhci_rx("now giveback urb %p\n", urb);
107 spin_lock(&the_controller->lock);
108 usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
109 spin_unlock(&the_controller->lock);
111 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status);
114 dbg_vhci_rx("Leave\n");
116 return;
120 static struct vhci_unlink *dequeue_pending_unlink(struct vhci_device *vdev,
121 struct usbip_header *pdu)
123 struct vhci_unlink *unlink, *tmp;
125 spin_lock(&vdev->priv_lock);
127 list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) {
128 uinfo("unlink->seqnum %lu\n", unlink->seqnum);
129 if (unlink->seqnum == pdu->base.seqnum) {
130 dbg_vhci_rx("found pending unlink, %lu\n",
131 unlink->seqnum);
132 list_del(&unlink->list);
134 spin_unlock(&vdev->priv_lock);
135 return unlink;
139 spin_unlock(&vdev->priv_lock);
141 return NULL;
145 static void vhci_recv_ret_unlink(struct vhci_device *vdev,
146 struct usbip_header *pdu)
148 struct vhci_unlink *unlink;
149 struct urb *urb;
151 usbip_dump_header(pdu);
153 unlink = dequeue_pending_unlink(vdev, pdu);
154 if (!unlink) {
155 uinfo("cannot find the pending unlink %u\n", pdu->base.seqnum);
156 return;
159 urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
160 if (!urb) {
162 * I get the result of a unlink request. But, it seems that I
163 * already received the result of its submit result and gave
164 * back the URB.
166 uinfo("the urb (seqnum %d) was already given backed\n",
167 pdu->base.seqnum);
168 } else {
169 dbg_vhci_rx("now giveback urb %p\n", urb);
171 /* If unlink is succeed, status is -ECONNRESET */
172 urb->status = pdu->u.ret_unlink.status;
173 uinfo("%d\n", urb->status);
175 spin_lock(&the_controller->lock);
176 usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
177 spin_unlock(&the_controller->lock);
179 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
180 urb->status);
183 kfree(unlink);
185 return;
188 /* recv a pdu */
189 static void vhci_rx_pdu(struct usbip_device *ud)
191 int ret;
192 struct usbip_header pdu;
193 struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
196 dbg_vhci_rx("Enter\n");
198 memset(&pdu, 0, sizeof(pdu));
201 /* 1. receive a pdu header */
202 ret = usbip_xmit(0, ud->tcp_socket, (char *) &pdu, sizeof(pdu), 0);
203 if (ret != sizeof(pdu)) {
204 uerr("receiving pdu failed! size is %d, should be %d\n",
205 ret, (unsigned int)sizeof(pdu));
206 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
207 return;
210 usbip_header_correct_endian(&pdu, 0);
212 if (dbg_flag_vhci_rx)
213 usbip_dump_header(&pdu);
215 switch (pdu.base.command) {
216 case USBIP_RET_SUBMIT:
217 vhci_recv_ret_submit(vdev, &pdu);
218 break;
219 case USBIP_RET_UNLINK:
220 vhci_recv_ret_unlink(vdev, &pdu);
221 break;
222 default:
223 /* NOTREACHED */
224 uerr("unknown pdu %u\n", pdu.base.command);
225 usbip_dump_header(&pdu);
226 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
231 /*-------------------------------------------------------------------------*/
233 void vhci_rx_loop(struct usbip_task *ut)
235 struct usbip_device *ud = container_of(ut, struct usbip_device, tcp_rx);
238 while (1) {
239 if (signal_pending(current)) {
240 dbg_vhci_rx("signal catched!\n");
241 break;
245 if (usbip_event_happend(ud))
246 break;
248 vhci_rx_pdu(ud);