]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/staging/usbip/vhci_rx.c
2d989c487f615d47f6bf2daa2882fbcdce9b709e
[karo-tx-linux.git] / drivers / staging / usbip / vhci_rx.c
1 /*
2  * Copyright (C) 2003-2008 Takahiro Hirofuchi
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include "usbip_common.h"
21 #include "vhci.h"
22
23
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)
27 {
28         struct vhci_priv *priv, *tmp;
29         struct urb *urb = NULL;
30         int status;
31
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;
36
37                         usbip_dbg_vhci_rx("find urb %p vurb %p seqnum %u\n",
38                                     urb, priv, seqnum);
39
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);
52                         }
53
54                         list_del(&priv->list);
55                         kfree(priv);
56                         urb->hcpriv = NULL;
57
58                         break;
59                 }
60         }
61
62         return urb;
63 }
64
65 static void vhci_recv_ret_submit(struct vhci_device *vdev,
66                                                 struct usbip_header *pdu)
67 {
68         struct usbip_device *ud = &vdev->ud;
69         struct urb *urb;
70
71         spin_lock(&vdev->priv_lock);
72
73         urb = pickup_urb_and_free_priv(vdev, pdu->base.seqnum);
74
75         spin_unlock(&vdev->priv_lock);
76
77         if (!urb) {
78                 usbip_uerr("cannot find a urb of seqnum %u\n",
79                                                         pdu->base.seqnum);
80                 usbip_uinfo("max seqnum %d\n",
81                                         atomic_read(&the_controller->seqnum));
82                 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
83                 return;
84         }
85
86
87         /* unpack the pdu to a urb */
88         usbip_pack_pdu(pdu, urb, USBIP_RET_SUBMIT, 0);
89
90
91         /* recv transfer buffer */
92         if (usbip_recv_xbuff(ud, urb) < 0)
93                 return;
94
95
96         /* recv iso_packet_descriptor */
97         if (usbip_recv_iso(ud, urb) < 0)
98                 return;
99
100
101         if (usbip_dbg_flag_vhci_rx)
102                 usbip_dump_urb(urb);
103
104
105         usbip_dbg_vhci_rx("now giveback urb %p\n", urb);
106
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);
110
111         usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status);
112
113
114         usbip_dbg_vhci_rx("Leave\n");
115
116         return;
117 }
118
119
120 static struct vhci_unlink *dequeue_pending_unlink(struct vhci_device *vdev,
121                 struct usbip_header *pdu)
122 {
123         struct vhci_unlink *unlink, *tmp;
124
125         spin_lock(&vdev->priv_lock);
126
127         list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) {
128                 usbip_uinfo("unlink->seqnum %lu\n", unlink->seqnum);
129                 if (unlink->seqnum == pdu->base.seqnum) {
130                         usbip_dbg_vhci_rx("found pending unlink, %lu\n",
131                                                         unlink->seqnum);
132                         list_del(&unlink->list);
133
134                         spin_unlock(&vdev->priv_lock);
135                         return unlink;
136                 }
137         }
138
139         spin_unlock(&vdev->priv_lock);
140
141         return NULL;
142 }
143
144
145 static void vhci_recv_ret_unlink(struct vhci_device *vdev,
146                                                 struct usbip_header *pdu)
147 {
148         struct vhci_unlink *unlink;
149         struct urb *urb;
150
151         usbip_dump_header(pdu);
152
153         unlink = dequeue_pending_unlink(vdev, pdu);
154         if (!unlink) {
155                 usbip_uinfo("cannot find the pending unlink %u\n",
156                                                         pdu->base.seqnum);
157                 return;
158         }
159
160         spin_lock(&vdev->priv_lock);
161
162         urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
163
164         spin_unlock(&vdev->priv_lock);
165
166         if (!urb) {
167                 /*
168                  * I get the result of a unlink request. But, it seems that I
169                  * already received the result of its submit result and gave
170                  * back the URB.
171                  */
172                 usbip_uinfo("the urb (seqnum %d) was already given backed\n",
173                                                         pdu->base.seqnum);
174         } else {
175                 usbip_dbg_vhci_rx("now giveback urb %p\n", urb);
176
177                 /* If unlink is succeed, status is -ECONNRESET */
178                 urb->status = pdu->u.ret_unlink.status;
179                 usbip_uinfo("%d\n", urb->status);
180
181                 spin_lock(&the_controller->lock);
182                 usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
183                 spin_unlock(&the_controller->lock);
184
185                 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
186                                                                 urb->status);
187         }
188
189         kfree(unlink);
190
191         return;
192 }
193
194 /* recv a pdu */
195 static void vhci_rx_pdu(struct usbip_device *ud)
196 {
197         int ret;
198         struct usbip_header pdu;
199         struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
200
201
202         usbip_dbg_vhci_rx("Enter\n");
203
204         memset(&pdu, 0, sizeof(pdu));
205
206
207         /* 1. receive a pdu header */
208         ret = usbip_xmit(0, ud->tcp_socket, (char *) &pdu, sizeof(pdu), 0);
209         if (ret != sizeof(pdu)) {
210                 usbip_uerr("receiving pdu failed! size is %d, should be %d\n",
211                                         ret, (unsigned int)sizeof(pdu));
212                 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
213                 return;
214         }
215
216         usbip_header_correct_endian(&pdu, 0);
217
218         if (usbip_dbg_flag_vhci_rx)
219                 usbip_dump_header(&pdu);
220
221         switch (pdu.base.command) {
222         case USBIP_RET_SUBMIT:
223                 vhci_recv_ret_submit(vdev, &pdu);
224                 break;
225         case USBIP_RET_UNLINK:
226                 vhci_recv_ret_unlink(vdev, &pdu);
227                 break;
228         default:
229                 /* NOTREACHED */
230                 usbip_uerr("unknown pdu %u\n", pdu.base.command);
231                 usbip_dump_header(&pdu);
232                 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
233         }
234 }
235
236
237 /*-------------------------------------------------------------------------*/
238
239 void vhci_rx_loop(struct usbip_task *ut)
240 {
241         struct usbip_device *ud = container_of(ut, struct usbip_device, tcp_rx);
242
243
244         while (1) {
245                 if (signal_pending(current)) {
246                         usbip_dbg_vhci_rx("signal catched!\n");
247                         break;
248                 }
249
250
251                 if (usbip_event_happened(ud))
252                         break;
253
254                 vhci_rx_pdu(ud);
255         }
256 }
257