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