sh: select ARCH_NO_SYSDEV_OPS.
[pandora-kernel.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
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 static int vhci_priv_tx_empty(struct vhci_device *vdev)
198 {
199         int empty = 0;
200
201         spin_lock(&vdev->priv_lock);
202
203         empty = list_empty(&vdev->priv_rx);
204
205         spin_unlock(&vdev->priv_lock);
206
207         return empty;
208 }
209
210 /* recv a pdu */
211 static void vhci_rx_pdu(struct usbip_device *ud)
212 {
213         int ret;
214         struct usbip_header pdu;
215         struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
216
217
218         usbip_dbg_vhci_rx("Enter\n");
219
220         memset(&pdu, 0, sizeof(pdu));
221
222         /* 1. receive a pdu header */
223         ret = usbip_xmit(0, ud->tcp_socket, (char *) &pdu, sizeof(pdu), 0);
224         if (ret < 0) {
225                 if (ret == -ECONNRESET)
226                         usbip_uinfo("connection reset by peer\n");
227                 else if (ret == -EAGAIN) {
228                         /* ignore if connection was idle */
229                         if (vhci_priv_tx_empty(vdev))
230                                 return;
231                         usbip_uinfo("connection timed out with pending urbs\n");
232                 } else if (ret != -ERESTARTSYS)
233                         usbip_uinfo("xmit failed %d\n", ret);
234
235                 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
236                 return;
237         }
238         if (ret == 0) {
239                 usbip_uinfo("connection closed");
240                 usbip_event_add(ud, VDEV_EVENT_DOWN);
241                 return;
242         }
243         if (ret != sizeof(pdu)) {
244                 usbip_uerr("received pdu size is %d, should be %d\n",
245                                         ret, (unsigned int)sizeof(pdu));
246                 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
247                 return;
248         }
249
250         usbip_header_correct_endian(&pdu, 0);
251
252         if (usbip_dbg_flag_vhci_rx)
253                 usbip_dump_header(&pdu);
254
255         switch (pdu.base.command) {
256         case USBIP_RET_SUBMIT:
257                 vhci_recv_ret_submit(vdev, &pdu);
258                 break;
259         case USBIP_RET_UNLINK:
260                 vhci_recv_ret_unlink(vdev, &pdu);
261                 break;
262         default:
263                 /* NOTREACHED */
264                 usbip_uerr("unknown pdu %u\n", pdu.base.command);
265                 usbip_dump_header(&pdu);
266                 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
267         }
268 }
269
270
271 /*-------------------------------------------------------------------------*/
272
273 int vhci_rx_loop(void *data)
274 {
275         struct usbip_device *ud = data;
276
277
278         while (!kthread_should_stop()) {
279                 if (usbip_event_happened(ud))
280                         break;
281
282                 vhci_rx_pdu(ud);
283         }
284
285         return 0;
286 }