8ed52063edb88cf4ba0a2b860989b371a25233df
[firefly-linux-kernel-4.4.55.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         /* restore the padding in iso packets */
101         if (usbip_pad_iso(ud, urb) < 0)
102                 return;
103
104         if (usbip_dbg_flag_vhci_rx)
105                 usbip_dump_urb(urb);
106
107
108         usbip_dbg_vhci_rx("now giveback urb %p\n", urb);
109
110         spin_lock(&the_controller->lock);
111         usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
112         spin_unlock(&the_controller->lock);
113
114         usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status);
115
116
117         usbip_dbg_vhci_rx("Leave\n");
118
119         return;
120 }
121
122
123 static struct vhci_unlink *dequeue_pending_unlink(struct vhci_device *vdev,
124                 struct usbip_header *pdu)
125 {
126         struct vhci_unlink *unlink, *tmp;
127
128         spin_lock(&vdev->priv_lock);
129
130         list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) {
131                 usbip_uinfo("unlink->seqnum %lu\n", unlink->seqnum);
132                 if (unlink->seqnum == pdu->base.seqnum) {
133                         usbip_dbg_vhci_rx("found pending unlink, %lu\n",
134                                                         unlink->seqnum);
135                         list_del(&unlink->list);
136
137                         spin_unlock(&vdev->priv_lock);
138                         return unlink;
139                 }
140         }
141
142         spin_unlock(&vdev->priv_lock);
143
144         return NULL;
145 }
146
147
148 static void vhci_recv_ret_unlink(struct vhci_device *vdev,
149                                                 struct usbip_header *pdu)
150 {
151         struct vhci_unlink *unlink;
152         struct urb *urb;
153
154         usbip_dump_header(pdu);
155
156         unlink = dequeue_pending_unlink(vdev, pdu);
157         if (!unlink) {
158                 usbip_uinfo("cannot find the pending unlink %u\n",
159                                                         pdu->base.seqnum);
160                 return;
161         }
162
163         spin_lock(&vdev->priv_lock);
164
165         urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
166
167         spin_unlock(&vdev->priv_lock);
168
169         if (!urb) {
170                 /*
171                  * I get the result of a unlink request. But, it seems that I
172                  * already received the result of its submit result and gave
173                  * back the URB.
174                  */
175                 usbip_uinfo("the urb (seqnum %d) was already given backed\n",
176                                                         pdu->base.seqnum);
177         } else {
178                 usbip_dbg_vhci_rx("now giveback urb %p\n", urb);
179
180                 /* If unlink is succeed, status is -ECONNRESET */
181                 urb->status = pdu->u.ret_unlink.status;
182                 usbip_uinfo("%d\n", urb->status);
183
184                 spin_lock(&the_controller->lock);
185                 usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
186                 spin_unlock(&the_controller->lock);
187
188                 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
189                                                                 urb->status);
190         }
191
192         kfree(unlink);
193
194         return;
195 }
196
197 /* recv a pdu */
198 static void vhci_rx_pdu(struct usbip_device *ud)
199 {
200         int ret;
201         struct usbip_header pdu;
202         struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
203
204
205         usbip_dbg_vhci_rx("Enter\n");
206
207         memset(&pdu, 0, sizeof(pdu));
208
209
210         /* 1. receive a pdu header */
211         ret = usbip_xmit(0, ud->tcp_socket, (char *) &pdu, sizeof(pdu), 0);
212         if (ret != sizeof(pdu)) {
213                 usbip_uerr("receiving pdu failed! size is %d, should be %d\n",
214                                         ret, (unsigned int)sizeof(pdu));
215                 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
216                 return;
217         }
218
219         usbip_header_correct_endian(&pdu, 0);
220
221         if (usbip_dbg_flag_vhci_rx)
222                 usbip_dump_header(&pdu);
223
224         switch (pdu.base.command) {
225         case USBIP_RET_SUBMIT:
226                 vhci_recv_ret_submit(vdev, &pdu);
227                 break;
228         case USBIP_RET_UNLINK:
229                 vhci_recv_ret_unlink(vdev, &pdu);
230                 break;
231         default:
232                 /* NOTREACHED */
233                 usbip_uerr("unknown pdu %u\n", pdu.base.command);
234                 usbip_dump_header(&pdu);
235                 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
236         }
237 }
238
239
240 /*-------------------------------------------------------------------------*/
241
242 void vhci_rx_loop(struct usbip_task *ut)
243 {
244         struct usbip_device *ud = container_of(ut, struct usbip_device, tcp_rx);
245
246
247         while (1) {
248                 if (signal_pending(current)) {
249                         usbip_dbg_vhci_rx("signal catched!\n");
250                         break;
251                 }
252
253
254                 if (usbip_event_happened(ud))
255                         break;
256
257                 vhci_rx_pdu(ud);
258         }
259 }
260