]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/usbip/vhci_rx.c
Merge branches 'dma-debug/next', 'amd-iommu/command-cleanups', 'amd-iommu/ats' and...
[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 <linux/slab.h>
21 #include <linux/kthread.h>
22
23 #include "usbip_common.h"
24 #include "vhci.h"
25
26
27 /* get URB from transmitted urb queue. caller must hold vdev->priv_lock */
28 struct urb *pickup_urb_and_free_priv(struct vhci_device *vdev,
29                                             __u32 seqnum)
30 {
31         struct vhci_priv *priv, *tmp;
32         struct urb *urb = NULL;
33         int status;
34
35         list_for_each_entry_safe(priv, tmp, &vdev->priv_rx, list) {
36                 if (priv->seqnum == seqnum) {
37                         urb = priv->urb;
38                         status = urb->status;
39
40                         usbip_dbg_vhci_rx("find urb %p vurb %p seqnum %u\n",
41                                     urb, priv, seqnum);
42
43                         /* TODO: fix logic here to improve indent situtation */
44                         if (status != -EINPROGRESS) {
45                                 if (status == -ENOENT ||
46                                      status == -ECONNRESET)
47                                         dev_info(&urb->dev->dev,
48                                                  "urb %p was unlinked "
49                                                  "%ssynchronuously.\n", urb,
50                                                  status == -ENOENT ? "" : "a");
51                                 else
52                                         dev_info(&urb->dev->dev,
53                                                  "urb %p may be in a error, "
54                                                  "status %d\n", urb, status);
55                         }
56
57                         list_del(&priv->list);
58                         kfree(priv);
59                         urb->hcpriv = NULL;
60
61                         break;
62                 }
63         }
64
65         return urb;
66 }
67
68 static void vhci_recv_ret_submit(struct vhci_device *vdev,
69                                                 struct usbip_header *pdu)
70 {
71         struct usbip_device *ud = &vdev->ud;
72         struct urb *urb;
73
74         spin_lock(&vdev->priv_lock);
75
76         urb = pickup_urb_and_free_priv(vdev, pdu->base.seqnum);
77
78         spin_unlock(&vdev->priv_lock);
79
80         if (!urb) {
81                 usbip_uerr("cannot find a urb of seqnum %u\n",
82                                                         pdu->base.seqnum);
83                 usbip_uinfo("max seqnum %d\n",
84                                         atomic_read(&the_controller->seqnum));
85                 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
86                 return;
87         }
88
89
90         /* unpack the pdu to a urb */
91         usbip_pack_pdu(pdu, urb, USBIP_RET_SUBMIT, 0);
92
93
94         /* recv transfer buffer */
95         if (usbip_recv_xbuff(ud, urb) < 0)
96                 return;
97
98
99         /* recv iso_packet_descriptor */
100         if (usbip_recv_iso(ud, urb) < 0)
101                 return;
102
103         /* restore the padding in iso packets */
104         if (usbip_pad_iso(ud, urb) < 0)
105                 return;
106
107         if (usbip_dbg_flag_vhci_rx)
108                 usbip_dump_urb(urb);
109
110
111         usbip_dbg_vhci_rx("now giveback urb %p\n", urb);
112
113         spin_lock(&the_controller->lock);
114         usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
115         spin_unlock(&the_controller->lock);
116
117         usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status);
118
119
120         usbip_dbg_vhci_rx("Leave\n");
121
122         return;
123 }
124
125
126 static struct vhci_unlink *dequeue_pending_unlink(struct vhci_device *vdev,
127                 struct usbip_header *pdu)
128 {
129         struct vhci_unlink *unlink, *tmp;
130
131         spin_lock(&vdev->priv_lock);
132
133         list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) {
134                 usbip_uinfo("unlink->seqnum %lu\n", unlink->seqnum);
135                 if (unlink->seqnum == pdu->base.seqnum) {
136                         usbip_dbg_vhci_rx("found pending unlink, %lu\n",
137                                                         unlink->seqnum);
138                         list_del(&unlink->list);
139
140                         spin_unlock(&vdev->priv_lock);
141                         return unlink;
142                 }
143         }
144
145         spin_unlock(&vdev->priv_lock);
146
147         return NULL;
148 }
149
150
151 static void vhci_recv_ret_unlink(struct vhci_device *vdev,
152                                                 struct usbip_header *pdu)
153 {
154         struct vhci_unlink *unlink;
155         struct urb *urb;
156
157         usbip_dump_header(pdu);
158
159         unlink = dequeue_pending_unlink(vdev, pdu);
160         if (!unlink) {
161                 usbip_uinfo("cannot find the pending unlink %u\n",
162                                                         pdu->base.seqnum);
163                 return;
164         }
165
166         spin_lock(&vdev->priv_lock);
167
168         urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
169
170         spin_unlock(&vdev->priv_lock);
171
172         if (!urb) {
173                 /*
174                  * I get the result of a unlink request. But, it seems that I
175                  * already received the result of its submit result and gave
176                  * back the URB.
177                  */
178                 usbip_uinfo("the urb (seqnum %d) was already given backed\n",
179                                                         pdu->base.seqnum);
180         } else {
181                 usbip_dbg_vhci_rx("now giveback urb %p\n", urb);
182
183                 /* If unlink is succeed, status is -ECONNRESET */
184                 urb->status = pdu->u.ret_unlink.status;
185                 usbip_uinfo("%d\n", urb->status);
186
187                 spin_lock(&the_controller->lock);
188                 usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
189                 spin_unlock(&the_controller->lock);
190
191                 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
192                                                                 urb->status);
193         }
194
195         kfree(unlink);
196
197         return;
198 }
199
200 static int vhci_priv_tx_empty(struct vhci_device *vdev)
201 {
202         int empty = 0;
203
204         spin_lock(&vdev->priv_lock);
205
206         empty = list_empty(&vdev->priv_rx);
207
208         spin_unlock(&vdev->priv_lock);
209
210         return empty;
211 }
212
213 /* recv a pdu */
214 static void vhci_rx_pdu(struct usbip_device *ud)
215 {
216         int ret;
217         struct usbip_header pdu;
218         struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
219
220
221         usbip_dbg_vhci_rx("Enter\n");
222
223         memset(&pdu, 0, sizeof(pdu));
224
225         /* 1. receive a pdu header */
226         ret = usbip_xmit(0, ud->tcp_socket, (char *) &pdu, sizeof(pdu), 0);
227         if (ret < 0) {
228                 if (ret == -ECONNRESET)
229                         usbip_uinfo("connection reset by peer\n");
230                 else if (ret == -EAGAIN) {
231                         /* ignore if connection was idle */
232                         if (vhci_priv_tx_empty(vdev))
233                                 return;
234                         usbip_uinfo("connection timed out with pending urbs\n");
235                 } else if (ret != -ERESTARTSYS)
236                         usbip_uinfo("xmit failed %d\n", ret);
237
238                 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
239                 return;
240         }
241         if (ret == 0) {
242                 usbip_uinfo("connection closed");
243                 usbip_event_add(ud, VDEV_EVENT_DOWN);
244                 return;
245         }
246         if (ret != sizeof(pdu)) {
247                 usbip_uerr("received pdu size is %d, should be %d\n",
248                                         ret, (unsigned int)sizeof(pdu));
249                 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
250                 return;
251         }
252
253         usbip_header_correct_endian(&pdu, 0);
254
255         if (usbip_dbg_flag_vhci_rx)
256                 usbip_dump_header(&pdu);
257
258         switch (pdu.base.command) {
259         case USBIP_RET_SUBMIT:
260                 vhci_recv_ret_submit(vdev, &pdu);
261                 break;
262         case USBIP_RET_UNLINK:
263                 vhci_recv_ret_unlink(vdev, &pdu);
264                 break;
265         default:
266                 /* NOTREACHED */
267                 usbip_uerr("unknown pdu %u\n", pdu.base.command);
268                 usbip_dump_header(&pdu);
269                 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
270         }
271 }
272
273
274 /*-------------------------------------------------------------------------*/
275
276 int vhci_rx_loop(void *data)
277 {
278         struct usbip_device *ud = data;
279
280
281         while (!kthread_should_stop()) {
282                 if (usbip_event_happened(ud))
283                         break;
284
285                 vhci_rx_pdu(ud);
286         }
287
288         return 0;
289 }