First Support on Ginger and OMAP TI
[linux-ginger.git] / drivers / staging / usbip / vhci_rx.c
blob7636d86c23881589346167506f098e055aa4cafa
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 usbip_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 usbip_uerr("cannot find a urb of seqnum %u\n",
81 pdu->base.seqnum);
82 usbip_uinfo("max seqnum %d\n",
83 atomic_read(&the_controller->seqnum));
84 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
85 return;
89 /* unpack the pdu to a urb */
90 usbip_pack_pdu(pdu, urb, USBIP_RET_SUBMIT, 0);
93 /* recv transfer buffer */
94 if (usbip_recv_xbuff(ud, urb) < 0)
95 return;
98 /* recv iso_packet_descriptor */
99 if (usbip_recv_iso(ud, urb) < 0)
100 return;
103 if (usbip_dbg_flag_vhci_rx)
104 usbip_dump_urb(urb);
107 usbip_dbg_vhci_rx("now giveback urb %p\n", urb);
109 spin_lock(&the_controller->lock);
110 usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
111 spin_unlock(&the_controller->lock);
113 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status);
116 usbip_dbg_vhci_rx("Leave\n");
118 return;
122 static struct vhci_unlink *dequeue_pending_unlink(struct vhci_device *vdev,
123 struct usbip_header *pdu)
125 struct vhci_unlink *unlink, *tmp;
127 spin_lock(&vdev->priv_lock);
129 list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) {
130 usbip_uinfo("unlink->seqnum %lu\n", unlink->seqnum);
131 if (unlink->seqnum == pdu->base.seqnum) {
132 usbip_dbg_vhci_rx("found pending unlink, %lu\n",
133 unlink->seqnum);
134 list_del(&unlink->list);
136 spin_unlock(&vdev->priv_lock);
137 return unlink;
141 spin_unlock(&vdev->priv_lock);
143 return NULL;
147 static void vhci_recv_ret_unlink(struct vhci_device *vdev,
148 struct usbip_header *pdu)
150 struct vhci_unlink *unlink;
151 struct urb *urb;
153 usbip_dump_header(pdu);
155 unlink = dequeue_pending_unlink(vdev, pdu);
156 if (!unlink) {
157 usbip_uinfo("cannot find the pending unlink %u\n",
158 pdu->base.seqnum);
159 return;
162 urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
163 if (!urb) {
165 * I get the result of a unlink request. But, it seems that I
166 * already received the result of its submit result and gave
167 * back the URB.
169 usbip_uinfo("the urb (seqnum %d) was already given backed\n",
170 pdu->base.seqnum);
171 } else {
172 usbip_dbg_vhci_rx("now giveback urb %p\n", urb);
174 /* If unlink is succeed, status is -ECONNRESET */
175 urb->status = pdu->u.ret_unlink.status;
176 usbip_uinfo("%d\n", urb->status);
178 spin_lock(&the_controller->lock);
179 usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
180 spin_unlock(&the_controller->lock);
182 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
183 urb->status);
186 kfree(unlink);
188 return;
191 /* recv a pdu */
192 static void vhci_rx_pdu(struct usbip_device *ud)
194 int ret;
195 struct usbip_header pdu;
196 struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
199 usbip_dbg_vhci_rx("Enter\n");
201 memset(&pdu, 0, sizeof(pdu));
204 /* 1. receive a pdu header */
205 ret = usbip_xmit(0, ud->tcp_socket, (char *) &pdu, sizeof(pdu), 0);
206 if (ret != sizeof(pdu)) {
207 usbip_uerr("receiving pdu failed! size is %d, should be %d\n",
208 ret, (unsigned int)sizeof(pdu));
209 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
210 return;
213 usbip_header_correct_endian(&pdu, 0);
215 if (usbip_dbg_flag_vhci_rx)
216 usbip_dump_header(&pdu);
218 switch (pdu.base.command) {
219 case USBIP_RET_SUBMIT:
220 vhci_recv_ret_submit(vdev, &pdu);
221 break;
222 case USBIP_RET_UNLINK:
223 vhci_recv_ret_unlink(vdev, &pdu);
224 break;
225 default:
226 /* NOTREACHED */
227 usbip_uerr("unknown pdu %u\n", pdu.base.command);
228 usbip_dump_header(&pdu);
229 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
234 /*-------------------------------------------------------------------------*/
236 void vhci_rx_loop(struct usbip_task *ut)
238 struct usbip_device *ud = container_of(ut, struct usbip_device, tcp_rx);
241 while (1) {
242 if (signal_pending(current)) {
243 usbip_dbg_vhci_rx("signal catched!\n");
244 break;
248 if (usbip_event_happened(ud))
249 break;
251 vhci_rx_pdu(ud);