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